@snowtop/ent 0.1.0-alpha30 → 0.1.0-alpha35
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 +1 -1
- package/action/experimental_action.d.ts +1 -1
- package/action/orchestrator.js +14 -6
- package/package.json +1 -1
- package/schema/schema.d.ts +4 -4
- package/tsc/transform_schema.js +34 -9
package/action/action.d.ts
CHANGED
|
@@ -53,7 +53,7 @@ export interface Action<TEnt extends Ent<TViewer>, TBuilder extends Builder<TEnt
|
|
|
53
53
|
getObservers?(): Observer<TEnt, TBuilder, TViewer, TInput, TExistingEnt>[];
|
|
54
54
|
getValidators?(): Validator<TEnt, TBuilder, TViewer, TInput, TExistingEnt>[];
|
|
55
55
|
getInput(): TInput;
|
|
56
|
-
transformWrite?: (stmt: UpdateOperation<TEnt>) => Promise<TransformedUpdateOperation<TEnt>> | TransformedUpdateOperation<TEnt> | null;
|
|
56
|
+
transformWrite?: (stmt: UpdateOperation<TEnt, TViewer>) => Promise<TransformedUpdateOperation<TEnt>> | TransformedUpdateOperation<TEnt> | null;
|
|
57
57
|
valid(): Promise<boolean>;
|
|
58
58
|
validX(): Promise<void>;
|
|
59
59
|
viewerForEntLoad?(data: Data): TViewer | Promise<TViewer>;
|
|
@@ -36,6 +36,6 @@ export declare class BaseAction<TEnt extends Ent<TViewer>, TViewer extends Viewe
|
|
|
36
36
|
interface BuilderConstructor<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data> {
|
|
37
37
|
new (viewer: TViewer, operation: WriteOperation, action: Action<TEnt, EntBuilder<TEnt, TViewer, TInput>, TViewer, TInput>, existingEnt: TEnt | null): EntBuilder<TEnt, TViewer, TInput>;
|
|
38
38
|
}
|
|
39
|
-
export declare function updateRawObject<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer:
|
|
39
|
+
export declare function updateRawObject<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, existingEnt: TEnt, input: TInput): Promise<TEnt>;
|
|
40
40
|
export declare function getSimpleEditAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput>, existingEnt: TEnt, input: TInput): Action<TEnt, Builder<TEnt, TViewer>, TViewer, TInput>;
|
|
41
41
|
export {};
|
package/action/orchestrator.js
CHANGED
|
@@ -379,23 +379,29 @@ class Orchestrator {
|
|
|
379
379
|
// if disable transformations set, don't do schema transform and just do the right thing
|
|
380
380
|
// else apply schema tranformation if it exists
|
|
381
381
|
let transformed = null;
|
|
382
|
+
const sqlOp = this.getSQLStatementOperation();
|
|
382
383
|
if (action?.transformWrite) {
|
|
383
384
|
transformed = await action.transformWrite({
|
|
384
|
-
|
|
385
|
-
|
|
385
|
+
builder,
|
|
386
|
+
input,
|
|
387
|
+
op: sqlOp,
|
|
386
388
|
data: editedFields,
|
|
387
|
-
existingEnt: this.existingEnt,
|
|
388
389
|
});
|
|
389
390
|
}
|
|
390
391
|
else if (!this.disableTransformations) {
|
|
391
392
|
transformed = (0, schema_1.getTransformedUpdateOp)(this.options.schema, {
|
|
392
|
-
|
|
393
|
-
|
|
393
|
+
builder,
|
|
394
|
+
input,
|
|
395
|
+
op: sqlOp,
|
|
394
396
|
data: editedFields,
|
|
395
|
-
existingEnt: this.existingEnt,
|
|
396
397
|
});
|
|
397
398
|
}
|
|
398
399
|
if (transformed) {
|
|
400
|
+
if (sqlOp === schema_1.SQLStatementOperation.Insert && sqlOp !== transformed.op) {
|
|
401
|
+
if (!transformed.existingEnt) {
|
|
402
|
+
throw new Error(`cannot transform an insert operation without providing an existing ent`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
399
405
|
if (transformed.data) {
|
|
400
406
|
updateInput = true;
|
|
401
407
|
for (const k in transformed.data) {
|
|
@@ -418,6 +424,8 @@ class Orchestrator {
|
|
|
418
424
|
if (transformed.existingEnt) {
|
|
419
425
|
// @ts-ignore
|
|
420
426
|
this.existingEnt = transformed.existingEnt;
|
|
427
|
+
// modify existing ent in builder. it's readonly in generated ents but doesn't apply here
|
|
428
|
+
builder.existingEnt = transformed.existingEnt;
|
|
421
429
|
}
|
|
422
430
|
}
|
|
423
431
|
// transforming before doing default fields so that we don't create a new id
|
package/package.json
CHANGED
package/schema/schema.d.ts
CHANGED
|
@@ -82,10 +82,10 @@ export declare enum SQLStatementOperation {
|
|
|
82
82
|
Update = "update",
|
|
83
83
|
Delete = "delete"
|
|
84
84
|
}
|
|
85
|
-
export interface UpdateOperation<
|
|
85
|
+
export interface UpdateOperation<TEnt extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
|
|
86
86
|
op: SQLStatementOperation;
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
builder: Builder<TEnt, TViewer>;
|
|
88
|
+
input: Data;
|
|
89
89
|
data?: Map<string, any>;
|
|
90
90
|
}
|
|
91
91
|
export interface TransformedUpdateOperation<T extends Ent> {
|
|
@@ -204,7 +204,7 @@ interface objectLoaderOptions {
|
|
|
204
204
|
instanceKey?: string;
|
|
205
205
|
}
|
|
206
206
|
export declare function getObjectLoaderProperties(value: SchemaInputType, tableName: string): objectLoaderOptions | undefined;
|
|
207
|
-
export declare function getTransformedUpdateOp<
|
|
207
|
+
export declare function getTransformedUpdateOp<TEnt extends Ent<TViewer>, TViewer extends Viewer>(value: SchemaInputType, stmt: UpdateOperation<TEnt, TViewer>): TransformedUpdateOperation<TEnt> | null;
|
|
208
208
|
export declare enum ActionOperation {
|
|
209
209
|
Create = 1,
|
|
210
210
|
Edit = 2,
|
package/tsc/transform_schema.js
CHANGED
|
@@ -91,7 +91,7 @@ function getTransformClassInfo(fileContents, sourceFile, node, transformSchema)
|
|
|
91
91
|
}
|
|
92
92
|
// intentionally doesn't parse decorators since we don't need it
|
|
93
93
|
function getClassElementInfo(fileContents, member, sourceFile) {
|
|
94
|
-
if (isFieldElement(member, sourceFile)) {
|
|
94
|
+
if (isFieldElement(fileContents, member, sourceFile)) {
|
|
95
95
|
return getFieldElementInfo(fileContents, member, sourceFile);
|
|
96
96
|
}
|
|
97
97
|
if (member.kind === typescript_1.default.SyntaxKind.Constructor) {
|
|
@@ -201,7 +201,7 @@ function getConstructorElementInfo(fileContents, member, sourceFile) {
|
|
|
201
201
|
comment: "",
|
|
202
202
|
};
|
|
203
203
|
}
|
|
204
|
-
function isFieldElement(member, sourceFile) {
|
|
204
|
+
function isFieldElement(fileContents, member, sourceFile) {
|
|
205
205
|
if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
|
|
206
206
|
return false;
|
|
207
207
|
}
|
|
@@ -215,16 +215,31 @@ function isFieldElement(member, sourceFile) {
|
|
|
215
215
|
return false;
|
|
216
216
|
}
|
|
217
217
|
if (property.initializer?.kind !== typescript_1.default.SyntaxKind.ArrayLiteralExpression) {
|
|
218
|
-
|
|
218
|
+
throwErr(fileContents, member, "invalid array type");
|
|
219
219
|
return false;
|
|
220
220
|
}
|
|
221
221
|
return true;
|
|
222
222
|
}
|
|
223
|
+
// if there's an error transforming any of the schemas, we should stop...
|
|
224
|
+
function throwErr(fileContents, node, error) {
|
|
225
|
+
console.error(error);
|
|
226
|
+
throw new Error(`error transforming this field ${fileContents.substring(node.getFullStart(), node.getEnd())}`);
|
|
227
|
+
}
|
|
223
228
|
function parseFieldElement(element, sourceFile, fileContents, nested) {
|
|
224
|
-
if (element.kind !== typescript_1.default.SyntaxKind.CallExpression
|
|
225
|
-
|
|
229
|
+
if (element.kind !== typescript_1.default.SyntaxKind.CallExpression &&
|
|
230
|
+
element.kind !== typescript_1.default.SyntaxKind.PropertyAccessExpression) {
|
|
231
|
+
throwErr(fileContents, element, `skipped unknown (non-call|non-property) expression ${element.kind}`);
|
|
226
232
|
return null;
|
|
227
233
|
}
|
|
234
|
+
if (element.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
|
|
235
|
+
const ret = parseFieldElement(element.expression, sourceFile, fileContents, true);
|
|
236
|
+
if (ret !== null) {
|
|
237
|
+
if (!nested) {
|
|
238
|
+
ret.suffix = fileContents.substring(ret.callEx.getEnd(), element.getEnd());
|
|
239
|
+
}
|
|
240
|
+
return ret;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
228
243
|
let callEx = element;
|
|
229
244
|
if (callEx.arguments.length !== 1) {
|
|
230
245
|
// have a situation like: StringType({ name: "canonicalName" }).trim().toLowerCase(),
|
|
@@ -238,12 +253,22 @@ function parseFieldElement(element, sourceFile, fileContents, nested) {
|
|
|
238
253
|
return ret;
|
|
239
254
|
}
|
|
240
255
|
}
|
|
241
|
-
|
|
242
|
-
return null;
|
|
256
|
+
throwErr(fileContents, element, "callExpression with arguments not of length 1");
|
|
243
257
|
}
|
|
244
258
|
let arg = callEx.arguments[0];
|
|
245
259
|
if (arg.kind !== typescript_1.default.SyntaxKind.ObjectLiteralExpression) {
|
|
246
|
-
|
|
260
|
+
// this and the check above for PropertyAccessExpression are to handle things like
|
|
261
|
+
// FooType({
|
|
262
|
+
/// ...
|
|
263
|
+
// }).function(blah)
|
|
264
|
+
const ret = parseFieldElement(callEx.expression, sourceFile, fileContents, true);
|
|
265
|
+
if (ret !== null) {
|
|
266
|
+
if (!nested) {
|
|
267
|
+
ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
|
|
268
|
+
}
|
|
269
|
+
return ret;
|
|
270
|
+
}
|
|
271
|
+
throwErr(fileContents, element, `not objectLiteralExpression. kind ${arg.kind}`);
|
|
247
272
|
return null;
|
|
248
273
|
}
|
|
249
274
|
let expr = arg;
|
|
@@ -263,7 +288,7 @@ function parseFieldElement(element, sourceFile, fileContents, nested) {
|
|
|
263
288
|
}
|
|
264
289
|
}
|
|
265
290
|
if (!name) {
|
|
266
|
-
|
|
291
|
+
throwErr(fileContents, element, `couldn't find name property`);
|
|
267
292
|
return null;
|
|
268
293
|
}
|
|
269
294
|
// remove quotes
|