From 91d5f40dce91d1922fe57669039a61a904dfd723 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Wed, 2 Oct 2024 15:29:51 +0200 Subject: [PATCH 1/2] Fixed custom sql operations typing in PongoCollection type --- .../dumbo/src/postgres/pg/execute/execute.ts | 2 +- .../pongo/src/core/typing/operations.ts | 28 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/packages/dumbo/src/postgres/pg/execute/execute.ts b/src/packages/dumbo/src/postgres/pg/execute/execute.ts index 3d60011..46523e8 100644 --- a/src/packages/dumbo/src/postgres/pg/execute/execute.ts +++ b/src/packages/dumbo/src/postgres/pg/execute/execute.ts @@ -88,8 +88,8 @@ async function batch( //TODO: make it smarter at some point for (let i = 0; i < sqls.length; i++) { - const result = await client.query(sqls[i]!); tracer.info('db:sql:query', { sql: sqls[i]! }); + const result = await client.query(sqls[i]!); results[i] = { rowCount: result.rowCount, rows: result.rows }; } return Array.isArray(sqlOrSqls) ? results : results[0]!; diff --git a/src/packages/pongo/src/core/typing/operations.ts b/src/packages/pongo/src/core/typing/operations.ts index 3ba22be..d319c38 100644 --- a/src/packages/pongo/src/core/typing/operations.ts +++ b/src/packages/pongo/src/core/typing/operations.ts @@ -134,52 +134,52 @@ export interface PongoCollection { options?: CollectionOperationOptions, ): Promise; updateOne( - filter: PongoFilter, - update: PongoUpdate, + filter: PongoFilter | SQL, + update: PongoUpdate | SQL, options?: UpdateOneOptions, ): Promise; replaceOne( - filter: PongoFilter, + filter: PongoFilter | SQL, document: WithoutId, options?: ReplaceOneOptions, ): Promise; updateMany( - filter: PongoFilter, - update: PongoUpdate, + filter: PongoFilter | SQL, + update: PongoUpdate | SQL, options?: UpdateManyOptions, ): Promise; deleteOne( - filter?: PongoFilter, + filter?: PongoFilter | SQL, options?: DeleteOneOptions, ): Promise; deleteMany( - filter?: PongoFilter, + filter?: PongoFilter | SQL, options?: DeleteManyOptions, ): Promise; findOne( - filter?: PongoFilter, + filter?: PongoFilter | SQL, options?: CollectionOperationOptions, ): Promise; find( - filter?: PongoFilter, + filter?: PongoFilter | SQL, options?: CollectionOperationOptions, ): Promise; findOneAndDelete( - filter: PongoFilter, + filter: PongoFilter | SQL, options?: DeleteOneOptions, ): Promise; findOneAndReplace( - filter: PongoFilter, + filter: PongoFilter | SQL, replacement: WithoutId, options?: ReplaceOneOptions, ): Promise; findOneAndUpdate( - filter: PongoFilter, - update: PongoUpdate, + filter: PongoFilter | SQL, + update: PongoUpdate | SQL, options?: UpdateOneOptions, ): Promise; countDocuments( - filter?: PongoFilter, + filter?: PongoFilter | SQL, options?: CollectionOperationOptions, ): Promise; drop(options?: CollectionOperationOptions): Promise; From a8bcf2bd629ffdccce63ca463c1938c8c593b36c Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Wed, 2 Oct 2024 15:36:30 +0200 Subject: [PATCH 2/2] Returned WithIdAndVersion from find methods --- samples/simple-ts/src/shim.ts | 2 +- samples/simple-ts/src/typedClient.ts | 4 ++-- src/package-lock.json | 6 +++--- src/package.json | 2 +- src/packages/pongo/package.json | 2 +- .../pongo/src/core/collection/pongoCollection.ts | 15 ++++++++------- src/packages/pongo/src/core/typing/operations.ts | 13 ++++++++----- src/packages/pongo/src/mongo/mongoCollection.ts | 4 ++-- 8 files changed, 26 insertions(+), 22 deletions(-) diff --git a/samples/simple-ts/src/shim.ts b/samples/simple-ts/src/shim.ts index 3a7ccb2..e189824 100644 --- a/samples/simple-ts/src/shim.ts +++ b/samples/simple-ts/src/shim.ts @@ -33,6 +33,6 @@ console.log(anitaFromDb); // Finding more const usersFromDB = await users.find({ age: { $lt: 40 } }).toArray(); -console.log(JSON.stringify(usersFromDB)); +console.log(usersFromDB); await pongoClient.close(); diff --git a/samples/simple-ts/src/typedClient.ts b/samples/simple-ts/src/typedClient.ts index 4cbad9c..8cca086 100644 --- a/samples/simple-ts/src/typedClient.ts +++ b/samples/simple-ts/src/typedClient.ts @@ -31,10 +31,10 @@ await users.deleteOne({ _id: cruella._id }); // Finding by Id const anitaFromDb = await users.findOne({ _id: anitaId }); -console.log(JSON.stringify(anitaFromDb)); +console.log(anitaFromDb); // Finding more const usersFromDB = await users.find({ age: { $lt: 40 } }); -console.log(JSON.stringify(usersFromDB)); +console.log(usersFromDB); await pongo.close(); diff --git a/src/package-lock.json b/src/package-lock.json index 49574a5..b54fa31 100644 --- a/src/package-lock.json +++ b/src/package-lock.json @@ -1,12 +1,12 @@ { "name": "@event-driven-io/pongo-core", - "version": "0.15.0", + "version": "0.15.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@event-driven-io/pongo-core", - "version": "0.15.0", + "version": "0.15.1", "workspaces": [ "packages/dumbo", "packages/pongo" @@ -8674,7 +8674,7 @@ }, "packages/pongo": { "name": "@event-driven-io/pongo", - "version": "0.15.0", + "version": "0.15.1", "bin": { "pongo": "dist/cli.js" }, diff --git a/src/package.json b/src/package.json index 4e1bd72..f1427dd 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "@event-driven-io/pongo-core", - "version": "0.15.0", + "version": "0.15.1", "description": "Pongo - Mongo with strong consistency on top of Postgres", "type": "module", "engines": { diff --git a/src/packages/pongo/package.json b/src/packages/pongo/package.json index adf6459..aa0ebe6 100644 --- a/src/packages/pongo/package.json +++ b/src/packages/pongo/package.json @@ -1,6 +1,6 @@ { "name": "@event-driven-io/pongo", - "version": "0.15.0", + "version": "0.15.1", "description": "Pongo - Mongo with strong consistency on top of Postgres", "type": "module", "scripts": { diff --git a/src/packages/pongo/src/core/collection/pongoCollection.ts b/src/packages/pongo/src/core/collection/pongoCollection.ts index d3f57db..4846d0f 100644 --- a/src/packages/pongo/src/core/collection/pongoCollection.ts +++ b/src/packages/pongo/src/core/collection/pongoCollection.ts @@ -38,6 +38,7 @@ import { type ReplaceOneOptions, type UpdateManyOptions, type UpdateOneOptions, + type WithIdAndVersion, type WithoutId, type WithVersion, } from '..'; @@ -282,16 +283,16 @@ export const pongoCollection = < findOne: async ( filter?: PongoFilter, options?: CollectionOperationOptions, - ): Promise => { + ): Promise | null> => { await ensureCollectionCreated(options); const result = await query(SqlFor.findOne(filter ?? {}), options); - return (result.rows[0]?.data ?? null) as T | null; + return (result.rows[0]?.data ?? null) as WithIdAndVersion | null; }, findOneAndDelete: async ( filter: PongoFilter, options?: DeleteOneOptions, - ): Promise => { + ): Promise | null> => { await ensureCollectionCreated(options); const existingDoc = await collection.findOne(filter, options); @@ -305,7 +306,7 @@ export const pongoCollection = < filter: PongoFilter, replacement: WithoutId, options?: ReplaceOneOptions, - ): Promise => { + ): Promise | null> => { await ensureCollectionCreated(options); const existingDoc = await collection.findOne(filter, options); @@ -320,7 +321,7 @@ export const pongoCollection = < filter: PongoFilter, update: PongoUpdate, options?: UpdateOneOptions, - ): Promise => { + ): Promise | null> => { await ensureCollectionCreated(options); const existingDoc = await collection.findOne(filter, options); @@ -427,11 +428,11 @@ export const pongoCollection = < find: async ( filter?: PongoFilter, options?: CollectionOperationOptions, - ): Promise => { + ): Promise[]> => { await ensureCollectionCreated(options); const result = await query(SqlFor.find(filter ?? {})); - return result.rows.map((row) => row.data as T); + return result.rows.map((row) => row.data as WithIdAndVersion); }, countDocuments: async ( filter?: PongoFilter, diff --git a/src/packages/pongo/src/core/typing/operations.ts b/src/packages/pongo/src/core/typing/operations.ts index d319c38..718a4d9 100644 --- a/src/packages/pongo/src/core/typing/operations.ts +++ b/src/packages/pongo/src/core/typing/operations.ts @@ -159,25 +159,25 @@ export interface PongoCollection { findOne( filter?: PongoFilter | SQL, options?: CollectionOperationOptions, - ): Promise; + ): Promise | null>; find( filter?: PongoFilter | SQL, options?: CollectionOperationOptions, - ): Promise; + ): Promise[]>; findOneAndDelete( filter: PongoFilter | SQL, options?: DeleteOneOptions, - ): Promise; + ): Promise | null>; findOneAndReplace( filter: PongoFilter | SQL, replacement: WithoutId, options?: ReplaceOneOptions, - ): Promise; + ): Promise | null>; findOneAndUpdate( filter: PongoFilter | SQL, update: PongoUpdate | SQL, options?: UpdateOneOptions, - ): Promise; + ): Promise | null>; countDocuments( filter?: PongoFilter | SQL, options?: CollectionOperationOptions, @@ -261,6 +261,9 @@ export declare type WithVersion = EnhancedOmit & { }; export type WithoutVersion = Omit; +export type WithIdAndVersion = WithId>; +export type WithoutIdAndVersion = WithoutId>; + /** @public */ export declare type RegExpOrString = T extends string ? RegExp | T : T; diff --git a/src/packages/pongo/src/mongo/mongoCollection.ts b/src/packages/pongo/src/mongo/mongoCollection.ts index 49038c8..ba620c2 100644 --- a/src/packages/pongo/src/mongo/mongoCollection.ts +++ b/src/packages/pongo/src/mongo/mongoCollection.ts @@ -252,10 +252,10 @@ export class Collection implements MongoCollection { filter?: unknown, options?: FindOptions | undefined, ): Promise | T | null> { - return this.collection.findOne( + return (await this.collection.findOne( filter as PongoFilter, toCollectionOperationOptions(options), - ); + )) as T; } find(): MongoFindCursor>; find(