@medyll/idae-idbql 0.64.0 → 0.66.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.
@@ -1,5 +1,5 @@
1
1
  import { CollectionCore } from "../collection/collection.svelte.js";
2
- import { type StateCollectionDyn } from "../state/idbstate.svelte.js";
2
+ import { type CollectionState } from "../state/idbstate.svelte.js";
3
3
  export declare enum enumPrimitive {
4
4
  id = "id",
5
5
  any = "any",
@@ -70,7 +70,7 @@ type ReadonlyCollections<T extends IdbqModel> = {
70
70
  [K in keyof T]: CollectionCore<T[K]["ts"]>;
71
71
  };
72
72
  type StateCollections<T extends IdbqModel> = {
73
- [K in keyof T]: StateCollectionDyn<T[K]["ts"]>;
73
+ [K in keyof T]: CollectionState<T[K]["ts"]>;
74
74
  };
75
75
  /**
76
76
  * Represents the IndexedDB wrapper for managing database operations.
@@ -112,6 +112,7 @@ export declare const createIdbqDb: <T extends IdbqModel>(model: T, version: numb
112
112
  idbDatabase: IdbqlIndexedCore<T>;
113
113
  idbql: ReadonlyCollections<T>;
114
114
  idbqlState: StateCollections<T>;
115
+ qolie: any;
115
116
  idbqModel: T;
116
117
  };
117
118
  };
@@ -120,6 +121,7 @@ export declare const idbqBase: <T extends IdbqModel>(model: T, version: number)
120
121
  idbDatabase: IdbqlIndexedCore<T>;
121
122
  idbql: ReadonlyCollections<T>;
122
123
  idbqlState: StateCollections<T>;
124
+ qolie: any;
123
125
  idbqModel: T;
124
126
  };
125
127
  };
@@ -143,10 +143,12 @@ export const createIdbqDb = (model, version) => {
143
143
  return {
144
144
  create: (name) => {
145
145
  const idb_ = new IdbqlIndexedCore(name, model, version);
146
+ const idbqlState = createIdbqlState(idb_);
146
147
  return {
147
148
  idbDatabase: idb_,
148
149
  idbql: idb_,
149
- idbqlState: createIdbqlState(idb_).state,
150
+ idbqlState: idbqlState.collectionState,
151
+ qolie: idbqlState.qolie,
150
152
  idbqModel: model,
151
153
  };
152
154
  },
@@ -77,6 +77,7 @@ class IdbqlStateEvent {
77
77
  }
78
78
  });
79
79
  for (let i = indicesToRemove.length - 1; i >= 0; i--) {
80
+ this.dataState[collection][indicesToRemove[i]] = undefined;
80
81
  this.dataState[collection].splice(indicesToRemove[i], 1);
81
82
  }
82
83
  }
@@ -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 state: Record<string, any>;
15
- onCollection: <T>(collectionName: string) => StateCollectionDyn<T>;
16
- addCollection: <T>(collectionName: string) => StateCollectionDyn<T>;
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
- * Adds a collection to the svelte 5 state and synchronize with indexedDB if it exists.
20
- * @template T - The type of data in the collection.
21
- * @param {string} collectionName - The name of the collection.
22
- * @param {string} [keyPath="id"] - The key path for the collection.
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 StateCollectionDyn<T> {
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
- getOne(value: any, pathKey?: string): T;
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
- /** @deprecated */
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
- * Adds a collection to the svelte 5 state and synchronize with indexedDB if it exists.
18
- * @template T - The type of data in the collection.
19
- * @param {string} collectionName - The name of the collection.
20
- * @param {string} [keyPath="id"] - The key path for the collection.
21
- * @returns {Proxy} - A proxy object with methods to interact with the collection.
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 StateCollectionDyn(collectionName, idbBase);
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 state() {
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
- * Adds a collection to the svelte 5 state and synchronize with indexedDB if it exists.
42
- * @template T - The type of data in the collection.
43
- * @param {string} collectionName - The name of the collection.
44
- * @param {string} [keyPath="id"] - The key path for the collection.
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 StateCollectionDyn {
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 OP */
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.filter((d) => d[pathKey] === value);
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.collectionState.filter((d) => d?.[pathKey] === value)?.[0];
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 OP */
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
- /** @deprecated */
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.64.0",
4
+ "version": "0.66.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.17.3",
36
- "@sveltejs/package": "^2.3.10",
37
- "@sveltejs/vite-plugin-svelte": "^5.0.3",
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.65.0"
47
+ "@medyll/idae-query": "^0.67.0"
49
48
  }
50
49
  }