@jskit-ai/crud-core 0.2.13 → 0.2.15
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 +16 -0
- package/locales/en.json +1 -11
- package/package.json +19 -19
- package/patterns/{json-api-resource-package → crud-feature-package}/PATTERN.md +69 -37
- package/patterns/{json-api-resource-package → crud-feature-package}/example/packages/books/package.json +5 -4
- package/patterns/crud-feature-package/example/packages/books/src/server/BooksFeature.js +13 -0
- package/patterns/crud-feature-package/example/packages/books/src/shared/bookResource.js +74 -0
- package/patterns/crud-feature-package/example/packages/books/src/shared/index.js +1 -0
- package/src/server/createHooksToCollectChildren.js +56 -34
- package/src/server/crudContracts.js +54 -0
- package/src/server/defineCrudFeature.js +509 -0
- package/src/server/jsonApiModule/actions.js +44 -30
- package/src/server/jsonApiModule/routes.js +2 -2
- package/src/server/jsonApiModule/service.js +213 -32
- package/src/server/jsonApiResourceContract.js +41 -1
- package/src/server/listQueryValidators.js +1 -1
- package/src/server/removedApi.js +87 -0
- package/src/server/types.js +26 -50
- package/test/assistantActionConformance.test.js +20 -21
- package/test/assistantPagination.integration.test.js +32 -42
- package/test/createHooksToCollectChildren.test.js +61 -16
- package/test/crudFeaturePackagePattern.test.js +50 -0
- package/test/crudOnHandles.test.js +454 -0
- package/test/{defineCrudJsonApiFeature.test.js → defineCrudFeature.test.js} +444 -141
- package/test/errorCodes.test.js +11 -52
- package/test/hiddenFields.test.js +62 -0
- package/test/listQueryValidators.test.js +1 -1
- package/test/routeContracts.test.js +1 -1
- package/test/support/crudJsonApiActionOptions.js +3 -3
- package/test/unservedCrudAutomation.test.js +27 -22
- package/patterns/json-api-resource-package/example/packages/books/src/server/BooksFeature.js +0 -13
- package/patterns/json-api-resource-package/example/packages/books/src/shared/bookResource.js +0 -83
- package/patterns/json-api-resource-package/example/packages/books/src/shared/index.js +0 -1
- package/src/server/createCrudServiceFromResource.js +0 -63
- package/src/server/defineCrudJsonApiFeature.js +0 -346
- package/src/server/fieldAccess.js +0 -298
- package/src/server/jsonApiModule/repository.js +0 -355
- package/src/server/lookupPathSupport.js +0 -58
- package/src/server/lookups.js +0 -40
- package/src/server/repositorySupport.js +0 -528
- package/src/server/resourceRuntime/compiledRuntime.js +0 -284
- package/src/server/resourceRuntime/index.js +0 -659
- package/src/server/resourceRuntime/listControls.js +0 -333
- package/src/server/resourceRuntime/lookupHydration.js +0 -626
- package/src/server/resourceRuntime/operationStages.js +0 -140
- package/src/server/resourceRuntime/recordValidation.js +0 -65
- package/src/server/serviceMethods.js +0 -149
- package/test/createCrudServiceFromResource.test.js +0 -323
- package/test/fieldAccess.test.js +0 -86
- package/test/jsonApiRepository.test.js +0 -333
- package/test/jsonApiRepositoryDataCosts.test.js +0 -183
- package/test/jsonApiRepositoryPageLimits.test.js +0 -158
- package/test/jsonApiRepositoryRowLocks.test.js +0 -137
- package/test/jsonApiResourcePackagePattern.test.js +0 -20
- package/test/lookups.test.js +0 -111
- package/test/repositorySupport.test.js +0 -767
- package/test/resourceRuntime.test.js +0 -1014
- package/test/serviceMethods.test.js +0 -139
- /package/patterns/{json-api-resource-package → crud-feature-package}/example/migrations/20260815000000_books.cjs +0 -0
- /package/patterns/{json-api-resource-package → crud-feature-package}/example/packages/books/src/server/types.js +0 -0
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
|
-
import {
|
|
3
|
-
buildRepositoryColumnMetadata,
|
|
4
|
-
deriveRepositoryMappingFromResource,
|
|
5
|
-
requireCrudTableName,
|
|
6
|
-
resolveColumnName,
|
|
7
|
-
resolveCrudIdColumn
|
|
8
|
-
} from "../repositorySupport.js";
|
|
9
|
-
import { resolveSchemaTransportSchemaDefinition } from "@jskit-ai/kernel/shared/validators";
|
|
10
|
-
import { buildCrudLookupRuntime } from "./lookupHydration.js";
|
|
11
|
-
import { CRUD_FIELD_STORAGE_COLUMN } from "@jskit-ai/resource-crud-core/shared/crudFieldContract";
|
|
12
|
-
import { resolveListRuntimeConfig } from "./listControls.js";
|
|
13
|
-
|
|
14
|
-
const REPOSITORY_OPERATION_KEYS = Object.freeze([
|
|
15
|
-
"read",
|
|
16
|
-
"list",
|
|
17
|
-
"findById",
|
|
18
|
-
"listByIds",
|
|
19
|
-
"create",
|
|
20
|
-
"updateById",
|
|
21
|
-
"deleteById"
|
|
22
|
-
]);
|
|
23
|
-
const REPOSITORY_OPERATION_STAGE_KEYS = Object.freeze({
|
|
24
|
-
read: ["applyQuery"],
|
|
25
|
-
list: ["applyQuery"],
|
|
26
|
-
findById: ["applyQuery"],
|
|
27
|
-
listByIds: ["applyQuery"],
|
|
28
|
-
create: ["preparePayload", "prepareInsertPayload", "applyQuery"],
|
|
29
|
-
updateById: ["preparePatch", "applyQuery"],
|
|
30
|
-
deleteById: ["applyQuery"]
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
function requireCrudRepositoryOptions(value) {
|
|
34
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
35
|
-
throw new TypeError("createCrudResourceRuntime requires repository options object when provided.");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return value;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function resolveRepositoryDefaults(resource, repositoryMapping, { context }) {
|
|
42
|
-
const resourceName = normalizeText(resource.namespace);
|
|
43
|
-
const tableName = normalizeText(resource.tableName) || resourceName;
|
|
44
|
-
if (!tableName) {
|
|
45
|
-
throw new TypeError(`${context} requires resource.tableName or resource.namespace.`);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const idColumn = normalizeText(resource.idColumn) || resolveColumnName("id", repositoryMapping.columnOverrides) || "id";
|
|
49
|
-
const createdAtColumn = repositoryMapping.outputKeys.includes("createdAt") &&
|
|
50
|
-
repositoryMapping.fieldStorageByKey.createdAt === CRUD_FIELD_STORAGE_COLUMN
|
|
51
|
-
? resolveColumnName("createdAt", repositoryMapping.columnOverrides)
|
|
52
|
-
: "";
|
|
53
|
-
const updatedAtColumn = repositoryMapping.outputKeys.includes("updatedAt") &&
|
|
54
|
-
repositoryMapping.fieldStorageByKey.updatedAt === CRUD_FIELD_STORAGE_COLUMN
|
|
55
|
-
? resolveColumnName("updatedAt", repositoryMapping.columnOverrides)
|
|
56
|
-
: "";
|
|
57
|
-
|
|
58
|
-
return Object.freeze({
|
|
59
|
-
tableName,
|
|
60
|
-
idColumn,
|
|
61
|
-
createdAtColumn,
|
|
62
|
-
updatedAtColumn
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function normalizeCrudVirtualFieldHandlers(virtualFields = {}, repositoryMapping, { context }) {
|
|
67
|
-
const expectedKeys = new Set(repositoryMapping.virtualOutputKeys);
|
|
68
|
-
if (expectedKeys.size < 1) {
|
|
69
|
-
if (virtualFields === null) {
|
|
70
|
-
return Object.freeze([]);
|
|
71
|
-
}
|
|
72
|
-
if (!virtualFields || typeof virtualFields !== "object" || Array.isArray(virtualFields)) {
|
|
73
|
-
throw new TypeError(`${context} virtualFields must be an object when provided.`);
|
|
74
|
-
}
|
|
75
|
-
if (Object.keys(virtualFields).length > 0) {
|
|
76
|
-
throw new Error(
|
|
77
|
-
`${context} virtualFields contains registrations, but the resource does not declare any storage.virtual fields.`
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
return Object.freeze([]);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (!virtualFields || typeof virtualFields !== "object" || Array.isArray(virtualFields)) {
|
|
84
|
-
throw new TypeError(`${context} virtualFields must be an object.`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const normalizedHandlers = [];
|
|
88
|
-
const seenKeys = new Set();
|
|
89
|
-
for (const [rawKey, handlerConfig] of Object.entries(virtualFields)) {
|
|
90
|
-
const key = normalizeText(rawKey);
|
|
91
|
-
if (!key || seenKeys.has(key)) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (!expectedKeys.has(key)) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
`${context} virtualFields["${key}"] is unknown; declare the field in the resource schema with storage.virtual true.`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
if (!handlerConfig || typeof handlerConfig !== "object" || Array.isArray(handlerConfig)) {
|
|
100
|
-
throw new TypeError(`${context} virtualFields["${key}"] must be an object.`);
|
|
101
|
-
}
|
|
102
|
-
// readable-check: allow method-probe - input validation: an application's virtual field handler must have applyProjection()
|
|
103
|
-
if (typeof handlerConfig.applyProjection !== "function") {
|
|
104
|
-
throw new TypeError(`${context} virtualFields["${key}"].applyProjection must be a function.`);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
seenKeys.add(key);
|
|
108
|
-
normalizedHandlers.push(Object.freeze({
|
|
109
|
-
key,
|
|
110
|
-
alias: resolveColumnName(key, repositoryMapping.columnOverrides),
|
|
111
|
-
applyProjection: handlerConfig.applyProjection
|
|
112
|
-
}));
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
for (const key of expectedKeys) {
|
|
116
|
-
if (!seenKeys.has(key)) {
|
|
117
|
-
throw new Error(
|
|
118
|
-
`${context} resource output field "${key}" is virtual but no repository runtime projection was registered.`
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return Object.freeze(normalizedHandlers);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function resolveRecordOutputValidator(resource, { context }) {
|
|
127
|
-
const output = resource.operations.view?.output;
|
|
128
|
-
if (!output || typeof output !== "object" || Array.isArray(output)) {
|
|
129
|
-
throw new TypeError(`${context} requires operations.view.output.`);
|
|
130
|
-
}
|
|
131
|
-
const schema = resolveSchemaTransportSchemaDefinition(output, {
|
|
132
|
-
context: `${context} operations.view.output`,
|
|
133
|
-
defaultMode: "replace"
|
|
134
|
-
});
|
|
135
|
-
if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
|
|
136
|
-
throw new TypeError(`${context} requires operations.view.output to resolve to an object schema.`);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return output;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function resolveOperationBodyValidator(resource, operationKey, { context }) {
|
|
143
|
-
const body = resource.operations[operationKey]?.body;
|
|
144
|
-
if (body == null) {
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
148
|
-
throw new TypeError(`${context} operations.${operationKey}.body must be an object when provided.`);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return body;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function normalizeCrudRepositoryOperationStage(stage, stageLabel, { context }) {
|
|
155
|
-
if (stage === undefined || stage === null) {
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
if (typeof stage !== "function") {
|
|
159
|
-
throw new TypeError(`${context} ${stageLabel} must be a function when provided.`);
|
|
160
|
-
}
|
|
161
|
-
return stage;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function normalizeCrudRepositoryOperationConfig(config, operationKey, { context }) {
|
|
165
|
-
if (config === undefined || config === null) {
|
|
166
|
-
return Object.freeze({});
|
|
167
|
-
}
|
|
168
|
-
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
169
|
-
throw new TypeError(`${context} operations.${operationKey} must be an object when provided.`);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const allowedKeys = REPOSITORY_OPERATION_STAGE_KEYS[operationKey];
|
|
173
|
-
const normalized = {};
|
|
174
|
-
for (const [rawKey, rawValue] of Object.entries(config)) {
|
|
175
|
-
const key = normalizeText(rawKey);
|
|
176
|
-
if (!key) {
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
if (!allowedKeys.includes(key)) {
|
|
180
|
-
throw new TypeError(
|
|
181
|
-
`${context} operations.${operationKey}.${key} is not supported. Allowed keys: ${allowedKeys.join(", ")}.`
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
normalized[key] = normalizeCrudRepositoryOperationStage(
|
|
185
|
-
rawValue,
|
|
186
|
-
`operations.${operationKey}.${key}`,
|
|
187
|
-
{ context }
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return Object.freeze(normalized);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Each repository operation's stages, with an entry for every operation, so a
|
|
196
|
-
* stage is read as `operations.<operation>.<stage>` (undefined when not configured).
|
|
197
|
-
* @param {unknown} sourceOperations The repository options' operations, when given.
|
|
198
|
-
* @param {{ context: string }} labels
|
|
199
|
-
*/
|
|
200
|
-
function normalizeCrudRepositoryOperations(sourceOperations, { context }) {
|
|
201
|
-
const source = sourceOperations ?? {};
|
|
202
|
-
if (typeof source !== "object" || Array.isArray(source)) {
|
|
203
|
-
throw new TypeError(`${context} operations must be an object when provided.`);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const normalized = {};
|
|
207
|
-
for (const operationKey of REPOSITORY_OPERATION_KEYS) {
|
|
208
|
-
normalized[operationKey] = normalizeCrudRepositoryOperationConfig(
|
|
209
|
-
source[operationKey],
|
|
210
|
-
operationKey,
|
|
211
|
-
{ context }
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
for (const rawKey of Object.keys(source)) {
|
|
216
|
-
const key = normalizeText(rawKey);
|
|
217
|
-
if (key && !REPOSITORY_OPERATION_KEYS.includes(key)) {
|
|
218
|
-
throw new TypeError(
|
|
219
|
-
`${context} operations.${key} is not supported. Allowed keys: ${REPOSITORY_OPERATION_KEYS.join(", ")}.`
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return Object.freeze(normalized);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* What a records repository resolves once from its resource and options: the
|
|
229
|
-
* error context, table and id column (the options' tableName and idColumn, else
|
|
230
|
-
* the resource's), the field mapping, the input and output schemas, the list
|
|
231
|
-
* controls, the lookups, each operation's stages and the virtual fields.
|
|
232
|
-
* @param {import("../types.js").CrudResourceDefinition} resource
|
|
233
|
-
* @param {object} repositoryOptions
|
|
234
|
-
*/
|
|
235
|
-
function buildCompiledCrudRepositoryRuntime(resource, repositoryOptions) {
|
|
236
|
-
const sourceOptions = requireCrudRepositoryOptions(repositoryOptions);
|
|
237
|
-
const context = normalizeText(sourceOptions.context) || "crudRepository";
|
|
238
|
-
const repositoryMapping = deriveRepositoryMappingFromResource(resource, { context });
|
|
239
|
-
const defaults = resolveRepositoryDefaults(resource, repositoryMapping, { context });
|
|
240
|
-
const output = resolveRecordOutputValidator(resource, { context });
|
|
241
|
-
const lookupRuntime = buildCrudLookupRuntime(resource, {
|
|
242
|
-
outputKeys: repositoryMapping.outputKeys,
|
|
243
|
-
context
|
|
244
|
-
});
|
|
245
|
-
const listRuntime = resolveListRuntimeConfig(sourceOptions.list, repositoryMapping.listSearchColumns, {
|
|
246
|
-
idColumn: defaults.idColumn
|
|
247
|
-
});
|
|
248
|
-
const { selectColumns } = buildRepositoryColumnMetadata({
|
|
249
|
-
outputKeys: repositoryMapping.columnBackedOutputKeys,
|
|
250
|
-
writeKeys: repositoryMapping.writeKeys,
|
|
251
|
-
columnOverrides: repositoryMapping.columnOverrides,
|
|
252
|
-
fieldStorageByKey: repositoryMapping.fieldStorageByKey
|
|
253
|
-
});
|
|
254
|
-
const normalizedSelectColumns = Object.freeze(
|
|
255
|
-
[...new Set([
|
|
256
|
-
...selectColumns,
|
|
257
|
-
...listRuntime.orderBy.map(({ column }) => column)
|
|
258
|
-
])]
|
|
259
|
-
);
|
|
260
|
-
|
|
261
|
-
return Object.freeze({
|
|
262
|
-
context,
|
|
263
|
-
resource,
|
|
264
|
-
repositoryOptions: sourceOptions,
|
|
265
|
-
tableName: requireCrudTableName(sourceOptions.tableName ?? defaults.tableName, { context }),
|
|
266
|
-
idColumn: resolveCrudIdColumn(sourceOptions.idColumn, { fallback: defaults.idColumn }),
|
|
267
|
-
defaults,
|
|
268
|
-
input: Object.freeze({
|
|
269
|
-
create: resolveOperationBodyValidator(resource, "create", { context }),
|
|
270
|
-
patch: resolveOperationBodyValidator(resource, "patch", { context })
|
|
271
|
-
}),
|
|
272
|
-
selectColumns: normalizedSelectColumns,
|
|
273
|
-
output,
|
|
274
|
-
list: listRuntime,
|
|
275
|
-
lookup: lookupRuntime,
|
|
276
|
-
mapping: repositoryMapping,
|
|
277
|
-
operations: normalizeCrudRepositoryOperations(sourceOptions.operations, { context }),
|
|
278
|
-
virtualFields: normalizeCrudVirtualFieldHandlers(sourceOptions.virtualFields, repositoryMapping, { context })
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
export {
|
|
283
|
-
buildCompiledCrudRepositoryRuntime
|
|
284
|
-
};
|