edinburgh 0.4.6 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +403 -461
- package/build/src/datapack.d.ts +9 -9
- package/build/src/datapack.js +10 -10
- package/build/src/datapack.js.map +1 -1
- package/build/src/edinburgh.d.ts +21 -10
- package/build/src/edinburgh.js +33 -55
- package/build/src/edinburgh.js.map +1 -1
- package/build/src/indexes.d.ts +99 -288
- package/build/src/indexes.js +253 -636
- package/build/src/indexes.js.map +1 -1
- package/build/src/migrate.js +17 -39
- package/build/src/migrate.js.map +1 -1
- package/build/src/models.d.ts +177 -113
- package/build/src/models.js +487 -259
- package/build/src/models.js.map +1 -1
- package/build/src/types.d.ts +41 -51
- package/build/src/types.js +39 -52
- package/build/src/types.js.map +1 -1
- package/build/src/utils.d.ts +4 -4
- package/build/src/utils.js +4 -4
- package/package.json +1 -3
- package/skill/AnyModelClass.md +7 -0
- package/skill/FindOptions.md +37 -0
- package/skill/Lifecycle Hooks.md +24 -0
- package/skill/{Model_delete.md → Lifecycle Hooks_delete.md } +2 -2
- package/skill/{Model_getPrimaryKeyHash.md → Lifecycle Hooks_getPrimaryKeyHash.md } +1 -1
- package/skill/{Model_isValid.md → Lifecycle Hooks_isValid.md } +1 -1
- package/skill/Lifecycle Hooks_migrate.md +26 -0
- package/skill/{Model_preCommit.md → Lifecycle Hooks_preCommit.md } +3 -5
- package/skill/{Model_preventPersist.md → Lifecycle Hooks_preventPersist.md } +2 -2
- package/skill/{Model_validate.md → Lifecycle Hooks_validate.md } +2 -2
- package/skill/ModelBase.md +7 -0
- package/skill/ModelClass.md +8 -0
- package/skill/SKILL.md +253 -215
- package/skill/Schema Evolution.md +19 -0
- package/skill/TypeWrapper_containsNull.md +11 -0
- package/skill/TypeWrapper_deserialize.md +9 -0
- package/skill/TypeWrapper_getError.md +11 -0
- package/skill/TypeWrapper_serialize.md +10 -0
- package/skill/TypeWrapper_serializeType.md +9 -0
- package/skill/array.md +2 -2
- package/skill/defineModel.md +23 -0
- package/skill/deleteEverything.md +8 -0
- package/skill/field.md +4 -4
- package/skill/link.md +12 -10
- package/skill/literal.md +1 -1
- package/skill/opt.md +1 -1
- package/skill/or.md +1 -1
- package/skill/record.md +1 -1
- package/skill/set.md +2 -2
- package/skill/setOnSaveCallback.md +2 -2
- package/skill/transact.md +3 -3
- package/src/datapack.ts +10 -10
- package/src/edinburgh.ts +46 -58
- package/src/indexes.ts +338 -802
- package/src/migrate.ts +15 -37
- package/src/models.ts +617 -314
- package/src/types.ts +61 -54
- package/src/utils.ts +4 -4
- package/skill/BaseIndex.md +0 -16
- package/skill/BaseIndex_batchProcess.md +0 -10
- package/skill/BaseIndex_find.md +0 -7
- package/skill/Model.md +0 -22
- package/skill/Model_findAll.md +0 -12
- package/skill/Model_migrate.md +0 -34
- package/skill/Model_replaceInto.md +0 -16
- package/skill/PrimaryIndex.md +0 -8
- package/skill/PrimaryIndex_get.md +0 -17
- package/skill/PrimaryIndex_getLazy.md +0 -13
- package/skill/SecondaryIndex.md +0 -9
- package/skill/UniqueIndex.md +0 -9
- package/skill/UniqueIndex_get.md +0 -17
- package/skill/dump.md +0 -8
- package/skill/index.md +0 -32
- package/skill/primary.md +0 -26
- package/skill/registerModel.md +0 -26
- package/skill/unique.md +0 -32
package/build/src/models.d.ts
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import DataPack from "./datapack.js";
|
|
2
2
|
import { TypeWrapper } from "./types.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* Returns the current transaction from AsyncLocalStorage.
|
|
6
|
-
* Throws if called outside a transact() callback.
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export declare function currentTxn(): Transaction;
|
|
10
|
-
export interface Transaction {
|
|
11
|
-
id: number;
|
|
12
|
-
instances: Set<Model<unknown>>;
|
|
13
|
-
instancesByPk: Map<number, Model<unknown>>;
|
|
14
|
-
}
|
|
15
|
-
import { BaseIndex as BaseIndex, PrimaryIndex, IndexRangeIterator } from "./indexes.js";
|
|
3
|
+
import { type Transaction } from "./edinburgh.js";
|
|
4
|
+
import { PrimaryKey, NonPrimaryIndex, IndexRangeIterator, FindOptions, VersionInfo } from "./indexes.js";
|
|
16
5
|
/**
|
|
17
6
|
* Configuration interface for model fields.
|
|
18
7
|
* @template T - The field type.
|
|
@@ -33,53 +22,161 @@ export interface FieldConfig<T> {
|
|
|
33
22
|
* This allows for both runtime introspection and compile-time type safety.
|
|
34
23
|
*
|
|
35
24
|
* @template T - The field type.
|
|
36
|
-
* @param type
|
|
37
|
-
* @param options
|
|
25
|
+
* @param type The type wrapper for this field.
|
|
26
|
+
* @param options Additional field configuration options.
|
|
38
27
|
* @returns The field value (typed as T, but actually returns FieldConfig<T>).
|
|
39
28
|
*
|
|
40
29
|
* @example
|
|
41
30
|
* ```typescript
|
|
42
|
-
*
|
|
31
|
+
* const User = E.defineModel("User", class {
|
|
43
32
|
* name = E.field(E.string, {description: "User's full name"});
|
|
44
33
|
* age = E.field(E.opt(E.number), {description: "User's age", default: 25});
|
|
45
|
-
* }
|
|
34
|
+
* });
|
|
46
35
|
* ```
|
|
47
36
|
*/
|
|
48
37
|
export declare function field<T>(type: TypeWrapper<T>, options?: Partial<FieldConfig<T>>): T;
|
|
49
|
-
export declare const modelRegistry: Record<string, typeof Model>;
|
|
50
38
|
export type Change = Record<any, any> | "created" | "deleted";
|
|
39
|
+
type FieldsOf<T> = T extends new () => infer I ? I : never;
|
|
40
|
+
type PKArgs<FIELDS, PK> = PK extends readonly (keyof FIELDS & string)[] ? {
|
|
41
|
+
[I in keyof PK]: PK[I] extends keyof FIELDS ? FIELDS[PK[I]] : never;
|
|
42
|
+
} : PK extends keyof FIELDS & string ? [FIELDS[PK]] : [string];
|
|
43
|
+
type IndexArgs<FIELDS, SPEC> = SPEC extends readonly (keyof FIELDS & string)[] ? {
|
|
44
|
+
[I in keyof SPEC]: SPEC[I] extends keyof FIELDS ? FIELDS[SPEC[I]] : never;
|
|
45
|
+
} : SPEC extends keyof FIELDS & string ? [FIELDS[SPEC]] : SPEC extends (instance: any) => infer R ? R extends (infer V)[] ? [V] : [R] : never;
|
|
51
46
|
/**
|
|
52
|
-
*
|
|
47
|
+
* A model constructor with its generic information erased.
|
|
53
48
|
*
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
* Useful when accepting or storing arbitrary registered model classes.
|
|
50
|
+
*/
|
|
51
|
+
export type AnyModelClass = ModelClass<new () => any, readonly any[], any, any>;
|
|
52
|
+
type SecondaryRegistry<FIELDS> = Record<string, NonPrimaryIndex<Model<FIELDS>, readonly (keyof FIELDS & string)[], readonly any[]>>;
|
|
53
|
+
export declare const modelRegistry: Record<string, AnyModelClass>;
|
|
54
|
+
export declare const pendingModelInits: Set<AnyModelClass>;
|
|
55
|
+
declare class ModelClassRuntime<FIELDS, PKA extends readonly any[], UNIQUE = {}, INDEX = {}> extends PrimaryKey<Model<FIELDS>, readonly (keyof FIELDS & string)[], PKA> {
|
|
56
|
+
tableName: string;
|
|
57
|
+
fields: Record<string | symbol | number, FieldConfig<unknown>>;
|
|
58
|
+
_secondaries?: SecondaryRegistry<FIELDS>;
|
|
59
|
+
_nonKeyFields: (keyof FIELDS & string)[];
|
|
60
|
+
_lazyDescriptors: Record<string | symbol | number, PropertyDescriptor>;
|
|
61
|
+
_resetDescriptors: Record<string | symbol | number, PropertyDescriptor>;
|
|
62
|
+
_freezePrimaryKeyDescriptors: Record<string | symbol | number, PropertyDescriptor>;
|
|
63
|
+
_currentVersion: number;
|
|
64
|
+
_currentMigrateHash: number;
|
|
65
|
+
_versions: Map<number, VersionInfo>;
|
|
66
|
+
_serializeVersionValue(): Uint8Array;
|
|
67
|
+
_initialize(reset?: boolean): Promise<void>;
|
|
68
|
+
_getSecondary(name: string): NonPrimaryIndex<Model<FIELDS>, readonly (keyof FIELDS & string)[], readonly any[]>;
|
|
69
|
+
_get(txn: Transaction, args: PKA | Uint8Array, loadNow: false | Uint8Array): Model<FIELDS>;
|
|
70
|
+
_get(txn: Transaction, args: PKA | Uint8Array, loadNow: true): Model<FIELDS> | undefined;
|
|
71
|
+
_lazyLoad(model: Model<FIELDS>): void;
|
|
72
|
+
/**
|
|
73
|
+
* Load a model by primary key inside the current transaction.
|
|
74
|
+
*
|
|
75
|
+
* @returns The matching model, or `undefined` if no row exists.
|
|
76
|
+
*/
|
|
77
|
+
get(...args: PKA): Model<FIELDS> | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Load a model by primary key without fetching its non-key fields immediately.
|
|
80
|
+
*
|
|
81
|
+
* Accessing a lazy field later will load the remaining fields transparently.
|
|
82
|
+
*/
|
|
83
|
+
getLazy(...args: PKA): Model<FIELDS>;
|
|
84
|
+
_pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, valueBuffer: ArrayBuffer): Model<FIELDS>;
|
|
85
|
+
/**
|
|
86
|
+
* Load an existing instance by primary key and update it, or create a new one.
|
|
87
|
+
* If a row already exists, its non-primary-key fields are updated in place.
|
|
88
|
+
* Otherwise, a new instance is created with `obj` as its initial properties.
|
|
89
|
+
*
|
|
90
|
+
* @param obj Partial model data that **must** include every primary key field.
|
|
91
|
+
* @returns The loaded-and-updated or newly created instance.
|
|
92
|
+
*/
|
|
93
|
+
replaceInto(obj: Partial<FIELDS>): Model<FIELDS>;
|
|
94
|
+
/**
|
|
95
|
+
* Look up a model through a named unique index.
|
|
96
|
+
*
|
|
97
|
+
* @param name The name from the model's `unique` definition.
|
|
98
|
+
* @param args The unique-index key values.
|
|
99
|
+
* @returns The matching model instance, if any.
|
|
100
|
+
*/
|
|
101
|
+
getBy<K extends string & keyof UNIQUE>(name: K, ...args: IndexArgs<FIELDS, UNIQUE[K]>): Model<FIELDS> | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Query rows through a named unique or secondary index.
|
|
104
|
+
*
|
|
105
|
+
* This mirrors `find()`, but targets a named entry from the model's `unique`
|
|
106
|
+
* or `index` registration.
|
|
107
|
+
*/
|
|
108
|
+
findBy<K extends string & keyof (UNIQUE & INDEX)>(name: K, opts: FindOptions<IndexArgs<FIELDS, (UNIQUE & INDEX)[K]>, 'first'>): Model<FIELDS> | undefined;
|
|
109
|
+
findBy<K extends string & keyof (UNIQUE & INDEX)>(name: K, opts: FindOptions<IndexArgs<FIELDS, (UNIQUE & INDEX)[K]>, 'single'>): Model<FIELDS>;
|
|
110
|
+
findBy<K extends string & keyof (UNIQUE & INDEX)>(name: K, opts?: FindOptions<IndexArgs<FIELDS, (UNIQUE & INDEX)[K]>>): IndexRangeIterator<Model<FIELDS>>;
|
|
111
|
+
/**
|
|
112
|
+
* Process rows from a named unique or secondary index in batched transactions.
|
|
113
|
+
*
|
|
114
|
+
* Uses the same range options as `findBy()`, plus batch limits.
|
|
115
|
+
*/
|
|
116
|
+
batchProcessBy<K extends string & keyof (UNIQUE & INDEX)>(name: K, opts: FindOptions<IndexArgs<FIELDS, (UNIQUE & INDEX)[K]>> & {
|
|
117
|
+
limitSeconds?: number;
|
|
118
|
+
limitRows?: number;
|
|
119
|
+
}, callback: (row: Model<FIELDS>) => any): Promise<void>;
|
|
120
|
+
_loadValueFields(model: Model<FIELDS>, valueArray: Uint8Array): void;
|
|
121
|
+
_loadVersionInfo(txnId: number, version: number): VersionInfo;
|
|
122
|
+
_migrateValueFields(model: Model<FIELDS>, version: number, valuePack: DataPack): void;
|
|
123
|
+
_serializeValue(data: Record<string, any>): Uint8Array;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Runtime base constructor for model classes returned by `defineModel()`.
|
|
57
127
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @E.registerModel
|
|
61
|
-
* class User extends E.Model<User> {
|
|
62
|
-
* static pk = E.index(User, ["id"], "primary");
|
|
63
|
-
* id = E.field(E.identifier);
|
|
64
|
-
* name = E.field(E.string);
|
|
65
|
-
* }
|
|
66
|
-
* ```
|
|
128
|
+
* Prefer the `ModelClass` type alias for annotations and the result of
|
|
129
|
+
* `defineModel()` for concrete model classes.
|
|
67
130
|
*/
|
|
68
|
-
export declare
|
|
69
|
-
export declare function getMockModel<T extends typeof Model<unknown>>(OrgModel: T): T;
|
|
131
|
+
export declare const ModelClass: typeof ModelClassRuntime;
|
|
70
132
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
133
|
+
* The static side of a model class returned by `defineModel()`.
|
|
134
|
+
*
|
|
135
|
+
* Besides the class constructor itself, this includes primary-key lookup
|
|
136
|
+
* helpers like `get()` and `getLazy()`, range-query helpers like `find()`, and
|
|
137
|
+
* named-index helpers like `getBy()` and `findBy()`.
|
|
138
|
+
*
|
|
139
|
+
* @template T - The original class passed to `defineModel()`.
|
|
140
|
+
* @template PKA - Tuple of primary-key argument types.
|
|
141
|
+
* @template UNIQUE - Named unique-index specifications.
|
|
142
|
+
* @template INDEX - Named secondary-index specifications.
|
|
143
|
+
*/
|
|
144
|
+
export type ModelClass<T extends new () => any, PKA extends readonly any[], UNIQUE = {}, INDEX = {}> = T & ModelClassRuntime<FieldsOf<T>, PKA, UNIQUE, INDEX> & {
|
|
145
|
+
new (initial?: Partial<FieldsOf<T>>, txn?: Transaction): Model<FieldsOf<T>>;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Minimal instance-side model shape used for typing the constructor property.
|
|
73
149
|
*/
|
|
74
|
-
export interface
|
|
75
|
-
constructor:
|
|
150
|
+
export interface ModelBase {
|
|
151
|
+
constructor: AnyModelClass;
|
|
76
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Register a model class with the Edinburgh ORM system.
|
|
155
|
+
*
|
|
156
|
+
* Converts a plain class into a fully-featured model with database persistence,
|
|
157
|
+
* typed fields, primary key access, and optional secondary and unique indexes.
|
|
158
|
+
*
|
|
159
|
+
* @param tableName The database table name for this model.
|
|
160
|
+
* @param cls A plain class whose properties use E.field().
|
|
161
|
+
* @param opts Registration options.
|
|
162
|
+
* @param opts.pk Primary key field name or array of field names.
|
|
163
|
+
* @param opts.unique Named unique index specifications (field name, field array, or compute function).
|
|
164
|
+
* @param opts.index Named secondary index specifications (field name, field array, or compute function).
|
|
165
|
+
* @param opts.override Replace a previous model with the same table name.
|
|
166
|
+
* @returns The enhanced model constructor.
|
|
167
|
+
*/
|
|
168
|
+
export declare function defineModel<T extends new () => any, const PK extends (keyof FieldsOf<T> & string) | readonly (keyof FieldsOf<T> & string)[], const UNIQUE extends Record<string, (keyof FieldsOf<T> & string) | readonly (keyof FieldsOf<T> & string)[] | ((instance: any) => any)>, const INDEX extends Record<string, (keyof FieldsOf<T> & string) | readonly (keyof FieldsOf<T> & string)[] | ((instance: any) => any)>>(tableName: string, cls: T, opts?: {
|
|
169
|
+
pk?: PK;
|
|
170
|
+
unique?: UNIQUE;
|
|
171
|
+
index?: INDEX;
|
|
172
|
+
override?: boolean;
|
|
173
|
+
}): ModelClass<T, PKArgs<FieldsOf<T>, PK>, UNIQUE, INDEX>;
|
|
77
174
|
/**
|
|
78
175
|
* Base class for all database models in the Edinburgh ORM.
|
|
79
176
|
*
|
|
80
177
|
* Models represent database entities with typed fields, automatic serialization,
|
|
81
|
-
* change tracking, and relationship management.
|
|
82
|
-
*
|
|
178
|
+
* change tracking, and relationship management. Model classes are created using
|
|
179
|
+
* `E.defineModel()`.
|
|
83
180
|
*
|
|
84
181
|
* ### Schema Evolution
|
|
85
182
|
*
|
|
@@ -105,65 +202,46 @@ export interface Model<SUB> {
|
|
|
105
202
|
*
|
|
106
203
|
* - **`static migrate(record)`**: Called when deserializing rows written with an older schema
|
|
107
204
|
* version. Receives a plain record object; mutate it in-place to match the current schema.
|
|
108
|
-
* See {@link Model.migrate}.
|
|
109
205
|
*
|
|
110
206
|
* - **`preCommit()`**: Called on each modified instance right before the transaction commits.
|
|
111
207
|
* Useful for computing derived fields, enforcing cross-field invariants, or creating related
|
|
112
|
-
* instances.
|
|
113
|
-
*
|
|
114
|
-
* @template SUB - The concrete model subclass (for proper typing).
|
|
208
|
+
* instances.
|
|
115
209
|
*
|
|
116
210
|
* @example
|
|
117
211
|
* ```typescript
|
|
118
|
-
*
|
|
119
|
-
* class User extends E.Model<User> {
|
|
120
|
-
* static pk = E.primary(User, "id");
|
|
121
|
-
*
|
|
212
|
+
* const User = E.defineModel("User", class {
|
|
122
213
|
* id = E.field(E.identifier);
|
|
123
214
|
* name = E.field(E.string);
|
|
124
215
|
* email = E.field(E.string);
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* }
|
|
216
|
+
* }, {
|
|
217
|
+
* pk: "id",
|
|
218
|
+
* unique: { email: "email" },
|
|
219
|
+
* });
|
|
220
|
+
* // Optional: declare a companion type so `let u: User` works.
|
|
221
|
+
* // Not needed if you only use `new User()`, `User.find()`, etc.
|
|
222
|
+
* type User = InstanceType<typeof User>;
|
|
128
223
|
* ```
|
|
129
224
|
*/
|
|
130
|
-
export declare abstract class
|
|
131
|
-
static _primary: PrimaryIndex<any, any>;
|
|
132
|
-
/** @internal All non-primary indexes for this model. */
|
|
133
|
-
static _secondaries?: BaseIndex<any, readonly (keyof any & string)[]>[];
|
|
134
|
-
/** The database table name (defaults to class name). */
|
|
135
|
-
static tableName: string;
|
|
136
|
-
/** When true, registerModel replaces an existing model with the same tableName. */
|
|
137
|
-
static override?: boolean;
|
|
138
|
-
/** Field configuration metadata. */
|
|
139
|
-
static fields: Record<string | symbol | number, FieldConfig<unknown>>;
|
|
225
|
+
export declare abstract class ModelBase {
|
|
140
226
|
/**
|
|
141
227
|
* Optional migration function called when deserializing rows written with an older schema version.
|
|
142
|
-
|
|
143
|
-
|
|
228
|
+
* Receives a plain record with all fields and should mutate it in-place to match the current schema.
|
|
229
|
+
* It runs during lazy loading and during `runMigration()`. Changing this method creates a new schema version.
|
|
230
|
+
* If it updates values used by secondary or unique indexes, those index entries are refreshed only by `runMigration()`.
|
|
144
231
|
*
|
|
145
|
-
*
|
|
146
|
-
* migration (via `runMigration()` / `npx migrate-edinburgh`). The function's source code is hashed
|
|
147
|
-
* to detect changes. Modifying `migrate()` triggers a new schema version.
|
|
148
|
-
*
|
|
149
|
-
* If `migrate()` changes values of fields used in secondary or unique indexes, those indexes
|
|
150
|
-
* will only be updated when `runMigration()` is run (not during lazy loading).
|
|
151
|
-
*
|
|
152
|
-
* @param record - A plain object with all field values from the old schema version.
|
|
232
|
+
* @param record A plain object containing the row's field values from the older schema version.
|
|
153
233
|
*
|
|
154
234
|
* @example
|
|
155
235
|
* ```typescript
|
|
156
|
-
*
|
|
157
|
-
* class User extends E.Model<User> {
|
|
158
|
-
* static pk = E.primary(User, "id");
|
|
236
|
+
* const User = E.defineModel("User", class {
|
|
159
237
|
* id = E.field(E.identifier);
|
|
160
238
|
* name = E.field(E.string);
|
|
161
|
-
* role = E.field(E.string);
|
|
239
|
+
* role = E.field(E.string);
|
|
162
240
|
*
|
|
163
241
|
* static migrate(record: Record<string, any>) {
|
|
164
|
-
* record.role ??= "user";
|
|
242
|
+
* record.role ??= "user";
|
|
165
243
|
* }
|
|
166
|
-
* }
|
|
244
|
+
* }, { pk: "id" });
|
|
167
245
|
* ```
|
|
168
246
|
*/
|
|
169
247
|
static migrate?(record: Record<string, any>): void;
|
|
@@ -171,13 +249,13 @@ export declare abstract class Model<SUB> {
|
|
|
171
249
|
* @internal
|
|
172
250
|
* - _oldValues===undefined: New instance, not yet saved.
|
|
173
251
|
* - _oldValues===null: Instance is to be deleted.
|
|
252
|
+
* - _oldValues===false: Instance excluded from persistence (preventPersist).
|
|
174
253
|
* - _oldValues is an object: Loaded (possibly only partial, still lazy) from disk, _oldValues contains (partial) old values
|
|
175
254
|
*/
|
|
176
|
-
_oldValues: Record<string, any> | undefined | null;
|
|
255
|
+
_oldValues: Record<string, any> | undefined | null | false;
|
|
177
256
|
_primaryKey: Uint8Array | undefined;
|
|
178
257
|
_primaryKeyHash: number | undefined;
|
|
179
258
|
_txn: Transaction;
|
|
180
|
-
constructor(initial?: Partial<Omit<SUB, "constructor">>);
|
|
181
259
|
/**
|
|
182
260
|
* Optional hook called on each modified instance right before the transaction commits.
|
|
183
261
|
* Runs before data is written to disk, so changes made here are included in the commit.
|
|
@@ -190,9 +268,7 @@ export declare abstract class Model<SUB> {
|
|
|
190
268
|
*
|
|
191
269
|
* @example
|
|
192
270
|
* ```typescript
|
|
193
|
-
*
|
|
194
|
-
* class Post extends E.Model<Post> {
|
|
195
|
-
* static pk = E.primary(Post, "id");
|
|
271
|
+
* const Post = E.defineModel("Post", class {
|
|
196
272
|
* id = E.field(E.identifier);
|
|
197
273
|
* title = E.field(E.string);
|
|
198
274
|
* slug = E.field(E.string);
|
|
@@ -200,18 +276,12 @@ export declare abstract class Model<SUB> {
|
|
|
200
276
|
* preCommit() {
|
|
201
277
|
* this.slug = this.title.toLowerCase().replace(/\s+/g, "-");
|
|
202
278
|
* }
|
|
203
|
-
* }
|
|
279
|
+
* }, { pk: "id" });
|
|
204
280
|
* ```
|
|
205
281
|
*/
|
|
206
282
|
preCommit?(): void;
|
|
207
|
-
/**
|
|
208
|
-
* Transform the model's `E.field` properties into the appropriate JavaScript properties. Normally this is done
|
|
209
|
-
* automatically when using `transact()`, but in case you need to access `Model.fields` directly before the first
|
|
210
|
-
* transaction, you can call this method manually.
|
|
211
|
-
*/
|
|
212
|
-
static initFields(reset?: boolean): void;
|
|
213
|
-
static _loadCreateIndexes(): Promise<void>;
|
|
214
283
|
_setLoadedField(fieldName: string, value: any): void;
|
|
284
|
+
_restoreLazyFields(): void;
|
|
215
285
|
/**
|
|
216
286
|
* @returns The primary key for this instance.
|
|
217
287
|
*/
|
|
@@ -230,32 +300,12 @@ export declare abstract class Model<SUB> {
|
|
|
230
300
|
*
|
|
231
301
|
* @example
|
|
232
302
|
* ```typescript
|
|
233
|
-
* const user = User.
|
|
303
|
+
* const user = User.get("user123");
|
|
234
304
|
* user.name = "New Name";
|
|
235
305
|
* user.preventPersist(); // Changes won't be saved
|
|
236
306
|
* ```
|
|
237
307
|
*/
|
|
238
308
|
preventPersist(): this;
|
|
239
|
-
/**
|
|
240
|
-
* Find all instances of this model in the database, ordered by primary key.
|
|
241
|
-
* @param opts - Optional parameters.
|
|
242
|
-
* @param opts.reverse - If true, iterate in reverse order.
|
|
243
|
-
* @returns An iterator.
|
|
244
|
-
*/
|
|
245
|
-
static findAll<T extends typeof Model<unknown>>(this: T, opts?: {
|
|
246
|
-
reverse?: boolean;
|
|
247
|
-
}): IndexRangeIterator<T>;
|
|
248
|
-
/**
|
|
249
|
-
* Load an existing instance by primary key and update it, or create a new one.
|
|
250
|
-
*
|
|
251
|
-
* The provided object must contain all primary key fields. If a matching row exists,
|
|
252
|
-
* the remaining properties from `obj` are set on the loaded instance. Otherwise a
|
|
253
|
-
* new instance is created with `obj` as its initial properties.
|
|
254
|
-
*
|
|
255
|
-
* @param obj - Partial model data that **must** include every primary key field.
|
|
256
|
-
* @returns The loaded-and-updated or newly created instance.
|
|
257
|
-
*/
|
|
258
|
-
static replaceInto<T extends typeof Model<any>>(this: T, obj: Partial<Omit<InstanceType<T>, "constructor">>): InstanceType<T>;
|
|
259
309
|
/**
|
|
260
310
|
* Delete this model instance from the database.
|
|
261
311
|
*
|
|
@@ -263,14 +313,14 @@ export declare abstract class Model<SUB> {
|
|
|
263
313
|
*
|
|
264
314
|
* @example
|
|
265
315
|
* ```typescript
|
|
266
|
-
* const user = User.
|
|
316
|
+
* const user = User.get("user123");
|
|
267
317
|
* user.delete(); // Removes from database
|
|
268
318
|
* ```
|
|
269
319
|
*/
|
|
270
320
|
delete(): void;
|
|
271
321
|
/**
|
|
272
322
|
* Validate all fields in this model instance.
|
|
273
|
-
* @param raise
|
|
323
|
+
* @param raise If true, throw on first validation error.
|
|
274
324
|
* @returns Array of validation errors (empty if valid).
|
|
275
325
|
*
|
|
276
326
|
* @example
|
|
@@ -297,3 +347,17 @@ export declare abstract class Model<SUB> {
|
|
|
297
347
|
getState(): "deleted" | "created" | "loaded" | "lazy";
|
|
298
348
|
toString(): string;
|
|
299
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Delete every key/value entry in the database and reinitialize all registered models.
|
|
352
|
+
*
|
|
353
|
+
* This clears rows, index metadata, and schema-version records. It is mainly useful
|
|
354
|
+
* for tests, local resets, or tooling that needs a completely empty database.
|
|
355
|
+
*/
|
|
356
|
+
export declare function deleteEverything(): Promise<void>;
|
|
357
|
+
/**
|
|
358
|
+
* A model instance, including its user-defined fields.
|
|
359
|
+
* @template FIELDS - The fields defined on this model.
|
|
360
|
+
*/
|
|
361
|
+
export type Model<FIELDS> = FIELDS & ModelBase;
|
|
362
|
+
export declare const Model: typeof ModelBase;
|
|
363
|
+
export {};
|