|
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 | +}; |
2 | 189 |
|
3 | 190 | export interface PongoSchemaConfig< |
4 | 191 | TypedClientSchema extends PongoClientSchema = PongoClientSchema, |
|
0 commit comments