Skip to content

Commit d4374c2

Browse files
committed
Added command handling method
1 parent 0ddbe14 commit d4374c2

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

src/packages/pongo/src/main/dbClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { postgresClient, type PongoClientOptions } from '../postgres';
2-
import type { PongoCollection } from './typing/operations';
2+
import type { PongoCollection, PongoDocument } from './typing/operations';
33

44
export interface DbClient {
55
connect(): Promise<void>;
66
close(): Promise<void>;
7-
collection: <T>(name: string) => PongoCollection<T>;
7+
collection: <T extends PongoDocument>(name: string) => PongoCollection<T>;
88
}
99

1010
export const getDbClient = (options: PongoClientOptions): DbClient => {

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface PongoClient {
77
}
88

99
export interface PongoDb {
10-
collection<T>(name: string): PongoCollection<T>;
10+
collection<T extends PongoDocument>(name: string): PongoCollection<T>;
1111
}
1212

1313
export interface PongoCollection<T> {
@@ -101,3 +101,9 @@ export interface PongoDeleteManyResult {
101101
acknowledged: boolean;
102102
deletedCount: number;
103103
}
104+
105+
export type PongoDocument = Record<string, unknown>;
106+
107+
export type DocumentHandler<T extends PongoDocument> =
108+
| ((document: T | null) => T | null)
109+
| ((document: T | null) => Promise<T | null>);

src/packages/pongo/src/postgres/client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getPool,
55
} from '@event-driven-io/dumbo';
66
import pg from 'pg';
7-
import { type DbClient } from '../main';
7+
import { type DbClient, type PongoDocument } from '../main';
88
import { postgresCollection } from './postgresCollection';
99

1010
export type PongoClientOptions = {
@@ -25,7 +25,7 @@ export const postgresClient = (options: PongoClientOptions): DbClient => {
2525
managesPoolLifetime
2626
? endPool({ connectionString, database: dbName })
2727
: Promise.resolve(),
28-
collection: <T>(name: string) =>
28+
collection: <T extends PongoDocument>(name: string) =>
2929
postgresCollection<T>(name, {
3030
dbName: dbName ?? getDatabaseNameOrDefault(connectionString),
3131
poolOrClient,

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

+25-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import pg from 'pg';
33
import format from 'pg-format';
44
import { v4 as uuid } from 'uuid';
55
import {
6+
type DocumentHandler,
67
type PongoCollection,
78
type PongoDeleteResult,
9+
type PongoDocument,
810
type PongoFilter,
911
type PongoInsertManyResult,
1012
type PongoInsertOneResult,
@@ -16,7 +18,7 @@ import {
1618
import { constructFilterQuery } from './filter';
1719
import { buildUpdateQuery } from './update';
1820

19-
export const postgresCollection = <T>(
21+
export const postgresCollection = <T extends PongoDocument>(
2022
collectionName: string,
2123
{
2224
dbName,
@@ -117,6 +119,24 @@ export const postgresCollection = <T>(
117119
const result = await execute(SqlFor.findOne(filter));
118120
return (result.rows[0]?.data ?? null) as T | null;
119121
},
122+
handle: async (
123+
id: string,
124+
handle: DocumentHandler<T>,
125+
): Promise<T | null> => {
126+
await createCollection;
127+
128+
const byId: PongoFilter<T> = { _id: id };
129+
130+
const existing = await collection.findOne(byId);
131+
132+
const result = await handle(existing);
133+
134+
if (!existing && result) await collection.insertOne(result);
135+
else if (existing && result) await collection.replaceOne(byId, result);
136+
else if (existing && !result) await collection.deleteOne(byId);
137+
138+
return result;
139+
},
120140
find: async (filter: PongoFilter<T>): Promise<T[]> => {
121141
await createCollection;
122142

@@ -133,12 +153,12 @@ export const postgresCollection = <T>(
133153
},
134154
drop: async (): Promise<boolean> => {
135155
await createCollection;
136-
const result = await execute(SqlFor.dropCollection());
156+
const result = await execute(SqlFor.drop());
137157
return (result?.rowCount ?? 0) > 0;
138158
},
139159
rename: async (newName: string): Promise<PongoCollection<T>> => {
140160
await createCollection;
141-
await execute(SqlFor.renameCollection(newName));
161+
await execute(SqlFor.rename(newName));
142162
collectionName = newName;
143163
return collection;
144164
},
@@ -231,8 +251,8 @@ export const collectionSQLBuilder = (collectionName: string) => ({
231251
filterQuery,
232252
);
233253
},
234-
renameCollection: (newName: string): SQL =>
254+
rename: (newName: string): SQL =>
235255
sql('ALTER TABLE %I RENAME TO %I', collectionName, newName),
236-
dropCollection: (targetName: string = collectionName): SQL =>
256+
drop: (targetName: string = collectionName): SQL =>
237257
sql('DROP TABLE IF EXISTS %I', targetName),
238258
});

0 commit comments

Comments
 (0)