Skip to content

Commit

Permalink
Returned WithIdAndVersion from find methods
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Oct 2, 2024
1 parent 8049d19 commit f3e2157
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 22 deletions.
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/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
13 changes: 8 additions & 5 deletions src/packages/pongo/src/core/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,25 @@ export interface PongoCollection<T extends PongoDocument> {
findOne(
filter?: PongoFilter<T> | SQL,
options?: CollectionOperationOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
find(
filter?: PongoFilter<T> | SQL,
options?: CollectionOperationOptions,
): Promise<T[]>;
): Promise<WithIdAndVersion<T>[]>;
findOneAndDelete(
filter: PongoFilter<T> | SQL,
options?: DeleteOneOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
findOneAndReplace(
filter: PongoFilter<T> | SQL,
replacement: WithoutId<T>,
options?: ReplaceOneOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
findOneAndUpdate(
filter: PongoFilter<T> | SQL,
update: PongoUpdate<T> | SQL,
options?: UpdateOneOptions,
): Promise<T | null>;
): Promise<WithIdAndVersion<T> | null>;
countDocuments(
filter?: PongoFilter<T> | SQL,
options?: CollectionOperationOptions,
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

0 comments on commit f3e2157

Please sign in to comment.