kindstore 0.1.4 → 0.1.5
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/index.d.mts +3 -6
- package/dist/index.mjs +12 -21
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -142,9 +142,9 @@ type KindStoreSurface<TKinds extends KindRegistry, TMetadata extends MetadataDef
|
|
|
142
142
|
batch<TResult>(callback: () => TResult): TResult;
|
|
143
143
|
close(): void;
|
|
144
144
|
} & { [K in keyof TKinds]: TKinds[K] extends KindDefinition<infer TBag> ? KindCollectionSurface<TBag> : never };
|
|
145
|
-
type
|
|
146
|
-
type
|
|
147
|
-
type
|
|
145
|
+
type KindCollection<T extends KindDefinitionBag> = KindCollectionSurface<T>;
|
|
146
|
+
type MetadataCollection<T extends MetadataDefinitionMap> = MetadataSurface<T>;
|
|
147
|
+
type Kindstore<TKinds extends KindRegistry, TMetadata extends MetadataDefinitionMap> = KindStoreSurface<TKinds, TMetadata>;
|
|
148
148
|
//#endregion
|
|
149
149
|
//#region src/store.d.ts
|
|
150
150
|
type AnyStoreInput = {
|
|
@@ -156,9 +156,6 @@ type InferKinds<TInput extends AnyStoreInput> = { [K in keyof TInput as K extend
|
|
|
156
156
|
type InferMetadata<TInput extends AnyStoreInput> = TInput extends {
|
|
157
157
|
metadata: infer TMetadata extends MetadataDefinitionMap;
|
|
158
158
|
} ? TMetadata : {};
|
|
159
|
-
type KindCollection<T extends KindDefinitionBag> = PublicKindCollection<T>;
|
|
160
|
-
type MetadataCollection<T extends MetadataDefinitionMap> = PublicMetadataCollection<T>;
|
|
161
|
-
type Kindstore<TKinds extends KindRegistry, TMetadata extends MetadataDefinitionMap = {}> = PublicKindstore<TKinds, TMetadata>;
|
|
162
159
|
declare function kindstore<const TInput extends AnyStoreInput>(input: TInput): Kindstore<InferKinds<TInput>, InferMetadata<TInput>>;
|
|
163
160
|
type MetadataSchemas = Record<string, z.ZodTypeAny>;
|
|
164
161
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -348,19 +348,11 @@ var KindstoreRuntime = class {
|
|
|
348
348
|
if (!step) throw new Error(`Kind "${definition.key}" is missing migration step ${currentVersion} -> ${currentVersion + 1}.`);
|
|
349
349
|
value = step(value, context);
|
|
350
350
|
}
|
|
351
|
-
updateRow.run(JSON.stringify(
|
|
351
|
+
updateRow.run(JSON.stringify(applyManagedTimestamps(definition, value, parsePayload(row.payload), now, false)), row.id);
|
|
352
352
|
}
|
|
353
353
|
this.internal.setKindVersion(definition.key, definition.definition.version);
|
|
354
354
|
})();
|
|
355
355
|
}
|
|
356
|
-
applyManagedTimestamps(definition, value, current, now, insert) {
|
|
357
|
-
const next = { ...value };
|
|
358
|
-
if (definition.createdAtField) if (current && Object.hasOwn(current, definition.createdAtField)) next[definition.createdAtField] = current[definition.createdAtField];
|
|
359
|
-
else if (insert) next[definition.createdAtField] = now;
|
|
360
|
-
else delete next[definition.createdAtField];
|
|
361
|
-
if (definition.updatedAtField) next[definition.updatedAtField] = now;
|
|
362
|
-
return definition.definition.schema.parse(next);
|
|
363
|
-
}
|
|
364
356
|
};
|
|
365
357
|
var KindCollectionRuntime = class {
|
|
366
358
|
database;
|
|
@@ -390,7 +382,7 @@ var KindCollectionRuntime = class {
|
|
|
390
382
|
assertTaggedId(this.definition.definition.tag, id);
|
|
391
383
|
return this.database.transaction(() => {
|
|
392
384
|
const row = this.getStatement.get(id);
|
|
393
|
-
const parsed = this.
|
|
385
|
+
const parsed = applyManagedTimestamps(this.definition, value, row ? parsePayload(row.payload) : void 0, Date.now(), !row);
|
|
394
386
|
this.putStatement.run(id, JSON.stringify(parsed));
|
|
395
387
|
return parsed;
|
|
396
388
|
})();
|
|
@@ -405,10 +397,10 @@ var KindCollectionRuntime = class {
|
|
|
405
397
|
const row = this.getStatement.get(id);
|
|
406
398
|
if (!row) return;
|
|
407
399
|
const current = this.parseRow(row);
|
|
408
|
-
const parsed = this.
|
|
400
|
+
const parsed = applyManagedTimestamps(this.definition, typeof updater === "function" ? updater(current) : {
|
|
409
401
|
...current,
|
|
410
402
|
...updater
|
|
411
|
-
}, row, Date.now(), false);
|
|
403
|
+
}, parsePayload(row.payload), Date.now(), false);
|
|
412
404
|
this.updateStatement.run(JSON.stringify(parsed), id);
|
|
413
405
|
return parsed;
|
|
414
406
|
})();
|
|
@@ -447,15 +439,6 @@ var KindCollectionRuntime = class {
|
|
|
447
439
|
parseRow(row) {
|
|
448
440
|
return this.definition.definition.schema.parse(parsePayload(row.payload));
|
|
449
441
|
}
|
|
450
|
-
applyManagedTimestamps(value, row, now, insert) {
|
|
451
|
-
const current = row ? parsePayload(row.payload) : void 0;
|
|
452
|
-
const next = { ...value };
|
|
453
|
-
if (this.definition.createdAtField) if (current && Object.hasOwn(current, this.definition.createdAtField)) next[this.definition.createdAtField] = current[this.definition.createdAtField];
|
|
454
|
-
else if (insert) next[this.definition.createdAtField] = now;
|
|
455
|
-
else delete next[this.definition.createdAtField];
|
|
456
|
-
if (this.definition.updatedAtField) next[this.definition.updatedAtField] = now;
|
|
457
|
-
return this.definition.definition.schema.parse(next);
|
|
458
|
-
}
|
|
459
442
|
compileSelect(options) {
|
|
460
443
|
if (options.limit != null && (!Number.isInteger(options.limit) || options.limit < 0)) throw new Error(`Query limit for kind "${this.definition.key}" must be a non-negative integer.`);
|
|
461
444
|
const where = compileWhere(this.definition.columns, options.where);
|
|
@@ -902,6 +885,14 @@ function snapshotIndexes(definition) {
|
|
|
902
885
|
}
|
|
903
886
|
return indexes;
|
|
904
887
|
}
|
|
888
|
+
function applyManagedTimestamps(definition, value, current, now, insert) {
|
|
889
|
+
const next = { ...value };
|
|
890
|
+
if (definition.createdAtField) if (current && Object.hasOwn(current, definition.createdAtField)) next[definition.createdAtField] = current[definition.createdAtField];
|
|
891
|
+
else if (insert) next[definition.createdAtField] = now;
|
|
892
|
+
else delete next[definition.createdAtField];
|
|
893
|
+
if (definition.updatedAtField) next[definition.updatedAtField] = now;
|
|
894
|
+
return definition.definition.schema.parse(next);
|
|
895
|
+
}
|
|
905
896
|
//#endregion
|
|
906
897
|
//#region src/store.ts
|
|
907
898
|
function kindstore(input) {
|
package/package.json
CHANGED