@snowtop/ent 0.1.0-alpha1 → 0.1.0-alpha100
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/action/action.d.ts +38 -30
- package/action/action.js +22 -7
- package/action/executor.d.ts +4 -4
- package/action/executor.js +8 -3
- package/action/experimental_action.d.ts +32 -22
- package/action/experimental_action.js +35 -9
- package/action/index.d.ts +2 -0
- package/action/index.js +7 -1
- package/action/orchestrator.d.ts +48 -16
- package/action/orchestrator.js +343 -81
- package/action/privacy.d.ts +2 -2
- package/action/relative_value.d.ts +47 -0
- package/action/relative_value.js +125 -0
- package/action/transaction.d.ts +10 -0
- package/action/transaction.js +23 -0
- package/auth/auth.d.ts +1 -1
- package/core/base.d.ts +54 -27
- package/core/base.js +23 -1
- package/core/clause.d.ts +105 -3
- package/core/clause.js +563 -30
- package/core/config.d.ts +30 -1
- package/core/config.js +24 -1
- package/core/context.d.ts +5 -3
- package/core/context.js +20 -2
- package/core/convert.d.ts +1 -1
- package/core/date.js +1 -5
- package/core/db.d.ts +14 -11
- package/core/db.js +22 -8
- package/core/ent.d.ts +82 -28
- package/core/ent.js +692 -202
- package/core/loaders/assoc_count_loader.d.ts +3 -2
- package/core/loaders/assoc_count_loader.js +10 -2
- package/core/loaders/assoc_edge_loader.d.ts +3 -3
- package/core/loaders/assoc_edge_loader.js +13 -15
- package/core/loaders/index.d.ts +1 -1
- package/core/loaders/index.js +1 -3
- package/core/loaders/index_loader.d.ts +2 -2
- package/core/loaders/index_loader.js +1 -0
- package/core/loaders/loader.js +5 -5
- package/core/loaders/object_loader.d.ts +13 -7
- package/core/loaders/object_loader.js +95 -32
- package/core/loaders/query_loader.d.ts +6 -12
- package/core/loaders/query_loader.js +52 -11
- package/core/loaders/raw_count_loader.d.ts +2 -2
- package/core/loaders/raw_count_loader.js +5 -1
- package/core/logger.d.ts +1 -1
- package/core/logger.js +1 -0
- package/core/privacy.d.ts +26 -25
- package/core/privacy.js +23 -24
- package/core/query/assoc_query.d.ts +7 -6
- package/core/query/assoc_query.js +9 -1
- package/core/query/custom_clause_query.d.ts +26 -0
- package/core/query/custom_clause_query.js +78 -0
- package/core/query/custom_query.d.ts +20 -5
- package/core/query/custom_query.js +87 -12
- package/core/query/index.d.ts +1 -0
- package/core/query/index.js +3 -1
- package/core/query/query.d.ts +8 -4
- package/core/query/query.js +101 -53
- package/core/query/shared_assoc_test.d.ts +2 -1
- package/core/query/shared_assoc_test.js +35 -45
- package/core/query/shared_test.d.ts +8 -1
- package/core/query/shared_test.js +469 -236
- package/core/viewer.d.ts +4 -3
- package/core/viewer.js +5 -1
- package/graphql/builtins/connection.js +3 -3
- package/graphql/builtins/edge.js +2 -2
- package/graphql/builtins/node.js +1 -1
- package/graphql/graphql.d.ts +17 -9
- package/graphql/graphql.js +47 -30
- package/graphql/index.d.ts +1 -1
- package/graphql/index.js +3 -4
- package/graphql/mutations/union.d.ts +2 -0
- package/graphql/mutations/union.js +35 -0
- package/graphql/node_resolver.d.ts +0 -1
- package/graphql/query/connection_type.d.ts +9 -9
- package/graphql/query/connection_type.js +6 -6
- package/graphql/query/edge_connection.d.ts +9 -9
- package/graphql/query/page_info.d.ts +1 -1
- package/graphql/query/page_info.js +4 -4
- package/graphql/query/shared_assoc_test.js +3 -3
- package/graphql/query/shared_edge_connection.js +1 -19
- package/graphql/scalars/time.d.ts +1 -1
- package/imports/index.d.ts +6 -1
- package/imports/index.js +19 -4
- package/index.d.ts +23 -1
- package/index.js +32 -6
- package/package.json +18 -17
- package/parse_schema/parse.d.ts +45 -8
- package/parse_schema/parse.js +193 -15
- package/schema/base_schema.d.ts +38 -1
- package/schema/base_schema.js +53 -2
- package/schema/field.d.ts +75 -21
- package/schema/field.js +185 -72
- package/schema/index.d.ts +4 -2
- package/schema/index.js +15 -2
- package/schema/json_field.d.ts +13 -1
- package/schema/json_field.js +28 -1
- package/schema/schema.d.ts +125 -10
- package/schema/schema.js +133 -5
- package/schema/struct_field.d.ts +27 -0
- package/schema/struct_field.js +138 -0
- package/schema/union_field.d.ts +23 -0
- package/schema/union_field.js +79 -0
- package/scripts/custom_compiler.js +10 -6
- package/scripts/custom_graphql.js +224 -36
- package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
- package/scripts/migrate_v0.1.js +36 -0
- package/scripts/move_types.d.ts +1 -0
- package/scripts/move_types.js +117 -0
- package/scripts/read_schema.js +35 -6
- package/testutils/action/complex_schemas.d.ts +69 -0
- package/testutils/action/complex_schemas.js +398 -0
- package/testutils/builder.d.ts +52 -49
- package/testutils/builder.js +143 -44
- package/testutils/context/test_context.d.ts +2 -2
- package/testutils/context/test_context.js +7 -1
- package/testutils/db/fixture.d.ts +10 -0
- package/testutils/db/fixture.js +26 -0
- package/testutils/db/{test_db.d.ts → temp_db.d.ts} +26 -9
- package/testutils/db/{test_db.js → temp_db.js} +190 -46
- package/testutils/db/value.d.ts +7 -0
- package/testutils/db/value.js +251 -0
- package/testutils/db_mock.d.ts +16 -4
- package/testutils/db_mock.js +51 -6
- package/testutils/db_time_zone.d.ts +4 -0
- package/testutils/db_time_zone.js +41 -0
- package/testutils/ent-graphql-tests/index.d.ts +9 -1
- package/testutils/ent-graphql-tests/index.js +53 -25
- package/testutils/fake_data/const.d.ts +2 -1
- package/testutils/fake_data/const.js +3 -0
- package/testutils/fake_data/fake_contact.d.ts +10 -10
- package/testutils/fake_data/fake_contact.js +23 -21
- package/testutils/fake_data/fake_event.d.ts +8 -9
- package/testutils/fake_data/fake_event.js +25 -28
- package/testutils/fake_data/fake_tag.d.ts +36 -0
- package/testutils/fake_data/fake_tag.js +89 -0
- package/testutils/fake_data/fake_user.d.ts +10 -11
- package/testutils/fake_data/fake_user.js +20 -23
- package/testutils/fake_data/index.js +5 -1
- package/testutils/fake_data/internal.d.ts +2 -0
- package/testutils/fake_data/internal.js +7 -1
- package/testutils/fake_data/tag_query.d.ts +13 -0
- package/testutils/fake_data/tag_query.js +43 -0
- package/testutils/fake_data/test_helpers.d.ts +11 -4
- package/testutils/fake_data/test_helpers.js +29 -13
- package/testutils/fake_data/user_query.d.ts +13 -6
- package/testutils/fake_data/user_query.js +54 -22
- package/testutils/fake_log.d.ts +3 -3
- package/testutils/parse_sql.d.ts +6 -0
- package/testutils/parse_sql.js +16 -2
- package/testutils/test_edge_global_schema.d.ts +15 -0
- package/testutils/test_edge_global_schema.js +62 -0
- package/testutils/write.d.ts +2 -2
- package/testutils/write.js +33 -7
- package/tsc/ast.d.ts +44 -0
- package/tsc/ast.js +277 -0
- package/tsc/compilerOptions.d.ts +6 -0
- package/tsc/compilerOptions.js +45 -2
- package/tsc/move_generated.d.ts +1 -0
- package/tsc/move_generated.js +164 -0
- package/tsc/transform.d.ts +22 -0
- package/tsc/transform.js +181 -0
- package/tsc/transform_action.d.ts +22 -0
- package/tsc/transform_action.js +183 -0
- package/tsc/transform_ent.d.ts +17 -0
- package/tsc/transform_ent.js +59 -0
- package/tsc/transform_schema.d.ts +27 -0
- package/tsc/transform_schema.js +383 -0
- package/graphql/enums.d.ts +0 -3
- package/graphql/enums.js +0 -25
- package/scripts/transform_schema.js +0 -288
package/schema/schema.d.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
import { Data, Ent, LoaderInfo } from "../core/base";
|
|
2
|
-
import { Builder } from "../action/action";
|
|
1
|
+
import { Data, Ent, LoaderInfo, PrivacyPolicy, Viewer } from "../core/base";
|
|
2
|
+
import { Builder, Changeset } from "../action/action";
|
|
3
|
+
import { Clause } from "../core/clause";
|
|
4
|
+
import { AssocEdgeInput } from "../core/ent";
|
|
3
5
|
export declare type FieldMap = {
|
|
4
6
|
[key: string]: Field;
|
|
5
7
|
};
|
|
8
|
+
interface FieldInfo {
|
|
9
|
+
dbCol: string;
|
|
10
|
+
inputKey: string;
|
|
11
|
+
}
|
|
12
|
+
export type FieldInfoMap = {
|
|
13
|
+
[key: string]: FieldInfo;
|
|
14
|
+
};
|
|
15
|
+
export interface GlobalSchema {
|
|
16
|
+
edges?: Edge[];
|
|
17
|
+
extraEdgeFields?: FieldMap;
|
|
18
|
+
transformEdgeRead?: () => Clause;
|
|
19
|
+
transformEdgeWrite?: (stmt: EdgeUpdateOperation) => TransformedEdgeUpdateOperation | null;
|
|
20
|
+
}
|
|
21
|
+
type FieldOverride = Pick<FieldOptions, "nullable" | "storageKey" | "serverDefault" | "unique" | "hideFromGraphQL" | "graphqlName" | "index">;
|
|
22
|
+
export type FieldOverrideMap = {
|
|
23
|
+
[key: string]: FieldOverride;
|
|
24
|
+
};
|
|
6
25
|
export default interface Schema {
|
|
7
26
|
fields: FieldMap;
|
|
27
|
+
fieldOverrides?: FieldOverrideMap;
|
|
8
28
|
tableName?: string;
|
|
9
29
|
patterns?: Pattern[];
|
|
10
30
|
edges?: Edge[];
|
|
@@ -53,15 +73,47 @@ export interface AssocEdgeGroup {
|
|
|
53
73
|
tableName?: string;
|
|
54
74
|
assocEdges: AssocEdge[];
|
|
55
75
|
statusEnums?: string[];
|
|
56
|
-
|
|
76
|
+
viewerBased?: boolean;
|
|
77
|
+
nullStates?: string | string[];
|
|
57
78
|
nullStateFn?: string;
|
|
58
79
|
edgeAction?: EdgeGroupAction;
|
|
59
80
|
}
|
|
60
|
-
export
|
|
81
|
+
export type Edge = AssocEdge;
|
|
61
82
|
export interface Pattern {
|
|
62
83
|
name: string;
|
|
63
84
|
fields: FieldMap;
|
|
85
|
+
disableMixin?: boolean;
|
|
64
86
|
edges?: Edge[];
|
|
87
|
+
transformRead?: () => Clause;
|
|
88
|
+
transformWrite?: <T extends Ent<TViewer>, TViewer extends Viewer = Viewer>(stmt: UpdateOperation<T, TViewer>) => TransformedUpdateOperation<T, TViewer> | null;
|
|
89
|
+
transformsDelete?: boolean;
|
|
90
|
+
transformsInsert?: boolean;
|
|
91
|
+
transformsUpdate?: boolean;
|
|
92
|
+
}
|
|
93
|
+
export declare enum SQLStatementOperation {
|
|
94
|
+
Insert = "insert",
|
|
95
|
+
Update = "update",
|
|
96
|
+
Delete = "delete"
|
|
97
|
+
}
|
|
98
|
+
export interface EdgeUpdateOperation {
|
|
99
|
+
op: SQLStatementOperation;
|
|
100
|
+
edge: AssocEdgeInput;
|
|
101
|
+
}
|
|
102
|
+
export interface TransformedEdgeUpdateOperation {
|
|
103
|
+
op: SQLStatementOperation;
|
|
104
|
+
data?: Data;
|
|
105
|
+
}
|
|
106
|
+
export interface UpdateOperation<TEnt extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
|
|
107
|
+
op: SQLStatementOperation;
|
|
108
|
+
builder: Builder<TEnt, TViewer, any>;
|
|
109
|
+
input: Data;
|
|
110
|
+
data?: Map<string, any>;
|
|
111
|
+
}
|
|
112
|
+
export interface TransformedUpdateOperation<T extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
|
|
113
|
+
op: SQLStatementOperation;
|
|
114
|
+
data?: Data;
|
|
115
|
+
existingEnt?: T | null;
|
|
116
|
+
changeset?(): Promise<Changeset> | Changeset;
|
|
65
117
|
}
|
|
66
118
|
export declare enum DBType {
|
|
67
119
|
UUID = "UUID",
|
|
@@ -77,6 +129,7 @@ export declare enum DBType {
|
|
|
77
129
|
JSONB = "JSONB",
|
|
78
130
|
Enum = "Enum",
|
|
79
131
|
StringEnum = "StringEnum",
|
|
132
|
+
IntEnum = "IntEnum",
|
|
80
133
|
Date = "Date",
|
|
81
134
|
Time = "Time",
|
|
82
135
|
Timetz = "Timetz",
|
|
@@ -85,10 +138,18 @@ export declare enum DBType {
|
|
|
85
138
|
export interface ImportType {
|
|
86
139
|
path: string;
|
|
87
140
|
type: string;
|
|
141
|
+
[x: string]: any;
|
|
142
|
+
}
|
|
143
|
+
export interface ConvertType {
|
|
144
|
+
path: string;
|
|
145
|
+
function: string;
|
|
88
146
|
}
|
|
89
147
|
declare type EnumMap = {
|
|
90
148
|
[key: string]: string;
|
|
91
149
|
};
|
|
150
|
+
declare type IntEnumMap = {
|
|
151
|
+
[key: string]: number;
|
|
152
|
+
};
|
|
92
153
|
export interface Type {
|
|
93
154
|
dbType: DBType;
|
|
94
155
|
listElemType?: Type;
|
|
@@ -96,7 +157,13 @@ export interface Type {
|
|
|
96
157
|
graphQLType?: string;
|
|
97
158
|
values?: string[];
|
|
98
159
|
enumMap?: EnumMap;
|
|
160
|
+
intEnumMap?: IntEnumMap;
|
|
161
|
+
deprecatedIntEnumMap?: IntEnumMap;
|
|
162
|
+
disableUnknownType?: boolean;
|
|
99
163
|
importType?: ImportType;
|
|
164
|
+
subFields?: FieldMap;
|
|
165
|
+
unionFields?: FieldMap;
|
|
166
|
+
[x: string]: any;
|
|
100
167
|
}
|
|
101
168
|
export interface ForeignKey {
|
|
102
169
|
schema: string;
|
|
@@ -104,8 +171,9 @@ export interface ForeignKey {
|
|
|
104
171
|
name?: string;
|
|
105
172
|
disableIndex?: boolean;
|
|
106
173
|
disableBuilderType?: boolean;
|
|
174
|
+
[x: string]: any;
|
|
107
175
|
}
|
|
108
|
-
|
|
176
|
+
type getLoaderInfoFn = (type: string) => LoaderInfo;
|
|
109
177
|
export interface InverseFieldEdge {
|
|
110
178
|
name: string;
|
|
111
179
|
edgeConstName?: string;
|
|
@@ -119,13 +187,16 @@ export interface FieldEdge {
|
|
|
119
187
|
getLoaderInfoFromSchema?: getLoaderInfoFn;
|
|
120
188
|
disableBuilderType?: boolean;
|
|
121
189
|
}
|
|
190
|
+
interface PrivateOptions {
|
|
191
|
+
exposeToActions?: boolean;
|
|
192
|
+
}
|
|
122
193
|
export interface FieldOptions {
|
|
123
194
|
nullable?: boolean;
|
|
124
195
|
storageKey?: string;
|
|
125
196
|
serverDefault?: any;
|
|
126
197
|
unique?: boolean;
|
|
127
198
|
hideFromGraphQL?: boolean;
|
|
128
|
-
private?: boolean;
|
|
199
|
+
private?: boolean | PrivateOptions;
|
|
129
200
|
sensitive?: boolean;
|
|
130
201
|
graphqlName?: string;
|
|
131
202
|
index?: boolean;
|
|
@@ -133,29 +204,51 @@ export interface FieldOptions {
|
|
|
133
204
|
fieldEdge?: FieldEdge;
|
|
134
205
|
primaryKey?: boolean;
|
|
135
206
|
disableUserEditable?: boolean;
|
|
207
|
+
disableUserGraphQLEditable?: boolean;
|
|
136
208
|
defaultValueOnCreate?(builder: Builder<Ent>, input: Data): any;
|
|
137
209
|
defaultToViewerOnCreate?: boolean;
|
|
138
210
|
defaultValueOnEdit?(builder: Builder<Ent>, input: Data): any;
|
|
139
211
|
derivedWhenEmbedded?: boolean;
|
|
140
212
|
polymorphic?: boolean | PolymorphicOptions;
|
|
213
|
+
privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
|
|
141
214
|
getDerivedFields?(name: string): FieldMap;
|
|
215
|
+
convert?: ConvertType;
|
|
216
|
+
fetchOnDemand?: boolean;
|
|
217
|
+
dbOnly?: boolean;
|
|
218
|
+
[x: string]: any;
|
|
142
219
|
}
|
|
143
220
|
export interface PolymorphicOptions {
|
|
221
|
+
name?: string;
|
|
144
222
|
types?: string[];
|
|
145
223
|
hideFromInverseGraphQL?: boolean;
|
|
146
224
|
disableBuilderType?: boolean;
|
|
225
|
+
serverDefault?: any;
|
|
147
226
|
}
|
|
148
227
|
export interface Field extends FieldOptions {
|
|
149
228
|
type: Type;
|
|
150
229
|
valid?(val: any): Promise<boolean> | boolean;
|
|
151
|
-
|
|
230
|
+
validateWithFullData?(val: any, builder: Builder<any>): boolean | Promise<boolean>;
|
|
231
|
+
format?(val: any, nested?: boolean): any;
|
|
152
232
|
logValue(val: any): any;
|
|
153
233
|
}
|
|
154
234
|
export interface SchemaConstructor {
|
|
155
235
|
new (): Schema;
|
|
156
236
|
}
|
|
157
|
-
export
|
|
237
|
+
export type SchemaInputType = Schema | SchemaConstructor;
|
|
238
|
+
export declare function getSchema(value: SchemaInputType): Schema;
|
|
158
239
|
export declare function getFields(value: SchemaInputType): Map<string, Field>;
|
|
240
|
+
/**
|
|
241
|
+
* @deprecated should only be used by tests
|
|
242
|
+
*/
|
|
243
|
+
export declare function getStorageKey(field: Field, fieldName: string): string;
|
|
244
|
+
export declare function getFieldsWithPrivacy(value: SchemaInputType, fieldMap: FieldInfoMap): Map<string, PrivacyPolicy>;
|
|
245
|
+
export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
|
|
246
|
+
interface objectLoaderOptions {
|
|
247
|
+
clause?: () => Clause | undefined;
|
|
248
|
+
instanceKey?: string;
|
|
249
|
+
}
|
|
250
|
+
export declare function getObjectLoaderProperties(value: SchemaInputType, tableName: string): objectLoaderOptions | undefined;
|
|
251
|
+
export declare function getTransformedUpdateOp<TEnt extends Ent<TViewer>, TViewer extends Viewer>(value: SchemaInputType, stmt: UpdateOperation<TEnt, TViewer>): TransformedUpdateOperation<TEnt> | null;
|
|
159
252
|
export declare enum ActionOperation {
|
|
160
253
|
Create = 1,
|
|
161
254
|
Edit = 2,
|
|
@@ -165,8 +258,8 @@ export declare enum ActionOperation {
|
|
|
165
258
|
RemoveEdge = 32,
|
|
166
259
|
EdgeGroup = 64
|
|
167
260
|
}
|
|
168
|
-
|
|
169
|
-
|
|
261
|
+
type actionFieldType = "ID" | "Boolean" | "Int" | "Float" | "String" | "Time" | "Object";
|
|
262
|
+
type NullableListOptions = "contents" | "contentsAndList";
|
|
170
263
|
export interface ActionField {
|
|
171
264
|
name: string;
|
|
172
265
|
type: actionFieldType;
|
|
@@ -174,6 +267,8 @@ export interface ActionField {
|
|
|
174
267
|
list?: boolean;
|
|
175
268
|
actionName?: string;
|
|
176
269
|
excludedFields?: string[];
|
|
270
|
+
hideFromGraphQL?: boolean;
|
|
271
|
+
[x: string]: any;
|
|
177
272
|
}
|
|
178
273
|
export interface Action {
|
|
179
274
|
operation: ActionOperation;
|
|
@@ -187,6 +282,7 @@ export interface Action {
|
|
|
187
282
|
optionalFields?: string[];
|
|
188
283
|
requiredFields?: string[];
|
|
189
284
|
noFields?: boolean;
|
|
285
|
+
[x: string]: any;
|
|
190
286
|
}
|
|
191
287
|
export declare const NoFields = "__NO_FIELDS__";
|
|
192
288
|
export declare function requiredField(field: string): string;
|
|
@@ -197,16 +293,35 @@ export interface Constraint {
|
|
|
197
293
|
columns: string[];
|
|
198
294
|
fkey?: ForeignKeyInfo;
|
|
199
295
|
condition?: string;
|
|
296
|
+
[x: string]: any;
|
|
297
|
+
}
|
|
298
|
+
export interface FullTextWeight {
|
|
299
|
+
A?: string[];
|
|
300
|
+
B?: string[];
|
|
301
|
+
C?: string[];
|
|
302
|
+
D?: string[];
|
|
303
|
+
}
|
|
304
|
+
export interface FullText {
|
|
305
|
+
generatedColumnName?: string;
|
|
306
|
+
language?: "english" | "french" | "german" | "simple";
|
|
307
|
+
languageColumn?: string;
|
|
308
|
+
indexType?: "gin" | "gist";
|
|
309
|
+
weights?: FullTextWeight;
|
|
310
|
+
[x: string]: any;
|
|
200
311
|
}
|
|
201
312
|
export interface Index {
|
|
202
313
|
name: string;
|
|
203
314
|
columns: string[];
|
|
204
315
|
unique?: boolean;
|
|
316
|
+
fulltext?: FullText;
|
|
317
|
+
indexType?: "gin" | "btree";
|
|
318
|
+
[x: string]: any;
|
|
205
319
|
}
|
|
206
320
|
export interface ForeignKeyInfo {
|
|
207
321
|
tableName: string;
|
|
208
322
|
ondelete?: "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION";
|
|
209
323
|
columns: string[];
|
|
324
|
+
[x: string]: any;
|
|
210
325
|
}
|
|
211
326
|
export declare enum ConstraintType {
|
|
212
327
|
PrimaryKey = "primary",
|
package/schema/schema.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getFields = exports.DBType = void 0;
|
|
3
|
+
exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getTransformedUpdateOp = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.getFieldsWithPrivacy = exports.getStorageKey = exports.getFields = exports.getSchema = exports.DBType = exports.SQLStatementOperation = void 0;
|
|
4
|
+
const snake_case_1 = require("snake-case");
|
|
5
|
+
// we also want this transformation to exist on a per-action basis
|
|
6
|
+
// if it exists on an action, we don't do the global schema transformation
|
|
7
|
+
var SQLStatementOperation;
|
|
8
|
+
(function (SQLStatementOperation) {
|
|
9
|
+
// transform insert e.g. to an update based on whatever logic
|
|
10
|
+
SQLStatementOperation["Insert"] = "insert";
|
|
11
|
+
// // transform select e.g. deleted_at. can't change from select to different query type
|
|
12
|
+
// // but can change the query
|
|
13
|
+
// Select = "select",
|
|
14
|
+
// e.g. change updated value
|
|
15
|
+
SQLStatementOperation["Update"] = "update";
|
|
16
|
+
// delete -> update theoretically e.g. deleted_at
|
|
17
|
+
SQLStatementOperation["Delete"] = "delete";
|
|
18
|
+
})(SQLStatementOperation = exports.SQLStatementOperation || (exports.SQLStatementOperation = {}));
|
|
4
19
|
// we want --strictNullChecks flag so nullable is used to type graphql, ts, db
|
|
5
20
|
// should eventually generate (boolean | null) etc
|
|
6
21
|
// supported db types
|
|
@@ -20,6 +35,7 @@ var DBType;
|
|
|
20
35
|
DBType["JSONB"] = "JSONB";
|
|
21
36
|
DBType["Enum"] = "Enum";
|
|
22
37
|
DBType["StringEnum"] = "StringEnum";
|
|
38
|
+
DBType["IntEnum"] = "IntEnum";
|
|
23
39
|
DBType["Date"] = "Date";
|
|
24
40
|
DBType["Time"] = "Time";
|
|
25
41
|
DBType["Timetz"] = "Timetz";
|
|
@@ -28,17 +44,23 @@ var DBType;
|
|
|
28
44
|
function isSchema(value) {
|
|
29
45
|
return value.fields !== undefined;
|
|
30
46
|
}
|
|
31
|
-
function
|
|
32
|
-
let schema;
|
|
47
|
+
function getSchema(value) {
|
|
33
48
|
if (isSchema(value)) {
|
|
34
|
-
|
|
49
|
+
return value;
|
|
35
50
|
}
|
|
36
51
|
else {
|
|
37
|
-
|
|
52
|
+
return new value();
|
|
38
53
|
}
|
|
54
|
+
}
|
|
55
|
+
exports.getSchema = getSchema;
|
|
56
|
+
function getFields(value) {
|
|
57
|
+
const schema = getSchema(value);
|
|
39
58
|
function addFields(fields) {
|
|
40
59
|
for (const name in fields) {
|
|
41
60
|
const field = fields[name];
|
|
61
|
+
if (field.dbOnly) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
42
64
|
if (field.getDerivedFields !== undefined) {
|
|
43
65
|
addFields(field.getDerivedFields(name));
|
|
44
66
|
}
|
|
@@ -55,6 +77,109 @@ function getFields(value) {
|
|
|
55
77
|
return m;
|
|
56
78
|
}
|
|
57
79
|
exports.getFields = getFields;
|
|
80
|
+
/**
|
|
81
|
+
* @deprecated should only be used by tests
|
|
82
|
+
*/
|
|
83
|
+
function getStorageKey(field, fieldName) {
|
|
84
|
+
return field.storageKey || (0, snake_case_1.snakeCase)(fieldName);
|
|
85
|
+
}
|
|
86
|
+
exports.getStorageKey = getStorageKey;
|
|
87
|
+
// returns a mapping of storage key to field privacy
|
|
88
|
+
function getFieldsWithPrivacy(value, fieldMap) {
|
|
89
|
+
const schema = getSchema(value);
|
|
90
|
+
function addFields(fields) {
|
|
91
|
+
if (Array.isArray(fields)) {
|
|
92
|
+
for (const field of fields) {
|
|
93
|
+
const name = field.name;
|
|
94
|
+
if (!field.name) {
|
|
95
|
+
throw new Error(`name required`);
|
|
96
|
+
}
|
|
97
|
+
if (field.getDerivedFields !== undefined) {
|
|
98
|
+
addFields(field.getDerivedFields(name));
|
|
99
|
+
}
|
|
100
|
+
if (field.privacyPolicy) {
|
|
101
|
+
let privacyPolicy;
|
|
102
|
+
if (typeof field.privacyPolicy === "function") {
|
|
103
|
+
privacyPolicy = field.privacyPolicy();
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
privacyPolicy = field.privacyPolicy;
|
|
107
|
+
}
|
|
108
|
+
const info = fieldMap[name];
|
|
109
|
+
if (!info) {
|
|
110
|
+
throw new Error(`field with name ${name} not passed in fieldMap`);
|
|
111
|
+
}
|
|
112
|
+
m.set(info.dbCol, privacyPolicy);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
for (const name in fields) {
|
|
118
|
+
const field = fields[name];
|
|
119
|
+
if (field.getDerivedFields !== undefined) {
|
|
120
|
+
addFields(field.getDerivedFields(name));
|
|
121
|
+
}
|
|
122
|
+
if (field.privacyPolicy) {
|
|
123
|
+
let privacyPolicy;
|
|
124
|
+
if (typeof field.privacyPolicy === "function") {
|
|
125
|
+
privacyPolicy = field.privacyPolicy();
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
privacyPolicy = field.privacyPolicy;
|
|
129
|
+
}
|
|
130
|
+
const info = fieldMap[name];
|
|
131
|
+
if (!info) {
|
|
132
|
+
throw new Error(`field with name ${name} not passed in fieldMap`);
|
|
133
|
+
}
|
|
134
|
+
m.set(info.dbCol, privacyPolicy);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
let m = new Map();
|
|
139
|
+
if (schema.patterns) {
|
|
140
|
+
for (const pattern of schema.patterns) {
|
|
141
|
+
addFields(pattern.fields);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
addFields(schema.fields);
|
|
145
|
+
return m;
|
|
146
|
+
}
|
|
147
|
+
exports.getFieldsWithPrivacy = getFieldsWithPrivacy;
|
|
148
|
+
function getTransformedReadClause(value) {
|
|
149
|
+
const schema = getSchema(value);
|
|
150
|
+
if (!schema.patterns) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
for (const p of schema.patterns) {
|
|
154
|
+
// e.g. discarded_at, deleted_at, etc
|
|
155
|
+
if (p.transformRead) {
|
|
156
|
+
// return clause.Eq('deleted_at', null);
|
|
157
|
+
return p.transformRead();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
exports.getTransformedReadClause = getTransformedReadClause;
|
|
163
|
+
function getObjectLoaderProperties(value, tableName) {
|
|
164
|
+
return {
|
|
165
|
+
clause: () => getTransformedReadClause(value),
|
|
166
|
+
instanceKey: `${tableName}:transformedReadClause`,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
exports.getObjectLoaderProperties = getObjectLoaderProperties;
|
|
170
|
+
function getTransformedUpdateOp(value, stmt) {
|
|
171
|
+
const schema = getSchema(value);
|
|
172
|
+
if (!schema.patterns) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
for (const p of schema.patterns) {
|
|
176
|
+
if (p.transformWrite) {
|
|
177
|
+
return p.transformWrite(stmt);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
exports.getTransformedUpdateOp = getTransformedUpdateOp;
|
|
58
183
|
// this maps to ActionOperation in ent/action.go
|
|
59
184
|
var ActionOperation;
|
|
60
185
|
(function (ActionOperation) {
|
|
@@ -76,6 +201,9 @@ var ActionOperation;
|
|
|
76
201
|
// RemoveEdge is used to provide the ability to remove an edge in an AssociationEdge.
|
|
77
202
|
ActionOperation[ActionOperation["RemoveEdge"] = 32] = "RemoveEdge";
|
|
78
203
|
// EdgeGroup is used to provide the ability to edit an edge group in an AssociationEdgeGroup.
|
|
204
|
+
// if you want to clear this edge group e.g. any set edge with id1 and id2,
|
|
205
|
+
// you should create a custom action that takes the id as an action only field,
|
|
206
|
+
// and then calls `clearEdgeTypeInGroup` in a trigger to clear
|
|
79
207
|
ActionOperation[ActionOperation["EdgeGroup"] = 64] = "EdgeGroup";
|
|
80
208
|
})(ActionOperation = exports.ActionOperation || (exports.ActionOperation = {}));
|
|
81
209
|
// sentinel that indicates an action has no fields
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BaseField, ListField } from "./field";
|
|
2
|
+
import { FieldOptions, Field, Type, FieldMap } from "./schema";
|
|
3
|
+
export interface StructOptions extends FieldOptions {
|
|
4
|
+
tsType: string;
|
|
5
|
+
fields: FieldMap;
|
|
6
|
+
graphQLType?: string;
|
|
7
|
+
jsonNotJSONB?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface allStructOptions extends StructOptions {
|
|
10
|
+
jsonAsList?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare class StructField extends BaseField implements Field {
|
|
13
|
+
private options;
|
|
14
|
+
type: Type;
|
|
15
|
+
constructor(options: allStructOptions);
|
|
16
|
+
formatImpl(obj: any, nested?: boolean): string | Object;
|
|
17
|
+
format(obj: any, nested?: boolean): string | Object;
|
|
18
|
+
private validImpl;
|
|
19
|
+
valid(obj: any): Promise<boolean>;
|
|
20
|
+
}
|
|
21
|
+
export declare function StructType(options: StructOptions): StructField & StructOptions;
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated use StructTypeAsList
|
|
24
|
+
*/
|
|
25
|
+
export declare function StructListType(options: StructOptions): ListField;
|
|
26
|
+
export declare function StructTypeAsList(options: allStructOptions): StructField & allStructOptions;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StructTypeAsList = exports.StructListType = exports.StructType = exports.StructField = void 0;
|
|
4
|
+
const camel_case_1 = require("camel-case");
|
|
5
|
+
const field_1 = require("./field");
|
|
6
|
+
const schema_1 = require("./schema");
|
|
7
|
+
class StructField extends field_1.BaseField {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
super();
|
|
10
|
+
this.options = options;
|
|
11
|
+
this.type = {
|
|
12
|
+
dbType: schema_1.DBType.JSONB,
|
|
13
|
+
};
|
|
14
|
+
this.type.subFields = options.fields;
|
|
15
|
+
this.type.type = options.tsType;
|
|
16
|
+
this.type.graphQLType = options.graphQLType || options.tsType;
|
|
17
|
+
if (options.jsonNotJSONB) {
|
|
18
|
+
this.type.dbType = schema_1.DBType.JSON;
|
|
19
|
+
}
|
|
20
|
+
if (options?.jsonAsList) {
|
|
21
|
+
this.type.listElemType = {
|
|
22
|
+
dbType: schema_1.DBType.JSONB,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
formatImpl(obj, nested) {
|
|
27
|
+
if (!(obj instanceof Object)) {
|
|
28
|
+
throw new Error("valid was not called");
|
|
29
|
+
}
|
|
30
|
+
let ret = {};
|
|
31
|
+
for (const k in this.options.fields) {
|
|
32
|
+
const field = this.options.fields[k];
|
|
33
|
+
// check two values
|
|
34
|
+
// store in dbKey format
|
|
35
|
+
// TODO more #510
|
|
36
|
+
let dbKey = (0, schema_1.getStorageKey)(field, k);
|
|
37
|
+
let camelKey = (0, camel_case_1.camelCase)(k);
|
|
38
|
+
let val = obj[camelKey];
|
|
39
|
+
if (val === undefined && obj[dbKey] !== undefined) {
|
|
40
|
+
val = obj[dbKey];
|
|
41
|
+
}
|
|
42
|
+
if (val === undefined) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (field.format) {
|
|
46
|
+
// indicate nested so this isn't JSON stringified
|
|
47
|
+
ret[dbKey] = field.format(val, true);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
ret[dbKey] = val;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// don't json.stringify if nested or list
|
|
54
|
+
if (nested) {
|
|
55
|
+
return ret;
|
|
56
|
+
}
|
|
57
|
+
return JSON.stringify(ret);
|
|
58
|
+
}
|
|
59
|
+
format(obj, nested) {
|
|
60
|
+
if (Array.isArray(obj) && this.options.jsonAsList) {
|
|
61
|
+
const ret = obj.map((v) => this.formatImpl(v, true));
|
|
62
|
+
if (nested) {
|
|
63
|
+
return ret;
|
|
64
|
+
}
|
|
65
|
+
return JSON.stringify(ret);
|
|
66
|
+
}
|
|
67
|
+
return this.formatImpl(obj, nested);
|
|
68
|
+
}
|
|
69
|
+
async validImpl(obj) {
|
|
70
|
+
if (!(obj instanceof Object)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
let promises = [];
|
|
74
|
+
// TODO probably need to support optional fields...
|
|
75
|
+
let valid = true;
|
|
76
|
+
for (const k in this.options.fields) {
|
|
77
|
+
const field = this.options.fields[k];
|
|
78
|
+
// TODO more #510
|
|
79
|
+
let dbKey = (0, schema_1.getStorageKey)(field, k);
|
|
80
|
+
let camelKey = (0, camel_case_1.camelCase)(k);
|
|
81
|
+
let val = obj[camelKey];
|
|
82
|
+
if (val === undefined && obj[dbKey] !== undefined) {
|
|
83
|
+
val = obj[dbKey];
|
|
84
|
+
}
|
|
85
|
+
if (val === undefined || val === null) {
|
|
86
|
+
// nullable, nothing to do here
|
|
87
|
+
if (field.nullable) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
valid = false;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
if (!field.valid) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
promises.push(field.valid(val));
|
|
97
|
+
}
|
|
98
|
+
if (!valid) {
|
|
99
|
+
return valid;
|
|
100
|
+
}
|
|
101
|
+
const ret = await Promise.all(promises);
|
|
102
|
+
return ret.every((v) => v);
|
|
103
|
+
}
|
|
104
|
+
async valid(obj) {
|
|
105
|
+
if (this.options.jsonAsList) {
|
|
106
|
+
if (!Array.isArray(obj)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const valid = await Promise.all(obj.map((v) => this.validImpl(v)));
|
|
110
|
+
return valid.every((b) => b);
|
|
111
|
+
}
|
|
112
|
+
if (!(obj instanceof Object)) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return this.validImpl(obj);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.StructField = StructField;
|
|
119
|
+
function StructType(options) {
|
|
120
|
+
let result = new StructField(options);
|
|
121
|
+
return Object.assign(result, options);
|
|
122
|
+
}
|
|
123
|
+
exports.StructType = StructType;
|
|
124
|
+
/**
|
|
125
|
+
* @deprecated use StructTypeAsList
|
|
126
|
+
*/
|
|
127
|
+
function StructListType(options) {
|
|
128
|
+
return new field_1.ListField(StructType(options), options);
|
|
129
|
+
}
|
|
130
|
+
exports.StructListType = StructListType;
|
|
131
|
+
function StructTypeAsList(options) {
|
|
132
|
+
let result = new StructField({
|
|
133
|
+
...options,
|
|
134
|
+
jsonAsList: true,
|
|
135
|
+
});
|
|
136
|
+
return Object.assign(result, options);
|
|
137
|
+
}
|
|
138
|
+
exports.StructTypeAsList = StructTypeAsList;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StructField } from "./struct_field";
|
|
2
|
+
import { FieldOptions, Type } from "./schema";
|
|
3
|
+
import { BaseField, ListField } from "./field";
|
|
4
|
+
export declare type StructMap = {
|
|
5
|
+
[key: string]: StructField;
|
|
6
|
+
};
|
|
7
|
+
export interface UnionOptions extends FieldOptions {
|
|
8
|
+
tsType: string;
|
|
9
|
+
fields: StructMap;
|
|
10
|
+
graphQLType?: string;
|
|
11
|
+
jsonNotJSONB?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class UnionField extends BaseField implements FieldOptions {
|
|
14
|
+
private options;
|
|
15
|
+
type: Type;
|
|
16
|
+
m: Map<Object, string>;
|
|
17
|
+
constructor(options: UnionOptions);
|
|
18
|
+
format(obj: any): string | Object;
|
|
19
|
+
private validField;
|
|
20
|
+
valid(obj: any): Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export declare function UnionType(options: UnionOptions): UnionField & UnionOptions;
|
|
23
|
+
export declare function UnionListType(options: UnionOptions): ListField;
|