@snowtop/ent 0.0.37 → 0.0.39-alpha14

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.
@@ -7,6 +7,7 @@ function processFields(src, patternName) {
7
7
  let f = { ...field };
8
8
  f.hasDefaultValueOnCreate = field.defaultValueOnCreate != undefined;
9
9
  f.hasDefaultValueOnEdit = field.defaultValueOnEdit != undefined;
10
+ f.hasFieldPrivacy = field.privacyPolicy !== undefined;
10
11
  if (field.polymorphic) {
11
12
  // convert boolean into object
12
13
  // we keep boolean as an option to keep API simple
@@ -78,6 +79,9 @@ function processEdgeGroups(processedSchema, edgeGroups) {
78
79
  }
79
80
  }
80
81
  function processPattern(patterns, pattern, processedSchema) {
82
+ let ret = {
83
+ ...pattern,
84
+ };
81
85
  const name = pattern.name;
82
86
  const fields = processFields(pattern.fields, pattern.name);
83
87
  processedSchema.fields.push(...fields);
@@ -85,6 +89,10 @@ function processPattern(patterns, pattern, processedSchema) {
85
89
  const edges = processEdges(pattern.edges, pattern.name);
86
90
  processedSchema.assocEdges.push(...edges);
87
91
  }
92
+ // flag transformsSelect
93
+ if (pattern.transformRead) {
94
+ ret.transformsSelect = true;
95
+ }
88
96
  if (patterns[name] === undefined) {
89
97
  // intentionally processing separately and not passing pattern.name
90
98
  const edges = processEdges(pattern.edges || []);
@@ -98,6 +106,7 @@ function processPattern(patterns, pattern, processedSchema) {
98
106
  // TODO ideally we want to make sure that different patterns don't have the same name
99
107
  // can't do a deepEqual check because function calls and therefore different instances in fields
100
108
  }
109
+ return ret;
101
110
  }
102
111
  var NullableResult;
103
112
  (function (NullableResult) {
@@ -160,7 +169,19 @@ function parseSchema(potentialSchemas) {
160
169
  // ¯\_(ツ)_/¯
161
170
  if (schema.patterns) {
162
171
  for (const pattern of schema.patterns) {
163
- processPattern(patterns, pattern, processedSchema);
172
+ const ret = processPattern(patterns, pattern, processedSchema);
173
+ if (ret.transformsSelect) {
174
+ if (processedSchema.transformsSelect) {
175
+ throw new Error(`can only have one pattern which transforms default querying behavior`);
176
+ }
177
+ processedSchema.transformsSelect = true;
178
+ }
179
+ if (ret.transformsDelete) {
180
+ if (processedSchema.transformsDelete) {
181
+ throw new Error(`can only have one pattern which transforms default deletion behavior`);
182
+ }
183
+ processedSchema.transformsDelete = true;
184
+ }
164
185
  }
165
186
  }
166
187
  const fields = processFields(schema.fields);
package/schema/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import Schema from "./schema";
2
2
  export { Schema };
3
- export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, ActionOperation, Action, EdgeAction, NoFields, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, } from "./schema";
3
+ export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, getFieldsWithPrivacy, ActionOperation, Action, EdgeAction, NoFields, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, getTransformedReadClause, } from "./schema";
4
4
  export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, } from "./base_schema";
5
5
  export * from "./field";
6
6
  export * from "./json_field";
package/schema/index.js CHANGED
@@ -10,15 +10,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getFields = exports.DBType = void 0;
13
+ exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.getTransformedReadClause = exports.SQLStatementOperation = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getFieldsWithPrivacy = exports.getFields = exports.DBType = void 0;
14
14
  var schema_1 = require("./schema");
15
15
  Object.defineProperty(exports, "DBType", { enumerable: true, get: function () { return schema_1.DBType; } });
16
16
  Object.defineProperty(exports, "getFields", { enumerable: true, get: function () { return schema_1.getFields; } });
17
+ Object.defineProperty(exports, "getFieldsWithPrivacy", { enumerable: true, get: function () { return schema_1.getFieldsWithPrivacy; } });
17
18
  Object.defineProperty(exports, "ActionOperation", { enumerable: true, get: function () { return schema_1.ActionOperation; } });
18
19
  Object.defineProperty(exports, "NoFields", { enumerable: true, get: function () { return schema_1.NoFields; } });
19
20
  Object.defineProperty(exports, "ConstraintType", { enumerable: true, get: function () { return schema_1.ConstraintType; } });
20
21
  Object.defineProperty(exports, "requiredField", { enumerable: true, get: function () { return schema_1.requiredField; } });
21
22
  Object.defineProperty(exports, "optionalField", { enumerable: true, get: function () { return schema_1.optionalField; } });
23
+ Object.defineProperty(exports, "SQLStatementOperation", { enumerable: true, get: function () { return schema_1.SQLStatementOperation; } });
24
+ Object.defineProperty(exports, "getTransformedReadClause", { enumerable: true, get: function () { return schema_1.getTransformedReadClause; } });
22
25
  var base_schema_1 = require("./base_schema");
23
26
  Object.defineProperty(exports, "Timestamps", { enumerable: true, get: function () { return base_schema_1.Timestamps; } });
24
27
  Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return base_schema_1.Node; } });
@@ -1,5 +1,6 @@
1
- import { Data, Ent, LoaderInfo } from "../core/base";
1
+ import { Data, Ent, LoaderInfo, PrivacyPolicy, Viewer } from "../core/base";
2
2
  import { Builder } from "../action/action";
3
+ import { Clause } from "../core/clause";
3
4
  export default interface Schema {
4
5
  fields: Field[];
5
6
  tableName?: string;
@@ -59,6 +60,27 @@ export interface Pattern {
59
60
  name: string;
60
61
  fields: Field[];
61
62
  edges?: Edge[];
63
+ transformRead?: () => Clause;
64
+ transformWrite?: <T extends Ent>(stmt: UpdateOperation<T>) => TransformedUpdateOperation<T> | undefined;
65
+ transformsDelete?: boolean;
66
+ transformsInsert?: boolean;
67
+ transformsUpdate?: boolean;
68
+ }
69
+ export declare enum SQLStatementOperation {
70
+ Insert = "insert",
71
+ Update = "update",
72
+ Delete = "delete"
73
+ }
74
+ export interface UpdateOperation<T extends Ent> {
75
+ op: SQLStatementOperation;
76
+ existingEnt?: T;
77
+ viewer: Viewer;
78
+ data?: Map<string, any>;
79
+ }
80
+ export interface TransformedUpdateOperation<T extends Ent> {
81
+ op: SQLStatementOperation;
82
+ data?: Data;
83
+ existingEnt?: T;
62
84
  }
63
85
  export declare enum DBType {
64
86
  UUID = "UUID",
@@ -139,6 +161,7 @@ export interface FieldOptions {
139
161
  derivedWhenEmbedded?: boolean;
140
162
  polymorphic?: boolean | PolymorphicOptions;
141
163
  derivedFields?: Field[];
164
+ privacyPolicy?: PrivacyPolicy;
142
165
  }
143
166
  export interface PolymorphicOptions {
144
167
  types?: string[];
@@ -155,7 +178,12 @@ export interface SchemaConstructor {
155
178
  new (): Schema;
156
179
  }
157
180
  export declare type SchemaInputType = Schema | SchemaConstructor;
181
+ export declare function getSchema(value: SchemaInputType): Schema;
158
182
  export declare function getFields(value: SchemaInputType): Map<string, Field>;
183
+ export declare function getStorageKey(field: Field): string;
184
+ export declare function getFieldsWithPrivacy(value: SchemaInputType): Map<string, PrivacyPolicy>;
185
+ export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
186
+ export declare function getTransformedUpdateOp<T extends Ent>(value: SchemaInputType, stmt: UpdateOperation<T>): TransformedUpdateOperation<T> | undefined;
159
187
  export declare enum ActionOperation {
160
188
  Create = 1,
161
189
  Edit = 2,
@@ -198,10 +226,24 @@ export interface Constraint {
198
226
  fkey?: ForeignKeyInfo;
199
227
  condition?: string;
200
228
  }
229
+ export interface FullTextWeight {
230
+ A?: string[];
231
+ B?: string[];
232
+ C?: string[];
233
+ D?: string[];
234
+ }
235
+ export interface FullText {
236
+ generatedColumnName?: string;
237
+ language?: "english" | "french" | "german" | "simple";
238
+ languageColumn?: string;
239
+ indexType?: "gin" | "gist";
240
+ weights?: FullTextWeight;
241
+ }
201
242
  export interface Index {
202
243
  name: string;
203
244
  columns: string[];
204
245
  unique?: boolean;
246
+ fulltext?: FullText;
205
247
  }
206
248
  export interface ForeignKeyInfo {
207
249
  tableName: string;
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.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
@@ -28,14 +43,17 @@ var DBType;
28
43
  function isSchema(value) {
29
44
  return value.fields !== undefined;
30
45
  }
31
- function getFields(value) {
32
- let schema;
46
+ function getSchema(value) {
33
47
  if (isSchema(value)) {
34
- schema = value;
48
+ return value;
35
49
  }
36
50
  else {
37
- schema = new value();
51
+ return new value();
38
52
  }
53
+ }
54
+ exports.getSchema = getSchema;
55
+ function getFields(value) {
56
+ const schema = getSchema(value);
39
57
  function addFields(fields) {
40
58
  for (const field of fields) {
41
59
  const derivedFields = field.derivedFields;
@@ -55,6 +73,61 @@ function getFields(value) {
55
73
  return m;
56
74
  }
57
75
  exports.getFields = getFields;
76
+ function getStorageKey(field) {
77
+ return field.storageKey || (0, snake_case_1.snakeCase)(field.name);
78
+ }
79
+ exports.getStorageKey = getStorageKey;
80
+ // returns a mapping of storage key to field privacy
81
+ function getFieldsWithPrivacy(value) {
82
+ const schema = getSchema(value);
83
+ function addFields(fields) {
84
+ for (const field of fields) {
85
+ const derivedFields = field.derivedFields;
86
+ if (derivedFields !== undefined) {
87
+ addFields(derivedFields);
88
+ }
89
+ if (field.privacyPolicy) {
90
+ m.set(getStorageKey(field), field.privacyPolicy);
91
+ }
92
+ }
93
+ }
94
+ let m = new Map();
95
+ if (schema.patterns) {
96
+ for (const pattern of schema.patterns) {
97
+ addFields(pattern.fields);
98
+ }
99
+ }
100
+ addFields(schema.fields);
101
+ return m;
102
+ }
103
+ exports.getFieldsWithPrivacy = getFieldsWithPrivacy;
104
+ function getTransformedReadClause(value) {
105
+ const schema = getSchema(value);
106
+ if (!schema.patterns) {
107
+ return;
108
+ }
109
+ for (const p of schema.patterns) {
110
+ // e.g. discarded_at, deleted_at, etc
111
+ if (p.transformRead) {
112
+ // return clause.Eq('deleted_at', null);
113
+ return p.transformRead();
114
+ }
115
+ }
116
+ return;
117
+ }
118
+ exports.getTransformedReadClause = getTransformedReadClause;
119
+ function getTransformedUpdateOp(value, stmt) {
120
+ const schema = getSchema(value);
121
+ if (!schema.patterns) {
122
+ return;
123
+ }
124
+ for (const p of schema.patterns) {
125
+ if (p.transformWrite) {
126
+ return p.transformWrite(stmt);
127
+ }
128
+ }
129
+ }
130
+ exports.getTransformedUpdateOp = getTransformedUpdateOp;
58
131
  // this maps to ActionOperation in ent/action.go
59
132
  var ActionOperation;
60
133
  (function (ActionOperation) {
@@ -24,6 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const glob_1 = __importDefault(require("glob"));
27
+ const json5_1 = __importDefault(require("json5"));
27
28
  const minimist_1 = __importDefault(require("minimist"));
28
29
  const graphql_1 = require("../graphql/graphql");
29
30
  const readline = __importStar(require("readline"));
@@ -53,7 +54,103 @@ async function readInputs() {
53
54
  });
54
55
  });
55
56
  }
56
- async function captureCustom(filePath, filesCsv) {
57
+ function processCustomObjects(l, gqlCapture, input) {
58
+ let m;
59
+ if (input) {
60
+ m = gqlCapture.getCustomInputObjects();
61
+ }
62
+ else {
63
+ m = gqlCapture.getCustomObjects();
64
+ }
65
+ for (const input of l) {
66
+ m.set(input.name, {
67
+ nodeName: input.graphQLName || input.name,
68
+ className: input.name,
69
+ });
70
+ if (input.fields) {
71
+ processCustomFields(input.fields, gqlCapture, input.name);
72
+ }
73
+ }
74
+ }
75
+ function transformArgs(f) {
76
+ return (f.args || []).map((v) => {
77
+ const ret = {
78
+ ...v,
79
+ };
80
+ // duplicated from getType in graphql.ts
81
+ if ((0, graphql_1.isCustomType)(ret.type)) {
82
+ ret.type = v.type.type;
83
+ (0, graphql_1.addCustomType)(v.type);
84
+ }
85
+ // scalar types not supported for now
86
+ ret.tsType = graphql_1.knownAllowedNames.get(v.type);
87
+ return ret;
88
+ });
89
+ }
90
+ function transformResultType(f) {
91
+ return f.resultType
92
+ ? [
93
+ {
94
+ name: "",
95
+ type: f.resultType,
96
+ tsType: graphql_1.knownAllowedNames.get(f.resultType),
97
+ list: f.list,
98
+ nullable: f.nullable,
99
+ },
100
+ ]
101
+ : [];
102
+ }
103
+ function processTopLevel(l, l2) {
104
+ for (const custom of l) {
105
+ l2.push({
106
+ nodeName: custom.class,
107
+ functionName: custom.functionName || custom.name,
108
+ gqlName: custom.graphQLName || custom.name,
109
+ fieldType: custom.fieldType,
110
+ args: transformArgs(custom),
111
+ results: transformResultType(custom),
112
+ });
113
+ }
114
+ }
115
+ function processCustomFields(fields, gqlCapture, nodeName) {
116
+ const m = gqlCapture.getCustomFields();
117
+ let results = [];
118
+ for (const f of fields) {
119
+ results.push({
120
+ nodeName: nodeName,
121
+ gqlName: f.graphQLName || f.name,
122
+ functionName: f.functionName || f.name,
123
+ fieldType: f.fieldType,
124
+ args: transformArgs(f),
125
+ results: transformResultType(f),
126
+ });
127
+ }
128
+ m.set(nodeName, results);
129
+ }
130
+ async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
131
+ if (jsonPath !== undefined) {
132
+ let json = json5_1.default.parse(fs.readFileSync(jsonPath, {
133
+ encoding: "utf8",
134
+ }));
135
+ if (json.fields) {
136
+ for (const k in json.fields) {
137
+ processCustomFields(json.fields[k], gqlCapture, k);
138
+ }
139
+ }
140
+ if (json.inputs) {
141
+ processCustomObjects(json.inputs, gqlCapture, true);
142
+ }
143
+ if (json.objects) {
144
+ processCustomObjects(json.objects, gqlCapture);
145
+ }
146
+ if (json.queries) {
147
+ processTopLevel(json.queries, gqlCapture.getCustomQueries());
148
+ }
149
+ if (json.mutations) {
150
+ processTopLevel(json.mutations, gqlCapture.getCustomMutations());
151
+ }
152
+ return;
153
+ }
57
154
  if (filesCsv !== undefined) {
58
155
  let files = filesCsv.split(",");
59
156
  for (let i = 0; i < files.length; i++) {
@@ -153,15 +250,25 @@ async function main() {
153
250
  if (!gqlPath) {
154
251
  throw new Error("could not find graphql path");
155
252
  }
156
- const r = require(gqlPath);
157
- if (!r.GQLCapture) {
158
- throw new Error("could not find GQLCapture in module");
253
+ // use different variable so that we use the correct GQLCapture as needed
254
+ // for local dev, get the one from the file system. otherwise, get the one
255
+ // from node_modules
256
+ let gqlCapture;
257
+ if (process.env.LOCAL_SCRIPT_PATH) {
258
+ const r = require("../graphql/graphql");
259
+ gqlCapture = r.GQLCapture;
260
+ }
261
+ else {
262
+ const r = require(gqlPath);
263
+ if (!r.GQLCapture) {
264
+ throw new Error("could not find GQLCapture in module");
265
+ }
266
+ gqlCapture = r.GQLCapture;
267
+ gqlCapture.enable(true);
159
268
  }
160
- const GQLCapture = r.GQLCapture;
161
- GQLCapture.enable(true);
162
269
  const [inputsRead, _, imports] = await Promise.all([
163
270
  readInputs(),
164
- captureCustom(options.path, options.files),
271
+ captureCustom(options.path, options.files, options.json_path, gqlCapture),
165
272
  parseImports(options.path),
166
273
  ]);
167
274
  const { nodes, nodesMap } = inputsRead;
@@ -172,14 +279,14 @@ async function main() {
172
279
  }
173
280
  return result;
174
281
  }
175
- GQLCapture.resolve(nodes);
176
- let args = fromMap(GQLCapture.getCustomArgs());
177
- let inputs = fromMap(GQLCapture.getCustomInputObjects());
178
- let fields = GQLCapture.getProcessedCustomFields();
179
- let queries = GQLCapture.getProcessedCustomQueries();
180
- let mutations = GQLCapture.getProcessedCustomMutations();
181
- let objects = fromMap(GQLCapture.getCustomObjects());
182
- let customTypes = fromMap(GQLCapture.getCustomTypes());
282
+ gqlCapture.resolve(nodes);
283
+ let args = fromMap(gqlCapture.getCustomArgs());
284
+ let inputs = fromMap(gqlCapture.getCustomInputObjects());
285
+ let fields = gqlCapture.getProcessedCustomFields();
286
+ let queries = gqlCapture.getProcessedCustomQueries();
287
+ let mutations = gqlCapture.getProcessedCustomMutations();
288
+ let objects = fromMap(gqlCapture.getCustomObjects());
289
+ let customTypes = fromMap(gqlCapture.getCustomTypes());
183
290
  let classes = {};
184
291
  let allFiles = {};
185
292
  const buildClasses2 = (args) => {
@@ -149,6 +149,11 @@ class SimpleBuilder {
149
149
  editedFields: () => {
150
150
  return this.fields;
151
151
  },
152
+ updateInput: (input) => {
153
+ for (const k in input) {
154
+ this.fields.set(k, input[k]);
155
+ }
156
+ },
152
157
  });
153
158
  }
154
159
  build() {