@smartive/graphql-magic 23.10.0 → 24.0.0-next.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.
- package/.github/workflows/docs.yml +1 -1
- package/.github/workflows/release.yml +1 -1
- package/CHANGELOG.md +1 -5
- package/dist/bin/gqm.cjs +24 -12
- package/dist/cjs/index.cjs +48 -24
- package/dist/esm/client/mutations.js +18 -2
- package/dist/esm/client/mutations.js.map +1 -1
- package/dist/esm/migrations/generate.js +2 -7
- package/dist/esm/migrations/generate.js.map +1 -1
- package/dist/esm/models/model-definitions.d.ts +2 -0
- package/dist/esm/models/models.d.ts +2 -0
- package/dist/esm/models/models.js.map +1 -1
- package/dist/esm/models/mutation-hook.d.ts +1 -0
- package/dist/esm/resolvers/mutations.d.ts +17 -5
- package/dist/esm/resolvers/mutations.js +22 -10
- package/dist/esm/resolvers/mutations.js.map +1 -1
- package/dist/esm/schema/generate.js +2 -0
- package/dist/esm/schema/generate.js.map +1 -1
- package/docker-compose.yml +1 -1
- package/docs/package-lock.json +3423 -2391
- package/package.json +1 -1
- package/src/client/mutations.ts +23 -2
- package/src/migrations/generate.ts +8 -15
- package/src/models/model-definitions.ts +14 -2
- package/src/models/models.ts +14 -2
- package/src/models/mutation-hook.ts +1 -0
- package/src/resolvers/mutations.ts +38 -9
- package/src/schema/generate.ts +2 -0
|
@@ -13,7 +13,7 @@ jobs:
|
|
|
13
13
|
persist-credentials: false
|
|
14
14
|
|
|
15
15
|
- name: Setup Node
|
|
16
|
-
uses: actions/setup-node@
|
|
16
|
+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
|
|
17
17
|
with:
|
|
18
18
|
node-version: '24'
|
|
19
19
|
- name: Install and Build
|
|
@@ -22,7 +22,7 @@ jobs:
|
|
|
22
22
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
|
|
23
23
|
with:
|
|
24
24
|
submodules: true
|
|
25
|
-
- uses: actions/setup-node@
|
|
25
|
+
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
|
|
26
26
|
with:
|
|
27
27
|
node-version: 24
|
|
28
28
|
registry-url: 'https://registry.npmjs.org'
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1 @@
|
|
|
1
|
-
## [
|
|
2
|
-
|
|
3
|
-
### Features
|
|
4
|
-
|
|
5
|
-
* extend exclude constraint with message, add DELETE event to constraint_trigger ([18818c9](https://github.com/smartive/graphql-magic/commit/18818c932e5fcff3ad2e15276287bf5385092e2c))
|
|
1
|
+
## [24.0.0-next.1](https://github.com/smartive/graphql-magic/compare/v23.11.0-next.1...v24.0.0-next.1) (2026-03-16)
|
package/dist/bin/gqm.cjs
CHANGED
|
@@ -41,27 +41,39 @@ var import_graphql = require("graphql");
|
|
|
41
41
|
// src/client/mutations.ts
|
|
42
42
|
var import_upperCase = __toESM(require("lodash/upperCase"), 1);
|
|
43
43
|
var constantCase = (str) => (0, import_upperCase.default)(str).replace(/ /g, "_");
|
|
44
|
+
var argTypeStr = (field) => {
|
|
45
|
+
const base = field.list ? `[${field.type}!]` : field.type;
|
|
46
|
+
return field.nonNull ? `${base}!` : base;
|
|
47
|
+
};
|
|
48
|
+
var argsToVariables = (args2) => args2.map((a) => `$${a.name}: ${argTypeStr(a)}`).join(", ");
|
|
49
|
+
var argsToMutationArgs = (args2) => args2.map((a) => `${a.name}: $${a.name}`).join(", ");
|
|
44
50
|
var generateMutations = (models) => {
|
|
45
51
|
const parts = [];
|
|
46
52
|
for (const { name: name2, creatable, updatable, deletable } of models.entities.filter(not(isRootModel))) {
|
|
47
53
|
if (creatable) {
|
|
54
|
+
const extraArgs = creatable !== true && creatable.args ? creatable.args : [];
|
|
55
|
+
const variables = extraArgs.length ? `$data: Create${name2}!, ${argsToVariables(extraArgs)}` : `$data: Create${name2}!`;
|
|
56
|
+
const mutationArgs = extraArgs.length ? `data: $data, ${argsToMutationArgs(extraArgs)}` : `data: $data`;
|
|
48
57
|
parts.push(
|
|
49
58
|
`export const CREATE_${constantCase(
|
|
50
59
|
name2
|
|
51
60
|
)} = gql\`
|
|
52
|
-
mutation Create${name2}Mutation($
|
|
53
|
-
create${name2}(
|
|
61
|
+
mutation Create${name2}Mutation(${variables}) {
|
|
62
|
+
create${name2}(${mutationArgs}) { id }
|
|
54
63
|
}
|
|
55
64
|
\`;`
|
|
56
65
|
);
|
|
57
66
|
}
|
|
58
67
|
if (updatable) {
|
|
68
|
+
const extraArgs = updatable !== true && updatable.args ? updatable.args : [];
|
|
69
|
+
const variables = extraArgs.length ? `$id: ID!, $data: Update${name2}!, ${argsToVariables(extraArgs)}` : `$id: ID!, $data: Update${name2}!`;
|
|
70
|
+
const mutationArgs = extraArgs.length ? `where: { id: $id }, data: $data, ${argsToMutationArgs(extraArgs)}` : `where: { id: $id }, data: $data`;
|
|
59
71
|
parts.push(
|
|
60
72
|
`export const UPDATE_${constantCase(
|
|
61
73
|
name2
|
|
62
74
|
)} = gql\`
|
|
63
|
-
mutation Update${name2}Mutation($
|
|
64
|
-
update${name2}(
|
|
75
|
+
mutation Update${name2}Mutation(${variables}) {
|
|
76
|
+
update${name2}(${mutationArgs}) { id }
|
|
65
77
|
}
|
|
66
78
|
\`;`
|
|
67
79
|
);
|
|
@@ -1884,11 +1896,7 @@ var MigrationGenerator = class _MigrationGenerator {
|
|
|
1884
1896
|
}
|
|
1885
1897
|
return -1;
|
|
1886
1898
|
}
|
|
1887
|
-
static TRIGGER_EVENT_ORDER = [
|
|
1888
|
-
"INSERT",
|
|
1889
|
-
"UPDATE",
|
|
1890
|
-
"DELETE"
|
|
1891
|
-
];
|
|
1899
|
+
static TRIGGER_EVENT_ORDER = ["INSERT", "UPDATE", "DELETE"];
|
|
1892
1900
|
static sortTriggerEvents(events) {
|
|
1893
1901
|
return [...events].sort(
|
|
1894
1902
|
(a, b) => _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) - _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b)
|
|
@@ -1896,7 +1904,9 @@ var MigrationGenerator = class _MigrationGenerator {
|
|
|
1896
1904
|
}
|
|
1897
1905
|
normalizeTriggerDef(def) {
|
|
1898
1906
|
let s = def.replace(/\s+/g, " ").replace(/\s*\(\s*/g, "(").replace(/\s*\)\s*/g, ")").replace(/\bON\s+[a-zA-Z_][a-zA-Z0-9_]*\./gi, "ON ").trim();
|
|
1899
|
-
const eventsMatch = s.match(
|
|
1907
|
+
const eventsMatch = s.match(
|
|
1908
|
+
/\b(AFTER|BEFORE)\s+((?:INSERT|UPDATE|DELETE)(?:\s+OR\s+(?:INSERT|UPDATE|DELETE))+)\s+ON\b/i
|
|
1909
|
+
);
|
|
1900
1910
|
if (eventsMatch) {
|
|
1901
1911
|
const events = eventsMatch[2].split(/\s+OR\s+/).map((e) => e.toUpperCase());
|
|
1902
1912
|
const sorted = [...events].sort(
|
|
@@ -2804,7 +2814,8 @@ var generateDefinitions = ({
|
|
|
2804
2814
|
name: "data",
|
|
2805
2815
|
type: `Create${model.name}`,
|
|
2806
2816
|
nonNull: true
|
|
2807
|
-
}
|
|
2817
|
+
},
|
|
2818
|
+
...model.creatable && model.creatable !== true && model.creatable.args || []
|
|
2808
2819
|
]
|
|
2809
2820
|
});
|
|
2810
2821
|
}
|
|
@@ -2823,7 +2834,8 @@ var generateDefinitions = ({
|
|
|
2823
2834
|
name: "data",
|
|
2824
2835
|
type: `Update${model.name}`,
|
|
2825
2836
|
nonNull: true
|
|
2826
|
-
}
|
|
2837
|
+
},
|
|
2838
|
+
...model.updatable && model.updatable !== true && model.updatable.args || []
|
|
2827
2839
|
]
|
|
2828
2840
|
});
|
|
2829
2841
|
}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -307,27 +307,39 @@ var gql = (chunks, ...variables) => {
|
|
|
307
307
|
// src/client/mutations.ts
|
|
308
308
|
var import_upperCase = __toESM(require("lodash/upperCase"), 1);
|
|
309
309
|
var constantCase = (str) => (0, import_upperCase.default)(str).replace(/ /g, "_");
|
|
310
|
+
var argTypeStr = (field) => {
|
|
311
|
+
const base = field.list ? `[${field.type}!]` : field.type;
|
|
312
|
+
return field.nonNull ? `${base}!` : base;
|
|
313
|
+
};
|
|
314
|
+
var argsToVariables = (args2) => args2.map((a) => `$${a.name}: ${argTypeStr(a)}`).join(", ");
|
|
315
|
+
var argsToMutationArgs = (args2) => args2.map((a) => `${a.name}: $${a.name}`).join(", ");
|
|
310
316
|
var generateMutations = (models) => {
|
|
311
317
|
const parts = [];
|
|
312
318
|
for (const { name: name2, creatable, updatable, deletable } of models.entities.filter(not(isRootModel))) {
|
|
313
319
|
if (creatable) {
|
|
320
|
+
const extraArgs = creatable !== true && creatable.args ? creatable.args : [];
|
|
321
|
+
const variables = extraArgs.length ? `$data: Create${name2}!, ${argsToVariables(extraArgs)}` : `$data: Create${name2}!`;
|
|
322
|
+
const mutationArgs = extraArgs.length ? `data: $data, ${argsToMutationArgs(extraArgs)}` : `data: $data`;
|
|
314
323
|
parts.push(
|
|
315
324
|
`export const CREATE_${constantCase(
|
|
316
325
|
name2
|
|
317
326
|
)} = gql\`
|
|
318
|
-
mutation Create${name2}Mutation($
|
|
319
|
-
create${name2}(
|
|
327
|
+
mutation Create${name2}Mutation(${variables}) {
|
|
328
|
+
create${name2}(${mutationArgs}) { id }
|
|
320
329
|
}
|
|
321
330
|
\`;`
|
|
322
331
|
);
|
|
323
332
|
}
|
|
324
333
|
if (updatable) {
|
|
334
|
+
const extraArgs = updatable !== true && updatable.args ? updatable.args : [];
|
|
335
|
+
const variables = extraArgs.length ? `$id: ID!, $data: Update${name2}!, ${argsToVariables(extraArgs)}` : `$id: ID!, $data: Update${name2}!`;
|
|
336
|
+
const mutationArgs = extraArgs.length ? `where: { id: $id }, data: $data, ${argsToMutationArgs(extraArgs)}` : `where: { id: $id }, data: $data`;
|
|
325
337
|
parts.push(
|
|
326
338
|
`export const UPDATE_${constantCase(
|
|
327
339
|
name2
|
|
328
340
|
)} = gql\`
|
|
329
|
-
mutation Update${name2}Mutation($
|
|
330
|
-
update${name2}(
|
|
341
|
+
mutation Update${name2}Mutation(${variables}) {
|
|
342
|
+
update${name2}(${mutationArgs}) { id }
|
|
331
343
|
}
|
|
332
344
|
\`;`
|
|
333
345
|
);
|
|
@@ -2337,30 +2349,31 @@ var mutationResolver = async (_parent, args2, partialCtx, info) => withTransacti
|
|
|
2337
2349
|
const [, mutation, modelName] = it(info.fieldName.match(/^(create|update|delete|restore)(.+)$/));
|
|
2338
2350
|
switch (mutation) {
|
|
2339
2351
|
case "create": {
|
|
2340
|
-
const id = await createEntity(modelName, args2.data, ctx, "mutation");
|
|
2352
|
+
const id = await createEntity(modelName, args2.data, ctx, { trigger: "mutation", args: args2 });
|
|
2341
2353
|
return await resolve(ctx, id);
|
|
2342
2354
|
}
|
|
2343
2355
|
case "update": {
|
|
2344
2356
|
const id = args2.where.id;
|
|
2345
|
-
await updateEntity(modelName, id, args2.data, ctx, "mutation");
|
|
2357
|
+
await updateEntity(modelName, id, args2.data, ctx, { trigger: "mutation", args: args2 });
|
|
2346
2358
|
return await resolve(ctx, id);
|
|
2347
2359
|
}
|
|
2348
2360
|
case "delete": {
|
|
2349
2361
|
const id = args2.where.id;
|
|
2350
2362
|
await deleteEntity(modelName, id, ctx, {
|
|
2351
2363
|
dryRun: args2.dryRun,
|
|
2352
|
-
trigger: "mutation"
|
|
2364
|
+
trigger: "mutation",
|
|
2365
|
+
args: args2
|
|
2353
2366
|
});
|
|
2354
2367
|
return id;
|
|
2355
2368
|
}
|
|
2356
2369
|
case "restore": {
|
|
2357
2370
|
const id = args2.where.id;
|
|
2358
|
-
await restoreEntity(modelName, id, ctx, "mutation");
|
|
2371
|
+
await restoreEntity(modelName, id, ctx, { trigger: "mutation", args: args2 });
|
|
2359
2372
|
return id;
|
|
2360
2373
|
}
|
|
2361
2374
|
}
|
|
2362
2375
|
});
|
|
2363
|
-
var createEntity = async (modelName, input2, ctx, trigger = "direct-call") => withTransaction(ctx, async (ctx2) => {
|
|
2376
|
+
var createEntity = async (modelName, input2, ctx, { args: args2, trigger = "direct-call" } = {}) => withTransaction(ctx, async (ctx2) => {
|
|
2364
2377
|
const model = ctx2.models.getModel(modelName, "entity");
|
|
2365
2378
|
const normalizedInput = { ...input2 };
|
|
2366
2379
|
if (!normalizedInput.id) {
|
|
@@ -2384,6 +2397,7 @@ var createEntity = async (modelName, input2, ctx, trigger = "direct-call") => wi
|
|
|
2384
2397
|
trigger,
|
|
2385
2398
|
when: "before",
|
|
2386
2399
|
data: { prev: {}, input: input2, normalizedInput, next: normalizedInput },
|
|
2400
|
+
args: args2,
|
|
2387
2401
|
ctx: ctx2
|
|
2388
2402
|
});
|
|
2389
2403
|
if (model.parent) {
|
|
@@ -2416,17 +2430,18 @@ var createEntity = async (modelName, input2, ctx, trigger = "direct-call") => wi
|
|
|
2416
2430
|
trigger,
|
|
2417
2431
|
when: "after",
|
|
2418
2432
|
data: { prev: {}, input: input2, normalizedInput, next: normalizedInput },
|
|
2433
|
+
args: args2,
|
|
2419
2434
|
ctx: ctx2
|
|
2420
2435
|
});
|
|
2421
2436
|
return normalizedInput.id;
|
|
2422
2437
|
});
|
|
2423
|
-
var updateEntities = async (modelName, where, updateFields, ctx) => withTransaction(ctx, async (ctx2) => {
|
|
2438
|
+
var updateEntities = async (modelName, where, updateFields, ctx, { args: args2 } = {}) => withTransaction(ctx, async (ctx2) => {
|
|
2424
2439
|
const entities = await ctx2.knex(modelName).where(where).select("id");
|
|
2425
2440
|
for (const entity of entities) {
|
|
2426
|
-
await updateEntity(modelName, entity.id, updateFields, ctx2);
|
|
2441
|
+
await updateEntity(modelName, entity.id, updateFields, ctx2, args2);
|
|
2427
2442
|
}
|
|
2428
2443
|
});
|
|
2429
|
-
var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") => withTransaction(ctx, async (ctx2) => {
|
|
2444
|
+
var updateEntity = async (modelName, id, input2, ctx, { args: args2, trigger = "direct-call" } = {}) => withTransaction(ctx, async (ctx2) => {
|
|
2430
2445
|
const model = ctx2.models.getModel(modelName, "entity");
|
|
2431
2446
|
const normalizedInput = { ...input2 };
|
|
2432
2447
|
sanitize(ctx2, model, normalizedInput);
|
|
@@ -2444,6 +2459,7 @@ var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") =
|
|
|
2444
2459
|
trigger,
|
|
2445
2460
|
when: "before",
|
|
2446
2461
|
data: { prev: currentEntity, input: input2, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2462
|
+
args: args2,
|
|
2447
2463
|
ctx: ctx2
|
|
2448
2464
|
});
|
|
2449
2465
|
await doUpdate(model, currentEntity, normalizedInput, ctx2);
|
|
@@ -2453,15 +2469,17 @@ var updateEntity = async (modelName, id, input2, ctx, trigger = "direct-call") =
|
|
|
2453
2469
|
trigger,
|
|
2454
2470
|
when: "after",
|
|
2455
2471
|
data: { prev: currentEntity, input: input2, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2472
|
+
args: args2,
|
|
2456
2473
|
ctx: ctx2
|
|
2457
2474
|
});
|
|
2458
2475
|
}
|
|
2459
2476
|
});
|
|
2460
|
-
var deleteEntities = async (modelName, where, ctx) => withTransaction(ctx, async (ctx2) => {
|
|
2477
|
+
var deleteEntities = async (modelName, where, ctx, args2) => withTransaction(ctx, async (ctx2) => {
|
|
2461
2478
|
const entities = await ctx2.knex(modelName).where(where).select("id");
|
|
2462
2479
|
for (const entity of entities) {
|
|
2463
2480
|
await deleteEntity(modelName, entity.id, ctx2, {
|
|
2464
|
-
trigger: "direct-call"
|
|
2481
|
+
trigger: "direct-call",
|
|
2482
|
+
args: args2
|
|
2465
2483
|
});
|
|
2466
2484
|
}
|
|
2467
2485
|
});
|
|
@@ -2482,7 +2500,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2482
2500
|
const mutations = [];
|
|
2483
2501
|
const afterHooks = [];
|
|
2484
2502
|
const mutationHook = ctx2.mutationHook;
|
|
2485
|
-
const deleteCascade = async (currentModel, currentEntity, currentTrigger) => {
|
|
2503
|
+
const deleteCascade = async (currentModel, currentEntity, currentTrigger, args2) => {
|
|
2486
2504
|
if (!(currentModel.name in toDelete)) {
|
|
2487
2505
|
toDelete[currentModel.name] = {};
|
|
2488
2506
|
}
|
|
@@ -2506,6 +2524,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2506
2524
|
trigger: currentTrigger,
|
|
2507
2525
|
when: "before",
|
|
2508
2526
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2527
|
+
args: args2,
|
|
2509
2528
|
ctx: ctx2
|
|
2510
2529
|
});
|
|
2511
2530
|
});
|
|
@@ -2521,6 +2540,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2521
2540
|
trigger: currentTrigger,
|
|
2522
2541
|
when: "after",
|
|
2523
2542
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2543
|
+
args: args2,
|
|
2524
2544
|
ctx: ctx2
|
|
2525
2545
|
});
|
|
2526
2546
|
});
|
|
@@ -2561,6 +2581,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2561
2581
|
trigger: "set-null",
|
|
2562
2582
|
when: "before",
|
|
2563
2583
|
data: { prev: descendant, input: {}, normalizedInput, next: { ...descendant, ...normalizedInput } },
|
|
2584
|
+
args: args2,
|
|
2564
2585
|
ctx: ctx2
|
|
2565
2586
|
});
|
|
2566
2587
|
});
|
|
@@ -2576,6 +2597,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2576
2597
|
trigger: "set-null",
|
|
2577
2598
|
when: "after",
|
|
2578
2599
|
data: { prev: descendant, input: {}, normalizedInput, next: { ...descendant, ...normalizedInput } },
|
|
2600
|
+
args: args2,
|
|
2579
2601
|
ctx: ctx2
|
|
2580
2602
|
});
|
|
2581
2603
|
});
|
|
@@ -2642,7 +2664,7 @@ var deleteEntity = async (modelName, id, ctx, {
|
|
|
2642
2664
|
});
|
|
2643
2665
|
}
|
|
2644
2666
|
});
|
|
2645
|
-
var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withTransaction(ctx, async (ctx2) => {
|
|
2667
|
+
var restoreEntity = async (modelName, id, ctx, { args: args2, trigger = "direct-call" } = {}) => withTransaction(ctx, async (ctx2) => {
|
|
2646
2668
|
const model = ctx2.models.getModel(modelName, "entity");
|
|
2647
2669
|
const rootModel = model.rootModel;
|
|
2648
2670
|
const entity = await getEntityToMutate(ctx2, rootModel, { id }, "RESTORE");
|
|
@@ -2687,6 +2709,7 @@ var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withT
|
|
|
2687
2709
|
trigger: currentTrigger,
|
|
2688
2710
|
when: "before",
|
|
2689
2711
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2712
|
+
args: args2,
|
|
2690
2713
|
ctx: ctx2
|
|
2691
2714
|
});
|
|
2692
2715
|
});
|
|
@@ -2717,6 +2740,7 @@ var restoreEntity = async (modelName, id, ctx, trigger = "direct-call") => withT
|
|
|
2717
2740
|
trigger: currentTrigger,
|
|
2718
2741
|
when: "after",
|
|
2719
2742
|
data: { prev: currentEntity, input: {}, normalizedInput, next: { ...currentEntity, ...normalizedInput } },
|
|
2743
|
+
args: args2,
|
|
2720
2744
|
ctx: ctx2
|
|
2721
2745
|
});
|
|
2722
2746
|
});
|
|
@@ -3899,11 +3923,7 @@ var MigrationGenerator = class _MigrationGenerator {
|
|
|
3899
3923
|
}
|
|
3900
3924
|
return -1;
|
|
3901
3925
|
}
|
|
3902
|
-
static TRIGGER_EVENT_ORDER = [
|
|
3903
|
-
"INSERT",
|
|
3904
|
-
"UPDATE",
|
|
3905
|
-
"DELETE"
|
|
3906
|
-
];
|
|
3926
|
+
static TRIGGER_EVENT_ORDER = ["INSERT", "UPDATE", "DELETE"];
|
|
3907
3927
|
static sortTriggerEvents(events) {
|
|
3908
3928
|
return [...events].sort(
|
|
3909
3929
|
(a, b) => _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) - _MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b)
|
|
@@ -3911,7 +3931,9 @@ var MigrationGenerator = class _MigrationGenerator {
|
|
|
3911
3931
|
}
|
|
3912
3932
|
normalizeTriggerDef(def) {
|
|
3913
3933
|
let s = def.replace(/\s+/g, " ").replace(/\s*\(\s*/g, "(").replace(/\s*\)\s*/g, ")").replace(/\bON\s+[a-zA-Z_][a-zA-Z0-9_]*\./gi, "ON ").trim();
|
|
3914
|
-
const eventsMatch = s.match(
|
|
3934
|
+
const eventsMatch = s.match(
|
|
3935
|
+
/\b(AFTER|BEFORE)\s+((?:INSERT|UPDATE|DELETE)(?:\s+OR\s+(?:INSERT|UPDATE|DELETE))+)\s+ON\b/i
|
|
3936
|
+
);
|
|
3915
3937
|
if (eventsMatch) {
|
|
3916
3938
|
const events = eventsMatch[2].split(/\s+OR\s+/).map((e) => e.toUpperCase());
|
|
3917
3939
|
const sorted = [...events].sort(
|
|
@@ -4912,7 +4934,8 @@ var generateDefinitions = ({
|
|
|
4912
4934
|
name: "data",
|
|
4913
4935
|
type: `Create${model.name}`,
|
|
4914
4936
|
nonNull: true
|
|
4915
|
-
}
|
|
4937
|
+
},
|
|
4938
|
+
...model.creatable && model.creatable !== true && model.creatable.args || []
|
|
4916
4939
|
]
|
|
4917
4940
|
});
|
|
4918
4941
|
}
|
|
@@ -4931,7 +4954,8 @@ var generateDefinitions = ({
|
|
|
4931
4954
|
name: "data",
|
|
4932
4955
|
type: `Update${model.name}`,
|
|
4933
4956
|
nonNull: true
|
|
4934
|
-
}
|
|
4957
|
+
},
|
|
4958
|
+
...model.updatable && model.updatable !== true && model.updatable.args || []
|
|
4935
4959
|
]
|
|
4936
4960
|
});
|
|
4937
4961
|
}
|
|
@@ -1,14 +1,30 @@
|
|
|
1
1
|
import upperCase from 'lodash/upperCase';
|
|
2
2
|
import { isRootModel, not } from '..';
|
|
3
3
|
const constantCase = (str) => upperCase(str).replace(/ /g, '_');
|
|
4
|
+
const argTypeStr = (field) => {
|
|
5
|
+
const base = field.list ? `[${field.type}!]` : field.type;
|
|
6
|
+
return field.nonNull ? `${base}!` : base;
|
|
7
|
+
};
|
|
8
|
+
const argsToVariables = (args) => args.map((a) => `$${a.name}: ${argTypeStr(a)}`).join(', ');
|
|
9
|
+
const argsToMutationArgs = (args) => args.map((a) => `${a.name}: $${a.name}`).join(', ');
|
|
4
10
|
export const generateMutations = (models) => {
|
|
5
11
|
const parts = [];
|
|
6
12
|
for (const { name, creatable, updatable, deletable } of models.entities.filter(not(isRootModel))) {
|
|
7
13
|
if (creatable) {
|
|
8
|
-
|
|
14
|
+
const extraArgs = creatable !== true && creatable.args ? creatable.args : [];
|
|
15
|
+
const variables = extraArgs.length ? `$data: Create${name}!, ${argsToVariables(extraArgs)}` : `$data: Create${name}!`;
|
|
16
|
+
const mutationArgs = extraArgs.length ? `data: $data, ${argsToMutationArgs(extraArgs)}` : `data: $data`;
|
|
17
|
+
parts.push(`export const CREATE_${constantCase(name)} = gql\`\n mutation Create${name}Mutation(${variables}) {\n create${name}(${mutationArgs}) { id }\n }\n\`;`);
|
|
9
18
|
}
|
|
10
19
|
if (updatable) {
|
|
11
|
-
|
|
20
|
+
const extraArgs = updatable !== true && updatable.args ? updatable.args : [];
|
|
21
|
+
const variables = extraArgs.length
|
|
22
|
+
? `$id: ID!, $data: Update${name}!, ${argsToVariables(extraArgs)}`
|
|
23
|
+
: `$id: ID!, $data: Update${name}!`;
|
|
24
|
+
const mutationArgs = extraArgs.length
|
|
25
|
+
? `where: { id: $id }, data: $data, ${argsToMutationArgs(extraArgs)}`
|
|
26
|
+
: `where: { id: $id }, data: $data`;
|
|
27
|
+
parts.push(`export const UPDATE_${constantCase(name)} = gql\`\n mutation Update${name}Mutation(${variables}) {\n update${name}(${mutationArgs}) { id }\n }\n\`;`);
|
|
12
28
|
}
|
|
13
29
|
if (deletable) {
|
|
14
30
|
parts.push(`export const DELETE_${constantCase(name)} = gql\`\n mutation Delete${name}Mutation($id: ID!) {\n delete${name}(where: { id: $id })\n }\n\`;`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../../src/client/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../../src/client/mutations.ts"],"names":[],"mappings":"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"}
|
|
@@ -801,14 +801,9 @@ export class MigrationGenerator {
|
|
|
801
801
|
}
|
|
802
802
|
return -1;
|
|
803
803
|
}
|
|
804
|
-
static TRIGGER_EVENT_ORDER = [
|
|
805
|
-
'INSERT',
|
|
806
|
-
'UPDATE',
|
|
807
|
-
'DELETE',
|
|
808
|
-
];
|
|
804
|
+
static TRIGGER_EVENT_ORDER = ['INSERT', 'UPDATE', 'DELETE'];
|
|
809
805
|
static sortTriggerEvents(events) {
|
|
810
|
-
return [...events].sort((a, b) => MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) -
|
|
811
|
-
MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b));
|
|
806
|
+
return [...events].sort((a, b) => MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(a) - MigrationGenerator.TRIGGER_EVENT_ORDER.indexOf(b));
|
|
812
807
|
}
|
|
813
808
|
normalizeTriggerDef(def) {
|
|
814
809
|
let s = def
|