@jskit-ai/crud-core 0.2.14 → 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,158 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
import knexLib from "knex";
|
|
4
|
-
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
5
|
-
import {
|
|
6
|
-
createJsonRestApiHost,
|
|
7
|
-
createJsonRestResourceScopeOptions
|
|
8
|
-
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
9
|
-
import { defineCrudResource } from "@jskit-ai/resource-crud-core/shared/crudResource";
|
|
10
|
-
import { createCrudJsonApiActions } from "../src/server/jsonApiModule/actions.js";
|
|
11
|
-
import { createCrudJsonApiRepository } from "../src/server/jsonApiModule/repository.js";
|
|
12
|
-
import { createCrudJsonApiService } from "../src/server/jsonApiModule/service.js";
|
|
13
|
-
import { crudJsonApiActionOptions } from "./support/crudJsonApiActionOptions.js";
|
|
14
|
-
|
|
15
|
-
// 250 notes by 30 authors, in teams "a" and "b", read through json-rest-api's default page cap (100).
|
|
16
|
-
async function createFixture(t) {
|
|
17
|
-
const knex = knexLib({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
|
|
18
|
-
t.after(() => knex.destroy());
|
|
19
|
-
await knex.schema.createTable("authors", (table) => {
|
|
20
|
-
table.increments("id");
|
|
21
|
-
table.string("name").notNullable();
|
|
22
|
-
});
|
|
23
|
-
await knex.schema.createTable("notes", (table) => {
|
|
24
|
-
table.increments("id");
|
|
25
|
-
table.integer("author_id").notNullable();
|
|
26
|
-
table.string("team").notNullable();
|
|
27
|
-
table.string("title").notNullable();
|
|
28
|
-
});
|
|
29
|
-
await knex.batchInsert("authors", Array.from({ length: 30 }, (_, index) => ({ name: `Author ${index + 1}` })), 100);
|
|
30
|
-
await knex.batchInsert("notes", Array.from({ length: 250 }, (_, index) => ({
|
|
31
|
-
author_id: (index % 30) + 1,
|
|
32
|
-
team: index % 2 === 0 ? "a" : "b",
|
|
33
|
-
title: `Note ${index + 1}`
|
|
34
|
-
})), 100);
|
|
35
|
-
|
|
36
|
-
const authors = defineCrudResource({
|
|
37
|
-
namespace: "authors",
|
|
38
|
-
tableName: "authors",
|
|
39
|
-
schema: { name: { type: "string", required: true } }
|
|
40
|
-
});
|
|
41
|
-
const notes = defineCrudResource({
|
|
42
|
-
namespace: "notes",
|
|
43
|
-
tableName: "notes",
|
|
44
|
-
schema: {
|
|
45
|
-
authorId: { type: "id", required: true, column: "author_id", belongsTo: "authors", as: "author" },
|
|
46
|
-
team: { type: "string", required: true, search: true },
|
|
47
|
-
title: { type: "string", required: true }
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
const api = await createJsonRestApiHost({ knex, logger: silentLogger });
|
|
51
|
-
await api.addResource("authors", createJsonRestResourceScopeOptions(authors));
|
|
52
|
-
await api.addResource("notes", createJsonRestResourceScopeOptions(notes));
|
|
53
|
-
return {
|
|
54
|
-
knex,
|
|
55
|
-
resource: notes,
|
|
56
|
-
repository: createCrudJsonApiRepository({ api, resource: notes, resourceScopeName: "notes" })
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const sortedIds = (rows) => rows.map((row) => Number(row.id)).sort((left, right) => left - right);
|
|
61
|
-
|
|
62
|
-
test("queryDocuments refuses a limit above queryMaxLimit and names queryEveryDocument", async (t) => {
|
|
63
|
-
const { repository } = await createFixture(t);
|
|
64
|
-
assert.equal(repository.queryMaxLimit(), 100);
|
|
65
|
-
|
|
66
|
-
const page = await repository.queryDocuments({ limit: 100 });
|
|
67
|
-
assert.equal(page.data.length, 100);
|
|
68
|
-
assert.equal(page.meta.pagination.hasMore, true);
|
|
69
|
-
assert.equal((await repository.queryDocuments({ limit: "100" })).data.length, 100);
|
|
70
|
-
|
|
71
|
-
for (const limit of [101, "250", 500]) {
|
|
72
|
-
await assert.rejects(
|
|
73
|
-
() => repository.queryDocuments({ limit }),
|
|
74
|
-
(error) => error instanceof TypeError &&
|
|
75
|
-
error.message.includes(`limit ${limit} is above its queryMaxLimit (100)`) &&
|
|
76
|
-
error.message.includes("queryEveryDocument(query, options)"),
|
|
77
|
-
`limit ${limit}`
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("queryEveryDocument returns every one of 250 records once, each included author once", async (t) => {
|
|
83
|
-
const { repository } = await createFixture(t);
|
|
84
|
-
|
|
85
|
-
const document = await repository.queryEveryDocument({ include: ["author"] });
|
|
86
|
-
|
|
87
|
-
assert.equal(document.data.length, 250);
|
|
88
|
-
assert.deepEqual(sortedIds(document.data), Array.from({ length: 250 }, (_, index) => index + 1));
|
|
89
|
-
assert.equal(document.included.length, 30);
|
|
90
|
-
assert.equal(new Set(document.included.map(({ type, id }) => `${type}:${id}`)).size, 30);
|
|
91
|
-
assert.ok(document.included.every(({ type }) => type === "authors"));
|
|
92
|
-
assert.deepEqual(document.data[0].relationships.author.data, { type: "authors", id: "1" });
|
|
93
|
-
|
|
94
|
-
const teamA = await repository.queryEveryDocument({ team: "a" });
|
|
95
|
-
assert.equal(teamA.data.length, 125);
|
|
96
|
-
assert.ok(teamA.data.every((record) => record.attributes.team === "a"));
|
|
97
|
-
assert.equal(Object.hasOwn(teamA, "included"), false);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test("queryEveryDocument reads in the caller's transaction and refuses a limit, cursor or sort", async (t) => {
|
|
101
|
-
const { repository } = await createFixture(t);
|
|
102
|
-
await repository.withTransaction(async (trx) => {
|
|
103
|
-
await trx("notes").insert({ author_id: 1, team: "a", title: "Uncommitted" });
|
|
104
|
-
const document = await repository.queryEveryDocument({ team: "a" }, { trx });
|
|
105
|
-
assert.equal(document.data.length, 126);
|
|
106
|
-
assert.ok(document.data.some((record) => record.attributes.title === "Uncommitted"));
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
for (const query of [{ limit: 10 }, { cursor: "abc" }, { sort: ["title"] }]) {
|
|
110
|
-
const [key] = Object.keys(query);
|
|
111
|
-
await assert.rejects(
|
|
112
|
-
() => repository.queryEveryDocument(query),
|
|
113
|
-
(error) => error instanceof TypeError && error.message.includes(`its query cannot name ${key}`),
|
|
114
|
-
key
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test("the list action keeps answering a larger limit with one capped page", async (t) => {
|
|
120
|
-
const { repository, resource } = await createFixture(t);
|
|
121
|
-
const actions = createCrudJsonApiActions(crudJsonApiActionOptions({
|
|
122
|
-
namespace: "notes",
|
|
123
|
-
resource,
|
|
124
|
-
repository,
|
|
125
|
-
service: createCrudJsonApiService({ repository }),
|
|
126
|
-
surface: "admin",
|
|
127
|
-
operations: ["list"],
|
|
128
|
-
permissionForOperation: () => ({ require: "authenticated" })
|
|
129
|
-
}));
|
|
130
|
-
const list = actions.find((entry) => entry.id.split(".").at(-1) === "list");
|
|
131
|
-
|
|
132
|
-
for (const limit of [500, "250", 100]) {
|
|
133
|
-
const result = await list.execute({ limit }, {});
|
|
134
|
-
assert.equal(result.value.data.length, 100, `limit ${limit}`);
|
|
135
|
-
assert.equal(result.value.meta.pagination.pageSize, 100, `limit ${limit}`);
|
|
136
|
-
assert.equal(result.value.meta.pagination.hasMore, true, `limit ${limit}`);
|
|
137
|
-
assert.equal(typeof result.value.meta.pagination.cursor.next, "string");
|
|
138
|
-
}
|
|
139
|
-
assert.equal((await list.execute({ limit: 20 }, {})).value.data.length, 20);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
// json-rest-api 2.4.0 honours pageSizeLimit, so 250 records are one page; an earlier one reads three.
|
|
143
|
-
test("queryEveryDocument reads 250 records and their authors in one page", async (t) => {
|
|
144
|
-
const { knex, repository } = await createFixture(t);
|
|
145
|
-
let statements = 0;
|
|
146
|
-
const count = () => {
|
|
147
|
-
statements++;
|
|
148
|
-
};
|
|
149
|
-
knex.on("query", count);
|
|
150
|
-
try {
|
|
151
|
-
const document = await repository.queryEveryDocument({ include: ["author"] });
|
|
152
|
-
assert.equal(document.data.length, 250);
|
|
153
|
-
assert.equal(document.included.length, 30);
|
|
154
|
-
} finally {
|
|
155
|
-
knex.removeListener("query", count);
|
|
156
|
-
}
|
|
157
|
-
assert.ok(statements <= 3, `${statements} statements for 250 records with their authors`);
|
|
158
|
-
});
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
import knexLib from "knex";
|
|
4
|
-
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
5
|
-
import {
|
|
6
|
-
createJsonRestApiHost,
|
|
7
|
-
createJsonRestResourceScopeOptions
|
|
8
|
-
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
9
|
-
import { defineCrudResource } from "@jskit-ai/resource-crud-core/shared/crudResource";
|
|
10
|
-
import { createCrudJsonApiRepository } from "../src/server/jsonApiModule/repository.js";
|
|
11
|
-
import { createCrudJsonApiService } from "../src/server/jsonApiModule/service.js";
|
|
12
|
-
|
|
13
|
-
// The PostgreSQL case runs in a disposable schema of the test database: run with DB_NAME set to
|
|
14
|
-
// TEST_DB_NAME (and the usual DB_HOST, DB_PORT, DB_USER and DB_PASSWORD) and DB_CLIENT unset or pg.
|
|
15
|
-
const TEST_DB_NAME = process.env.TEST_DB_NAME?.trim();
|
|
16
|
-
const ON_POSTGRES_TEST_DATABASE = Boolean(TEST_DB_NAME) && process.env.DB_NAME === TEST_DB_NAME &&
|
|
17
|
-
(!process.env.DB_CLIENT || process.env.DB_CLIENT === "pg");
|
|
18
|
-
|
|
19
|
-
const counters = defineCrudResource({
|
|
20
|
-
namespace: "counters",
|
|
21
|
-
tableName: "counters",
|
|
22
|
-
schema: {
|
|
23
|
-
name: { type: "string", required: true },
|
|
24
|
-
hits: { type: "number", required: true }
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
async function createRepository(knex) {
|
|
29
|
-
await knex.schema.createTable("counters", (table) => {
|
|
30
|
-
table.increments("id");
|
|
31
|
-
table.string("name").notNullable();
|
|
32
|
-
table.integer("hits").notNullable();
|
|
33
|
-
});
|
|
34
|
-
await knex("counters").insert({ name: "Drier", hits: 0 });
|
|
35
|
-
const api = await createJsonRestApiHost({ knex, logger: silentLogger });
|
|
36
|
-
await api.addResource("counters", createJsonRestResourceScopeOptions(counters));
|
|
37
|
-
return createCrudJsonApiRepository({ api, resource: counters, resourceScopeName: "counters" });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
test("getDocumentById forUpdate needs the caller's transaction and refuses anything but a boolean", async (t) => {
|
|
41
|
-
const knex = knexLib({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
|
|
42
|
-
t.after(() => knex.destroy());
|
|
43
|
-
const repository = await createRepository(knex);
|
|
44
|
-
const service = createCrudJsonApiService({ repository });
|
|
45
|
-
|
|
46
|
-
await assert.rejects(
|
|
47
|
-
() => repository.getDocumentById(1, {}, { forUpdate: true }),
|
|
48
|
-
(error) => error instanceof TypeError && error.message.includes("forUpdate") && error.message.includes("options.trx")
|
|
49
|
-
);
|
|
50
|
-
await assert.rejects(() => service.getDocumentById(1, {}, { forUpdate: true }), TypeError);
|
|
51
|
-
for (const forUpdate of ["yes", 1]) {
|
|
52
|
-
await assert.rejects(
|
|
53
|
-
() => repository.getDocumentById(1, {}, { forUpdate }),
|
|
54
|
-
(error) => error instanceof TypeError && error.message.includes("must be true or false"),
|
|
55
|
-
String(forUpdate)
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const plain = await repository.getDocumentById(1);
|
|
60
|
-
await repository.withTransaction(async (trx) => {
|
|
61
|
-
assert.deepEqual(await repository.getDocumentById(1, {}, { trx, forUpdate: true }), plain);
|
|
62
|
-
assert.equal(await repository.getDocumentById(99, {}, { trx, forUpdate: true }), null);
|
|
63
|
-
assert.deepEqual((await service.getDocumentById(1, {}, { trx, forUpdate: true })).value, plain);
|
|
64
|
-
});
|
|
65
|
-
assert.deepEqual(await repository.getDocumentById(1, {}, { forUpdate: false }), plain);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// Two transactions each read the counter forUpdate, then write hits + 1. The second one's read
|
|
69
|
-
// waits on the first one's lock, so it reads the committed 1 and writes 2: no update is lost.
|
|
70
|
-
test("a forUpdate read waits for another transaction's lock on the record (PostgreSQL)", {
|
|
71
|
-
skip: ON_POSTGRES_TEST_DATABASE ? false : "not on a PostgreSQL test database (DB_NAME=$TEST_DB_NAME)"
|
|
72
|
-
}, async (t) => {
|
|
73
|
-
const schema = `crud_row_lock_test_${process.pid}_${Date.now()}`;
|
|
74
|
-
const connection = {
|
|
75
|
-
host: process.env.DB_HOST,
|
|
76
|
-
port: Number(process.env.DB_PORT) || 5432,
|
|
77
|
-
user: process.env.DB_USER,
|
|
78
|
-
password: process.env.DB_PASSWORD,
|
|
79
|
-
database: TEST_DB_NAME
|
|
80
|
-
};
|
|
81
|
-
const admin = knexLib({ client: "pg", connection });
|
|
82
|
-
await admin.raw("create schema ??", [schema]);
|
|
83
|
-
const knex = knexLib({ client: "pg", connection, searchPath: [schema], pool: { min: 0, max: 4 } });
|
|
84
|
-
t.after(async () => {
|
|
85
|
-
await knex.destroy();
|
|
86
|
-
await admin.raw("drop schema ?? cascade", [schema]);
|
|
87
|
-
await admin.destroy();
|
|
88
|
-
});
|
|
89
|
-
const repository = await createRepository(knex);
|
|
90
|
-
const hitsOf = (document) => document.data.attributes.hits;
|
|
91
|
-
|
|
92
|
-
const events = [];
|
|
93
|
-
let releaseFirst;
|
|
94
|
-
const firstHoldsLock = new Promise((resolve) => {
|
|
95
|
-
releaseFirst = resolve;
|
|
96
|
-
});
|
|
97
|
-
let firstLocked;
|
|
98
|
-
const firstLockTaken = new Promise((resolve) => {
|
|
99
|
-
firstLocked = resolve;
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
const first = repository.withTransaction(async (trx) => {
|
|
103
|
-
const document = await repository.getDocumentById(1, {}, { trx, forUpdate: true });
|
|
104
|
-
events.push(`first read ${hitsOf(document)}`);
|
|
105
|
-
firstLocked();
|
|
106
|
-
await firstHoldsLock;
|
|
107
|
-
await repository.patchDocumentById(1, { hits: hitsOf(document) + 1 }, { trx, returning: "none" });
|
|
108
|
-
events.push("first commits");
|
|
109
|
-
});
|
|
110
|
-
await firstLockTaken;
|
|
111
|
-
|
|
112
|
-
const second = repository.withTransaction(async (trx) => {
|
|
113
|
-
const document = await repository.getDocumentById(1, {}, { trx, forUpdate: true });
|
|
114
|
-
events.push(`second read ${hitsOf(document)}`);
|
|
115
|
-
await repository.patchDocumentById(1, { hits: hitsOf(document) + 1 }, { trx, returning: "none" });
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
// The second transaction is blocked on a row lock before the first one writes.
|
|
119
|
-
let waiting = 0;
|
|
120
|
-
for (let attempt = 0; attempt < 200 && waiting < 1; attempt++) {
|
|
121
|
-
const { rows } = await admin.raw(
|
|
122
|
-
"select count(*)::int as waiting from pg_stat_activity where datname = current_database() " +
|
|
123
|
-
"and wait_event_type = 'Lock' and query ilike '%from \"counters\"%for update%'"
|
|
124
|
-
);
|
|
125
|
-
waiting = rows[0].waiting;
|
|
126
|
-
if (waiting < 1) {
|
|
127
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
assert.ok(waiting >= 1, "the second forUpdate read is waiting on the lock");
|
|
131
|
-
assert.deepEqual(events, ["first read 0"]);
|
|
132
|
-
|
|
133
|
-
releaseFirst();
|
|
134
|
-
await Promise.all([first, second]);
|
|
135
|
-
assert.deepEqual(events, ["first read 0", "first commits", "second read 1"]);
|
|
136
|
-
assert.equal(hitsOf(await repository.getDocumentById(1)), 2);
|
|
137
|
-
});
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { readFile } from "node:fs/promises";
|
|
3
|
-
import test from "node:test";
|
|
4
|
-
import { bookResource } from "../patterns/json-api-resource-package/example/packages/books/src/shared/bookResource.js";
|
|
5
|
-
|
|
6
|
-
const providerPath = new URL(
|
|
7
|
-
"../patterns/json-api-resource-package/example/packages/books/src/server/BooksFeature.js",
|
|
8
|
-
import.meta.url
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
test("JSON API resource package pattern leaves repetition in the framework", async () => {
|
|
12
|
-
const providerSource = await readFile(providerPath, "utf8");
|
|
13
|
-
|
|
14
|
-
assert.equal(bookResource.namespace, "books");
|
|
15
|
-
assert.equal(bookResource.tableName, "books");
|
|
16
|
-
assert.equal(bookResource.autofilter, "user");
|
|
17
|
-
assert.match(providerSource, /defineCrudJsonApiFeature/u);
|
|
18
|
-
assert.doesNotMatch(providerSource, /app\.singleton|app\.service|router\.register|createRepository/u);
|
|
19
|
-
assert.doesNotMatch(providerSource, /generator|scaffold|receipt|provenance/u);
|
|
20
|
-
});
|
package/test/lookups.test.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import {
|
|
4
|
-
resolveCrudLookupToken,
|
|
5
|
-
createCrudLookup
|
|
6
|
-
} from "../src/server/lookups.js";
|
|
7
|
-
|
|
8
|
-
test("resolveCrudLookupToken normalizes namespace to lookup token", () => {
|
|
9
|
-
assert.equal(resolveCrudLookupToken("vets"), "lookup.vets");
|
|
10
|
-
assert.equal(resolveCrudLookupToken("vets/clinics/"), "lookup.vets.clinics");
|
|
11
|
-
assert.equal(resolveCrudLookupToken("contact-categories"), "lookup.contact_categories");
|
|
12
|
-
assert.equal(resolveCrudLookupToken("customer-categories/line-items"), "lookup.customer_categories.line_items");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("resolveCrudLookupToken throws for empty namespace", () => {
|
|
16
|
-
assert.throws(
|
|
17
|
-
() => resolveCrudLookupToken(""),
|
|
18
|
-
/requires relation\.namespace/
|
|
19
|
-
);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("createCrudLookup wraps repository.listByIds and preserves include when provided", async () => {
|
|
23
|
-
const calls = [];
|
|
24
|
-
const lookup = createCrudLookup({
|
|
25
|
-
ownershipFilter: "workspace",
|
|
26
|
-
async listByIds(ids = [], options = {}) {
|
|
27
|
-
calls.push({
|
|
28
|
-
ids,
|
|
29
|
-
options
|
|
30
|
-
});
|
|
31
|
-
return [{ id: 1 }];
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const result = await lookup.listByIds([1, 2], {
|
|
36
|
-
include: "*",
|
|
37
|
-
limit: 10
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
assert.deepEqual(result, [{ id: 1 }]);
|
|
41
|
-
assert.equal(lookup.ownershipFilter, "workspace");
|
|
42
|
-
assert.deepEqual(calls[0], {
|
|
43
|
-
ids: [1, 2],
|
|
44
|
-
options: {
|
|
45
|
-
include: "*",
|
|
46
|
-
limit: 10
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("createCrudLookup defaults include=none when include is not provided", async () => {
|
|
52
|
-
const calls = [];
|
|
53
|
-
const lookup = createCrudLookup({
|
|
54
|
-
async listByIds(ids = [], options = {}) {
|
|
55
|
-
calls.push({
|
|
56
|
-
ids,
|
|
57
|
-
options
|
|
58
|
-
});
|
|
59
|
-
return [{ id: 1 }];
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
await lookup.listByIds([1, 2], {
|
|
64
|
-
limit: 10
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
assert.deepEqual(calls[0], {
|
|
68
|
-
ids: [1, 2],
|
|
69
|
-
options: {
|
|
70
|
-
include: "none",
|
|
71
|
-
limit: 10
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
assert.equal(lookup.ownershipFilter, null);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test("createCrudLookup accepts ownershipFilter option override", () => {
|
|
78
|
-
const lookup = createCrudLookup(
|
|
79
|
-
{
|
|
80
|
-
ownershipFilter: "workspace",
|
|
81
|
-
async listByIds() {
|
|
82
|
-
return [];
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
ownershipFilter: "public"
|
|
87
|
-
}
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
assert.equal(lookup.ownershipFilter, "public");
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test("createCrudLookup validates ownershipFilter token", () => {
|
|
94
|
-
assert.throws(
|
|
95
|
-
() =>
|
|
96
|
-
createCrudLookup({
|
|
97
|
-
ownershipFilter: "workspace-only",
|
|
98
|
-
async listByIds() {
|
|
99
|
-
return [];
|
|
100
|
-
}
|
|
101
|
-
}),
|
|
102
|
-
/must be one of/
|
|
103
|
-
);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test("createCrudLookup validates repository contract", () => {
|
|
107
|
-
assert.throws(
|
|
108
|
-
() => createCrudLookup({}),
|
|
109
|
-
/requires repository\.listByIds/
|
|
110
|
-
);
|
|
111
|
-
});
|