@bodil/bdb 0.1.2 → 0.2.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/dist/query.d.ts CHANGED
@@ -4,46 +4,52 @@ import type { Index, UnitIndex } from "./indices";
4
4
  import type { Table } from "./table";
5
5
  export declare enum IteratorDirection {
6
6
  Ascending = 0,
7
- Descending = 1,
8
- DescendingExclusive = 2
7
+ Descending = 1
9
8
  }
10
- export declare abstract class Query<A extends object> implements Iterable<Readonly<A>> {
11
- abstract [Symbol.iterator](): IterableIterator<Readonly<A>>;
12
- abstract signal(): Signal.Computed<Array<Readonly<A>>>;
13
- map<B>(mapFn: (item: Readonly<A>) => B): IterableIterator<B>;
14
- forEach(action: (item: A) => void): void;
15
- toArray(): Array<Readonly<A>>;
9
+ export declare abstract class Query<Document extends object> implements Iterable<Readonly<Document>> {
10
+ abstract [Symbol.iterator](): IterableIterator<Readonly<Document>>;
11
+ abstract signal(): Signal.Computed<Array<Readonly<Document>>>;
12
+ map<B>(mapFn: (item: Readonly<Document>) => B): IterableIterator<B>;
13
+ forEach(action: (item: Document) => void): void;
14
+ toArray(): Array<Readonly<Document>>;
16
15
  }
17
- export declare abstract class TableQuery<A extends object, PI extends UnitIndex<A>, Ix extends object> extends Query<A> {
18
- table: Table<A, PI, Ix>;
19
- constructor(table: Table<A, PI, Ix>);
20
- signal(): Signal.Computed<Array<Readonly<A>>>;
16
+ export declare abstract class TableQuery<Document extends object, PI extends UnitIndex<Document>, Ix extends object> extends Query<Document> {
17
+ /** @ignore */
18
+ protected table: Table<Document, PI, Ix>;
19
+ constructor(table: Table<Document, PI, Ix>);
20
+ signal(): Signal.Computed<Array<Readonly<Document>>>;
21
+ /**
22
+ * Delete all documents from the table matching this query.
23
+ */
21
24
  delete(): number;
22
- filter(predicate: (item: Readonly<A>) => boolean): ChainQuery<A, PI, Ix>;
23
- limit(count: number): ChainQuery<A, PI, Ix>;
25
+ filter(predicate: (item: Readonly<Document>) => boolean): ChainQuery<Document, PI, Ix>;
26
+ limit(count: number): ChainQuery<Document, PI, Ix>;
24
27
  }
25
- export declare class IndexQuery<A extends object, PI extends UnitIndex<A>, I extends Index<A>, Ix extends object> extends TableQuery<A, PI, Ix> {
28
+ export declare class IndexQuery<Document extends object, PI extends UnitIndex<Document>, I extends Index<Document>, Ix extends object> extends TableQuery<Document, PI, Ix> {
26
29
  private readonly index;
27
30
  private readonly indexTable?;
28
31
  private readonly isPrimary;
29
32
  private start?;
33
+ private skipStart;
30
34
  private direction;
31
- constructor(table: Table<A, PI, Ix>, indexKey: keyof Ix & string, direction: IteratorDirection, start?: I["keyType"]);
32
- [Symbol.iterator](): IterableIterator<Readonly<A>>;
35
+ constructor(table: Table<Document, PI, Ix>, indexKey: keyof Ix & string, direction: IteratorDirection, start?: I["keyType"]);
36
+ [Symbol.iterator](): IterableIterator<Readonly<Document>>;
33
37
  reverse(): this;
34
- equals(value: I["keyType"]): ArrayQuery<A, PI, Ix>;
38
+ equals(value: I["keyType"]): ArrayQuery<Document, PI, Ix>;
35
39
  below(value: I["keyType"]): this;
40
+ above(value: I["keyType"]): this;
41
+ inclusive(): this;
36
42
  max(): Option<I["keyType"]>;
37
43
  min(): Option<I["keyType"]>;
38
44
  }
39
- export declare class ChainQuery<A extends object, PI extends UnitIndex<A>, Ix extends object> extends TableQuery<A, PI, Ix> {
45
+ export declare class ChainQuery<Document extends object, PI extends UnitIndex<Document>, Ix extends object> extends TableQuery<Document, PI, Ix> {
40
46
  private readonly parent;
41
47
  private readonly iterator;
42
- constructor(table: Table<A, PI, Ix>, parent: Query<A>, iterator: (parentIterator: IterableIterator<Readonly<A>>) => IterableIterator<Readonly<A>>);
43
- [Symbol.iterator](): IterableIterator<Readonly<A>>;
48
+ constructor(table: Table<Document, PI, Ix>, parent: Query<Document>, iterator: (parentIterator: IterableIterator<Readonly<Document>>) => IterableIterator<Readonly<Document>>);
49
+ [Symbol.iterator](): IterableIterator<Readonly<Document>>;
44
50
  }
45
- export declare class ArrayQuery<A extends object, PI extends UnitIndex<A>, Ix extends object> extends TableQuery<A, PI, Ix> {
51
+ export declare class ArrayQuery<Document extends object, PI extends UnitIndex<Document>, Ix extends object> extends TableQuery<Document, PI, Ix> {
46
52
  private readonly values;
47
- constructor(table: Table<A, PI, Ix>, values: Array<Readonly<A>>);
48
- [Symbol.iterator](): IterableIterator<Readonly<A>>;
53
+ constructor(table: Table<Document, PI, Ix>, values: Array<Readonly<Document>>);
54
+ [Symbol.iterator](): IterableIterator<Readonly<Document>>;
49
55
  }
package/dist/query.js CHANGED
@@ -5,38 +5,37 @@ export var IteratorDirection;
5
5
  (function (IteratorDirection) {
6
6
  IteratorDirection[IteratorDirection["Ascending"] = 0] = "Ascending";
7
7
  IteratorDirection[IteratorDirection["Descending"] = 1] = "Descending";
8
- IteratorDirection[IteratorDirection["DescendingExclusive"] = 2] = "DescendingExclusive";
9
8
  })(IteratorDirection || (IteratorDirection = {}));
10
- function* indexIterator(table, direction, start) {
9
+ function* indexIterator(table, direction, start, skipStart = false) {
11
10
  if (table.isEmpty) {
12
11
  return;
13
12
  }
14
- if (direction === IteratorDirection.Ascending) {
15
- for (const items of table.values(start)) {
16
- for (const item of items) {
17
- yield item;
18
- }
13
+ const entries = direction === IteratorDirection.Ascending
14
+ ? table.entries(start)
15
+ : table.entriesReversed(start);
16
+ for (const entry of entries) {
17
+ if (skipStart && table._compare(entry[0], start) === 0) {
18
+ skipStart = false;
19
19
  }
20
- }
21
- else {
22
- for (const entry of table.entriesReversed(start, undefined, direction === IteratorDirection.DescendingExclusive)) {
20
+ else {
23
21
  for (const item of entry[1]) {
24
22
  yield item;
25
23
  }
26
24
  }
27
25
  }
28
26
  }
29
- function* primaryIndexIterator(table, direction, start) {
27
+ function* primaryIndexIterator(table, direction, start, skipStart = false) {
30
28
  if (table.isEmpty) {
31
29
  return;
32
30
  }
33
- if (direction === IteratorDirection.Ascending) {
34
- for (const item of table.values(start)) {
35
- yield item;
31
+ const entries = direction === IteratorDirection.Ascending
32
+ ? table.entries(start)
33
+ : table.entriesReversed(start);
34
+ for (const entry of entries) {
35
+ if (skipStart && table._compare(entry[0], start) === 0) {
36
+ skipStart = false;
36
37
  }
37
- }
38
- else {
39
- for (const entry of table.entriesReversed(start, undefined, direction === IteratorDirection.DescendingExclusive)) {
38
+ else {
40
39
  yield entry[1];
41
40
  }
42
41
  }
@@ -65,8 +64,11 @@ export class TableQuery extends Query {
65
64
  return Array.from(this);
66
65
  });
67
66
  }
67
+ /**
68
+ * Delete all documents from the table matching this query.
69
+ */
68
70
  delete() {
69
- return this.table.delete(...Array.from(this));
71
+ return this.table.delete(...Array.from(this).map((doc) => this.table.primaryIndex.extractKey(doc)));
70
72
  }
71
73
  filter(predicate) {
72
74
  return new ChainQuery(this.table, this, (iter) => Iterator.from(iter).filter(predicate));
@@ -78,6 +80,7 @@ export class TableQuery extends Query {
78
80
  export class IndexQuery extends TableQuery {
79
81
  constructor(table, indexKey, direction, start) {
80
82
  super(table);
83
+ this.skipStart = false;
81
84
  this.isPrimary = table.primaryIndex.name === indexKey;
82
85
  if (this.isPrimary) {
83
86
  this.index = table.primaryIndex;
@@ -92,8 +95,8 @@ export class IndexQuery extends TableQuery {
92
95
  }
93
96
  [Symbol.iterator]() {
94
97
  return this.isPrimary
95
- ? primaryIndexIterator(this.table.primary, this.direction, this.start)
96
- : indexIterator(this.indexTable, this.direction, this.start);
98
+ ? primaryIndexIterator(this.table.primary, this.direction, this.start, this.skipStart)
99
+ : indexIterator(this.indexTable, this.direction, this.start, this.skipStart);
97
100
  }
98
101
  reverse() {
99
102
  switch (this.direction) {
@@ -103,8 +106,6 @@ export class IndexQuery extends TableQuery {
103
106
  case IteratorDirection.Descending:
104
107
  this.direction = IteratorDirection.Ascending;
105
108
  return this;
106
- case IteratorDirection.DescendingExclusive:
107
- throw new Error("not sure how to reverse IteratorDirection.DescendingExclusive");
108
109
  }
109
110
  }
110
111
  equals(value) {
@@ -116,7 +117,18 @@ export class IndexQuery extends TableQuery {
116
117
  }
117
118
  below(value) {
118
119
  this.start = value;
119
- this.direction = IteratorDirection.DescendingExclusive;
120
+ this.direction = IteratorDirection.Descending;
121
+ this.skipStart = true;
122
+ return this;
123
+ }
124
+ above(value) {
125
+ this.start = value;
126
+ this.direction = IteratorDirection.Ascending;
127
+ this.skipStart = true;
128
+ return this;
129
+ }
130
+ inclusive() {
131
+ this.skipStart = false;
120
132
  return this;
121
133
  }
122
134
  max() {
package/dist/query.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAMvC,MAAM,CAAN,IAAY,iBAIX;AAJD,WAAY,iBAAiB;IACzB,mEAAa,CAAA;IACb,qEAAc,CAAA;IACd,uFAAuB,CAAA;AAC3B,CAAC,EAJW,iBAAiB,KAAjB,iBAAiB,QAI5B;AAED,QAAQ,CAAC,CAAC,aAAa,CACnB,KAAoC,EACpC,SAA4B,EAC5B,KAAoB;IAEpB,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;QAChB,OAAO;IACX,CAAC;IACD,IAAI,SAAS,KAAK,iBAAiB,CAAC,SAAS,EAAE,CAAC;QAC5C,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACtC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBACvB,MAAM,IAAI,CAAC;YACf,CAAC;QACL,CAAC;IACL,CAAC;SAAM,CAAC;QACJ,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,eAAe,CACrC,KAAK,EACL,SAAS,EACT,SAAS,KAAK,iBAAiB,CAAC,mBAAmB,CACtD,EAAE,CAAC;YACA,KAAK,MAAM,IAAI,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QAAQ,CAAC,EAAE,CAAC,CAAC;IACzD,CAAC;CACJ;AAED,MAAM,OAAO,UAIX,SAAQ,UAA4B;IAGlC,YAAY,KAA8B,EAAE,MAAiC;QACzE,KAAK,CAAC,KAAK,CAAC,CAAC;QACb,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED,CAAC,MAAM,CAAC,QAAQ,CAAC;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IAC1C,CAAC;CACJ"}
package/dist/table.d.ts CHANGED
@@ -15,61 +15,182 @@ export type TableEvent<A extends object, PK> = {
15
15
  type: "delete";
16
16
  key: PK;
17
17
  };
18
- export declare class Table<A extends object, PI extends UnitIndex<A>, Ix extends object> extends Emitter<TableEvent<A, PI["keyType"]>> implements Disposable, Iterable<Readonly<A>> {
18
+ /**
19
+ * A database table.
20
+ *
21
+ * To learn how to create a table, see the static method {@link Table.create}.
22
+ *
23
+ * @template Document The type of the documents this table stores.
24
+ */
25
+ export declare class Table<Document extends object, PrimaryIndex extends UnitIndex<Document>, Indices extends object> extends Emitter<TableEvent<Document, PrimaryIndex["keyType"]>> implements Disposable, Iterable<Readonly<Document>> {
19
26
  #private;
20
- ready: Promise<void>;
21
- readonly primaryIndex: PI;
22
- readonly primary: BTree<PI["keyType"], Readonly<A>>;
27
+ /**
28
+ * A promise which resolves when this table is ready to use.
29
+ *
30
+ * If the table isn't connected to a {@link StorageBackend}, this is a
31
+ * promise which resolves immediately, and you don't really need to await
32
+ * it. The table will be ready for use immediately.
33
+ *
34
+ * With an attached {@link StorageBackend}, this promise will resolve when
35
+ * the table has finished restoring its contents from the storage. You
36
+ * should not under any circumstances use the table before this is complete.
37
+ */
38
+ get ready(): Promise<void>;
39
+ /** @ignore */
40
+ readonly primaryIndex: PrimaryIndex;
41
+ /** @ignore */
42
+ readonly primary: BTree<PrimaryIndex["keyType"], Readonly<Document>>;
43
+ /** @ignore */
23
44
  readonly indices: {
24
- [K in Exclude<keyof Ix & string, PI["name"]>]: Index<A>;
45
+ [K in Exclude<keyof Indices & string, PrimaryIndex["name"]>]: Index<Document>;
25
46
  };
47
+ /** @ignore */
26
48
  readonly indexTables: {
27
- [K in Exclude<keyof Ix & string, PI["name"]>]: BTree<Ix[K], Array<A>>;
49
+ [K in Exclude<keyof Indices & string, PrimaryIndex["name"]>]: BTree<Indices[K], Array<Document>>;
50
+ };
51
+ /**
52
+ * A signal which updates whenever the table has been modified.
53
+ */
54
+ get changed(): Signal.Computed<null>;
55
+ /**
56
+ * Create a database {@link Table}.
57
+ *
58
+ * `Table.create` is a function which takes one type argument, the type of the
59
+ * object (which we'll call a *document*) you want the table to store, and no
60
+ * value arguments.
61
+ *
62
+ * This returns an object with one method: `withPrimaryIndex`. This method is
63
+ * what actually creates the table. So, the full incantation is, for instance:
64
+ *
65
+ * ```ts
66
+ * type Document = { id: string; value: number };
67
+ * const table = Table.create<Document>()
68
+ * .withPrimaryIndex(index<Document>().key("id"));
69
+ * ```
70
+ *
71
+ * In order to look up something in a database table, you need an index. You can
72
+ * create an index using the {@link index} function, which, like `Table.create`,
73
+ * takes the document you're creating an index for as its type argument, and
74
+ * returns a selection of index constructors, of which the most straightforward
75
+ * one is {@link IndexConstructor.key}. This creates an index for a single named
76
+ * property of the document containing a primitive comparable value (a string, a
77
+ * number, or a bigint), and allows you to search for documents where the given
78
+ * property matches any given value. You can also create an index over multiple
79
+ * keys using {@link IndexConstructor.keys} or over a key containing an array
80
+ * using {@link IndexConstructor.array}. You can even create a completely
81
+ * customised index using {@link IndexConstructor.custom}.
82
+ *
83
+ * The primary index, unlike a regular index, is a unique identifier, and only
84
+ * one document can exist at any given time under the key or keys represented by
85
+ * the primary index. A table is required to have a primary index, which is why
86
+ * `Table.create` doesn't actually create the table until you call
87
+ * `withPrimaryIndex` on it. You can then add as many extra indices as you like
88
+ * using `withIndex`. To extend our example above with an additional index over
89
+ * the `value` property:
90
+ *
91
+ * ```ts
92
+ * type Document = { id: string; value: number };
93
+ * const table = Table.create<Document>()
94
+ * .withPrimaryIndex(index<Document>().key("id"))
95
+ * .withIndex(index<Document>().key("value"));
96
+ * ```
97
+ *
98
+ * @see {@link index}
99
+ *
100
+ * @template Document The type of the document this table stores.
101
+ */
102
+ static create<Document extends object>(): {
103
+ withPrimaryIndex: <PrimaryIndex extends UnitIndex<Document>>(primaryIndex: PrimaryIndex) => Table<Document, PrimaryIndex, PrimaryIndex["record"]>;
28
104
  };
29
- readonly changed: Signal.State<number>;
30
- constructor(primaryIndex: PI);
105
+ /** @internal */
106
+ private constructor();
31
107
  [Symbol.dispose](): void;
32
- withIndex<I extends Index<A>>(index: I): Table<A, PI, {
33
- [K in keyof (Ix & I["record"])]: (Ix & I["record"])[K];
108
+ /**
109
+ * Add an index to a table.
110
+ *
111
+ * @example
112
+ * type Document = { id: string; value: number };
113
+ * const table = Table.create<Document>()
114
+ * .withPrimaryIndex(index<Document>().key("id"))
115
+ * .withIndex(index<Document>().key("value"));
116
+ */
117
+ withIndex<I extends Index<Document>>(index: I): Table<Document, PrimaryIndex, {
118
+ [K in keyof (Indices & I["record"])]: (Indices & I["record"])[K];
34
119
  }>;
120
+ /**
121
+ * Attach a table to a {@link StorageBackend}.
122
+ *
123
+ * See {@link IndexedDBBackend.open} for an example of how to use this.
124
+ */
35
125
  withStorage(backend: StorageBackend, name: string): this;
36
- signal(primaryKey: PI["keyType"]): Signal.Computed<Readonly<A> | undefined>;
37
- get(primaryKey: PI["keyType"]): Readonly<A> | undefined;
126
+ signal(primaryKey: PrimaryIndex["keyType"]): Signal.Computed<Readonly<Document> | undefined>;
127
+ get(primaryKey: PrimaryIndex["keyType"]): Readonly<Document> | undefined;
38
128
  /**
39
129
  * Given a primary key, apply an update function to the value stored under
40
130
  * that key. If there's no value, call the create function to create the
41
131
  * value before passing it to the update function.
42
132
  */
43
- createAndUpdate(primaryKey: PI["keyType"], create: () => A, update: (item: Draft<A>) => Draft<A> | void): Readonly<A>;
133
+ createAndUpdate(primaryKey: PrimaryIndex["keyType"], create: () => Document, update: (item: Draft<Document>) => Draft<Document> | void): Readonly<Document>;
44
134
  /**
45
- * Given a primary key, apply an update function to the value stored under
46
- * that key. If there's no value, call the create function to create a new
47
- * value. In this case, the update function is not called.
135
+ * Create or update a document.
136
+ *
137
+ * Given a primary key, apply the `update` function to the document stored
138
+ * under that key.
139
+ *
140
+ * If there's no such document, call the `create` function to create a new
141
+ * document and insert that under the given primary key. In this case, the
142
+ * update function is not called.
143
+ *
144
+ * The `update` function is passed to {@link produce | Immer.produce} to
145
+ * perform the update. It should modify the provided document in place, and
146
+ * it's not necessary to return it.
48
147
  */
49
- createOrUpdate(primaryKey: PI["keyType"], create: () => A, update: (item: Draft<A>) => Draft<A> | void): Readonly<A>;
148
+ createOrUpdate(primaryKey: PrimaryIndex["keyType"], create: () => Document, update: (item: Draft<Document>) => Draft<Document> | void): Readonly<Document>;
50
149
  /**
51
- * Apply an update function to the value stored under the given primary key.
52
- * If no value exists, the update function is not called and `undefined` is
53
- * returned. Otherwise, the updated value is returned.
150
+ * Update a document.
151
+ *
152
+ * Apply the `update` function to the document stored under the given
153
+ * primary key.
154
+ *
155
+ * If no such document exists, the update function is not called and
156
+ * `undefined` is returned. Otherwise, the updated document is returned.
157
+ *
158
+ * The `update` function is passed to {@link produce | Immer.produce} to
159
+ * perform the update. It should modify the provided document in place, and
160
+ * it's not necessary to return it.
54
161
  */
55
- update(primaryKey: PI["keyType"], update: (item: Draft<A>) => void | Draft<A>): Readonly<A> | undefined;
162
+ update(primaryKey: PrimaryIndex["keyType"], update: (item: Draft<Document>) => void | Draft<Document>): Readonly<Document> | undefined;
56
163
  /**
57
- * Add items to the table, overwriting any previous values under their
164
+ * Add documents to the table, overwriting any previous values under their
58
165
  * primary keys.
59
166
  */
60
- add(...items: Array<A>): void;
61
- add(items: Iterable<A>): void;
167
+ add(...items: Array<Document>): void;
168
+ add(items: Iterable<Document>): void;
62
169
  /**
63
- * Delete items from the table by their primary keys.
170
+ * Delete documents from the table by their primary keys.
64
171
  */
65
- delete(...primaryKeys: Array<PI["keyType"]>): number;
66
- delete(primaryKeys: Iterable<PI["keyType"]>): number;
172
+ delete(...primaryKeys: Array<PrimaryIndex["keyType"]>): number;
173
+ delete(primaryKeys: Iterable<PrimaryIndex["keyType"]>): number;
67
174
  /**
68
- * Delete all data from the table.
175
+ * Delete all documents from the table.
69
176
  */
70
177
  clear(): void;
71
- [Symbol.iterator](): IterableIterator<Readonly<A>>;
72
- where<K extends keyof Ix & string, I extends Index<A> & Ix[K]>(index: K): IndexQuery<A, PI, I, Ix>;
73
- orderBy<K extends keyof Ix & string, I extends Index<A> & Ix[K]>(index: K): IndexQuery<A, PI, I, Ix>;
178
+ /**
179
+ * Iterate over the documents in the table, ordered by primary key.
180
+ */
181
+ [Symbol.iterator](): IteratorObject<Readonly<Document>>;
182
+ /**
183
+ * Perform an {@link IndexQuery} using the specified index.
184
+ */
185
+ where<K extends keyof Indices & string, I extends Index<Document> & Indices[K]>(index: K): IndexQuery<Document, PrimaryIndex, I, Indices>;
186
+ /**
187
+ * Perform an {@link IndexQuery} using the specified index.
188
+ *
189
+ * This is an alias for {@link Table.where}.
190
+ */
191
+ orderBy<K extends keyof Indices & string, I extends Index<Document> & Indices[K]>(index: K): IndexQuery<Document, PrimaryIndex, I, Indices>;
192
+ /**
193
+ * Get the number of documents currently stored in the table.
194
+ */
74
195
  size(): number;
75
196
  }