wirejs-resources 0.1.34-table-resource → 0.1.36-table-resource

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.ts CHANGED
@@ -6,6 +6,6 @@ export { withContext, requiresContext, Context, ContextWrapped } from './adapter
6
6
  export { Resource } from './resource.js';
7
7
  export { overrides } from './overrides.js';
8
8
  export { Secret } from './resources/secret.js';
9
- export { DistributedTable, DistributedTableOptions, Filter, FieldComparison, FieldComparisonOptions, FieldType, Parser, PassThruParser, RecordType, RecordKey, matchesFilter, } from './resources/distributed-table.js';
9
+ export { AnyKeyFieldOption, DistributedTable, DistributedTableOptions, Filter, FieldComparison, FieldComparisonOptions, FieldType, KeyFieldDefinition, KeyFieldOptions, Parser, PassThruParser, PKFieldTypes, RecordType, RecordKey, matchesFilter, } from './resources/distributed-table.js';
10
10
  export * from './types.js';
11
11
  export * from './derived-types.js';
@@ -1,14 +1,29 @@
1
1
  import { Resource } from '../resource.js';
2
+ export type KindaPretty<T> = T extends object ? {
3
+ [K in keyof T]: KindaPretty<T[K]>;
4
+ } : T;
5
+ export type PKFieldTypes = 'string' | 'number';
2
6
  export type DistributedTableOptions<T> = {
3
7
  parse: Parser<T>;
4
8
  key: {
5
- partition: keyof T & string;
6
- sort?: (keyof T & string)[];
9
+ partition: AnyKeyFieldOption<T>;
10
+ sort?: AnyKeyFieldOption<T>;
7
11
  };
8
12
  };
13
+ export type KeyFieldOptions<T> = {
14
+ [K in keyof T as K extends string ? K : never]: K extends string ? KeyFieldDefinition<T, K, T[K]> : never;
15
+ };
16
+ export type AnyKeyFieldOption<T> = KeyFieldOptions<T>[keyof KeyFieldOptions<T>];
17
+ export type KeyFieldDefinition<T, FieldName extends string & keyof T, FieldType> = FieldType extends number ? {
18
+ field: FieldName & keyof T;
19
+ type: 'number';
20
+ } : {
21
+ field: FieldName & keyof T;
22
+ type: 'string';
23
+ };
9
24
  export type Parser<T> = (record: Record<string, any>) => T;
10
25
  export type RecordType<T extends DistributedTableOptions<any>> = T extends DistributedTableOptions<infer IT> ? IT : Record<string, any>;
11
- export type RecordKey<T, PK extends keyof T, SK extends (keyof T)[] | undefined> = Pick<T, PK | (SK extends string[] ? SK[number] : never)>;
26
+ export type RecordKey<T, PK extends AnyKeyFieldOption<T>, SK extends AnyKeyFieldOption<T> | undefined> = Pick<T, PK['field'] | (SK extends Record<string, any> ? (SK extends undefined ? never : SK['field']) : never)>;
12
27
  export type FieldType<T> = undefined extends T ? (T | null) : T;
13
28
  export type FieldComparisonOptions<T> = {
14
29
  [K in keyof T & string]?: {
@@ -55,7 +70,7 @@ export declare function PassThruParser<T>(record: Record<string, any>): T;
55
70
  *
56
71
  * High cardinality, non-sequential partition keys allow for the best overall scaling.
57
72
  */
58
- export declare class DistributedTable<const P extends Parser<any>, const T extends ReturnType<P>, const PK extends keyof T & string, const SK extends (keyof T & string)[] | undefined> extends Resource {
73
+ export declare class DistributedTable<const P extends Parser<any>, const T extends KindaPretty<ReturnType<P>>, const PK extends AnyKeyFieldOption<T>, const SK extends AnyKeyFieldOption<T> | undefined> extends Resource {
59
74
  #private;
60
75
  parse: P;
61
76
  partitionKey: PK;
@@ -67,15 +82,17 @@ export declare class DistributedTable<const P extends Parser<any>, const T exten
67
82
  sort?: SK;
68
83
  };
69
84
  });
85
+ get partitionKeyName(): PK['field'];
86
+ get sortKeyName(): 'field' extends (keyof SK) ? SK['field'] : undefined;
70
87
  save(item: T): Promise<void>;
71
88
  saveMany(items: T[]): Promise<void>;
72
- delete(item: RecordKey<T, PK, SK>): Promise<void>;
73
- deleteMany(items: (RecordKey<T, PK, SK>)[]): Promise<void>;
74
- get(key: RecordKey<T, PK, SK>): Promise<T | undefined>;
89
+ delete(item: KindaPretty<RecordKey<T, PK, SK>>): Promise<void>;
90
+ deleteMany(items: (KindaPretty<RecordKey<T, PK, SK>>)[]): Promise<void>;
91
+ get(key: KindaPretty<RecordKey<T, PK, SK>>): Promise<KindaPretty<T> | undefined>;
75
92
  scan(options: {
76
93
  filter?: Filter<T>;
77
94
  }): AsyncGenerator<T>;
78
- query(partition: Pick<T, PK>, options?: {
95
+ query(partition: KindaPretty<Pick<T, PK['field']>>, options?: {
79
96
  filter?: Filter<T>;
80
97
  }): AsyncGenerator<T>;
81
98
  }
@@ -64,12 +64,16 @@ export class DistributedTable extends Resource {
64
64
  this.sort = options.key.sort;
65
65
  this.#fileService = new (overrides.FileService || FileService)(this, 'files');
66
66
  }
67
+ get partitionKeyName() {
68
+ return this.partitionKey.field;
69
+ }
70
+ get sortKeyName() {
71
+ return this.sort?.field;
72
+ }
67
73
  #getFilename(key) {
68
- const parts = [key[this.partitionKey]];
69
- if (this.sort) {
70
- for (const sk of this.sort) {
71
- parts.push(key[sk]);
72
- }
74
+ const parts = [key[this.partitionKeyName]];
75
+ if (this.sortKeyName) {
76
+ parts.push(key[this.sortKeyName]);
73
77
  }
74
78
  return parts.map(String).join('__') + '.json';
75
79
  }
@@ -117,14 +121,26 @@ export class DistributedTable extends Resource {
117
121
  }
118
122
  }
119
123
  async *query(partition, options = {}) {
120
- const prefix = partition[this.partitionKey];
124
+ const prefix = partition[this.partitionKeyName];
121
125
  for await (const filename of this.#fileService.list({ prefix })) {
122
126
  const data = await this.#fileService.read(filename);
123
127
  const record = this.parse(JSON.parse(data));
124
- if (record[this.partitionKey] === partition[this.partitionKey]
128
+ if (record[this.partitionKeyName] === partition[this.partitionKeyName]
125
129
  && (!options.filter || matchesFilter(record, options.filter))) {
126
130
  yield record;
127
131
  }
128
132
  }
129
133
  }
130
134
  }
135
+ // export type Todo = {
136
+ // id: string;
137
+ // text: string;
138
+ // order: number;
139
+ // };
140
+ // const userTodos = new DistributedTable('app', 'userTodos', {
141
+ // parse: PassThruParser<Todo & { userId: string }>,
142
+ // key: {
143
+ // partition: { userId: 'string' },
144
+ // sort: { id: 'string' }
145
+ // }
146
+ // });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.34-table-resource",
3
+ "version": "0.1.36-table-resource",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",