@blitznocode/blitz-orm 0.4.7 → 0.5.0

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.
package/dist/index.d.ts CHANGED
@@ -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,16 @@ 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 = ({
98
+ $entity: string;
99
+ } | {
100
+ $relation: string;
101
+ }) & Record<string, any>;
102
+ type ParsedBQLMutation = {
103
+ things: BQLMutationBlock[];
104
+ edges: BQLMutationBlock[];
105
+ };
106
+
198
107
  type RawBQLQuery = {
199
108
  $id?: string | string[];
200
109
  $filter?: Record<string, any>;
@@ -205,11 +114,6 @@ type RawBQLQuery = {
205
114
  } | {
206
115
  $relation: string;
207
116
  });
208
- type RawBQLMutation = ({
209
- $entity: string;
210
- } | {
211
- $relation: string;
212
- }) & Record<string, any>;
213
117
  type ParsedBQLQuery = Omit<RawBQLQuery, '$entity' | '$relation'> & {
214
118
  $localFilters?: Record<string, any>;
215
119
  $nestedFilters?: Record<string, any>;
@@ -218,10 +122,7 @@ type ParsedBQLQuery = Omit<RawBQLQuery, '$entity' | '$relation'> & {
218
122
  } | {
219
123
  $relation: EnrichedBormRelation;
220
124
  });
221
- type ParsedBQLMutation = {
222
- things: BQLMutationBlock[];
223
- edges: BQLMutationBlock[];
224
- };
125
+
225
126
  type TQLRequest = {
226
127
  entity?: string;
227
128
  roles?: {
@@ -247,15 +148,202 @@ type TQLEntityMutation = {
247
148
  request: string;
248
149
  }[];
249
150
  };
250
- type BQLResponseSingle = (({
251
- $entity: string;
252
- $id: string;
151
+
152
+ type BormSchema = {
153
+ entities: {
154
+ [s: string]: BormEntity;
155
+ };
156
+ relations: {
157
+ [s: string]: BormRelation;
158
+ };
159
+ };
160
+ type BormEntity = {
161
+ extends: string;
162
+ idFields?: readonly string[];
163
+ defaultDBConnector: DBConnector;
164
+ dataFields?: readonly DataField[];
165
+ linkFields?: readonly LinkField[];
253
166
  } | {
254
- $relation: string;
255
- $id: string;
256
- }) | undefined) & Record<string, any>;
257
- type BQLResponseMulti = BQLResponseSingle[];
258
- type BQLResponse = BQLResponseSingle | BQLResponseMulti;
167
+ extends?: string;
168
+ idFields: readonly string[];
169
+ defaultDBConnector: DBConnector;
170
+ dataFields?: readonly DataField[];
171
+ linkFields?: readonly LinkField[];
172
+ };
173
+ type BormRelation = BormEntity & {
174
+ defaultDBConnector: DBConnector & {
175
+ path: string;
176
+ };
177
+ roles?: {
178
+ [key: string]: RoleField;
179
+ };
180
+ };
181
+
182
+ type BormField = {
183
+ path: string;
184
+ cardinality: CardinalityType;
185
+ ordered?: boolean;
186
+ embedded?: boolean;
187
+ rights?: readonly RightType[];
188
+ };
189
+ type RoleField = {
190
+ cardinality: CardinalityType;
191
+ dbConnector?: DBConnector;
192
+ };
193
+ type LinkField = BormField & {
194
+ relation: string;
195
+ plays: string;
196
+ } & ({
197
+ target: 'role';
198
+ filter?: Filter | Filter[];
199
+ } | {
200
+ target: 'relation';
201
+ });
202
+ type LinkedFieldWithThing = LinkField & {
203
+ thing: string;
204
+ thingType: ThingType;
205
+ };
206
+ type DataField = BormField & {
207
+ shared?: boolean;
208
+ default?: any;
209
+ contentType: ContentType;
210
+ validations?: any;
211
+ isVirtual?: boolean;
212
+ dbConnectors?: [DBConnector, ...DBConnector[]];
213
+ };
214
+ 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';
215
+ type ContentTypeMapping = {
216
+ ID: string;
217
+ JSON: any;
218
+ COLOR: string;
219
+ BOOLEAN: boolean;
220
+ POINT: {
221
+ x: number;
222
+ y: number;
223
+ };
224
+ FILE: string;
225
+ EMAIL: string;
226
+ PHONE: string;
227
+ WEEK_DAY: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
228
+ DURATION: number;
229
+ HOUR: number;
230
+ TIME: Date;
231
+ DATE: Date;
232
+ RATING: number;
233
+ CURRENCY: number;
234
+ PERCENTAGE: number;
235
+ NUMBER_DECIMAL: number;
236
+ NUMBER: number;
237
+ URL: string;
238
+ PASSWORD: string;
239
+ LANGUAGE_TEXT: string;
240
+ RICH_TEXT: string;
241
+ TEXT: string;
242
+ };
243
+ type CardinalityType = 'ONE' | 'MANY' | 'INTERVAL';
244
+ type RightType = 'CREATE' | 'DELETE' | 'UPDATE' | 'LINK' | 'UNLINK';
245
+ type BQLFieldObj = {
246
+ $path: string;
247
+ } & Omit<RawBQLQuery, '$entity' | '$relation'>;
248
+ type BQLField = string | BQLFieldObj;
249
+
250
+ type EnrichedBormSchema = {
251
+ entities: {
252
+ [s: string]: EnrichedBormEntity;
253
+ };
254
+ relations: {
255
+ [s: string]: EnrichedBormRelation;
256
+ };
257
+ };
258
+ type EnrichedBormEntity = Omit<BormEntity, 'linkFields' | 'idFields' | 'dataFields'> & {
259
+ extends?: string;
260
+ allExtends?: string[];
261
+ idFields: string[];
262
+ thingType: 'entity';
263
+ name: string;
264
+ computedFields: string[];
265
+ virtualFields: string[];
266
+ linkFields?: EnrichedLinkField[];
267
+ dataFields?: EnrichedDataField[];
268
+ };
269
+ type EnrichedBormRelation = Omit<BormRelation, 'linkFields' | 'dataFields'> & {
270
+ thingType: 'relation';
271
+ name: string;
272
+ computedFields: string[];
273
+ virtualFields: string[];
274
+ linkFields?: EnrichedLinkField[];
275
+ dataFields?: EnrichedDataField[];
276
+ roles: {
277
+ [key: string]: EnrichedRoleField;
278
+ };
279
+ };
280
+ type EnrichedRoleField = RoleField & {
281
+ playedBy?: LinkedFieldWithThing[];
282
+ name: string;
283
+ };
284
+ type EnrichedDataField = DataField & {
285
+ dbPath: string;
286
+ };
287
+ type EnrichedLinkField = BormField & {
288
+ name: string;
289
+ relation: string;
290
+ plays: string;
291
+ } & ({
292
+ target: 'role';
293
+ filter?: Filter | Filter[];
294
+ oppositeLinkFieldsPlayedBy: LinkedFieldWithThing[];
295
+ } | {
296
+ target: 'relation';
297
+ oppositeLinkFieldsPlayedBy: Pick<LinkedFieldWithThing, 'thing' | 'thingType' | 'plays'>[];
298
+ });
299
+
300
+ type ContentTypeToType<C extends keyof ContentTypeMapping> = ContentTypeMapping[C];
301
+ type HandleCardinality<T, C extends 'ONE' | 'MANY'> = C extends 'MANY' ? T[] : T;
302
+ type ContentTypeAndCardinality = {
303
+ contentType: keyof ContentTypeMapping;
304
+ cardinality: 'ONE' | 'MANY';
305
+ };
306
+ type ForDataField = {
307
+ path: string;
308
+ contentType: keyof ContentTypeMapping;
309
+ cardinality: 'ONE' | 'MANY';
310
+ };
311
+ type ForLinkField = {
312
+ path: string;
313
+ cardinality: 'ONE' | 'MANY';
314
+ };
315
+ type ForRoleFIeld = {
316
+ cardinality: 'ONE' | 'MANY';
317
+ };
318
+ type BaseSchemaEntity = {
319
+ dataFields: readonly ForDataField[];
320
+ linkFields?: readonly ForLinkField[];
321
+ };
322
+ type BaseSchemaRelation = BaseSchemaEntity & {
323
+ roles?: Record<string, ForRoleFIeld[]>;
324
+ };
325
+ type BaseSchema = BaseSchemaEntity | BaseSchemaRelation;
326
+ type FieldToType<F extends ContentTypeAndCardinality> = HandleCardinality<ContentTypeToType<F['contentType']>, F['cardinality']>;
327
+ type ExtractDataFields<S extends {
328
+ dataFields: readonly ForDataField[];
329
+ }> = {
330
+ [K in S['dataFields'][number]['path']]?: FieldToType<Extract<S['dataFields'][number], {
331
+ path: K;
332
+ }>>;
333
+ };
334
+ type ExtractLinkFields<S> = S extends {
335
+ linkFields?: readonly ForLinkField[];
336
+ } ? S['linkFields'] extends readonly ForLinkField[] ? {
337
+ [K in S['linkFields'][number]['path']]?: HandleCardinality<string, Extract<S['linkFields'][number], {
338
+ path: K;
339
+ }>['cardinality']>;
340
+ } : {} : {};
341
+ type ExtractRoles<S> = 'roles' extends keyof S ? {
342
+ [K in keyof S['roles']]?: S['roles'][K] extends {
343
+ cardinality: 'ONE' | 'MANY';
344
+ } ? HandleCardinality<string, S['roles'][K]['cardinality']> : never;
345
+ } : {};
346
+ type TypeGen<S extends BaseSchema> = ExtractDataFields<S> & ExtractLinkFields<S> & ExtractRoles<S>;
259
347
 
260
348
  type BormProps = {
261
349
  schema: BormSchema;
@@ -270,27 +358,9 @@ declare class BormClient {
270
358
  init: () => Promise<void>;
271
359
  introspect: () => Promise<BormSchema>;
272
360
  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>;
361
+ query: (query: RawBQLQuery, queryConfig?: any) => Promise<BQLResponse>;
362
+ mutate: (mutation: RawBQLMutation | RawBQLMutation[], mutationConfig?: any) => Promise<BQLResponseMulti>;
293
363
  close: () => Promise<void>;
294
364
  }
295
365
 
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 };
366
+ 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 };