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.
Files changed (77) hide show
  1. package/README.md +403 -461
  2. package/build/src/datapack.d.ts +9 -9
  3. package/build/src/datapack.js +10 -10
  4. package/build/src/datapack.js.map +1 -1
  5. package/build/src/edinburgh.d.ts +21 -10
  6. package/build/src/edinburgh.js +33 -55
  7. package/build/src/edinburgh.js.map +1 -1
  8. package/build/src/indexes.d.ts +99 -288
  9. package/build/src/indexes.js +253 -636
  10. package/build/src/indexes.js.map +1 -1
  11. package/build/src/migrate.js +17 -39
  12. package/build/src/migrate.js.map +1 -1
  13. package/build/src/models.d.ts +177 -113
  14. package/build/src/models.js +487 -259
  15. package/build/src/models.js.map +1 -1
  16. package/build/src/types.d.ts +41 -51
  17. package/build/src/types.js +39 -52
  18. package/build/src/types.js.map +1 -1
  19. package/build/src/utils.d.ts +4 -4
  20. package/build/src/utils.js +4 -4
  21. package/package.json +1 -3
  22. package/skill/AnyModelClass.md +7 -0
  23. package/skill/FindOptions.md +37 -0
  24. package/skill/Lifecycle Hooks.md +24 -0
  25. package/skill/{Model_delete.md → Lifecycle Hooks_delete.md } +2 -2
  26. package/skill/{Model_getPrimaryKeyHash.md → Lifecycle Hooks_getPrimaryKeyHash.md } +1 -1
  27. package/skill/{Model_isValid.md → Lifecycle Hooks_isValid.md } +1 -1
  28. package/skill/Lifecycle Hooks_migrate.md +26 -0
  29. package/skill/{Model_preCommit.md → Lifecycle Hooks_preCommit.md } +3 -5
  30. package/skill/{Model_preventPersist.md → Lifecycle Hooks_preventPersist.md } +2 -2
  31. package/skill/{Model_validate.md → Lifecycle Hooks_validate.md } +2 -2
  32. package/skill/ModelBase.md +7 -0
  33. package/skill/ModelClass.md +8 -0
  34. package/skill/SKILL.md +253 -215
  35. package/skill/Schema Evolution.md +19 -0
  36. package/skill/TypeWrapper_containsNull.md +11 -0
  37. package/skill/TypeWrapper_deserialize.md +9 -0
  38. package/skill/TypeWrapper_getError.md +11 -0
  39. package/skill/TypeWrapper_serialize.md +10 -0
  40. package/skill/TypeWrapper_serializeType.md +9 -0
  41. package/skill/array.md +2 -2
  42. package/skill/defineModel.md +23 -0
  43. package/skill/deleteEverything.md +8 -0
  44. package/skill/field.md +4 -4
  45. package/skill/link.md +12 -10
  46. package/skill/literal.md +1 -1
  47. package/skill/opt.md +1 -1
  48. package/skill/or.md +1 -1
  49. package/skill/record.md +1 -1
  50. package/skill/set.md +2 -2
  51. package/skill/setOnSaveCallback.md +2 -2
  52. package/skill/transact.md +3 -3
  53. package/src/datapack.ts +10 -10
  54. package/src/edinburgh.ts +46 -58
  55. package/src/indexes.ts +338 -802
  56. package/src/migrate.ts +15 -37
  57. package/src/models.ts +617 -314
  58. package/src/types.ts +61 -54
  59. package/src/utils.ts +4 -4
  60. package/skill/BaseIndex.md +0 -16
  61. package/skill/BaseIndex_batchProcess.md +0 -10
  62. package/skill/BaseIndex_find.md +0 -7
  63. package/skill/Model.md +0 -22
  64. package/skill/Model_findAll.md +0 -12
  65. package/skill/Model_migrate.md +0 -34
  66. package/skill/Model_replaceInto.md +0 -16
  67. package/skill/PrimaryIndex.md +0 -8
  68. package/skill/PrimaryIndex_get.md +0 -17
  69. package/skill/PrimaryIndex_getLazy.md +0 -13
  70. package/skill/SecondaryIndex.md +0 -9
  71. package/skill/UniqueIndex.md +0 -9
  72. package/skill/UniqueIndex_get.md +0 -17
  73. package/skill/dump.md +0 -8
  74. package/skill/index.md +0 -32
  75. package/skill/primary.md +0 -26
  76. package/skill/registerModel.md +0 -26
  77. package/skill/unique.md +0 -32
@@ -1,15 +1,28 @@
1
1
  import DataPack from "./datapack.js";
2
- import { Model, Transaction } from "./models.js";
2
+ import { type Transaction } from "./edinburgh.js";
3
3
  import { TypeWrapper } from "./types.js";
4
- type IndexArgTypes<M extends typeof Model<any>, F extends readonly (keyof InstanceType<M> & string)[]> = {
5
- [I in keyof F]: InstanceType<M>[F[I]];
4
+ type IndexItem = {
5
+ _setLoadedField(fieldName: string, value: any): void;
6
+ _restoreLazyFields?(): void;
7
+ };
8
+ type PrimaryKeyItem = IndexItem & {
9
+ _oldValues: Record<string, any> | undefined | null | false;
10
+ _primaryKey: Uint8Array | undefined;
11
+ _txn: Transaction;
12
+ _setPrimaryKey(key: Uint8Array, hash?: number): void;
13
+ };
14
+ type FieldTypes = ReadonlyMap<string, TypeWrapper<any>>;
15
+ type LoadPrimary<ITEM> = (txn: Transaction, primaryKey: Uint8Array, loadNow: boolean | Uint8Array) => ITEM | undefined;
16
+ type QueueInitialization = () => void;
17
+ type IndexArgTypes<ITEM, F extends readonly (keyof ITEM & string)[]> = {
18
+ [I in keyof F]: ITEM[F[I]];
6
19
  };
7
20
  /** Cached information about a specific version of a primary index's value format. */
8
- interface VersionInfo {
21
+ export interface VersionInfo {
9
22
  migrateHash: number;
10
23
  /** Non-key field names → TypeWrappers for deserialization of this version's data. */
11
24
  nonKeyFields: Map<string, TypeWrapper<any>>;
12
- /** Set of serialized secondary index signatures that existed in this version. */
25
+ /** Set of serialized secondary index signatures that existed in this version's data. */
13
26
  secondaryKeys: Set<string>;
14
27
  }
15
28
  /**
@@ -17,18 +30,30 @@ interface VersionInfo {
17
30
  * Handles common iteration logic for both primary and unique indexes.
18
31
  * Extends built-in Iterator to provide map/filter/reduce/toArray/etc.
19
32
  */
20
- export declare class IndexRangeIterator<M extends typeof Model> extends Iterator<InstanceType<M>> {
33
+ export declare class IndexRangeIterator<ITEM> extends Iterator<ITEM> {
21
34
  private txn;
22
35
  private iteratorId;
23
- private indexId;
24
36
  private parentIndex;
25
- constructor(txn: Transaction, iteratorId: number, indexId: number, parentIndex: BaseIndex<M, any>);
26
- next(): IteratorResult<InstanceType<M>>;
37
+ constructor(txn: Transaction, iteratorId: number, parentIndex: BaseIndex<ITEM, any>);
38
+ [Symbol.iterator](): this;
39
+ next(): IteratorResult<ITEM>;
27
40
  count(): number;
28
- fetch(): InstanceType<M> | undefined;
41
+ fetch(): ITEM | undefined;
29
42
  }
30
43
  type ArrayOrOnlyItem<ARG_TYPES extends readonly any[]> = ARG_TYPES extends readonly [infer A] ? (A | Partial<ARG_TYPES>) : Partial<ARG_TYPES>;
31
- type FindOptions<ARG_TYPES extends readonly any[]> = (({
44
+ /**
45
+ * Range-query options accepted by `find()`, `findBy()`, `batchProcess()`, and `batchProcessBy()`.
46
+ *
47
+ * Supports exact-match lookups via `is`, inclusive bounds via `from` / `to`,
48
+ * exclusive bounds via `after` / `before`, and reverse scans.
49
+ *
50
+ * For single-field indexes, values can be passed directly. For composite indexes,
51
+ * pass tuples or partial tuples for prefix matching.
52
+ *
53
+ * @template ARG_TYPES - Tuple of index argument types.
54
+ * @template FETCH - Optional fetch mode used by overloads that return one row.
55
+ */
56
+ export type FindOptions<ARG_TYPES extends readonly any[], FETCH extends 'first' | 'single' | undefined = undefined> = (({
32
57
  is: ArrayOrOnlyItem<ARG_TYPES>;
33
58
  } | (({
34
59
  from: ArrayOrOnlyItem<ARG_TYPES>;
@@ -40,314 +65,100 @@ type FindOptions<ARG_TYPES extends readonly any[]> = (({
40
65
  before: ArrayOrOnlyItem<ARG_TYPES>;
41
66
  } | {}))) & {
42
67
  reverse?: boolean;
43
- });
68
+ } & (FETCH extends undefined ? {
69
+ fetch?: undefined;
70
+ } : {
71
+ fetch: FETCH;
72
+ }));
44
73
  /**
45
74
  * Base class for database indexes for efficient lookups on model fields.
46
75
  *
47
76
  * Indexes enable fast queries on specific field combinations and enforce uniqueness constraints.
48
- *
49
- * @template M - The model class this index belongs to.
50
- * @template F - The field names that make up this index.
51
77
  */
52
- export declare abstract class BaseIndex<M extends typeof Model, const F extends readonly (keyof InstanceType<M> & string)[], ARGS extends readonly any[] = IndexArgTypes<M, F>> {
53
- _fieldNames: F;
54
- _MyModel: M;
55
- _fieldTypes: Map<keyof InstanceType<M> & string, TypeWrapper<any>>;
56
- _fieldCount: number;
57
- _resetIndexFieldDescriptors: Record<string | symbol | number, PropertyDescriptor>;
78
+ export declare abstract class BaseIndex<ITEM, const F extends readonly (keyof ITEM & string)[], ARGS extends readonly any[] = IndexArgTypes<ITEM, F>> {
79
+ tableName: string;
80
+ _indexFields: Map<F[number], TypeWrapper<any>>;
58
81
  _computeFn?: (data: any) => any[];
59
- /**
60
- * Create a new index.
61
- * @param MyModel - The model class this index belongs to.
62
- * @param _fieldNames - Array of field names that make up this index.
63
- */
64
- constructor(MyModel: M, _fieldNames: F);
65
- _delayedInit(): Promise<void>;
66
82
  _indexId?: number;
67
- /** Human-readable signature for version tracking, e.g. "secondary category:string" */
68
83
  _signature?: string;
69
- /**
70
- * Serialize array of key values to a (index-id prefixed) Bytes instance that can be used as a key.
71
- * @param args - Field values to serialize (can be partial for range queries).
72
- * @returns A Bytes instance containing the index id and serialized key parts.
73
- * @internal
74
- */
84
+ constructor(tableName: string, fieldNames: F);
85
+ _initializeIndex(fields: FieldTypes, reset?: boolean, primaryFieldTypes?: FieldTypes): Promise<void>;
75
86
  _argsToKeyBytes(args: [], allowPartial: boolean): DataPack;
76
87
  _argsToKeyBytes(args: Partial<ARGS>, allowPartial: boolean): DataPack;
88
+ abstract _pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, valueBuffer: ArrayBuffer): ITEM;
89
+ abstract _getTypeName(): string;
90
+ _retrieveIndexId(fields: FieldTypes, primaryFieldTypes?: FieldTypes): Promise<void>;
91
+ _computeKeyBounds(opts: FindOptions<ARGS>): [DataPack | undefined, DataPack | undefined] | null;
77
92
  /**
78
- * Extract model from iterator entry - implemented differently by each index type.
79
- * @param keyBuffer - Key bytes (including index id).
80
- * @param valueBuffer - Value bytes from the entry.
81
- * @returns Model instance or undefined.
82
- * @internal
83
- */
84
- abstract _pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, valueBuffer: ArrayBuffer): InstanceType<M>;
85
- _hasNullIndexValues(data: Record<string, any>): boolean;
86
- abstract _serializeKey(primaryKey: Uint8Array, data: Record<string, any>): Uint8Array;
87
- _serializeKeyFields(data: Record<string, any>): DataPack;
88
- /**
89
- * Retrieve (or create) a stable index ID from the DB, with retry on transaction races.
90
- * Sets `this._indexId` on success.
91
- */
92
- _retrieveIndexId(): Promise<void>;
93
- abstract _delete(txn: Transaction, primaryKey: Uint8Array, model: InstanceType<M>): void;
94
- abstract _write(txn: Transaction, primaryKey: Uint8Array, model: InstanceType<M>): void;
95
- /**
96
- * Find model instances using flexible range query options.
97
- *
98
- * Supports exact matches, inclusive/exclusive range queries, and reverse iteration.
99
- * For single-field indexes, you can pass values directly or in arrays.
100
- * For multi-field indexes, pass arrays or partial arrays for prefix matching.
93
+ * Find rows using exact-match or range-query options.
101
94
  *
102
- * @param opts - Query options object
103
- * @param opts.is - Exact match (sets both `from` and `to` to same value)
104
- * @param opts.from - Range start (inclusive)
105
- * @param opts.after - Range start (exclusive)
106
- * @param opts.to - Range end (inclusive)
107
- * @param opts.before - Range end (exclusive)
108
- * @param opts.reverse - Whether to iterate in reverse order
109
- * @returns An iterable of model instances matching the query
95
+ * Supports exact matches, inclusive and exclusive bounds, open-ended ranges,
96
+ * and reverse iteration. For single-field indexes, values can be passed
97
+ * directly. For composite indexes, pass tuples or partial tuples.
110
98
  *
111
99
  * @example
112
100
  * ```typescript
113
- * // Exact match
114
- * for (const user of User.byEmail.find({is: "john@example.com"})) {
115
- * console.log(user.name);
116
- * }
117
- *
118
- * // Range query (inclusive)
119
- * for (const user of User.byEmail.find({from: "a@", to: "m@"})) {
120
- * console.log(user.email);
121
- * }
122
- *
123
- * // Range query (exclusive)
124
- * for (const user of User.byEmail.find({after: "a@", before: "m@"})) {
125
- * console.log(user.email);
126
- * }
127
- *
128
- * // Open-ended ranges
129
- * for (const user of User.byEmail.find({from: "m@"})) { // m@ and later
130
- * console.log(user.email);
131
- * }
132
- *
133
- * for (const user of User.byEmail.find({to: "m@"})) { // up to and including m@
134
- * console.log(user.email);
135
- * }
136
- *
137
- * // Reverse iteration
138
- * for (const user of User.byEmail.find({reverse: true})) {
139
- * console.log(user.email); // Z to A order
140
- * }
141
- *
142
- * // Multi-field index prefix matching
143
- * for (const item of CompositeModel.pk.find({from: ["electronics", "phones"]})) {
144
- * console.log(item.name); // All electronics/phones items
145
- * }
146
- *
147
- * // For single-field indexes, you can use the value directly
148
- * for (const user of User.byEmail.find({is: "john@example.com"})) {
149
- * console.log(user.name);
150
- * }
101
+ * const exact = User.find({ is: "user-123", fetch: "first" });
102
+ * const email = [...User.findBy("email", { from: "a@test.com", to: "m@test.com" })];
103
+ * const reverse = [...Product.findBy("category", { is: "electronics", reverse: true })];
151
104
  * ```
152
105
  */
153
- _computeKeyBounds(opts: FindOptions<ARGS>): [DataPack | undefined, DataPack | undefined] | null;
154
- find(opts?: FindOptions<ARGS>): IndexRangeIterator<M>;
106
+ find(opts: FindOptions<ARGS, 'first'>): ITEM | undefined;
107
+ find(opts: FindOptions<ARGS, 'single'>): ITEM;
108
+ find(opts?: FindOptions<ARGS>): IndexRangeIterator<ITEM>;
155
109
  /**
156
- * Process all matching rows in batched transactions.
110
+ * Process matching rows in batched transactions.
157
111
  *
158
- * Uses the same query options as {@link find}. The batch is committed and a new
159
- * transaction started once either `limitSeconds` or `limitRows` is exceeded.
112
+ * Uses the same range options as {@link find}, plus optional row and time
113
+ * limits that control when the current transaction is committed and a new one starts.
160
114
  *
161
- * @param opts - Query options (same as `find()`), plus:
162
- * @param opts.limitSeconds - Max seconds per transaction batch (default: 1)
163
- * @param opts.limitRows - Max rows per transaction batch (default: 4096)
164
- * @param callback - Called for each matching row within a transaction
115
+ * @param opts Query options plus batch limits.
116
+ * @param callback Called for each matching row inside a transaction.
165
117
  */
166
118
  batchProcess(opts: (FindOptions<ARGS> & {
167
119
  limitSeconds?: number;
168
120
  limitRows?: number;
169
- }) | undefined, callback: (row: InstanceType<M>) => void | Promise<void>): Promise<void>;
170
- abstract _getTypeName(): string;
121
+ }) | undefined, callback: (row: ITEM) => void | Promise<void>): Promise<void>;
171
122
  toString(): string;
172
123
  }
173
- /**
174
- * Primary index that stores the actual model data.
175
- *
176
- * @template M - The model class this index belongs to.
177
- * @template F - The field names that make up this index.
178
- */
179
- export declare class PrimaryIndex<M extends typeof Model, const F extends readonly (keyof InstanceType<M> & string)[]> extends BaseIndex<M, F, IndexArgTypes<M, F>> {
180
- _nonKeyFields: (keyof InstanceType<M> & string)[];
181
- _lazyDescriptors: Record<string | symbol | number, PropertyDescriptor>;
182
- _resetDescriptors: Record<string | symbol | number, PropertyDescriptor>;
183
- _freezePrimaryKeyDescriptors: Record<string | symbol | number, PropertyDescriptor>;
184
- /** Current version number for this primary index's value format. */
185
- _currentVersion: number;
186
- /** Hash of the current migrate() function source, or 0 if none. */
187
- _currentMigrateHash: number;
188
- /** Cached version info for old versions (loaded on demand). */
189
- _versions: Map<number, VersionInfo>;
190
- constructor(MyModel: M, fieldNames: F);
191
- _delayedInit(): Promise<void>;
192
- /** Serialize the current version fingerprint as a DataPack object. */
193
- _serializeVersionValue(): Uint8Array;
194
- /** Look up or create the current version number for this primary index. */
195
- _initVersioning(): Promise<void>;
196
- /**
197
- * Get a model instance by primary key values.
198
- * @param args - The primary key values.
199
- * @returns The model instance if found, undefined otherwise.
200
- *
201
- * @example
202
- * ```typescript
203
- * const user = User.pk.get("john_doe");
204
- * ```
205
- */
206
- get(...args: IndexArgTypes<M, F>): InstanceType<M> | undefined;
207
- /**
208
- * Does the same as as `get()`, but will delay loading the instance from disk until the first
209
- * property access. In case it turns out the instance doesn't exist, an error will be thrown
210
- * at that time.
211
- * @param args Primary key field values. (Or a single Uint8Array containing the key.)
212
- * @returns The (lazily loaded) model instance.
213
- */
214
- getLazy(...args: IndexArgTypes<M, F>): InstanceType<M>;
215
- _get(txn: Transaction, args: IndexArgTypes<M, F> | Uint8Array, loadNow: false | Uint8Array): InstanceType<M>;
216
- _get(txn: Transaction, args: IndexArgTypes<M, F> | Uint8Array, loadNow: true): InstanceType<M> | undefined;
217
- _serializeKey(primaryKey: Uint8Array, _data: Record<string, any>): Uint8Array;
218
- _lazyNow(model: InstanceType<M>): void;
219
- _setNonKeyValues(model: InstanceType<M>, valueArray: Uint8Array): void;
220
- /** Load a version's info from DB, caching the result. */
221
- _loadVersionInfo(txnId: number, version: number): VersionInfo;
222
- /** Deserialize and migrate a row from an old version. */
223
- _migrateFromVersion(model: InstanceType<M>, version: number, valuePack: DataPack): void;
224
- _keyToArray(key: Uint8Array): IndexArgTypes<M, F>;
225
- _pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, valueBuffer: ArrayBuffer): InstanceType<M>;
124
+ export declare abstract class PrimaryKey<ITEM extends PrimaryKeyItem, const F extends readonly (keyof ITEM & string)[], ARGS extends readonly any[] = IndexArgTypes<ITEM, F>> extends BaseIndex<ITEM, F, ARGS> {
226
125
  _getTypeName(): string;
227
- _write(txn: Transaction, primaryKey: Uint8Array, data: Record<string, any>): void;
228
- _delete(txn: Transaction, primaryKey: Uint8Array, _data: Record<string, any>): void;
126
+ abstract _serializeValue(data: Record<string, any>): Uint8Array;
127
+ _versionInfoKey(version: number): Uint8Array;
128
+ _ensureVersionEntry(currentValueBytes: Uint8Array): Promise<{
129
+ version: number;
130
+ created: boolean;
131
+ }>;
132
+ _serializePK(data: Record<string, any>): DataPack;
133
+ _pkToArray(key: Uint8Array): ARGS;
134
+ _writePK(txn: Transaction, primaryKey: Uint8Array, data: Record<string, any>): void;
135
+ _deletePK(txn: Transaction, primaryKey: Uint8Array, _data: Record<string, any>): void;
229
136
  }
230
- /**
231
- * Unique index that stores references to the primary key.
232
- *
233
- * @template M - The model class this index belongs to.
234
- * @template F - The field names that make up this index.
235
- */
236
- export declare class UniqueIndex<M extends typeof Model, const F extends readonly (keyof InstanceType<M> & string)[], ARGS extends readonly any[] = IndexArgTypes<M, F>> extends BaseIndex<M, F, ARGS> {
237
- constructor(MyModel: M, fieldsOrFn: F | ((data: any) => any[]));
238
- /**
239
- * Get a model instance by unique index key values.
240
- * @param args - The unique index key values.
241
- * @returns The model instance if found, undefined otherwise.
242
- *
243
- * @example
244
- * ```typescript
245
- * const userByEmail = User.byEmail.get("john@example.com");
246
- * ```
247
- */
248
- get(...args: ARGS): InstanceType<M> | undefined;
249
- _serializeKey(primaryKey: Uint8Array, data: Record<string, any>): Uint8Array;
250
- _delete(txn: Transaction, primaryKey: Uint8Array, data: Record<string, any>): void;
251
- _write(txn: Transaction, primaryKey: Uint8Array, data: Record<string, any>): void;
252
- _pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, valueBuffer: ArrayBuffer): InstanceType<M>;
137
+ export declare abstract class NonPrimaryIndex<ITEM extends IndexItem, const F extends readonly (keyof ITEM & string)[], ARGS extends readonly any[] = IndexArgTypes<ITEM, F>> extends BaseIndex<ITEM, F, ARGS> {
138
+ protected _loadPrimary: LoadPrimary<ITEM>;
139
+ _resetIndexFieldDescriptors: Record<string | symbol | number, PropertyDescriptor>;
140
+ constructor(tableName: string, fieldsOrFn: F | ((data: any) => any[]), _loadPrimary: LoadPrimary<ITEM>, queueInitialization: QueueInitialization);
141
+ _initializeIndex(fields: FieldTypes, reset?: boolean, primaryFieldTypes?: FieldTypes): Promise<void>;
142
+ _buildKeyPacks(data: Record<string, any>): DataPack[];
143
+ _serializeKeys(primaryKey: Uint8Array, data: Record<string, any>): Uint8Array[];
144
+ abstract _writeKey(txn: Transaction, key: Uint8Array, primaryKey: Uint8Array): void;
145
+ _write(txn: Transaction, primaryKey: Uint8Array, model: ITEM): void;
146
+ _delete(txn: Transaction, primaryKey: Uint8Array, model: ITEM): void;
147
+ _update(txn: Transaction, primaryKey: Uint8Array, newData: ITEM, oldData: Record<string, any>): number;
148
+ }
149
+ export declare class UniqueIndex<ITEM extends IndexItem, const F extends readonly (keyof ITEM & string)[], ARGS extends readonly any[] = IndexArgTypes<ITEM, F>> extends NonPrimaryIndex<ITEM, F, ARGS> {
150
+ constructor(tableName: string, fieldsOrFn: F | ((data: any) => any[]), loadPrimary: LoadPrimary<ITEM>, queueInitialization: QueueInitialization);
253
151
  _getTypeName(): string;
152
+ getPK(...args: ARGS): ITEM | undefined;
153
+ _writeKey(txn: Transaction, key: Uint8Array, primaryKey: Uint8Array): void;
154
+ _pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, valueBuffer: ArrayBuffer): ITEM;
254
155
  }
255
- /**
256
- * Secondary index for non-unique lookups.
257
- *
258
- * @template M - The model class this index belongs to.
259
- * @template F - The field names that make up this index.
260
- */
261
- export declare class SecondaryIndex<M extends typeof Model, const F extends readonly (keyof InstanceType<M> & string)[], ARGS extends readonly any[] = IndexArgTypes<M, F>> extends BaseIndex<M, F, ARGS> {
262
- constructor(MyModel: M, fieldsOrFn: F | ((data: any) => any[]));
263
- _pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, _valueBuffer: ArrayBuffer): InstanceType<M>;
264
- _serializeKey(primaryKey: Uint8Array, model: InstanceType<M>): Uint8Array;
265
- _write(txn: Transaction, primaryKey: Uint8Array, model: InstanceType<M>): void;
266
- _delete(txn: Transaction, primaryKey: Uint8Array, model: InstanceType<M>): void;
156
+ export declare class SecondaryIndex<ITEM extends IndexItem, const F extends readonly (keyof ITEM & string)[], ARGS extends readonly any[] = IndexArgTypes<ITEM, F>> extends NonPrimaryIndex<ITEM, F, ARGS> {
157
+ constructor(tableName: string, fieldsOrFn: F | ((data: any) => any[]), loadPrimary: LoadPrimary<ITEM>, queueInitialization: QueueInitialization);
267
158
  _getTypeName(): string;
159
+ _pairToInstance(txn: Transaction, keyBuffer: ArrayBuffer, _valueBuffer: ArrayBuffer): ITEM;
160
+ _serializeKeys(primaryKey: Uint8Array, data: Record<string, any>): Uint8Array[];
161
+ _writeKey(txn: Transaction, key: Uint8Array, _primaryKey: Uint8Array): void;
268
162
  }
269
- export type Index<M extends typeof Model, F extends readonly (keyof InstanceType<M> & string)[]> = PrimaryIndex<M, F> | UniqueIndex<M, F> | SecondaryIndex<M, F>;
270
- /**
271
- * Create a primary index on model fields.
272
- * @template M - The model class.
273
- * @template F - The field name (for single field index).
274
- * @template FS - The field names array (for composite index).
275
- * @param MyModel - The model class to create the index for.
276
- * @param field - Single field name for simple indexes.
277
- * @param fields - Array of field names for composite indexes.
278
- * @returns A new PrimaryIndex instance.
279
- *
280
- * @example
281
- * ```typescript
282
- * class User extends E.Model<User> {
283
- * static pk = E.primary(User, ["id"]);
284
- * static pkSingle = E.primary(User, "id");
285
- * }
286
- * ```
287
- */
288
- export declare function primary<M extends typeof Model, const F extends (keyof InstanceType<M> & string)>(MyModel: M, field: F): PrimaryIndex<M, [F]>;
289
- export declare function primary<M extends typeof Model, const FS extends readonly (keyof InstanceType<M> & string)[]>(MyModel: M, fields: FS): PrimaryIndex<M, FS>;
290
- /**
291
- * Create a unique index on model fields, or a computed unique index using a function.
292
- *
293
- * For field-based indexes, pass a field name or array of field names.
294
- * For computed indexes, pass a function that takes a model instance and returns an array of
295
- * index keys. Return `[]` to skip indexing for that instance. Each array element creates a
296
- * separate index entry, enabling multi-value indexes (e.g., indexing by each word in a name).
297
- *
298
- * @template M - The model class.
299
- * @template V - The computed index value type (for function-based indexes).
300
- * @template F - The field name (for single field index).
301
- * @template FS - The field names array (for composite index).
302
- * @param MyModel - The model class to create the index for.
303
- * @param field - Field name, array of field names, or a compute function.
304
- * @returns A new UniqueIndex instance.
305
- *
306
- * @example
307
- * ```typescript
308
- * class User extends E.Model<User> {
309
- * static byEmail = E.unique(User, "email");
310
- * static byNameAge = E.unique(User, ["name", "age"]);
311
- * static byFullName = E.unique(User, (u: User) => [`${u.firstName} ${u.lastName}`]);
312
- * }
313
- * ```
314
- */
315
- export declare function unique<M extends typeof Model, V>(MyModel: M, fn: (instance: InstanceType<M>) => V[]): UniqueIndex<M, [], [V]>;
316
- export declare function unique<M extends typeof Model, const F extends (keyof InstanceType<M> & string)>(MyModel: M, field: F): UniqueIndex<M, [F]>;
317
- export declare function unique<M extends typeof Model, const FS extends readonly (keyof InstanceType<M> & string)[]>(MyModel: M, fields: FS): UniqueIndex<M, FS>;
318
- /**
319
- * Create a secondary index on model fields, or a computed secondary index using a function.
320
- *
321
- * For field-based indexes, pass a field name or array of field names.
322
- * For computed indexes, pass a function that takes a model instance and returns an array of
323
- * index keys. Return `[]` to skip indexing for that instance. Each array element creates a
324
- * separate index entry, enabling multi-value indexes (e.g., indexing by each word in a name).
325
- *
326
- * @template M - The model class.
327
- * @template V - The computed index value type (for function-based indexes).
328
- * @template F - The field name (for single field index).
329
- * @template FS - The field names array (for composite index).
330
- * @param MyModel - The model class to create the index for.
331
- * @param field - Field name, array of field names, or a compute function.
332
- * @returns A new SecondaryIndex instance.
333
- *
334
- * @example
335
- * ```typescript
336
- * class User extends E.Model<User> {
337
- * static byAge = E.index(User, "age");
338
- * static byTagsDate = E.index(User, ["tags", "createdAt"]);
339
- * static byWord = E.index(User, (u: User) => u.name.split(" "));
340
- * }
341
- * ```
342
- */
343
- export declare function index<M extends typeof Model, V>(MyModel: M, fn: (instance: InstanceType<M>) => V[]): SecondaryIndex<M, [], [V]>;
344
- export declare function index<M extends typeof Model, const F extends (keyof InstanceType<M> & string)>(MyModel: M, field: F): SecondaryIndex<M, [F]>;
345
- export declare function index<M extends typeof Model, const FS extends readonly (keyof InstanceType<M> & string)[]>(MyModel: M, fields: FS): SecondaryIndex<M, FS>;
346
- /**
347
- * Dump database contents for debugging.
348
- *
349
- * Prints all indexes and their data to the console for inspection.
350
- * This is primarily useful for development and debugging purposes.
351
- */
352
163
  export declare function dump(): void;
353
164
  export {};