localdb-ces6q 0.0.4 → 0.1.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/LocalDB.d.ts +2 -2
- package/LocalDB.js +26 -19
- package/package.json +2 -1
- package/types.d.ts +5 -1
package/LocalDB.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
+
import { BTree } from 'bplustree-mq4uj/btree.js';
|
1
2
|
import { Debounce } from 'util-3gcvv/class/Debounce.js';
|
2
|
-
import { SortedArray } from 'util-3gcvv/class/SortedArray.js';
|
3
3
|
import { LocalDBState } from './LocalDBState.js';
|
4
4
|
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
5
5
|
import type { Collection, Collections, CollectionsChange, CollectionsChangeMap, ColListener, DBListener, DocListener, FieldListener, ICollectionConfig, ICollectionsConfig, ICollectionsFields, IDBOperation, IDBOperationHistoryItem, IDBQuery, IDBTxOptions, IDocument } from './types.js';
|
@@ -13,7 +13,7 @@ export declare class LocalDB<CollectionTypes extends Record<string, IDocument>>
|
|
13
13
|
[ColName in KeyOf<CollectionTypes>]: Debounce<() => void>;
|
14
14
|
};
|
15
15
|
protected _indexes: {
|
16
|
-
[ColName in KeyOf<CollectionTypes>]: Record<string,
|
16
|
+
[ColName in KeyOf<CollectionTypes>]: Record<string, BTree<CollectionTypes[ColName], CollectionTypes[ColName]>>;
|
17
17
|
};
|
18
18
|
protected _txChangedFields: CollectionsChangeMap<CollectionTypes>;
|
19
19
|
protected _txChanges: CollectionsChange<CollectionTypes>;
|
package/LocalDB.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
+
import { BTree } from 'bplustree-mq4uj/btree.js';
|
1
2
|
import { Debounce } from 'util-3gcvv/class/Debounce.js';
|
2
|
-
import { SortedArray } from 'util-3gcvv/class/SortedArray.js';
|
3
3
|
import { deepEqual } from 'util-3gcvv/deepEqual.js';
|
4
4
|
import { objectEmpty, objectKeys } from 'util-3gcvv/object.js';
|
5
5
|
import { randomString } from 'util-3gcvv/string.js';
|
@@ -57,7 +57,7 @@ export class LocalDB {
|
|
57
57
|
for (let col in this._indexes) {
|
58
58
|
const indexes = this._indexes[col];
|
59
59
|
for (let name in indexes) {
|
60
|
-
indexes[name].
|
60
|
+
indexes[name].clear();
|
61
61
|
}
|
62
62
|
}
|
63
63
|
this._indexes = {};
|
@@ -135,7 +135,14 @@ export class LocalDB {
|
|
135
135
|
if (!comparator) {
|
136
136
|
throw new Error(`Comparator must be set to index ${col}/${field}`);
|
137
137
|
}
|
138
|
-
fieldIndexes[field] = new
|
138
|
+
fieldIndexes[field] = new BTree(docs.map((d) => [d, d]), (a, b) => comparator(a[field], b[field]));
|
139
|
+
}
|
140
|
+
}
|
141
|
+
const { indexes } = config;
|
142
|
+
if (indexes) {
|
143
|
+
for (const name in indexes) {
|
144
|
+
const { compare } = indexes[name];
|
145
|
+
fieldIndexes[name] = new BTree(docs.map((d) => [d, d]), compare);
|
139
146
|
}
|
140
147
|
}
|
141
148
|
this._indexes[col] = fieldIndexes;
|
@@ -287,7 +294,7 @@ export class LocalDB {
|
|
287
294
|
if (!(index in col)) {
|
288
295
|
throw new Error(`index ${colName}/${index} not found`);
|
289
296
|
}
|
290
|
-
return col[index].
|
297
|
+
return col[index].valuesArray();
|
291
298
|
}
|
292
299
|
query({ collection, orderBy, limit, startAfter, startAt, endAfter, endAt, }) {
|
293
300
|
const col = this._indexes[collection];
|
@@ -296,7 +303,7 @@ export class LocalDB {
|
|
296
303
|
}
|
297
304
|
const index = col[orderBy];
|
298
305
|
// TODO
|
299
|
-
return index.
|
306
|
+
return index.valuesArray();
|
300
307
|
}
|
301
308
|
//
|
302
309
|
// Set / Update
|
@@ -353,12 +360,12 @@ export class LocalDB {
|
|
353
360
|
// update indexes
|
354
361
|
const indexes = this._indexes[colName];
|
355
362
|
for (let indexName in indexes) {
|
356
|
-
indexes[indexName].
|
363
|
+
indexes[indexName].set(next, next, true);
|
357
364
|
}
|
358
365
|
this._txRollbacks.push(() => {
|
359
366
|
const indexes = this._indexes[colName];
|
360
367
|
for (let indexName in indexes) {
|
361
|
-
indexes[indexName].
|
368
|
+
indexes[indexName].delete(next);
|
362
369
|
}
|
363
370
|
});
|
364
371
|
// update undo history
|
@@ -556,14 +563,14 @@ export class LocalDB {
|
|
556
563
|
// update indexes
|
557
564
|
const indexes = this._indexes[colName];
|
558
565
|
for (let indexName in indexes) {
|
559
|
-
indexes[indexName].
|
560
|
-
indexes[indexName].
|
566
|
+
indexes[indexName].delete(prev);
|
567
|
+
indexes[indexName].set(next, next, true);
|
561
568
|
}
|
562
569
|
this._txRollbacks.push(() => {
|
563
570
|
const indexes = this._indexes[colName];
|
564
571
|
for (let indexName in indexes) {
|
565
|
-
indexes[indexName].
|
566
|
-
indexes[indexName].
|
572
|
+
indexes[indexName].delete(next);
|
573
|
+
indexes[indexName].set(prev, prev, true);
|
567
574
|
}
|
568
575
|
});
|
569
576
|
// update undo history
|
@@ -652,14 +659,14 @@ export class LocalDB {
|
|
652
659
|
// update indexes
|
653
660
|
const indexes = this._indexes[colName];
|
654
661
|
for (let indexName in indexes) {
|
655
|
-
indexes[indexName].
|
656
|
-
indexes[indexName].
|
662
|
+
indexes[indexName].deleteKeys(prevDocs);
|
663
|
+
indexes[indexName].setPairs(nextDocs.map((doc) => [doc, doc]), true);
|
657
664
|
}
|
658
665
|
this._txRollbacks.push(() => {
|
659
666
|
const indexes = this._indexes[colName];
|
660
667
|
for (let indexName in indexes) {
|
661
|
-
indexes[indexName].
|
662
|
-
indexes[indexName].
|
668
|
+
indexes[indexName].deleteKeys(nextDocs);
|
669
|
+
indexes[indexName].setPairs(prevDocs.map((doc) => [doc, doc]), true);
|
663
670
|
}
|
664
671
|
});
|
665
672
|
// update undo history
|
@@ -715,12 +722,12 @@ export class LocalDB {
|
|
715
722
|
// update indexes
|
716
723
|
const indexes = this._indexes[colName];
|
717
724
|
for (let indexName in indexes) {
|
718
|
-
indexes[indexName].
|
725
|
+
indexes[indexName].delete(prev);
|
719
726
|
}
|
720
727
|
this._txRollbacks.push(() => {
|
721
728
|
const indexes = this._indexes[colName];
|
722
729
|
for (let indexName in indexes) {
|
723
|
-
indexes[indexName].
|
730
|
+
indexes[indexName].set(prev, prev, true);
|
724
731
|
}
|
725
732
|
});
|
726
733
|
// update undo history
|
@@ -786,12 +793,12 @@ export class LocalDB {
|
|
786
793
|
// update indexes
|
787
794
|
const indexes = this._indexes[colName];
|
788
795
|
for (let indexName in indexes) {
|
789
|
-
indexes[indexName].
|
796
|
+
indexes[indexName].deleteKeys(prevDocs);
|
790
797
|
}
|
791
798
|
this._txRollbacks.push(() => {
|
792
799
|
const indexes = this._indexes[colName];
|
793
800
|
for (let indexName in indexes) {
|
794
|
-
indexes[indexName].
|
801
|
+
indexes[indexName].setPairs(prevDocs.map((d) => [d, d]), true);
|
795
802
|
}
|
796
803
|
});
|
797
804
|
// update undo history
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "localdb-ces6q",
|
3
|
-
"version": "0.0
|
3
|
+
"version": "0.1.0",
|
4
4
|
"description": "TypeScript ECMAScript Module Library Template",
|
5
5
|
"type": "module",
|
6
6
|
"module": "dist/index.js",
|
@@ -26,6 +26,7 @@
|
|
26
26
|
},
|
27
27
|
"homepage": "https://github.com/joonhocho/localdb#readme",
|
28
28
|
"dependencies": {
|
29
|
+
"bplustree-mq4uj": "^0.0.2",
|
29
30
|
"util-3gcvv": "^0.4.7"
|
30
31
|
},
|
31
32
|
"devDependencies": {
|
package/types.d.ts
CHANGED
@@ -29,12 +29,16 @@ export type ICollectionFieldsConfig<Doc extends IDocument> = {
|
|
29
29
|
index?: 'asc' | 'desc';
|
30
30
|
};
|
31
31
|
};
|
32
|
+
export interface IDocumentIndexConfig<Doc extends IDocument> {
|
33
|
+
fields: Array<KeyOf<Doc>>;
|
34
|
+
compare: (a: Doc, b: Doc) => number;
|
35
|
+
}
|
32
36
|
export interface ICollectionConfig<CollectionTypes extends Record<string, IDocument>, Doc extends IDocument> {
|
33
37
|
localStorageKey?: string;
|
34
38
|
localStorageSetWait?: number;
|
35
39
|
remoteStorageKey?: string;
|
36
40
|
remoteStorageSetWait?: number;
|
37
|
-
indexes?:
|
41
|
+
indexes?: Record<string, IDocumentIndexConfig<Doc>>;
|
38
42
|
idFields?: Array<KeyOf<Doc>>;
|
39
43
|
fields: ICollectionFieldsConfig<Doc>;
|
40
44
|
computes?: Array<{
|