@blitznocode/blitz-orm 0.4.8 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,19 @@
1
1
  import { TypeDBCredential, TypeDBClient, TypeDBSession } from 'typedb-client';
2
2
 
3
+ interface TypeDBProviderObject {
4
+ provider: 'typeDB';
5
+ url: string;
6
+ }
7
+ interface TypeDBClusterProviderObject {
8
+ provider: 'typeDBCluster';
9
+ addresses: string[];
10
+ credentials: TypeDBCredential;
11
+ }
12
+ type TypeDBHandles = Map<string, {
13
+ client: TypeDBClient;
14
+ session: TypeDBSession;
15
+ }>;
16
+
3
17
  type BormConfig = {
4
18
  server: {
5
19
  provider: 'blitz-orm-js';
@@ -20,103 +34,31 @@ interface CommonProperties {
20
34
  dbName: string;
21
35
  }
22
36
  type Provider = 'typeDB' | 'typeDBCluster';
23
- interface TypeDBProviderObject {
24
- provider: 'typeDB';
25
- url: string;
26
- }
27
- interface TypeDBClusterProviderObject {
28
- provider: 'typeDBCluster';
29
- addresses: string[];
30
- credentials: TypeDBCredential;
31
- }
32
37
  type DBConnector = {
33
38
  id: string;
34
39
  subs?: string;
35
40
  path?: string;
36
41
  as?: string;
37
42
  };
38
- type TypeDBHandles = Map<string, {
39
- client: TypeDBClient;
40
- session: TypeDBSession;
41
- }>;
42
43
  type DBHandles = {
43
44
  typeDB: TypeDBHandles;
44
45
  };
45
- type BormSchema = {
46
- entities: {
47
- [s: string]: BormEntity;
48
- };
49
- relations: {
50
- [s: string]: BormRelation;
51
- };
52
- };
53
- type EnrichedBormSchema = {
54
- entities: {
55
- [s: string]: EnrichedBormEntity;
56
- };
57
- relations: {
58
- [s: string]: EnrichedBormRelation;
59
- };
60
- };
61
- type BormEntity = {
62
- extends: string;
63
- idFields?: string[];
64
- defaultDBConnector: DBConnector;
65
- dataFields?: DataField[];
66
- linkFields?: LinkField[];
67
- } | {
68
- extends?: string;
69
- idFields: string[];
70
- defaultDBConnector: DBConnector;
71
- dataFields?: DataField[];
72
- linkFields?: LinkField[];
73
- };
74
- type BormRelation = BormEntity & {
75
- defaultDBConnector: DBConnector & {
76
- path: string;
77
- };
78
- roles?: {
79
- [key: string]: RoleField;
80
- };
81
- };
82
- type EnrichedBormEntity = Omit<BormEntity, 'linkFields' | 'idFields' | 'dataFields'> & {
83
- extends?: string;
84
- allExtends?: string[];
85
- idFields: string[];
86
- thingType: 'entity';
87
- name: string;
88
- computedFields: string[];
89
- virtualFields: string[];
90
- linkFields?: EnrichedLinkField[];
91
- dataFields?: EnrichedDataField[];
92
- };
93
- type EnrichedBormRelation = Omit<BormRelation, 'linkFields' | 'dataFields'> & {
94
- thingType: 'relation';
95
- name: string;
96
- computedFields: string[];
97
- virtualFields: string[];
98
- linkFields?: EnrichedLinkField[];
99
- dataFields?: EnrichedDataField[];
100
- roles: {
101
- [key: string]: EnrichedRoleField;
102
- };
103
- };
104
- type LinkField = BormField & {
105
- relation: string;
106
- plays: string;
107
- } & ({
108
- target: 'role';
109
- filter?: Filter | Filter[];
110
- } | {
111
- target: 'relation';
112
- });
113
- type LinkedFieldWithThing = LinkField & {
114
- thing: string;
115
- thingType: ThingType;
46
+
47
+ type ThingType = 'entity' | 'relation' | 'attribute';
48
+ type BormMetadata = {
49
+ $id: string;
50
+ $entity?: string;
51
+ $relation?: string;
52
+ $tempId?: string;
116
53
  };
117
- type RequireAtLeastOne<T> = {
118
- [K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
119
- }[keyof T];
54
+ type WithBormMetadata<T> = T extends any[] ? T[number] extends object ? Array<WithBormMetadataObject<T[number]>> : T : T extends object ? WithBormMetadataObject<T> : T;
55
+ type WithBormMetadataObject<T> = {
56
+ [K in keyof T]: WithBormMetadata<T[K]>;
57
+ } & BormMetadata;
58
+ type BQLResponseSingle = Record<string, any>;
59
+ type BQLResponseMulti = BQLResponseSingle[];
60
+ type BQLResponse = BQLResponseSingle | BQLResponseMulti;
61
+
120
62
  type LinkFilter = {
121
63
  $thing?: string;
122
64
  $thingType?: string;
@@ -128,53 +70,14 @@ type MiddleFilter = {
128
70
  $and?: Filter[];
129
71
  $or?: Filter[];
130
72
  };
73
+ type RequireAtLeastOne<T> = {
74
+ [K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
75
+ }[keyof T];
131
76
  type DataFilter = RequireAtLeastOne<{
132
77
  $eq?: any;
133
78
  $ge?: number | string;
134
79
  }>;
135
- type EnrichedLinkField = BormField & {
136
- name: string;
137
- relation: string;
138
- plays: string;
139
- } & ({
140
- target: 'role';
141
- filter?: Filter | Filter[];
142
- oppositeLinkFieldsPlayedBy: LinkedFieldWithThing[];
143
- } | {
144
- target: 'relation';
145
- oppositeLinkFieldsPlayedBy: Pick<LinkedFieldWithThing, 'thing' | 'thingType' | 'plays'>[];
146
- });
147
- type BormField = {
148
- path: string;
149
- cardinality: CardinalityType;
150
- ordered?: boolean;
151
- embedded?: boolean;
152
- rights?: RightType[];
153
- };
154
- type RoleField = {
155
- cardinality: CardinalityType;
156
- dbConnector?: DBConnector;
157
- };
158
- type EnrichedRoleField = RoleField & {
159
- playedBy?: LinkedFieldWithThing[];
160
- name: string;
161
- };
162
- type DataField = BormField & {
163
- shared?: boolean;
164
- default?: any;
165
- contentType: ContentType;
166
- validations?: any;
167
- isVirtual?: boolean;
168
- dbConnectors?: [DBConnector, ...DBConnector[]];
169
- };
170
- type EnrichedDataField = DataField & {
171
- dbPath: string;
172
- };
173
- type ThingType = 'entity' | 'relation' | 'attribute';
174
- type RightType = 'CREATE' | 'DELETE' | 'UPDATE' | 'LINK' | 'UNLINK';
175
- type ContentType = 'ID' | 'JSON' | 'COLOR' | 'BOOLEAN' | 'POINT' | 'FILE' | 'EMAIL' | 'PHONE' | 'WEEK_DAY' | 'DURATION' | 'HOUR' | 'TIME' | 'DATE' | 'RATING' | 'CURRENCY' | 'PERCENTAGE' | 'NUMBER_DECIMAL' | 'NUMBER' | 'URL' | 'PASSWORD' | 'LANGUAGE_TEXT' | 'RICH_TEXT' | 'TEXT';
176
- type CardinalityType = 'ONE' | 'MANY' | 'INTERVAL';
177
- type RelationClassType = 'SYMMETRICAL' | 'OWNED';
80
+
178
81
  type RequiredKey<T, K extends keyof T> = T & {
179
82
  [P in K]-?: T[P];
180
83
  };
@@ -191,10 +94,20 @@ type BQLMutationBlock = {
191
94
  $relation: string;
192
95
  });
193
96
  type FilledBQLMutationBlock = WithRequired<BQLMutationBlock, '$tempId' | '$op'>;
194
- type BQLFieldObj = {
195
- $path: string;
196
- } & Omit<RawBQLQuery, '$entity' | '$relation'>;
197
- type BQLField = string | BQLFieldObj;
97
+ type RawBQLMutation<T extends Record<string, any> = Record<string, any>> = ({
98
+ $id?: string;
99
+ $op?: 'create' | 'delete' | 'update';
100
+ $tempId?: string;
101
+ } | {
102
+ $entity: string;
103
+ } | {
104
+ $relation: string;
105
+ }) & T;
106
+ type ParsedBQLMutation = {
107
+ things: BQLMutationBlock[];
108
+ edges: BQLMutationBlock[];
109
+ };
110
+
198
111
  type RawBQLQuery = {
199
112
  $id?: string | string[];
200
113
  $filter?: Record<string, any>;
@@ -205,11 +118,6 @@ type RawBQLQuery = {
205
118
  } | {
206
119
  $relation: string;
207
120
  });
208
- type RawBQLMutation = ({
209
- $entity: string;
210
- } | {
211
- $relation: string;
212
- }) & Record<string, any>;
213
121
  type ParsedBQLQuery = Omit<RawBQLQuery, '$entity' | '$relation'> & {
214
122
  $localFilters?: Record<string, any>;
215
123
  $nestedFilters?: Record<string, any>;
@@ -218,10 +126,7 @@ type ParsedBQLQuery = Omit<RawBQLQuery, '$entity' | '$relation'> & {
218
126
  } | {
219
127
  $relation: EnrichedBormRelation;
220
128
  });
221
- type ParsedBQLMutation = {
222
- things: BQLMutationBlock[];
223
- edges: BQLMutationBlock[];
224
- };
129
+
225
130
  type TQLRequest = {
226
131
  entity?: string;
227
132
  roles?: {
@@ -247,15 +152,202 @@ type TQLEntityMutation = {
247
152
  request: string;
248
153
  }[];
249
154
  };
250
- type BQLResponseSingle = (({
251
- $entity: string;
252
- $id: string;
155
+
156
+ type BormSchema = {
157
+ entities: {
158
+ [s: string]: BormEntity;
159
+ };
160
+ relations: {
161
+ [s: string]: BormRelation;
162
+ };
163
+ };
164
+ type BormEntity = {
165
+ extends: string;
166
+ idFields?: readonly string[];
167
+ defaultDBConnector: DBConnector;
168
+ dataFields?: readonly DataField[];
169
+ linkFields?: readonly LinkField[];
253
170
  } | {
254
- $relation: string;
255
- $id: string;
256
- }) | undefined) & Record<string, any>;
257
- type BQLResponseMulti = BQLResponseSingle[];
258
- type BQLResponse = BQLResponseSingle | BQLResponseMulti;
171
+ extends?: string;
172
+ idFields: readonly string[];
173
+ defaultDBConnector: DBConnector;
174
+ dataFields?: readonly DataField[];
175
+ linkFields?: readonly LinkField[];
176
+ };
177
+ type BormRelation = BormEntity & {
178
+ defaultDBConnector: DBConnector & {
179
+ path: string;
180
+ };
181
+ roles?: {
182
+ [key: string]: RoleField;
183
+ };
184
+ };
185
+
186
+ type BormField = {
187
+ path: string;
188
+ cardinality: CardinalityType;
189
+ ordered?: boolean;
190
+ embedded?: boolean;
191
+ rights?: readonly RightType[];
192
+ };
193
+ type RoleField = {
194
+ cardinality: CardinalityType;
195
+ dbConnector?: DBConnector;
196
+ };
197
+ type LinkField = BormField & {
198
+ relation: string;
199
+ plays: string;
200
+ } & ({
201
+ target: 'role';
202
+ filter?: Filter | Filter[];
203
+ } | {
204
+ target: 'relation';
205
+ });
206
+ type LinkedFieldWithThing = LinkField & {
207
+ thing: string;
208
+ thingType: ThingType;
209
+ };
210
+ type DataField = BormField & {
211
+ shared?: boolean;
212
+ default?: any;
213
+ contentType: ContentType;
214
+ validations?: any;
215
+ isVirtual?: boolean;
216
+ dbConnectors?: [DBConnector, ...DBConnector[]];
217
+ };
218
+ type ContentType = 'ID' | 'JSON' | 'COLOR' | 'BOOLEAN' | 'POINT' | 'FILE' | 'EMAIL' | 'PHONE' | 'WEEK_DAY' | 'DURATION' | 'HOUR' | 'TIME' | 'DATE' | 'RATING' | 'CURRENCY' | 'PERCENTAGE' | 'NUMBER_DECIMAL' | 'NUMBER' | 'URL' | 'PASSWORD' | 'LANGUAGE_TEXT' | 'RICH_TEXT' | 'TEXT';
219
+ type ContentTypeMapping = {
220
+ ID: string;
221
+ JSON: any;
222
+ COLOR: string;
223
+ BOOLEAN: boolean;
224
+ POINT: {
225
+ x: number;
226
+ y: number;
227
+ };
228
+ FILE: string;
229
+ EMAIL: string;
230
+ PHONE: string;
231
+ WEEK_DAY: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
232
+ DURATION: number;
233
+ HOUR: number;
234
+ TIME: Date;
235
+ DATE: Date;
236
+ RATING: number;
237
+ CURRENCY: number;
238
+ PERCENTAGE: number;
239
+ NUMBER_DECIMAL: number;
240
+ NUMBER: number;
241
+ URL: string;
242
+ PASSWORD: string;
243
+ LANGUAGE_TEXT: string;
244
+ RICH_TEXT: string;
245
+ TEXT: string;
246
+ };
247
+ type CardinalityType = 'ONE' | 'MANY' | 'INTERVAL';
248
+ type RightType = 'CREATE' | 'DELETE' | 'UPDATE' | 'LINK' | 'UNLINK';
249
+ type BQLFieldObj = {
250
+ $path: string;
251
+ } & Omit<RawBQLQuery, '$entity' | '$relation'>;
252
+ type BQLField = string | BQLFieldObj;
253
+
254
+ type EnrichedBormSchema = {
255
+ entities: {
256
+ [s: string]: EnrichedBormEntity;
257
+ };
258
+ relations: {
259
+ [s: string]: EnrichedBormRelation;
260
+ };
261
+ };
262
+ type EnrichedBormEntity = Omit<BormEntity, 'linkFields' | 'idFields' | 'dataFields'> & {
263
+ extends?: string;
264
+ allExtends?: string[];
265
+ idFields: string[];
266
+ thingType: 'entity';
267
+ name: string;
268
+ computedFields: string[];
269
+ virtualFields: string[];
270
+ linkFields?: EnrichedLinkField[];
271
+ dataFields?: EnrichedDataField[];
272
+ };
273
+ type EnrichedBormRelation = Omit<BormRelation, 'linkFields' | 'dataFields'> & {
274
+ thingType: 'relation';
275
+ name: string;
276
+ computedFields: string[];
277
+ virtualFields: string[];
278
+ linkFields?: EnrichedLinkField[];
279
+ dataFields?: EnrichedDataField[];
280
+ roles: {
281
+ [key: string]: EnrichedRoleField;
282
+ };
283
+ };
284
+ type EnrichedRoleField = RoleField & {
285
+ playedBy?: LinkedFieldWithThing[];
286
+ name: string;
287
+ };
288
+ type EnrichedDataField = DataField & {
289
+ dbPath: string;
290
+ };
291
+ type EnrichedLinkField = BormField & {
292
+ name: string;
293
+ relation: string;
294
+ plays: string;
295
+ } & ({
296
+ target: 'role';
297
+ filter?: Filter | Filter[];
298
+ oppositeLinkFieldsPlayedBy: LinkedFieldWithThing[];
299
+ } | {
300
+ target: 'relation';
301
+ oppositeLinkFieldsPlayedBy: Pick<LinkedFieldWithThing, 'thing' | 'thingType' | 'plays'>[];
302
+ });
303
+
304
+ type ContentTypeToType<C extends keyof ContentTypeMapping> = ContentTypeMapping[C];
305
+ type HandleCardinality<T, C extends 'ONE' | 'MANY'> = C extends 'MANY' ? T[] : T;
306
+ type ContentTypeAndCardinality = {
307
+ contentType: keyof ContentTypeMapping;
308
+ cardinality: 'ONE' | 'MANY';
309
+ };
310
+ type ForDataField = {
311
+ path: string;
312
+ contentType: keyof ContentTypeMapping;
313
+ cardinality: 'ONE' | 'MANY';
314
+ };
315
+ type ForLinkField = {
316
+ path: string;
317
+ cardinality: 'ONE' | 'MANY';
318
+ };
319
+ type ForRoleFIeld = {
320
+ cardinality: 'ONE' | 'MANY';
321
+ };
322
+ type BaseSchemaEntity = {
323
+ dataFields: readonly ForDataField[];
324
+ linkFields?: readonly ForLinkField[];
325
+ };
326
+ type BaseSchemaRelation = BaseSchemaEntity & {
327
+ roles?: Record<string, ForRoleFIeld[]>;
328
+ };
329
+ type BaseSchema = BaseSchemaEntity | BaseSchemaRelation;
330
+ type FieldToType<F extends ContentTypeAndCardinality> = HandleCardinality<ContentTypeToType<F['contentType']>, F['cardinality']>;
331
+ type ExtractDataFields<S extends {
332
+ dataFields: readonly ForDataField[];
333
+ }> = {
334
+ [K in S['dataFields'][number]['path']]?: FieldToType<Extract<S['dataFields'][number], {
335
+ path: K;
336
+ }>>;
337
+ };
338
+ type ExtractLinkFields<S> = S extends {
339
+ linkFields?: readonly ForLinkField[];
340
+ } ? S['linkFields'] extends readonly ForLinkField[] ? {
341
+ [K in S['linkFields'][number]['path']]?: HandleCardinality<string, Extract<S['linkFields'][number], {
342
+ path: K;
343
+ }>['cardinality']>;
344
+ } : {} : {};
345
+ type ExtractRoles<S> = 'roles' extends keyof S ? {
346
+ [K in keyof S['roles']]?: S['roles'][K] extends {
347
+ cardinality: 'ONE' | 'MANY';
348
+ } ? HandleCardinality<string, S['roles'][K]['cardinality']> : never;
349
+ } : {};
350
+ type TypeGen<S extends BaseSchema> = ExtractDataFields<S> & ExtractLinkFields<S> & ExtractRoles<S>;
259
351
 
260
352
  type BormProps = {
261
353
  schema: BormSchema;
@@ -270,27 +362,9 @@ declare class BormClient {
270
362
  init: () => Promise<void>;
271
363
  introspect: () => Promise<BormSchema>;
272
364
  define: () => Promise<void>;
273
- query<T extends Record<string, any>>(query: RawBQLQuery & {
274
- $filter: Record<string, any>;
275
- } & ({
276
- $entity: string;
277
- } | {
278
- $relation: string;
279
- }), queryConfig?: any): Promise<(BQLResponseSingle & T)[] | (BQLResponseSingle & T)>;
280
- query<T extends Record<string, any>>(query: RawBQLQuery & {
281
- $id: string;
282
- }, queryConfig?: any): Promise<BQLResponseSingle & T>;
283
- query<T extends Record<string, any>>(query: RawBQLQuery & {
284
- $id: string[];
285
- }, queryConfig?: any): Promise<(BQLResponseSingle & T)[]>;
286
- query<T extends Record<string, any>>(query: Omit<RawBQLQuery, '$id'> & ({
287
- $entity: string;
288
- } | {
289
- $relation: string;
290
- }), queryConfig?: any): Promise<(BQLResponseSingle & T)[]>;
291
- mutate<T extends Record<string, any>>(mutation: RawBQLMutation, mutationConfig?: any): Promise<BQLResponseSingle & T>;
292
- mutate(mutation: RawBQLMutation[], mutationConfig?: any): Promise<BQLResponseMulti>;
365
+ query: (query: RawBQLQuery, queryConfig?: any) => Promise<BQLResponse>;
366
+ mutate: (mutation: RawBQLMutation | RawBQLMutation[], mutationConfig?: any) => Promise<BQLResponseMulti>;
293
367
  close: () => Promise<void>;
294
368
  }
295
369
 
296
- export { BQLField, BQLFieldObj, BQLMutationBlock, BQLResponse, BQLResponseMulti, BQLResponseSingle, BormConfig, BormEntity, BormField, BormRelation, BormSchema, CardinalityType, CommonProperties, ContentType, DBConnector, DBHandles, DataField, DataFilter, EnrichedBormEntity, EnrichedBormRelation, EnrichedBormSchema, EnrichedDataField, EnrichedLinkField, EnrichedRoleField, FilledBQLMutationBlock, Filter, LinkField, LinkFilter, LinkedFieldWithThing, MiddleFilter, ParsedBQLMutation, ParsedBQLQuery, Provider, ProviderObject, RawBQLMutation, RawBQLQuery, RelationClassType, RightType, RoleField, TQLEntityMutation, TQLRequest, ThingType, TypeDBClusterProviderObject, TypeDBProviderObject, BormClient as default };
370
+ export { BQLField, BQLFieldObj, BQLMutationBlock, BQLResponse, BQLResponseMulti, BQLResponseSingle, BormConfig, BormEntity, BormField, BormMetadata, BormRelation, BormSchema, CardinalityType, CommonProperties, ContentType, ContentTypeMapping, DBConnector, DBHandles, DataField, DataFilter, EnrichedBormEntity, EnrichedBormRelation, EnrichedBormSchema, EnrichedDataField, EnrichedLinkField, EnrichedRoleField, FilledBQLMutationBlock, Filter, LinkField, LinkFilter, LinkedFieldWithThing, MiddleFilter, ParsedBQLMutation, ParsedBQLQuery, Provider, ProviderObject, RawBQLMutation, RawBQLQuery, RightType, RoleField, TQLEntityMutation, TQLRequest, ThingType, TypeDBClusterProviderObject, TypeDBHandles, TypeDBProviderObject, TypeGen, WithBormMetadata, BormClient as default };