Skip to content

Commit a9a4372

Browse files
committed
Added possibility to use regular database methods on strongly-typed database
1 parent 3d4dbd4 commit a9a4372

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/packages/pongo/src/core/schema/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ export type PongoDbWithSchema<
4242
ConnectorType extends string = string,
4343
> = CollectionsMap<T> & PongoDb<ConnectorType>;
4444

45-
export type DBsMap<T extends Record<string, PongoDbSchema>> = {
46-
[K in keyof T]: CollectionsMap<T[K]['collections']>;
45+
export type DBsMap<
46+
T extends Record<string, PongoDbSchema>,
47+
ConnectorType extends string = string,
48+
> = {
49+
[K in keyof T]: CollectionsMap<T[K]['collections']> & PongoDb<ConnectorType>;
4750
};
4851

49-
export type PongoClientWithSchema<T extends PongoClientSchema> = DBsMap<
50-
T['dbs']
51-
> &
52-
PongoClient;
52+
export type PongoClientWithSchema<
53+
T extends PongoClientSchema,
54+
ConnectorType extends string = string,
55+
> = DBsMap<T['dbs'], ConnectorType> & PongoClient;
5356

5457
const pongoCollectionSchema = <T extends PongoDocument>(
5558
name: string,
@@ -124,7 +127,6 @@ export const proxyPongoDbWithSchema = <
124127
) as PongoDbWithSchema<T, ConnectorType>;
125128
};
126129

127-
// Factory function to create Client instances
128130
export const proxyClientWithSchema = <
129131
TypedClientSchema extends PongoClientSchema,
130132
>(

src/packages/pongo/src/e2e/postgres.e2e.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ void describe('MongoDB Compatibility Tests', () => {
11471147
}),
11481148
});
11491149

1150-
void it('should access collection by name and perform operation', async () => {
1150+
void it('should access typed collection and perform operation', async () => {
11511151
const typedClient = pongoClient(postgresConnectionString, {
11521152
schema: { definition: schema },
11531153
});
@@ -1171,5 +1171,30 @@ void describe('MongoDB Compatibility Tests', () => {
11711171
await typedClient.close();
11721172
}
11731173
});
1174+
1175+
void it('should access collection by name and perform operation', async () => {
1176+
const typedClient = pongoClient(postgresConnectionString, {
1177+
schema: { definition: schema },
1178+
});
1179+
try {
1180+
const users = typedClient.database.collection<User>('users');
1181+
1182+
const _id = new Date().toISOString();
1183+
const doc: User = {
1184+
_id,
1185+
name: 'Anita',
1186+
age: 25,
1187+
};
1188+
const pongoInsertResult = await users.insertOne(doc);
1189+
assert(pongoInsertResult.insertedId);
1190+
1191+
const pongoDoc = await users.findOne({
1192+
_id: pongoInsertResult.insertedId,
1193+
});
1194+
assert.ok(pongoDoc);
1195+
} finally {
1196+
await typedClient.close();
1197+
}
1198+
});
11741199
});
11751200
});

0 commit comments

Comments
 (0)