kindstore 0.1.3 → 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/README.md CHANGED
@@ -32,7 +32,7 @@ const Post = z.object({
32
32
  const db = kindstore({
33
33
  connection: { filename: ":memory:" },
34
34
  posts: kind("pst", Post)
35
- .updatedAt("updatedAt")
35
+ .updatedAt()
36
36
  .index("authorId")
37
37
  .index("slug")
38
38
  .index("status")
package/dist/index.d.mts CHANGED
@@ -71,6 +71,7 @@ type ConnectionConfig = {
71
71
  //#endregion
72
72
  //#region src/kind.d.ts
73
73
  type MultiIndexFields<T extends KindDefinitionBag> = { [K in keyof KindValue<T> & string]?: IndexDirection };
74
+ type DefaultManagedTimestampField<T extends KindDefinitionBag, TName extends "createdAt" | "updatedAt"> = Extract<TName, keyof KindValue<T> & string>;
74
75
  type IndexDefinition = {
75
76
  field: string;
76
77
  type?: SqliteTypeHint;
@@ -95,10 +96,10 @@ declare class KindDefinition<T extends KindDefinitionBag> {
95
96
  }): KindDefinition<Omit<T, "indexed"> & {
96
97
  indexed: T["indexed"] | TKey;
97
98
  }>;
98
- createdAt<TKey extends keyof KindValue<T> & string>(field: TKey): KindDefinition<Omit<T, "createdAt"> & {
99
+ createdAt<TKey extends keyof KindValue<T> & string = DefaultManagedTimestampField<T, "createdAt">>(...args: DefaultManagedTimestampField<T, "createdAt"> extends never ? [field: keyof KindValue<T> & string] : [field?: TKey]): KindDefinition<Omit<T, "createdAt"> & {
99
100
  createdAt: TKey;
100
101
  }>;
101
- updatedAt<TKey extends keyof KindValue<T> & string>(field: TKey): KindDefinition<Omit<T, "updatedAt"> & {
102
+ updatedAt<TKey extends keyof KindValue<T> & string = DefaultManagedTimestampField<T, "updatedAt">>(...args: DefaultManagedTimestampField<T, "updatedAt"> extends never ? [field: keyof KindValue<T> & string] : [field?: TKey]): KindDefinition<Omit<T, "updatedAt"> & {
102
103
  updatedAt: TKey;
103
104
  }>;
104
105
  multi<const TName extends string, const TFields extends MultiIndexFields<T> & Record<string, IndexDirection>>(name: TName, fields: TFields): KindDefinition<Omit<T, "indexed"> & {
@@ -141,9 +142,9 @@ type KindStoreSurface<TKinds extends KindRegistry, TMetadata extends MetadataDef
141
142
  batch<TResult>(callback: () => TResult): TResult;
142
143
  close(): void;
143
144
  } & { [K in keyof TKinds]: TKinds[K] extends KindDefinition<infer TBag> ? KindCollectionSurface<TBag> : never };
144
- type PublicKindCollection<T extends KindDefinitionBag> = KindCollectionSurface<T>;
145
- type PublicMetadataCollection<T extends MetadataDefinitionMap> = MetadataSurface<T>;
146
- type PublicKindstore<TKinds extends KindRegistry, TMetadata extends MetadataDefinitionMap> = KindStoreSurface<TKinds, TMetadata>;
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>;
147
148
  //#endregion
148
149
  //#region src/store.d.ts
149
150
  type AnyStoreInput = {
@@ -155,9 +156,6 @@ type InferKinds<TInput extends AnyStoreInput> = { [K in keyof TInput as K extend
155
156
  type InferMetadata<TInput extends AnyStoreInput> = TInput extends {
156
157
  metadata: infer TMetadata extends MetadataDefinitionMap;
157
158
  } ? TMetadata : {};
158
- type KindCollection<T extends KindDefinitionBag> = PublicKindCollection<T>;
159
- type MetadataCollection<T extends MetadataDefinitionMap> = PublicMetadataCollection<T>;
160
- type Kindstore<TKinds extends KindRegistry, TMetadata extends MetadataDefinitionMap = {}> = PublicKindstore<TKinds, TMetadata>;
161
159
  declare function kindstore<const TInput extends AnyStoreInput>(input: TInput): Kindstore<InferKinds<TInput>, InferMetadata<TInput>>;
162
160
  type MetadataSchemas = Record<string, z.ZodTypeAny>;
163
161
  //#endregion
package/dist/index.mjs CHANGED
@@ -24,12 +24,14 @@ var KindDefinition = class {
24
24
  });
25
25
  return this;
26
26
  }
27
- createdAt(field) {
27
+ createdAt(...args) {
28
+ const field = args[0] ?? "createdAt";
28
29
  if (field === this.updatedAtField) throw new Error(`Kind "${this.tag}" cannot use "${field}" for both createdAt and updatedAt.`);
29
30
  this.createdAtField = field;
30
31
  return this;
31
32
  }
32
- updatedAt(field) {
33
+ updatedAt(...args) {
34
+ const field = args[0] ?? "updatedAt";
33
35
  if (field === this.createdAtField) throw new Error(`Kind "${this.tag}" cannot use "${field}" for both createdAt and updatedAt.`);
34
36
  this.updatedAtField = field;
35
37
  return this;
@@ -346,19 +348,11 @@ var KindstoreRuntime = class {
346
348
  if (!step) throw new Error(`Kind "${definition.key}" is missing migration step ${currentVersion} -> ${currentVersion + 1}.`);
347
349
  value = step(value, context);
348
350
  }
349
- updateRow.run(JSON.stringify(this.applyManagedTimestamps(definition, value, parsePayload(row.payload), now, false)), row.id);
351
+ updateRow.run(JSON.stringify(applyManagedTimestamps(definition, value, parsePayload(row.payload), now, false)), row.id);
350
352
  }
351
353
  this.internal.setKindVersion(definition.key, definition.definition.version);
352
354
  })();
353
355
  }
354
- applyManagedTimestamps(definition, value, current, now, insert) {
355
- const next = { ...value };
356
- if (definition.createdAtField) if (current && Object.hasOwn(current, definition.createdAtField)) next[definition.createdAtField] = current[definition.createdAtField];
357
- else if (insert) next[definition.createdAtField] = now;
358
- else delete next[definition.createdAtField];
359
- if (definition.updatedAtField) next[definition.updatedAtField] = now;
360
- return definition.definition.schema.parse(next);
361
- }
362
356
  };
363
357
  var KindCollectionRuntime = class {
364
358
  database;
@@ -388,7 +382,7 @@ var KindCollectionRuntime = class {
388
382
  assertTaggedId(this.definition.definition.tag, id);
389
383
  return this.database.transaction(() => {
390
384
  const row = this.getStatement.get(id);
391
- const parsed = this.applyManagedTimestamps(value, row, Date.now(), !row);
385
+ const parsed = applyManagedTimestamps(this.definition, value, row ? parsePayload(row.payload) : void 0, Date.now(), !row);
392
386
  this.putStatement.run(id, JSON.stringify(parsed));
393
387
  return parsed;
394
388
  })();
@@ -403,10 +397,10 @@ var KindCollectionRuntime = class {
403
397
  const row = this.getStatement.get(id);
404
398
  if (!row) return;
405
399
  const current = this.parseRow(row);
406
- const parsed = this.applyManagedTimestamps(typeof updater === "function" ? updater(current) : {
400
+ const parsed = applyManagedTimestamps(this.definition, typeof updater === "function" ? updater(current) : {
407
401
  ...current,
408
402
  ...updater
409
- }, row, Date.now(), false);
403
+ }, parsePayload(row.payload), Date.now(), false);
410
404
  this.updateStatement.run(JSON.stringify(parsed), id);
411
405
  return parsed;
412
406
  })();
@@ -445,15 +439,6 @@ var KindCollectionRuntime = class {
445
439
  parseRow(row) {
446
440
  return this.definition.definition.schema.parse(parsePayload(row.payload));
447
441
  }
448
- applyManagedTimestamps(value, row, now, insert) {
449
- const current = row ? parsePayload(row.payload) : void 0;
450
- const next = { ...value };
451
- if (this.definition.createdAtField) if (current && Object.hasOwn(current, this.definition.createdAtField)) next[this.definition.createdAtField] = current[this.definition.createdAtField];
452
- else if (insert) next[this.definition.createdAtField] = now;
453
- else delete next[this.definition.createdAtField];
454
- if (this.definition.updatedAtField) next[this.definition.updatedAtField] = now;
455
- return this.definition.definition.schema.parse(next);
456
- }
457
442
  compileSelect(options) {
458
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.`);
459
444
  const where = compileWhere(this.definition.columns, options.where);
@@ -900,6 +885,14 @@ function snapshotIndexes(definition) {
900
885
  }
901
886
  return indexes;
902
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
+ }
903
896
  //#endregion
904
897
  //#region src/store.ts
905
898
  function kindstore(input) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kindstore",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "A lightweight, typed document store on SQLite with Zod schemas, indexed queries, and explicit migrations.",
5
5
  "license": "MIT",
6
6
  "author": "Alec Larson",