localdb-ces6q 0.0.2
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/LICENSE +21 -0
- package/README.md +2 -0
- package/package.json +49 -0
- package/src/LocalDB.d.ts +102 -0
- package/src/LocalDB.js +966 -0
- package/src/LocalDBDocument.d.ts +18 -0
- package/src/LocalDBDocument.js +55 -0
- package/src/LocalDBState.d.ts +14 -0
- package/src/LocalDBState.js +46 -0
- package/src/comparators.d.ts +15 -0
- package/src/comparators.js +105 -0
- package/src/hooks.d.ts +5 -0
- package/src/hooks.js +34 -0
- package/src/types.d.ts +100 -0
- package/src/types.js +1 -0
- package/test/LocalDB.test.d.ts +1 -0
- package/test/LocalDB.test.js +1137 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Joon Ho Cho
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/package.json
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
{
|
2
|
+
"name": "localdb-ces6q",
|
3
|
+
"version": "0.0.2",
|
4
|
+
"description": "TypeScript ECMAScript Module Library Template",
|
5
|
+
"type": "module",
|
6
|
+
"module": "dist/index.js",
|
7
|
+
"scripts": {
|
8
|
+
"build": "tsc",
|
9
|
+
"build:watch": "tsc -w",
|
10
|
+
"format": "prettier --write {src,test}/**/*.{ts,tsx,js,jsx}",
|
11
|
+
"pub": "node publish.js",
|
12
|
+
"test": "jest -c ./jest.config.ts",
|
13
|
+
"test:cov": "jest -c ./jest.config.ts --collect-coverage",
|
14
|
+
"test:watch": "jest -c ./jest.config.ts --watch",
|
15
|
+
"typecheck": "tsc --noEmit",
|
16
|
+
"typecheck:watch": "tsc --noEmit -w"
|
17
|
+
},
|
18
|
+
"repository": {
|
19
|
+
"type": "git",
|
20
|
+
"url": "git+https://github.com/joonhocho/localdb.git"
|
21
|
+
},
|
22
|
+
"author": "",
|
23
|
+
"license": "MIT",
|
24
|
+
"bugs": {
|
25
|
+
"url": "https://github.com/joonhocho/localdb/issues"
|
26
|
+
},
|
27
|
+
"homepage": "https://github.com/joonhocho/localdb#readme",
|
28
|
+
"dependencies": {
|
29
|
+
"util-3gcvv": "^0.4.6"
|
30
|
+
},
|
31
|
+
"devDependencies": {
|
32
|
+
"@babel/core": "^7.21.3",
|
33
|
+
"@babel/preset-env": "^7.20.2",
|
34
|
+
"@babel/preset-typescript": "^7.21.0",
|
35
|
+
"@jest/globals": "^29.5.0",
|
36
|
+
"@types/jest": "^29.4.2",
|
37
|
+
"@types/react": "^18.0.28",
|
38
|
+
"jest-ts-webcompat-resolver": "^1.0.0",
|
39
|
+
"prettier": "^2.8.4",
|
40
|
+
"react": "^18.2.0",
|
41
|
+
"shelljs": "^0.8.5",
|
42
|
+
"ts-jest": "^29.0.5",
|
43
|
+
"typescript": "^4.9.5"
|
44
|
+
},
|
45
|
+
"publishConfig": {
|
46
|
+
"access": "public"
|
47
|
+
},
|
48
|
+
"sideEffects": false
|
49
|
+
}
|
package/src/LocalDB.d.ts
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
import { Debounce } from 'util-3gcvv/class/Debounce.js';
|
2
|
+
import { SortedArray } from 'util-3gcvv/class/SortedArray.js';
|
3
|
+
import { LocalDBState } from './LocalDBState.js';
|
4
|
+
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
5
|
+
import type { Collection, Collections, CollectionsChange, CollectionsChangeMap, ColListener, DBListener, DocListener, FieldListener, ICollectionConfig, ICollectionsConfig, ICollectionsFields, IDBOperation, IDBOperationHistoryItem, IDBQuery, IDBTxOptions, IDocument } from './types.js';
|
6
|
+
export declare class LocalDB<CollectionTypes extends Record<string, IDocument>> {
|
7
|
+
protected _options?: IDBTxOptions;
|
8
|
+
protected _collectionNames: Array<KeyOf<CollectionTypes>>;
|
9
|
+
protected _collections: Collections<CollectionTypes>;
|
10
|
+
protected _config: ICollectionsConfig<CollectionTypes>;
|
11
|
+
protected _fields: ICollectionsFields<CollectionTypes>;
|
12
|
+
protected _saveCols: {
|
13
|
+
[ColName in KeyOf<CollectionTypes>]: Debounce<() => void>;
|
14
|
+
};
|
15
|
+
protected _indexes: {
|
16
|
+
[ColName in KeyOf<CollectionTypes>]: Record<string, SortedArray<CollectionTypes[ColName]>>;
|
17
|
+
};
|
18
|
+
protected _txChangedFields: CollectionsChangeMap<CollectionTypes>;
|
19
|
+
protected _txChanges: CollectionsChange<CollectionTypes>;
|
20
|
+
protected _txKey: string | null;
|
21
|
+
protected _txOps: IDBOperationHistoryItem[];
|
22
|
+
protected _txRollbacks: VoidFunction[];
|
23
|
+
protected _txSnapshot: Collections<CollectionTypes> | null;
|
24
|
+
protected _txOptions: IDBTxOptions | null;
|
25
|
+
protected _noEvent: boolean;
|
26
|
+
protected _undoable: boolean;
|
27
|
+
protected _inUndoOrRedo: boolean;
|
28
|
+
protected _opQueue: IDBOperation[];
|
29
|
+
protected _commiting: boolean;
|
30
|
+
protected _listenerId: number;
|
31
|
+
protected _dbListeners: Array<{
|
32
|
+
lid: number;
|
33
|
+
handler: DBListener<CollectionTypes>;
|
34
|
+
}>;
|
35
|
+
protected _colListeners: Array<{
|
36
|
+
lid: number;
|
37
|
+
collection: KeyOf<CollectionTypes>;
|
38
|
+
handler: ColListener<CollectionTypes, any>;
|
39
|
+
}>;
|
40
|
+
protected _docListeners: Array<{
|
41
|
+
lid: number;
|
42
|
+
collection: KeyOf<CollectionTypes>;
|
43
|
+
id: string;
|
44
|
+
handler: DocListener<CollectionTypes, any>;
|
45
|
+
}>;
|
46
|
+
protected _fieldListeners: Array<{
|
47
|
+
lid: number;
|
48
|
+
collection: KeyOf<CollectionTypes>;
|
49
|
+
id: string;
|
50
|
+
field: string;
|
51
|
+
handler: FieldListener<CollectionTypes, any, any>;
|
52
|
+
}>;
|
53
|
+
state: LocalDBState;
|
54
|
+
constructor(config: ICollectionsConfig<CollectionTypes>, initialData?: Collections<CollectionTypes>, options?: IDBTxOptions);
|
55
|
+
destroy(): void;
|
56
|
+
toJSON(): Collections<CollectionTypes>;
|
57
|
+
equals(other: Collections<CollectionTypes>): boolean;
|
58
|
+
defineCollection<Data extends IDocument>(colName: string, config: ICollectionConfig<CollectionTypes, Data>, initialData?: {
|
59
|
+
[id in string]: Data;
|
60
|
+
}): void;
|
61
|
+
deleteCollection<ColName extends KeyOf<CollectionTypes>>(col: ColName): void;
|
62
|
+
existsCollection(col: string): boolean;
|
63
|
+
get collectionNames(): Array<KeyOf<CollectionTypes>>;
|
64
|
+
beginTx(options?: IDBTxOptions | undefined): string | null;
|
65
|
+
endTx(txKey: string | null): void;
|
66
|
+
protected _rollback(): void;
|
67
|
+
tx(fn: () => void, options?: IDBTxOptions): void;
|
68
|
+
undoableTx(fn: () => void, options?: IDBTxOptions): void;
|
69
|
+
subToDB(handler: DBListener<CollectionTypes>): () => void;
|
70
|
+
unsubFromDB(lid: number): void;
|
71
|
+
subToCol<ColName extends KeyOf<CollectionTypes>>(collection: ColName, handler: ColListener<CollectionTypes, ColName>): () => void;
|
72
|
+
unsubFromCol(lid: number): void;
|
73
|
+
subToDoc<ColName extends KeyOf<CollectionTypes>>(collection: ColName, id: string, handler: DocListener<CollectionTypes, ColName>): () => void;
|
74
|
+
unsubFromDoc(lid: number): void;
|
75
|
+
subToField<ColName extends KeyOf<CollectionTypes>, Field extends KeyOf<CollectionTypes[ColName]>>(collection: ColName, id: string, field: Field, handler: FieldListener<CollectionTypes, ColName, Field>): () => void;
|
76
|
+
unsubFromField(lid: number): void;
|
77
|
+
collection<ColName extends KeyOf<CollectionTypes>>(colName: ColName): Collection<CollectionTypes, ColName>;
|
78
|
+
doc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, id: string): CollectionTypes[ColName] | undefined;
|
79
|
+
docs<ColName extends KeyOf<CollectionTypes>>(colName: ColName, ids: string[]): Array<CollectionTypes[ColName] | undefined>;
|
80
|
+
docsOrderBy<ColName extends KeyOf<CollectionTypes>>(colName: ColName, index: string): Array<CollectionTypes[ColName]>;
|
81
|
+
query<ColName extends KeyOf<CollectionTypes>>({ collection, orderBy, limit, startAfter, startAt, endAfter, endAt, }: IDBQuery<CollectionTypes, ColName>): Array<CollectionTypes[ColName]>;
|
82
|
+
setDoc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, doc: CollectionTypes[ColName], options?: IDBTxOptions): void;
|
83
|
+
setDocs<ColName extends KeyOf<CollectionTypes>>(colName: ColName, docs: Array<CollectionTypes[ColName]>, options?: IDBTxOptions): void;
|
84
|
+
protected _updateDoc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, prev: CollectionTypes[ColName], update: Partial<CollectionTypes[ColName]>): {
|
85
|
+
next: CollectionTypes[ColName];
|
86
|
+
changedKeys: Array<KeyOf<CollectionTypes[ColName]>>;
|
87
|
+
} | null;
|
88
|
+
updateDoc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, id: string, updater: Partial<CollectionTypes[ColName]> | ((prev: CollectionTypes[ColName]) => Partial<CollectionTypes[ColName]> | null | undefined), options?: IDBTxOptions): void;
|
89
|
+
updateDocs<ColName extends KeyOf<CollectionTypes>>(colName: ColName, updater: Record<string, Partial<CollectionTypes[ColName]>> | ((prev: Collections<CollectionTypes>[ColName]) => Record<string, Partial<CollectionTypes[ColName]> | null | undefined> | null | undefined), options?: IDBTxOptions): void;
|
90
|
+
deleteDoc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, id: string, options?: IDBTxOptions): void;
|
91
|
+
deleteDocs<ColName extends KeyOf<CollectionTypes>>(colName: ColName, ids: string[], options?: IDBTxOptions): void;
|
92
|
+
protected _commitChanges(): void;
|
93
|
+
protected _flushQueue(): void;
|
94
|
+
protected _pushHistory(): void;
|
95
|
+
undo(): void;
|
96
|
+
redo(): void;
|
97
|
+
loadCollectionFromStorage<ColName extends KeyOf<CollectionTypes>>(collection: ColName): Record<string, CollectionTypes[ColName]> | undefined;
|
98
|
+
saveCollectionToStorage<ColName extends KeyOf<CollectionTypes>>(collection: ColName): void;
|
99
|
+
protected loadFromStorage(): void;
|
100
|
+
saveToStorage(): void;
|
101
|
+
saveDebounced: Debounce<() => void>;
|
102
|
+
}
|