@medyll/idae-idbql 0.65.0 → 0.67.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/idbqlCore/idbqlCore.d.ts +11 -74
- package/dist/idbqlCore/idbqlCore.js +20 -36
- package/dist/idbqlCore/idbqlSchema.js +12 -1
- package/dist/idbqlCore/types.d.ts +69 -0
- package/dist/idbqlCore/types.js +23 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/state/idbqlEvent.svelte.js +1 -0
- package/dist/state/idbstate.svelte.d.ts +110 -11
- package/dist/state/idbstate.svelte.js +121 -25
- package/package.json +5 -6
|
@@ -1,76 +1,11 @@
|
|
|
1
|
-
import { CollectionCore } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
date = "date",
|
|
7
|
-
text = "text",
|
|
8
|
-
number = "number",// IdbqModelCollectionTemplate
|
|
9
|
-
boolean = "boolean",
|
|
10
|
-
datetime = "datetime",
|
|
11
|
-
url = "url",
|
|
12
|
-
email = "email",
|
|
13
|
-
phone = "phone",
|
|
14
|
-
time = "time",
|
|
15
|
-
password = "password"
|
|
16
|
-
}
|
|
17
|
-
export declare enum TplProperties {
|
|
18
|
-
private = "private",
|
|
19
|
-
readonly = "readonly",
|
|
20
|
-
required = "required"
|
|
21
|
-
}
|
|
22
|
-
type CombineElements<T extends string, U extends string = T> = T extends any ? T | `${T} ${CombineElements<Exclude<U, T>>}` : never;
|
|
23
|
-
type CombinedArgs = CombineElements<TplProperties>;
|
|
24
|
-
type IdbObjectify<T extends string> = `array-of-${T}` | `object-${T}`;
|
|
25
|
-
export type TplCollectionFields = Record<string, string>;
|
|
26
|
-
export type TplFieldPrimitive<T = {}> = keyof typeof enumPrimitive | `text-${"tiny" | "short" | "medium" | "long" | "area"}` | `${string}.${string}` | `fk-${string}.${string}`;
|
|
27
|
-
export type TplObjectFieldPrimitive = IdbObjectify<TplFieldPrimitive>;
|
|
28
|
-
export type TplFieldFk = `fk-${string}.${string}`;
|
|
29
|
-
export type TplFkObject = IdbObjectify<TplFieldFk>;
|
|
30
|
-
export type TplTypes = TplFieldPrimitive | TplObjectFieldPrimitive | TplFieldFk | TplFkObject;
|
|
31
|
-
export type TplFieldArgs = `${TplTypes} (${CombinedArgs})`;
|
|
32
|
-
/** rules */
|
|
33
|
-
export type TplFieldRules = TplFieldArgs | TplTypes;
|
|
34
|
-
export type TplFieldType = TplFieldArgs | TplTypes;
|
|
35
|
-
export type IDbForge = {
|
|
36
|
-
collection?: TplCollectionName;
|
|
37
|
-
fieldName?: keyof TplFields;
|
|
38
|
-
fieldType?: TplFieldType;
|
|
39
|
-
fieldRule?: TplFieldRules;
|
|
40
|
-
fieldArgs?: [keyof typeof TplProperties] | undefined;
|
|
41
|
-
is: any;
|
|
42
|
-
};
|
|
43
|
-
export type IdbqModel<T = Record<string, Record<string, any>>> = {
|
|
44
|
-
readonly [K in keyof T]: CollectionModel<T[K]>;
|
|
45
|
-
};
|
|
46
|
-
export type TplCollectionName<T = TplCollectionFields> = keyof IdbqModel<T>;
|
|
47
|
-
export type Tpl<T = TplCollectionFields> = CollectionModel<T>["template"];
|
|
48
|
-
export type TplFields<T = TplCollectionFields> = CollectionModel<T>["template"]["fields"];
|
|
49
|
-
export type CollectionModel<T = TplCollectionFields> = {
|
|
50
|
-
keyPath: string | any;
|
|
51
|
-
/** @deprecated use ts instead */
|
|
52
|
-
model: any;
|
|
53
|
-
ts: any;
|
|
54
|
-
template: {
|
|
55
|
-
index: string;
|
|
56
|
-
presentation: CombineElements<keyof CollectionModel<T>["ts"]>;
|
|
57
|
-
fields: {
|
|
58
|
-
[K in keyof T]: TplFieldRules;
|
|
59
|
-
};
|
|
60
|
-
fks: {
|
|
61
|
-
[K in TplCollectionName]?: {
|
|
62
|
-
code: K;
|
|
63
|
-
multiple: boolean;
|
|
64
|
-
rules: CombinedArgs;
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
type ReadonlyCollections<T extends IdbqModel> = {
|
|
70
|
-
[K in keyof T]: CollectionCore<T[K]["ts"]>;
|
|
1
|
+
import { CollectionCore } from '../collection/collection.svelte.js';
|
|
2
|
+
import { CollectionState } from '../state/idbstate.svelte.js';
|
|
3
|
+
import type { IdbqModel } from './types.js';
|
|
4
|
+
export type ReadonlyCollections<T extends IdbqModel> = {
|
|
5
|
+
[K in keyof T]: CollectionCore<T[K]['ts']>;
|
|
71
6
|
};
|
|
72
|
-
type StateCollections<T extends IdbqModel> = {
|
|
73
|
-
[K in keyof T]:
|
|
7
|
+
export type StateCollections<T extends IdbqModel> = {
|
|
8
|
+
[K in keyof T]: CollectionState<T[K]['ts']>;
|
|
74
9
|
};
|
|
75
10
|
/**
|
|
76
11
|
* Represents the IndexedDB wrapper for managing database operations.
|
|
@@ -89,7 +24,7 @@ export declare class IdbqlIndexedCore<T = any> {
|
|
|
89
24
|
get schema(): Record<string, any>;
|
|
90
25
|
get idbqModel(): IdbqModel<Record<string, Record<string, any>>>;
|
|
91
26
|
/**
|
|
92
|
-
*
|
|
27
|
+
* return a database, with the version is provided
|
|
93
28
|
* @param {number} version - The version number of the database.
|
|
94
29
|
* @returns {object} - An object with a `stores` method to define the object stores.
|
|
95
30
|
*/
|
|
@@ -98,6 +33,7 @@ export declare class IdbqlIndexedCore<T = any> {
|
|
|
98
33
|
private createCollections;
|
|
99
34
|
}
|
|
100
35
|
/**
|
|
36
|
+
* Main entrance function to create an IndexedDB database store schema.
|
|
101
37
|
* Creates an IndexedDB database store schema for the given model and version.
|
|
102
38
|
*
|
|
103
39
|
* @param model - The IdbqModel representing the structure of the store.
|
|
@@ -112,6 +48,7 @@ export declare const createIdbqDb: <T extends IdbqModel>(model: T, version: numb
|
|
|
112
48
|
idbDatabase: IdbqlIndexedCore<T>;
|
|
113
49
|
idbql: ReadonlyCollections<T>;
|
|
114
50
|
idbqlState: StateCollections<T>;
|
|
51
|
+
qolie: any;
|
|
115
52
|
idbqModel: T;
|
|
116
53
|
};
|
|
117
54
|
};
|
|
@@ -120,7 +57,7 @@ export declare const idbqBase: <T extends IdbqModel>(model: T, version: number)
|
|
|
120
57
|
idbDatabase: IdbqlIndexedCore<T>;
|
|
121
58
|
idbql: ReadonlyCollections<T>;
|
|
122
59
|
idbqlState: StateCollections<T>;
|
|
60
|
+
qolie: any;
|
|
123
61
|
idbqModel: T;
|
|
124
62
|
};
|
|
125
63
|
};
|
|
126
|
-
export {};
|
|
@@ -1,29 +1,7 @@
|
|
|
1
1
|
/* src\lib\scripts\idbqlCore\idbqlCore.ts */
|
|
2
|
-
import { Collection, CollectionCore } from
|
|
3
|
-
import {
|
|
4
|
-
import { Schema } from
|
|
5
|
-
export var enumPrimitive;
|
|
6
|
-
(function (enumPrimitive) {
|
|
7
|
-
enumPrimitive["id"] = "id";
|
|
8
|
-
enumPrimitive["any"] = "any";
|
|
9
|
-
enumPrimitive["date"] = "date";
|
|
10
|
-
enumPrimitive["text"] = "text";
|
|
11
|
-
enumPrimitive["number"] = "number";
|
|
12
|
-
enumPrimitive["boolean"] = "boolean";
|
|
13
|
-
enumPrimitive["datetime"] = "datetime";
|
|
14
|
-
enumPrimitive["url"] = "url";
|
|
15
|
-
enumPrimitive["email"] = "email";
|
|
16
|
-
enumPrimitive["phone"] = "phone";
|
|
17
|
-
enumPrimitive["time"] = "time";
|
|
18
|
-
enumPrimitive["password"] = "password";
|
|
19
|
-
})(enumPrimitive || (enumPrimitive = {}));
|
|
20
|
-
export var TplProperties;
|
|
21
|
-
(function (TplProperties) {
|
|
22
|
-
TplProperties["private"] = "private";
|
|
23
|
-
TplProperties["readonly"] = "readonly";
|
|
24
|
-
TplProperties["required"] = "required";
|
|
25
|
-
})(TplProperties || (TplProperties = {}));
|
|
26
|
-
const a = "object-any (readonly private)";
|
|
2
|
+
import { Collection, CollectionCore } from '../collection/collection.svelte.js';
|
|
3
|
+
import { CollectionState, createIdbqlState } from '../state/idbstate.svelte.js';
|
|
4
|
+
import { Schema } from './idbqlSchema.js';
|
|
27
5
|
/**
|
|
28
6
|
* Represents the IndexedDB wrapper for managing database operations.
|
|
29
7
|
* @template T - The type of data stored in the IndexedDB.
|
|
@@ -45,13 +23,13 @@ export class IdbqlIndexedCore {
|
|
|
45
23
|
const stores = {};
|
|
46
24
|
Object.keys(idbqModel).forEach((modelName) => {
|
|
47
25
|
const modelInfo = idbqModel[modelName];
|
|
48
|
-
stores[modelName] = modelInfo.keyPath ||
|
|
26
|
+
stores[modelName] = modelInfo.keyPath || '';
|
|
49
27
|
Object.defineProperty(this, modelName, {
|
|
50
28
|
// @ts-ignore
|
|
51
29
|
value: undefined,
|
|
52
30
|
writable: true,
|
|
53
31
|
enumerable: true,
|
|
54
|
-
configurable: true
|
|
32
|
+
configurable: true
|
|
55
33
|
});
|
|
56
34
|
});
|
|
57
35
|
this.stores(stores);
|
|
@@ -63,12 +41,12 @@ export class IdbqlIndexedCore {
|
|
|
63
41
|
return this.#idbqModel;
|
|
64
42
|
}
|
|
65
43
|
/**
|
|
66
|
-
*
|
|
44
|
+
* return a database, with the version is provided
|
|
67
45
|
* @param {number} version - The version number of the database.
|
|
68
46
|
* @returns {object} - An object with a `stores` method to define the object stores.
|
|
69
47
|
*/
|
|
70
48
|
async stores(args) {
|
|
71
|
-
if (typeof indexedDB !==
|
|
49
|
+
if (typeof indexedDB !== 'undefined') {
|
|
72
50
|
return new Promise((resolve, reject) => {
|
|
73
51
|
this.#schema = args;
|
|
74
52
|
const dbConnection = indexedDB.open(this.databaseName, this.dbVersion);
|
|
@@ -80,11 +58,14 @@ export class IdbqlIndexedCore {
|
|
|
80
58
|
};
|
|
81
59
|
dbConnection.onupgradeneeded = async (event) => {
|
|
82
60
|
const db = event.target.result;
|
|
61
|
+
console.log('upgrading db', db);
|
|
83
62
|
if (db) {
|
|
84
63
|
const m = new Schema();
|
|
85
|
-
|
|
64
|
+
console.log('schema', m);
|
|
65
|
+
await m.createSchema(db, args);
|
|
86
66
|
}
|
|
87
67
|
else {
|
|
68
|
+
console.log('no db');
|
|
88
69
|
reject(true);
|
|
89
70
|
}
|
|
90
71
|
};
|
|
@@ -98,7 +79,7 @@ export class IdbqlIndexedCore {
|
|
|
98
79
|
}
|
|
99
80
|
async transaction(storeNames, mode, callback) {
|
|
100
81
|
if (!this.idbDatabase) {
|
|
101
|
-
throw new Error(
|
|
82
|
+
throw new Error('Database not initialized');
|
|
102
83
|
}
|
|
103
84
|
return new Promise((resolve, reject) => {
|
|
104
85
|
const tx = this.idbDatabase.transaction(storeNames, mode);
|
|
@@ -120,16 +101,17 @@ export class IdbqlIndexedCore {
|
|
|
120
101
|
// @ts-ignore
|
|
121
102
|
value: new Collection(storeName, this.#schema[storeName], {
|
|
122
103
|
dbName: this.databaseName,
|
|
123
|
-
version
|
|
104
|
+
version // @ts-ignore
|
|
124
105
|
}),
|
|
125
106
|
writable: true,
|
|
126
107
|
enumerable: true,
|
|
127
|
-
configurable: true
|
|
108
|
+
configurable: true
|
|
128
109
|
});
|
|
129
110
|
});
|
|
130
111
|
}
|
|
131
112
|
}
|
|
132
113
|
/**
|
|
114
|
+
* Main entrance function to create an IndexedDB database store schema.
|
|
133
115
|
* Creates an IndexedDB database store schema for the given model and version.
|
|
134
116
|
*
|
|
135
117
|
* @param model - The IdbqModel representing the structure of the store.
|
|
@@ -143,13 +125,15 @@ export const createIdbqDb = (model, version) => {
|
|
|
143
125
|
return {
|
|
144
126
|
create: (name) => {
|
|
145
127
|
const idb_ = new IdbqlIndexedCore(name, model, version);
|
|
128
|
+
const idbqlState = createIdbqlState(idb_);
|
|
146
129
|
return {
|
|
147
130
|
idbDatabase: idb_,
|
|
148
131
|
idbql: idb_,
|
|
149
|
-
idbqlState:
|
|
150
|
-
|
|
132
|
+
idbqlState: idbqlState.collectionState,
|
|
133
|
+
qolie: idbqlState.qolie,
|
|
134
|
+
idbqModel: model
|
|
151
135
|
};
|
|
152
|
-
}
|
|
136
|
+
}
|
|
153
137
|
};
|
|
154
138
|
};
|
|
155
139
|
// main export is here ?
|
|
@@ -15,6 +15,10 @@ export class Schema {
|
|
|
15
15
|
* @returns The created object store or null if creation fails.
|
|
16
16
|
*/
|
|
17
17
|
createStore(db, storeName, keyPath, autoIncrement = false) {
|
|
18
|
+
if (db.objectStoreNames.contains(storeName)) {
|
|
19
|
+
console.warn(`Store ${storeName} already exists, skipping creation.`);
|
|
20
|
+
return db.transaction(storeName, 'readwrite').objectStore(storeName);
|
|
21
|
+
}
|
|
18
22
|
try {
|
|
19
23
|
return db.createObjectStore(storeName, { keyPath, autoIncrement });
|
|
20
24
|
}
|
|
@@ -37,7 +41,14 @@ export class Schema {
|
|
|
37
41
|
const declaredIndex = fields.find((field) => field.startsWith('&'))?.replace('&', '');
|
|
38
42
|
const keyPath = incrementField || declaredIndex || fields[0];
|
|
39
43
|
const increment = Boolean(incrementField);
|
|
40
|
-
|
|
44
|
+
let store;
|
|
45
|
+
if (db.objectStoreNames.contains(storeName)) {
|
|
46
|
+
console.warn(`Store ${storeName} already exists, skipping creation.`);
|
|
47
|
+
store = db.transaction(storeName, 'readwrite').objectStore(storeName);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
store = this.createStore(db, storeName, keyPath, increment);
|
|
51
|
+
}
|
|
41
52
|
if (store) {
|
|
42
53
|
for (const field of fields) {
|
|
43
54
|
await this.createIndexes(store, field, field);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type ExpandProps<T> = T extends infer O ? {
|
|
2
|
+
[K in keyof O]: O[K];
|
|
3
|
+
} : never;
|
|
4
|
+
export declare enum enumPrimitive {
|
|
5
|
+
id = "id",
|
|
6
|
+
any = "any",
|
|
7
|
+
date = "date",
|
|
8
|
+
text = "text",
|
|
9
|
+
number = "number",// IdbqModelCollectionTemplate
|
|
10
|
+
boolean = "boolean",
|
|
11
|
+
datetime = "datetime",
|
|
12
|
+
url = "url",
|
|
13
|
+
email = "email",
|
|
14
|
+
phone = "phone",
|
|
15
|
+
time = "time",
|
|
16
|
+
password = "password"
|
|
17
|
+
}
|
|
18
|
+
export declare enum TplProperties {
|
|
19
|
+
private = "private",
|
|
20
|
+
readonly = "readonly",
|
|
21
|
+
required = "required"
|
|
22
|
+
}
|
|
23
|
+
export type CombineElements<T extends string, U extends string = T> = T extends any ? T | `${T} ${CombineElements<Exclude<U, T>>}` : never;
|
|
24
|
+
export type CombinedArgs = CombineElements<TplProperties>;
|
|
25
|
+
export type IdbObjectify<T extends string> = `array-of-${T}` | `object-${T}`;
|
|
26
|
+
export type TplCollectionFields = Record<string, string>;
|
|
27
|
+
export type TplFieldPrimitive<T = {}> = keyof typeof enumPrimitive | `text-${'tiny' | 'short' | 'medium' | 'long' | 'area'}` | `${string}.${string}` | `fk-${string}.${string}`;
|
|
28
|
+
export type TplObjectFieldPrimitive = IdbObjectify<TplFieldPrimitive>;
|
|
29
|
+
export type TplFieldFk = `fk-${string}.${string}`;
|
|
30
|
+
export type TplFkObject = IdbObjectify;
|
|
31
|
+
export type TplTypes = TplFieldPrimitive | TplObjectFieldPrimitive | TplFieldFk | TplFkObject;
|
|
32
|
+
export type TplFieldArgs = `${TplTypes} (${CombinedArgs})`;
|
|
33
|
+
/** rules */
|
|
34
|
+
export type TplFieldRules = TplFieldArgs | TplTypes;
|
|
35
|
+
export type TplFieldType = TplFieldArgs | TplTypes;
|
|
36
|
+
export type IDbForge = {
|
|
37
|
+
collection?: TplCollectionName;
|
|
38
|
+
fieldName?: keyof TplFields;
|
|
39
|
+
fieldType?: TplFieldType;
|
|
40
|
+
fieldRule?: TplFieldRules;
|
|
41
|
+
fieldArgs?: [keyof typeof TplProperties] | undefined;
|
|
42
|
+
is: any;
|
|
43
|
+
};
|
|
44
|
+
export type IdbqModel<T = Record<string, Record<string, any>>> = {
|
|
45
|
+
readonly [K in keyof T]: CollectionModel<T[K]>;
|
|
46
|
+
};
|
|
47
|
+
export type TplCollectionName<T = TplCollectionFields> = keyof IdbqModel<T>;
|
|
48
|
+
export type Tpl<T = TplCollectionFields> = CollectionModel<T>['template'];
|
|
49
|
+
export type TplFields<T = TplCollectionFields> = CollectionModel<T>['template']['fields'];
|
|
50
|
+
export type CollectionModel<T = TplCollectionFields> = {
|
|
51
|
+
keyPath: string | any;
|
|
52
|
+
/** @deprecated use ts instead */
|
|
53
|
+
model: any;
|
|
54
|
+
ts: any;
|
|
55
|
+
template: {
|
|
56
|
+
index: string;
|
|
57
|
+
presentation: CombineElements<keyof CollectionModel<T>['ts']>;
|
|
58
|
+
fields: {
|
|
59
|
+
[K in keyof T]: TplFieldRules;
|
|
60
|
+
};
|
|
61
|
+
fks: {
|
|
62
|
+
[K in TplCollectionName]?: {
|
|
63
|
+
code: K;
|
|
64
|
+
multiple: boolean;
|
|
65
|
+
rules: CombinedArgs;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export var enumPrimitive;
|
|
2
|
+
(function (enumPrimitive) {
|
|
3
|
+
enumPrimitive["id"] = "id";
|
|
4
|
+
enumPrimitive["any"] = "any";
|
|
5
|
+
enumPrimitive["date"] = "date";
|
|
6
|
+
enumPrimitive["text"] = "text";
|
|
7
|
+
enumPrimitive["number"] = "number";
|
|
8
|
+
enumPrimitive["boolean"] = "boolean";
|
|
9
|
+
enumPrimitive["datetime"] = "datetime";
|
|
10
|
+
enumPrimitive["url"] = "url";
|
|
11
|
+
enumPrimitive["email"] = "email";
|
|
12
|
+
enumPrimitive["phone"] = "phone";
|
|
13
|
+
enumPrimitive["time"] = "time";
|
|
14
|
+
enumPrimitive["password"] = "password";
|
|
15
|
+
})(enumPrimitive || (enumPrimitive = {}));
|
|
16
|
+
export var TplProperties;
|
|
17
|
+
(function (TplProperties) {
|
|
18
|
+
TplProperties["private"] = "private";
|
|
19
|
+
TplProperties["readonly"] = "readonly";
|
|
20
|
+
TplProperties["required"] = "required";
|
|
21
|
+
})(TplProperties || (TplProperties = {}));
|
|
22
|
+
(TplFieldFk);
|
|
23
|
+
const a = 'object-any (readonly private)';
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './state/idbstate.svelte.js';
|
|
|
2
2
|
export * from './state/idbqlEvent.svelte.js';
|
|
3
3
|
export * from './scripts/types.js';
|
|
4
4
|
export * from './path/pathResolver.js';
|
|
5
|
+
export * from './idbqlCore/types.js';
|
|
5
6
|
export * from './idbqlCore/idbqlSchema.js';
|
|
6
7
|
export * from './idbqlCore/idbqlCore.js';
|
|
7
8
|
export * from './collection/collection.svelte.js';
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from './state/idbstate.svelte.js';
|
|
|
3
3
|
export * from './state/idbqlEvent.svelte.js';
|
|
4
4
|
export * from './scripts/types.js';
|
|
5
5
|
export * from './path/pathResolver.js';
|
|
6
|
+
export * from './idbqlCore/types.js';
|
|
6
7
|
export * from './idbqlCore/idbqlSchema.js';
|
|
7
8
|
export * from './idbqlCore/idbqlCore.js';
|
|
8
9
|
export * from './collection/collection.svelte.js';
|
|
@@ -11,36 +11,135 @@ import { type Where, type ResultSet, type ResultsetOptions } from "@medyll/idae-
|
|
|
11
11
|
* @returns {object} - The state object.
|
|
12
12
|
*/
|
|
13
13
|
export declare const createIdbqlState: (idbBase: IdbqlIndexedCore) => {
|
|
14
|
-
readonly
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
readonly collectionState: Record<string, CollectionState<any>>;
|
|
15
|
+
readonly qolie: (collection: string) => {
|
|
16
|
+
get: (value: any, pathKey?: string) => ResultSet<any>;
|
|
17
|
+
getBy: (value: any, pathKey?: string) => ResultSet<any>;
|
|
18
|
+
getOne: (value: any, pathKey?: string) => any;
|
|
19
|
+
getAll: () => ResultSet<any>;
|
|
20
|
+
create: (value: Partial<any>) => Promise<any>;
|
|
21
|
+
delete: (keyPathValue: string | number) => Promise<boolean | undefined>;
|
|
22
|
+
deleteWhere: (where: Where<any>) => Promise<boolean | undefined>;
|
|
23
|
+
update: (keyPathValue: string | number, data: Partial<any>) => Promise<boolean | undefined>;
|
|
24
|
+
where: (qy: Where<any>, options?: ResultsetOptions) => ResultSet<any>;
|
|
25
|
+
updateWhere: (where: Where<any>, data: Partial<any>) => Promise<boolean | undefined>;
|
|
26
|
+
};
|
|
27
|
+
onCollection: <T>(collectionName: string) => CollectionState<T>;
|
|
28
|
+
addCollection: <T>(collectionName: string) => CollectionState<T>;
|
|
17
29
|
};
|
|
18
30
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @
|
|
23
|
-
* @returns {Proxy} - A proxy object with methods to interact with the collection.
|
|
31
|
+
* Represents a dynamic state collection that interacts with an IndexedDB base.
|
|
32
|
+
* Provides methods for performing CRUD operations and querying the collection.
|
|
33
|
+
*
|
|
34
|
+
* @template T - The type of items in the collection.
|
|
24
35
|
*/
|
|
25
|
-
export declare class
|
|
36
|
+
export declare class CollectionState<T> {
|
|
26
37
|
private collectionName;
|
|
27
38
|
private idbBase;
|
|
39
|
+
/**
|
|
40
|
+
* Creates an instance of StateCollectionDyn.
|
|
41
|
+
* @param {string} collectionName - The name of the collection.
|
|
42
|
+
* @param {IdbqlIndexedCore} idbBase - The instance of the IndexedDB base.
|
|
43
|
+
*/
|
|
28
44
|
constructor(collectionName: string, idbBase: IdbqlIndexedCore);
|
|
45
|
+
/**
|
|
46
|
+
* Gets the current state of the collection.
|
|
47
|
+
* @returns {ResultSet<T>} The state of the collection.
|
|
48
|
+
*/
|
|
29
49
|
get collectionState(): ResultSet<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Tests if the collection exists in the IndexedDB base.
|
|
52
|
+
* @param {string} collection - The name of the collection.
|
|
53
|
+
* @returns {boolean} True if the collection exists, otherwise false.
|
|
54
|
+
*/
|
|
30
55
|
private testIdbql;
|
|
56
|
+
/**
|
|
57
|
+
* Feeds the collection with data from the IndexedDB base.
|
|
58
|
+
*/
|
|
31
59
|
private feed;
|
|
60
|
+
/**
|
|
61
|
+
* Performs a "where" query on the collection.
|
|
62
|
+
* @param {Where<T>} qy - The "where" query.
|
|
63
|
+
* @param {ResultsetOptions} [options] - Optional result set options.
|
|
64
|
+
* @returns {ResultSet<T>} The result set of the query.
|
|
65
|
+
*/
|
|
32
66
|
_where(qy: Where<T>, options?: ResultsetOptions): ResultSet<T>;
|
|
67
|
+
/**
|
|
68
|
+
* Gets a function to perform a "where" query on the collection.
|
|
69
|
+
* @returns {(qy: Where<T>, options?: ResultsetOptions) => ResultSet<T>} The function to perform the query.
|
|
70
|
+
*/
|
|
33
71
|
get where(): (qy: Where<T>, options?: ResultsetOptions) => ResultSet<T>;
|
|
72
|
+
/**
|
|
73
|
+
* Gets items from the collection by a specific value and key.
|
|
74
|
+
* @param {any} value - The value to search for.
|
|
75
|
+
* @param {string} [pathKey="id"] - The key to search by.
|
|
76
|
+
* @returns {ResultSet<T>} The result set of the query.
|
|
77
|
+
*/
|
|
34
78
|
get(value: any, pathKey?: string): ResultSet<T>;
|
|
35
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Gets items from the collection by a specific value and key using a "where" query.
|
|
81
|
+
* @param {any} value - The value to search for.
|
|
82
|
+
* @param {string} [pathKey="id"] - The key to search by.
|
|
83
|
+
* @returns {ResultSet<T>} The result set of the query.
|
|
84
|
+
*/
|
|
85
|
+
getBy(value: any, pathKey?: string): ResultSet<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Gets a single item from the collection by a specific value and key.
|
|
88
|
+
* @deprecated Use getBy instead.
|
|
89
|
+
* @param {any} value - The value to search for.
|
|
90
|
+
* @param {string} [pathKey="id"] - The key to search by.
|
|
91
|
+
* @returns {T | undefined} The item found, or undefined if not found.
|
|
92
|
+
*/
|
|
93
|
+
getOne(value: any, pathKey?: string): T | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Gets all items from the collection.
|
|
96
|
+
* @returns {ResultSet<T>} The result set of all items.
|
|
97
|
+
*/
|
|
36
98
|
getAll: () => ResultSet<T>;
|
|
99
|
+
/**
|
|
100
|
+
* Updates an item in the collection by a specific key path value.
|
|
101
|
+
* @param {string | number} keyPathValue - The key path value of the item to update.
|
|
102
|
+
* @param {Partial<T>} data - The data to update.
|
|
103
|
+
* @returns {Promise<boolean | undefined>} True if the update was successful, otherwise undefined.
|
|
104
|
+
*/
|
|
37
105
|
update(keyPathValue: string | number, data: Partial<T>): Promise<boolean | undefined>;
|
|
106
|
+
/**
|
|
107
|
+
* Updates items in the collection that match a "where" query.
|
|
108
|
+
* @param {Where<T>} where - The "where" query to match items.
|
|
109
|
+
* @param {Partial<T>} data - The data to update.
|
|
110
|
+
* @returns {Promise<boolean | undefined>} True if the update was successful, otherwise undefined.
|
|
111
|
+
*/
|
|
38
112
|
updateWhere(where: Where<T>, data: Partial<T>): Promise<boolean | undefined>;
|
|
113
|
+
/**
|
|
114
|
+
* Puts an item into the collection.
|
|
115
|
+
* @param {Partial<T>} value - The item to put.
|
|
116
|
+
* @returns {Promise<T | undefined>} The item put, or undefined if the operation failed.
|
|
117
|
+
*/
|
|
39
118
|
put(value: Partial<T>): Promise<T | undefined>;
|
|
119
|
+
/**
|
|
120
|
+
* Adds an item to the collection.
|
|
121
|
+
* @param {T} data - The item to add.
|
|
122
|
+
* @returns {Promise<T | undefined>} The item added, or undefined if the operation failed.
|
|
123
|
+
*/
|
|
40
124
|
add(data: T): Promise<T | undefined>;
|
|
125
|
+
/**
|
|
126
|
+
* Deletes an item from the collection by a specific key path value.
|
|
127
|
+
* @param {string | number} keyPathValue - The key path value of the item to delete.
|
|
128
|
+
* @returns {Promise<boolean | undefined>} True if the deletion was successful, otherwise undefined.
|
|
129
|
+
*/
|
|
41
130
|
delete(keyPathValue: string | number): Promise<boolean | undefined>;
|
|
42
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Deletes an item from the collection by a specific key path value.
|
|
133
|
+
* @deprecated Use delete instead.
|
|
134
|
+
* @param {string | number} keyPathValue - The key path value of the item to delete.
|
|
135
|
+
* @returns {Promise<boolean | undefined>} True if the deletion was successful, otherwise undefined.
|
|
136
|
+
*/
|
|
43
137
|
del(keyPathValue: string | number): Promise<boolean | undefined>;
|
|
138
|
+
/**
|
|
139
|
+
* Deletes items from the collection that match a "where" query.
|
|
140
|
+
* @param {Where<T>} where - The "where" query to match items.
|
|
141
|
+
* @returns {Promise<boolean | undefined>} True if the deletion was successful, otherwise undefined.
|
|
142
|
+
*/
|
|
44
143
|
deleteWhere(where: Where<T>): Promise<boolean | undefined>;
|
|
45
144
|
}
|
|
46
145
|
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { idbqlEvent } from "./idbqlEvent.svelte.js";
|
|
2
2
|
//
|
|
3
3
|
import { Operators, getResultset, } from "@medyll/idae-query";
|
|
4
|
-
//
|
|
5
4
|
/**
|
|
6
5
|
* Main entry point.
|
|
7
6
|
* Creates a state object with indexedDB synchronization.
|
|
@@ -13,15 +12,24 @@ export const createIdbqlState = (idbBase) => {
|
|
|
13
12
|
if (idbBase.schema) {
|
|
14
13
|
addCollections(idbBase.schema);
|
|
15
14
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
let qolie = function dbQuery(collection) {
|
|
16
|
+
if (!collections[collection])
|
|
17
|
+
throw new Error(`Collection ${collection} not found`);
|
|
18
|
+
return {
|
|
19
|
+
get: collections[collection].get,
|
|
20
|
+
getBy: collections[collection].getBy,
|
|
21
|
+
getOne: collections[collection].getOne,
|
|
22
|
+
getAll: collections[collection].getAll,
|
|
23
|
+
create: collections[collection].put,
|
|
24
|
+
delete: collections[collection].delete,
|
|
25
|
+
deleteWhere: collections[collection].deleteWhere,
|
|
26
|
+
update: collections[collection].update,
|
|
27
|
+
where: collections[collection].where,
|
|
28
|
+
updateWhere: collections[collection].updateWhere,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
23
31
|
function addCollection(collectionName) {
|
|
24
|
-
return new
|
|
32
|
+
return new CollectionState(collectionName, idbBase);
|
|
25
33
|
}
|
|
26
34
|
function addCollections(args) {
|
|
27
35
|
Object.keys(args).map((collection) => {
|
|
@@ -30,23 +38,30 @@ export const createIdbqlState = (idbBase) => {
|
|
|
30
38
|
});
|
|
31
39
|
}
|
|
32
40
|
return {
|
|
33
|
-
get
|
|
41
|
+
get collectionState() {
|
|
34
42
|
return collections;
|
|
35
43
|
},
|
|
44
|
+
get qolie() {
|
|
45
|
+
return qolie;
|
|
46
|
+
},
|
|
36
47
|
onCollection: addCollection,
|
|
37
48
|
addCollection: addCollection,
|
|
38
49
|
};
|
|
39
50
|
};
|
|
40
51
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @
|
|
45
|
-
* @returns {Proxy} - A proxy object with methods to interact with the collection.
|
|
52
|
+
* Represents a dynamic state collection that interacts with an IndexedDB base.
|
|
53
|
+
* Provides methods for performing CRUD operations and querying the collection.
|
|
54
|
+
*
|
|
55
|
+
* @template T - The type of items in the collection.
|
|
46
56
|
*/
|
|
47
|
-
export class
|
|
57
|
+
export class CollectionState {
|
|
48
58
|
collectionName;
|
|
49
59
|
idbBase;
|
|
60
|
+
/**
|
|
61
|
+
* Creates an instance of StateCollectionDyn.
|
|
62
|
+
* @param {string} collectionName - The name of the collection.
|
|
63
|
+
* @param {IdbqlIndexedCore} idbBase - The instance of the IndexedDB base.
|
|
64
|
+
*/
|
|
50
65
|
constructor(collectionName, idbBase) {
|
|
51
66
|
if (!idbqlEvent.dataState?.[collectionName]) {
|
|
52
67
|
idbqlEvent.dataState[collectionName] = [];
|
|
@@ -56,24 +71,38 @@ export class StateCollectionDyn {
|
|
|
56
71
|
this.feed();
|
|
57
72
|
return this;
|
|
58
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Gets the current state of the collection.
|
|
76
|
+
* @returns {ResultSet<T>} The state of the collection.
|
|
77
|
+
*/
|
|
59
78
|
get collectionState() {
|
|
60
79
|
return idbqlEvent?.dataState?.[this.collectionName];
|
|
61
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Tests if the collection exists in the IndexedDB base.
|
|
83
|
+
* @param {string} collection - The name of the collection.
|
|
84
|
+
* @returns {boolean} True if the collection exists, otherwise false.
|
|
85
|
+
*/
|
|
62
86
|
testIdbql(collection) {
|
|
63
87
|
return this.idbBase && Boolean(this.idbBase?.[collection]);
|
|
64
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Feeds the collection with data from the IndexedDB base.
|
|
91
|
+
*/
|
|
65
92
|
feed() {
|
|
66
93
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
67
94
|
this.idbBase[this.collectionName].getAll().then((data) => {
|
|
68
95
|
idbqlEvent.dataState[this.collectionName] = getResultset(data);
|
|
69
|
-
/* idbqlEvent.dataState = {
|
|
70
|
-
...idbqlEvent.dataState,
|
|
71
|
-
[this.collectionName]: getResultset(data),
|
|
72
|
-
}; */
|
|
73
96
|
});
|
|
74
97
|
}
|
|
75
98
|
}
|
|
76
|
-
/* READ
|
|
99
|
+
/* READ OPERATIONS */
|
|
100
|
+
/**
|
|
101
|
+
* Performs a "where" query on the collection.
|
|
102
|
+
* @param {Where<T>} qy - The "where" query.
|
|
103
|
+
* @param {ResultsetOptions} [options] - Optional result set options.
|
|
104
|
+
* @returns {ResultSet<T>} The result set of the query.
|
|
105
|
+
*/
|
|
77
106
|
_where(qy, options) {
|
|
78
107
|
let dts = this.collectionState;
|
|
79
108
|
let c = Operators.parse(dts ?? getResultset([]), qy);
|
|
@@ -82,31 +111,78 @@ export class StateCollectionDyn {
|
|
|
82
111
|
r.setOptions(options);
|
|
83
112
|
return r;
|
|
84
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Gets a function to perform a "where" query on the collection.
|
|
116
|
+
* @returns {(qy: Where<T>, options?: ResultsetOptions) => ResultSet<T>} The function to perform the query.
|
|
117
|
+
*/
|
|
85
118
|
get where() {
|
|
86
119
|
return (qy, options) => this._where(qy, options);
|
|
87
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Gets items from the collection by a specific value and key.
|
|
123
|
+
* @param {any} value - The value to search for.
|
|
124
|
+
* @param {string} [pathKey="id"] - The key to search by.
|
|
125
|
+
* @returns {ResultSet<T>} The result set of the query.
|
|
126
|
+
*/
|
|
88
127
|
get(value, pathKey = "id") {
|
|
89
|
-
return this.collectionState
|
|
128
|
+
return this.collectionState?.filter((d) => d[pathKey] === value);
|
|
90
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Gets items from the collection by a specific value and key using a "where" query.
|
|
132
|
+
* @param {any} value - The value to search for.
|
|
133
|
+
* @param {string} [pathKey="id"] - The key to search by.
|
|
134
|
+
* @returns {ResultSet<T>} The result set of the query.
|
|
135
|
+
*/
|
|
136
|
+
getBy(value, pathKey = "id") {
|
|
137
|
+
return this.where({ [pathKey]: value });
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Gets a single item from the collection by a specific value and key.
|
|
141
|
+
* @deprecated Use getBy instead.
|
|
142
|
+
* @param {any} value - The value to search for.
|
|
143
|
+
* @param {string} [pathKey="id"] - The key to search by.
|
|
144
|
+
* @returns {T | undefined} The item found, or undefined if not found.
|
|
145
|
+
*/
|
|
91
146
|
getOne(value, pathKey = "id") {
|
|
92
|
-
return this.
|
|
147
|
+
return this.where({ [pathKey]: value })?.[0];
|
|
93
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Gets all items from the collection.
|
|
151
|
+
* @returns {ResultSet<T>} The result set of all items.
|
|
152
|
+
*/
|
|
94
153
|
getAll = () => {
|
|
95
154
|
return this.collectionState;
|
|
96
155
|
};
|
|
97
|
-
/* WRITE
|
|
156
|
+
/* WRITE OPERATIONS */
|
|
157
|
+
/**
|
|
158
|
+
* Updates an item in the collection by a specific key path value.
|
|
159
|
+
* @param {string | number} keyPathValue - The key path value of the item to update.
|
|
160
|
+
* @param {Partial<T>} data - The data to update.
|
|
161
|
+
* @returns {Promise<boolean | undefined>} True if the update was successful, otherwise undefined.
|
|
162
|
+
*/
|
|
98
163
|
async update(keyPathValue, data) {
|
|
99
164
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
100
165
|
return (await this.idbBase[this.collectionName].update(keyPathValue, data));
|
|
101
166
|
}
|
|
102
167
|
return undefined;
|
|
103
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Updates items in the collection that match a "where" query.
|
|
171
|
+
* @param {Where<T>} where - The "where" query to match items.
|
|
172
|
+
* @param {Partial<T>} data - The data to update.
|
|
173
|
+
* @returns {Promise<boolean | undefined>} True if the update was successful, otherwise undefined.
|
|
174
|
+
*/
|
|
104
175
|
async updateWhere(where, data) {
|
|
105
176
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
106
177
|
return (await this.idbBase[this.collectionName].updateWhere(where, data));
|
|
107
178
|
}
|
|
108
179
|
return undefined;
|
|
109
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Puts an item into the collection.
|
|
183
|
+
* @param {Partial<T>} value - The item to put.
|
|
184
|
+
* @returns {Promise<T | undefined>} The item put, or undefined if the operation failed.
|
|
185
|
+
*/
|
|
110
186
|
async put(value) {
|
|
111
187
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
112
188
|
return this.idbBase[this.collectionName].put(value).then((data) => {
|
|
@@ -115,24 +191,44 @@ export class StateCollectionDyn {
|
|
|
115
191
|
}
|
|
116
192
|
return undefined;
|
|
117
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Adds an item to the collection.
|
|
196
|
+
* @param {T} data - The item to add.
|
|
197
|
+
* @returns {Promise<T | undefined>} The item added, or undefined if the operation failed.
|
|
198
|
+
*/
|
|
118
199
|
async add(data) {
|
|
119
200
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
120
201
|
return (await this.idbBase[this.collectionName].add(data));
|
|
121
202
|
}
|
|
122
203
|
return undefined;
|
|
123
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Deletes an item from the collection by a specific key path value.
|
|
207
|
+
* @param {string | number} keyPathValue - The key path value of the item to delete.
|
|
208
|
+
* @returns {Promise<boolean | undefined>} True if the deletion was successful, otherwise undefined.
|
|
209
|
+
*/
|
|
124
210
|
async delete(keyPathValue) {
|
|
125
211
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
126
212
|
return await this.idbBase[this.collectionName].delete(keyPathValue);
|
|
127
213
|
}
|
|
128
214
|
return undefined;
|
|
129
215
|
}
|
|
130
|
-
/**
|
|
216
|
+
/**
|
|
217
|
+
* Deletes an item from the collection by a specific key path value.
|
|
218
|
+
* @deprecated Use delete instead.
|
|
219
|
+
* @param {string | number} keyPathValue - The key path value of the item to delete.
|
|
220
|
+
* @returns {Promise<boolean | undefined>} True if the deletion was successful, otherwise undefined.
|
|
221
|
+
*/
|
|
131
222
|
async del(keyPathValue) {
|
|
132
223
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
133
224
|
return await this.idbBase[this.collectionName].delete(keyPathValue);
|
|
134
225
|
}
|
|
135
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Deletes items from the collection that match a "where" query.
|
|
229
|
+
* @param {Where<T>} where - The "where" query to match items.
|
|
230
|
+
* @returns {Promise<boolean | undefined>} True if the deletion was successful, otherwise undefined.
|
|
231
|
+
*/
|
|
136
232
|
async deleteWhere(where) {
|
|
137
233
|
if (this.idbBase && this.testIdbql(this.collectionName)) {
|
|
138
234
|
return await this.idbBase[this.collectionName].deleteWhere(where);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medyll/idae-idbql",
|
|
3
3
|
"scope": "@medyll",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.67.0",
|
|
5
5
|
"description": "A powerful and flexible IndexedDB query library for TypeScript and JavaScript applications, offering a MongoDB-like query interface, strong TypeScript support, reactive state management, and easy integration with front-end frameworks like Svelte.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite dev",
|
|
@@ -32,10 +32,9 @@
|
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@medyll/idae-prettier-config": "^1.1.0",
|
|
34
34
|
"@sveltejs/adapter-auto": "^4.0.0",
|
|
35
|
-
"@sveltejs/kit": "^2.
|
|
36
|
-
"@sveltejs/package": "^2.
|
|
37
|
-
"
|
|
38
|
-
"svelte": "^5.20.5",
|
|
35
|
+
"@sveltejs/kit": "^2.18.0",
|
|
36
|
+
"@sveltejs/package": "^2.0.0",
|
|
37
|
+
"svelte": "^5.22.4",
|
|
39
38
|
"svelte-check": "^4.1.4",
|
|
40
39
|
"tslib": "^2.8.1",
|
|
41
40
|
"typescript": "^5.8.2",
|
|
@@ -45,6 +44,6 @@
|
|
|
45
44
|
"types": "./dist/index.d.ts",
|
|
46
45
|
"type": "module",
|
|
47
46
|
"dependencies": {
|
|
48
|
-
"@medyll/idae-query": "^0.
|
|
47
|
+
"@medyll/idae-query": "^0.68.0"
|
|
49
48
|
}
|
|
50
49
|
}
|