Skip to content

Commit 03b382a

Browse files
committed
Exported missing pongoSchema
1 parent cc08a9f commit 03b382a

File tree

11 files changed

+202
-203
lines changed

11 files changed

+202
-203
lines changed

src/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo-core",
3-
"version": "0.14.1",
3+
"version": "0.14.2",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"engines": {

src/packages/pongo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo",
3-
"version": "0.14.1",
3+
"version": "0.14.2",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"scripts": {

src/packages/pongo/src/commandLine/configFile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Command } from 'commander';
22
import fs from 'node:fs';
3-
import { objectEntries, type PongoSchemaConfig } from '../core';
43
import {
4+
objectEntries,
55
toDbSchemaMetadata,
66
type PongoDbSchemaMetadata,
7-
} from '../core/typing/schema';
7+
type PongoSchemaConfig,
8+
} from '../core';
89

910
const formatTypeName = (input: string): string => {
1011
if (input.length === 0) {

src/packages/pongo/src/core/pongoClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import pg from 'pg';
77
import type { PostgresDbClientOptions } from '../postgres';
88
import { getPongoDb, type AllowedDbClientOptions } from './pongoDb';
99
import { pongoSession } from './pongoSession';
10-
import type { PongoClient, PongoDb, PongoSession } from './typing/operations';
1110
import {
1211
proxyClientWithSchema,
1312
type PongoClientSchema,
1413
type PongoClientWithSchema,
15-
} from './typing/schema';
14+
} from './schema';
15+
import type { PongoClient, PongoDb, PongoSession } from './typing';
1616

1717
export type PooledPongoClientOptions =
1818
| {

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

Lines changed: 188 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,191 @@
1-
import type { PongoClientSchema } from '../typing/schema';
1+
import {
2+
type Document,
3+
type PongoClient,
4+
type PongoCollection,
5+
type PongoDb,
6+
type PongoDocument,
7+
objectEntries,
8+
} from '../typing';
9+
10+
export interface PongoCollectionSchema<
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
T extends PongoDocument = PongoDocument,
13+
> {
14+
name: string;
15+
}
16+
17+
// Database schema interface
18+
export interface PongoDbSchema<
19+
T extends Record<string, PongoCollectionSchema> = Record<
20+
string,
21+
PongoCollectionSchema
22+
>,
23+
> {
24+
name?: string;
25+
collections: T;
26+
}
27+
28+
export interface PongoClientSchema<
29+
T extends Record<string, PongoDbSchema> = Record<string, PongoDbSchema>,
30+
> {
31+
dbs: T;
32+
}
33+
34+
export type CollectionsMap<T extends Record<string, PongoCollectionSchema>> = {
35+
[K in keyof T]: PongoCollection<
36+
T[K] extends PongoCollectionSchema<infer U> ? U : PongoDocument
37+
>;
38+
};
39+
40+
export type PongoDbWithSchema<
41+
T extends Record<string, PongoCollectionSchema>,
42+
ConnectorType extends string = string,
43+
> = CollectionsMap<T> & PongoDb<ConnectorType>;
44+
45+
export type DBsMap<T extends Record<string, PongoDbSchema>> = {
46+
[K in keyof T]: CollectionsMap<T[K]['collections']>;
47+
};
48+
49+
export type PongoClientWithSchema<T extends PongoClientSchema> = DBsMap<
50+
T['dbs']
51+
> &
52+
PongoClient;
53+
54+
const pongoCollectionSchema = <T extends PongoDocument>(
55+
name: string,
56+
): PongoCollectionSchema<T> => ({
57+
name,
58+
});
59+
60+
function pongoDbSchema<T extends Record<string, PongoCollectionSchema>>(
61+
collections: T,
62+
): PongoDbSchema<T>;
63+
function pongoDbSchema<T extends Record<string, PongoCollectionSchema>>(
64+
name: string,
65+
collections: T,
66+
): PongoDbSchema<T>;
67+
function pongoDbSchema<T extends Record<string, PongoCollectionSchema>>(
68+
nameOrCollections: string | T,
69+
collections?: T | undefined,
70+
): PongoDbSchema<T> {
71+
if (collections === undefined) {
72+
if (typeof nameOrCollections === 'string') {
73+
throw new Error('You need to provide colleciton definition');
74+
}
75+
return {
76+
collections: nameOrCollections,
77+
};
78+
}
79+
80+
return nameOrCollections && typeof nameOrCollections === 'string'
81+
? {
82+
name: nameOrCollections,
83+
collections,
84+
}
85+
: { collections: collections };
86+
}
87+
88+
const pongoClientSchema = <T extends Record<string, PongoDbSchema>>(
89+
dbs: T,
90+
): PongoClientSchema<T> => ({
91+
dbs,
92+
});
93+
94+
export const pongoSchema = {
95+
client: pongoClientSchema,
96+
db: pongoDbSchema,
97+
collection: pongoCollectionSchema,
98+
};
99+
100+
// Factory function to create DB instances
101+
export const proxyPongoDbWithSchema = <
102+
T extends Record<string, PongoCollectionSchema>,
103+
ConnectorType extends string = string,
104+
>(
105+
pongoDb: PongoDb<ConnectorType>,
106+
dbSchema: PongoDbSchema<T>,
107+
collections: Map<string, PongoCollection<Document>>,
108+
): PongoDbWithSchema<T, ConnectorType> => {
109+
const collectionNames = Object.keys(dbSchema.collections);
110+
111+
for (const collectionName of collectionNames) {
112+
collections.set(collectionName, pongoDb.collection(collectionName));
113+
}
114+
115+
return new Proxy(
116+
pongoDb as PongoDb<ConnectorType> & {
117+
[key: string]: unknown;
118+
},
119+
{
120+
get(target, prop: string) {
121+
return collections.get(prop) ?? target[prop];
122+
},
123+
},
124+
) as PongoDbWithSchema<T, ConnectorType>;
125+
};
126+
127+
// Factory function to create Client instances
128+
export const proxyClientWithSchema = <
129+
TypedClientSchema extends PongoClientSchema,
130+
>(
131+
client: PongoClient,
132+
schema: TypedClientSchema | undefined,
133+
): PongoClientWithSchema<TypedClientSchema> => {
134+
if (!schema) return client as PongoClientWithSchema<TypedClientSchema>;
135+
136+
const dbNames = Object.keys(schema.dbs);
137+
138+
return new Proxy(
139+
client as PongoClient & {
140+
[key: string]: unknown;
141+
},
142+
{
143+
get(target, prop: string) {
144+
if (dbNames.includes(prop)) return client.db(schema.dbs[prop]?.name);
145+
146+
return target[prop];
147+
},
148+
},
149+
) as PongoClientWithSchema<TypedClientSchema>;
150+
};
151+
152+
export type PongoCollectionSchemaMetadata = {
153+
name: string;
154+
};
155+
156+
export type PongoDbSchemaMetadata = {
157+
name?: string | undefined;
158+
collections: PongoCollectionSchemaMetadata[];
159+
};
160+
161+
export type PongoClientSchemaMetadata = {
162+
databases: PongoDbSchemaMetadata[];
163+
database: (name?: string) => PongoDbSchemaMetadata | undefined;
164+
};
165+
166+
export const toDbSchemaMetadata = <TypedDbSchema extends PongoDbSchema>(
167+
schema: TypedDbSchema,
168+
): PongoDbSchemaMetadata => ({
169+
name: schema.name,
170+
collections: objectEntries(schema.collections).map((c) => ({
171+
name: c[1].name,
172+
})),
173+
});
174+
175+
export const toClientSchemaMetadata = <
176+
TypedClientSchema extends PongoClientSchema,
177+
>(
178+
schema: TypedClientSchema,
179+
): PongoClientSchemaMetadata => {
180+
const databases = objectEntries(schema.dbs).map((e) =>
181+
toDbSchemaMetadata(e[1]),
182+
);
183+
184+
return {
185+
databases,
186+
database: (name) => databases.find((db) => db.name === name),
187+
};
188+
};
2189

3190
export interface PongoSchemaConfig<
4191
TypedClientSchema extends PongoClientSchema = PongoClientSchema,

0 commit comments

Comments
 (0)