Skip to content

Commit 9110998

Browse files
committed
Added findOneAndDelete, findOneAndReplace, findOneAndUpdate methods
1 parent 37f4ae6 commit 9110998

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export interface PongoCollection<T extends PongoDocument> {
3232
deleteMany(filter?: PongoFilter<T>): Promise<PongoDeleteResult>;
3333
findOne(filter?: PongoFilter<T>): Promise<T | null>;
3434
find(filter?: PongoFilter<T>): Promise<T[]>;
35+
findOneAndDelete(filter: PongoFilter<T>): Promise<T | null>;
36+
findOneAndReplace(
37+
filter: PongoFilter<T>,
38+
replacement: WithoutId<T>,
39+
): Promise<T | null>;
40+
findOneAndUpdate(
41+
filter: PongoFilter<T>,
42+
update: PongoUpdate<T>,
43+
): Promise<T | null>;
3544
countDocuments(filter?: PongoFilter<T>): Promise<number>;
3645
drop(): Promise<boolean>;
3746
rename(newName: string): Promise<PongoCollection<T>>;

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ export class Collection<T extends Document> implements MongoCollection<T> {
378378
): Promise<WithId<T> | null>;
379379
findOneAndDelete(filter: Filter<T>): Promise<WithId<T> | null>;
380380
findOneAndDelete(
381-
_filter: unknown,
381+
filter: unknown,
382382
_options?: unknown,
383-
):
384-
| Promise<import('mongodb').WithId<T> | null>
385-
| Promise<import('mongodb').ModifyResult<T>> {
386-
throw new Error('Method not implemented.');
383+
): Promise<WithId<T> | null | ModifyResult<T>> {
384+
return this.collection.findOneAndDelete(
385+
filter as PongoFilter<T>,
386+
) as Promise<WithId<T> | null>;
387387
}
388388
findOneAndReplace(
389389
filter: Filter<T>,
@@ -405,13 +405,14 @@ export class Collection<T extends Document> implements MongoCollection<T> {
405405
replacement: WithoutId<T>,
406406
): Promise<WithId<T> | null>;
407407
findOneAndReplace(
408-
_filter: unknown,
409-
_replacement: unknown,
408+
filter: unknown,
409+
replacement: unknown,
410410
_options?: unknown,
411-
):
412-
| Promise<import('mongodb').WithId<T> | null>
413-
| Promise<import('mongodb').ModifyResult<T>> {
414-
throw new Error('Method not implemented.');
411+
): Promise<WithId<T> | null | ModifyResult<T>> {
412+
return this.collection.findOneAndReplace(
413+
filter as PongoFilter<T>,
414+
replacement as WithoutId<T>,
415+
) as Promise<WithId<T> | null>;
415416
}
416417
findOneAndUpdate(
417418
filter: Filter<T>,
@@ -433,13 +434,14 @@ export class Collection<T extends Document> implements MongoCollection<T> {
433434
update: UpdateFilter<T>,
434435
): Promise<WithId<T> | null>;
435436
findOneAndUpdate(
436-
_filter: unknown,
437-
_update: unknown,
437+
filter: unknown,
438+
update: unknown,
438439
_options?: unknown,
439-
):
440-
| Promise<import('mongodb').WithId<T> | null>
441-
| Promise<import('mongodb').ModifyResult<T>> {
442-
throw new Error('Method not implemented.');
440+
): Promise<WithId<T> | null | ModifyResult<T>> {
441+
return this.collection.findOneAndUpdate(
442+
filter as PongoFilter<T>,
443+
update as PongoUpdate<T>,
444+
) as Promise<WithId<T> | null>;
443445
}
444446
aggregate<T extends Document = Document>(
445447
_pipeline?: Document[] | undefined,

src/packages/pongo/src/postgres/postgresCollection.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,44 @@ export const postgresCollection = <T extends PongoDocument>(
119119
const result = await execute(SqlFor.findOne(filter ?? {}));
120120
return (result.rows[0]?.data ?? null) as T | null;
121121
},
122+
findOneAndDelete: async (filter: PongoFilter<T>): Promise<T | null> => {
123+
await createCollection;
124+
125+
const existingDoc = await collection.findOne(filter);
126+
127+
if (existingDoc === null) return null;
128+
129+
await collection.deleteOne(filter);
130+
return existingDoc;
131+
},
132+
findOneAndReplace: async (
133+
filter: PongoFilter<T>,
134+
replacement: WithoutId<T>,
135+
): Promise<T | null> => {
136+
await createCollection;
137+
138+
const existingDoc = await collection.findOne(filter);
139+
140+
if (existingDoc === null) return null;
141+
142+
await collection.replaceOne(filter, replacement);
143+
144+
return existingDoc;
145+
},
146+
findOneAndUpdate: async (
147+
filter: PongoFilter<T>,
148+
update: PongoUpdate<T>,
149+
): Promise<T | null> => {
150+
await createCollection;
151+
152+
const existingDoc = await collection.findOne(filter);
153+
154+
if (existingDoc === null) return null;
155+
156+
await collection.updateOne(filter, update);
157+
158+
return existingDoc;
159+
},
122160
handle: async (
123161
id: string,
124162
handle: DocumentHandler<T>,

0 commit comments

Comments
 (0)