edinburgh 0.4.6 → 0.5.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 +263 -381
- package/build/src/datapack.js +1 -1
- package/build/src/datapack.js.map +1 -1
- package/build/src/edinburgh.d.ts +5 -5
- package/build/src/edinburgh.js +6 -7
- package/build/src/edinburgh.js.map +1 -1
- package/build/src/indexes.d.ts +44 -113
- package/build/src/indexes.js +145 -175
- package/build/src/indexes.js.map +1 -1
- package/build/src/migrate.js +11 -31
- package/build/src/migrate.js.map +1 -1
- package/build/src/models.d.ts +73 -54
- package/build/src/models.js +110 -171
- package/build/src/models.js.map +1 -1
- package/build/src/types.d.ts +29 -21
- package/build/src/types.js +16 -30
- package/build/src/types.js.map +1 -1
- package/package.json +1 -3
- package/skill/BaseIndex_batchProcess.md +1 -1
- package/skill/BaseIndex_find.md +2 -2
- package/skill/BaseIndex_find_2.md +7 -0
- package/skill/BaseIndex_find_3.md +7 -0
- package/skill/BaseIndex_find_4.md +7 -0
- package/skill/Model.md +5 -7
- package/skill/Model_batchProcess.md +8 -0
- package/skill/Model_delete.md +1 -1
- package/skill/Model_migrate.md +2 -4
- package/skill/Model_preCommit.md +2 -4
- package/skill/Model_preventPersist.md +1 -1
- package/skill/Model_replaceInto.md +2 -2
- package/skill/NonPrimaryIndex.md +10 -0
- package/skill/SKILL.md +140 -150
- package/skill/SecondaryIndex.md +2 -2
- package/skill/UniqueIndex.md +2 -2
- package/skill/defineModel.md +22 -0
- package/skill/field.md +2 -2
- package/skill/link.md +11 -9
- package/skill/transact.md +2 -2
- package/src/datapack.ts +1 -1
- package/src/edinburgh.ts +6 -9
- package/src/indexes.ts +155 -271
- package/src/migrate.ts +9 -30
- package/src/models.ts +186 -180
- package/src/types.ts +31 -26
- package/skill/Model_findAll.md +0 -12
- package/skill/PrimaryIndex.md +0 -8
- package/skill/PrimaryIndex_get.md +0 -17
- package/skill/PrimaryIndex_getLazy.md +0 -13
- package/skill/UniqueIndex_get.md +0 -17
- 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/migrate.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as lowlevel from "olmdb/lowlevel";
|
|
2
2
|
import DataPack from "./datapack.js";
|
|
3
3
|
import { modelRegistry, currentTxn } from "./models.js";
|
|
4
|
-
import { dbDel, toBuffer
|
|
4
|
+
import { dbDel, toBuffer } from "./utils.js";
|
|
5
5
|
import { deserializeType } from "./types.js";
|
|
6
6
|
import { transact } from "./edinburgh.js";
|
|
7
7
|
const INDEX_ID_PREFIX = -2;
|
|
@@ -73,13 +73,12 @@ export async function runMigration(options = {}) {
|
|
|
73
73
|
};
|
|
74
74
|
// Build maps of known index IDs
|
|
75
75
|
const knownIndexIds = new Set();
|
|
76
|
-
const
|
|
76
|
+
const modelByPkIndexId = new Map();
|
|
77
77
|
for (const model of Object.values(modelRegistry)) {
|
|
78
78
|
if (options.tables && !options.tables.includes(model.tableName))
|
|
79
79
|
continue;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
primaryByIndexId.set(primary._indexId, { model, primary });
|
|
80
|
+
knownIndexIds.add(model._indexId);
|
|
81
|
+
modelByPkIndexId.set(model._indexId, model);
|
|
83
82
|
for (const sec of model._secondaries || []) {
|
|
84
83
|
knownIndexIds.add(sec._indexId);
|
|
85
84
|
}
|
|
@@ -110,7 +109,7 @@ export async function runMigration(options = {}) {
|
|
|
110
109
|
});
|
|
111
110
|
// Phase 1: Populate secondary indexes and/or rewrite row data
|
|
112
111
|
if (populateSecondaries || rewriteData) {
|
|
113
|
-
for (const [indexId,
|
|
112
|
+
for (const [indexId, model] of modelByPkIndexId) {
|
|
114
113
|
let secondaryCount = 0;
|
|
115
114
|
let rewrittenCount = 0;
|
|
116
115
|
const migrateFn = model.migrate;
|
|
@@ -118,14 +117,14 @@ export async function runMigration(options = {}) {
|
|
|
118
117
|
await forEachRow(indexId, (txn, keyBuf, valueBuf) => {
|
|
119
118
|
const valuePack = new DataPack(valueBuf);
|
|
120
119
|
const version = valuePack.readNumber();
|
|
121
|
-
if (version ===
|
|
120
|
+
if (version === model._currentVersion)
|
|
122
121
|
return; // Already current
|
|
123
|
-
const versionInfo =
|
|
122
|
+
const versionInfo = model._loadVersionInfo(txn.id, version);
|
|
124
123
|
// Deserialize pre-migrate values from key + old-format value
|
|
125
124
|
const record = {};
|
|
126
125
|
const keyPack = new DataPack(keyBuf);
|
|
127
126
|
keyPack.readNumber(); // skip indexId
|
|
128
|
-
for (const [name, type] of
|
|
127
|
+
for (const [name, type] of model._pkFieldTypes.entries()) {
|
|
129
128
|
record[name] = type.deserialize(keyPack);
|
|
130
129
|
}
|
|
131
130
|
for (const [name, type] of versionInfo.nonKeyFields.entries()) {
|
|
@@ -144,33 +143,14 @@ export async function runMigration(options = {}) {
|
|
|
144
143
|
secondaryCount++;
|
|
145
144
|
}
|
|
146
145
|
else if (preMigrate) {
|
|
147
|
-
if (sec.
|
|
148
|
-
|
|
149
|
-
const oldKeyBytes = sec._serializeKeyFields(preMigrate).toUint8Array();
|
|
150
|
-
const newKeyBytes = sec._serializeKeyFields(record).toUint8Array();
|
|
151
|
-
if (!bytesEqual(oldKeyBytes, newKeyBytes)) {
|
|
152
|
-
sec._delete(txn, keyBuf, preMigrate);
|
|
153
|
-
sec._write(txn, keyBuf, record);
|
|
154
|
-
secondaryCount++;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
// Existing secondary, update if migrate changed any of its fields
|
|
159
|
-
for (const [field, type] of sec._fieldTypes.entries()) {
|
|
160
|
-
if (!type.equals(preMigrate[field], record[field])) {
|
|
161
|
-
sec._delete(txn, keyBuf, preMigrate);
|
|
162
|
-
sec._write(txn, keyBuf, record);
|
|
163
|
-
secondaryCount++;
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
146
|
+
if (sec._update(txn, keyBuf, record, preMigrate))
|
|
147
|
+
secondaryCount++;
|
|
168
148
|
}
|
|
169
149
|
}
|
|
170
150
|
}
|
|
171
151
|
// Rewrite primary row data to current version
|
|
172
152
|
if (rewriteData) {
|
|
173
|
-
|
|
153
|
+
model._writePrimary(txn, keyBuf, record);
|
|
174
154
|
rewrittenCount++;
|
|
175
155
|
}
|
|
176
156
|
});
|
package/build/src/migrate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate.js","sourceRoot":"","sources":["../../src/migrate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAC3C,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAe,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"migrate.js","sourceRoot":"","sources":["../../src/migrate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAC3C,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAe,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAc,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,eAAe,EAAe,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,MAAM,eAAe,GAAG,CAAC,CAAC,CAAC;AA6C3B;;;GAGG;AACH,KAAK,UAAU,UAAU,CACrB,OAAe,EACf,YAA4E;IAE5E,IAAI,IAAI,GAAG,KAAK,CAAC;IACjB,IAAI,OAA+B,CAAC;IACpC,MAAM,UAAU,GAAG,IAAI,QAAQ,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,QAAQ,CAAC,UAAU,CAAC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,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACnC,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,QAAQ,CAAC;YAC3D,CAAC;QACL,CAAC;IACL,CAAC;IAED,0DAA0D;IAC1D,IAAI,aAAa,EAAE,CAAC;QAChB,KAAK,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;YAC7B,IAAI,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,QAAQ,KAAK,SAAS;gBAAE,SAAS;YACtE,MAAM,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;gBACrC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;gBACtB,MAAM,CAAC,OAAO,EAAE,CAAC;YACrB,CAAC,CAAC,CAAC;YACH,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;QAClE,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
package/build/src/models.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export interface Transaction {
|
|
|
12
12
|
instances: Set<Model<unknown>>;
|
|
13
13
|
instancesByPk: Map<number, Model<unknown>>;
|
|
14
14
|
}
|
|
15
|
-
import {
|
|
15
|
+
import { NonPrimaryIndex, PrimaryIndex, IndexRangeIterator, UniqueIndex, SecondaryIndex, FindOptions } from "./indexes.js";
|
|
16
16
|
/**
|
|
17
17
|
* Configuration interface for model fields.
|
|
18
18
|
* @template T - The field type.
|
|
@@ -39,34 +39,63 @@ export interface FieldConfig<T> {
|
|
|
39
39
|
*
|
|
40
40
|
* @example
|
|
41
41
|
* ```typescript
|
|
42
|
-
*
|
|
42
|
+
* const User = E.defineModel(class {
|
|
43
43
|
* name = E.field(E.string, {description: "User's full name"});
|
|
44
44
|
* age = E.field(E.opt(E.number), {description: "User's age", default: 25});
|
|
45
|
-
* }
|
|
45
|
+
* });
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
48
|
export declare function field<T>(type: TypeWrapper<T>, options?: Partial<FieldConfig<T>>): T;
|
|
49
49
|
export declare const modelRegistry: Record<string, typeof Model>;
|
|
50
50
|
export type Change = Record<any, any> | "created" | "deleted";
|
|
51
|
+
type FieldsOf<T> = T extends new () => infer I ? I : never;
|
|
52
|
+
type ModelInstance<FIELDS> = FIELDS & Model<FIELDS>;
|
|
53
|
+
type PKArgs<FIELDS, PK> = PK extends readonly (keyof FIELDS & string)[] ? {
|
|
54
|
+
[I in keyof PK]: PK[I] extends keyof FIELDS ? FIELDS[PK[I]] : never;
|
|
55
|
+
} : PK extends keyof FIELDS & string ? [FIELDS[PK]] : [string];
|
|
56
|
+
type UniqueFor<SPEC> = SPEC extends readonly string[] ? UniqueIndex<any, SPEC> : SPEC extends string ? UniqueIndex<any, [SPEC]> : SPEC extends (instance: any) => infer R ? R extends (infer V)[] ? UniqueIndex<any, [], [V]> : UniqueIndex<any, [], [R]> : never;
|
|
57
|
+
type SecondaryFor<SPEC> = SPEC extends readonly string[] ? SecondaryIndex<any, SPEC> : SPEC extends string ? SecondaryIndex<any, [SPEC]> : SPEC extends (instance: any) => infer R ? R extends (infer V)[] ? SecondaryIndex<any, [], [V]> : SecondaryIndex<any, [], [R]> : never;
|
|
58
|
+
type RegisteredModel<FIELDS, PKA extends readonly any[], UNIQUE, INDEX> = {
|
|
59
|
+
new (initial?: Partial<FIELDS>): ModelInstance<FIELDS>;
|
|
60
|
+
tableName: string;
|
|
61
|
+
fields: Record<string | symbol | number, FieldConfig<unknown>>;
|
|
62
|
+
get(...args: PKA): ModelInstance<FIELDS> | undefined;
|
|
63
|
+
getLazy(...args: PKA): ModelInstance<FIELDS>;
|
|
64
|
+
find(opts: FindOptions<PKA, 'first'>): ModelInstance<FIELDS> | undefined;
|
|
65
|
+
find(opts: FindOptions<PKA, 'single'>): ModelInstance<FIELDS>;
|
|
66
|
+
find(opts?: FindOptions<PKA>): IndexRangeIterator<any>;
|
|
67
|
+
batchProcess(opts?: FindOptions<PKA> & {
|
|
68
|
+
limitSeconds?: number;
|
|
69
|
+
limitRows?: number;
|
|
70
|
+
}, callback?: (row: ModelInstance<FIELDS>) => any): Promise<void>;
|
|
71
|
+
replaceInto(obj: Partial<FIELDS>): ModelInstance<FIELDS>;
|
|
72
|
+
} & {
|
|
73
|
+
[K in keyof UNIQUE]: UniqueFor<UNIQUE[K]>;
|
|
74
|
+
} & {
|
|
75
|
+
[K in keyof INDEX]: SecondaryFor<INDEX[K]>;
|
|
76
|
+
};
|
|
51
77
|
/**
|
|
52
78
|
* Register a model class with the Edinburgh ORM system.
|
|
53
79
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* @returns The enhanced model class with ORM capabilities.
|
|
80
|
+
* Converts a plain class into a fully-featured model with database persistence,
|
|
81
|
+
* typed fields, primary key access, and optional secondary and unique indexes.
|
|
57
82
|
*
|
|
58
|
-
* @
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* ```
|
|
83
|
+
* @param cls - A plain class whose properties use E.field().
|
|
84
|
+
* @param opts - Registration options.
|
|
85
|
+
* @param opts.pk - Primary key field name or array of field names.
|
|
86
|
+
* @param opts.unique - Named unique index specifications (field name, field array, or compute function).
|
|
87
|
+
* @param opts.index - Named secondary index specifications (field name, field array, or compute function).
|
|
88
|
+
* @param opts.tableName - Explicit database table name.
|
|
89
|
+
* @param opts.override - Replace a previous model with the same table name.
|
|
90
|
+
* @returns The enhanced model constructor.
|
|
67
91
|
*/
|
|
68
|
-
export declare function
|
|
69
|
-
|
|
92
|
+
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)>>(cls: T, opts?: {
|
|
93
|
+
pk?: PK;
|
|
94
|
+
unique?: UNIQUE;
|
|
95
|
+
index?: INDEX;
|
|
96
|
+
tableName?: string;
|
|
97
|
+
override?: boolean;
|
|
98
|
+
}): RegisteredModel<FieldsOf<T>, PKArgs<FieldsOf<T>, PK>, UNIQUE, INDEX>;
|
|
70
99
|
/**
|
|
71
100
|
* Model interface that ensures proper typing for the constructor property.
|
|
72
101
|
* @template SUB - The concrete model subclass.
|
|
@@ -78,8 +107,8 @@ export interface Model<SUB> {
|
|
|
78
107
|
* Base class for all database models in the Edinburgh ORM.
|
|
79
108
|
*
|
|
80
109
|
* Models represent database entities with typed fields, automatic serialization,
|
|
81
|
-
* change tracking, and relationship management.
|
|
82
|
-
*
|
|
110
|
+
* change tracking, and relationship management. Model classes are created using
|
|
111
|
+
* `E.defineModel()`.
|
|
83
112
|
*
|
|
84
113
|
* ### Schema Evolution
|
|
85
114
|
*
|
|
@@ -115,28 +144,31 @@ export interface Model<SUB> {
|
|
|
115
144
|
*
|
|
116
145
|
* @example
|
|
117
146
|
* ```typescript
|
|
118
|
-
*
|
|
119
|
-
* class User extends E.Model<User> {
|
|
120
|
-
* static pk = E.primary(User, "id");
|
|
121
|
-
*
|
|
147
|
+
* const User = E.defineModel(class {
|
|
122
148
|
* id = E.field(E.identifier);
|
|
123
149
|
* name = E.field(E.string);
|
|
124
150
|
* email = E.field(E.string);
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* }
|
|
151
|
+
* }, {
|
|
152
|
+
* pk: "id",
|
|
153
|
+
* unique: { byEmail: "email" },
|
|
154
|
+
* });
|
|
128
155
|
* ```
|
|
129
156
|
*/
|
|
130
157
|
export declare abstract class Model<SUB> {
|
|
131
158
|
static _primary: PrimaryIndex<any, any>;
|
|
132
159
|
/** @internal All non-primary indexes for this model. */
|
|
133
|
-
static _secondaries?:
|
|
160
|
+
static _secondaries?: NonPrimaryIndex<any, readonly (keyof any & string)[]>[];
|
|
134
161
|
/** The database table name (defaults to class name). */
|
|
135
162
|
static tableName: string;
|
|
136
|
-
/** When true,
|
|
163
|
+
/** When true, defineModel replaces an existing model with the same tableName. */
|
|
137
164
|
static override?: boolean;
|
|
138
165
|
/** Field configuration metadata. */
|
|
139
166
|
static fields: Record<string | symbol | number, FieldConfig<unknown>>;
|
|
167
|
+
static get _indexId(): number | undefined;
|
|
168
|
+
static get _currentVersion(): number;
|
|
169
|
+
static get _pkFieldTypes(): Map<string, TypeWrapper<any>>;
|
|
170
|
+
static _loadVersionInfo(txnId: number, version: number): import("./indexes.js").VersionInfo;
|
|
171
|
+
static _writePrimary(txn: Transaction, pk: Uint8Array, data: Record<string, any>): void;
|
|
140
172
|
/**
|
|
141
173
|
* Optional migration function called when deserializing rows written with an older schema version.
|
|
142
174
|
* Receives a plain record with all fields (primary key fields + value fields) and should mutate it
|
|
@@ -153,9 +185,7 @@ export declare abstract class Model<SUB> {
|
|
|
153
185
|
*
|
|
154
186
|
* @example
|
|
155
187
|
* ```typescript
|
|
156
|
-
*
|
|
157
|
-
* class User extends E.Model<User> {
|
|
158
|
-
* static pk = E.primary(User, "id");
|
|
188
|
+
* const User = E.defineModel(class {
|
|
159
189
|
* id = E.field(E.identifier);
|
|
160
190
|
* name = E.field(E.string);
|
|
161
191
|
* role = E.field(E.string); // new field
|
|
@@ -163,7 +193,7 @@ export declare abstract class Model<SUB> {
|
|
|
163
193
|
* static migrate(record: Record<string, any>) {
|
|
164
194
|
* record.role ??= "user"; // default for rows that predate the 'role' field
|
|
165
195
|
* }
|
|
166
|
-
* }
|
|
196
|
+
* }, { pk: "id" });
|
|
167
197
|
* ```
|
|
168
198
|
*/
|
|
169
199
|
static migrate?(record: Record<string, any>): void;
|
|
@@ -190,9 +220,7 @@ export declare abstract class Model<SUB> {
|
|
|
190
220
|
*
|
|
191
221
|
* @example
|
|
192
222
|
* ```typescript
|
|
193
|
-
*
|
|
194
|
-
* class Post extends E.Model<Post> {
|
|
195
|
-
* static pk = E.primary(Post, "id");
|
|
223
|
+
* const Post = E.defineModel(class {
|
|
196
224
|
* id = E.field(E.identifier);
|
|
197
225
|
* title = E.field(E.string);
|
|
198
226
|
* slug = E.field(E.string);
|
|
@@ -200,16 +228,11 @@ export declare abstract class Model<SUB> {
|
|
|
200
228
|
* preCommit() {
|
|
201
229
|
* this.slug = this.title.toLowerCase().replace(/\s+/g, "-");
|
|
202
230
|
* }
|
|
203
|
-
* }
|
|
231
|
+
* }, { pk: "id" });
|
|
204
232
|
* ```
|
|
205
233
|
*/
|
|
206
234
|
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;
|
|
235
|
+
static _resetIndexes(): void;
|
|
213
236
|
static _loadCreateIndexes(): Promise<void>;
|
|
214
237
|
_setLoadedField(fieldName: string, value: any): void;
|
|
215
238
|
/**
|
|
@@ -230,21 +253,16 @@ export declare abstract class Model<SUB> {
|
|
|
230
253
|
*
|
|
231
254
|
* @example
|
|
232
255
|
* ```typescript
|
|
233
|
-
* const user = User.
|
|
256
|
+
* const user = User.get("user123");
|
|
234
257
|
* user.name = "New Name";
|
|
235
258
|
* user.preventPersist(); // Changes won't be saved
|
|
236
259
|
* ```
|
|
237
260
|
*/
|
|
238
261
|
preventPersist(): this;
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
* @returns An iterator.
|
|
244
|
-
*/
|
|
245
|
-
static findAll<T extends typeof Model<unknown>>(this: T, opts?: {
|
|
246
|
-
reverse?: boolean;
|
|
247
|
-
}): IndexRangeIterator<T>;
|
|
262
|
+
static get(...args: any[]): any;
|
|
263
|
+
static getLazy(...args: any[]): any;
|
|
264
|
+
static find(opts?: any): any;
|
|
265
|
+
static batchProcess(opts: any, callback?: any): any;
|
|
248
266
|
/**
|
|
249
267
|
* Load an existing instance by primary key and update it, or create a new one.
|
|
250
268
|
*
|
|
@@ -255,7 +273,7 @@ export declare abstract class Model<SUB> {
|
|
|
255
273
|
* @param obj - Partial model data that **must** include every primary key field.
|
|
256
274
|
* @returns The loaded-and-updated or newly created instance.
|
|
257
275
|
*/
|
|
258
|
-
static replaceInto<T extends typeof Model<any>>(this: T, obj: Partial<
|
|
276
|
+
static replaceInto<T extends typeof Model<any>>(this: T, obj: Partial<Record<string, any>>): InstanceType<T>;
|
|
259
277
|
/**
|
|
260
278
|
* Delete this model instance from the database.
|
|
261
279
|
*
|
|
@@ -263,7 +281,7 @@ export declare abstract class Model<SUB> {
|
|
|
263
281
|
*
|
|
264
282
|
* @example
|
|
265
283
|
* ```typescript
|
|
266
|
-
* const user = User.
|
|
284
|
+
* const user = User.get("user123");
|
|
267
285
|
* user.delete(); // Removes from database
|
|
268
286
|
* ```
|
|
269
287
|
*/
|
|
@@ -297,3 +315,4 @@ export declare abstract class Model<SUB> {
|
|
|
297
315
|
getState(): "deleted" | "created" | "loaded" | "lazy";
|
|
298
316
|
toString(): string;
|
|
299
317
|
}
|
|
318
|
+
export {};
|