@forestfuture/active-record 0.8.2 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter.d.mts +2 -2
- package/dist/adapter.mjs +4 -4
- package/dist/{batch-D3nP3g83.mjs → batch-DpMoy3-7.mjs} +2566 -1706
- package/dist/cli.d.mts +4 -4
- package/dist/cli.mjs +3 -3
- package/dist/command.mjs +19 -18
- package/dist/d1.d.mts +2 -2
- package/dist/d1.mjs +9 -4
- package/dist/{for-adapters-CgEQfuyD.mjs → for-adapters-DSVRZL3n.mjs} +6 -3
- package/dist/{for-adapters-DAn1wSYz.d.mts → for-adapters-GQfwyuMl.d.mts} +1 -1
- package/dist/for-adapters-unqbZQM7.mjs +12 -0
- package/dist/{generated-model-FB7oT7V1.mjs → generated-model-CKIciUat.mjs} +1982 -1148
- package/dist/{epoch-storage-C7186hd1.mjs → identifiers-BP6aRwew.mjs} +285 -223
- package/dist/{index-BfX3Dgy_.d.mts → index-DkWlkGy2.d.mts} +139 -96
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +5 -5
- package/dist/metadata-nKZ_6DzA.d.mts +378 -0
- package/dist/{plugin-BY2QaRpq.mjs → plugin-A93qwqAh.mjs} +10 -7
- package/dist/{plugin-IHD0I6Rg.mjs → plugin-CsOOLZkx.mjs} +7 -7
- package/dist/postgresql.d.mts +1 -1
- package/dist/postgresql.mjs +28 -4
- package/dist/{src-DnuvSHF6.mjs → src-CB2AUq70.mjs} +2 -2
- package/dist/{storage-override-BFC-GSR0.mjs → storage-override-B7WTiA_U.mjs} +1 -1
- package/dist/{validations-DFLjJ203.d.mts → validations-C2-HKi1V.d.mts} +325 -118
- package/llms-full.txt +8489 -4094
- package/package.json +1 -1
- package/skills/active-record/SKILL.md +5 -3
- package/dist/for-adapters-l4rNitkK.mjs +0 -31
- package/dist/metadata-CzntpYzC.d.mts +0 -182
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as pluralize,
|
|
1
|
+
import { B as MissingAttribute, C as pluralize, D as underscore, E as capitalized, K as RecordDestroyed, L as InvalidModel, S as listedNames, V as NoDatabase, W as ReadonlyAttribute, _ as enumEntries, a as isSameCell, c as castValue, d as uncastable, h as digitsProblem, l as isInvalidDate, n as reservedWords, nt as UnknownAttribute, o as castCondition, p as writeValue, r as epochStorages, s as castKey, t as isIdentifier, u as readValue, v as enumProblem, x as isEnumValues } from "./identifiers-BP6aRwew.mjs";
|
|
2
2
|
// src/ancestors.ts
|
|
3
3
|
/** A class and every class (and object) it extends, nearest first. */
|
|
4
4
|
function ancestors(model) {
|
|
@@ -38,7 +38,7 @@ function modelNameOf(model) {
|
|
|
38
38
|
return model.modelName;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* Gives the library's base class `modelName`: each class's own name,
|
|
41
|
+
* Gives the library's base class `modelName`: each class's own name,
|
|
42
42
|
* unless it sets `static modelName = "…"`, which the classes extending it inherit (a set one is
|
|
43
43
|
* a value, found before this getter). Empty for a class with no name: anonymous, or `export
|
|
44
44
|
* default`.
|
|
@@ -53,8 +53,8 @@ function defineModelName(root) {
|
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
* The `modelName` of the class that extends `base` directly, the one a table is named after
|
|
57
|
-
*
|
|
56
|
+
* The `modelName` of the class that extends `base` directly, the one a table is named after;
|
|
57
|
+
* empty when it has none (anonymous, `export default`, or `Model.define({...})` itself).
|
|
58
58
|
*/
|
|
59
59
|
function modelNameBelow(base, model) {
|
|
60
60
|
let current = model;
|
|
@@ -62,7 +62,7 @@ function modelNameBelow(base, model) {
|
|
|
62
62
|
return current === base ? "" : modelNameOf(current);
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
|
-
* A class name's table
|
|
65
|
+
* A class name's table: the name through `underscore`, pluralized
|
|
66
66
|
* (`BlogPost` → `blog_posts`, `Person` → `people`). The generators name tables with it too.
|
|
67
67
|
*/
|
|
68
68
|
function tableize(className) {
|
|
@@ -125,7 +125,7 @@ function boundTableOf(model) {
|
|
|
125
125
|
}
|
|
126
126
|
/**
|
|
127
127
|
* Whether a Model ignores some of its table's columns, so its queries name their columns rather
|
|
128
|
-
* than `*` and an ignored column is never read
|
|
128
|
+
* than `*` and an ignored column is never read. The nearest bound table decides.
|
|
129
129
|
* Remembered per Model: its table is bound when it's first used, before any query, so the
|
|
130
130
|
* answer can't change.
|
|
131
131
|
*/
|
|
@@ -139,7 +139,7 @@ function isIgnoringColumns(model) {
|
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
141
141
|
* An abstract base class (the app's `ApplicationRecord`, or one marked `static abstract = true`)
|
|
142
|
-
* has no table
|
|
142
|
+
* has no table: it's only inherited from, so using it is an
|
|
143
143
|
* `InvalidModel`.
|
|
144
144
|
*/
|
|
145
145
|
function rejectAbstract(model) {
|
|
@@ -238,168 +238,285 @@ function listsOf(lists, model) {
|
|
|
238
238
|
lists.set(model, found);
|
|
239
239
|
return found;
|
|
240
240
|
}
|
|
241
|
-
// src/
|
|
242
|
-
/**
|
|
243
|
-
function
|
|
244
|
-
return
|
|
245
|
-
if (definition.enum === void 0) return [];
|
|
246
|
-
const before = affix(attribute, definition.prefix);
|
|
247
|
-
const after = affix(attribute, definition.suffix);
|
|
248
|
-
return enumEntries(definition.enum).map(([name]) => {
|
|
249
|
-
const stem = `${before}${methodPart(name)}${after}`;
|
|
250
|
-
const records = [
|
|
251
|
-
`is${stem}`,
|
|
252
|
-
`saveAs${stem}`,
|
|
253
|
-
`saveAs${stem}OrThrow`
|
|
254
|
-
];
|
|
255
|
-
const scopes = [uncapitalized(stem), `not${stem}`];
|
|
256
|
-
return {
|
|
257
|
-
attribute,
|
|
258
|
-
name,
|
|
259
|
-
records: definition.instanceMethods === false ? [] : records,
|
|
260
|
-
scopes: definition.scopes === false ? [] : scopes
|
|
261
|
-
};
|
|
262
|
-
});
|
|
263
|
-
});
|
|
241
|
+
// src/attribute-aliases.ts
|
|
242
|
+
/** Whether a declared attribute is an alias: an object with `aliasOf`. */
|
|
243
|
+
function isAlias(declared) {
|
|
244
|
+
return typeof declared === "object" && declared !== null && "aliasOf" in declared;
|
|
264
245
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
246
|
+
/**
|
|
247
|
+
* The aliases a Model's declared attributes hold, by alias, each to the attribute it names;
|
|
248
|
+
* `prefix` goes before an alias's name in an error (`"Post."`, or nothing for `define`).
|
|
249
|
+
*/
|
|
250
|
+
function aliasesIn(prefix, declared, attributes) {
|
|
251
|
+
const given = Object.entries(declared).filter((entry) => isAlias(entry[1]));
|
|
252
|
+
const aliases = new Map(given.map(([name, { aliasOf }]) => [name, aliasOf]));
|
|
253
|
+
for (const [name, alias] of given) {
|
|
254
|
+
const problem = aliasProblem(name, alias, aliases, attributes);
|
|
255
|
+
if (problem !== void 0) throw new InvalidModel(`${prefix}${name}${problem}`);
|
|
256
|
+
}
|
|
257
|
+
return aliases;
|
|
268
258
|
}
|
|
269
|
-
/**
|
|
270
|
-
|
|
271
|
-
|
|
259
|
+
/**
|
|
260
|
+
* What's wrong with an alias, after its name: another key, a name that's an attribute already,
|
|
261
|
+
* an alias of an alias, or of a name that isn't an attribute.
|
|
262
|
+
*/
|
|
263
|
+
function aliasProblem(name, alias, aliases, attributes) {
|
|
264
|
+
const { aliasOf } = alias;
|
|
265
|
+
if (Object.keys(alias).length !== 1 || typeof aliasOf !== "string") return ": an alias is { aliasOf: \"attribute\" }, and nothing else";
|
|
266
|
+
if (attributes.has(name)) return " is an attribute already, so it can't be an alias";
|
|
267
|
+
const target = aliases.get(aliasOf);
|
|
268
|
+
if (target !== void 0) return ` is an alias of ${aliasOf}, an alias itself; name ${target}`;
|
|
269
|
+
if (!attributes.has(aliasOf)) return ` is an alias of ${aliasOf}, which isn't an attribute`;
|
|
272
270
|
}
|
|
273
|
-
/**
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
271
|
+
/**
|
|
272
|
+
* The attribute `name` names: itself, or the one it's an alias of. Every name a query, a write, a
|
|
273
|
+
* validation or a change-tracking method takes goes through it.
|
|
274
|
+
*/
|
|
275
|
+
function attributeName(model, name) {
|
|
276
|
+
return model.attributeAliases?.get(name) ?? name;
|
|
277
277
|
}
|
|
278
|
-
const capitalized = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
279
|
-
const uncapitalized = (text) => text.charAt(0).toLowerCase() + text.slice(1);
|
|
280
278
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
* changes too, as Rails' `published!` does.
|
|
279
|
+
* `values` keyed by attribute: an alias's value under the attribute it names, so a row writes
|
|
280
|
+
* each column once. Giving both an alias and its attribute is a `TypeError`.
|
|
284
281
|
*/
|
|
285
|
-
function
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
defineMethod(prototype, throwingSetter, function() {
|
|
296
|
-
return this.updateOrThrow({ [attribute]: name });
|
|
297
|
-
});
|
|
282
|
+
function withAttributeNames(model, values) {
|
|
283
|
+
const givenAs = /* @__PURE__ */ new Map();
|
|
284
|
+
for (const given of Object.keys(values)) {
|
|
285
|
+
const name = attributeName(model, given);
|
|
286
|
+
const earlier = givenAs.get(name);
|
|
287
|
+
if (earlier !== void 0) {
|
|
288
|
+
const alias = given === name ? earlier : given;
|
|
289
|
+
throw new TypeError(`${model.name}: ${alias} is an alias of ${name}, so give one of them`);
|
|
290
|
+
}
|
|
291
|
+
givenAs.set(name, given);
|
|
298
292
|
}
|
|
293
|
+
return Object.fromEntries([...givenAs].map(([name, given]) => [name, values[given]]));
|
|
299
294
|
}
|
|
295
|
+
// src/password-algorithm.ts
|
|
300
296
|
/**
|
|
301
|
-
*
|
|
302
|
-
*
|
|
297
|
+
* The iterations a new digest takes: the most Workers allow in production, as each request's CPU
|
|
298
|
+
* time can't stop PBKDF2 once it's started.
|
|
303
299
|
*/
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
300
|
+
const ITERATIONS = 1e5;
|
|
301
|
+
const SALT_BYTES = 16;
|
|
302
|
+
const KEY_BITS = 256;
|
|
303
|
+
const PREFIX = "$pbkdf2-sha256$";
|
|
304
|
+
/** `$pbkdf2-sha256$i=<iterations>$<salt>$<key>`, each part in base64 without padding. */
|
|
305
|
+
const DIGEST = /^\$pbkdf2-sha256\$i=(\d{1,9})\$([A-Za-z0-9+/]+)\$([A-Za-z0-9+/]+)$/;
|
|
306
|
+
/**
|
|
307
|
+
* The default algorithm: PBKDF2 with HMAC-SHA256, a 16-byte salt, and the iterations its digest
|
|
308
|
+
* names, so a digest made with another count still verifies. At most 72 bytes, as bcrypt's.
|
|
309
|
+
*/
|
|
310
|
+
const pbkdf2Sha256 = {
|
|
311
|
+
maxBytes: 72,
|
|
312
|
+
async hash(password) {
|
|
313
|
+
const salt = crypto.getRandomValues(new Uint8Array(SALT_BYTES));
|
|
314
|
+
const key = await derive(password, salt, ITERATIONS);
|
|
315
|
+
return `${PREFIX}i=${ITERATIONS}$${toBase64(salt)}$${toBase64(key)}`;
|
|
316
|
+
},
|
|
317
|
+
async verify(password, digest) {
|
|
318
|
+
const { iterations, salt, key } = parsed(digest);
|
|
319
|
+
return isSameBytes(await derive(password, salt, iterations), key);
|
|
315
320
|
}
|
|
316
|
-
}
|
|
317
|
-
/** Puts `method` on `target` as `name`, writable as a declared method is, and names it unless told not to. */
|
|
318
|
-
function defineMethod(target, name, method, { named = true } = {}) {
|
|
319
|
-
if (Object.hasOwn(target, name)) return;
|
|
320
|
-
if (named) Object.defineProperty(method, "name", { value: name });
|
|
321
|
-
Object.defineProperty(target, name, {
|
|
322
|
-
value: method,
|
|
323
|
-
writable: true,
|
|
324
|
-
configurable: true
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
/** The scope statics `defineEnumStatics` made, so a static a Model declares itself is told apart. */
|
|
328
|
-
const enumScopes = /* @__PURE__ */ new WeakSet();
|
|
321
|
+
};
|
|
329
322
|
/**
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
* the Model declares itself. Rails refuses the same when an enum is declared.
|
|
323
|
+
* The fewest and most iterations a digest may name: a digest is data, so one naming a count no
|
|
324
|
+
* digest this algorithm made could have would weaken a check, or stall it.
|
|
333
325
|
*/
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
326
|
+
const FEWEST_ITERATIONS = 1e3;
|
|
327
|
+
const MOST_ITERATIONS = 1e7;
|
|
328
|
+
/** A digest's parts; `TypeError` for one this algorithm couldn't have made. */
|
|
329
|
+
function parsed(digest) {
|
|
330
|
+
const [, count = "", salt = "", key = ""] = DIGEST.exec(digest) ?? [];
|
|
331
|
+
const iterations = Number(count);
|
|
332
|
+
const decoded = {
|
|
333
|
+
iterations,
|
|
334
|
+
salt: fromBase64(salt),
|
|
335
|
+
key: fromBase64(key)
|
|
338
336
|
};
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
337
|
+
if (!(iterations >= FEWEST_ITERATIONS && iterations <= MOST_ITERATIONS && decoded.salt.length === SALT_BYTES && decoded.key.length === KEY_BITS / 8)) throw new TypeError(`The password digest isn't one PBKDF2-SHA256 made (${PREFIX}…); give the algorithm that made it as securePassword's algorithm`);
|
|
338
|
+
return decoded;
|
|
339
|
+
}
|
|
340
|
+
/** PBKDF2-SHA256's key for `password`'s UTF-8 bytes. */
|
|
341
|
+
async function derive(password, salt, iterations) {
|
|
342
|
+
const material = await crypto.subtle.importKey("raw", new TextEncoder().encode(password), "PBKDF2", false, ["deriveBits"]);
|
|
343
|
+
const bits = await crypto.subtle.deriveBits({
|
|
344
|
+
name: "PBKDF2",
|
|
345
|
+
hash: "SHA-256",
|
|
346
|
+
salt,
|
|
347
|
+
iterations
|
|
348
|
+
}, material, KEY_BITS);
|
|
349
|
+
return new Uint8Array(bits);
|
|
350
|
+
}
|
|
351
|
+
/** Whether two keys are the same, in a time that doesn't depend on where they differ. */
|
|
352
|
+
function isSameBytes(left, right) {
|
|
353
|
+
if (left.length !== right.length) return false;
|
|
354
|
+
let difference = 0;
|
|
355
|
+
for (let index = 0; index < left.length; index += 1) difference |= left[index] ^ right[index];
|
|
356
|
+
return difference === 0;
|
|
357
|
+
}
|
|
358
|
+
function toBase64(bytes) {
|
|
359
|
+
return btoa(String.fromCharCode(...bytes)).replaceAll("=", "");
|
|
360
|
+
}
|
|
361
|
+
/** Base64's bytes; none for text that isn't base64, which a digest's check then refuses. */
|
|
362
|
+
function fromBase64(text) {
|
|
363
|
+
try {
|
|
364
|
+
return Uint8Array.from(atob(text), (character) => character.charCodeAt(0));
|
|
365
|
+
} catch {
|
|
366
|
+
return /* @__PURE__ */ new Uint8Array();
|
|
353
367
|
}
|
|
354
368
|
}
|
|
369
|
+
// src/custom-types.ts
|
|
370
|
+
/** Every type `defineType` made, so a spec can't pass off another object as one. */
|
|
371
|
+
const defined = /* @__PURE__ */ new WeakSet();
|
|
355
372
|
/**
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
373
|
+
* Defines an attribute type, used in a spec as a type name is: `price: money`, or
|
|
374
|
+
* `{ type: money, null: false }`. Its values are kept as `storedAs` keeps its own, so they're
|
|
375
|
+
* read strictly, compared and ordered as that type's are.
|
|
359
376
|
*/
|
|
360
|
-
function
|
|
361
|
-
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
return reserved === void 0 ? void 0 : `${model.name}'s enum ${attribute} gives a scope named "${reserved}", which is a reserved word; ${fix}`;
|
|
377
|
+
function defineType(type) {
|
|
378
|
+
requireWellFormed(type);
|
|
379
|
+
const made = Object.freeze({
|
|
380
|
+
storedAs: type.storedAs,
|
|
381
|
+
cast: (input) => type.cast(input),
|
|
382
|
+
serialize: (value) => type.serialize(value),
|
|
383
|
+
deserialize: (stored) => type.deserialize(stored)
|
|
384
|
+
});
|
|
385
|
+
defined.add(made);
|
|
386
|
+
return made;
|
|
371
387
|
}
|
|
372
|
-
/**
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
388
|
+
/** The functions a type needs, each checked when it's made. */
|
|
389
|
+
const functionNames = [
|
|
390
|
+
"cast",
|
|
391
|
+
"serialize",
|
|
392
|
+
"deserialize"
|
|
393
|
+
];
|
|
394
|
+
/**
|
|
395
|
+
* Throws `TypeError` for a type that couldn't work, past the types (plain JavaScript, or a cast):
|
|
396
|
+
* a `storedAs` that isn't a built-in type's name, or a function it lacks.
|
|
397
|
+
*/
|
|
398
|
+
function requireWellFormed(type) {
|
|
399
|
+
if (!isAttributeType(type.storedAs)) throw new TypeError(`defineType: storedAs ${JSON.stringify(type.storedAs)} isn't a type's name; use ${attributeTypeNames}`);
|
|
400
|
+
const missing = functionNames.find((name) => typeof type[name] !== "function");
|
|
401
|
+
if (missing !== void 0) throw new TypeError(`defineType: ${missing} isn't a function`);
|
|
377
402
|
}
|
|
378
|
-
/** `
|
|
379
|
-
function
|
|
380
|
-
|
|
381
|
-
return fix === void 0 ? "" : `, or leave ${attribute}'s methods off (remove ${fix})`;
|
|
403
|
+
/** Whether `value` is a type `defineType` made. */
|
|
404
|
+
function isCustomType(value) {
|
|
405
|
+
return typeof value === "object" && value !== null && defined.has(value);
|
|
382
406
|
}
|
|
383
|
-
|
|
384
|
-
const
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
407
|
+
// src/durations.ts
|
|
408
|
+
const SECONDS = {
|
|
409
|
+
seconds: 1,
|
|
410
|
+
minutes: 60,
|
|
411
|
+
hours: 3600,
|
|
412
|
+
days: 86400
|
|
413
|
+
};
|
|
414
|
+
/** The longest a token may last: 10,000 years, well inside what a Date can hold from now. */
|
|
415
|
+
const MOST_SECONDS = 365e4 * SECONDS.days;
|
|
416
|
+
/**
|
|
417
|
+
* Whether `value` is a duration: one or more units, each a number that isn't negative, lasting
|
|
418
|
+
* at most 10,000 years in all.
|
|
419
|
+
*/
|
|
420
|
+
function isDuration(value) {
|
|
421
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
422
|
+
const entries = Object.entries(value).filter(([, each]) => each !== void 0);
|
|
423
|
+
const isEach = entries.every(([unit, each]) => Object.hasOwn(SECONDS, unit) && typeof each === "number" && each >= 0);
|
|
424
|
+
return entries.length > 0 && isEach && totalSeconds(entries) <= MOST_SECONDS;
|
|
425
|
+
}
|
|
426
|
+
function totalSeconds(entries) {
|
|
427
|
+
return entries.reduce((total, [unit, value]) => total + SECONDS[unit] * value, 0);
|
|
428
|
+
}
|
|
429
|
+
/** The seconds a duration lasts; `TypeError`, after `where`, for one that isn't a duration. */
|
|
430
|
+
function secondsOf(duration, where) {
|
|
431
|
+
if (!isDuration(duration)) throw new TypeError(`${where} takes a duration such as { minutes: 15 }: seconds, minutes, hours or days, none negative, at most 10,000 years`);
|
|
432
|
+
return totalSeconds(Object.entries(duration).filter(([, value]) => value !== void 0));
|
|
433
|
+
}
|
|
434
|
+
/** When a token made now, lasting `duration`, expires. */
|
|
435
|
+
function expiryAfter(duration, where) {
|
|
436
|
+
return new Date(Date.now() + secondsOf(duration, where) * 1e3);
|
|
437
|
+
}
|
|
438
|
+
// src/secure-tokens.ts
|
|
439
|
+
/** The fewest characters a secure token has. */
|
|
440
|
+
const MINIMUM_TOKEN_LENGTH = 24;
|
|
441
|
+
/** Base58: digits and letters, without `0`, `O`, `I` and `l`, which read alike. */
|
|
442
|
+
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
443
|
+
/** The bytes below it map onto the alphabet evenly; the rest are drawn again. */
|
|
444
|
+
const EVEN_BYTES = 232;
|
|
445
|
+
/**
|
|
446
|
+
* A random base58 token of `length` characters (24 unless given), from `crypto.getRandomValues`,
|
|
447
|
+
* each character equally likely. Nothing checks it's unique: a unique index does that.
|
|
448
|
+
*/
|
|
449
|
+
function generateSecureToken(length = MINIMUM_TOKEN_LENGTH) {
|
|
450
|
+
if (!Number.isSafeInteger(length) || length < 1) throw new RangeError(`generateSecureToken needs a whole number of characters, not ${length}`);
|
|
451
|
+
let token = "";
|
|
452
|
+
while (token.length < length) for (const byte of crypto.getRandomValues(new Uint8Array(length))) if (byte < EVEN_BYTES && token.length < length) token += ALPHABET[byte % 58];
|
|
453
|
+
return token;
|
|
454
|
+
}
|
|
455
|
+
/** Whether an attribute's `secureToken` is well formed: `true`, `false`, or its options. */
|
|
456
|
+
function isSecureTokenOption(value) {
|
|
457
|
+
if (typeof value === "boolean") return true;
|
|
458
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
459
|
+
return Object.entries(value).every(([key, each]) => isTokenOption(key, each));
|
|
460
|
+
}
|
|
461
|
+
function isTokenOption(key, value) {
|
|
462
|
+
if (value === void 0) return true;
|
|
463
|
+
if (key === "length") return Number.isSafeInteger(value);
|
|
464
|
+
if (key === "prefix") return value === true || typeof value === "string" && value !== "";
|
|
465
|
+
return key === "on" && (value === "initialize" || value === "create");
|
|
466
|
+
}
|
|
467
|
+
/** What's wrong with a well-formed `secureToken`, after the attribute's name: too short a length. */
|
|
468
|
+
function secureTokenProblem(value) {
|
|
469
|
+
const length = value?.length;
|
|
470
|
+
return length !== void 0 && length < MINIMUM_TOKEN_LENGTH ? ` has a secureToken of length ${length}; a secure token is at least ${MINIMUM_TOKEN_LENGTH} characters` : void 0;
|
|
471
|
+
}
|
|
472
|
+
/** An attribute's secure token options, if it has one. */
|
|
473
|
+
function tokenOptionsOf(definition) {
|
|
474
|
+
const { secureToken } = definition;
|
|
475
|
+
if (secureToken === void 0) return void 0;
|
|
476
|
+
return secureToken === true ? {} : secureToken;
|
|
477
|
+
}
|
|
478
|
+
/** Whether an attribute's token is generated as a record is built: its `on` isn't `"create"`. */
|
|
479
|
+
function isTokenOnInitialize(definition) {
|
|
480
|
+
const options = tokenOptionsOf(definition);
|
|
481
|
+
return options !== void 0 && options.on !== "create";
|
|
482
|
+
}
|
|
483
|
+
/** A new token for an attribute declared with `secureToken`: its prefix, then its characters. */
|
|
484
|
+
function tokenFor(definition) {
|
|
485
|
+
const { length = MINIMUM_TOKEN_LENGTH, prefix } = tokenOptionsOf(definition) ?? {};
|
|
486
|
+
return `${prefix === true ? `${definition.column}_` : prefix ?? ""}${generateSecureToken(length)}`;
|
|
389
487
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
488
|
+
/**
|
|
489
|
+
* Generates each token a new record doesn't have (null or `""`) as it's built (`"initialize"`,
|
|
490
|
+
* after its given values) or created (`"create"`), whichever its `on` says.
|
|
491
|
+
*/
|
|
492
|
+
function generateTokens(definitions, holder, moment) {
|
|
493
|
+
for (const [name, definition] of definitions) {
|
|
494
|
+
const options = tokenOptionsOf(definition);
|
|
495
|
+
if (options === void 0 || (options.on ?? "initialize") !== moment) continue;
|
|
496
|
+
const value = holder.read(name);
|
|
497
|
+
if (value === null || value === "") holder.write(name, tokenFor(definition));
|
|
394
498
|
}
|
|
395
499
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
if (
|
|
500
|
+
/**
|
|
501
|
+
* Defines each secure token's methods on a Model's prototype: `regenerateAuthToken()`, which
|
|
502
|
+
* saves a new token as `update` would, and its `OrThrow`.
|
|
503
|
+
*/
|
|
504
|
+
function defineSecureTokenMethods(prototype, definitions) {
|
|
505
|
+
for (const [name, definition] of definitions) {
|
|
506
|
+
if (definition.secureToken === void 0) continue;
|
|
507
|
+
const methods = {
|
|
508
|
+
[`regenerate${capitalized(name)}`]: function() {
|
|
509
|
+
return this.update({ [name]: tokenFor(definition) });
|
|
510
|
+
},
|
|
511
|
+
[`regenerate${capitalized(name)}OrThrow`]: function() {
|
|
512
|
+
return this.updateOrThrow({ [name]: tokenFor(definition) });
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
for (const [method, value] of Object.entries(methods)) Object.defineProperty(prototype, method, {
|
|
516
|
+
value,
|
|
517
|
+
configurable: true,
|
|
518
|
+
writable: true
|
|
519
|
+
});
|
|
403
520
|
}
|
|
404
521
|
}
|
|
405
522
|
// src/spec-keys.ts
|
|
@@ -489,8 +606,85 @@ const rules = {
|
|
|
489
606
|
isGiven: isGivenUnless(false),
|
|
490
607
|
requires: "enum",
|
|
491
608
|
onRetype: "drop"
|
|
609
|
+
},
|
|
610
|
+
virtual: {
|
|
611
|
+
isWellFormed: (value) => typeof value === "boolean",
|
|
612
|
+
describedAs: "virtual",
|
|
613
|
+
isGiven: isGivenUnless(false),
|
|
614
|
+
conflictsWith: [
|
|
615
|
+
"column",
|
|
616
|
+
"generateId",
|
|
617
|
+
"storage",
|
|
618
|
+
"enum"
|
|
619
|
+
]
|
|
620
|
+
},
|
|
621
|
+
securePassword: {
|
|
622
|
+
isWellFormed: isSecurePasswordOption,
|
|
623
|
+
describedAs: "securePassword",
|
|
624
|
+
wellFormedAs: "use true, or { algorithm?, validations? }",
|
|
625
|
+
onlyFor: {
|
|
626
|
+
types: ["string"],
|
|
627
|
+
because: "a password is a string"
|
|
628
|
+
},
|
|
629
|
+
requires: "virtual",
|
|
630
|
+
conflictsWith: ["default"]
|
|
631
|
+
},
|
|
632
|
+
secureToken: secureTokenRule(),
|
|
633
|
+
readonly: {
|
|
634
|
+
isWellFormed: (value) => typeof value === "boolean",
|
|
635
|
+
describedAs: "readonly",
|
|
636
|
+
isGiven: isGivenUnless(false),
|
|
637
|
+
conflictsWith: [
|
|
638
|
+
"virtual",
|
|
639
|
+
"securePassword",
|
|
640
|
+
"secureToken"
|
|
641
|
+
]
|
|
492
642
|
}
|
|
493
643
|
};
|
|
644
|
+
/** The rule for `secureToken`: a string's random token, generated for a new record (#355). */
|
|
645
|
+
function secureTokenRule() {
|
|
646
|
+
return {
|
|
647
|
+
isWellFormed: isSecureTokenOption,
|
|
648
|
+
describedAs: "secureToken",
|
|
649
|
+
wellFormedAs: "use true, or { length?, prefix?, on?: \"initialize\" | \"create\" }",
|
|
650
|
+
isGiven: isGivenUnless(false),
|
|
651
|
+
onlyFor: {
|
|
652
|
+
types: ["string"],
|
|
653
|
+
because: "a token is a string"
|
|
654
|
+
},
|
|
655
|
+
problemFor: secureTokenProblem,
|
|
656
|
+
conflictsWith: [
|
|
657
|
+
"default",
|
|
658
|
+
"generateId",
|
|
659
|
+
"virtual",
|
|
660
|
+
"enum"
|
|
661
|
+
],
|
|
662
|
+
onRetype: "drop"
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
/** `true`, or an object with only an algorithm (`hash` and `verify`) and a boolean `validations`. */
|
|
666
|
+
function isSecurePasswordOption(value) {
|
|
667
|
+
if (value === true) return true;
|
|
668
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
669
|
+
return Object.entries(value).every(([key, each]) => {
|
|
670
|
+
if (each === void 0) return true;
|
|
671
|
+
if (key === "validations") return typeof each === "boolean";
|
|
672
|
+
if (key === "resetToken") return isResetTokenOption(each);
|
|
673
|
+
return key === "algorithm" && isAlgorithm(each);
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
/** Whether a secure password's `resetToken` is well formed: a boolean, or `{ expiresIn }`. */
|
|
677
|
+
function isResetTokenOption(value) {
|
|
678
|
+
if (typeof value === "boolean") return true;
|
|
679
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
680
|
+
return Object.entries(value).every(([key, each]) => each === void 0 || key === "expiresIn" && isDuration(each));
|
|
681
|
+
}
|
|
682
|
+
/** Whether a value can hash and verify a password: an object with both functions. */
|
|
683
|
+
function isAlgorithm(value) {
|
|
684
|
+
if (typeof value !== "object" || value === null) return false;
|
|
685
|
+
const { hash, verify } = value;
|
|
686
|
+
return typeof hash === "function" && typeof verify === "function";
|
|
687
|
+
}
|
|
494
688
|
/** The rule for a decimal's precision or scale: a whole number of digits. */
|
|
495
689
|
function decimalDigits(describedAs) {
|
|
496
690
|
return {
|
|
@@ -644,1171 +838,1775 @@ function copiedKeys(spec) {
|
|
|
644
838
|
const copied = entries.filter(([key, rule]) => rule.copiedToDefinition !== false && givenValue(spec, key) !== void 0).map(([key]) => [key, spec[key]]);
|
|
645
839
|
return Object.fromEntries(copied);
|
|
646
840
|
}
|
|
647
|
-
// src/
|
|
648
|
-
/**
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
/**
|
|
687
|
-
function
|
|
688
|
-
return
|
|
841
|
+
// src/error-messages.ts
|
|
842
|
+
/** @internal The built-in English messages, where no class has its own (listed in the guide). */
|
|
843
|
+
const builtInMessages = {
|
|
844
|
+
blank: "can't be blank",
|
|
845
|
+
present: "must be blank",
|
|
846
|
+
invalid: "is invalid",
|
|
847
|
+
required: "must exist",
|
|
848
|
+
inclusion: "is not included in the list",
|
|
849
|
+
exclusion: "is reserved",
|
|
850
|
+
accepted: "must be accepted",
|
|
851
|
+
confirmation: "doesn't match %{attribute}",
|
|
852
|
+
taken: "has already been taken",
|
|
853
|
+
tooShort: {
|
|
854
|
+
one: "is too short (minimum is 1 character)",
|
|
855
|
+
other: "is too short (minimum is %{count} characters)"
|
|
856
|
+
},
|
|
857
|
+
tooLong: {
|
|
858
|
+
one: "is too long (maximum is 1 character)",
|
|
859
|
+
other: "is too long (maximum is %{count} characters)"
|
|
860
|
+
},
|
|
861
|
+
wrongLength: {
|
|
862
|
+
one: "is the wrong length (should be 1 character)",
|
|
863
|
+
other: "is the wrong length (should be %{count} characters)"
|
|
864
|
+
},
|
|
865
|
+
notANumber: "is not a number",
|
|
866
|
+
notAnInteger: "must be an integer",
|
|
867
|
+
greaterThan: "must be greater than %{count}",
|
|
868
|
+
greaterThanOrEqualTo: "must be greater than or equal to %{count}",
|
|
869
|
+
lessThan: "must be less than %{count}",
|
|
870
|
+
lessThanOrEqualTo: "must be less than or equal to %{count}",
|
|
871
|
+
equalTo: "must be equal to %{count}",
|
|
872
|
+
otherThan: "must be other than %{count}",
|
|
873
|
+
passwordTooLong: "is too long",
|
|
874
|
+
restrictDependentDestroy: {
|
|
875
|
+
hasOne: "Cannot delete record because a dependent %{record} exists",
|
|
876
|
+
hasMany: "Cannot delete record because dependent %{record} exist"
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
const plurals = new Intl.PluralRules("en");
|
|
880
|
+
/** `object[key]` when it's the object's own, so `toString` is never a message. */
|
|
881
|
+
function ownValue(object, key) {
|
|
882
|
+
return object !== void 0 && Object.hasOwn(object, key) ? object[key] : void 0;
|
|
689
883
|
}
|
|
690
|
-
/**
|
|
691
|
-
function
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
if (isCustomType(spec)) return {
|
|
697
|
-
...resolvedType(spec),
|
|
698
|
-
column: underscore(name)
|
|
884
|
+
/** One class's `errorMessages` calls as one: a later call's message wins, type by type. */
|
|
885
|
+
function mergedCalls(calls) {
|
|
886
|
+
const attributes = {};
|
|
887
|
+
for (const call of calls) for (const [name, types] of Object.entries(call.attributes ?? {})) attributes[name] = {
|
|
888
|
+
...attributes[name],
|
|
889
|
+
...types
|
|
699
890
|
};
|
|
700
|
-
return {
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
891
|
+
return Object.assign({}, ...calls, { attributes });
|
|
892
|
+
}
|
|
893
|
+
const catalogs = /* @__PURE__ */ new WeakMap();
|
|
894
|
+
/** A Model's catalog, built once it's sealed; before that, built afresh, as declarations can still come. */
|
|
895
|
+
function catalogOf(model) {
|
|
896
|
+
const kept = catalogs.get(model);
|
|
897
|
+
if (kept !== void 0) return kept;
|
|
898
|
+
const names = /* @__PURE__ */ new Map();
|
|
899
|
+
for (const { values } of declaredByClass(model, "humanAttributeNames").toReversed()) for (const value of values) for (const [attribute, name] of Object.entries(value)) names.set(attribute, name);
|
|
900
|
+
const catalog = {
|
|
901
|
+
messages: [...declaredByClass(model, "errorMessages").map(({ values }) => mergedCalls(values)), builtInMessages],
|
|
902
|
+
names
|
|
704
903
|
};
|
|
904
|
+
if (isSealed(model)) catalogs.set(model, catalog);
|
|
905
|
+
return catalog;
|
|
705
906
|
}
|
|
706
|
-
/**
|
|
707
|
-
function
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
907
|
+
/** The message for a type: for the attribute, then the class's own, nearest class first. */
|
|
908
|
+
function templateFor(model, attribute, type) {
|
|
909
|
+
if (type === "attributes") return void 0;
|
|
910
|
+
for (const messages of catalogOf(model).messages) {
|
|
911
|
+
const found = ownValue(ownValue(messages.attributes, attribute), type) ?? ownValue(messages, type);
|
|
912
|
+
if (found !== void 0) return found;
|
|
913
|
+
}
|
|
712
914
|
}
|
|
713
|
-
/**
|
|
714
|
-
function
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
return
|
|
915
|
+
/** The text of a message: the form the entry names, or else the plural form for `count`. */
|
|
916
|
+
function messageText(template, entry) {
|
|
917
|
+
if (template === void 0 || typeof template === "string") return template;
|
|
918
|
+
const { count } = entry.values;
|
|
919
|
+
return ownValue(template, entry.form ?? (typeof count === "number" ? plurals.select(count) : "other")) ?? template.other;
|
|
718
920
|
}
|
|
719
|
-
/**
|
|
720
|
-
function
|
|
721
|
-
|
|
921
|
+
/** "createdAt" → "Created at" */
|
|
922
|
+
function inWords(name) {
|
|
923
|
+
const words = underscore(name).replaceAll(/[_.]/g, " ");
|
|
924
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
722
925
|
}
|
|
723
|
-
/**
|
|
724
|
-
function
|
|
725
|
-
|
|
926
|
+
/** An attribute's name as messages show it: a class's `humanAttributeNames`, or "Created at". */
|
|
927
|
+
function humanAttributeName(model, attribute) {
|
|
928
|
+
return catalogOf(model).names.get(attribute) ?? inWords(attribute);
|
|
726
929
|
}
|
|
727
|
-
/**
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
930
|
+
/** An attribute's value as `%{value}` shows it. */
|
|
931
|
+
function shownValue$1(source, attribute) {
|
|
932
|
+
if (attribute === "base") return "";
|
|
933
|
+
const value = source.read(attribute);
|
|
934
|
+
if (value === null || value === void 0) return "";
|
|
935
|
+
if (isFiltered(source.model, attribute)) return FILTERED;
|
|
936
|
+
if (value instanceof Date) return value.toISOString();
|
|
937
|
+
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
733
938
|
}
|
|
734
|
-
/**
|
|
735
|
-
function
|
|
736
|
-
|
|
939
|
+
/** An entry's message: given, or looked up now, with its `%{name}` placeholders filled in. */
|
|
940
|
+
function messageOf(source, entry) {
|
|
941
|
+
if (entry.message !== void 0) return entry.message;
|
|
942
|
+
const { model } = source;
|
|
943
|
+
const text = messageText(templateFor(model, entry.attribute, entry.type), entry);
|
|
944
|
+
if (text === void 0) throw missingMessage(model, entry);
|
|
945
|
+
return text.replace(/%\{(\w+)\}/g, (placeholder, name) => {
|
|
946
|
+
if (name === "attribute") return String(ownValue(entry.values, name) ?? humanAttributeName(model, entry.attribute));
|
|
947
|
+
if (name === "model") return inWords(model.modelName);
|
|
948
|
+
if (name === "value") return shownValue$1(source, entry.attribute);
|
|
949
|
+
return String(ownValue(entry.values, name) ?? placeholder);
|
|
950
|
+
});
|
|
737
951
|
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
const
|
|
741
|
-
|
|
742
|
-
* Rails' default `filter_parameters` for a new app: an attribute whose snake_case name holds
|
|
743
|
-
* one of these, in any case, is filtered (`passwordDigest`, `apiKey`, `resetToken`, `email`).
|
|
744
|
-
*/
|
|
745
|
-
const defaultFilterAttributes = [
|
|
746
|
-
"passw",
|
|
747
|
-
"email",
|
|
748
|
-
"secret",
|
|
749
|
-
"token",
|
|
750
|
-
"_key",
|
|
751
|
-
"crypt",
|
|
752
|
-
"salt",
|
|
753
|
-
"certificate",
|
|
754
|
-
"otp",
|
|
755
|
-
"ssn",
|
|
756
|
-
"cvv",
|
|
757
|
-
"cvc"
|
|
758
|
-
];
|
|
759
|
-
/**
|
|
760
|
-
* Whether `attribute`'s values are hidden from listeners: its snake_case name holds a string
|
|
761
|
-
* entry lowercased, or read in snake_case (`"apiKey"` as `api_key`, #397), or a RegExp entry
|
|
762
|
-
* matches the attribute or its snake_case name.
|
|
763
|
-
*/
|
|
764
|
-
function isFiltered(model, attribute) {
|
|
765
|
-
const name = underscore(attribute);
|
|
766
|
-
return model.filterAttributes.some((entry) => typeof entry === "string" ? name.includes(entry.toLowerCase()) || name.includes(underscore(entry)) : attribute.search(entry) !== -1 || name.search(entry) !== -1);
|
|
952
|
+
/** The error for a type no class has a message for. */
|
|
953
|
+
function missingMessage(model, entry) {
|
|
954
|
+
const where = `${model.name}'s errorMessages, or a class it extends`;
|
|
955
|
+
return /* @__PURE__ */ new TypeError(`No message for the error "${entry.type}" on "${entry.attribute}": add one to ${where}, as ${entry.type} or attributes.${entry.attribute}.${entry.type}`);
|
|
767
956
|
}
|
|
768
|
-
/** The
|
|
769
|
-
function
|
|
770
|
-
return
|
|
957
|
+
/** The attributes each class names in its `errorMessages` or `humanAttributeNames`, nearest class first. */
|
|
958
|
+
function attributesNamedInMessages(model) {
|
|
959
|
+
return [...declaredByClass(model, "errorMessages").map(({ from, values }) => ({
|
|
960
|
+
from,
|
|
961
|
+
declaration: "errorMessages",
|
|
962
|
+
names: values.flatMap((value) => Object.keys(value.attributes ?? {}))
|
|
963
|
+
})), ...declaredByClass(model, "humanAttributeNames").map(({ from, values }) => ({
|
|
964
|
+
from,
|
|
965
|
+
declaration: "humanAttributeNames",
|
|
966
|
+
names: values.flatMap((value) => Object.keys(value))
|
|
967
|
+
}))];
|
|
771
968
|
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
969
|
+
// src/inspect-support.ts
|
|
970
|
+
/** Node and workerd call a value's inspector under this registered symbol. */
|
|
971
|
+
const inspectCustom = Symbol.for("nodejs.util.inspect.custom");
|
|
972
|
+
/** Text in the color of a special value, or plain when colors are off. */
|
|
973
|
+
function styled(options, text) {
|
|
974
|
+
return options.stylize ? options.stylize(text, "special") : text;
|
|
975
|
+
}
|
|
976
|
+
/** A filtered value's stand-in: prints unquoted, so it isn't taken for a string, as a special value. */
|
|
977
|
+
var FilteredMark = class {
|
|
978
|
+
#changed;
|
|
979
|
+
constructor(changed) {
|
|
980
|
+
this.#changed = changed;
|
|
981
|
+
}
|
|
982
|
+
[inspectCustom](_depth, options) {
|
|
983
|
+
return styled(options, FILTERED) + (this.#changed ? " (changed)" : "");
|
|
779
984
|
}
|
|
780
985
|
};
|
|
781
|
-
/** A primary key as an error shows it: "[FILTERED]" when the key is a filtered attribute. */
|
|
782
|
-
function shownKey(model, key) {
|
|
783
|
-
return isFiltered(model, model.primaryKey) ? FILTERED : String(key);
|
|
784
|
-
}
|
|
785
|
-
/** The value D1 binds. */
|
|
786
|
-
function boundValue(param) {
|
|
787
|
-
return param instanceof Filtered ? param.value : param;
|
|
788
|
-
}
|
|
789
|
-
/** The value listeners see. */
|
|
790
|
-
function shownValue$1(param) {
|
|
791
|
-
return param instanceof Filtered ? param.shown : param;
|
|
792
|
-
}
|
|
793
|
-
// src/attribute.ts
|
|
794
|
-
/** What `condition()` and `conditionCell()` give for a value that can't match anything. */
|
|
795
|
-
const noMatch = uncastable;
|
|
796
986
|
/**
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
* stored are known here, so a caller only says what the value is for.
|
|
987
|
+
* `label` and the runtime's own `inspect` of `body`, which is built only when there is depth left;
|
|
988
|
+
* otherwise `[name]`, as Node prints any class instance nested too deep.
|
|
800
989
|
*/
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
990
|
+
function printed(label, name, body, { depth, options, inspect }) {
|
|
991
|
+
if (depth < 0) return styled(options, `[${name}]`);
|
|
992
|
+
return `${label} ${inspect(body(), {
|
|
993
|
+
...options,
|
|
994
|
+
depth
|
|
995
|
+
})}`;
|
|
996
|
+
}
|
|
997
|
+
// src/errors-collection.ts
|
|
998
|
+
/** @internal The library's key for emptying an errors collection. */
|
|
999
|
+
const clearErrors = Symbol("clearErrors");
|
|
1000
|
+
/** @internal The library's key for adding a failure it found, with an optional message to use as it is. */
|
|
1001
|
+
const addFailure = Symbol("addFailure");
|
|
1002
|
+
/** @internal The library's key for a failure's full message, without adding it (`strict`). */
|
|
1003
|
+
const fullMessageOf = Symbol("fullMessageOf");
|
|
1004
|
+
/** A record's validation errors: `record.errors`. Cleared at the start of each validation run. */
|
|
1005
|
+
var Errors = class {
|
|
1006
|
+
#entries = [];
|
|
1007
|
+
#source;
|
|
1008
|
+
/** @internal Made by the library for each record. */
|
|
1009
|
+
constructor(source) {
|
|
1010
|
+
this.#source = source;
|
|
820
1011
|
}
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
1012
|
+
add(attribute, message, options = {}) {
|
|
1013
|
+
if (typeof message === "string") {
|
|
1014
|
+
this.#entries.push({
|
|
1015
|
+
attribute,
|
|
1016
|
+
type: options.type ?? "invalid",
|
|
1017
|
+
values: {},
|
|
1018
|
+
message
|
|
1019
|
+
});
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
const { type, ...values } = message;
|
|
1023
|
+
if (typeof type !== "string") throw new TypeError("errors.add needs a message or a type");
|
|
1024
|
+
const entry = {
|
|
1025
|
+
attribute,
|
|
1026
|
+
type,
|
|
1027
|
+
values
|
|
1028
|
+
};
|
|
1029
|
+
messageOf(this.#source, entry);
|
|
1030
|
+
this.#entries.push(entry);
|
|
824
1031
|
}
|
|
825
|
-
/**
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
* what can't be cast throws `TypeError` naming the Model and attribute, showing the value
|
|
829
|
-
* unless the attribute is filtered (#83).
|
|
830
|
-
*/
|
|
831
|
-
cast(value) {
|
|
832
|
-
requireDefined(this.#model, this.name, value);
|
|
833
|
-
const cast = this.#convertOrRefuse(value, () => castValue(this.#definition, value));
|
|
834
|
-
if (cast === uncastable && this.#isValidatedEnum()) return value;
|
|
835
|
-
return this.#orRefuse(cast, value);
|
|
1032
|
+
/** The messages for one attribute (always an array). */
|
|
1033
|
+
get(attribute) {
|
|
1034
|
+
return this.#entries.filter((entry) => entry.attribute === attribute).map((entry) => messageOf(this.#source, entry));
|
|
836
1035
|
}
|
|
837
|
-
/**
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
return values !== void 0 && validate !== false;
|
|
1036
|
+
/** Every message with its attribute's human name first: "Email can't be blank". */
|
|
1037
|
+
fullMessages() {
|
|
1038
|
+
return this.#entries.map((entry) => this.#fullMessage(entry));
|
|
841
1039
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
key(value, association) {
|
|
847
|
-
requireDefined(this.#model, this.name, value);
|
|
848
|
-
return this.#orRefuse(castKey(this.#definition, value), value, association);
|
|
1040
|
+
#fullMessage(entry) {
|
|
1041
|
+
const message = messageOf(this.#source, entry);
|
|
1042
|
+
if (entry.attribute === "base") return message;
|
|
1043
|
+
return `${humanAttributeName(this.#source.model, entry.attribute)} ${message}`;
|
|
849
1044
|
}
|
|
850
|
-
/**
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
1045
|
+
/** Every error as `{ attribute, type, message }`, with its `values` when it has some (copies). */
|
|
1046
|
+
toArray() {
|
|
1047
|
+
return this.#entries.map((entry) => {
|
|
1048
|
+
const { attribute, type, values } = entry;
|
|
1049
|
+
const error = {
|
|
1050
|
+
attribute,
|
|
1051
|
+
type,
|
|
1052
|
+
message: messageOf(this.#source, entry)
|
|
1053
|
+
};
|
|
1054
|
+
return Object.keys(values).length === 0 ? error : {
|
|
1055
|
+
...error,
|
|
1056
|
+
values: { ...values }
|
|
1057
|
+
};
|
|
1058
|
+
});
|
|
857
1059
|
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
const cast = this.condition(value);
|
|
861
|
-
return cast === noMatch ? noMatch : this.write(cast);
|
|
1060
|
+
isEmpty() {
|
|
1061
|
+
return this.#entries.length === 0;
|
|
862
1062
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
* can't write throws `TypeError` naming the attribute, as assigning it would (#146).
|
|
866
|
-
*/
|
|
867
|
-
write(value) {
|
|
868
|
-
let stored;
|
|
869
|
-
try {
|
|
870
|
-
stored = writeValue(this.#definition, value);
|
|
871
|
-
} catch (error) {
|
|
872
|
-
if (this.#definition.customType !== void 0) throw this.#refusal(value, void 0, error);
|
|
873
|
-
if (this.#definition.type !== "json") throw error;
|
|
874
|
-
throw this.#refusal(value);
|
|
875
|
-
}
|
|
876
|
-
if (stored === void 0) throw this.#refusal(value);
|
|
877
|
-
return stored;
|
|
1063
|
+
get size() {
|
|
1064
|
+
return this.#entries.length;
|
|
878
1065
|
}
|
|
879
|
-
/**
|
|
880
|
-
|
|
881
|
-
return
|
|
1066
|
+
/** `Errors [ { attribute, type, message }, … ]`, so a console shows what `#entries` holds (#104). */
|
|
1067
|
+
[inspectCustom](depth, options, inspect) {
|
|
1068
|
+
return printed("Errors", "Errors", () => this.toArray(), {
|
|
1069
|
+
depth,
|
|
1070
|
+
options,
|
|
1071
|
+
inspect
|
|
1072
|
+
});
|
|
882
1073
|
}
|
|
883
|
-
/**
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
return
|
|
1074
|
+
/** @internal Adds an error the library found, such as a validator's, returning its message. */
|
|
1075
|
+
[addFailure](attribute, { values = {}, ...failure }) {
|
|
1076
|
+
const entry = {
|
|
1077
|
+
attribute,
|
|
1078
|
+
values,
|
|
1079
|
+
...failure
|
|
1080
|
+
};
|
|
1081
|
+
this.#entries.push(entry);
|
|
1082
|
+
return messageOf(this.#source, entry);
|
|
892
1083
|
}
|
|
893
|
-
/** A
|
|
894
|
-
|
|
895
|
-
return this
|
|
1084
|
+
/** @internal A failure's full message, as `fullMessages()` would give it, without adding it. */
|
|
1085
|
+
[fullMessageOf](attribute, { values = {}, ...failure }) {
|
|
1086
|
+
return this.#fullMessage({
|
|
1087
|
+
attribute,
|
|
1088
|
+
values,
|
|
1089
|
+
...failure
|
|
1090
|
+
});
|
|
896
1091
|
}
|
|
897
|
-
/**
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
* Rails' do, and one that can't be cast is the same as nothing (#20).
|
|
901
|
-
*/
|
|
902
|
-
isSame(a, b) {
|
|
903
|
-
const [x, y] = [this.#comparable(a), this.#comparable(b)];
|
|
904
|
-
if (x === noMatch || y === noMatch) return false;
|
|
905
|
-
return isSameCell(writeValue(this.#definition, x), writeValue(this.#definition, y));
|
|
1092
|
+
/** @internal Called at the start of each validation run. */
|
|
1093
|
+
[clearErrors]() {
|
|
1094
|
+
this.#entries = [];
|
|
906
1095
|
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
1096
|
+
};
|
|
1097
|
+
// src/loaded-associations.ts
|
|
1098
|
+
/**
|
|
1099
|
+
* The associations one record has loaded (preloaded, read, or set by a write): a record, an
|
|
1100
|
+
* array of records, or null. Each stays loaded while the owner's attribute still holds the key
|
|
1101
|
+
* it was loaded for, and a load overtaken by a change to its association isn't kept.
|
|
1102
|
+
*/
|
|
1103
|
+
var LoadedAssociations = class {
|
|
1104
|
+
/** The owner's attribute value now, which an entry's key is checked against. */
|
|
1105
|
+
#keyOf;
|
|
1106
|
+
#entries = /* @__PURE__ */ new Map();
|
|
1107
|
+
/** Counted when an association changes, by name and record-wide, so a stale load isn't kept. */
|
|
1108
|
+
#loadsDiscarded = /* @__PURE__ */ new Map();
|
|
1109
|
+
#allLoadsDiscarded = 0;
|
|
1110
|
+
constructor(keyOf) {
|
|
1111
|
+
this.#keyOf = keyOf;
|
|
915
1112
|
}
|
|
916
|
-
/**
|
|
917
|
-
|
|
918
|
-
|
|
1113
|
+
/** The loaded association, if it was loaded for the key the owner holds now. */
|
|
1114
|
+
get(name) {
|
|
1115
|
+
const entry = this.#entries.get(name);
|
|
1116
|
+
if (entry === void 0 || !isSameKey(this.#key(entry.attribute), entry.key)) return void 0;
|
|
1117
|
+
return entry;
|
|
919
1118
|
}
|
|
920
1119
|
/**
|
|
921
|
-
*
|
|
922
|
-
*
|
|
923
|
-
* time, since `filterAttributes` is a static a Model may set after this was resolved.
|
|
1120
|
+
* Sets the association to what a write or preload made it, for the key `attribute` holds now;
|
|
1121
|
+
* loads of it already sent won't be kept.
|
|
924
1122
|
*/
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
1123
|
+
set(name, attribute, value) {
|
|
1124
|
+
this.#entries.set(name, {
|
|
1125
|
+
attribute,
|
|
1126
|
+
key: this.#key(attribute),
|
|
1127
|
+
value
|
|
1128
|
+
});
|
|
1129
|
+
this.#discardPendingLoads(name);
|
|
932
1130
|
}
|
|
933
1131
|
/**
|
|
934
|
-
*
|
|
935
|
-
*
|
|
1132
|
+
* Starts a load of the association, before its query is sent, for the key its query was built
|
|
1133
|
+
* for (the attributes' values now, unless it's given): the function it returns keeps the loaded value only if nothing changed the association
|
|
1134
|
+
* meanwhile (#181).
|
|
936
1135
|
*/
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
1136
|
+
startLoad(name, attribute, key = this.#key(attribute)) {
|
|
1137
|
+
const generation = this.#generation(name);
|
|
1138
|
+
return (value) => {
|
|
1139
|
+
if (this.#generation(name) === generation) this.#entries.set(name, {
|
|
1140
|
+
attribute,
|
|
1141
|
+
key,
|
|
1142
|
+
value
|
|
1143
|
+
});
|
|
1144
|
+
};
|
|
944
1145
|
}
|
|
945
|
-
/**
|
|
946
|
-
|
|
947
|
-
|
|
1146
|
+
/** Forgets the association; loads of it already sent won't be kept. */
|
|
1147
|
+
clear(name) {
|
|
1148
|
+
this.#entries.delete(name);
|
|
1149
|
+
this.#discardPendingLoads(name);
|
|
1150
|
+
}
|
|
1151
|
+
/** Forgets every association (`reload()`); loads already sent won't be kept. */
|
|
1152
|
+
clearAll() {
|
|
1153
|
+
this.#entries.clear();
|
|
1154
|
+
this.#allLoadsDiscarded += 1;
|
|
1155
|
+
}
|
|
1156
|
+
#key(attribute) {
|
|
1157
|
+
if (typeof attribute !== "string") return attribute.map((each) => this.#keyOf(each) ?? null);
|
|
1158
|
+
return this.#keyOf(attribute) ?? null;
|
|
1159
|
+
}
|
|
1160
|
+
#discardPendingLoads(name) {
|
|
1161
|
+
this.#loadsDiscarded.set(name, (this.#loadsDiscarded.get(name) ?? 0) + 1);
|
|
948
1162
|
}
|
|
949
1163
|
/**
|
|
950
|
-
*
|
|
951
|
-
*
|
|
952
|
-
* cell that isn't JSON (#146). A custom type's own error is its cause, unless the attribute is
|
|
953
|
-
* filtered: the error may show the value (#20).
|
|
1164
|
+
* Changes with each change to the association, so a load can tell whether it's still current.
|
|
1165
|
+
* The two counts are summed, so `clearAll()` makes every name stale without touching the map.
|
|
954
1166
|
*/
|
|
955
|
-
#
|
|
956
|
-
|
|
957
|
-
const text = hidden ? FILTERED : shown(value);
|
|
958
|
-
const from = association === void 0 ? "" : `, the key of ${association}`;
|
|
959
|
-
return new TypeError(`${this.#model.name}: ${text} isn't ${this.#described()} for "${this.name}"${from}`, hidden || cause === void 0 ? void 0 : { cause });
|
|
960
|
-
}
|
|
961
|
-
/** What a value of the attribute is, as a refusal says it: `a boolean`, `one of "draft", "published"`. */
|
|
962
|
-
#described() {
|
|
963
|
-
const { type, enum: values, customType } = this.#definition;
|
|
964
|
-
if (values !== void 0) return `one of ${listedNames(values)}`;
|
|
965
|
-
if (customType !== void 0) return "a valid value";
|
|
966
|
-
return valueNames[type] ?? withArticle(type);
|
|
1167
|
+
#generation(name) {
|
|
1168
|
+
return this.#allLoadsDiscarded + (this.#loadsDiscarded.get(name) ?? 0);
|
|
967
1169
|
}
|
|
968
1170
|
};
|
|
969
|
-
|
|
1171
|
+
/** Whether two keys are the same: one value, or each of a list's (#60). */
|
|
1172
|
+
function isSameKey(now, loaded) {
|
|
1173
|
+
if (!Array.isArray(now) || !Array.isArray(loaded)) return Object.is(now, loaded);
|
|
1174
|
+
return now.every((each, index) => Object.is(each, loaded[index]));
|
|
1175
|
+
}
|
|
1176
|
+
// src/pending-autosaves.ts
|
|
970
1177
|
/**
|
|
971
|
-
* The
|
|
972
|
-
*
|
|
1178
|
+
* The records saving one record may also save: those built through its hasMany and hasOne
|
|
1179
|
+
* associations or pushed onto a hasMany, after it (`autosavesOf` says which), and the new ones
|
|
1180
|
+
* its belongsTo associations point at, before it.
|
|
973
1181
|
*/
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
1182
|
+
var PendingAutosaves = class {
|
|
1183
|
+
#built = /* @__PURE__ */ new Map();
|
|
1184
|
+
#belongsTo = /* @__PURE__ */ new Map();
|
|
1185
|
+
/** A record built through one of this record's hasMany or hasOne associations. */
|
|
1186
|
+
addBuilt(name, record) {
|
|
1187
|
+
this.#built.set(name, [...this.#built.get(name) ?? [], record]);
|
|
979
1188
|
}
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
1189
|
+
/** Takes `records` out of those built through the association (`delete`, `destroy`). */
|
|
1190
|
+
removeBuilt(name, records) {
|
|
1191
|
+
const kept = this.built(name).filter((record) => !records.includes(record));
|
|
1192
|
+
this.#built.set(name, kept);
|
|
984
1193
|
}
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
* The values a record built from a Relation inherits from its conditions, cast as keys are: a
|
|
989
|
-
* hasMany's foreign key names its association (`link`) when it can't be cast.
|
|
990
|
-
*/
|
|
991
|
-
function castInherited(model, inherited, link) {
|
|
992
|
-
return Object.fromEntries(Object.entries(inherited).map(([attribute, value]) => {
|
|
993
|
-
const association = attribute === link?.attribute ? link.association : void 0;
|
|
994
|
-
return [attribute, attributeFor(model, attribute).key(value, association)];
|
|
995
|
-
}));
|
|
996
|
-
}
|
|
997
|
-
/** A word with `a` or `an` before it, as a message reads it: `an integer`, `a string`. */
|
|
998
|
-
function withArticle(word) {
|
|
999
|
-
return `${/^[aeiou]/.test(word) ? "an" : "a"} ${word}`;
|
|
1000
|
-
}
|
|
1001
|
-
/** How much of a value an error shows: it came from outside, and errors are often logged. */
|
|
1002
|
-
const SHOWN_LENGTH = 60;
|
|
1003
|
-
/** What a value of an attribute type is called in a refusal, where `a`/`an` and its name isn't it. */
|
|
1004
|
-
const valueNames = {
|
|
1005
|
-
json: "a JSON value",
|
|
1006
|
-
binary: "binary data"
|
|
1007
|
-
};
|
|
1008
|
-
/** A value as an error shows it: JSON where it has one, numbers as written, and at most 60 characters. */
|
|
1009
|
-
function shown(value) {
|
|
1010
|
-
const text = textOf(value);
|
|
1011
|
-
return text.length > SHOWN_LENGTH ? `${text.slice(0, SHOWN_LENGTH)}…` : text;
|
|
1012
|
-
}
|
|
1013
|
-
function textOf(value) {
|
|
1014
|
-
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return `<${value.constructor.name} of ${value.byteLength} bytes>`;
|
|
1015
|
-
if (value instanceof Blob) return `<${value.constructor.name} of ${value.size} bytes>`;
|
|
1016
|
-
if (typeof value === "number" || isInvalidDate(value)) return String(value);
|
|
1017
|
-
try {
|
|
1018
|
-
return JSON.stringify(value) ?? String(value);
|
|
1019
|
-
} catch {
|
|
1020
|
-
return plainText(value);
|
|
1194
|
+
/** A hasOne's one record to save with this record, replacing any before it. */
|
|
1195
|
+
setBuilt(name, record) {
|
|
1196
|
+
this.#built.set(name, [record]);
|
|
1021
1197
|
}
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
try {
|
|
1026
|
-
return String(value);
|
|
1027
|
-
} catch {
|
|
1028
|
-
return Object.prototype.toString.call(value);
|
|
1198
|
+
/** The records built through the association, in the order they were built. */
|
|
1199
|
+
built(name) {
|
|
1200
|
+
return this.#built.get(name) ?? [];
|
|
1029
1201
|
}
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
greaterThan: "must be greater than %{count}",
|
|
1057
|
-
greaterThanOrEqualTo: "must be greater than or equal to %{count}",
|
|
1058
|
-
lessThan: "must be less than %{count}",
|
|
1059
|
-
lessThanOrEqualTo: "must be less than or equal to %{count}",
|
|
1060
|
-
equalTo: "must be equal to %{count}",
|
|
1061
|
-
otherThan: "must be other than %{count}",
|
|
1062
|
-
restrictDependentDestroy: {
|
|
1063
|
-
hasOne: "Cannot delete record because a dependent %{record} exists",
|
|
1064
|
-
hasMany: "Cannot delete record because dependent %{record} exist"
|
|
1202
|
+
/** Every association's built records, by association name. */
|
|
1203
|
+
allBuilt() {
|
|
1204
|
+
return [...this.#built];
|
|
1205
|
+
}
|
|
1206
|
+
/** A new record a belongsTo points at, saved first when this record is; undefined clears it. */
|
|
1207
|
+
setBelongsTo(name, record) {
|
|
1208
|
+
if (record === void 0) this.#belongsTo.delete(name);
|
|
1209
|
+
else this.#belongsTo.set(name, record);
|
|
1210
|
+
}
|
|
1211
|
+
/** The new records this record's belongsTo associations point at, by association name. */
|
|
1212
|
+
allBelongsTo() {
|
|
1213
|
+
return [...this.#belongsTo];
|
|
1214
|
+
}
|
|
1215
|
+
/** Whether there are none: saving the record saves no other record with it. */
|
|
1216
|
+
isEmpty() {
|
|
1217
|
+
return [...this.#built.values()].every((records) => records.length === 0) && this.#belongsTo.size === 0;
|
|
1218
|
+
}
|
|
1219
|
+
/** Forgets what was built through the association or set on it (`reloadAssociation`). */
|
|
1220
|
+
clear(name) {
|
|
1221
|
+
this.#built.delete(name);
|
|
1222
|
+
this.#belongsTo.delete(name);
|
|
1223
|
+
}
|
|
1224
|
+
/** Forgets every association's (`reload()`). */
|
|
1225
|
+
clearAll() {
|
|
1226
|
+
this.#built.clear();
|
|
1227
|
+
this.#belongsTo.clear();
|
|
1065
1228
|
}
|
|
1066
1229
|
};
|
|
1067
|
-
|
|
1068
|
-
/**
|
|
1069
|
-
function
|
|
1070
|
-
return
|
|
1230
|
+
// src/metadata.ts
|
|
1231
|
+
/** A spec written out: a type alone (a name, or a custom type) as `{ type }`. */
|
|
1232
|
+
function specObjectOf(spec) {
|
|
1233
|
+
return typeof spec === "string" || isCustomType(spec) ? { type: spec } : spec;
|
|
1071
1234
|
}
|
|
1072
|
-
/**
|
|
1073
|
-
function
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1235
|
+
/** Resolves an attribute's spec into its type, column and default. */
|
|
1236
|
+
function define(name, spec) {
|
|
1237
|
+
if (typeof spec === "string") return {
|
|
1238
|
+
type: spec,
|
|
1239
|
+
column: underscore(name)
|
|
1240
|
+
};
|
|
1241
|
+
if (isCustomType(spec)) return {
|
|
1242
|
+
...resolvedType(spec),
|
|
1243
|
+
column: underscore(name)
|
|
1244
|
+
};
|
|
1245
|
+
if (spec.virtual === true) return defineVirtual(name, spec);
|
|
1246
|
+
return {
|
|
1247
|
+
...resolvedType(spec.type),
|
|
1248
|
+
column: spec.column ?? underscore(name),
|
|
1249
|
+
...copiedKeys(spec)
|
|
1078
1250
|
};
|
|
1079
|
-
return Object.assign({}, ...calls, { attributes });
|
|
1080
1251
|
}
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
const
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1252
|
+
/**
|
|
1253
|
+
* A virtual attribute's definition (#356): no column (`""`, unless one was given, which a Model's
|
|
1254
|
+
* check refuses), and non-null only with a default.
|
|
1255
|
+
*/
|
|
1256
|
+
function defineVirtual(name, spec) {
|
|
1257
|
+
const problem = virtualNullProblem(name, spec);
|
|
1258
|
+
if (problem !== void 0) throw new InvalidModel(problem);
|
|
1259
|
+
return {
|
|
1260
|
+
...resolvedType(spec.type),
|
|
1261
|
+
column: spec.column ?? "",
|
|
1262
|
+
...copiedKeys(spec)
|
|
1091
1263
|
};
|
|
1092
|
-
if (isSealed(model)) catalogs.set(model, catalog);
|
|
1093
|
-
return catalog;
|
|
1094
1264
|
}
|
|
1095
|
-
/**
|
|
1096
|
-
function
|
|
1097
|
-
|
|
1098
|
-
for (const messages of catalogOf(model).messages) {
|
|
1099
|
-
const found = ownValue(ownValue(messages.attributes, attribute), type) ?? ownValue(messages, type);
|
|
1100
|
-
if (found !== void 0) return found;
|
|
1101
|
-
}
|
|
1265
|
+
/** A virtual attribute declared non-null with no default, which would read as null (#356). */
|
|
1266
|
+
function virtualNullProblem(where, spec) {
|
|
1267
|
+
return spec.null === false && spec.default === void 0 ? `${where} is virtual and null: false, so it needs a default` : void 0;
|
|
1102
1268
|
}
|
|
1103
|
-
/**
|
|
1104
|
-
function
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1269
|
+
/** A spec's type as a definition holds it: a custom type as the type it's stored as, and itself. */
|
|
1270
|
+
function resolvedType(type) {
|
|
1271
|
+
return isCustomType(type) ? {
|
|
1272
|
+
type: type.storedAs,
|
|
1273
|
+
customType: type
|
|
1274
|
+
} : { type };
|
|
1108
1275
|
}
|
|
1109
|
-
/**
|
|
1110
|
-
function
|
|
1111
|
-
const
|
|
1112
|
-
|
|
1276
|
+
/** Looks up a declared attribute, or the one an alias names; throws `UnknownAttribute` for any other name. */
|
|
1277
|
+
function attributeOf(model, name) {
|
|
1278
|
+
const attribute = model.attributeDefinitions.get(attributeName(model, name));
|
|
1279
|
+
if (attribute === void 0) throw new UnknownAttribute(`${model.name} has no attribute "${name}"`);
|
|
1280
|
+
return attribute;
|
|
1113
1281
|
}
|
|
1114
|
-
/**
|
|
1115
|
-
|
|
1116
|
-
|
|
1282
|
+
/**
|
|
1283
|
+
* Looks up an attribute a query reads or writes as a column: `UnknownAttribute` for a name the
|
|
1284
|
+
* Model doesn't have, or a virtual attribute, which has none (#356).
|
|
1285
|
+
*/
|
|
1286
|
+
function columnOf(model, name) {
|
|
1287
|
+
const attribute = attributeOf(model, name);
|
|
1288
|
+
if (attribute.virtual === true) throw new UnknownAttribute(`${model.name}.${name} is a virtual attribute, which has no column to query`);
|
|
1289
|
+
return attribute;
|
|
1117
1290
|
}
|
|
1118
|
-
/**
|
|
1119
|
-
function
|
|
1120
|
-
|
|
1121
|
-
const value = source.read(attribute);
|
|
1122
|
-
if (value === null || value === void 0) return "";
|
|
1123
|
-
if (isFiltered(source.model, attribute)) return FILTERED;
|
|
1124
|
-
if (value instanceof Date) return value.toISOString();
|
|
1125
|
-
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
1291
|
+
/** The attribute names mapped to `column` (usually one; none if the column isn't declared). */
|
|
1292
|
+
function attributesForColumn(model, column) {
|
|
1293
|
+
return [...model.attributeDefinitions].filter(([, definition]) => definition.column === column).map(([name]) => name);
|
|
1126
1294
|
}
|
|
1127
|
-
/**
|
|
1128
|
-
function
|
|
1129
|
-
if (
|
|
1130
|
-
const { model } = source;
|
|
1131
|
-
const text = messageText(templateFor(model, entry.attribute, entry.type), entry);
|
|
1132
|
-
if (text === void 0) throw missingMessage(model, entry);
|
|
1133
|
-
return text.replace(/%\{(\w+)\}/g, (placeholder, name) => {
|
|
1134
|
-
if (name === "attribute") return humanAttributeName(model, entry.attribute);
|
|
1135
|
-
if (name === "model") return inWords(model.modelName);
|
|
1136
|
-
if (name === "value") return shownValue(source, entry.attribute);
|
|
1137
|
-
return String(ownValue(entry.values, name) ?? placeholder);
|
|
1138
|
-
});
|
|
1295
|
+
/** `undefined` would silently store NULL, so writing it throws `TypeError` (use `null`). */
|
|
1296
|
+
function requireDefined(model, name, value) {
|
|
1297
|
+
if (value === void 0) throw new TypeError(`${model.name}: got undefined for "${name}"; use null to store NULL`);
|
|
1139
1298
|
}
|
|
1140
|
-
/**
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1299
|
+
/**
|
|
1300
|
+
* The initial values of every attribute not in `given` that has one, made now: each one's
|
|
1301
|
+
* `default`, for a generated id, the Model's `generateId(name)`, and for a secure token generated
|
|
1302
|
+
* as a record is built, a new token (#355).
|
|
1303
|
+
*/
|
|
1304
|
+
function initialValues(model, given = {}) {
|
|
1305
|
+
return [...model.attributeDefinitions].filter(([name, definition]) => !(name in given) && hasInitialValue(definition)).map(([name, definition]) => [name, initialValue(model, name, definition)]);
|
|
1144
1306
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
return
|
|
1148
|
-
from,
|
|
1149
|
-
declaration: "errorMessages",
|
|
1150
|
-
names: values.flatMap((value) => Object.keys(value.attributes ?? {}))
|
|
1151
|
-
})), ...declaredByClass(model, "humanAttributeNames").map(({ from, values }) => ({
|
|
1152
|
-
from,
|
|
1153
|
-
declaration: "humanAttributeNames",
|
|
1154
|
-
names: values.flatMap((value) => Object.keys(value))
|
|
1155
|
-
}))];
|
|
1307
|
+
function initialValue(model, name, definition) {
|
|
1308
|
+
if (definition.generateId) return model.generateId(name);
|
|
1309
|
+
return isTokenOnInitialize(definition) ? tokenFor(definition) : definition.default();
|
|
1156
1310
|
}
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1311
|
+
/** Whether an attribute gets a value when none is given: from its `default`, `generateId`, or a token. */
|
|
1312
|
+
function hasInitialValue(definition) {
|
|
1313
|
+
return definition.generateId === true || definition.default !== void 0 || isTokenOnInitialize(definition);
|
|
1314
|
+
}
|
|
1315
|
+
// src/record-state.ts
|
|
1316
|
+
/** @internal The hidden slot where a Record keeps its `RecordState`. */
|
|
1317
|
+
const state = Symbol("state");
|
|
1318
|
+
/** Each record's link to its database (its `RecordState`), for pinning it to one (#93). */
|
|
1319
|
+
function linksOf(records) {
|
|
1320
|
+
return [...records].map((record) => record[state]);
|
|
1163
1321
|
}
|
|
1164
|
-
/** A filtered value's stand-in: prints unquoted, so it isn't taken for a string, as a special value. */
|
|
1165
|
-
var FilteredMark = class {
|
|
1166
|
-
#changed;
|
|
1167
|
-
constructor(changed) {
|
|
1168
|
-
this.#changed = changed;
|
|
1169
|
-
}
|
|
1170
|
-
[inspectCustom](_depth, options) {
|
|
1171
|
-
return styled(options, FILTERED) + (this.#changed ? " (changed)" : "");
|
|
1172
|
-
}
|
|
1173
|
-
};
|
|
1174
1322
|
/**
|
|
1175
|
-
*
|
|
1176
|
-
*
|
|
1323
|
+
* Everything one Record knows about itself: its attribute values, lifecycle, the attributes
|
|
1324
|
+
* assigned since it was built, its Snapshot (each loaded attribute's stored form as last loaded
|
|
1325
|
+
* or saved), the changes its last save wrote, and the Database it belongs to.
|
|
1326
|
+
*
|
|
1327
|
+
* This is the only module that reads or writes those fields. Every rule about them (unloaded
|
|
1328
|
+
* attributes, undefined values, destroyed records, change detection) is enforced here.
|
|
1177
1329
|
*/
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
/**
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
/**
|
|
1197
|
-
|
|
1198
|
-
|
|
1330
|
+
var RecordState = class {
|
|
1331
|
+
#model;
|
|
1332
|
+
#executor;
|
|
1333
|
+
#lifecycle = "new";
|
|
1334
|
+
#values = {};
|
|
1335
|
+
#assigned = /* @__PURE__ */ new Set();
|
|
1336
|
+
#snapshot = /* @__PURE__ */ new Map();
|
|
1337
|
+
/** The row the Snapshot was loaded from: its entries for the row's cells are made on first use (#91). */
|
|
1338
|
+
#snapshotRow;
|
|
1339
|
+
/** A new record's initial values in stored form, which `restore` returns attributes to. */
|
|
1340
|
+
#initialValues = /* @__PURE__ */ new Map();
|
|
1341
|
+
#savedChanges = {};
|
|
1342
|
+
#errors;
|
|
1343
|
+
#abortReason;
|
|
1344
|
+
/** The associations it has loaded, each kept while its key is current. */
|
|
1345
|
+
associations = new LoadedAssociations((attribute) => this.#values[attribute]);
|
|
1346
|
+
/** The new records saving it also saves, built through or pointed at by its associations. */
|
|
1347
|
+
autosaves = new PendingAutosaves();
|
|
1348
|
+
/** Destroyed when its owner is saved through an `autosave: true` association (#37). */
|
|
1349
|
+
isMarkedForDestruction = false;
|
|
1350
|
+
/** Whether the save being prepared sends a locked UPDATE, which sets the row's version (#357). */
|
|
1351
|
+
isSavingVersion = false;
|
|
1352
|
+
/** Marked read-only (#358): by `markReadonly()`, or loaded through a `readonly()` Relation. */
|
|
1353
|
+
isReadonly = false;
|
|
1354
|
+
/** Whether its last save created it (#351). */
|
|
1355
|
+
isPreviouslyNew = false;
|
|
1356
|
+
/** The contexts it's being validated in, while it is (#51). */
|
|
1357
|
+
validationContexts = [];
|
|
1358
|
+
constructor(model) {
|
|
1359
|
+
this.#model = model;
|
|
1360
|
+
const read = (name) => Object.hasOwn(this.#values, name) ? this.#values[name] : null;
|
|
1361
|
+
this.#errors = new Errors({
|
|
1362
|
+
model,
|
|
1363
|
+
read
|
|
1364
|
+
});
|
|
1199
1365
|
}
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1366
|
+
/** The record's validation errors, filled by the last validation run. */
|
|
1367
|
+
get errors() {
|
|
1368
|
+
return this.#errors;
|
|
1369
|
+
}
|
|
1370
|
+
/** Why a callback last refused a save or destroy; cleared at the start of each run. */
|
|
1371
|
+
get abortReason() {
|
|
1372
|
+
return this.#abortReason;
|
|
1373
|
+
}
|
|
1374
|
+
set abortReason(reason) {
|
|
1375
|
+
this.#abortReason = reason;
|
|
1376
|
+
}
|
|
1377
|
+
/** Not yet saved: built, or created but not persisted. */
|
|
1378
|
+
get isNew() {
|
|
1379
|
+
return this.#lifecycle === "new";
|
|
1380
|
+
}
|
|
1381
|
+
/** Backed by a row: loaded, or saved successfully. */
|
|
1382
|
+
get isPersisted() {
|
|
1383
|
+
return this.#lifecycle === "persisted";
|
|
1384
|
+
}
|
|
1385
|
+
/** Deleted by this instance: a read-only snapshot of the row. */
|
|
1386
|
+
get isDestroyed() {
|
|
1387
|
+
return this.#lifecycle === "destroyed";
|
|
1388
|
+
}
|
|
1389
|
+
/** Records belong to the Database they were built or loaded through (ADR-0003). */
|
|
1390
|
+
belongTo(executor) {
|
|
1391
|
+
this.#executor = executor;
|
|
1392
|
+
}
|
|
1393
|
+
/** The record's Database; `NoDatabase` for a record constructed directly (`new User()`). */
|
|
1394
|
+
get executor() {
|
|
1395
|
+
if (this.#executor === void 0) {
|
|
1396
|
+
const { name } = this.#model;
|
|
1397
|
+
throw new NoDatabase(`${name} record has no Database; build or load records through db.model(${name})`);
|
|
1209
1398
|
}
|
|
1210
|
-
|
|
1211
|
-
if (typeof type !== "string") throw new TypeError("errors.add needs a message or a type");
|
|
1212
|
-
const entry = {
|
|
1213
|
-
attribute,
|
|
1214
|
-
type,
|
|
1215
|
-
values
|
|
1216
|
-
};
|
|
1217
|
-
messageOf(this.#source, entry);
|
|
1218
|
-
this.#entries.push(entry);
|
|
1399
|
+
return this.#executor;
|
|
1219
1400
|
}
|
|
1220
|
-
/**
|
|
1221
|
-
|
|
1222
|
-
|
|
1401
|
+
/**
|
|
1402
|
+
* Refuses to use a record as a live one when it can't be: a destroyed record is a read-only
|
|
1403
|
+
* snapshot, and a persisted record without its primary key (from `select(...).distinct()`)
|
|
1404
|
+
* has no identity to write by.
|
|
1405
|
+
*/
|
|
1406
|
+
requireLive(action) {
|
|
1407
|
+
this.#requireNotDestroyed(action);
|
|
1408
|
+
this.requireKey(action);
|
|
1223
1409
|
}
|
|
1224
|
-
/**
|
|
1225
|
-
|
|
1226
|
-
|
|
1410
|
+
/** A persisted record needs its primary key to be found by; one from `distinct()` has none. */
|
|
1411
|
+
requireKey(action) {
|
|
1412
|
+
const { name, primaryKey } = this.#model;
|
|
1413
|
+
if (this.#lifecycle === "persisted" && !this.#isInSnapshot(primaryKey)) throw new MissingAttribute(`${name} record has no primary key loaded (it came from select(...).distinct()); it can't ${action}`);
|
|
1227
1414
|
}
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
return
|
|
1415
|
+
/** Whether `name` is one of the Model's attributes and loaded: not left out of `select(...)`. */
|
|
1416
|
+
hasAttribute(given) {
|
|
1417
|
+
const name = attributeName(this.#model, given);
|
|
1418
|
+
return this.#model.attributeDefinitions.has(name) && Object.hasOwn(this.#values, name);
|
|
1232
1419
|
}
|
|
1233
|
-
/**
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
type,
|
|
1240
|
-
message: messageOf(this.#source, entry)
|
|
1241
|
-
};
|
|
1242
|
-
return Object.keys(values).length === 0 ? error : {
|
|
1243
|
-
...error,
|
|
1244
|
-
values: { ...values }
|
|
1245
|
-
};
|
|
1246
|
-
});
|
|
1420
|
+
/** An attribute's value; `MissingAttribute` if it wasn't loaded (left out of `select`). */
|
|
1421
|
+
read(given) {
|
|
1422
|
+
const name = attributeName(this.#model, given);
|
|
1423
|
+
this.#requireAttribute(name);
|
|
1424
|
+
if (!Object.hasOwn(this.#values, name)) throw new MissingAttribute(`${this.#model.name} record has no "${name}" loaded; it was left out of select(...)`);
|
|
1425
|
+
return this.#values[name];
|
|
1247
1426
|
}
|
|
1248
|
-
|
|
1249
|
-
|
|
1427
|
+
/**
|
|
1428
|
+
* Assigns one attribute. `undefined` throws `TypeError`, because it would silently store NULL
|
|
1429
|
+
* (use `null` for that); a destroyed record is read-only. A secure password's assignment
|
|
1430
|
+
* changes its digest only to clear it.
|
|
1431
|
+
*/
|
|
1432
|
+
write(given, value) {
|
|
1433
|
+
this.#requireNotDestroyed("be changed");
|
|
1434
|
+
const name = attributeName(this.#model, given);
|
|
1435
|
+
const assigned = this.#attribute(name).assigned(value);
|
|
1436
|
+
this.#requireSameIfReadonly(name, assigned);
|
|
1437
|
+
const digest = digestOf(this.#model.attributeDefinitions, name);
|
|
1438
|
+
if (digest !== void 0 && assigned === "") return;
|
|
1439
|
+
if (digest !== void 0 && assigned === null) {
|
|
1440
|
+
this.#store(digest, null);
|
|
1441
|
+
if (this.#lifecycle === "persisted") this.#snapshot.set(name, null);
|
|
1442
|
+
}
|
|
1443
|
+
this.#store(name, assigned);
|
|
1250
1444
|
}
|
|
1251
|
-
|
|
1252
|
-
|
|
1445
|
+
/**
|
|
1446
|
+
* Writes a key copied from another record through `association`, cast as a condition's value
|
|
1447
|
+
* is (#94): an integer key into a text foreign key becomes its digits.
|
|
1448
|
+
*/
|
|
1449
|
+
writeKey(name, value, association) {
|
|
1450
|
+
this.#requireNotDestroyed("be changed");
|
|
1451
|
+
const key = this.#attribute(name).key(value, association);
|
|
1452
|
+
this.#requireSameIfReadonly(name, key);
|
|
1453
|
+
this.#store(name, key);
|
|
1253
1454
|
}
|
|
1254
|
-
/**
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1455
|
+
/**
|
|
1456
|
+
* Takes a value the database already holds, as stored, such as a count a counter cache changed
|
|
1457
|
+
* (#61): no read-only check (#358), and nothing to save.
|
|
1458
|
+
*/
|
|
1459
|
+
takeStored(given, value) {
|
|
1460
|
+
const name = attributeName(this.#model, given);
|
|
1461
|
+
this.#store(name, this.#attribute(name).assigned(value));
|
|
1462
|
+
this.commitColumns([name]);
|
|
1261
1463
|
}
|
|
1262
|
-
/**
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1464
|
+
/** Stores an attribute's already-cast value, as assigned. */
|
|
1465
|
+
#store(name, value) {
|
|
1466
|
+
this.#values[name] = value;
|
|
1467
|
+
this.#assigned.add(name);
|
|
1468
|
+
}
|
|
1469
|
+
/** Assigns several attributes; unknown names throw `UnknownAttribute`. */
|
|
1470
|
+
assign(attributes) {
|
|
1471
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
1472
|
+
attributeOf(this.#model, name);
|
|
1473
|
+
this.write(name, value);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
/** A new record: every attribute reads as null until assigned, and initial values are made now. */
|
|
1477
|
+
build(attributes, kept = {}) {
|
|
1478
|
+
for (const name of this.#model.attributeDefinitions.keys()) this.#values[name] = null;
|
|
1479
|
+
for (const [name, value] of initialValues(this.#model)) {
|
|
1480
|
+
this.write(name, value);
|
|
1481
|
+
this.#initialValues.set(name, this.#storedForm(name));
|
|
1482
|
+
}
|
|
1483
|
+
for (const [name, value] of Object.entries(kept)) this.#store(name, value);
|
|
1484
|
+
this.assign(attributes);
|
|
1485
|
+
generateTokens(this.#model.attributeDefinitions, this, "initialize");
|
|
1486
|
+
}
|
|
1487
|
+
/**
|
|
1488
|
+
* Points this record at `value` through `association`'s foreign key, keeping it loaded for its
|
|
1489
|
+
* key attributes (a polymorphic belongsTo's type is written before, by its `set`; #60).
|
|
1490
|
+
*/
|
|
1491
|
+
pointAt(association, key, value) {
|
|
1492
|
+
const { name, ownerAttribute, keyAttributes } = association;
|
|
1493
|
+
this.writeKey(ownerAttribute, key, name);
|
|
1494
|
+
this.associations.set(name, keyAttributes, value);
|
|
1495
|
+
}
|
|
1496
|
+
/** Forgets an association: what's loaded and its autosaves (`reloadAssociation`). */
|
|
1497
|
+
resetAssociation(name) {
|
|
1498
|
+
this.associations.clear(name);
|
|
1499
|
+
this.autosaves.clear(name);
|
|
1500
|
+
}
|
|
1501
|
+
/** Forgets every association, loaded and autosaved (`reload()`). */
|
|
1502
|
+
clearAssociations() {
|
|
1503
|
+
this.associations.clearAll();
|
|
1504
|
+
this.autosaves.clearAll();
|
|
1505
|
+
}
|
|
1506
|
+
/**
|
|
1507
|
+
* Loads a D1 row; the record becomes persisted and clean. Its virtual attributes, which no row
|
|
1508
|
+
* holds, start at their defaults, on a load or `reload` (#356); an insert's re-read
|
|
1509
|
+
* row keeps the values it was saved with (`keepVirtual`).
|
|
1510
|
+
*/
|
|
1511
|
+
loadRow(row, { keepVirtual = false } = {}) {
|
|
1512
|
+
let loadedCount = 0;
|
|
1513
|
+
for (const [name, definition] of this.#model.attributeDefinitions) {
|
|
1514
|
+
if (definition.virtual === true) {
|
|
1515
|
+
if (!keepVirtual) this.#values[name] = this.#virtualDefault(name, definition);
|
|
1516
|
+
continue;
|
|
1517
|
+
}
|
|
1518
|
+
const { column } = definition;
|
|
1519
|
+
if (!(column in row)) continue;
|
|
1520
|
+
this.#values[name] = this.#attribute(name).read(row[column]);
|
|
1521
|
+
loadedCount++;
|
|
1522
|
+
}
|
|
1523
|
+
this.#markSaved(row, loadedCount);
|
|
1524
|
+
}
|
|
1525
|
+
/** A virtual attribute's value on a loaded record: its default, cast and normalized, or null (#356). */
|
|
1526
|
+
#virtualDefault(name, definition) {
|
|
1527
|
+
return definition.default === void 0 ? null : this.#attribute(name).assigned(definition.default());
|
|
1528
|
+
}
|
|
1529
|
+
/** The loaded attributes as a plain object (the record's own values, not copies). */
|
|
1530
|
+
attributes() {
|
|
1531
|
+
return { ...this.#values };
|
|
1532
|
+
}
|
|
1533
|
+
/**
|
|
1534
|
+
* A new record's assigned attributes that differ from their initial values (a declared default
|
|
1535
|
+
* or generated id, else null), so its changes run from its defaults (#41); or a persisted
|
|
1536
|
+
* one's that differ from the Snapshot. Both compare stored forms.
|
|
1537
|
+
*/
|
|
1538
|
+
changedNames() {
|
|
1539
|
+
if (this.#lifecycle === "new") return [...this.#assigned].filter((name) => !isSameCell(this.#storedForm(name), this.#initialOf(name)));
|
|
1540
|
+
return Object.keys(this.#values).filter((name) => !this.#isInSnapshot(name) || !isSameCell(this.#snapshotOf(name), this.#storedForm(name)));
|
|
1541
|
+
}
|
|
1542
|
+
/** `{ attribute: [before, after] }`; both are copies, never the record's own objects. */
|
|
1543
|
+
changes() {
|
|
1544
|
+
const changes = {};
|
|
1545
|
+
for (const name of this.changedNames()) changes[name] = [this.#valueBefore(name), this.#copyOf(name)];
|
|
1546
|
+
return changes;
|
|
1547
|
+
}
|
|
1548
|
+
/** `changes()`, with `undefined` before an attribute the record never loaded (a partial record's). */
|
|
1549
|
+
changesSinceLoad() {
|
|
1550
|
+
return Object.fromEntries(Object.entries(this.changes()).map(([name, [before, after]]) => [name, [this.#isInSnapshot(name) ? before : void 0, after]]));
|
|
1551
|
+
}
|
|
1552
|
+
/** The changes the last successful save wrote (copies). */
|
|
1553
|
+
savedChanges() {
|
|
1554
|
+
return { ...this.#savedChanges };
|
|
1555
|
+
}
|
|
1556
|
+
/**
|
|
1557
|
+
* An attribute's value as last loaded or saved (a copy). A new record's is its initial
|
|
1558
|
+
* value (a default or generated id), or null, which `attributeWas` returns.
|
|
1559
|
+
*/
|
|
1560
|
+
valueInDatabase(given) {
|
|
1561
|
+
const name = attributeName(this.#model, given);
|
|
1562
|
+
this.read(name);
|
|
1563
|
+
return this.#valueBefore(name);
|
|
1564
|
+
}
|
|
1565
|
+
/**
|
|
1566
|
+
* Whether the attribute has an unsaved change, optionally `from` and `to` given values
|
|
1567
|
+
* (compared as stored, so dates by time and json by content).
|
|
1568
|
+
*/
|
|
1569
|
+
isAttributeChanged(given, options = {}) {
|
|
1570
|
+
const name = attributeName(this.#model, given);
|
|
1571
|
+
this.read(name);
|
|
1572
|
+
if (!this.changedNames().includes(name)) return false;
|
|
1573
|
+
return this.#isMatching(name, this.valueInDatabase(name), this.#values[name], options);
|
|
1574
|
+
}
|
|
1575
|
+
/** The last save's change to the attribute, or an alias's, `[before, after]`, if it changed it. */
|
|
1576
|
+
savedChangeOf(given) {
|
|
1577
|
+
const name = attributeName(this.#model, given);
|
|
1578
|
+
this.read(name);
|
|
1579
|
+
return this.#savedChanges[name];
|
|
1580
|
+
}
|
|
1581
|
+
/** Whether the last save changed the attribute, optionally `from` and `to` given values. */
|
|
1582
|
+
hasSavedChangeTo(given, options = {}) {
|
|
1583
|
+
const name = attributeName(this.#model, given);
|
|
1584
|
+
this.read(name);
|
|
1585
|
+
const change = this.#savedChanges[name];
|
|
1586
|
+
return change !== void 0 && this.#isMatching(name, change[0], change[1], options);
|
|
1587
|
+
}
|
|
1588
|
+
/** Puts unsaved changes back: to the Snapshot, or to a new record's initial values (or null). */
|
|
1589
|
+
restore(given = this.changedNames()) {
|
|
1590
|
+
this.#requireNotDestroyed("be changed");
|
|
1591
|
+
for (const name of given.map((each) => attributeName(this.#model, each))) {
|
|
1592
|
+
const attribute = this.#attribute(name);
|
|
1593
|
+
if (this.#lifecycle !== "new") {
|
|
1594
|
+
if (this.#isInSnapshot(name)) this.#values[name] = this.#snapshotValue(name);
|
|
1595
|
+
} else if (this.#initialValues.has(name)) this.#values[name] = attribute.read(this.#initialValues.get(name));
|
|
1596
|
+
else {
|
|
1597
|
+
this.#values[name] = null;
|
|
1598
|
+
this.#assigned.delete(name);
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1271
1601
|
}
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
return
|
|
1275
|
-
attribute,
|
|
1276
|
-
values,
|
|
1277
|
-
...failure
|
|
1278
|
-
});
|
|
1602
|
+
#isMatching(name, before, after, options) {
|
|
1603
|
+
const isSame = (a, b) => this.#attribute(name).isSame(a, b);
|
|
1604
|
+
return (!("from" in options) || isSame(options.from, before)) && (!("to" in options) || isSame(options.to, after));
|
|
1279
1605
|
}
|
|
1280
|
-
/**
|
|
1281
|
-
|
|
1282
|
-
this.#
|
|
1606
|
+
/** The attributes an insert writes: the assigned ones, so column defaults apply to the rest. */
|
|
1607
|
+
insertAssignments() {
|
|
1608
|
+
return this.#assignmentsOf([...this.#assigned]);
|
|
1283
1609
|
}
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
* The associations one record has loaded (preloaded, read, or set by a write): a record, an
|
|
1288
|
-
* array of records, or null. Each stays loaded while the owner's attribute still holds the key
|
|
1289
|
-
* it was loaded for, and a load overtaken by a change to its association isn't kept.
|
|
1290
|
-
*/
|
|
1291
|
-
var LoadedAssociations = class {
|
|
1292
|
-
/** The owner's attribute value now, which an entry's key is checked against. */
|
|
1293
|
-
#keyOf;
|
|
1294
|
-
#entries = /* @__PURE__ */ new Map();
|
|
1295
|
-
/** Counted when an association changes, by name and record-wide, so a stale load isn't kept. */
|
|
1296
|
-
#loadsDiscarded = /* @__PURE__ */ new Map();
|
|
1297
|
-
#allLoadsDiscarded = 0;
|
|
1298
|
-
constructor(keyOf) {
|
|
1299
|
-
this.#keyOf = keyOf;
|
|
1610
|
+
/** The attributes an update writes: the changed ones. */
|
|
1611
|
+
updateAssignments() {
|
|
1612
|
+
return this.#assignmentsOf(this.changedNames());
|
|
1300
1613
|
}
|
|
1301
|
-
/** The
|
|
1302
|
-
get(
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1614
|
+
/** The primary key as stored in D1: the row's key before any unsaved change to it. */
|
|
1615
|
+
get storedKey() {
|
|
1616
|
+
return this.#snapshotOf(this.#model.primaryKey);
|
|
1617
|
+
}
|
|
1618
|
+
/** The stored primary key as a JavaScript value, e.g. to find the row again. */
|
|
1619
|
+
get storedKeyValue() {
|
|
1620
|
+
return this.#attribute(this.#model.primaryKey).read(this.storedKey);
|
|
1306
1621
|
}
|
|
1307
1622
|
/**
|
|
1308
|
-
*
|
|
1309
|
-
*
|
|
1623
|
+
* After an insert: load what D1 stored. `savedChanges` is the changes the insert wrote, and
|
|
1624
|
+
* what the write or the database filled in (a generated key, timestamps, a column default),
|
|
1625
|
+
* each from its value before: a declared default the record kept isn't one (#41).
|
|
1310
1626
|
*/
|
|
1311
|
-
|
|
1312
|
-
this
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
value
|
|
1316
|
-
});
|
|
1317
|
-
|
|
1627
|
+
commitInsert(row) {
|
|
1628
|
+
const written = new Set(this.changedNames());
|
|
1629
|
+
const before = new Map(Object.keys(this.#values).map((name) => [name, {
|
|
1630
|
+
stored: this.#storedForm(name),
|
|
1631
|
+
value: this.#valueBefore(name)
|
|
1632
|
+
}]));
|
|
1633
|
+
if (row === void 0) {
|
|
1634
|
+
this.#settle();
|
|
1635
|
+
this.#markSaved();
|
|
1636
|
+
} else this.loadRow(row, { keepVirtual: true });
|
|
1637
|
+
this.#savedChanges = Object.fromEntries([...before].filter(([name, { stored }]) => written.has(name) || !isSameCell(this.#storedForm(name), stored)).map(([name, { value }]) => [name, [value, this.#copyOf(name)]]));
|
|
1638
|
+
this.isPreviouslyNew = true;
|
|
1318
1639
|
}
|
|
1319
1640
|
/**
|
|
1320
|
-
*
|
|
1321
|
-
*
|
|
1322
|
-
*
|
|
1641
|
+
* After an update: apply values the write added (timestamps), then record the save. Every
|
|
1642
|
+
* attribute holds what was stored (a `unixSeconds` Date has no milliseconds, a `json` NaN is
|
|
1643
|
+
* null), as an insert's re-read row gives (#73, #163), including one assigned a value equal
|
|
1644
|
+
* as stored, which wasn't a change.
|
|
1323
1645
|
*/
|
|
1324
|
-
|
|
1325
|
-
const
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
value
|
|
1331
|
-
});
|
|
1332
|
-
};
|
|
1333
|
-
}
|
|
1334
|
-
/** Forgets the association; loads of it already sent won't be kept. */
|
|
1335
|
-
clear(name) {
|
|
1336
|
-
this.#entries.delete(name);
|
|
1337
|
-
this.#discardPendingLoads(name);
|
|
1338
|
-
}
|
|
1339
|
-
/** Forgets every association (`reload()`); loads already sent won't be kept. */
|
|
1340
|
-
clearAll() {
|
|
1341
|
-
this.#entries.clear();
|
|
1342
|
-
this.#allLoadsDiscarded += 1;
|
|
1343
|
-
}
|
|
1344
|
-
#key(attribute) {
|
|
1345
|
-
if (typeof attribute !== "string") return attribute.map((each) => this.#keyOf(each) ?? null);
|
|
1346
|
-
return this.#keyOf(attribute) ?? null;
|
|
1646
|
+
commitUpdate(added) {
|
|
1647
|
+
for (const [name, value] of added) this.#values[name] = value;
|
|
1648
|
+
this.#settle();
|
|
1649
|
+
this.#savedChanges = this.changes();
|
|
1650
|
+
this.#markSaved();
|
|
1651
|
+
this.isPreviouslyNew = false;
|
|
1347
1652
|
}
|
|
1348
|
-
|
|
1349
|
-
|
|
1653
|
+
/**
|
|
1654
|
+
* After a write straight to the row (`updateColumns`, #351): these attributes hold what was
|
|
1655
|
+
* stored, or what `row` returned for them, and have no unsaved change. The rest, and
|
|
1656
|
+
* `savedChanges`, are as they were.
|
|
1657
|
+
*/
|
|
1658
|
+
commitColumns(given, row = {}) {
|
|
1659
|
+
for (const name of given.map((each) => attributeName(this.#model, each))) {
|
|
1660
|
+
const { column } = this.#definition(name);
|
|
1661
|
+
const attribute = this.#attribute(name);
|
|
1662
|
+
this.#values[name] = column in row ? attribute.read(row[column]) : this.#copyOf(name);
|
|
1663
|
+
this.#snapshot.set(name, this.#storedForm(name));
|
|
1664
|
+
}
|
|
1350
1665
|
}
|
|
1351
1666
|
/**
|
|
1352
|
-
*
|
|
1353
|
-
*
|
|
1667
|
+
* After a touch (#50): as `commitColumns`, and `savedChanges` holds only the touched
|
|
1668
|
+
* attributes' changes. The record's other changes stay unsaved.
|
|
1354
1669
|
*/
|
|
1355
|
-
|
|
1356
|
-
|
|
1670
|
+
commitTouch(given) {
|
|
1671
|
+
const names = given.map((each) => attributeName(this.#model, each));
|
|
1672
|
+
const changes = this.changes();
|
|
1673
|
+
this.commitColumns(names);
|
|
1674
|
+
this.#savedChanges = Object.fromEntries(names.filter((name) => Object.hasOwn(changes, name)).map((name) => [name, changes[name]]));
|
|
1357
1675
|
}
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
return now.every((each, index) => Object.is(each, loaded[index]));
|
|
1363
|
-
}
|
|
1364
|
-
// src/pending-autosaves.ts
|
|
1365
|
-
/**
|
|
1366
|
-
* The records saving one record may also save: those built through its hasMany and hasOne
|
|
1367
|
-
* associations or pushed onto a hasMany, after it (`autosavesOf` says which), and the new ones
|
|
1368
|
-
* its belongsTo associations point at, before it.
|
|
1369
|
-
*/
|
|
1370
|
-
var PendingAutosaves = class {
|
|
1371
|
-
#built = /* @__PURE__ */ new Map();
|
|
1372
|
-
#belongsTo = /* @__PURE__ */ new Map();
|
|
1373
|
-
/** A record built through one of this record's hasMany or hasOne associations. */
|
|
1374
|
-
addBuilt(name, record) {
|
|
1375
|
-
this.#built.set(name, [...this.#built.get(name) ?? [], record]);
|
|
1676
|
+
/** A save with nothing to write: nothing changes but `savedChanges`. */
|
|
1677
|
+
commitClean() {
|
|
1678
|
+
this.#savedChanges = {};
|
|
1679
|
+
this.isPreviouslyNew = false;
|
|
1376
1680
|
}
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
this.#built.set(name, kept);
|
|
1681
|
+
markDestroyed() {
|
|
1682
|
+
this.#lifecycle = "destroyed";
|
|
1683
|
+
this.isPreviouslyNew = false;
|
|
1381
1684
|
}
|
|
1382
|
-
/** A
|
|
1383
|
-
|
|
1384
|
-
this.#
|
|
1685
|
+
/** A destroyed record is a read-only snapshot. (A keyless record may change in memory.) */
|
|
1686
|
+
#requireNotDestroyed(action) {
|
|
1687
|
+
if (this.#lifecycle === "destroyed") throw new RecordDestroyed(`${this.#model.name} record was destroyed; it can't ${action}`);
|
|
1385
1688
|
}
|
|
1386
|
-
/**
|
|
1387
|
-
|
|
1388
|
-
|
|
1689
|
+
/**
|
|
1690
|
+
* A persisted record's read-only attribute takes only the value it holds, cast and normalized
|
|
1691
|
+
* (#358), so a form can post it back unchanged.
|
|
1692
|
+
*/
|
|
1693
|
+
#requireSameIfReadonly(name, assigned) {
|
|
1694
|
+
if (this.#definition(name).readonly !== true || this.#lifecycle !== "persisted") return;
|
|
1695
|
+
const before = this.#isInSnapshot(name) ? this.#snapshotValue(name) : void 0;
|
|
1696
|
+
if (before !== void 0 && this.#attribute(name).isSame(before, assigned)) return;
|
|
1697
|
+
throw new ReadonlyAttribute(this.#model.name, name);
|
|
1389
1698
|
}
|
|
1390
|
-
/**
|
|
1391
|
-
|
|
1392
|
-
|
|
1699
|
+
/** An unknown name is UnknownAttribute, even one every object inherits (`constructor`, #84). */
|
|
1700
|
+
#requireAttribute(name) {
|
|
1701
|
+
this.#definition(name);
|
|
1393
1702
|
}
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
if (record === void 0) this.#belongsTo.delete(name);
|
|
1397
|
-
else this.#belongsTo.set(name, record);
|
|
1703
|
+
#definition(name) {
|
|
1704
|
+
return attributeOf(this.#model, name);
|
|
1398
1705
|
}
|
|
1399
|
-
/** The
|
|
1400
|
-
|
|
1401
|
-
return
|
|
1706
|
+
/** The resolved attribute, so a refusal names the Model and attribute. */
|
|
1707
|
+
#attribute(name) {
|
|
1708
|
+
return attributeFor(this.#model, name);
|
|
1402
1709
|
}
|
|
1403
|
-
/**
|
|
1404
|
-
|
|
1405
|
-
return
|
|
1710
|
+
/** A new record's initial value as stored: its declared default or generated id, else null. */
|
|
1711
|
+
#initialOf(name) {
|
|
1712
|
+
return this.#initialValues.has(name) ? this.#initialValues.get(name) : this.#attribute(name).write(null);
|
|
1406
1713
|
}
|
|
1407
|
-
/**
|
|
1408
|
-
|
|
1409
|
-
this.#
|
|
1410
|
-
this.#
|
|
1714
|
+
/** The value before the unsaved change: as last loaded or saved, or a new record's initial one. */
|
|
1715
|
+
#valueBefore(name) {
|
|
1716
|
+
if (this.#isInSnapshot(name)) return this.#snapshotValue(name);
|
|
1717
|
+
return this.#attribute(name).read(this.#initialOf(name));
|
|
1411
1718
|
}
|
|
1412
|
-
/**
|
|
1413
|
-
|
|
1414
|
-
this.#
|
|
1415
|
-
this.#belongsTo.clear();
|
|
1719
|
+
/** An attribute's current value in its stored form, for comparing with the Snapshot. */
|
|
1720
|
+
#storedForm(name) {
|
|
1721
|
+
return this.#attribute(name).write(this.#values[name]);
|
|
1416
1722
|
}
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
/**
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
}
|
|
1425
|
-
/**
|
|
1426
|
-
* Everything one Record knows about itself: its attribute values, lifecycle, the attributes
|
|
1427
|
-
* assigned since it was built, its Snapshot (each loaded attribute's stored form as last loaded
|
|
1428
|
-
* or saved), the changes its last save wrote, and the Database it belongs to.
|
|
1429
|
-
*
|
|
1430
|
-
* This is the only module that reads or writes those fields. Every rule about them (unloaded
|
|
1431
|
-
* attributes, undefined values, destroyed records, change detection) is enforced here.
|
|
1432
|
-
*/
|
|
1433
|
-
var RecordState = class {
|
|
1434
|
-
#model;
|
|
1435
|
-
#executor;
|
|
1436
|
-
#lifecycle = "new";
|
|
1437
|
-
#values = {};
|
|
1438
|
-
#assigned = /* @__PURE__ */ new Set();
|
|
1439
|
-
#snapshot = /* @__PURE__ */ new Map();
|
|
1440
|
-
/** The row the Snapshot was loaded from: its entries for the row's cells are made on first use (#91). */
|
|
1441
|
-
#snapshotRow;
|
|
1442
|
-
/** A new record's initial values in stored form, which `restore` returns attributes to. */
|
|
1443
|
-
#initialValues = /* @__PURE__ */ new Map();
|
|
1444
|
-
#savedChanges = {};
|
|
1445
|
-
#errors;
|
|
1446
|
-
#abortReason;
|
|
1447
|
-
/** The associations it has loaded, each kept while its key is current. */
|
|
1448
|
-
associations = new LoadedAssociations((attribute) => this.#values[attribute]);
|
|
1449
|
-
/** The new records saving it also saves, built through or pointed at by its associations. */
|
|
1450
|
-
autosaves = new PendingAutosaves();
|
|
1451
|
-
/** Destroyed when its owner is saved through an `autosave: true` association (#37). */
|
|
1452
|
-
isMarkedForDestruction = false;
|
|
1453
|
-
/** Whether its last save created it, as Rails' `previously_new_record?` (#351). */
|
|
1454
|
-
isPreviouslyNew = false;
|
|
1455
|
-
/** The contexts it's being validated in, while it is (#51). */
|
|
1456
|
-
validationContexts = [];
|
|
1457
|
-
constructor(model) {
|
|
1458
|
-
this.#model = model;
|
|
1459
|
-
const read = (name) => Object.hasOwn(this.#values, name) ? this.#values[name] : null;
|
|
1460
|
-
this.#errors = new Errors({
|
|
1461
|
-
model,
|
|
1462
|
-
read
|
|
1463
|
-
});
|
|
1723
|
+
/** A copy of an attribute's current value, read back from its stored form. */
|
|
1724
|
+
#copyOf(name) {
|
|
1725
|
+
return this.#attribute(name).copy(this.#values[name]);
|
|
1726
|
+
}
|
|
1727
|
+
/** Every loaded attribute becomes what D1 holds for it: its value, written and read back. */
|
|
1728
|
+
#settle() {
|
|
1729
|
+
for (const name of Object.keys(this.#values)) this.#values[name] = this.#copyOf(name);
|
|
1464
1730
|
}
|
|
1465
|
-
/** The
|
|
1466
|
-
|
|
1467
|
-
return this.#
|
|
1731
|
+
/** The columns among `names` and their values: a virtual attribute has no column (#356). */
|
|
1732
|
+
#assignmentsOf(names) {
|
|
1733
|
+
return names.filter((name) => this.#definition(name).virtual !== true).map((name) => [name, this.#values[name]]);
|
|
1468
1734
|
}
|
|
1469
|
-
/**
|
|
1470
|
-
|
|
1471
|
-
|
|
1735
|
+
/**
|
|
1736
|
+
* The current values become the Snapshot: persisted and clean. After a load, the entries for
|
|
1737
|
+
* `row`'s cells (`loadedCount` of the values) are made only when first used, so a record that's
|
|
1738
|
+
* only read never writes them (#91).
|
|
1739
|
+
*/
|
|
1740
|
+
#markSaved(row, loadedCount = 0) {
|
|
1741
|
+
this.#snapshot.clear();
|
|
1742
|
+
this.#snapshotRow = row;
|
|
1743
|
+
const names = Object.keys(this.#values);
|
|
1744
|
+
if (names.length !== loadedCount) {
|
|
1745
|
+
for (const name of names) if (!this.#isInRow(name)) this.#snapshot.set(name, this.#storedForm(name));
|
|
1746
|
+
}
|
|
1747
|
+
this.#lifecycle = "persisted";
|
|
1748
|
+
this.#assigned.clear();
|
|
1472
1749
|
}
|
|
1473
|
-
|
|
1474
|
-
|
|
1750
|
+
/** Whether the Snapshot has `name`: it was loaded or saved. */
|
|
1751
|
+
#isInSnapshot(name) {
|
|
1752
|
+
return this.#snapshot.has(name) || this.#isInRow(name);
|
|
1475
1753
|
}
|
|
1476
|
-
/**
|
|
1477
|
-
|
|
1478
|
-
|
|
1754
|
+
/** `name`'s Snapshot entry, its stored form, made from the loaded row's cell on first use. */
|
|
1755
|
+
#snapshotOf(name) {
|
|
1756
|
+
if (this.#snapshot.has(name) || !this.#isInRow(name)) return this.#snapshot.get(name);
|
|
1757
|
+
const attribute = this.#attribute(name);
|
|
1758
|
+
const stored = attribute.write(attribute.read(this.#snapshotRow[this.#definition(name).column]));
|
|
1759
|
+
this.#snapshot.set(name, stored);
|
|
1760
|
+
return stored;
|
|
1479
1761
|
}
|
|
1480
|
-
/**
|
|
1481
|
-
|
|
1482
|
-
return this.#
|
|
1762
|
+
/** `name`'s value as loaded or saved: a fresh copy, read back from its Snapshot entry. */
|
|
1763
|
+
#snapshotValue(name) {
|
|
1764
|
+
return this.#attribute(name).read(this.#snapshotOf(name));
|
|
1483
1765
|
}
|
|
1484
|
-
/**
|
|
1485
|
-
|
|
1486
|
-
|
|
1766
|
+
/** Whether the row the Snapshot was loaded from has a cell for `name`. */
|
|
1767
|
+
#isInRow(name) {
|
|
1768
|
+
const { column, virtual } = this.#definition(name);
|
|
1769
|
+
return this.#snapshotRow !== void 0 && virtual !== true && column in this.#snapshotRow;
|
|
1487
1770
|
}
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1771
|
+
};
|
|
1772
|
+
// src/secure-passwords.ts
|
|
1773
|
+
/** Whether a declared attribute is a secure password: an object with `securePassword` and no type. */
|
|
1774
|
+
function isSecurePasswordDeclaration(declared) {
|
|
1775
|
+
return typeof declared === "object" && declared !== null && "securePassword" in declared && !("type" in declared);
|
|
1776
|
+
}
|
|
1777
|
+
/** The names of the attributes a secure password adds besides itself. */
|
|
1778
|
+
function companionsOf(name) {
|
|
1779
|
+
return [`${name}Confirmation`, `${name}Challenge`];
|
|
1780
|
+
}
|
|
1781
|
+
/**
|
|
1782
|
+
* The attributes that hold a secret, which `toJSON()` leaves out and every filter hides, whatever
|
|
1783
|
+
* `hiddenAttributes` and `filterAttributes` say: each secure password, its confirmation, its
|
|
1784
|
+
* challenge and its digest, and each secure token.
|
|
1785
|
+
*/
|
|
1786
|
+
function secretAttributeNames(definitions) {
|
|
1787
|
+
let names = secretNames.get(definitions);
|
|
1788
|
+
if (names === void 0) {
|
|
1789
|
+
names = secretsIn(definitions);
|
|
1790
|
+
secretNames.set(definitions, names);
|
|
1491
1791
|
}
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1792
|
+
return names;
|
|
1793
|
+
}
|
|
1794
|
+
/**
|
|
1795
|
+
* Each Model's secret attributes, by its attributes' definitions, which don't change once made.
|
|
1796
|
+
* Module-level, but not request state (ADR-0003): derived only from the classes.
|
|
1797
|
+
*/
|
|
1798
|
+
const secretNames = /* @__PURE__ */ new WeakMap();
|
|
1799
|
+
function secretsIn(definitions) {
|
|
1800
|
+
const passwords = securePasswordsOf(definitions).flatMap(({ name, digest }) => [
|
|
1801
|
+
name,
|
|
1802
|
+
...companionsOf(name),
|
|
1803
|
+
digest
|
|
1804
|
+
]);
|
|
1805
|
+
const tokens = [...definitions].filter(([, { secureToken }]) => secureToken !== void 0);
|
|
1806
|
+
return /* @__PURE__ */ new Set([...passwords, ...tokens.map(([name]) => name)]);
|
|
1807
|
+
}
|
|
1808
|
+
/**
|
|
1809
|
+
* The declared attributes with each secure password replaced by the virtual attributes it adds;
|
|
1810
|
+
* `where` names the Model in an error. One declared with another key, or alongside an attribute
|
|
1811
|
+
* it adds, is an `InvalidModel`.
|
|
1812
|
+
*/
|
|
1813
|
+
function withSecurePasswords(where, declared) {
|
|
1814
|
+
const expanded = {};
|
|
1815
|
+
for (const [name, each] of Object.entries(declared)) {
|
|
1816
|
+
if (!isSecurePasswordDeclaration(each)) {
|
|
1817
|
+
expanded[name] = each;
|
|
1818
|
+
continue;
|
|
1819
|
+
}
|
|
1820
|
+
if (Object.keys(each).length !== 1 || !isSecurePasswordOption(each.securePassword)) throw new InvalidModel(`${where}${name}: a secure password is { securePassword: true }, or { securePassword: { algorithm?, validations? } }`);
|
|
1821
|
+
expanded[name] = {
|
|
1822
|
+
type: "string",
|
|
1823
|
+
virtual: true,
|
|
1824
|
+
securePassword: each.securePassword
|
|
1825
|
+
};
|
|
1826
|
+
for (const companion of companionsOf(name)) {
|
|
1827
|
+
if (Object.hasOwn(declared, companion)) throw new InvalidModel(`${where}${companion} is declared by the secure password ${name}, so leave it out`);
|
|
1828
|
+
expanded[companion] = {
|
|
1829
|
+
type: "string",
|
|
1830
|
+
virtual: true
|
|
1831
|
+
};
|
|
1497
1832
|
}
|
|
1498
|
-
return this.#executor;
|
|
1499
1833
|
}
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1834
|
+
return expanded;
|
|
1835
|
+
}
|
|
1836
|
+
/** How long a reset token lasts unless `expiresIn` says. */
|
|
1837
|
+
const RESET_TOKEN_EXPIRES_IN = { minutes: 15 };
|
|
1838
|
+
function resetTokenExpiryOf(option) {
|
|
1839
|
+
if (option === false) return void 0;
|
|
1840
|
+
return (option === true ? void 0 : option?.expiresIn) ?? RESET_TOKEN_EXPIRES_IN;
|
|
1841
|
+
}
|
|
1842
|
+
/**
|
|
1843
|
+
* The tokens each secure password with a reset token has (#517): `passwordReset` for
|
|
1844
|
+
* `password`, lasting its `expiresIn`, which stop finding their record once its digest changes.
|
|
1845
|
+
*/
|
|
1846
|
+
function resetTokenDefinitions(definitions) {
|
|
1847
|
+
return securePasswordsOf(definitions).flatMap(({ name, digest, resetTokenExpiresIn }) => resetTokenExpiresIn === void 0 ? [] : [{
|
|
1848
|
+
purpose: resetPurposeOf(name),
|
|
1849
|
+
expiresIn: resetTokenExpiresIn,
|
|
1850
|
+
dependsOn: (record) => record[state].read(digest)
|
|
1851
|
+
}]);
|
|
1852
|
+
}
|
|
1853
|
+
/** A Model's secure passwords, from its attributes' definitions. */
|
|
1854
|
+
function securePasswordsOf(definitions) {
|
|
1855
|
+
return [...definitions].flatMap(([name, { securePassword }]) => {
|
|
1856
|
+
if (securePassword === void 0) return [];
|
|
1857
|
+
const options = securePassword === true ? {} : securePassword;
|
|
1858
|
+
return [{
|
|
1859
|
+
name,
|
|
1860
|
+
digest: digestOf(definitions, name),
|
|
1861
|
+
challenge: companionsOf(name)[1],
|
|
1862
|
+
algorithm: options.algorithm ?? pbkdf2Sha256,
|
|
1863
|
+
isValidated: options.validations !== false,
|
|
1864
|
+
resetTokenExpiresIn: resetTokenExpiryOf(options.resetToken)
|
|
1865
|
+
}];
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* Whether `given` is the password a record's digest was made from: never for a value that isn't a
|
|
1870
|
+
* string. Without a digest, the password is hashed anyway, so the time taken doesn't say whether the record has one.
|
|
1871
|
+
*/
|
|
1872
|
+
async function isPasswordOf({ digest, algorithm }, record, given) {
|
|
1873
|
+
if (typeof given !== "string") return false;
|
|
1874
|
+
const stored = record[state].read(digest);
|
|
1875
|
+
if (isPresent(stored)) return algorithm.verify(given, stored);
|
|
1876
|
+
await algorithm.hash(given);
|
|
1877
|
+
return false;
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* Defines each secure password's methods on a Model's prototype: `authenticatePassword`, and
|
|
1881
|
+
* `authenticate` for `password`, resolving to the record when the password is the one its
|
|
1882
|
+
* digest was made from, and `false` when it isn't, or there's no digest.
|
|
1883
|
+
*/
|
|
1884
|
+
function defineSecurePasswordMethods(prototype, definitions) {
|
|
1885
|
+
for (const password of securePasswordsOf(definitions)) {
|
|
1886
|
+
async function authenticate(given) {
|
|
1887
|
+
return await isPasswordOf(password, this, given) ? this : false;
|
|
1888
|
+
}
|
|
1889
|
+
const { name } = password;
|
|
1890
|
+
const names = [`authenticate${capitalized(name)}`, ...name === "password" ? ["authenticate"] : []];
|
|
1891
|
+
for (const method of names) Object.defineProperty(prototype, method, {
|
|
1892
|
+
value: authenticate,
|
|
1893
|
+
configurable: true,
|
|
1894
|
+
writable: true
|
|
1895
|
+
});
|
|
1896
|
+
if (password.resetTokenExpiresIn !== void 0) defineResetToken(prototype, name);
|
|
1508
1897
|
}
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1898
|
+
}
|
|
1899
|
+
/** The purpose a secure password's reset tokens are for: `passwordReset` for `password`. */
|
|
1900
|
+
function resetPurposeOf(name) {
|
|
1901
|
+
return `${name}Reset`;
|
|
1902
|
+
}
|
|
1903
|
+
/** `passwordResetToken()` on a Model's prototype: a token for `passwordReset`. */
|
|
1904
|
+
function defineResetToken(prototype, name) {
|
|
1905
|
+
function resetToken() {
|
|
1906
|
+
return this.generateTokenFor(resetPurposeOf(name));
|
|
1513
1907
|
}
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1908
|
+
Object.defineProperty(prototype, `${name}ResetToken`, {
|
|
1909
|
+
value: resetToken,
|
|
1910
|
+
configurable: true,
|
|
1911
|
+
writable: true
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1914
|
+
/** The digest's attribute, when `name` is a secure password: `passwordDigest` for `password`. */
|
|
1915
|
+
function digestOf(definitions, name) {
|
|
1916
|
+
return definitions.get(name)?.securePassword === void 0 ? void 0 : `${name}Digest`;
|
|
1917
|
+
}
|
|
1918
|
+
/**
|
|
1919
|
+
* Hashes each secure password changed since the record was loaded or saved into its digest,
|
|
1920
|
+
* as the record is saved; `""` and null leave the digest to their assignment. A save runs it
|
|
1921
|
+
* before and after its before-callbacks, so a password one assigns is hashed too, and one it
|
|
1922
|
+
* already hashed, into the digest the record holds, isn't hashed again.
|
|
1923
|
+
*/
|
|
1924
|
+
async function hashPasswords(definitions, record) {
|
|
1925
|
+
for (const password of securePasswordsOf(definitions)) {
|
|
1926
|
+
const waiting = waitingPassword(password, record);
|
|
1927
|
+
if (waiting === void 0 || isHashedAlready(record, password, waiting)) continue;
|
|
1928
|
+
const digest = await password.algorithm.hash(waiting);
|
|
1929
|
+
record.write(password.digest, digest);
|
|
1930
|
+
hashedOf(record).set(password.name, {
|
|
1931
|
+
password: waiting,
|
|
1932
|
+
digest
|
|
1933
|
+
});
|
|
1517
1934
|
}
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1935
|
+
}
|
|
1936
|
+
/**
|
|
1937
|
+
* Each record's passwords as last hashed, with the digests they made, so a save doesn't hash one
|
|
1938
|
+
* again. Module-level, but not request state (ADR-0003): keyed by the record itself.
|
|
1939
|
+
*/
|
|
1940
|
+
const hashed = /* @__PURE__ */ new WeakMap();
|
|
1941
|
+
function hashedOf(record) {
|
|
1942
|
+
let byName = hashed.get(record);
|
|
1943
|
+
if (byName === void 0) {
|
|
1944
|
+
byName = /* @__PURE__ */ new Map();
|
|
1945
|
+
hashed.set(record, byName);
|
|
1523
1946
|
}
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1947
|
+
return byName;
|
|
1948
|
+
}
|
|
1949
|
+
/** Whether `password` was hashed into the digest the record holds now. */
|
|
1950
|
+
function isHashedAlready(record, { name, digest }, password) {
|
|
1951
|
+
const last = hashed.get(record)?.get(name);
|
|
1952
|
+
return last !== void 0 && last.password === password && last.digest === record.read(digest);
|
|
1953
|
+
}
|
|
1954
|
+
/**
|
|
1955
|
+
* The failures of each validated secure password, in order: blank while it has no digest and no
|
|
1956
|
+
* password waiting to be hashed; its challenge, when given, invalid unless it's the password the
|
|
1957
|
+
* digest as last saved was made from; and too long past its algorithm's `maxBytes`, in UTF-8.
|
|
1958
|
+
*/
|
|
1959
|
+
async function passwordFailures(definitions, values) {
|
|
1960
|
+
const failures = [];
|
|
1961
|
+
for (const password of securePasswordsOf(definitions)) {
|
|
1962
|
+
if (!password.isValidated) continue;
|
|
1963
|
+
const waiting = waitingPassword(password, values);
|
|
1964
|
+
if (waiting === void 0 && !isPresent(values.read(password.digest))) failures.push({
|
|
1965
|
+
attribute: password.name,
|
|
1966
|
+
type: "blank"
|
|
1967
|
+
});
|
|
1968
|
+
if (!await isChallengeMet(password, values)) failures.push({
|
|
1969
|
+
attribute: password.challenge,
|
|
1970
|
+
type: "invalid"
|
|
1971
|
+
});
|
|
1972
|
+
const { maxBytes } = password.algorithm;
|
|
1973
|
+
if (maxBytes !== void 0 && new TextEncoder().encode(waiting).length > maxBytes) failures.push({
|
|
1974
|
+
attribute: password.name,
|
|
1975
|
+
type: "passwordTooLong"
|
|
1976
|
+
});
|
|
1531
1977
|
}
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1978
|
+
return failures;
|
|
1979
|
+
}
|
|
1980
|
+
/**
|
|
1981
|
+
* The password a save hashes, if any: one assigned since the record was loaded or saved, or
|
|
1982
|
+
* since null cleared its digest.
|
|
1983
|
+
*/
|
|
1984
|
+
function waitingPassword({ name }, values) {
|
|
1985
|
+
const password = values.read(name);
|
|
1986
|
+
if (typeof password !== "string" || password === "") return void 0;
|
|
1987
|
+
return values.isAttributeChanged(name) ? password : void 0;
|
|
1988
|
+
}
|
|
1989
|
+
/** Whether a digest is there to check a password against: a string that isn't blank. */
|
|
1990
|
+
function isPresent(digest) {
|
|
1991
|
+
return typeof digest === "string" && digest.trim() !== "";
|
|
1992
|
+
}
|
|
1993
|
+
/** Whether a challenge, if given, is the password the digest as last saved was made from. */
|
|
1994
|
+
async function isChallengeMet({ challenge: attribute, digest, algorithm }, values) {
|
|
1995
|
+
const challenge = values.read(attribute);
|
|
1996
|
+
if (typeof challenge !== "string") return true;
|
|
1997
|
+
const saved = values.valueInDatabase(digest);
|
|
1998
|
+
return isPresent(saved) && await algorithm.verify(challenge, saved);
|
|
1999
|
+
}
|
|
2000
|
+
/**
|
|
2001
|
+
* The `confirmation` rule of each validated secure password: its confirmation, when given, must
|
|
2002
|
+
* match it.
|
|
2003
|
+
*/
|
|
2004
|
+
function passwordConfirmations(definitions) {
|
|
2005
|
+
return securePasswordsOf(definitions).filter(({ isValidated }) => isValidated).map(({ name }) => ({
|
|
2006
|
+
attribute: name,
|
|
2007
|
+
options: {
|
|
2008
|
+
confirmation: true,
|
|
2009
|
+
allowNull: true
|
|
2010
|
+
}
|
|
2011
|
+
}));
|
|
2012
|
+
}
|
|
2013
|
+
/**
|
|
2014
|
+
* A secure password's digest must be a string attribute with a column: `passwordDigest` for
|
|
2015
|
+
* `password`; `InvalidModel` names the attribute it needs, after `where` (`User.`, or nothing).
|
|
2016
|
+
*/
|
|
2017
|
+
function rejectMissingDigests(where, definitions) {
|
|
2018
|
+
for (const { name, digest } of securePasswordsOf(definitions)) {
|
|
2019
|
+
const definition = definitions.get(digest);
|
|
2020
|
+
if (definition === void 0 || definition.virtual === true || definition.type !== "string") throw new InvalidModel(`${where}${name} is a secure password, so it needs a string attribute ${digest} with a column to keep its digest in`);
|
|
1539
2021
|
}
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
2022
|
+
}
|
|
2023
|
+
/**
|
|
2024
|
+
* The record the attributes that aren't secure passwords find, if each secure password given is
|
|
2025
|
+
* its own; null otherwise, or for a blank password, which finds nothing. When none is found, each
|
|
2026
|
+
* password is hashed anyway, so the time it takes doesn't say whether a record exists.
|
|
2027
|
+
*/
|
|
2028
|
+
async function authenticateBy(model, attributes) {
|
|
2029
|
+
const { passwords, identifiers } = splitPasswords(model, attributes);
|
|
2030
|
+
if (passwords.some(([, value]) => typeof value !== "string" || value === "")) return null;
|
|
2031
|
+
const record = await model.findBy(identifiers);
|
|
2032
|
+
if (record === null) {
|
|
2033
|
+
for (const [{ algorithm }, value] of passwords) await algorithm.hash(value);
|
|
2034
|
+
return null;
|
|
2035
|
+
}
|
|
2036
|
+
let matches = 0;
|
|
2037
|
+
for (const [password, value] of passwords) if (await isPasswordOf(password, record, value)) matches += 1;
|
|
2038
|
+
return matches === passwords.length ? record : null;
|
|
2039
|
+
}
|
|
2040
|
+
/**
|
|
2041
|
+
* `authenticateBy`'s attributes: its secure passwords, with their values, and the rest, which
|
|
2042
|
+
* find the record. Either missing is a `TypeError`.
|
|
2043
|
+
*/
|
|
2044
|
+
function splitPasswords(model, attributes) {
|
|
2045
|
+
const all = securePasswordsOf(model.attributeDefinitions);
|
|
2046
|
+
const passwords = all.flatMap((password) => Object.hasOwn(attributes, password.name) ? [[password, attributes[password.name]]] : []);
|
|
2047
|
+
const identifiers = Object.fromEntries(Object.entries(attributes).filter(([name]) => !all.some((each) => each.name === name)));
|
|
2048
|
+
if (passwords.length === 0) {
|
|
2049
|
+
const names = all.map(({ name }) => name).join(", ") || "none declared";
|
|
2050
|
+
throw new TypeError(`${model.name}.authenticateBy needs a secure password: ${names}`);
|
|
2051
|
+
}
|
|
2052
|
+
if (Object.keys(identifiers).length === 0) throw new TypeError(`${model.name}.authenticateBy needs an attribute to find the record by`);
|
|
2053
|
+
return {
|
|
2054
|
+
passwords,
|
|
2055
|
+
identifiers
|
|
2056
|
+
};
|
|
2057
|
+
}
|
|
2058
|
+
// src/filtering.ts
|
|
2059
|
+
/** What a listener sees in place of a filtered attribute's value (#83). */
|
|
2060
|
+
const FILTERED = "[FILTERED]";
|
|
2061
|
+
/**
|
|
2062
|
+
* The default filter list, of common secret and personal names: an attribute whose snake_case name
|
|
2063
|
+
* holds one of these, in any case, is filtered (`passwordDigest`, `apiKey`, `resetToken`, `email`).
|
|
2064
|
+
*/
|
|
2065
|
+
const defaultFilterAttributes = [
|
|
2066
|
+
"passw",
|
|
2067
|
+
"email",
|
|
2068
|
+
"secret",
|
|
2069
|
+
"token",
|
|
2070
|
+
"_key",
|
|
2071
|
+
"crypt",
|
|
2072
|
+
"salt",
|
|
2073
|
+
"certificate",
|
|
2074
|
+
"otp",
|
|
2075
|
+
"ssn",
|
|
2076
|
+
"cvv",
|
|
2077
|
+
"cvc"
|
|
2078
|
+
];
|
|
2079
|
+
/**
|
|
2080
|
+
* Whether an attribute's values are hidden from listeners: a secure password's or a secure
|
|
2081
|
+
* token's always (#355); any other when its snake_case name, or an alias's,
|
|
2082
|
+
* holds a string entry lowercased, or read in snake_case (`"apiKey"` as `api_key`, #397), or a
|
|
2083
|
+
* RegExp entry matches the name or its snake_case form.
|
|
2084
|
+
*/
|
|
2085
|
+
function isFiltered(model, given) {
|
|
2086
|
+
const attribute = attributeName(model, given);
|
|
2087
|
+
if (secretAttributeNames(model.attributeDefinitions).has(attribute)) return true;
|
|
2088
|
+
return [attribute, ...[...model.attributeAliases].filter(([, name]) => name === attribute).map(([alias]) => alias)].some((name) => isNameFiltered(model, name));
|
|
2089
|
+
}
|
|
2090
|
+
/** Whether one name matches an entry of `model`'s `filterAttributes`. */
|
|
2091
|
+
function isNameFiltered(model, attribute) {
|
|
2092
|
+
const name = underscore(attribute);
|
|
2093
|
+
return model.filterAttributes.some((entry) => typeof entry === "string" ? name.includes(entry.toLowerCase()) || name.includes(underscore(entry)) : attribute.search(entry) !== -1 || name.search(entry) !== -1);
|
|
2094
|
+
}
|
|
2095
|
+
/** The values of `attributes` that `model` filters, so a message can tell not to show them. */
|
|
2096
|
+
function filteredValues(model, attributes) {
|
|
2097
|
+
return Object.entries(attributes).filter(([name]) => isFiltered(model, name)).map(([, value]) => value);
|
|
2098
|
+
}
|
|
2099
|
+
/** A bound value D1 gets as `value`, and listeners see as `shown`. */
|
|
2100
|
+
var Filtered = class {
|
|
2101
|
+
value;
|
|
2102
|
+
shown;
|
|
2103
|
+
constructor(value, shown = FILTERED) {
|
|
2104
|
+
this.value = value;
|
|
2105
|
+
this.shown = shown;
|
|
1544
2106
|
}
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
2107
|
+
};
|
|
2108
|
+
/** A primary key as an error shows it: "[FILTERED]" when the key is a filtered attribute. */
|
|
2109
|
+
function shownKey(model, key) {
|
|
2110
|
+
return isFiltered(model, model.primaryKey) ? FILTERED : String(key);
|
|
2111
|
+
}
|
|
2112
|
+
/** The value D1 binds. */
|
|
2113
|
+
function boundValue(param) {
|
|
2114
|
+
return param instanceof Filtered ? param.value : param;
|
|
2115
|
+
}
|
|
2116
|
+
/** The value listeners see. */
|
|
2117
|
+
function shownValue(param) {
|
|
2118
|
+
return param instanceof Filtered ? param.shown : param;
|
|
2119
|
+
}
|
|
2120
|
+
// src/thenables.ts
|
|
2121
|
+
/**
|
|
2122
|
+
* Throws `TypeError` with `message` if `result` is a promise or another thenable, which the code
|
|
2123
|
+
* that ran it can't await. Its rejection, if it has one, is handled here, so the `TypeError` is
|
|
2124
|
+
* the only error.
|
|
2125
|
+
*/
|
|
2126
|
+
function refuseThenable(result, message) {
|
|
2127
|
+
if (!isThenable(result)) return;
|
|
2128
|
+
Promise.resolve(result).catch(() => {});
|
|
2129
|
+
throw new TypeError(message);
|
|
2130
|
+
}
|
|
2131
|
+
/** Whether `value` is a Promise or another thenable, as an async function returns. */
|
|
2132
|
+
function isThenable(value) {
|
|
2133
|
+
return typeof value?.then === "function";
|
|
2134
|
+
}
|
|
2135
|
+
// src/normalization.ts
|
|
2136
|
+
/** The normalizations declared for one of `model`'s attributes, a parent class's first. */
|
|
2137
|
+
function normalizationsOf(model, attribute) {
|
|
2138
|
+
return declared(model, "normalizations").filter((each) => each.attribute === attribute);
|
|
2139
|
+
}
|
|
2140
|
+
/** Refuses a normalization of a name that isn't one of `model`'s attributes. */
|
|
2141
|
+
function checkNormalizedName(model, attribute) {
|
|
2142
|
+
if (model.attributeDefinitions.has(attribute)) return;
|
|
2143
|
+
throw new InvalidModel(`${model.name} normalizes "${attribute}", which isn't one of its attributes`);
|
|
2144
|
+
}
|
|
2145
|
+
// src/lock-version.ts
|
|
2146
|
+
/** The attribute whose presence, as an integer, turns locking on. */
|
|
2147
|
+
const lockVersion = "lockVersion";
|
|
2148
|
+
/** Whether the Model's records lock: it has an integer `lockVersion`. */
|
|
2149
|
+
function isLocking(model) {
|
|
2150
|
+
return model.attributeDefinitions.get(lockVersion)?.type === "integer";
|
|
2151
|
+
}
|
|
2152
|
+
/** Whether `name` is the Model's lock version, which a NULL in its row reads as 0. */
|
|
2153
|
+
function isLockVersion(model, name) {
|
|
2154
|
+
return name === "lockVersion" && isLocking(model);
|
|
2155
|
+
}
|
|
2156
|
+
// src/attribute.ts
|
|
2157
|
+
/** What `condition()` and `conditionCell()` give for a value that can't match anything. */
|
|
2158
|
+
const noMatch = uncastable;
|
|
2159
|
+
/**
|
|
2160
|
+
* One attribute's value path, resolved once per Model class by `attributeFor` (#163). Which
|
|
2161
|
+
* Model and attribute a refusal names, whether the attribute is filtered, and how its type is
|
|
2162
|
+
* stored are known here, so a caller only says what the value is for.
|
|
2163
|
+
*/
|
|
2164
|
+
var Attribute = class {
|
|
2165
|
+
#model;
|
|
2166
|
+
name;
|
|
2167
|
+
#definition;
|
|
2168
|
+
/** @internal Use `attributeFor`. */
|
|
2169
|
+
constructor(model, name) {
|
|
2170
|
+
this.#model = model;
|
|
2171
|
+
this.name = name;
|
|
2172
|
+
this.#definition = attributeOf(model, name);
|
|
1551
2173
|
}
|
|
1552
|
-
/**
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
for (const [name, value] of initialValues(this.#model)) {
|
|
1556
|
-
this.write(name, value);
|
|
1557
|
-
this.#initialValues.set(name, this.#storedForm(name));
|
|
1558
|
-
}
|
|
1559
|
-
this.assign(attributes);
|
|
2174
|
+
/** Its column; `UnknownAttribute` for a virtual attribute, which has none (#356). */
|
|
2175
|
+
get column() {
|
|
2176
|
+
return columnOf(this.#model, this.name).column;
|
|
1560
2177
|
}
|
|
1561
|
-
/**
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
*/
|
|
1565
|
-
pointAt(association, key, value) {
|
|
1566
|
-
const { name, ownerAttribute, keyAttributes } = association;
|
|
1567
|
-
this.writeKey(ownerAttribute, key, name);
|
|
1568
|
-
this.associations.set(name, keyAttributes, value);
|
|
2178
|
+
/** Whether it's a `string` attribute, which D1 compares as text, not by number. */
|
|
2179
|
+
get isText() {
|
|
2180
|
+
return this.#definition.type === "string";
|
|
1569
2181
|
}
|
|
1570
|
-
/**
|
|
1571
|
-
|
|
1572
|
-
this.
|
|
1573
|
-
this.autosaves.clear(name);
|
|
2182
|
+
/** Whether it's a `binary` attribute, whose cells are bytes, which JSON can't hold (#20). */
|
|
2183
|
+
get isBinary() {
|
|
2184
|
+
return this.#definition.type === "binary";
|
|
1574
2185
|
}
|
|
1575
|
-
/**
|
|
1576
|
-
|
|
1577
|
-
this.
|
|
1578
|
-
this.autosaves.clearAll();
|
|
2186
|
+
/** Whether it's a `json` attribute, whose stored text is JSON itself. */
|
|
2187
|
+
get isJson() {
|
|
2188
|
+
return this.#definition.type === "json";
|
|
1579
2189
|
}
|
|
1580
|
-
/**
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
this.#
|
|
2190
|
+
/**
|
|
2191
|
+
* An assigned value (a setter, `build`, `update`, `insert`, `permit`) as the attribute's type:
|
|
2192
|
+
* only what's unambiguous for it (#81). `undefined` throws, as it would store NULL silently;
|
|
2193
|
+
* what can't be cast throws `TypeError` naming the Model and attribute, showing the value
|
|
2194
|
+
* unless the attribute is filtered (#83).
|
|
2195
|
+
*/
|
|
2196
|
+
cast(value) {
|
|
2197
|
+
requireDefined(this.#model, this.name, value);
|
|
2198
|
+
const cast = this.#convertOrRefuse(value, () => castValue(this.#definition, value));
|
|
2199
|
+
if (cast === uncastable && this.#isValidatedEnum()) return value;
|
|
2200
|
+
return this.#orRefuse(cast, value);
|
|
1590
2201
|
}
|
|
1591
|
-
/**
|
|
1592
|
-
|
|
1593
|
-
return
|
|
2202
|
+
/** An assigned value cast, then normalized (#354): what a record keeps for it. */
|
|
2203
|
+
assigned(value) {
|
|
2204
|
+
return this.normalize(this.cast(value));
|
|
1594
2205
|
}
|
|
1595
2206
|
/**
|
|
1596
|
-
*
|
|
1597
|
-
*
|
|
1598
|
-
*
|
|
2207
|
+
* An already cast value through each of the attribute's normalizations, in order (#354): null
|
|
2208
|
+
* only through those declared `applyToNull`. Each result is cast again, so it's a value of the
|
|
2209
|
+
* attribute's type, whatever plain JavaScript returns; a promise is refused.
|
|
1599
2210
|
*/
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
return Object.keys(this.#values).filter((name) => !this.#isInSnapshot(name) || !isSameCell(this.#snapshotOf(name), this.#storedForm(name)));
|
|
2211
|
+
normalize(value) {
|
|
2212
|
+
return this.#normalized(value, (result) => this.cast(result));
|
|
1603
2213
|
}
|
|
1604
|
-
/**
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
2214
|
+
/**
|
|
2215
|
+
* A condition's value cast as a condition's, then normalized as an assigned value is (#354), each
|
|
2216
|
+
* result cast again as a condition's, so a decimal's precision limits neither. A value that
|
|
2217
|
+
* can't be cast is kept as it was given, so it still matches nothing, and an error that names it
|
|
2218
|
+
* names what was given; a result that can't be cast throws, as assigning it would.
|
|
2219
|
+
*/
|
|
2220
|
+
normalizedCondition(value) {
|
|
2221
|
+
const cast = this.condition(value);
|
|
2222
|
+
if (cast === noMatch) return value;
|
|
2223
|
+
return this.#normalized(cast, (result) => this.#conditionResult(result));
|
|
2224
|
+
}
|
|
2225
|
+
/** A normalization's result as a condition's value; `TypeError` where assigning it would be. */
|
|
2226
|
+
#conditionResult(result) {
|
|
2227
|
+
const cast = this.condition(result);
|
|
2228
|
+
if (cast !== noMatch) return cast;
|
|
2229
|
+
this.cast(result);
|
|
2230
|
+
return result;
|
|
2231
|
+
}
|
|
2232
|
+
/** `value` through each normalization, each result cast by `recast`. */
|
|
2233
|
+
#normalized(value, recast) {
|
|
2234
|
+
let normalized = value;
|
|
2235
|
+
for (const { with: normalize, applyToNull } of normalizationsOf(this.#model, this.name)) {
|
|
2236
|
+
if (normalized === null && !applyToNull) continue;
|
|
2237
|
+
const result = normalize(normalized);
|
|
2238
|
+
refuseThenable(result, `${this.#model.name} normalizes "${this.name}" with a function that returned a promise; a normalization runs synchronously`);
|
|
2239
|
+
normalized = recast(result);
|
|
2240
|
+
}
|
|
2241
|
+
return normalized;
|
|
1609
2242
|
}
|
|
1610
|
-
/**
|
|
1611
|
-
|
|
1612
|
-
return
|
|
2243
|
+
/** Whether the attribute has a normalization, so its conditions need normalizing (#354). */
|
|
2244
|
+
get hasNormalization() {
|
|
2245
|
+
return normalizationsOf(this.#model, this.name).length > 0;
|
|
1613
2246
|
}
|
|
1614
|
-
/**
|
|
1615
|
-
|
|
1616
|
-
|
|
2247
|
+
/** Whether it's an enum declared `validate: true`, which keeps an unknown value it's given. */
|
|
2248
|
+
#isValidatedEnum() {
|
|
2249
|
+
const { enum: values, validate = false } = this.#definition;
|
|
2250
|
+
return values !== void 0 && validate !== false;
|
|
1617
2251
|
}
|
|
1618
2252
|
/**
|
|
1619
|
-
*
|
|
1620
|
-
*
|
|
2253
|
+
* A key copied from another record or inherited from a condition: cast as a condition's, but
|
|
2254
|
+
* one that can't be cast throws, naming the association it came through.
|
|
1621
2255
|
*/
|
|
1622
|
-
|
|
1623
|
-
this.
|
|
1624
|
-
return this.#
|
|
2256
|
+
key(value, association) {
|
|
2257
|
+
requireDefined(this.#model, this.name, value);
|
|
2258
|
+
return this.#orRefuse(castKey(this.#definition, value), value, association);
|
|
1625
2259
|
}
|
|
1626
2260
|
/**
|
|
1627
|
-
*
|
|
1628
|
-
*
|
|
2261
|
+
* A condition's value (`where`, `find`, a preload) as the attribute's type, or `noMatch`: a
|
|
2262
|
+
* number also matches a string attribute as its digits, and a value that can't be cast
|
|
2263
|
+
* matches nothing, so a value from a URL is never an error.
|
|
1629
2264
|
*/
|
|
1630
|
-
|
|
1631
|
-
this
|
|
1632
|
-
if (!this.changedNames().includes(name)) return false;
|
|
1633
|
-
return this.#isMatching(name, this.valueInDatabase(name), this.#values[name], options);
|
|
2265
|
+
condition(value) {
|
|
2266
|
+
return castCondition(this.#definition, value);
|
|
1634
2267
|
}
|
|
1635
|
-
/**
|
|
1636
|
-
|
|
1637
|
-
this.
|
|
1638
|
-
|
|
1639
|
-
return change !== void 0 && this.#isMatching(name, change[0], change[1], options);
|
|
2268
|
+
/** A condition's value in the form D1 stores, or `noMatch`. */
|
|
2269
|
+
conditionCell(value) {
|
|
2270
|
+
const cast = this.condition(value);
|
|
2271
|
+
return cast === noMatch ? noMatch : this.write(cast);
|
|
1640
2272
|
}
|
|
1641
|
-
/**
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
2273
|
+
/**
|
|
2274
|
+
* An already cast value in the form D1 stores. A `json` value changed in place into one JSON
|
|
2275
|
+
* can't write throws `TypeError` naming the attribute, as assigning it would (#146).
|
|
2276
|
+
*/
|
|
2277
|
+
write(value) {
|
|
2278
|
+
let stored;
|
|
2279
|
+
try {
|
|
2280
|
+
stored = writeValue(this.#definition, value);
|
|
2281
|
+
} catch (error) {
|
|
2282
|
+
if (this.#definition.customType !== void 0) throw this.#refusal(value, void 0, error);
|
|
2283
|
+
if (this.#definition.type !== "json") throw error;
|
|
2284
|
+
throw this.#refusal(value);
|
|
1653
2285
|
}
|
|
2286
|
+
if (stored === void 0) throw this.#refusal(value);
|
|
2287
|
+
return stored;
|
|
1654
2288
|
}
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
return (
|
|
1658
|
-
}
|
|
1659
|
-
/** The attributes an insert writes: the assigned ones, so column defaults apply to the rest. */
|
|
1660
|
-
insertAssignments() {
|
|
1661
|
-
return this.#assignmentsOf([...this.#assigned]);
|
|
1662
|
-
}
|
|
1663
|
-
/** The attributes an update writes: the changed ones. */
|
|
1664
|
-
updateAssignments() {
|
|
1665
|
-
return this.#assignmentsOf(this.changedNames());
|
|
1666
|
-
}
|
|
1667
|
-
/** The primary key as stored in D1: the row's key before any unsaved change to it. */
|
|
1668
|
-
get storedKey() {
|
|
1669
|
-
return this.#snapshotOf(this.#model.primaryKey);
|
|
2289
|
+
/** An assigned value cast, then in the form D1 stores. */
|
|
2290
|
+
store(value) {
|
|
2291
|
+
return this.write(this.cast(value));
|
|
1670
2292
|
}
|
|
1671
|
-
/**
|
|
1672
|
-
|
|
1673
|
-
return this
|
|
2293
|
+
/** An assigned value cast and normalized (#354), then in the form D1 stores. */
|
|
2294
|
+
storeNormalized(value) {
|
|
2295
|
+
return this.write(this.assigned(value));
|
|
1674
2296
|
}
|
|
1675
2297
|
/**
|
|
1676
|
-
*
|
|
1677
|
-
*
|
|
1678
|
-
*
|
|
2298
|
+
* A cell D1 returned as the attribute's value. One that isn't (text in a Unix time column, or
|
|
2299
|
+
* past the range of a `Date`; a json cell that isn't JSON) throws `TypeError` naming the
|
|
2300
|
+
* attribute, with the cell hidden if it's filtered, not the parser's own error (#73, #146).
|
|
1679
2301
|
*/
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
const
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
} else this.loadRow(row);
|
|
1690
|
-
this.#savedChanges = Object.fromEntries([...before].filter(([name, { stored }]) => written.has(name) || !isSameCell(this.#storedForm(name), stored)).map(([name, { value }]) => [name, [value, this.#copyOf(name)]]));
|
|
1691
|
-
this.isPreviouslyNew = true;
|
|
2302
|
+
read(cell) {
|
|
2303
|
+
if (cell === null && isLockVersion(this.#model, this.name)) return 0;
|
|
2304
|
+
const value = this.#convertOrRefuse(cell, () => readValue(this.#definition, cell));
|
|
2305
|
+
if (value === uncastable) throw this.#refusal(cell);
|
|
2306
|
+
return value;
|
|
2307
|
+
}
|
|
2308
|
+
/** A copy of a value: written as D1 stores it, then read back. */
|
|
2309
|
+
copy(value) {
|
|
2310
|
+
return this.read(this.write(value));
|
|
1692
2311
|
}
|
|
1693
2312
|
/**
|
|
1694
|
-
*
|
|
1695
|
-
*
|
|
1696
|
-
*
|
|
1697
|
-
* as stored, which wasn't a change.
|
|
2313
|
+
* Whether two values are the same as stored (dates by time, json and bytes by content). Each is
|
|
2314
|
+
* cast as a condition's value first, so `isAttributeChanged`'s `from` and `to` compare as
|
|
2315
|
+
* conditions do, and one that can't be cast is the same as nothing (#20).
|
|
1698
2316
|
*/
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
this.#
|
|
1703
|
-
this.#markSaved();
|
|
1704
|
-
this.isPreviouslyNew = false;
|
|
2317
|
+
isSame(a, b) {
|
|
2318
|
+
const [x, y] = [this.#comparable(a), this.#comparable(b)];
|
|
2319
|
+
if (x === noMatch || y === noMatch) return false;
|
|
2320
|
+
return isSameCell(writeValue(this.#definition, x), writeValue(this.#definition, y));
|
|
1705
2321
|
}
|
|
1706
2322
|
/**
|
|
1707
|
-
*
|
|
1708
|
-
*
|
|
1709
|
-
* `savedChanges`, are as they were.
|
|
2323
|
+
* A value to compare, cast as a condition's: `null` stays `null`, and a validated enum's
|
|
2324
|
+
* unknown value, which a record may hold, stays as it is (#340).
|
|
1710
2325
|
*/
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
2326
|
+
#comparable(value) {
|
|
2327
|
+
if (value === null || value === void 0) return value ?? null;
|
|
2328
|
+
const cast = this.condition(value);
|
|
2329
|
+
return cast === noMatch && this.#isValidatedEnum() ? value : cast;
|
|
2330
|
+
}
|
|
2331
|
+
/** An assigned value, cast and written, bound hidden from `onQuery` if filtered (#83). */
|
|
2332
|
+
bind(params, value) {
|
|
2333
|
+
this.bindCells(params, this.store(value));
|
|
1718
2334
|
}
|
|
1719
2335
|
/**
|
|
1720
|
-
*
|
|
1721
|
-
*
|
|
2336
|
+
* Binds cells (as D1 holds them) as the attribute's, each hidden from `onQuery` listeners when
|
|
2337
|
+
* the attribute is filtered (#83); D1 still gets the real values. Filtering is asked at the
|
|
2338
|
+
* time, since `filterAttributes` is a static a Model may set after this was resolved.
|
|
1722
2339
|
*/
|
|
1723
|
-
|
|
1724
|
-
const
|
|
1725
|
-
|
|
1726
|
-
this.#savedChanges = Object.fromEntries(names.filter((name) => Object.hasOwn(changes, name)).map((name) => [name, changes[name]]));
|
|
1727
|
-
}
|
|
1728
|
-
/** A save with nothing to write: nothing changes but `savedChanges`. */
|
|
1729
|
-
commitClean() {
|
|
1730
|
-
this.#savedChanges = {};
|
|
1731
|
-
this.isPreviouslyNew = false;
|
|
2340
|
+
bindCells(params, ...cells) {
|
|
2341
|
+
const hidden = this.#isHidden();
|
|
2342
|
+
for (const cell of cells) params.push(hidden ? new Filtered(cell) : cell);
|
|
1732
2343
|
}
|
|
1733
|
-
|
|
1734
|
-
this.#
|
|
1735
|
-
|
|
2344
|
+
#orRefuse(cast, value, association) {
|
|
2345
|
+
if (cast === uncastable) throw this.#refusal(value, association);
|
|
2346
|
+
return cast;
|
|
1736
2347
|
}
|
|
1737
|
-
/**
|
|
1738
|
-
|
|
1739
|
-
|
|
2348
|
+
/**
|
|
2349
|
+
* `convert`'s result. What a custom type's own function throws for `value` is refused as any
|
|
2350
|
+
* value its type can't hold, with the type's error as the cause (#20).
|
|
2351
|
+
*/
|
|
2352
|
+
#convertOrRefuse(value, convert) {
|
|
2353
|
+
try {
|
|
2354
|
+
return convert();
|
|
2355
|
+
} catch (error) {
|
|
2356
|
+
if (this.#definition.customType === void 0) throw error;
|
|
2357
|
+
throw this.#refusal(value, void 0, error);
|
|
2358
|
+
}
|
|
1740
2359
|
}
|
|
1741
|
-
/**
|
|
1742
|
-
#
|
|
1743
|
-
this.#
|
|
2360
|
+
/** Whether the attribute's values are hidden from listeners and errors (#83). */
|
|
2361
|
+
#isHidden() {
|
|
2362
|
+
return isFiltered(this.#model, this.name);
|
|
1744
2363
|
}
|
|
1745
|
-
|
|
1746
|
-
|
|
2364
|
+
/**
|
|
2365
|
+
* "Sample: "no" isn't a boolean for "enabled"", the value hidden for a filtered attribute. Also
|
|
2366
|
+
* what a stored value that can't be read says: a Unix time that isn't one (#73), or a json
|
|
2367
|
+
* cell that isn't JSON (#146). A custom type's own error is its cause, unless the attribute is
|
|
2368
|
+
* filtered: the error may show the value (#20).
|
|
2369
|
+
*/
|
|
2370
|
+
#refusal(value, association, cause) {
|
|
2371
|
+
const hidden = this.#isHidden();
|
|
2372
|
+
const text = hidden ? FILTERED : shown(value);
|
|
2373
|
+
const from = association === void 0 ? "" : `, the key of ${association}`;
|
|
2374
|
+
return new TypeError(`${this.#model.name}: ${text} isn't ${this.#described()} for "${this.name}"${from}`, hidden || cause === void 0 ? void 0 : { cause });
|
|
1747
2375
|
}
|
|
1748
|
-
/**
|
|
1749
|
-
#
|
|
1750
|
-
|
|
2376
|
+
/** What a value of the attribute is, as a refusal says it: `a boolean`, `one of "draft", "published"`. */
|
|
2377
|
+
#described() {
|
|
2378
|
+
const { type, enum: values, customType } = this.#definition;
|
|
2379
|
+
if (values !== void 0) return `one of ${listedNames(values)}`;
|
|
2380
|
+
if (customType !== void 0) return "a valid value";
|
|
2381
|
+
return valueNames[type] ?? withArticle(type);
|
|
1751
2382
|
}
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
2383
|
+
};
|
|
2384
|
+
const resolved = /* @__PURE__ */ new WeakMap();
|
|
2385
|
+
/**
|
|
2386
|
+
* The resolved attribute of a Model, remembered per class (derived from the class, ADR-0003):
|
|
2387
|
+
* `UnknownAttribute` for a name it doesn't have.
|
|
2388
|
+
*/
|
|
2389
|
+
function attributeFor(model, given) {
|
|
2390
|
+
const name = attributeName(model, given);
|
|
2391
|
+
let attributes = resolved.get(model);
|
|
2392
|
+
if (attributes === void 0) {
|
|
2393
|
+
attributes = /* @__PURE__ */ new Map();
|
|
2394
|
+
resolved.set(model, attributes);
|
|
1755
2395
|
}
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
2396
|
+
let attribute = attributes.get(name);
|
|
2397
|
+
if (attribute === void 0) {
|
|
2398
|
+
attribute = new Attribute(model, name);
|
|
2399
|
+
attributes.set(name, attribute);
|
|
1760
2400
|
}
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
2401
|
+
return attribute;
|
|
2402
|
+
}
|
|
2403
|
+
/**
|
|
2404
|
+
* The values a record built from a Relation inherits from its conditions, cast as keys are: a
|
|
2405
|
+
* hasMany's foreign key names its association (`link`) when it can't be cast.
|
|
2406
|
+
*/
|
|
2407
|
+
function castInherited(model, inherited, link) {
|
|
2408
|
+
return Object.fromEntries(Object.entries(inherited).map(([attribute, value]) => {
|
|
2409
|
+
const association = attribute === link?.attribute ? link.association : void 0;
|
|
2410
|
+
return [attribute, attributeFor(model, attribute).key(value, association)];
|
|
2411
|
+
}));
|
|
2412
|
+
}
|
|
2413
|
+
/** A word with `a` or `an` before it, as a message reads it: `an integer`, `a string`. */
|
|
2414
|
+
function withArticle(word) {
|
|
2415
|
+
return `${/^[aeiou]/.test(word) ? "an" : "a"} ${word}`;
|
|
2416
|
+
}
|
|
2417
|
+
/** How much of a value an error shows: it came from outside, and errors are often logged. */
|
|
2418
|
+
const SHOWN_LENGTH = 60;
|
|
2419
|
+
/** What a value of an attribute type is called in a refusal, where `a`/`an` and its name isn't it. */
|
|
2420
|
+
const valueNames = {
|
|
2421
|
+
json: "a JSON value",
|
|
2422
|
+
binary: "binary data"
|
|
2423
|
+
};
|
|
2424
|
+
/** A value as an error shows it: JSON where it has one, numbers as written, and at most 60 characters. */
|
|
2425
|
+
function shown(value) {
|
|
2426
|
+
const text = textOf(value);
|
|
2427
|
+
return text.length > SHOWN_LENGTH ? `${text.slice(0, SHOWN_LENGTH)}…` : text;
|
|
2428
|
+
}
|
|
2429
|
+
function textOf(value) {
|
|
2430
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return `<${value.constructor.name} of ${value.byteLength} bytes>`;
|
|
2431
|
+
if (value instanceof Blob) return `<${value.constructor.name} of ${value.size} bytes>`;
|
|
2432
|
+
if (typeof value === "number" || isInvalidDate(value)) return String(value);
|
|
2433
|
+
try {
|
|
2434
|
+
return JSON.stringify(value) ?? String(value);
|
|
2435
|
+
} catch {
|
|
2436
|
+
return plainText(value);
|
|
1764
2437
|
}
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
2438
|
+
}
|
|
2439
|
+
/** `String(value)`, or its tag (`[object Array]`) when it has no string form: building the refusal mustn't throw. */
|
|
2440
|
+
function plainText(value) {
|
|
2441
|
+
try {
|
|
2442
|
+
return String(value);
|
|
2443
|
+
} catch {
|
|
2444
|
+
return Object.prototype.toString.call(value);
|
|
1768
2445
|
}
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
2446
|
+
}
|
|
2447
|
+
// src/enum-methods.ts
|
|
2448
|
+
/** Each name of every enum attribute, with the methods it gives. */
|
|
2449
|
+
function enumNames(definitions) {
|
|
2450
|
+
return [...definitions].flatMap(([attribute, definition]) => {
|
|
2451
|
+
if (definition.enum === void 0) return [];
|
|
2452
|
+
const before = affix(attribute, definition.prefix);
|
|
2453
|
+
const after = affix(attribute, definition.suffix);
|
|
2454
|
+
return enumEntries(definition.enum).map(([name]) => {
|
|
2455
|
+
const stem = `${before}${methodPart(name)}${after}`;
|
|
2456
|
+
const records = [
|
|
2457
|
+
`is${stem}`,
|
|
2458
|
+
`saveAs${stem}`,
|
|
2459
|
+
`saveAs${stem}OrThrow`
|
|
2460
|
+
];
|
|
2461
|
+
const scopes = [uncapitalized(stem), `not${stem}`];
|
|
2462
|
+
return {
|
|
2463
|
+
attribute,
|
|
2464
|
+
name,
|
|
2465
|
+
records: definition.instanceMethods === false ? [] : records,
|
|
2466
|
+
scopes: definition.scopes === false ? [] : scopes
|
|
2467
|
+
};
|
|
2468
|
+
});
|
|
2469
|
+
});
|
|
2470
|
+
}
|
|
2471
|
+
function affix(attribute, option) {
|
|
2472
|
+
if (option === true) return methodPart(attribute);
|
|
2473
|
+
return typeof option === "string" ? methodPart(option) : "";
|
|
2474
|
+
}
|
|
2475
|
+
/** The scopes the enums of `definitions` give a Model, as `EnumStatics` types them. */
|
|
2476
|
+
function enumScopeNames(definitions) {
|
|
2477
|
+
return enumNames(definitions).flatMap(({ scopes }) => scopes);
|
|
2478
|
+
}
|
|
2479
|
+
/** How a word reads inside a method's name, as `MethodPart` types it. */
|
|
2480
|
+
function methodPart(word) {
|
|
2481
|
+
const [head = "", ...rest] = word.split("_");
|
|
2482
|
+
return capitalized(head + rest.map(capitalized).join(""));
|
|
2483
|
+
}
|
|
2484
|
+
const uncapitalized = (text) => text.charAt(0).toLowerCase() + text.slice(1);
|
|
2485
|
+
/**
|
|
2486
|
+
* Defines each enum attribute's methods on `prototype`: `isPublished()` is `status === "published"`,
|
|
2487
|
+
* and `saveAsPublished()` is `update({ status: "published" })`, so it saves the record's other
|
|
2488
|
+
* changes too.
|
|
2489
|
+
*/
|
|
2490
|
+
function defineEnumMethods(prototype, definitions) {
|
|
2491
|
+
for (const { attribute, name, records } of enumNames(definitions)) {
|
|
2492
|
+
const [predicate, setter, throwingSetter] = records;
|
|
2493
|
+
if (predicate === void 0) continue;
|
|
2494
|
+
defineMethod(prototype, predicate, function() {
|
|
2495
|
+
return this[attribute] === name;
|
|
2496
|
+
});
|
|
2497
|
+
defineMethod(prototype, setter, function() {
|
|
2498
|
+
return this.update({ [attribute]: name });
|
|
2499
|
+
});
|
|
2500
|
+
defineMethod(prototype, throwingSetter, function() {
|
|
2501
|
+
return this.updateOrThrow({ [attribute]: name });
|
|
2502
|
+
});
|
|
1772
2503
|
}
|
|
1773
|
-
|
|
1774
|
-
|
|
2504
|
+
}
|
|
2505
|
+
/**
|
|
2506
|
+
* Defines each enum attribute's scopes on `model`, as `this.scope(…)` statics: `published()` is
|
|
2507
|
+
* `where({ status: "published" })`, and `notPublished()` is `whereNot(…)`.
|
|
2508
|
+
*/
|
|
2509
|
+
function defineEnumStatics(model, definitions) {
|
|
2510
|
+
const declaring = model;
|
|
2511
|
+
for (const { attribute, name, scopes } of enumNames(definitions)) {
|
|
2512
|
+
const [only, without] = scopes;
|
|
2513
|
+
if (only === void 0) continue;
|
|
2514
|
+
const onlyScope = declaring.scope((q) => q.where({ [attribute]: name }));
|
|
2515
|
+
const withoutScope = declaring.scope((q) => q.whereNot({ [attribute]: name }));
|
|
2516
|
+
enumScopes.add(onlyScope);
|
|
2517
|
+
enumScopes.add(withoutScope);
|
|
2518
|
+
defineMethod(model, only, onlyScope, { named: false });
|
|
2519
|
+
defineMethod(model, without, withoutScope, { named: false });
|
|
1775
2520
|
}
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
2521
|
+
}
|
|
2522
|
+
/** Puts `method` on `target` as `name`, writable as a declared method is, and names it unless told not to. */
|
|
2523
|
+
function defineMethod(target, name, method, { named = true } = {}) {
|
|
2524
|
+
if (Object.hasOwn(target, name)) return;
|
|
2525
|
+
if (named) Object.defineProperty(method, "name", { value: name });
|
|
2526
|
+
Object.defineProperty(target, name, {
|
|
2527
|
+
value: method,
|
|
2528
|
+
writable: true,
|
|
2529
|
+
configurable: true
|
|
2530
|
+
});
|
|
2531
|
+
}
|
|
2532
|
+
/** The scope statics `defineEnumStatics` made, so a static a Model declares itself is told apart. */
|
|
2533
|
+
const enumScopes = /* @__PURE__ */ new WeakSet();
|
|
2534
|
+
/**
|
|
2535
|
+
* Why an enum's method names can't be as they are, or undefined: two enums give one name, or one
|
|
2536
|
+
* takes the name of a record method, an attribute, a Relation method, a Model static, or a static
|
|
2537
|
+
* the Model declares itself. It's refused when the enum is declared.
|
|
2538
|
+
*/
|
|
2539
|
+
function enumNameProblem(model, taken, methodsOff = /* @__PURE__ */ new Map()) {
|
|
2540
|
+
const checks = {
|
|
2541
|
+
...taken,
|
|
2542
|
+
methodsOffFix: (attribute) => methodsOff.get(attribute)
|
|
2543
|
+
};
|
|
2544
|
+
const givenBy = /* @__PURE__ */ new Map();
|
|
2545
|
+
for (const each of enumNames(model.attributeDefinitions)) {
|
|
2546
|
+
const { attribute, records, scopes } = each;
|
|
2547
|
+
for (const method of [...records, ...scopes]) {
|
|
2548
|
+
const other = givenBy.get(method);
|
|
2549
|
+
if (other !== void 0) return repeated(model.name, {
|
|
2550
|
+
first: other,
|
|
2551
|
+
second: attribute,
|
|
2552
|
+
method
|
|
2553
|
+
}, checks);
|
|
2554
|
+
givenBy.set(method, attribute);
|
|
1787
2555
|
}
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
}
|
|
1791
|
-
/** Whether the Snapshot has `name`: it was loaded or saved. */
|
|
1792
|
-
#isInSnapshot(name) {
|
|
1793
|
-
return this.#snapshot.has(name) || this.#isInRow(name);
|
|
2556
|
+
const problem = declarableProblem(model, each, [...records, ...scopes], checks) ?? recordProblem(model, attribute, records, checks) ?? staticProblem(model, attribute, scopes, checks);
|
|
2557
|
+
if (problem !== void 0) return problem;
|
|
1794
2558
|
}
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
2559
|
+
}
|
|
2560
|
+
/**
|
|
2561
|
+
* A method name an enum's name gives that TypeScript can't declare (#340): one that isn't an
|
|
2562
|
+
* identifier (`isIn-progress`), or a scope that's a reserved word (`new`), which the generated
|
|
2563
|
+
* schema couldn't declare as a static.
|
|
2564
|
+
*/
|
|
2565
|
+
function declarableProblem(model, { attribute, name, scopes }, methods, checks) {
|
|
2566
|
+
const fix = remedy(attribute, checks);
|
|
2567
|
+
const undeclarable = methods.find((method) => !isIdentifier(method));
|
|
2568
|
+
if (undeclarable !== void 0) {
|
|
2569
|
+
const kind = scopes.includes(undeclarable) ? "scope" : "method";
|
|
2570
|
+
const suggested = uncapitalized(methodPart(name.split(/[^\p{L}\p{N}_$]+/u).join("_")));
|
|
2571
|
+
const example = isIdentifier(suggested) ? ` ({ ${suggested}: ${JSON.stringify(name)} })` : "";
|
|
2572
|
+
return `${model.name}'s enum ${attribute} gives a ${kind} named "${undeclarable}", which isn't a name TypeScript can declare; name it with letters, digits, _ and $, and map it to the value it's stored as${example}${offFix(attribute, checks)}`;
|
|
1802
2573
|
}
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
2574
|
+
const reserved = methods.find((method) => reservedWords.has(method));
|
|
2575
|
+
return reserved === void 0 ? void 0 : `${model.name}'s enum ${attribute} gives a scope named "${reserved}", which is a reserved word; ${fix}`;
|
|
2576
|
+
}
|
|
2577
|
+
/** Two of an enum's names, or two enums, giving one method name. */
|
|
2578
|
+
function repeated(model, { first, second, method }, checks) {
|
|
2579
|
+
if (first !== second) return `${model}'s enums ${first} and ${second} both give a method named "${method}"; give one a prefix or a suffix${[first, second].map((attribute) => namedOffFix(attribute, checks)).join("")}`;
|
|
2580
|
+
const off = offFix(second, checks);
|
|
2581
|
+
return `${model}'s enum ${first} gives "${method}" for two of its names; ${off === "" ? "rename one" : "map one to a name of your own"}${off}`;
|
|
2582
|
+
}
|
|
2583
|
+
/** `offFix`, naming the enum: for a message about two of them. */
|
|
2584
|
+
function namedOffFix(attribute, checks) {
|
|
2585
|
+
const fix = checks.methodsOffFix(attribute);
|
|
2586
|
+
return fix === void 0 ? "" : `, or leave ${attribute}'s methods off (remove ${fix})`;
|
|
2587
|
+
}
|
|
2588
|
+
/** How to fix an enum whose method name is taken: an affix, or for the database's, its methods off. */
|
|
2589
|
+
const remedy = (attribute, checks) => `give ${attribute} a prefix or a suffix${offFix(attribute, checks)}`;
|
|
2590
|
+
/** For an enum whose names the database gave (#64): leaving its methods off, as they start. */
|
|
2591
|
+
function offFix(attribute, checks) {
|
|
2592
|
+
const fix = checks.methodsOffFix(attribute);
|
|
2593
|
+
return fix === void 0 ? "" : `, or leave its methods off (remove ${fix})`;
|
|
2594
|
+
}
|
|
2595
|
+
function recordProblem(model, attribute, methods, checks) {
|
|
2596
|
+
for (const method of methods) {
|
|
2597
|
+
const what = model.attributeDefinitions.has(method) ? "an attribute" : checks.isRecordName(method) ? "a record method" : void 0;
|
|
2598
|
+
if (what !== void 0) return `${model.name}'s enum ${attribute} gives a method named "${method}", which is the name of ${what}; ${remedy(attribute, checks)}`;
|
|
1806
2599
|
}
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
2600
|
+
}
|
|
2601
|
+
function staticProblem(model, attribute, scopes, checks) {
|
|
2602
|
+
for (const scope of scopes) {
|
|
2603
|
+
const what = scope in Function.prototype ? "a Function method" : checks.isReservedStatic(scope) ? "a Relation method or a Model static" : void 0;
|
|
2604
|
+
if (what !== void 0) return `${model.name}'s enum ${attribute} gives a scope named "${scope}", which is the name of ${what}; ${remedy(attribute, checks)}`;
|
|
2605
|
+
const declared = checks.staticOf(scope);
|
|
2606
|
+
const isEnumScope = typeof declared?.value === "function" && enumScopes.has(declared.value);
|
|
2607
|
+
if (declared !== void 0 && !isEnumScope) return `${model.name} declares "${scope}", which is also the name of a scope its enum ${attribute} gives; rename it, or ${remedy(attribute, checks)}`;
|
|
1810
2608
|
}
|
|
1811
|
-
}
|
|
2609
|
+
}
|
|
1812
2610
|
// src/enum-switch.ts
|
|
1813
2611
|
/** Takes an override's `enum: true` or `false` out of it, so its other changes are checked as any override's. */
|
|
1814
2612
|
function takeEnumSwitch(override) {
|
|
@@ -1877,6 +2675,7 @@ function methodsOffFixes(model) {
|
|
|
1877
2675
|
}
|
|
1878
2676
|
// src/generated-model.ts
|
|
1879
2677
|
const noAttributes = /* @__PURE__ */ new Map();
|
|
2678
|
+
const noAliases = /* @__PURE__ */ new Map();
|
|
1880
2679
|
/**
|
|
1881
2680
|
* What each generated Model settled on when it was first used, by class (a subclass shares its
|
|
1882
2681
|
* parent's). Until then, they're found again on each read, since a `static {}` block reads them
|
|
@@ -1904,7 +2703,10 @@ function tableOwnerOf(model) {
|
|
|
1904
2703
|
schema
|
|
1905
2704
|
};
|
|
1906
2705
|
}
|
|
1907
|
-
/**
|
|
2706
|
+
/**
|
|
2707
|
+
* The attribute accessors on a prototype, reading and writing a record's values; an alias's
|
|
2708
|
+
* (#509) read and write the attribute it names, which the record resolves.
|
|
2709
|
+
*/
|
|
1908
2710
|
function defineAttributeAccessors(prototype, names) {
|
|
1909
2711
|
for (const name of names) Object.defineProperty(prototype, name, {
|
|
1910
2712
|
get() {
|
|
@@ -1936,12 +2738,16 @@ function find(owner, schema) {
|
|
|
1936
2738
|
rejectUngenerated(name, table, schema);
|
|
1937
2739
|
const tableSchema = Object.hasOwn(schema.tables, table) ? schema.tables[table] : void 0;
|
|
1938
2740
|
if (tableSchema === void 0) throw new InvalidModel(`${owner.name}'s table, ${table}, isn't in the generated schema`);
|
|
1939
|
-
const
|
|
2741
|
+
const declared = withSecurePasswords(`${owner.name}.`, owner.attributes ?? {});
|
|
2742
|
+
const { specs, isIgnoringColumns } = specsFor(owner.name, tableSchema, declared);
|
|
1940
2743
|
const definitions = new Map(Object.entries(specs).map(([name, spec]) => [name, define(name, spec)]));
|
|
2744
|
+
const aliases = aliasesIn(`${owner.name}.`, declared, definitions);
|
|
2745
|
+
rejectMissingDigests(`${owner.name}.`, definitions);
|
|
1941
2746
|
return {
|
|
1942
2747
|
table,
|
|
1943
2748
|
primaryKey: Object.hasOwn(owner, "primaryKey") ? owner.primaryKey : tableSchema.primaryKey,
|
|
1944
2749
|
definitions,
|
|
2750
|
+
aliases,
|
|
1945
2751
|
isIgnoringColumns
|
|
1946
2752
|
};
|
|
1947
2753
|
}
|
|
@@ -1969,9 +2775,11 @@ function settleGenerated(model) {
|
|
|
1969
2775
|
if (found === void 0) {
|
|
1970
2776
|
found = find(owner, place.schema);
|
|
1971
2777
|
bindTable(owner, found.table, { isIgnoringColumns: found.isIgnoringColumns });
|
|
1972
|
-
defineAttributeAccessors(owner.prototype, found.definitions.keys());
|
|
2778
|
+
defineAttributeAccessors(owner.prototype, [...found.definitions.keys(), ...found.aliases.keys()]);
|
|
1973
2779
|
defineEnumMethods(owner.prototype, found.definitions);
|
|
1974
2780
|
defineEnumStatics(owner, found.definitions);
|
|
2781
|
+
defineSecurePasswordMethods(owner.prototype, found.definitions);
|
|
2782
|
+
defineSecureTokenMethods(owner.prototype, found.definitions);
|
|
1975
2783
|
settled.set(owner, found);
|
|
1976
2784
|
}
|
|
1977
2785
|
settled.set(model, found);
|
|
@@ -1999,7 +2807,8 @@ function defineGeneratedStatics(root) {
|
|
|
1999
2807
|
const statics = {
|
|
2000
2808
|
table: (model) => generated(model)?.table ?? "",
|
|
2001
2809
|
primaryKey: (model) => generated(model)?.primaryKey ?? "id",
|
|
2002
|
-
attributeDefinitions: (model) => generated(model)?.definitions ?? noAttributes
|
|
2810
|
+
attributeDefinitions: (model) => generated(model)?.definitions ?? noAttributes,
|
|
2811
|
+
attributeAliases: (model) => generated(model)?.aliases ?? noAliases
|
|
2003
2812
|
};
|
|
2004
2813
|
for (const [name, read] of Object.entries(statics)) Object.defineProperty(root, name, {
|
|
2005
2814
|
configurable: true,
|
|
@@ -2038,8 +2847,14 @@ function checkedOverride(where, spec, override) {
|
|
|
2038
2847
|
/** A table's attribute specs with `overrides` applied; throws `InvalidModel` for a bad one. */
|
|
2039
2848
|
function specsFor(name, table, overrides) {
|
|
2040
2849
|
const changes = /* @__PURE__ */ new Map();
|
|
2041
|
-
|
|
2850
|
+
const virtual = {};
|
|
2851
|
+
const given = Object.entries(overrides).filter(([, override]) => !isAlias(override));
|
|
2852
|
+
for (const [attribute, override] of given) {
|
|
2042
2853
|
if (override === void 0) continue;
|
|
2854
|
+
if (isVirtual(override)) {
|
|
2855
|
+
virtual[attribute] = virtualSpec(name, attribute, table, override);
|
|
2856
|
+
continue;
|
|
2857
|
+
}
|
|
2043
2858
|
if (!Object.hasOwn(table.attributes, attribute)) throw new InvalidModel(`${name} has no attribute ${attribute} to override`);
|
|
2044
2859
|
if (override === false && attribute === table.primaryKey) throw new InvalidModel(`${name}.${attribute} is the primary key, so it can't be ignored`);
|
|
2045
2860
|
changes.set(attribute, checked(name, attribute, override));
|
|
@@ -2051,8 +2866,27 @@ function specsFor(name, table, overrides) {
|
|
|
2051
2866
|
specs[attribute] = change === void 0 ? spec : checkedOverride(`${name}.${attribute}`, spec, change);
|
|
2052
2867
|
}
|
|
2053
2868
|
return {
|
|
2054
|
-
specs
|
|
2869
|
+
specs: {
|
|
2870
|
+
...specs,
|
|
2871
|
+
...virtual
|
|
2872
|
+
},
|
|
2055
2873
|
isIgnoringColumns: [...changes.values()].includes(false)
|
|
2056
2874
|
};
|
|
2057
2875
|
}
|
|
2058
|
-
|
|
2876
|
+
/** Whether an override declares a virtual attribute (#356): an object with `virtual: true`. */
|
|
2877
|
+
function isVirtual(override) {
|
|
2878
|
+
return typeof override === "object" && override !== null && "virtual" in override && override.virtual === true;
|
|
2879
|
+
}
|
|
2880
|
+
/**
|
|
2881
|
+
* A virtual attribute's spec (#356), checked: not a column's name, a whole spec with its type,
|
|
2882
|
+
* and non-null only with a default, since nothing loads a value for it.
|
|
2883
|
+
*/
|
|
2884
|
+
function virtualSpec(name, attribute, table, spec) {
|
|
2885
|
+
const where = `${name}.${attribute}`;
|
|
2886
|
+
if (Object.hasOwn(table.attributes, attribute)) throw new InvalidModel(`${where} is virtual, but ${name}'s table has a column for it; rename it, or drop virtual`);
|
|
2887
|
+
if (!isSpecOverride(spec) || spec.type === void 0) throw new InvalidModel(`${where}: a virtual attribute is { type, virtual: true, null?, default? }`);
|
|
2888
|
+
const problem = specProblem(where, spec) ?? virtualNullProblem(where, spec);
|
|
2889
|
+
if (problem !== void 0) throw new InvalidModel(problem);
|
|
2890
|
+
return spec;
|
|
2891
|
+
}
|
|
2892
|
+
export { FilteredMark as $, authenticateBy as A, tableize as At, RecordState as B, Filtered as C, isIgnoringColumns as Ct, isFiltered as D, requireModelName as Dt, filteredValues as E, rejectAbstract as Et, rejectMissingDigests as F, columnOf as G, state as H, resetPurposeOf as I, requireDefined as J, define as K, resetTokenDefinitions as L, hashPasswords as M, staticBelow as Mt, passwordConfirmations as N, shownKey as O, requireTableName as Ot, passwordFailures as P, fullMessageOf as Q, secretAttributeNames as R, FILTERED as S, inferTableNames as St, defaultFilterAttributes as T, ownValueSetter as Tt, attributeOf as U, linksOf as V, attributesForColumn as W, addFailure as X, Errors as Y, clearErrors as Z, isLocking as _, declared as _t, isGenerated as a, specProblem as at, isThenable as b, defineModelName as bt, methodsOffFixes as c, generateTokens as ct, enumNameProblem as d, defineType as dt, inspectCustom as et, enumScopeNames as f, aliasesIn as ft, withArticle as g, declare as gt, noMatch as h, withAttributeNames as ht, isBase as i, humanAttributeName as it, defineSecurePasswordMethods as j, ancestors as jt, shownValue as k, tableOwnerModelName as kt, defineEnumMethods as l, expiryAfter as lt, castInherited as m, isAlias as mt, defineGeneratedStatics as n, styled as nt, schemaAdapter as o, defineSecureTokenMethods as ot, attributeFor as p, attributeName as pt, initialValues as q, hasSettledAttributes as r, attributesNamedInMessages as rt, settleGenerated as s, generateSecureToken as st, defineAttributeAccessors as t, printed as tt, defineEnumStatics as u, secondsOf as ut, lockVersion as v, declaredByClass as vt, boundValue as w, modelNameOf as wt, refuseThenable as x, explainMissingTable as xt, checkNormalizedName as y, seal as yt, withSecurePasswords as z };
|