@ultimat3/entity 0.0.1 → 1.0.0
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/LICENSE +21 -0
- package/README.md +126 -40
- package/package.json +4 -3
- package/src/column.d.ts +28 -0
- package/src/column.d.ts.map +1 -0
- package/src/column.js +75 -0
- package/src/column.js.map +1 -0
- package/src/column.ts +134 -0
- package/src/columns.d.ts +39 -0
- package/src/columns.d.ts.map +1 -0
- package/src/columns.js +136 -0
- package/src/columns.js.map +1 -0
- package/src/columns.ts +164 -217
- package/src/cursor.ts +187 -0
- package/src/database.d.ts +21 -0
- package/src/database.d.ts.map +1 -0
- package/src/database.js +38 -0
- package/src/database.js.map +1 -0
- package/src/database.ts +62 -0
- package/src/describe.d.ts +16 -0
- package/src/describe.d.ts.map +1 -0
- package/src/describe.js +79 -0
- package/src/describe.js.map +1 -0
- package/src/describe.ts +106 -0
- package/src/entity.d.ts +58 -0
- package/src/entity.d.ts.map +1 -0
- package/src/entity.js +160 -0
- package/src/entity.js.map +1 -0
- package/src/entity.ts +246 -99
- package/src/errors.d.ts +18 -0
- package/src/errors.d.ts.map +1 -0
- package/src/errors.js +59 -0
- package/src/errors.js.map +1 -0
- package/src/errors.ts +27 -6
- package/src/expr.d.ts +41 -0
- package/src/expr.d.ts.map +1 -0
- package/src/expr.js +94 -0
- package/src/expr.js.map +1 -0
- package/src/expr.ts +231 -0
- package/src/index.d.ts +23 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +12 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +36 -20
- package/src/invariants.d.ts +36 -0
- package/src/invariants.d.ts.map +1 -0
- package/src/invariants.js +53 -0
- package/src/invariants.js.map +1 -0
- package/src/invariants.ts +53 -49
- package/src/pg-driver.ts +154 -0
- package/src/pg-row.ts +110 -0
- package/src/pg-sql.ts +162 -0
- package/src/plan.ts +82 -0
- package/src/query.d.ts +30 -0
- package/src/query.d.ts.map +1 -0
- package/src/query.js +74 -0
- package/src/query.js.map +1 -0
- package/src/query.ts +144 -0
- package/src/registry.d.ts +45 -0
- package/src/registry.d.ts.map +1 -0
- package/src/registry.js +26 -0
- package/src/registry.js.map +1 -0
- package/src/registry.ts +8 -5
- package/src/repo.d.ts +54 -0
- package/src/repo.d.ts.map +1 -0
- package/src/repo.js +203 -0
- package/src/repo.js.map +1 -0
- package/src/repo.ts +0 -0
- package/src/seed.d.ts +20 -0
- package/src/seed.d.ts.map +1 -0
- package/src/seed.js +43 -0
- package/src/seed.js.map +1 -0
- package/src/seed.ts +69 -0
- package/src/tenancy.d.ts +41 -0
- package/src/tenancy.d.ts.map +1 -0
- package/src/tenancy.js +57 -0
- package/src/tenancy.js.map +1 -0
- package/src/tenancy.ts +68 -19
- package/src/types.d.ts +99 -0
- package/src/types.d.ts.map +1 -0
- package/src/types.js +8 -0
- package/src/types.js.map +1 -0
- package/src/types.ts +94 -44
- package/src/view.ts +97 -0
package/src/repo.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// The repository seam. Two rules are structural rather than advisory:
|
|
2
|
+
//
|
|
3
|
+
// 1. `tx` is an explicit parameter on every write, so the transactional outbox can join the
|
|
4
|
+
// request's transaction instead of opening its own and losing atomicity.
|
|
5
|
+
// 2. Pagination is cursor-only. OFFSET is wrong under concurrent writes: a row inserted or
|
|
6
|
+
// deleted before the offset shifts every later page, so a client paging through a live
|
|
7
|
+
// table silently skips and repeats rows. A keyset cursor is stable because it names a
|
|
8
|
+
// position in the sort order, not a row count.
|
|
9
|
+
import { invariantViolated, notFound } from './errors';
|
|
10
|
+
import { assertScoped } from './tenancy';
|
|
11
|
+
export const encodeCursor = (key, id) => btoa(JSON.stringify({ k: key, id }));
|
|
12
|
+
export const decodeCursor = (cursor) => {
|
|
13
|
+
try {
|
|
14
|
+
const parsed = JSON.parse(atob(cursor));
|
|
15
|
+
return typeof parsed.k === 'string' && typeof parsed.id === 'string'
|
|
16
|
+
? { key: parsed.k, id: parsed.id }
|
|
17
|
+
: null;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const field = (row, property) => typeof row === 'object' && row !== null ? row[property] : undefined;
|
|
24
|
+
const matches = (row, predicate) => {
|
|
25
|
+
const actual = field(row, predicate.column);
|
|
26
|
+
switch (predicate.op) {
|
|
27
|
+
case 'eq':
|
|
28
|
+
return actual === predicate.value;
|
|
29
|
+
case 'neq':
|
|
30
|
+
return actual !== predicate.value;
|
|
31
|
+
case 'in':
|
|
32
|
+
return Array.isArray(predicate.value) && predicate.value.includes(actual);
|
|
33
|
+
case 'gt':
|
|
34
|
+
return compare(actual, predicate.value) > 0;
|
|
35
|
+
case 'gte':
|
|
36
|
+
return compare(actual, predicate.value) >= 0;
|
|
37
|
+
case 'lt':
|
|
38
|
+
return compare(actual, predicate.value) < 0;
|
|
39
|
+
case 'lte':
|
|
40
|
+
return compare(actual, predicate.value) <= 0;
|
|
41
|
+
case 'like':
|
|
42
|
+
return String(actual).includes(String(predicate.value).replaceAll('%', ''));
|
|
43
|
+
case 'is-null':
|
|
44
|
+
return actual === null || actual === undefined;
|
|
45
|
+
case 'is-not-null':
|
|
46
|
+
return actual !== null && actual !== undefined;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const compare = (left, right) => {
|
|
50
|
+
if (left instanceof Date && right instanceof Date)
|
|
51
|
+
return left.getTime() - right.getTime();
|
|
52
|
+
if (typeof left === 'number' && typeof right === 'number')
|
|
53
|
+
return left - right;
|
|
54
|
+
if (typeof left === 'bigint' && typeof right === 'bigint') {
|
|
55
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
56
|
+
}
|
|
57
|
+
const [a, b] = [String(left), String(right)];
|
|
58
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
59
|
+
};
|
|
60
|
+
const sortKey = (row, plan) => plan.orderBy
|
|
61
|
+
.map((entry) => {
|
|
62
|
+
const value = field(row, entry.column);
|
|
63
|
+
return value instanceof Date ? value.toISOString() : String(value);
|
|
64
|
+
})
|
|
65
|
+
.join(' ');
|
|
66
|
+
/**
|
|
67
|
+
* The default driver: correct semantics, no database. `x dev` uses it before the first
|
|
68
|
+
* migration and tests use it everywhere. Postgres is the production driver and implements
|
|
69
|
+
* this same interface.
|
|
70
|
+
*/
|
|
71
|
+
export const memoryRepo = (entity, seed = []) => {
|
|
72
|
+
const keyOf = (row) => entity.$primaryKey.map((property) => String(field(row, property))).join('');
|
|
73
|
+
const rows = new Map(seed.map((row) => [keyOf(row), row]));
|
|
74
|
+
const singleKey = (operation) => {
|
|
75
|
+
const [only] = entity.$primaryKey;
|
|
76
|
+
if (entity.$primaryKey.length !== 1 || only === undefined) {
|
|
77
|
+
throw invariantViolated(entity.$name, operation, `${entity.$name} has a composite primary key (${entity.$primaryKey.join(', ')}) — ` +
|
|
78
|
+
'use findMany({ where }) instead of an id');
|
|
79
|
+
}
|
|
80
|
+
return only;
|
|
81
|
+
};
|
|
82
|
+
const planFor = (args) => {
|
|
83
|
+
const scoped = args.orgId === undefined || entity.$tenantColumn === null
|
|
84
|
+
? []
|
|
85
|
+
: [{ column: entity.$tenantColumn, op: 'eq', value: args.orgId }];
|
|
86
|
+
const ordered = args.orderBy ?? [];
|
|
87
|
+
return {
|
|
88
|
+
entity: entity.$name,
|
|
89
|
+
where: [...(args.where ?? []), ...scoped],
|
|
90
|
+
// The primary key is always the final sort key: a cursor needs a total order, or two
|
|
91
|
+
// rows with the same sort value straddle a page boundary.
|
|
92
|
+
orderBy: [
|
|
93
|
+
...ordered,
|
|
94
|
+
...entity.$primaryKey
|
|
95
|
+
.filter((property) => !ordered.some((entry) => entry.column === property))
|
|
96
|
+
.map((property) => ({ column: property, direction: 'asc' })),
|
|
97
|
+
],
|
|
98
|
+
limit: args.limit ?? 50,
|
|
99
|
+
...(args.cursor === undefined || args.cursor === null ? {} : { cursor: args.cursor }),
|
|
100
|
+
...(args.select === undefined ? {} : { select: args.select }),
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
const select = (args, operation) => {
|
|
104
|
+
const plan = planFor(args);
|
|
105
|
+
assertScoped(entity.$name, entity.$tenantColumn, operation, plan);
|
|
106
|
+
const visible = (row) => !entity.$softDelete ||
|
|
107
|
+
args.includeDeleted === true ||
|
|
108
|
+
field(row, 'deletedAt') === null ||
|
|
109
|
+
field(row, 'deletedAt') === undefined;
|
|
110
|
+
const found = [...rows.values()]
|
|
111
|
+
.filter((row) => plan.where.every((predicate) => matches(row, predicate)))
|
|
112
|
+
.filter(visible)
|
|
113
|
+
.sort((left, right) => {
|
|
114
|
+
for (const entry of plan.orderBy) {
|
|
115
|
+
const order = compare(field(left, entry.column), field(right, entry.column));
|
|
116
|
+
if (order !== 0)
|
|
117
|
+
return entry.direction === 'desc' ? -order : order;
|
|
118
|
+
}
|
|
119
|
+
return 0;
|
|
120
|
+
});
|
|
121
|
+
return { plan, found };
|
|
122
|
+
};
|
|
123
|
+
const write = (row, options) => {
|
|
124
|
+
entity.$assert(row);
|
|
125
|
+
const key = keyOf(row);
|
|
126
|
+
const previous = rows.get(key);
|
|
127
|
+
options?.tx?.onRollback(() => {
|
|
128
|
+
if (previous === undefined)
|
|
129
|
+
rows.delete(key);
|
|
130
|
+
else
|
|
131
|
+
rows.set(key, previous);
|
|
132
|
+
});
|
|
133
|
+
rows.set(key, row);
|
|
134
|
+
return row;
|
|
135
|
+
};
|
|
136
|
+
const byId = (id) => {
|
|
137
|
+
const current = rows.get(id);
|
|
138
|
+
if (current === undefined)
|
|
139
|
+
throw notFound(entity.$name, id);
|
|
140
|
+
return current;
|
|
141
|
+
};
|
|
142
|
+
// Every method is async: a repository call that fails must reject, never throw
|
|
143
|
+
// synchronously, or half the call sites would need two error paths.
|
|
144
|
+
return {
|
|
145
|
+
async findById(id, options) {
|
|
146
|
+
const property = singleKey('findById');
|
|
147
|
+
const { found } = select({ ...options, where: [{ column: property, op: 'eq', value: id }] }, 'findById');
|
|
148
|
+
return found[0] ?? null;
|
|
149
|
+
},
|
|
150
|
+
async findMany(args = {}) {
|
|
151
|
+
const { plan, found } = select(args, 'findMany');
|
|
152
|
+
const decoded = plan.cursor === undefined ? null : decodeCursor(plan.cursor);
|
|
153
|
+
const start = decoded === null ? 0 : found.findIndex((row) => keyOf(row) === decoded.id) + 1;
|
|
154
|
+
const page = found.slice(start, start + plan.limit);
|
|
155
|
+
const last = page.at(-1);
|
|
156
|
+
const more = start + page.length < found.length;
|
|
157
|
+
return {
|
|
158
|
+
rows: page,
|
|
159
|
+
nextCursor: more && last !== undefined ? encodeCursor(sortKey(last, plan), keyOf(last)) : null,
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
async insert(values, options) {
|
|
163
|
+
return write(values, options);
|
|
164
|
+
},
|
|
165
|
+
async update(id, patch, options) {
|
|
166
|
+
singleKey('update');
|
|
167
|
+
return write(Object.assign({}, byId(id), patch), options);
|
|
168
|
+
},
|
|
169
|
+
async delete(id, options) {
|
|
170
|
+
singleKey('delete');
|
|
171
|
+
const current = byId(id);
|
|
172
|
+
// Soft delete hides the row without losing it; the column's presence is the switch.
|
|
173
|
+
if (entity.$softDelete) {
|
|
174
|
+
write(Object.assign({}, current, { deletedAt: new Date() }), options);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const key = keyOf(current);
|
|
178
|
+
options?.tx?.onRollback(() => rows.set(key, current));
|
|
179
|
+
rows.delete(key);
|
|
180
|
+
},
|
|
181
|
+
async count(args = {}) {
|
|
182
|
+
return select(args, 'count').found.length;
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
let txCounter = 0;
|
|
187
|
+
/** In-memory transactor: undo closures registered by drivers run on failure. */
|
|
188
|
+
export const memoryTransactor = () => ({
|
|
189
|
+
async run(work) {
|
|
190
|
+
const undos = [];
|
|
191
|
+
txCounter += 1;
|
|
192
|
+
const tx = { id: `tx-${txCounter}`, onRollback: (undo) => undos.push(undo) };
|
|
193
|
+
try {
|
|
194
|
+
return await work(tx);
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
for (const undo of undos.reverse())
|
|
198
|
+
undo();
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
//# sourceMappingURL=repo.js.map
|
package/src/repo.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repo.js","sourceRoot":"","sources":["repo.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,EAAE;AACF,6FAA6F;AAC7F,6EAA6E;AAC7E,4FAA4F;AAC5F,2FAA2F;AAC3F,0FAA0F;AAC1F,mDAAmD;AAGnD,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AA8CzC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAAW,EAAE,EAAU,EAAU,EAAE,CAC9D,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;AAEvC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAc,EAAsC,EAAE;IACjF,IAAI,CAAC;QACH,MAAM,MAAM,GAAkC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;QACvE,OAAO,OAAO,MAAM,CAAC,CAAC,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,EAAE,KAAK,QAAQ;YAClE,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE;YAClC,CAAC,CAAC,IAAI,CAAC;IACX,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,KAAK,GAAG,CAAC,GAAY,EA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C,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF,CAAC,CAAC"}
|
package/src/repo.ts
CHANGED
|
Binary file
|
package/src/seed.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Driver } from './database';
|
|
2
|
+
import type { EntityCore } from './entity';
|
|
3
|
+
import type { ColumnMap, Insertable } from './types';
|
|
4
|
+
/** RFC 4122 v5: SHA-1 of namespace + name, with the version and variant bits pinned. */
|
|
5
|
+
export declare const seedId: (label: string) => string;
|
|
6
|
+
export interface SeedContext {
|
|
7
|
+
insert<Row, C extends ColumnMap>(entity: EntityCore<Row, C>, rows: readonly Insertable<C>[]): Promise<void>;
|
|
8
|
+
/** Deterministic id for a label. Same label, same uuid, every run. */
|
|
9
|
+
id(label: string): string;
|
|
10
|
+
}
|
|
11
|
+
export interface SeedOptions {
|
|
12
|
+
/** Defaults to a fresh in-memory driver, so a seed runs with no database at all. */
|
|
13
|
+
readonly driver?: Driver;
|
|
14
|
+
}
|
|
15
|
+
export interface Seed {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
run(options?: SeedOptions): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
export declare const defineSeed: (name: string, build: (context: SeedContext) => Promise<void>) => Seed;
|
|
20
|
+
//# sourceMappingURL=seed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["seed.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAQrD,wFAAwF;AACxF,eAAO,MAAM,MAAM,UAAW,MAAM,KAAG,MAiBtC,CAAC;AAEF,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,GAAG,EAAE,CAAC,SAAS,SAAS,EAC7B,MAAM,EAAE,UAAU,CAAC,GAAG,EAAE,CAAC,CAAC,EAC1B,IAAI,EAAE,SAAS,UAAU,CAAC,CAAC,CAAC,EAAE,GAC7B,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,sEAAsE;IACtE,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,WAAW;IAC1B,oFAAoF;IACpF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,IAAI;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,GAAG,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3C;AAED,eAAO,MAAM,UAAU,SAAU,MAAM,SAAS,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,KAAG,IAYxF,CAAC"}
|
package/src/seed.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// A seed is the fixture graph, written once and replayed anywhere. `id('post:tenancy')` is a
|
|
2
|
+
// UUID v5 of the label, so the same row gets the same id on every machine and a bug reproduced
|
|
3
|
+
// locally reproduces in CI. Rows go through `entity.$parse` and the invariants, which makes a
|
|
4
|
+
// seed a test of the schema as well as data for one.
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import { memoryDriver } from './database';
|
|
7
|
+
/** Framework namespace for seed labels. Fixed forever: changing it moves every seeded id. */
|
|
8
|
+
const NAMESPACE = 'a3c1f0d6-5c2b-4a3e-9f1b-6d4e7c8a9b02';
|
|
9
|
+
const bytesOf = (uuid) => Uint8Array.from((uuid.replaceAll('-', '').match(/../g) ?? []).map((pair) => parseInt(pair, 16)));
|
|
10
|
+
/** RFC 4122 v5: SHA-1 of namespace + name, with the version and variant bits pinned. */
|
|
11
|
+
export const seedId = (label) => {
|
|
12
|
+
const name = new TextEncoder().encode(label);
|
|
13
|
+
const input = new Uint8Array(16 + name.length);
|
|
14
|
+
input.set(bytesOf(NAMESPACE));
|
|
15
|
+
input.set(name, 16);
|
|
16
|
+
const digest = new Uint8Array(createHash('sha1').update(input).digest());
|
|
17
|
+
const bytes = digest.slice(0, 16);
|
|
18
|
+
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x50;
|
|
19
|
+
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80;
|
|
20
|
+
const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
21
|
+
return [
|
|
22
|
+
hex.slice(0, 8),
|
|
23
|
+
hex.slice(8, 12),
|
|
24
|
+
hex.slice(12, 16),
|
|
25
|
+
hex.slice(16, 20),
|
|
26
|
+
hex.slice(20, 32),
|
|
27
|
+
].join('-');
|
|
28
|
+
};
|
|
29
|
+
export const defineSeed = (name, build) => ({
|
|
30
|
+
name,
|
|
31
|
+
run: async (options = {}) => {
|
|
32
|
+
const driver = options.driver ?? memoryDriver();
|
|
33
|
+
await build({
|
|
34
|
+
insert: async (entity, rows) => {
|
|
35
|
+
const repo = driver.repo(entity);
|
|
36
|
+
for (const row of rows)
|
|
37
|
+
await repo.insert(entity.$parse(row));
|
|
38
|
+
},
|
|
39
|
+
id: seedId,
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
//# sourceMappingURL=seed.js.map
|
package/src/seed.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed.js","sourceRoot":"","sources":["seed.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,+FAA+F;AAC/F,8FAA8F;AAC9F,qDAAqD;AAErD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAI1C,6FAA6F;AAC7F,MAAM,SAAS,GAAG,sCAAsC,CAAC;AAEzD,MAAM,OAAO,GAAG,CAAC,IAAY,EAAc,EAAE,C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AG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAClF,OAAO;QACL,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;QACf,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC;QAChB,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;QACjB,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;QACjB,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;KAClB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACd,CAAC,CAAC;AAqBF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,KAA8C,EAAQ,EAAE,CAAC,CAAC;IACjG,IAAI;IACJ,GAAG,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,EAAE,EAAE;QAC1B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,YAAY,EAAE,CAAC;QAChD,MAAM,KAAK,CAAC;YACV,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;gBAC7B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBACjC,KAAK,MAAM,GAAG,IAAI,IAAI;oBAAE,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;YAChE,CAAC;YACD,EAAE,EAAE,MAAM;SACX,CAAC,CAAC;IACL,CAAC;CACF,CAAC,CAAC"}
|
package/src/seed.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// A seed is the fixture graph, written once and replayed anywhere. `id('post:tenancy')` is a
|
|
2
|
+
// UUID v5 of the label, so the same row gets the same id on every machine and a bug reproduced
|
|
3
|
+
// locally reproduces in CI. Rows go through `entity.$parse` and the invariants, which makes a
|
|
4
|
+
// seed a test of the schema as well as data for one.
|
|
5
|
+
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import type { Driver } from './database';
|
|
8
|
+
import { memoryDriver } from './database';
|
|
9
|
+
import type { EntityCore } from './entity';
|
|
10
|
+
import type { ColumnMap, Insertable } from './types';
|
|
11
|
+
|
|
12
|
+
/** Framework namespace for seed labels. Fixed forever: changing it moves every seeded id. */
|
|
13
|
+
const NAMESPACE = 'a3c1f0d6-5c2b-4a3e-9f1b-6d4e7c8a9b02';
|
|
14
|
+
|
|
15
|
+
const bytesOf = (uuid: string): Uint8Array =>
|
|
16
|
+
Uint8Array.from((uuid.replaceAll('-', '').match(/../g) ?? []).map((pair) => parseInt(pair, 16)));
|
|
17
|
+
|
|
18
|
+
/** RFC 4122 v5: SHA-1 of namespace + name, with the version and variant bits pinned. */
|
|
19
|
+
export const seedId = (label: string): string => {
|
|
20
|
+
const name = new TextEncoder().encode(label);
|
|
21
|
+
const input = new Uint8Array(16 + name.length);
|
|
22
|
+
input.set(bytesOf(NAMESPACE));
|
|
23
|
+
input.set(name, 16);
|
|
24
|
+
const digest = new Uint8Array(createHash('sha1').update(input).digest());
|
|
25
|
+
const bytes = digest.slice(0, 16);
|
|
26
|
+
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x50;
|
|
27
|
+
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80;
|
|
28
|
+
const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
29
|
+
return [
|
|
30
|
+
hex.slice(0, 8),
|
|
31
|
+
hex.slice(8, 12),
|
|
32
|
+
hex.slice(12, 16),
|
|
33
|
+
hex.slice(16, 20),
|
|
34
|
+
hex.slice(20, 32),
|
|
35
|
+
].join('-');
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export interface SeedContext {
|
|
39
|
+
insert<Row, C extends ColumnMap>(
|
|
40
|
+
entity: EntityCore<Row, C>,
|
|
41
|
+
rows: readonly Insertable<C>[],
|
|
42
|
+
): Promise<void>;
|
|
43
|
+
/** Deterministic id for a label. Same label, same uuid, every run. */
|
|
44
|
+
id(label: string): string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface SeedOptions {
|
|
48
|
+
/** Defaults to a fresh in-memory driver, so a seed runs with no database at all. */
|
|
49
|
+
readonly driver?: Driver;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface Seed {
|
|
53
|
+
readonly name: string;
|
|
54
|
+
run(options?: SeedOptions): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const defineSeed = (name: string, build: (context: SeedContext) => Promise<void>): Seed => ({
|
|
58
|
+
name,
|
|
59
|
+
run: async (options = {}) => {
|
|
60
|
+
const driver = options.driver ?? memoryDriver();
|
|
61
|
+
await build({
|
|
62
|
+
insert: async (entity, rows) => {
|
|
63
|
+
const repo = driver.repo(entity);
|
|
64
|
+
for (const row of rows) await repo.insert(entity.$parse(row));
|
|
65
|
+
},
|
|
66
|
+
id: seedId,
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
});
|
package/src/tenancy.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ColumnMap } from './types';
|
|
2
|
+
export type Operator = 'eq' | 'neq' | 'in' | 'gt' | 'gte' | 'lt' | 'lte' | 'like' | 'is-null' | 'is-not-null';
|
|
3
|
+
export interface Predicate {
|
|
4
|
+
readonly column: string;
|
|
5
|
+
readonly op: Operator;
|
|
6
|
+
readonly value?: unknown;
|
|
7
|
+
}
|
|
8
|
+
export type SortDirection = 'asc' | 'desc';
|
|
9
|
+
export interface SortKey {
|
|
10
|
+
readonly column: string;
|
|
11
|
+
readonly direction: SortDirection;
|
|
12
|
+
}
|
|
13
|
+
export interface QueryPlan {
|
|
14
|
+
readonly entity: string;
|
|
15
|
+
readonly where: readonly Predicate[];
|
|
16
|
+
readonly orderBy: readonly SortKey[];
|
|
17
|
+
readonly limit: number;
|
|
18
|
+
/** Keyset position. There is no `offset` and there will not be one — see `repo.ts`. */
|
|
19
|
+
readonly cursor?: string;
|
|
20
|
+
readonly select?: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
/** The property key a tenant column takes when it is not marked explicitly. */
|
|
23
|
+
export declare const ORG_COLUMN = "orgId";
|
|
24
|
+
/**
|
|
25
|
+
* `.tenant()` is the switch; a column literally named `orgId` counts too, so an entity cannot
|
|
26
|
+
* become unscoped by forgetting one call.
|
|
27
|
+
*/
|
|
28
|
+
export declare const tenantColumnOf: (columns: ColumnMap) => string | null;
|
|
29
|
+
export declare const isOrgScoped: (columns: ColumnMap) => boolean;
|
|
30
|
+
export declare const emptyPlan: (entity: string, limit?: number) => QueryPlan;
|
|
31
|
+
export declare const hasOrgPredicate: (plan: QueryPlan, column?: string) => boolean;
|
|
32
|
+
/** Adds the org predicate exactly once; calling it twice is not an error. */
|
|
33
|
+
export declare const orgScoped: (plan: QueryPlan, orgId: string, column?: string) => QueryPlan;
|
|
34
|
+
/**
|
|
35
|
+
* Called by every repository operation. Runtime here, and a build-time check in `x verify`
|
|
36
|
+
* that no query for a tenant-scoped entity is constructed without it.
|
|
37
|
+
*/
|
|
38
|
+
export declare const assertScoped: (entityName: string, tenantColumn: string | null, operation: string, plan: QueryPlan) => void;
|
|
39
|
+
/** Debug and `x db explain` rendering. Values stay out: a plan is safe to log. */
|
|
40
|
+
export declare const describePlan: (plan: QueryPlan) => string;
|
|
41
|
+
//# sourceMappingURL=tenancy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenancy.d.ts","sourceRoot":"","sources":["tenancy.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,MAAM,MAAM,QAAQ,GAChB,IAAI,GACJ,KAAK,GACL,IAAI,GACJ,IAAI,GACJ,KAAK,GACL,IAAI,GACJ,KAAK,GACL,MAAM,GACN,SAAS,GACT,aAAa,CAAC;AAElB,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC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AC"}
|
package/src/tenancy.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Multi-tenancy is a guard, not a convention. An entity with a tenant column can only be read
|
|
2
|
+
// through a plan that carries an org predicate; building one without it throws
|
|
3
|
+
// `X_TENANCY_UNSCOPED` at the seam instead of leaking another tenant's rows.
|
|
4
|
+
import { tenancyUnscoped } from './errors';
|
|
5
|
+
/** The property key a tenant column takes when it is not marked explicitly. */
|
|
6
|
+
export const ORG_COLUMN = 'orgId';
|
|
7
|
+
/**
|
|
8
|
+
* `.tenant()` is the switch; a column literally named `orgId` counts too, so an entity cannot
|
|
9
|
+
* become unscoped by forgetting one call.
|
|
10
|
+
*/
|
|
11
|
+
export const tenantColumnOf = (columns) => {
|
|
12
|
+
for (const [property, column] of Object.entries(columns)) {
|
|
13
|
+
if (column.$meta.tenant)
|
|
14
|
+
return property;
|
|
15
|
+
}
|
|
16
|
+
return Object.hasOwn(columns, ORG_COLUMN) ? ORG_COLUMN : null;
|
|
17
|
+
};
|
|
18
|
+
export const isOrgScoped = (columns) => tenantColumnOf(columns) !== null;
|
|
19
|
+
export const emptyPlan = (entity, limit = 50) => ({
|
|
20
|
+
entity,
|
|
21
|
+
where: [],
|
|
22
|
+
orderBy: [],
|
|
23
|
+
limit,
|
|
24
|
+
});
|
|
25
|
+
export const hasOrgPredicate = (plan, column = ORG_COLUMN) => plan.where.some((predicate) => predicate.column === column);
|
|
26
|
+
/** Adds the org predicate exactly once; calling it twice is not an error. */
|
|
27
|
+
export const orgScoped = (plan, orgId, column = ORG_COLUMN) => hasOrgPredicate(plan, column)
|
|
28
|
+
? plan
|
|
29
|
+
: { ...plan, where: [...plan.where, { column, op: 'eq', value: orgId }] };
|
|
30
|
+
/**
|
|
31
|
+
* Called by every repository operation. Runtime here, and a build-time check in `x verify`
|
|
32
|
+
* that no query for a tenant-scoped entity is constructed without it.
|
|
33
|
+
*/
|
|
34
|
+
export const assertScoped = (entityName, tenantColumn, operation, plan) => {
|
|
35
|
+
if (tenantColumn === null)
|
|
36
|
+
return;
|
|
37
|
+
if (hasOrgPredicate(plan, tenantColumn))
|
|
38
|
+
return;
|
|
39
|
+
throw tenancyUnscoped(entityName, operation);
|
|
40
|
+
};
|
|
41
|
+
/** Debug and `x db explain` rendering. Values stay out: a plan is safe to log. */
|
|
42
|
+
export const describePlan = (plan) => {
|
|
43
|
+
const where = plan.where
|
|
44
|
+
.map((predicate) => `${predicate.column} ${predicate.op} ?`)
|
|
45
|
+
.join(' and ');
|
|
46
|
+
const order = plan.orderBy.map((entry) => `${entry.column} ${entry.direction}`).join(', ');
|
|
47
|
+
return [
|
|
48
|
+
`from ${plan.entity}`,
|
|
49
|
+
where === '' ? null : `where ${where}`,
|
|
50
|
+
order === '' ? null : `order by ${order}`,
|
|
51
|
+
`limit ${plan.limit}`,
|
|
52
|
+
plan.cursor === undefined ? null : 'after cursor',
|
|
53
|
+
]
|
|
54
|
+
.filter((part) => part !== null)
|
|
55
|
+
.join(' ');
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=tenancy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenancy.js","sourceRoot":"","sources":["tenancy.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,+EAA+E;AAC/E,6EAA6E;AAE7E,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAsC3C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,UAAU,GAAG,OAAO,CAAC;AAElC;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,OAAkB,EAAiB,EAAE;IAClE,KAAK,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACzD,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM;YAAE,OAAO,QAAQ,CAAC;IAC3C,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,UAAU,CA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,CAAC,MAAM,YAAY,GAAG,CAAC,IAAe,EAAU,EAAE;IACtD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK;SACrB,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,IAAI,SAAS,CAAC,EAAE,IAAI,CAAC;SAC3D,IAAI,CAAC,OAAO,CAAC,CAAC;IACjB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3F,OAAO;QACL,QAAQ,IAAI,CAAC,MAAM,EAAE;QACrB,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,KAAK,EAAE;QACtC,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY,KAAK,EAAE;QACzC,SAAS,IAAI,CAAC,KAAK,EAAE;QACrB,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc;KAClD;SACE,MAAM,CAAC,CAAC,IAAI,EAAkB,EAAE,CAAC,IAAI,KAAK,IAAI,CAAC;SAC/C,IAAI,CAAC,GAAG,CAAC,CAAC;AACf,CAAC,CAAC"}
|
package/src/tenancy.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// Multi-tenancy is a guard, not a convention. An entity with
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import
|
|
1
|
+
// Multi-tenancy is a guard, not a convention. An entity with a tenant column can only be read
|
|
2
|
+
// through a plan that carries an org predicate; building one without it throws
|
|
3
|
+
// `X_TENANCY_UNSCOPED` at the seam instead of leaking another tenant's rows.
|
|
4
|
+
|
|
5
|
+
import { EntityError, tenancyUnscoped } from './errors';
|
|
6
|
+
import type { ColumnMap } from './types';
|
|
6
7
|
|
|
7
8
|
export type Operator =
|
|
8
9
|
| 'eq'
|
|
@@ -24,18 +25,62 @@ export interface Predicate {
|
|
|
24
25
|
|
|
25
26
|
export type SortDirection = 'asc' | 'desc';
|
|
26
27
|
|
|
28
|
+
export interface SortKey {
|
|
29
|
+
readonly column: string;
|
|
30
|
+
readonly direction: SortDirection;
|
|
31
|
+
}
|
|
32
|
+
|
|
27
33
|
export interface QueryPlan {
|
|
28
34
|
readonly entity: string;
|
|
29
35
|
readonly where: readonly Predicate[];
|
|
30
|
-
readonly orderBy: readonly
|
|
36
|
+
readonly orderBy: readonly SortKey[];
|
|
31
37
|
readonly limit: number;
|
|
38
|
+
/** Keyset position. There is no `offset` and there will not be one — see `repo.ts`. */
|
|
32
39
|
readonly cursor?: string;
|
|
40
|
+
readonly select?: readonly string[];
|
|
33
41
|
}
|
|
34
42
|
|
|
43
|
+
/** The property key a tenant column takes when it is not marked explicitly. */
|
|
35
44
|
export const ORG_COLUMN = 'orgId';
|
|
36
45
|
|
|
37
|
-
/**
|
|
38
|
-
|
|
46
|
+
/**
|
|
47
|
+
* `.tenant()` is the switch; a column literally named `orgId` counts too, so an entity cannot
|
|
48
|
+
* become unscoped by forgetting one call.
|
|
49
|
+
*/
|
|
50
|
+
export const tenantColumnOf = (columns: ColumnMap): string | null => {
|
|
51
|
+
for (const [property, column] of Object.entries(columns)) {
|
|
52
|
+
if (column.$meta.tenant) return property;
|
|
53
|
+
}
|
|
54
|
+
return Object.hasOwn(columns, ORG_COLUMN) ? ORG_COLUMN : null;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const isOrgScoped = (columns: ColumnMap): boolean => tenantColumnOf(columns) !== null;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `entity(name, { tenant: 'workspaceId' })` wins over inference — a tenant column need not be
|
|
61
|
+
* called `orgId` and need not carry `.tenant()`. Omitting it keeps the inference, so an entity
|
|
62
|
+
* cannot become unscoped by forgetting the key; naming a column that does not exist is a
|
|
63
|
+
* declaration error, because the alternative is a silently unscoped table.
|
|
64
|
+
*/
|
|
65
|
+
export const resolveTenantColumn = (
|
|
66
|
+
entityName: string,
|
|
67
|
+
columns: ColumnMap,
|
|
68
|
+
declared: string | undefined,
|
|
69
|
+
): string | null => {
|
|
70
|
+
if (declared === undefined) return tenantColumnOf(columns);
|
|
71
|
+
if (!Object.hasOwn(columns, declared)) {
|
|
72
|
+
const available = Object.keys(columns).join(', ');
|
|
73
|
+
// Not `invariantViolated`: its fix points at `x entity explain`, which describes invariants
|
|
74
|
+
// the author never wrote. What repairs this is one edit to the declaration, so the error
|
|
75
|
+
// carries that edit and both ways out of it.
|
|
76
|
+
throw new EntityError({
|
|
77
|
+
code: 'X_INVARIANT_VIOLATED',
|
|
78
|
+
cause: `${entityName}.tenant: tenant: '${declared}' names no column — pick from: ${available}`,
|
|
79
|
+
fix: `set tenant to one of ${available} in entity('${entityName}'), or remove the tenant key — inference then takes the .tenant() column, else one named ${ORG_COLUMN}`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return declared;
|
|
83
|
+
};
|
|
39
84
|
|
|
40
85
|
export const emptyPlan = (entity: string, limit = 50): QueryPlan => ({
|
|
41
86
|
entity,
|
|
@@ -44,31 +89,35 @@ export const emptyPlan = (entity: string, limit = 50): QueryPlan => ({
|
|
|
44
89
|
limit,
|
|
45
90
|
});
|
|
46
91
|
|
|
47
|
-
export const hasOrgPredicate = (plan: QueryPlan): boolean =>
|
|
48
|
-
plan.where.some((predicate) => predicate.column ===
|
|
92
|
+
export const hasOrgPredicate = (plan: QueryPlan, column: string = ORG_COLUMN): boolean =>
|
|
93
|
+
plan.where.some((predicate) => predicate.column === column);
|
|
49
94
|
|
|
50
95
|
/** Adds the org predicate exactly once; calling it twice is not an error. */
|
|
51
|
-
export const orgScoped = (
|
|
52
|
-
|
|
96
|
+
export const orgScoped = (
|
|
97
|
+
plan: QueryPlan,
|
|
98
|
+
orgId: string,
|
|
99
|
+
column: string = ORG_COLUMN,
|
|
100
|
+
): QueryPlan =>
|
|
101
|
+
hasOrgPredicate(plan, column)
|
|
53
102
|
? plan
|
|
54
|
-
: { ...plan, where: [...plan.where, { column
|
|
103
|
+
: { ...plan, where: [...plan.where, { column, op: 'eq', value: orgId }] };
|
|
55
104
|
|
|
56
105
|
/**
|
|
57
|
-
* Called by every repository operation. Runtime here, and a build-time check in
|
|
58
|
-
*
|
|
106
|
+
* Called by every repository operation. Runtime here, and a build-time check in `x verify`
|
|
107
|
+
* that no query for a tenant-scoped entity is constructed without it.
|
|
59
108
|
*/
|
|
60
109
|
export const assertScoped = (
|
|
61
110
|
entityName: string,
|
|
62
|
-
|
|
111
|
+
tenantColumn: string | null,
|
|
63
112
|
operation: string,
|
|
64
113
|
plan: QueryPlan,
|
|
65
114
|
): void => {
|
|
66
|
-
if (
|
|
67
|
-
if (hasOrgPredicate(plan)) return;
|
|
115
|
+
if (tenantColumn === null) return;
|
|
116
|
+
if (hasOrgPredicate(plan, tenantColumn)) return;
|
|
68
117
|
throw tenancyUnscoped(entityName, operation);
|
|
69
118
|
};
|
|
70
119
|
|
|
71
|
-
/** Debug
|
|
120
|
+
/** Debug and `x db explain` rendering. Values stay out: a plan is safe to log. */
|
|
72
121
|
export const describePlan = (plan: QueryPlan): string => {
|
|
73
122
|
const where = plan.where
|
|
74
123
|
.map((predicate) => `${predicate.column} ${predicate.op} ?`)
|