Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed custom sql operations typing in PongoCollection typing and Find methods results #90

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/simple-ts/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
4 changes: 2 additions & 2 deletions samples/simple-ts/src/typedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
6 changes: 3 additions & 3 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/dumbo/src/postgres/pg/execute/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ async function batch<Result extends QueryResultRow = QueryResultRow>(

//TODO: make it smarter at some point
for (let i = 0; i < sqls.length; i++) {
const result = await client.query<Result>(sqls[i]!);
tracer.info('db:sql:query', { sql: sqls[i]! });
const result = await client.query<Result>(sqls[i]!);
results[i] = { rowCount: result.rowCount, rows: result.rows };
}
return Array.isArray(sqlOrSqls) ? results : results[0]!;
Expand Down
2 changes: 1 addition & 1 deletion src/packages/pongo/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
15 changes: 8 additions & 7 deletions src/packages/pongo/src/core/collection/pongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
type ReplaceOneOptions,
type UpdateManyOptions,
type UpdateOneOptions,
type WithIdAndVersion,
type WithoutId,
type WithVersion,
} from '..';
Expand Down Expand Up @@ -282,16 +283,16 @@ export const pongoCollection = <
findOne: async (
filter?: PongoFilter<T>,
options?: CollectionOperationOptions,
): Promise<T | null> => {
): Promise<WithIdAndVersion<T> | 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<T> | null;
},
findOneAndDelete: async (
filter: PongoFilter<T>,
options?: DeleteOneOptions,
): Promise<T | null> => {
): Promise<WithIdAndVersion<T> | null> => {
await ensureCollectionCreated(options);

const existingDoc = await collection.findOne(filter, options);
Expand All @@ -305,7 +306,7 @@ export const pongoCollection = <
filter: PongoFilter<T>,
replacement: WithoutId<T>,
options?: ReplaceOneOptions,
): Promise<T | null> => {
): Promise<WithIdAndVersion<T> | null> => {
await ensureCollectionCreated(options);

const existingDoc = await collection.findOne(filter, options);
Expand All @@ -320,7 +321,7 @@ export const pongoCollection = <
filter: PongoFilter<T>,
update: PongoUpdate<T>,
options?: UpdateOneOptions,
): Promise<T | null> => {
): Promise<WithIdAndVersion<T> | null> => {
await ensureCollectionCreated(options);

const existingDoc = await collection.findOne(filter, options);
Expand Down Expand Up @@ -427,11 +428,11 @@ export const pongoCollection = <
find: async (
filter?: PongoFilter<T>,
options?: CollectionOperationOptions,
): Promise<T[]> => {
): Promise<WithIdAndVersion<T>[]> => {
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<T>);
},
countDocuments: async (
filter?: PongoFilter<T>,
Expand Down
41 changes: 22 additions & 19 deletions src/packages/pongo/src/core/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,52 +134,52 @@ export interface PongoCollection<T extends PongoDocument> {
options?: CollectionOperationOptions,
): Promise<PongoInsertManyResult>;
updateOne(
filter: PongoFilter<T>,
update: PongoUpdate<T>,
filter: PongoFilter<T> | SQL,
update: PongoUpdate<T> | SQL,
options?: UpdateOneOptions,
): Promise<PongoUpdateResult>;
replaceOne(
filter: PongoFilter<T>,
filter: PongoFilter<T> | SQL,
document: WithoutId<T>,
options?: ReplaceOneOptions,
): Promise<PongoUpdateResult>;
updateMany(
filter: PongoFilter<T>,
update: PongoUpdate<T>,
filter: PongoFilter<T> | SQL,
update: PongoUpdate<T> | SQL,
options?: UpdateManyOptions,
): Promise<PongoUpdateManyResult>;
deleteOne(
filter?: PongoFilter<T>,
filter?: PongoFilter<T> | SQL,
options?: DeleteOneOptions,
): Promise<PongoDeleteResult>;
deleteMany(
filter?: PongoFilter<T>,
filter?: PongoFilter<T> | SQL,
options?: DeleteManyOptions,
): Promise<PongoDeleteResult>;
findOne(
filter?: PongoFilter<T>,
filter?: PongoFilter<T> | SQL,
options?: CollectionOperationOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
find(
filter?: PongoFilter<T>,
filter?: PongoFilter<T> | SQL,
options?: CollectionOperationOptions,
): Promise<T[]>;
): Promise<WithIdAndVersion<T>[]>;
findOneAndDelete(
filter: PongoFilter<T>,
filter: PongoFilter<T> | SQL,
options?: DeleteOneOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
findOneAndReplace(
filter: PongoFilter<T>,
filter: PongoFilter<T> | SQL,
replacement: WithoutId<T>,
options?: ReplaceOneOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
findOneAndUpdate(
filter: PongoFilter<T>,
update: PongoUpdate<T>,
filter: PongoFilter<T> | SQL,
update: PongoUpdate<T> | SQL,
options?: UpdateOneOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
countDocuments(
filter?: PongoFilter<T>,
filter?: PongoFilter<T> | SQL,
options?: CollectionOperationOptions,
): Promise<number>;
drop(options?: CollectionOperationOptions): Promise<boolean>;
Expand Down Expand Up @@ -261,6 +261,9 @@ export declare type WithVersion<TSchema> = EnhancedOmit<TSchema, '_version'> & {
};
export type WithoutVersion<T> = Omit<T, '_version'>;

export type WithIdAndVersion<T> = WithId<WithVersion<T>>;
export type WithoutIdAndVersion<T> = WithoutId<WithoutVersion<T>>;

/** @public */
export declare type RegExpOrString<T> = T extends string ? RegExp | T : T;

Expand Down
4 changes: 2 additions & 2 deletions src/packages/pongo/src/mongo/mongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ export class Collection<T extends Document> implements MongoCollection<T> {
filter?: unknown,
options?: FindOptions<Document> | undefined,
): Promise<import('mongodb').WithId<T> | T | null> {
return this.collection.findOne(
return (await this.collection.findOne(
filter as PongoFilter<T>,
toCollectionOperationOptions(options),
);
)) as T;
}
find(): MongoFindCursor<WithId<T>>;
find(
Expand Down