@jskit-ai/crud-core 0.2.15 → 0.2.16
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/CHANGELOG.md +5 -0
- package/package.json +7 -7
- package/patterns/crud-feature-package/example/packages/books/package.json +3 -3
- package/src/server/defineCrudFeature.js +58 -29
- package/src/server/jsonApiModule/actions.js +73 -29
- package/src/server/jsonApiModule/routes.js +34 -10
- package/src/server/jsonApiModule/service.js +16 -2
- package/test/crudFeaturePackagePattern.test.js +5 -4
- package/test/crudOnHandles.test.js +9 -12
- package/test/defineCrudFeature.test.js +7 -0
- package/test/hiddenFields.test.js +5 -4
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- Depends on json-rest-api 3.1.0 exactly (from 3.0.1), from the registry with its published integrity: `lockMode: "noKeyUpdate"`, ON CONFLICT on the matching unique index's exact columns, the PostgreSQL merge race on an uncommitted insert, and the `xid8` field type the change horizon reads.
|
|
6
|
+
- `defineCrudFeature` refuses a table resource with ownership `system`, which only a system context reaches: a CRUD surface serves requests, so the owner serves what a request may see through its own methods instead.
|
|
7
|
+
- The package's data tests run on `createPackageTestKnex`, so CI's `DB_CLIENT=pg` run proves the CRUD paths on PostgreSQL too.
|
|
8
|
+
- A CRUD feature builds its routes and its actions' definitions (their input schemas, channels, permissions and assistant extensions) once per `defineCrudFeature` definition, when a composition first needs them, and every later composition and boot reuses them; each CRUD contract's record and include shapes, which its services read, are worked out once per contract. They depend only on the definition and the frozen contract, so what a composition answers is unchanged, and a build that throws keeps nothing, so every composition that reaches it fails as the first did. Rebuilding them took most of each composition's time: an app of 24 CRUD features composed about six times faster after the first composition (around 84 ms each, from around 500 ms, on one host, back to back).
|
|
9
|
+
- **Security.** A CRUD feature's capability repository answers `joinSource({ alias, filters }, options)`, the table as a join source with the columns a read answers and never a hidden one, and, outside a system context, refuses any call whose `fields` names a hidden field, whatever the method, and any method answering another query builder: a join source `decorateRepository` built on `scopedQuery()`, which carried every column, becomes the handle's `joinSource`. A feature whose owned tables hide a field in one and show a field of that name in another fails to start (see `@jskit-ai/resource-runtime`).
|
|
5
10
|
- Depends on json-rest-api 3.0.1 (from 3.0.0), exactly, as `@jskit-ai/resource-runtime` does: see its CHANGELOG for the concurrent-merge fix.
|
|
6
11
|
- **Security.** A CRUD feature's capability repository answers without hidden fields through every method it holds, the ones `decorateRepository` adds included, for every table the feature owns, unless the call runs in a system context: a custom read that forwarded the caller's `fields` handed hidden values to other features.
|
|
7
12
|
- **Security.** A CRUD feature's capability repository (`resourceApi.repository`) on a table with hidden fields refuses another feature's `scopedQuery()` unless it runs in a system context, as it refuses a read naming a hidden field: the builder carries every column. An owner answers its rows to another owner's report through a join source method of its own.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/crud-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
"./locales/en.json": "./locales/en.json"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@jskit-ai/resource-core": "0.1.
|
|
24
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
25
|
-
"json-rest-api": "3.0
|
|
23
|
+
"@jskit-ai/resource-core": "0.1.206",
|
|
24
|
+
"@jskit-ai/resource-crud-core": "0.1.206",
|
|
25
|
+
"json-rest-api": "3.1.0",
|
|
26
26
|
"json-rest-schema": "^1.0.17"
|
|
27
27
|
},
|
|
28
28
|
"description": "Shared server-side CRUD service, repository, route, and query helpers.",
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
53
|
-
"@jskit-ai/kernel": "0.2.
|
|
54
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
52
|
+
"@jskit-ai/http-runtime": "0.2.15",
|
|
53
|
+
"@jskit-ai/kernel": "0.2.15",
|
|
54
|
+
"@jskit-ai/resource-runtime": "0.2.13"
|
|
55
55
|
}
|
|
56
56
|
}
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
"./shared": "./src/shared/index.js"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@jskit-ai/crud-core": "0.2.
|
|
13
|
-
"@jskit-ai/resource-core": "0.1.
|
|
14
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
12
|
+
"@jskit-ai/crud-core": "0.2.16",
|
|
13
|
+
"@jskit-ai/resource-core": "0.1.206",
|
|
14
|
+
"@jskit-ai/resource-crud-core": "0.1.206"
|
|
15
15
|
},
|
|
16
16
|
"jskit": {
|
|
17
17
|
"kind": "runtime",
|
|
@@ -6,10 +6,11 @@ import { withoutHiddenFields } from "@jskit-ai/resource-runtime/server";
|
|
|
6
6
|
import { isCrudContract } from "@jskit-ai/resource-crud-core/shared/crudContract";
|
|
7
7
|
import {
|
|
8
8
|
assertCrudOperationName,
|
|
9
|
+
createCrudJsonApiActionDefinitions,
|
|
9
10
|
createCrudJsonApiActions,
|
|
10
11
|
normalizeCrudOperationLifecycle
|
|
11
12
|
} from "./jsonApiModule/actions.js";
|
|
12
|
-
import {
|
|
13
|
+
import { createCrudJsonApiRoutes, registerCrudRoutes } from "./jsonApiModule/routes.js";
|
|
13
14
|
import { createCrudService } from "./jsonApiModule/service.js";
|
|
14
15
|
import {
|
|
15
16
|
LEGACY_REPOSITORY_METHODS,
|
|
@@ -88,6 +89,19 @@ const REMOVED_REPOSITORY_DEPENDENCIES = Object.freeze({
|
|
|
88
89
|
const REMOVED_SERVICE_DEPENDENCIES = Object.freeze({ resource: RESOURCE_RENAMED });
|
|
89
90
|
const REMOVED_RESOURCE_API = Object.freeze({ resource: RESOURCE_RENAMED });
|
|
90
91
|
|
|
92
|
+
/**
|
|
93
|
+
* What `build` answers, built on first use and then kept: the contracts a feature builds from its definition alone,
|
|
94
|
+
* which every composition and boot reuses. A build that throws keeps nothing, so each composition that reaches it
|
|
95
|
+
* fails as the first did.
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {() => T} build
|
|
98
|
+
* @returns {() => T}
|
|
99
|
+
*/
|
|
100
|
+
function builtOnce(build) {
|
|
101
|
+
let built;
|
|
102
|
+
return () => (built ??= build());
|
|
103
|
+
}
|
|
104
|
+
|
|
91
105
|
/**
|
|
92
106
|
* Refuses an option defineCrudFeature does not take, naming where an earlier
|
|
93
107
|
* option now goes, so a setting is never silently ignored.
|
|
@@ -368,6 +382,12 @@ function defineCrudFeature(feature) {
|
|
|
368
382
|
throw new TypeError("defineCrudFeature requires crud, a CRUD contract from defineCrudContract.");
|
|
369
383
|
}
|
|
370
384
|
const tableResource = crud.resource;
|
|
385
|
+
if (tableResource.ownership === "system") {
|
|
386
|
+
throw new TypeError(
|
|
387
|
+
`defineCrudFeature serves requests, and only a system context reaches "${tableResource.name}" (ownership "system"): ` +
|
|
388
|
+
"serve what a request may see through the owner's own methods instead."
|
|
389
|
+
);
|
|
390
|
+
}
|
|
371
391
|
const namespace = crud.namespace;
|
|
372
392
|
const featureId = normalizeText(id) || `crud.${namespace}`;
|
|
373
393
|
const featureCapability = normalizeText(capability) || featureId;
|
|
@@ -410,6 +430,41 @@ function defineCrudFeature(feature) {
|
|
|
410
430
|
const permissionOptions = permissions && typeof permissions === "object" && !Array.isArray(permissions)
|
|
411
431
|
? { ...permissions, namespace }
|
|
412
432
|
: { namespace };
|
|
433
|
+
const servedOverHttp = routes && !internal;
|
|
434
|
+
// The routes and the actions' inputs, permissions and assistant extensions depend on the definition alone (the
|
|
435
|
+
// CRUD contract, the operations, the scope and the options above), never on what a composition provides.
|
|
436
|
+
const crudRoutes = builtOnce(() => createCrudJsonApiRoutes({
|
|
437
|
+
namespace,
|
|
438
|
+
resource: crud,
|
|
439
|
+
routeBase: normalizedScope.routeBase,
|
|
440
|
+
relativePath: normalizedRelativePath,
|
|
441
|
+
surface: normalizedSurface,
|
|
442
|
+
ownershipFilter,
|
|
443
|
+
access,
|
|
444
|
+
internal,
|
|
445
|
+
operations: enabledOperations,
|
|
446
|
+
actions,
|
|
447
|
+
listFilterQueryValidator,
|
|
448
|
+
operationInputs,
|
|
449
|
+
routeParamsValidator: normalizedScope.routeParamsValidator,
|
|
450
|
+
scopeInput: normalizedScope.input
|
|
451
|
+
}));
|
|
452
|
+
const actionDefinitions = builtOnce(() => createCrudJsonApiActionDefinitions({
|
|
453
|
+
namespace,
|
|
454
|
+
resource: crud,
|
|
455
|
+
surface: normalizedSurface,
|
|
456
|
+
operations: enabledOperations,
|
|
457
|
+
listFilterQueryValidator,
|
|
458
|
+
operationInputs,
|
|
459
|
+
actions,
|
|
460
|
+
permissionForOperation: (operation) => resolveActionPermission(operation, {
|
|
461
|
+
access,
|
|
462
|
+
workspaceScoped,
|
|
463
|
+
permissions: permissionOptions
|
|
464
|
+
}),
|
|
465
|
+
scopeInputValidator: normalizedScope.actionInputValidator,
|
|
466
|
+
servedOverHttp
|
|
467
|
+
}));
|
|
413
468
|
|
|
414
469
|
return defineFeature({
|
|
415
470
|
id: featureId,
|
|
@@ -449,22 +504,7 @@ function defineCrudFeature(feature) {
|
|
|
449
504
|
const service = projectEnabledServiceOperations(decoratedService, enabledOperations);
|
|
450
505
|
assertServiceOperations(service, enabledOperations);
|
|
451
506
|
if (routes) {
|
|
452
|
-
|
|
453
|
-
namespace,
|
|
454
|
-
resource: crud,
|
|
455
|
-
routeBase: normalizedScope.routeBase,
|
|
456
|
-
relativePath: normalizedRelativePath,
|
|
457
|
-
surface: normalizedSurface,
|
|
458
|
-
ownershipFilter,
|
|
459
|
-
access,
|
|
460
|
-
internal,
|
|
461
|
-
operations: enabledOperations,
|
|
462
|
-
actions,
|
|
463
|
-
listFilterQueryValidator,
|
|
464
|
-
operationInputs,
|
|
465
|
-
routeParamsValidator: normalizedScope.routeParamsValidator,
|
|
466
|
-
scopeInput: normalizedScope.input
|
|
467
|
-
});
|
|
507
|
+
registerCrudRoutes(http.router, crudRoutes());
|
|
468
508
|
}
|
|
469
509
|
// The handle decorateRepository receives answers hidden fields to the owner's own methods; other features,
|
|
470
510
|
// and this feature's actions, read them only in a system context, through any method the repository holds.
|
|
@@ -485,22 +525,11 @@ function defineCrudFeature(feature) {
|
|
|
485
525
|
resource: crud,
|
|
486
526
|
repository: resourceApi.repository,
|
|
487
527
|
service: resourceApi.service,
|
|
488
|
-
surface: normalizedSurface,
|
|
489
|
-
operations: enabledOperations,
|
|
490
|
-
listFilterQueryValidator,
|
|
491
528
|
operationLifecycle: normalizedOperationLifecycle,
|
|
492
|
-
operationInputs,
|
|
493
|
-
actions,
|
|
494
529
|
dependencies: actionDependencies,
|
|
495
|
-
permissionForOperation: (operation) => resolveActionPermission(operation, {
|
|
496
|
-
access,
|
|
497
|
-
workspaceScoped,
|
|
498
|
-
permissions: permissionOptions
|
|
499
|
-
}),
|
|
500
|
-
scopeInputValidator: normalizedScope.actionInputValidator,
|
|
501
530
|
scopeInputKeys: normalizedScope.inputKeys,
|
|
502
531
|
realtimeAudience,
|
|
503
|
-
|
|
532
|
+
definitions: actionDefinitions()
|
|
504
533
|
});
|
|
505
534
|
}
|
|
506
535
|
});
|
|
@@ -164,6 +164,62 @@ function normalizeAdditionalActions(definitions, { namespace, surface, permissio
|
|
|
164
164
|
}));
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
/**
|
|
168
|
+
* What a CRUD feature's actions take from its definition alone, so a feature builds it once and every composition
|
|
169
|
+
* reuses it: each enabled standard operation's id, channels, permission, input and assistant extension, and the
|
|
170
|
+
* feature's custom actions, checked and with the scope's input composed in.
|
|
171
|
+
* @param {object} definition
|
|
172
|
+
* @param {string} definition.namespace
|
|
173
|
+
* @param {import("../types.js").CrudContract} definition.resource
|
|
174
|
+
* @param {string} definition.surface
|
|
175
|
+
* @param {(operation: string) => object} definition.permissionForOperation Also answers for each custom action's name.
|
|
176
|
+
* @param {readonly string[]} definition.operations
|
|
177
|
+
* @param {object | null} definition.scopeInputValidator
|
|
178
|
+
* @param {object | null} definition.listFilterQueryValidator
|
|
179
|
+
* @param {{ create?: object, update?: object }} definition.operationInputs
|
|
180
|
+
* @param {Record<string, object>} definition.actions The feature's custom actions, by name.
|
|
181
|
+
* @param {boolean} definition.servedOverHttp False when the feature is internal or registers no routes.
|
|
182
|
+
*/
|
|
183
|
+
function createCrudJsonApiActionDefinitions({
|
|
184
|
+
namespace,
|
|
185
|
+
resource,
|
|
186
|
+
surface,
|
|
187
|
+
permissionForOperation,
|
|
188
|
+
operations,
|
|
189
|
+
scopeInputValidator,
|
|
190
|
+
listFilterQueryValidator,
|
|
191
|
+
operationInputs,
|
|
192
|
+
actions,
|
|
193
|
+
servedOverHttp
|
|
194
|
+
}) {
|
|
195
|
+
const additional = normalizeAdditionalActions(actions, {
|
|
196
|
+
namespace,
|
|
197
|
+
surface,
|
|
198
|
+
permissionForAction: permissionForOperation,
|
|
199
|
+
scopeInputValidator,
|
|
200
|
+
servedOverHttp
|
|
201
|
+
});
|
|
202
|
+
const enabledOperations = new Set(operations);
|
|
203
|
+
const standard = CRUD_OPERATION_NAMES.filter((operation) => enabledOperations.has(operation)).map((operation) => {
|
|
204
|
+
const permission = permissionForOperation(operation);
|
|
205
|
+
return Object.freeze({
|
|
206
|
+
operation,
|
|
207
|
+
id: `crud.${namespace}.${operation}`,
|
|
208
|
+
channels: resolveCrudActionChannels(permission, servedOverHttp),
|
|
209
|
+
surfaces: [surface],
|
|
210
|
+
permission,
|
|
211
|
+
input: buildActionInput(resource, operation, scopeInputValidator, listFilterQueryValidator, operationInputs),
|
|
212
|
+
extensions: Object.freeze({
|
|
213
|
+
assistant: createCrudAssistantExtension(resource, namespace, operation)
|
|
214
|
+
})
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
return Object.freeze({
|
|
218
|
+
standard: Object.freeze(standard),
|
|
219
|
+
additional
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
167
223
|
/**
|
|
168
224
|
* The standard and custom actions of one CRUD feature, from the options
|
|
169
225
|
* defineCrudFeature resolved.
|
|
@@ -184,38 +240,28 @@ function normalizeAdditionalActions(definitions, { namespace, surface, permissio
|
|
|
184
240
|
* @param {Record<string, unknown>} dependencies.dependencies The capabilities in the feature's `requires`, by local name.
|
|
185
241
|
* @param {string} dependencies.realtimeAudience
|
|
186
242
|
* @param {boolean} dependencies.servedOverHttp False when the feature is internal or registers no routes.
|
|
243
|
+
* @param {ReturnType<typeof createCrudJsonApiActionDefinitions>} [dependencies.definitions] What
|
|
244
|
+
* createCrudJsonApiActionDefinitions built from the same options, when the caller keeps it; built here otherwise.
|
|
187
245
|
*/
|
|
188
246
|
function createCrudJsonApiActions({
|
|
189
247
|
namespace,
|
|
190
248
|
resource,
|
|
191
249
|
repository,
|
|
192
250
|
service,
|
|
193
|
-
surface,
|
|
194
|
-
permissionForOperation,
|
|
195
|
-
operations,
|
|
196
|
-
scopeInputValidator,
|
|
197
251
|
scopeInputKeys,
|
|
198
|
-
listFilterQueryValidator,
|
|
199
252
|
operationLifecycle,
|
|
200
|
-
|
|
201
|
-
actions,
|
|
202
|
-
dependencies,
|
|
253
|
+
dependencies: actionDependencies,
|
|
203
254
|
realtimeAudience,
|
|
204
|
-
|
|
255
|
+
definitions: builtDefinitions,
|
|
256
|
+
...definitionOptions
|
|
205
257
|
}) {
|
|
258
|
+
const definitions = builtDefinitions ?? createCrudJsonApiActionDefinitions({ namespace, resource, ...definitionOptions });
|
|
206
259
|
const mutationEvent = (operation, entityId) => buildCrudRecordChangedEvent({
|
|
207
260
|
namespace,
|
|
208
261
|
operation,
|
|
209
262
|
entityId,
|
|
210
263
|
audience: realtimeAudience
|
|
211
264
|
});
|
|
212
|
-
const additionalActions = normalizeAdditionalActions(actions, {
|
|
213
|
-
namespace,
|
|
214
|
-
surface,
|
|
215
|
-
permissionForAction: permissionForOperation,
|
|
216
|
-
scopeInputValidator,
|
|
217
|
-
servedOverHttp
|
|
218
|
-
});
|
|
219
265
|
|
|
220
266
|
// HTTP and automation callers keep the resource's page cap: a larger limit answers
|
|
221
267
|
// pageLimits().request records, and the response's pagination reports the page size, hasMore
|
|
@@ -352,26 +398,23 @@ function createCrudJsonApiActions({
|
|
|
352
398
|
}
|
|
353
399
|
];
|
|
354
400
|
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
const permission = permissionForOperation(definition.operation);
|
|
401
|
+
const standardActions = definitions.standard.map((built) => {
|
|
402
|
+
const definition = standardDefinitions.find(({ operation }) => operation === built.operation);
|
|
358
403
|
return Object.freeze({
|
|
359
|
-
id:
|
|
404
|
+
id: built.id,
|
|
360
405
|
version: 1,
|
|
361
406
|
kind: definition.kind,
|
|
362
|
-
channels:
|
|
363
|
-
surfaces: [
|
|
364
|
-
permission,
|
|
365
|
-
input:
|
|
407
|
+
channels: built.channels,
|
|
408
|
+
surfaces: [...built.surfaces],
|
|
409
|
+
permission: built.permission,
|
|
410
|
+
input: built.input,
|
|
366
411
|
output: null,
|
|
367
|
-
extensions:
|
|
368
|
-
assistant: createCrudAssistantExtension(resource, namespace, definition.operation)
|
|
369
|
-
}),
|
|
412
|
+
extensions: built.extensions,
|
|
370
413
|
events: definition.events,
|
|
371
414
|
execute: definition.execute
|
|
372
415
|
});
|
|
373
416
|
});
|
|
374
|
-
const productActions =
|
|
417
|
+
const productActions = definitions.additional.map((definition) => Object.freeze({
|
|
375
418
|
id: definition.id,
|
|
376
419
|
version: definition.version,
|
|
377
420
|
kind: definition.kind,
|
|
@@ -382,7 +425,7 @@ function createCrudJsonApiActions({
|
|
|
382
425
|
permission: definition.permission,
|
|
383
426
|
extensions: definition.extensions || {},
|
|
384
427
|
events: definition.events,
|
|
385
|
-
execute: (input, context) => definition.execute(Object.freeze({ ...
|
|
428
|
+
execute: (input, context) => definition.execute(Object.freeze({ ...actionDependencies, input, context, resource, repository, service }))
|
|
386
429
|
}));
|
|
387
430
|
return Object.freeze([...standardActions, ...productActions]);
|
|
388
431
|
}
|
|
@@ -396,6 +439,7 @@ function assertCrudOperationName(operation = "") {
|
|
|
396
439
|
|
|
397
440
|
export {
|
|
398
441
|
assertCrudOperationName,
|
|
442
|
+
createCrudJsonApiActionDefinitions,
|
|
399
443
|
createCrudJsonApiActions,
|
|
400
444
|
normalizeCrudOperationLifecycle
|
|
401
445
|
};
|
|
@@ -75,8 +75,8 @@ function normalizeCustomActionRoutes(actions, namespace) {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
78
|
+
* The routes of one CRUD feature, from the options defineCrudFeature resolved: each `{ method, path, contract,
|
|
79
|
+
* handler }`, built from the definition alone, so a feature builds them once and registers them on every router.
|
|
80
80
|
* @param {object} options
|
|
81
81
|
* @param {string} options.namespace
|
|
82
82
|
* @param {import("../types.js").CrudContract} options.resource
|
|
@@ -93,7 +93,7 @@ function normalizeCustomActionRoutes(actions, namespace) {
|
|
|
93
93
|
* @param {object | null} options.routeParamsValidator
|
|
94
94
|
* @param {((request: object) => object) | null} options.scopeInput
|
|
95
95
|
*/
|
|
96
|
-
function
|
|
96
|
+
function createCrudJsonApiRoutes({
|
|
97
97
|
namespace,
|
|
98
98
|
resource,
|
|
99
99
|
routeBase,
|
|
@@ -133,6 +133,9 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
133
133
|
const enabledOperations = new Set(operations);
|
|
134
134
|
const customActionRoutes = normalizeCustomActionRoutes(actions, namespace);
|
|
135
135
|
|
|
136
|
+
const routes = [];
|
|
137
|
+
const route = (method, path, contract, handler) => routes.push(Object.freeze({ method, path, contract, handler }));
|
|
138
|
+
|
|
136
139
|
function paramsContract({ record = false } = {}) {
|
|
137
140
|
if (record) {
|
|
138
141
|
return { params: routeContracts.recordRouteParamsValidator };
|
|
@@ -140,7 +143,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
140
143
|
return routeParamsValidator ? { params: routeParamsValidator } : {};
|
|
141
144
|
}
|
|
142
145
|
|
|
143
|
-
if (enabledOperations.has("list"))
|
|
146
|
+
if (enabledOperations.has("list")) route(
|
|
144
147
|
"GET",
|
|
145
148
|
basePath,
|
|
146
149
|
{
|
|
@@ -161,7 +164,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
161
164
|
}
|
|
162
165
|
);
|
|
163
166
|
|
|
164
|
-
if (enabledOperations.has("view"))
|
|
167
|
+
if (enabledOperations.has("view")) route(
|
|
165
168
|
"GET",
|
|
166
169
|
`${basePath}/:recordId`,
|
|
167
170
|
{
|
|
@@ -183,7 +186,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
183
186
|
}
|
|
184
187
|
);
|
|
185
188
|
|
|
186
|
-
if (enabledOperations.has("create"))
|
|
189
|
+
if (enabledOperations.has("create")) route(
|
|
187
190
|
"POST",
|
|
188
191
|
basePath,
|
|
189
192
|
{
|
|
@@ -204,7 +207,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
204
207
|
}
|
|
205
208
|
);
|
|
206
209
|
|
|
207
|
-
if (enabledOperations.has("update"))
|
|
210
|
+
if (enabledOperations.has("update")) route(
|
|
208
211
|
"PATCH",
|
|
209
212
|
`${basePath}/:recordId`,
|
|
210
213
|
{
|
|
@@ -226,7 +229,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
226
229
|
}
|
|
227
230
|
);
|
|
228
231
|
|
|
229
|
-
if (enabledOperations.has("delete"))
|
|
232
|
+
if (enabledOperations.has("delete")) route(
|
|
230
233
|
"DELETE",
|
|
231
234
|
`${basePath}/:recordId`,
|
|
232
235
|
{
|
|
@@ -258,7 +261,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
258
261
|
}
|
|
259
262
|
: {})
|
|
260
263
|
};
|
|
261
|
-
|
|
264
|
+
route(
|
|
262
265
|
actionRoute.method,
|
|
263
266
|
`${basePath}${actionRoute.path}`,
|
|
264
267
|
{
|
|
@@ -284,6 +287,27 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
284
287
|
}
|
|
285
288
|
);
|
|
286
289
|
}
|
|
290
|
+
return Object.freeze(routes);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Registers routes createCrudJsonApiRoutes built, each with its own copy of its contract.
|
|
295
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeRouter} router
|
|
296
|
+
* @param {ReadonlyArray<{ method: string, path: string, contract: object, handler: Function }>} routes
|
|
297
|
+
*/
|
|
298
|
+
function registerCrudRoutes(router, routes) {
|
|
299
|
+
for (const { method, path, contract, handler } of routes) {
|
|
300
|
+
router.register(method, path, { ...contract }, handler);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Registers the routes of one CRUD feature, from the options defineCrudFeature resolved.
|
|
306
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeRouter} router
|
|
307
|
+
* @param {Parameters<typeof createCrudJsonApiRoutes>[0]} options
|
|
308
|
+
*/
|
|
309
|
+
function registerCrudJsonApiRoutes(router, options) {
|
|
310
|
+
registerCrudRoutes(router, createCrudJsonApiRoutes(options));
|
|
287
311
|
}
|
|
288
312
|
|
|
289
|
-
export { registerCrudJsonApiRoutes };
|
|
313
|
+
export { createCrudJsonApiRoutes, registerCrudJsonApiRoutes, registerCrudRoutes };
|
|
@@ -75,6 +75,21 @@ function documentShapes(crud) {
|
|
|
75
75
|
return new Map(contractShape(crud).relationships.map((entry) => [entry.relationshipName, includedShape(entry.include, entry.labelKey)]));
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
// What each CRUD contract shows, by contract. defineCrudContract deep-freezes a contract, so what it shows is worked
|
|
79
|
+
// out once and every service on it reads the same shapes; a contract whose shapes throw keeps nothing.
|
|
80
|
+
const shapesByContract = new WeakMap();
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* What a contract's documents show: its own records' shape and its includes' shapes.
|
|
84
|
+
* @param {import("../types.js").CrudContract} crud
|
|
85
|
+
*/
|
|
86
|
+
function contractShapes(crud) {
|
|
87
|
+
if (!shapesByContract.has(crud)) {
|
|
88
|
+
shapesByContract.set(crud, Object.freeze({ own: contractShape(crud), linked: documentShapes(crud) }));
|
|
89
|
+
}
|
|
90
|
+
return shapesByContract.get(crud);
|
|
91
|
+
}
|
|
92
|
+
|
|
78
93
|
/**
|
|
79
94
|
* The CRUD service: the HTTP edge of a CRUD feature. It takes an operation's
|
|
80
95
|
* HTTP query and body, calls the feature's repository (its table's handle, as
|
|
@@ -91,8 +106,7 @@ function documentShapes(crud) {
|
|
|
91
106
|
*/
|
|
92
107
|
function createCrudService({ repository, crud }) {
|
|
93
108
|
const resourceName = crud.resource.name;
|
|
94
|
-
const own =
|
|
95
|
-
const shapes = documentShapes(crud);
|
|
109
|
+
const { own, linked: shapes } = contractShapes(crud);
|
|
96
110
|
|
|
97
111
|
/**
|
|
98
112
|
* @param {Record<string, any>} record A JSON:API resource object.
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { readFile } from "node:fs/promises";
|
|
3
3
|
import test from "node:test";
|
|
4
|
-
import
|
|
5
|
-
import { createTestResourceRuntime } from "@jskit-ai/resource-runtime/test";
|
|
4
|
+
import { createPackageTestKnex, createTestResourceRuntime } from "@jskit-ai/resource-runtime/test";
|
|
6
5
|
import { bookCrud, bookResource } from "../patterns/crud-feature-package/example/packages/books/src/shared/bookResource.js";
|
|
7
6
|
|
|
7
|
+
// SQLite in memory, or a PostgreSQL schema of its own on a DB_CLIENT=pg run (see createPackageTestKnex).
|
|
8
|
+
const testKnex = (t) => createPackageTestKnex(t, { environment: process.env });
|
|
9
|
+
|
|
8
10
|
const providerPath = new URL(
|
|
9
11
|
"../patterns/crud-feature-package/example/packages/books/src/server/BooksFeature.js",
|
|
10
12
|
import.meta.url
|
|
@@ -23,8 +25,7 @@ test("CRUD feature package pattern leaves repetition in the framework", async ()
|
|
|
23
25
|
});
|
|
24
26
|
|
|
25
27
|
test("the pattern's books resource is owned and creates a book, its timestamps in whole seconds as declared", async (t) => {
|
|
26
|
-
const knex =
|
|
27
|
-
t.after(() => knex.destroy());
|
|
28
|
+
const knex = await testKnex(t);
|
|
28
29
|
await knex.schema.createTable("books", (table) => {
|
|
29
30
|
table.increments("id");
|
|
30
31
|
table.integer("user_id").notNullable();
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import knexLib from "knex";
|
|
4
3
|
import { simplifyJsonApiResult } from "@jskit-ai/http-runtime/shared";
|
|
5
4
|
import { validateSchemaPayload } from "@jskit-ai/kernel/shared/validators";
|
|
6
5
|
import { passedOn } from "@jskit-ai/resource-runtime/server";
|
|
7
|
-
import { createTestResourceRuntime } from "@jskit-ai/resource-runtime/test";
|
|
6
|
+
import { createPackageTestKnex, createTestResourceRuntime } from "@jskit-ai/resource-runtime/test";
|
|
8
7
|
import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
|
|
9
8
|
import { defineCrudContract } from "@jskit-ai/resource-crud-core/shared/crudContract";
|
|
10
9
|
import { createCrudJsonApiActions } from "../src/server/jsonApiModule/actions.js";
|
|
@@ -12,12 +11,14 @@ import { createCrudService } from "../src/server/jsonApiModule/service.js";
|
|
|
12
11
|
import { createCrudContracts } from "../src/server/crudContracts.js";
|
|
13
12
|
import { crudJsonApiActionOptions } from "./support/crudJsonApiActionOptions.js";
|
|
14
13
|
|
|
14
|
+
// SQLite in memory, or a PostgreSQL schema of its own on a DB_CLIENT=pg run (see createPackageTestKnex).
|
|
15
|
+
const testKnex = (t) => createPackageTestKnex(t, { environment: process.env });
|
|
16
|
+
|
|
15
17
|
const WORKSPACE_ONE = Object.freeze({ scopeValues: Object.freeze({ workspaceId: "1" }) });
|
|
16
18
|
|
|
17
19
|
// 250 notes in workspace 1, read through json-rest-api's default page cap (100), with an audit table beside them.
|
|
18
20
|
async function createFixture(t) {
|
|
19
|
-
const knex =
|
|
20
|
-
t.after(() => knex.destroy());
|
|
21
|
+
const knex = await testKnex(t);
|
|
21
22
|
await knex.schema.createTable("notes", (table) => {
|
|
22
23
|
table.increments("id");
|
|
23
24
|
table.integer("workspace_id").notNullable();
|
|
@@ -113,8 +114,7 @@ test("a column the contract does not name is neither returned nor accepted", asy
|
|
|
113
114
|
|
|
114
115
|
// Notes, each by an author and with an editor, both authors; the authors table keeps a column no contract names.
|
|
115
116
|
async function createIncludeFixture(t, { authorOutput = "optional", authorsShow = { name: { output: "required" } } } = {}) {
|
|
116
|
-
const knex =
|
|
117
|
-
t.after(() => knex.destroy());
|
|
117
|
+
const knex = await testKnex(t);
|
|
118
118
|
await knex.schema.createTable("authors", (table) => {
|
|
119
119
|
table.increments("id");
|
|
120
120
|
table.string("name").notNullable();
|
|
@@ -164,8 +164,7 @@ async function createIncludeFixture(t, { authorOutput = "optional", authorsShow
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
test("a record a form saves back as the CRUD service showed it keeps its link to an author the writer cannot see", async (t) => {
|
|
167
|
-
const knex =
|
|
168
|
-
t.after(() => knex.destroy());
|
|
167
|
+
const knex = await testKnex(t);
|
|
169
168
|
await knex.schema.createTable("authors", (table) => {
|
|
170
169
|
table.increments("id");
|
|
171
170
|
table.string("name").notNullable();
|
|
@@ -260,8 +259,7 @@ test("an included record shows its lookup's label and the fields the including c
|
|
|
260
259
|
});
|
|
261
260
|
|
|
262
261
|
test("an include shows the records it links to only as deep, and only with the fields, as the including contract lists", async (t) => {
|
|
263
|
-
const knex =
|
|
264
|
-
t.after(() => knex.destroy());
|
|
262
|
+
const knex = await testKnex(t);
|
|
265
263
|
await knex.schema.createTable("publishers", (table) => {
|
|
266
264
|
table.increments("id");
|
|
267
265
|
table.string("name").notNullable();
|
|
@@ -408,8 +406,7 @@ test("a failing CRUD afterCommit hook reports committed and leaves the actual wr
|
|
|
408
406
|
});
|
|
409
407
|
|
|
410
408
|
test("the CRUD service's writes are stamped with createdAt and updatedAt when the table stores them", async (t) => {
|
|
411
|
-
const knex =
|
|
412
|
-
t.after(() => knex.destroy());
|
|
409
|
+
const knex = await testKnex(t);
|
|
413
410
|
await knex.schema.createTable("memos", (table) => {
|
|
414
411
|
table.increments("id");
|
|
415
412
|
table.string("title").notNullable();
|
|
@@ -1291,6 +1291,13 @@ test("defineCrudFeature rejects implicit workspace scope and public private owne
|
|
|
1291
1291
|
}),
|
|
1292
1292
|
/public CRUD feature serves a table resource whose ownership \(or visibility\) is "public"/u
|
|
1293
1293
|
);
|
|
1294
|
+
// A system table's rows reach no request, whatever visibility the routes would have.
|
|
1295
|
+
for (const visibility of [undefined, "public"]) {
|
|
1296
|
+
assert.throws(
|
|
1297
|
+
() => defineCrudFeature({ crud: createBookResource({ ownership: "system" }), surface: "app", visibility }),
|
|
1298
|
+
/defineCrudFeature serves requests, and only a system context reaches "books" \(ownership "system"\)/u
|
|
1299
|
+
);
|
|
1300
|
+
}
|
|
1294
1301
|
assert.throws(
|
|
1295
1302
|
() => defineCrudFeature({
|
|
1296
1303
|
crud: createBookResource(),
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import
|
|
4
|
-
import { startTestFeatures } from "@jskit-ai/resource-runtime/test";
|
|
3
|
+
import { createPackageTestKnex, startTestFeatures } from "@jskit-ai/resource-runtime/test";
|
|
5
4
|
import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
|
|
6
5
|
import { defineCrudContract } from "@jskit-ai/resource-crud-core/shared/crudContract";
|
|
7
6
|
import { defineCrudFeature } from "../src/server/defineCrudFeature.js";
|
|
8
7
|
import { CrudContractsProvider } from "../src/server/crudContracts.js";
|
|
9
8
|
|
|
9
|
+
// SQLite in memory, or a PostgreSQL schema of its own on a DB_CLIENT=pg run (see createPackageTestKnex).
|
|
10
|
+
const testKnex = (t) => createPackageTestKnex(t, { environment: process.env });
|
|
11
|
+
|
|
10
12
|
test("a CRUD feature's repository reads a hidden field of its table only in a system context; its owner's methods read it", async (t) => {
|
|
11
|
-
const knex =
|
|
12
|
-
t.after(() => knex.destroy());
|
|
13
|
+
const knex = await testKnex(t);
|
|
13
14
|
await knex.schema.createTable("tickets", (table) => {
|
|
14
15
|
table.increments("id");
|
|
15
16
|
table.string("title").notNullable();
|