Skip to content

Commit f3e2157

Browse files
committed
Returned WithIdAndVersion from find methods
1 parent 8049d19 commit f3e2157

File tree

8 files changed

+26
-22
lines changed

8 files changed

+26
-22
lines changed

samples/simple-ts/src/shim.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ console.log(anitaFromDb);
3333

3434
// Finding more
3535
const usersFromDB = await users.find({ age: { $lt: 40 } }).toArray();
36-
console.log(JSON.stringify(usersFromDB));
36+
console.log(usersFromDB);
3737

3838
await pongoClient.close();

samples/simple-ts/src/typedClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ await users.deleteOne({ _id: cruella._id });
3131

3232
// Finding by Id
3333
const anitaFromDb = await users.findOne({ _id: anitaId });
34-
console.log(JSON.stringify(anitaFromDb));
34+
console.log(anitaFromDb);
3535

3636
// Finding more
3737
const usersFromDB = await users.find({ age: { $lt: 40 } });
38-
console.log(JSON.stringify(usersFromDB));
38+
console.log(usersFromDB);
3939

4040
await pongo.close();

src/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo-core",
3-
"version": "0.15.0",
3+
"version": "0.15.1",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"engines": {

src/packages/pongo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo",
3-
"version": "0.15.0",
3+
"version": "0.15.1",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"scripts": {

src/packages/pongo/src/core/collection/pongoCollection.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
type ReplaceOneOptions,
3939
type UpdateManyOptions,
4040
type UpdateOneOptions,
41+
type WithIdAndVersion,
4142
type WithoutId,
4243
type WithVersion,
4344
} from '..';
@@ -282,16 +283,16 @@ export const pongoCollection = <
282283
findOne: async (
283284
filter?: PongoFilter<T>,
284285
options?: CollectionOperationOptions,
285-
): Promise<T | null> => {
286+
): Promise<WithIdAndVersion<T> | null> => {
286287
await ensureCollectionCreated(options);
287288

288289
const result = await query(SqlFor.findOne(filter ?? {}), options);
289-
return (result.rows[0]?.data ?? null) as T | null;
290+
return (result.rows[0]?.data ?? null) as WithIdAndVersion<T> | null;
290291
},
291292
findOneAndDelete: async (
292293
filter: PongoFilter<T>,
293294
options?: DeleteOneOptions,
294-
): Promise<T | null> => {
295+
): Promise<WithIdAndVersion<T> | null> => {
295296
await ensureCollectionCreated(options);
296297

297298
const existingDoc = await collection.findOne(filter, options);
@@ -305,7 +306,7 @@ export const pongoCollection = <
305306
filter: PongoFilter<T>,
306307
replacement: WithoutId<T>,
307308
options?: ReplaceOneOptions,
308-
): Promise<T | null> => {
309+
): Promise<WithIdAndVersion<T> | null> => {
309310
await ensureCollectionCreated(options);
310311

311312
const existingDoc = await collection.findOne(filter, options);
@@ -320,7 +321,7 @@ export const pongoCollection = <
320321
filter: PongoFilter<T>,
321322
update: PongoUpdate<T>,
322323
options?: UpdateOneOptions,
323-
): Promise<T | null> => {
324+
): Promise<WithIdAndVersion<T> | null> => {
324325
await ensureCollectionCreated(options);
325326

326327
const existingDoc = await collection.findOne(filter, options);
@@ -427,11 +428,11 @@ export const pongoCollection = <
427428
find: async (
428429
filter?: PongoFilter<T>,
429430
options?: CollectionOperationOptions,
430-
): Promise<T[]> => {
431+
): Promise<WithIdAndVersion<T>[]> => {
431432
await ensureCollectionCreated(options);
432433

433434
const result = await query(SqlFor.find(filter ?? {}));
434-
return result.rows.map((row) => row.data as T);
435+
return result.rows.map((row) => row.data as WithIdAndVersion<T>);
435436
},
436437
countDocuments: async (
437438
filter?: PongoFilter<T>,

src/packages/pongo/src/core/typing/operations.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,25 @@ export interface PongoCollection<T extends PongoDocument> {
159159
findOne(
160160
filter?: PongoFilter<T> | SQL,
161161
options?: CollectionOperationOptions,
162-
): Promise<T | null>;
162+
): Promise<WithIdAndVersion<T> | null>;
163163
find(
164164
filter?: PongoFilter<T> | SQL,
165165
options?: CollectionOperationOptions,
166-
): Promise<T[]>;
166+
): Promise<WithIdAndVersion<T>[]>;
167167
findOneAndDelete(
168168
filter: PongoFilter<T> | SQL,
169169
options?: DeleteOneOptions,
170-
): Promise<T | null>;
170+
): Promise<WithIdAndVersion<T> | null>;
171171
findOneAndReplace(
172172
filter: PongoFilter<T> | SQL,
173173
replacement: WithoutId<T>,
174174
options?: ReplaceOneOptions,
175-
): Promise<T | null>;
175+
): Promise<WithIdAndVersion<T> | null>;
176176
findOneAndUpdate(
177177
filter: PongoFilter<T> | SQL,
178178
update: PongoUpdate<T> | SQL,
179179
options?: UpdateOneOptions,
180-
): Promise<T | null>;
180+
): Promise<WithIdAndVersion<T> | null>;
181181
countDocuments(
182182
filter?: PongoFilter<T> | SQL,
183183
options?: CollectionOperationOptions,
@@ -261,6 +261,9 @@ export declare type WithVersion<TSchema> = EnhancedOmit<TSchema, '_version'> & {
261261
};
262262
export type WithoutVersion<T> = Omit<T, '_version'>;
263263

264+
export type WithIdAndVersion<T> = WithId<WithVersion<T>>;
265+
export type WithoutIdAndVersion<T> = WithoutId<WithoutVersion<T>>;
266+
264267
/** @public */
265268
export declare type RegExpOrString<T> = T extends string ? RegExp | T : T;
266269

src/packages/pongo/src/mongo/mongoCollection.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ export class Collection<T extends Document> implements MongoCollection<T> {
252252
filter?: unknown,
253253
options?: FindOptions<Document> | undefined,
254254
): Promise<import('mongodb').WithId<T> | T | null> {
255-
return this.collection.findOne(
255+
return (await this.collection.findOne(
256256
filter as PongoFilter<T>,
257257
toCollectionOperationOptions(options),
258-
);
258+
)) as T;
259259
}
260260
find(): MongoFindCursor<WithId<T>>;
261261
find(

0 commit comments

Comments
 (0)