localdb-ces6q 0.2.23 → 0.2.24
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/package.json +10 -1
- package/publish.js +21 -0
- package/LocalDB.d.ts +0 -103
- package/LocalDB.js +0 -980
- package/LocalDBDocument.d.ts +0 -18
- package/LocalDBDocument.js +0 -55
- package/LocalDBState.d.ts +0 -14
- package/LocalDBState.js +0 -46
- package/comparators.d.ts +0 -15
- package/comparators.js +0 -105
- package/hooks.d.ts +0 -5
- package/hooks.js +0 -34
- package/types.d.ts +0 -104
- package/types.js +0 -1
package/LocalDBDocument.d.ts
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
import { PubSub } from 'util-3gcvv/class/PubSub.js';
|
2
|
-
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
3
|
-
export declare class LocalDBDocument<Data = any> extends PubSub<[
|
4
|
-
Data,
|
5
|
-
Data,
|
6
|
-
Array<KeyOf<Data>>
|
7
|
-
]> {
|
8
|
-
private _data;
|
9
|
-
constructor(data: Data);
|
10
|
-
toJSON(): Data;
|
11
|
-
equals(other: LocalDBDocument<Data>): boolean;
|
12
|
-
destroy(): void;
|
13
|
-
get id(): string;
|
14
|
-
get data(): Data;
|
15
|
-
set data(next: Data);
|
16
|
-
set(next: Data): void;
|
17
|
-
update(update: Partial<Data>): void;
|
18
|
-
}
|
package/LocalDBDocument.js
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
import { PubSub } from 'util-3gcvv/class/PubSub.js';
|
2
|
-
import { deepEqual } from 'util-3gcvv/deepEqual.js';
|
3
|
-
import { objectKeys } from 'util-3gcvv/object.js';
|
4
|
-
export class LocalDBDocument extends PubSub {
|
5
|
-
constructor(data) {
|
6
|
-
super();
|
7
|
-
this._data = data;
|
8
|
-
}
|
9
|
-
toJSON() {
|
10
|
-
return this._data;
|
11
|
-
}
|
12
|
-
equals(other) {
|
13
|
-
return deepEqual(this._data, other._data);
|
14
|
-
}
|
15
|
-
destroy() {
|
16
|
-
super.destroy();
|
17
|
-
this._data = null;
|
18
|
-
}
|
19
|
-
get id() {
|
20
|
-
return this._data.id;
|
21
|
-
}
|
22
|
-
get data() {
|
23
|
-
return this._data;
|
24
|
-
}
|
25
|
-
set data(next) {
|
26
|
-
this.set(next);
|
27
|
-
}
|
28
|
-
set(next) {
|
29
|
-
return this.update(next);
|
30
|
-
}
|
31
|
-
update(update) {
|
32
|
-
const prev = this._data;
|
33
|
-
const keys = objectKeys(update);
|
34
|
-
const changed = {};
|
35
|
-
const changedKeys = [];
|
36
|
-
const next = { ...prev };
|
37
|
-
for (let i = 0, il = keys.length; i < il; i += 1) {
|
38
|
-
const key = keys[i];
|
39
|
-
if (!deepEqual(update[key], prev[key])) {
|
40
|
-
changed[key] = update[key];
|
41
|
-
if (update[key] === undefined) {
|
42
|
-
delete next[key];
|
43
|
-
}
|
44
|
-
else {
|
45
|
-
next[key] = update[key];
|
46
|
-
}
|
47
|
-
changedKeys.push(key);
|
48
|
-
}
|
49
|
-
}
|
50
|
-
if (changedKeys.length) {
|
51
|
-
this._data = next;
|
52
|
-
this.emit(next, prev, changedKeys);
|
53
|
-
}
|
54
|
-
}
|
55
|
-
}
|
package/LocalDBState.d.ts
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
import type { SmartState } from 'util-3gcvv/class/SmartStateTypes.js';
|
2
|
-
import type { IDBOperationHistoryItem } from './types.js';
|
3
|
-
export interface ILocalDBStateProps {
|
4
|
-
history: IDBOperationHistoryItem[][];
|
5
|
-
historyIndex: number;
|
6
|
-
canRedo: boolean;
|
7
|
-
canUndo: boolean;
|
8
|
-
redoItem: IDBOperationHistoryItem[] | undefined;
|
9
|
-
undoItem: IDBOperationHistoryItem[] | undefined;
|
10
|
-
}
|
11
|
-
type ComputedKeys = 'canUndo' | 'canRedo' | 'redoItem' | 'undoItem';
|
12
|
-
export declare const LocalDBState: import("util-3gcvv/class/SmartStateTypes.js").SmartStateConstructor<ILocalDBStateProps, ComputedKeys, {}, import("util-3gcvv/types/types.js").IEmptyInterface>;
|
13
|
-
export type LocalDBState = SmartState<ILocalDBStateProps, ComputedKeys, {}>;
|
14
|
-
export {};
|
package/LocalDBState.js
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
import { defineSmartState } from 'util-3gcvv/class/SmartState.js';
|
2
|
-
export const LocalDBState = defineSmartState({
|
3
|
-
statics: {
|
4
|
-
name: 'LocalDBState',
|
5
|
-
fromJSON: (json) => new LocalDBState(json.state, json.config),
|
6
|
-
},
|
7
|
-
properties: {
|
8
|
-
history: { type: 'array', item: 'object' },
|
9
|
-
historyIndex: { type: 'number' },
|
10
|
-
},
|
11
|
-
computed: {
|
12
|
-
// []
|
13
|
-
// [a, b, c, d]
|
14
|
-
redoItem: {
|
15
|
-
type: 'array',
|
16
|
-
item: 'object',
|
17
|
-
deps: ['history', 'historyIndex'],
|
18
|
-
get({ history, historyIndex }) {
|
19
|
-
return history[historyIndex];
|
20
|
-
},
|
21
|
-
},
|
22
|
-
undoItem: {
|
23
|
-
type: 'array',
|
24
|
-
item: 'object',
|
25
|
-
deps: ['history', 'historyIndex'],
|
26
|
-
get({ history, historyIndex }) {
|
27
|
-
return history[historyIndex - 1];
|
28
|
-
},
|
29
|
-
},
|
30
|
-
canRedo: {
|
31
|
-
type: 'boolean',
|
32
|
-
deps: ['redoItem'],
|
33
|
-
get({ redoItem }) {
|
34
|
-
return redoItem != null;
|
35
|
-
},
|
36
|
-
},
|
37
|
-
canUndo: {
|
38
|
-
type: 'boolean',
|
39
|
-
deps: ['undoItem'],
|
40
|
-
get({ undoItem }) {
|
41
|
-
return undoItem != null;
|
42
|
-
},
|
43
|
-
},
|
44
|
-
},
|
45
|
-
drafts: [],
|
46
|
-
});
|
package/comparators.d.ts
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
export declare const compareNil: (a: any, b: any) => number | null;
|
2
|
-
export declare const defaultComparators: {
|
3
|
-
string: {
|
4
|
-
asc: (a: string | null | undefined, b: string | null | undefined) => number;
|
5
|
-
desc: (a: string | null | undefined, b: string | null | undefined) => number;
|
6
|
-
};
|
7
|
-
number: {
|
8
|
-
asc: (a: number | null | undefined, b: number | null | undefined) => number;
|
9
|
-
desc: (a: number | null | undefined, b: number | null | undefined) => number;
|
10
|
-
};
|
11
|
-
boolean: {
|
12
|
-
asc: (a: boolean | null | undefined, b: boolean | null | undefined) => number;
|
13
|
-
desc: (a: boolean | null | undefined, b: boolean | null | undefined) => number;
|
14
|
-
};
|
15
|
-
};
|
package/comparators.js
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
export const compareNil = (a, b) => {
|
2
|
-
if (a === b)
|
3
|
-
return 0;
|
4
|
-
if (a === undefined)
|
5
|
-
return 1;
|
6
|
-
if (b === undefined)
|
7
|
-
return -1;
|
8
|
-
if (a === null)
|
9
|
-
return 1;
|
10
|
-
if (b === null)
|
11
|
-
return -1;
|
12
|
-
return null;
|
13
|
-
};
|
14
|
-
export const defaultComparators = {
|
15
|
-
string: {
|
16
|
-
// a -> z
|
17
|
-
asc: (a, b) => {
|
18
|
-
if (a === b)
|
19
|
-
return 0;
|
20
|
-
if (a === undefined)
|
21
|
-
return 1;
|
22
|
-
if (b === undefined)
|
23
|
-
return -1;
|
24
|
-
if (a === null)
|
25
|
-
return 1;
|
26
|
-
if (b === null)
|
27
|
-
return -1;
|
28
|
-
return a.localeCompare(b);
|
29
|
-
},
|
30
|
-
// z -> a
|
31
|
-
desc: (a, b) => {
|
32
|
-
if (a === b)
|
33
|
-
return 0;
|
34
|
-
if (a === undefined)
|
35
|
-
return 1;
|
36
|
-
if (b === undefined)
|
37
|
-
return -1;
|
38
|
-
if (a === null)
|
39
|
-
return 1;
|
40
|
-
if (b === null)
|
41
|
-
return -1;
|
42
|
-
return b.localeCompare(a);
|
43
|
-
},
|
44
|
-
},
|
45
|
-
number: {
|
46
|
-
// 0 -> 9
|
47
|
-
asc: (a, b) => {
|
48
|
-
if (a === b)
|
49
|
-
return 0;
|
50
|
-
if (a === undefined)
|
51
|
-
return 1;
|
52
|
-
if (b === undefined)
|
53
|
-
return -1;
|
54
|
-
if (a === null)
|
55
|
-
return 1;
|
56
|
-
if (b === null)
|
57
|
-
return -1;
|
58
|
-
return a - b;
|
59
|
-
},
|
60
|
-
// 9 -> 0
|
61
|
-
desc: (a, b) => {
|
62
|
-
if (a === b)
|
63
|
-
return 0;
|
64
|
-
if (a === undefined)
|
65
|
-
return 1;
|
66
|
-
if (b === undefined)
|
67
|
-
return -1;
|
68
|
-
if (a === null)
|
69
|
-
return 1;
|
70
|
-
if (b === null)
|
71
|
-
return -1;
|
72
|
-
return b - a;
|
73
|
-
},
|
74
|
-
},
|
75
|
-
boolean: {
|
76
|
-
// false -> true
|
77
|
-
asc: (a, b) => {
|
78
|
-
if (a === b)
|
79
|
-
return 0;
|
80
|
-
if (a === undefined)
|
81
|
-
return 1;
|
82
|
-
if (b === undefined)
|
83
|
-
return -1;
|
84
|
-
if (a === null)
|
85
|
-
return 1;
|
86
|
-
if (b === null)
|
87
|
-
return -1;
|
88
|
-
return (a ? 1 : 0) - (b ? 1 : 0);
|
89
|
-
},
|
90
|
-
// true -> false
|
91
|
-
desc: (a, b) => {
|
92
|
-
if (a === b)
|
93
|
-
return 0;
|
94
|
-
if (a === undefined)
|
95
|
-
return 1;
|
96
|
-
if (b === undefined)
|
97
|
-
return -1;
|
98
|
-
if (a === null)
|
99
|
-
return 1;
|
100
|
-
if (b === null)
|
101
|
-
return -1;
|
102
|
-
return (b ? 1 : 0) - (a ? 1 : 0);
|
103
|
-
},
|
104
|
-
},
|
105
|
-
};
|
package/hooks.d.ts
DELETED
@@ -1,5 +0,0 @@
|
|
1
|
-
import type { LocalDB } from './LocalDB.js';
|
2
|
-
import type { IDocument } from './types.js';
|
3
|
-
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
4
|
-
export declare const useLocalDbDoc: <T extends Record<string, IDocument>, K extends KeyOf<T>>(db: LocalDB<T>, col: K, id: string, deps?: any[]) => T[K] | undefined;
|
5
|
-
export declare const useLocalDbDocs: <T extends Record<string, IDocument>, K extends KeyOf<T>>(db: LocalDB<T>, col: K, ids: string[] | null | undefined, deps?: any[]) => (T[K] | undefined)[];
|
package/hooks.js
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
import { useEffect, useRef, useState } from 'react';
|
2
|
-
import { arraysEqual } from 'util-3gcvv/array.js';
|
3
|
-
export const useLocalDbDoc = (db, col, id, deps = [db, col, id]) => {
|
4
|
-
const [doc, setDoc] = useState(() => db.doc(col, id));
|
5
|
-
useEffect(() => {
|
6
|
-
setDoc(db.doc(col, id));
|
7
|
-
return db.subToDoc(col, id, setDoc);
|
8
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
9
|
-
}, deps);
|
10
|
-
return doc;
|
11
|
-
};
|
12
|
-
export const useLocalDbDocs = (db, col, ids, deps = [db, col, ids]) => {
|
13
|
-
const [docs, setDocs] = useState(() => ids?.length ? db.docs(col, ids) : []);
|
14
|
-
const docsRef = useRef(docs);
|
15
|
-
useEffect(() => {
|
16
|
-
const newDocs = ids?.length ? db.docs(col, ids) : [];
|
17
|
-
if (!arraysEqual(docsRef.current, newDocs)) {
|
18
|
-
docsRef.current = newDocs;
|
19
|
-
setDocs(newDocs);
|
20
|
-
}
|
21
|
-
if (!ids?.length)
|
22
|
-
return;
|
23
|
-
return db.subToCol(col, (next, prev, change) => {
|
24
|
-
for (let i = 0, il = ids.length; i < il; i += 1) {
|
25
|
-
if (ids[i] in change) {
|
26
|
-
docsRef.current = ids.map((id) => next[id]);
|
27
|
-
return setDocs(docsRef.current);
|
28
|
-
}
|
29
|
-
}
|
30
|
-
});
|
31
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
32
|
-
}, deps);
|
33
|
-
return docs;
|
34
|
-
};
|
package/types.d.ts
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
import type { LocalDB } from './LocalDB.js';
|
2
|
-
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
3
|
-
export interface IDocument {
|
4
|
-
id: string;
|
5
|
-
}
|
6
|
-
export type Collections<CollectionTypes extends Record<string, IDocument>> = {
|
7
|
-
[ColName in KeyOf<CollectionTypes>]: {
|
8
|
-
[id in string]: CollectionTypes[ColName];
|
9
|
-
};
|
10
|
-
};
|
11
|
-
export type CollectionsChange<CollectionTypes extends Record<string, IDocument>> = {
|
12
|
-
[ColName in KeyOf<CollectionTypes>]?: {
|
13
|
-
[id in string]: Partial<CollectionTypes[ColName]> | null;
|
14
|
-
};
|
15
|
-
};
|
16
|
-
export type CollectionsChangeMap<CollectionTypes extends Record<string, IDocument>> = {
|
17
|
-
[ColName in KeyOf<CollectionTypes>]?: {
|
18
|
-
[Field in KeyOf<CollectionTypes[ColName]>]: true;
|
19
|
-
};
|
20
|
-
};
|
21
|
-
export type Collection<CollectionTypes extends Record<string, IDocument>, ColName extends KeyOf<CollectionTypes>> = Record<string, CollectionTypes[ColName]>;
|
22
|
-
export type ICollectionFieldsConfig<Doc extends IDocument> = {
|
23
|
-
[P in KeyOf<Doc>]: {
|
24
|
-
type: 'boolean' | 'number' | 'string' | 'array' | 'object' | 'boolean[]' | 'number[]' | 'string[]' | 'array[]' | 'object[]';
|
25
|
-
nullable?: boolean;
|
26
|
-
normalize?(next: Doc[P], doc: Doc): Doc[P];
|
27
|
-
equals?(a: Doc[P], b: Doc[P]): boolean;
|
28
|
-
compare?(a: Doc[P] | null | undefined, b: Doc[P] | null | undefined): number;
|
29
|
-
index?: 'asc' | 'desc';
|
30
|
-
};
|
31
|
-
};
|
32
|
-
export interface IDocumentIndexConfig<Doc extends IDocument> {
|
33
|
-
fields: Array<KeyOf<Doc>>;
|
34
|
-
compare: (a: Doc, b: Doc) => number;
|
35
|
-
}
|
36
|
-
export interface ICollectionConfig<CollectionTypes extends Record<string, IDocument>, Doc extends IDocument> {
|
37
|
-
localStorageKey?: string;
|
38
|
-
localStorageSetWait?: number;
|
39
|
-
remoteStorageKey?: string;
|
40
|
-
remoteStorageSetWait?: number;
|
41
|
-
indexes?: Record<string, IDocumentIndexConfig<Doc>>;
|
42
|
-
idFields?: Array<KeyOf<Doc>>;
|
43
|
-
fields: ICollectionFieldsConfig<Doc>;
|
44
|
-
computes?: Array<{
|
45
|
-
deps: Array<KeyOf<Doc>>;
|
46
|
-
mutates: Array<KeyOf<Doc>>;
|
47
|
-
compute: (next: Doc, prev: Doc) => Partial<Doc> | null;
|
48
|
-
}>;
|
49
|
-
foreignComputes?: Array<{
|
50
|
-
mutates: Array<KeyOf<CollectionTypes>>;
|
51
|
-
compute: (db: LocalDB<CollectionTypes>, updates: Array<{
|
52
|
-
next: Doc | null;
|
53
|
-
prev: Doc | null;
|
54
|
-
}>) => void;
|
55
|
-
}>;
|
56
|
-
}
|
57
|
-
export type ICollectionsConfig<CollectionTypes extends Record<string, IDocument>> = {
|
58
|
-
[ColName in KeyOf<CollectionTypes>]: ICollectionConfig<CollectionTypes, CollectionTypes[ColName]>;
|
59
|
-
};
|
60
|
-
export type ICollectionsFields<CollectionTypes extends Record<string, IDocument>> = {
|
61
|
-
[ColName in KeyOf<CollectionTypes>]: Array<KeyOf<CollectionTypes[ColName]>>;
|
62
|
-
};
|
63
|
-
export type FieldListener<CollectionTypes extends Record<string, IDocument>, ColName extends KeyOf<CollectionTypes>, FieldName extends KeyOf<CollectionTypes[ColName]>> = (nextField: CollectionTypes[ColName][FieldName], prevField: CollectionTypes[ColName][FieldName], nextDoc: CollectionTypes[ColName], prevDoc: CollectionTypes[ColName], context: IDBTxContext) => void;
|
64
|
-
export type DocListener<CollectionTypes extends Record<string, IDocument>, ColName extends KeyOf<CollectionTypes>> = (nextDoc: CollectionTypes[ColName], prevDoc: CollectionTypes[ColName], change: Partial<CollectionTypes[ColName]> | null, context: IDBTxContext) => void;
|
65
|
-
export type ColListener<CollectionTypes extends Record<string, IDocument>, ColName extends KeyOf<CollectionTypes>> = (nextCol: Record<string, CollectionTypes[ColName]>, prevCol: Record<string, CollectionTypes[ColName]>, change: CollectionsChange<CollectionTypes>[ColName], changedFields: CollectionsChangeMap<CollectionTypes>[ColName], context: IDBTxContext) => void;
|
66
|
-
export type DBListener<CollectionTypes extends Record<string, IDocument>> = (nextDb: Collections<CollectionTypes>, prevDb: Collections<CollectionTypes>, change: CollectionsChange<CollectionTypes>, changedFields: CollectionsChangeMap<CollectionTypes>, context: IDBTxContext) => void;
|
67
|
-
export interface IDBOperations<CollectionTypes extends Record<string, IDocument> = any> {
|
68
|
-
setDoc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, doc: CollectionTypes[ColName]): void;
|
69
|
-
setDocs<ColName extends KeyOf<CollectionTypes>>(colName: ColName, docs: Array<CollectionTypes[ColName]>): void;
|
70
|
-
updateDoc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, id: string, updater: Partial<CollectionTypes[ColName]> | ((prev: CollectionTypes[ColName]) => Partial<CollectionTypes[ColName]> | null | undefined)): void;
|
71
|
-
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)): void;
|
72
|
-
deleteDoc<ColName extends KeyOf<CollectionTypes>>(colName: ColName, id: string): void;
|
73
|
-
deleteDocs<ColName extends KeyOf<CollectionTypes>>(colName: ColName, ids: string[]): void;
|
74
|
-
}
|
75
|
-
export type DBOpType = KeyOf<IDBOperations>;
|
76
|
-
export type IDBOperation = {
|
77
|
-
[Op in KeyOf<IDBOperations>]: {
|
78
|
-
op: Op;
|
79
|
-
args: Parameters<IDBOperations[Op]>;
|
80
|
-
};
|
81
|
-
}[KeyOf<IDBOperations>];
|
82
|
-
export interface IDBOperationHistoryItem {
|
83
|
-
undo: IDBOperation;
|
84
|
-
redo: IDBOperation;
|
85
|
-
}
|
86
|
-
export interface IDBTxContext {
|
87
|
-
[key: string]: any;
|
88
|
-
}
|
89
|
-
export interface IDBTxOptions {
|
90
|
-
undoable?: boolean;
|
91
|
-
noEvent?: boolean;
|
92
|
-
ignoreNotFound?: boolean;
|
93
|
-
idempotent?: boolean;
|
94
|
-
context?: IDBTxContext;
|
95
|
-
}
|
96
|
-
export interface IDBQuery<CollectionTypes extends Record<string, IDocument>, ColName extends KeyOf<CollectionTypes>> {
|
97
|
-
collection: ColName;
|
98
|
-
orderBy: string;
|
99
|
-
limit: number;
|
100
|
-
endAfter?: any;
|
101
|
-
endAt?: any;
|
102
|
-
startAfter?: any;
|
103
|
-
startAt?: any;
|
104
|
-
}
|
package/types.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|