@medyll/idae-idbql 0.44.0 → 0.55.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.
@@ -0,0 +1,130 @@
1
+ import { type Where, type ResultsetOptions, type ResultSet } from "@medyll/idae-query";
2
+ export declare class CollectionCore<T = any> {
3
+ protected _store: string;
4
+ private version?;
5
+ private dbName;
6
+ private dBOpenRequest;
7
+ private keyPath;
8
+ constructor(store: string, keyPath: string, args: {
9
+ dbName: string;
10
+ version?: number;
11
+ });
12
+ get name(): string;
13
+ private getDatabase;
14
+ /** get the collection */
15
+ private getCollection;
16
+ /**
17
+ * Retrieves the data from the collection based on the provided query.
18
+ * @param qy - The query object specifying the conditions for filtering the data.
19
+ * @param options - The options object specifying additional operations to be applied on the result set.
20
+ * @returns A promise that resolves to the filtered result set.
21
+ * @throws If an error occurs while retrieving the data.
22
+ */
23
+ where(qy: Where<T>, options?: ResultsetOptions): Promise<ResultSet<T>>;
24
+ get<T>(value: Partial<T>): Promise<T>;
25
+ getAll(): Promise<T[]>;
26
+ update(keyPathValue: string | number, data: Partial<T>): Promise<{
27
+ toString: () => string;
28
+ charAt: (pos: number) => string;
29
+ charCodeAt: (index: number) => number;
30
+ concat: (...strings: string[]) => string;
31
+ indexOf: (searchString: string, position?: number) => number;
32
+ lastIndexOf: (searchString: string, position?: number) => number;
33
+ localeCompare: {
34
+ (that: string): number;
35
+ (that: string, locales?: string | string[], options?: Intl.CollatorOptions): number;
36
+ (that: string, locales?: Intl.LocalesArgument, options?: Intl.CollatorOptions): number;
37
+ };
38
+ match: {
39
+ (regexp: string | RegExp): RegExpMatchArray | null;
40
+ (matcher: {
41
+ [Symbol.match](string: string): RegExpMatchArray | null;
42
+ }): RegExpMatchArray | null;
43
+ };
44
+ replace: {
45
+ (searchValue: string | RegExp, replaceValue: string): string;
46
+ (searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
47
+ (searchValue: {
48
+ [Symbol.replace](string: string, replaceValue: string): string;
49
+ }, replaceValue: string): string;
50
+ (searchValue: {
51
+ [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string;
52
+ }, replacer: (substring: string, ...args: any[]) => string): string;
53
+ };
54
+ search: {
55
+ (regexp: string | RegExp): number;
56
+ (searcher: {
57
+ [Symbol.search](string: string): number;
58
+ }): number;
59
+ };
60
+ slice: (start?: number, end?: number) => string;
61
+ split: {
62
+ (separator: string | RegExp, limit?: number): string[];
63
+ (splitter: {
64
+ [Symbol.split](string: string, limit?: number): string[];
65
+ }, limit?: number): string[];
66
+ };
67
+ substring: (start: number, end?: number) => string;
68
+ toLowerCase: () => string;
69
+ toLocaleLowerCase: {
70
+ (locales?: string | string[]): string;
71
+ (locales?: Intl.LocalesArgument): string;
72
+ };
73
+ toUpperCase: () => string;
74
+ toLocaleUpperCase: {
75
+ (locales?: string | string[]): string;
76
+ (locales?: Intl.LocalesArgument): string;
77
+ };
78
+ trim: () => string;
79
+ length: number;
80
+ substr: (from: number, length?: number) => string;
81
+ valueOf: () => string;
82
+ codePointAt: (pos: number) => number | undefined;
83
+ includes: (searchString: string, position?: number) => boolean;
84
+ endsWith: (searchString: string, endPosition?: number) => boolean;
85
+ normalize: {
86
+ (form: "NFC" | "NFD" | "NFKC" | "NFKD"): string;
87
+ (form?: string): string;
88
+ };
89
+ repeat: (count: number) => string;
90
+ startsWith: (searchString: string, position?: number) => boolean;
91
+ anchor: (name: string) => string;
92
+ big: () => string;
93
+ blink: () => string;
94
+ bold: () => string;
95
+ fixed: () => string;
96
+ fontcolor: (color: string) => string;
97
+ fontsize: {
98
+ (size: number): string;
99
+ (size: string): string;
100
+ };
101
+ italics: () => string;
102
+ link: (url: string) => string;
103
+ small: () => string;
104
+ strike: () => string;
105
+ sub: () => string;
106
+ sup: () => string;
107
+ padStart: (maxLength: number, fillString?: string) => string;
108
+ padEnd: (maxLength: number, fillString?: string) => string;
109
+ trimEnd: () => string;
110
+ trimStart: () => string;
111
+ trimLeft: () => string;
112
+ trimRight: () => string;
113
+ matchAll: (regexp: RegExp) => RegExpStringIterator<RegExpExecArray>;
114
+ replaceAll: {
115
+ (searchValue: string | RegExp, replaceValue: string): string;
116
+ (searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
117
+ };
118
+ at: (index: number) => string | undefined;
119
+ isWellFormed: () => boolean;
120
+ toWellFormed: () => string;
121
+ [Symbol.iterator]: () => StringIterator<string>;
122
+ }>;
123
+ updateWhere(where: Where<T>, data: Partial<T>): Promise<unknown>;
124
+ put<T>(value: Partial<T>): Promise<T>;
125
+ /** ok add data to the store */
126
+ add<T>(data: Partial<T>): Promise<T | boolean>;
127
+ delete(keyPathValue: string | number): Promise<boolean>;
128
+ deleteWhere(where: Where<T>): Promise<boolean>;
129
+ }
130
+ export declare const Collection: CollectionCore;
@@ -1,7 +1,7 @@
1
1
  /* path: src\lib\scripts\collection\collection.ts */
2
- import { Query, getResultset } from '@medyll/idae-query';
3
- import {} from '@medyll/idae-query';
4
- import { idbqlEvent } from '../state/idbqlEvent.svelte.js';
2
+ import { Query, getResultset } from "@medyll/idae-query";
3
+ import {} from "@medyll/idae-query";
4
+ import { idbqlEvent } from "../state/idbqlEvent.svelte.js";
5
5
  export class CollectionCore {
6
6
  _store;
7
7
  version;
@@ -12,7 +12,7 @@ export class CollectionCore {
12
12
  this._store = store;
13
13
  this.version = args.version;
14
14
  this.dbName = args.dbName;
15
- this.keyPath = keyPath.split(',')[0].replace(/[\s+]/g, '').replace(/&/, '');
15
+ this.keyPath = keyPath.split(",")[0].replace(/[\s+]/g, "").replace(/&/, "");
16
16
  }
17
17
  get name() {
18
18
  return this._store;
@@ -30,7 +30,7 @@ export class CollectionCore {
30
30
  if (!db.objectStoreNames.contains(this._store)) {
31
31
  throw new Error(`Collection ${this._store} not found`);
32
32
  }
33
- return db.transaction(this._store, 'readwrite').objectStore(this._store);
33
+ return db.transaction(this._store, "readwrite").objectStore(this._store);
34
34
  }
35
35
  /**
36
36
  * Retrieves the data from the collection based on the provided query.
@@ -53,7 +53,7 @@ export class CollectionCore {
53
53
  const storeObj = await this.getCollection();
54
54
  const get = storeObj.get(value);
55
55
  get.onsuccess = () => resolve(get.result);
56
- get.onerror = () => reject('not found');
56
+ get.onerror = () => reject("not found");
57
57
  });
58
58
  }
59
59
  getAll() {
@@ -64,7 +64,7 @@ export class CollectionCore {
64
64
  resolve(getAll.result);
65
65
  };
66
66
  getAll.onerror = function () {
67
- reject('not found');
67
+ reject("not found");
68
68
  };
69
69
  });
70
70
  }
@@ -73,7 +73,7 @@ export class CollectionCore {
73
73
  return this.put({
74
74
  [this.keyPath]: keyPathValue,
75
75
  ...dta,
76
- ...data
76
+ ...data,
77
77
  });
78
78
  }
79
79
  async updateWhere(where, data) {
@@ -84,7 +84,7 @@ export class CollectionCore {
84
84
  const newData = {
85
85
  [this.keyPath]: dta[this.keyPath],
86
86
  ...dta,
87
- ...data
87
+ ...data,
88
88
  };
89
89
  return this.put(newData);
90
90
  }
@@ -108,7 +108,7 @@ export class CollectionCore {
108
108
  resolve(updatedData);
109
109
  };
110
110
  put.onerror = function () {
111
- reject('data not put');
111
+ reject("data not put");
112
112
  };
113
113
  });
114
114
  }
@@ -132,10 +132,10 @@ export class CollectionCore {
132
132
  const storeObj = await this.getCollection();
133
133
  let objectStoreRequest = storeObj.delete(keyPathValue);
134
134
  objectStoreRequest.onsuccess = () => {
135
- idbqlEvent.registerEvent('delete', {
135
+ idbqlEvent.registerEvent("delete", {
136
136
  collection: this._store,
137
137
  data: keyPathValue,
138
- keyPath: this.keyPath
138
+ keyPath: this.keyPath,
139
139
  });
140
140
  resolve(true);
141
141
  };
@@ -146,19 +146,13 @@ export class CollectionCore {
146
146
  }
147
147
  async deleteWhere(where) {
148
148
  return this.where(where).then((data) => {
149
- return new Promise(async (resolve, reject) => {
150
- Promise.all(data.map((data) => {
151
- if (this.keyPath && data[this.keyPath]) {
152
- return this.delete(data[this.keyPath]);
153
- }
154
- }))
155
- .then(() => {
156
- resolve(true);
157
- })
158
- .catch((e) => {
159
- reject(e);
160
- });
161
- });
149
+ return Promise.all(data.map((item) => {
150
+ if (this.keyPath && item[this.keyPath]) {
151
+ return this.delete(item[this.keyPath]);
152
+ }
153
+ }))
154
+ .then(() => true)
155
+ .catch(() => false);
162
156
  });
163
157
  }
164
158
  }
@@ -170,8 +164,15 @@ function createIDBStoreProxy(store) {
170
164
  return new Proxy(instance, {
171
165
  get(target, prop, receiver) {
172
166
  const origMethod = target[prop];
173
- if (typeof origMethod === 'function' &&
174
- ['update', 'updateWhere', 'put', 'add', 'delete', 'deleteWhere'].includes(String(prop))) {
167
+ if (typeof origMethod === "function" &&
168
+ [
169
+ "update",
170
+ "updateWhere",
171
+ "put",
172
+ "add",
173
+ "delete",
174
+ "deleteWhere",
175
+ ].includes(String(prop))) {
175
176
  return function (...args) {
176
177
  return new Promise(async (resolve, reject) => {
177
178
  origMethod
@@ -180,7 +181,7 @@ function createIDBStoreProxy(store) {
180
181
  idbqlEvent.registerEvent(prop, {
181
182
  collection: instance._store,
182
183
  data: res,
183
- keyPath: instance.keyPath
184
+ keyPath: instance.keyPath,
184
185
  });
185
186
  resolve(res);
186
187
  })
@@ -192,8 +193,8 @@ function createIDBStoreProxy(store) {
192
193
  };
193
194
  }
194
195
  return Reflect.get(target, prop, receiver);
195
- }
196
+ },
196
197
  });
197
- }
198
+ },
198
199
  });
199
200
  }
@@ -1,5 +1,5 @@
1
- import { CollectionCore } from '../collection/collection.js';
2
- import { type StateCollectionDyn } from '../state/idbstate.svelte.js';
1
+ import { CollectionCore } from "../collection/collection.svelte.js";
2
+ import { type StateCollectionDyn } from "../state/idbstate.svelte.js";
3
3
  export declare enum enumPrimitive {
4
4
  id = "id",
5
5
  any = "any",
@@ -23,7 +23,7 @@ type CombineElements<T extends string, U extends string = T> = T extends any ? T
23
23
  type CombinedArgs = CombineElements<TplProperties>;
24
24
  type IdbObjectify<T extends string> = `array-of-${T}` | `object-${T}`;
25
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}`;
26
+ export type TplFieldPrimitive<T = {}> = keyof typeof enumPrimitive | `text-${"tiny" | "short" | "medium" | "long" | "area"}` | `${string}.${string}` | `fk-${string}.${string}`;
27
27
  export type TplObjectFieldPrimitive = IdbObjectify<TplFieldPrimitive>;
28
28
  export type TplFieldFk = `fk-${string}.${string}`;
29
29
  export type TplFkObject = IdbObjectify<TplFieldFk>;
@@ -44,8 +44,8 @@ export type IdbqModel<T = Record<string, Record<string, any>>> = {
44
44
  readonly [K in keyof T]: CollectionModel<T[K]>;
45
45
  };
46
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'];
47
+ export type Tpl<T = TplCollectionFields> = CollectionModel<T>["template"];
48
+ export type TplFields<T = TplCollectionFields> = CollectionModel<T>["template"]["fields"];
49
49
  export type CollectionModel<T = TplCollectionFields> = {
50
50
  keyPath: string | any;
51
51
  /** @deprecated use ts instead */
@@ -53,7 +53,7 @@ export type CollectionModel<T = TplCollectionFields> = {
53
53
  ts: any;
54
54
  template: {
55
55
  index: string;
56
- presentation: CombineElements<keyof CollectionModel<T>['ts']>;
56
+ presentation: CombineElements<keyof CollectionModel<T>["ts"]>;
57
57
  fields: {
58
58
  [K in keyof T]: TplFieldRules;
59
59
  };
@@ -67,10 +67,10 @@ export type CollectionModel<T = TplCollectionFields> = {
67
67
  };
68
68
  };
69
69
  type ReadonlyCollections<T extends IdbqModel> = {
70
- [K in keyof T]: CollectionCore<T[K]['ts']>;
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]: StateCollectionDyn<T[K]["ts"]>;
74
74
  };
75
75
  /**
76
76
  * Represents the IndexedDB wrapper for managing database operations.
@@ -1,7 +1,7 @@
1
1
  /* src\lib\scripts\idbqlCore\idbqlCore.ts */
2
- import { Collection, CollectionCore } from '../collection/collection.js';
3
- import { createIdbqlState } from '../state/idbstate.svelte.js';
4
- import { Schema } from './idbqlSchema.js';
2
+ import { Collection, CollectionCore } from "../collection/collection.svelte.js";
3
+ import { createIdbqlState, } from "../state/idbstate.svelte.js";
4
+ import { Schema } from "./idbqlSchema.js";
5
5
  export var enumPrimitive;
6
6
  (function (enumPrimitive) {
7
7
  enumPrimitive["id"] = "id";
@@ -23,7 +23,7 @@ export var TplProperties;
23
23
  TplProperties["readonly"] = "readonly";
24
24
  TplProperties["required"] = "required";
25
25
  })(TplProperties || (TplProperties = {}));
26
- const a = 'object-any (readonly private)';
26
+ const a = "object-any (readonly private)";
27
27
  /**
28
28
  * Represents the IndexedDB wrapper for managing database operations.
29
29
  * @template T - The type of data stored in the IndexedDB.
@@ -45,13 +45,13 @@ export class IdbqlIndexedCore {
45
45
  const stores = {};
46
46
  Object.keys(idbqModel).forEach((modelName) => {
47
47
  const modelInfo = idbqModel[modelName];
48
- stores[modelName] = modelInfo.keyPath || '';
48
+ stores[modelName] = modelInfo.keyPath || "";
49
49
  Object.defineProperty(this, modelName, {
50
50
  // @ts-ignore
51
51
  value: undefined,
52
52
  writable: true,
53
53
  enumerable: true,
54
- configurable: true
54
+ configurable: true,
55
55
  });
56
56
  });
57
57
  this.stores(stores);
@@ -68,7 +68,7 @@ export class IdbqlIndexedCore {
68
68
  * @returns {object} - An object with a `stores` method to define the object stores.
69
69
  */
70
70
  async stores(args) {
71
- if (typeof indexedDB !== 'undefined') {
71
+ if (typeof indexedDB !== "undefined") {
72
72
  return new Promise((resolve, reject) => {
73
73
  this.#schema = args;
74
74
  const dbConnection = indexedDB.open(this.databaseName, this.dbVersion);
@@ -98,7 +98,7 @@ export class IdbqlIndexedCore {
98
98
  }
99
99
  async transaction(storeNames, mode, callback) {
100
100
  if (!this.idbDatabase) {
101
- throw new Error('Database not initialized');
101
+ throw new Error("Database not initialized");
102
102
  }
103
103
  return new Promise((resolve, reject) => {
104
104
  const tx = this.idbDatabase.transaction(storeNames, mode);
@@ -120,11 +120,11 @@ export class IdbqlIndexedCore {
120
120
  // @ts-ignore
121
121
  value: new Collection(storeName, this.#schema[storeName], {
122
122
  dbName: this.databaseName,
123
- version // @ts-ignore
123
+ version, // @ts-ignore
124
124
  }),
125
125
  writable: true,
126
126
  enumerable: true,
127
- configurable: true
127
+ configurable: true,
128
128
  });
129
129
  });
130
130
  }
@@ -147,9 +147,9 @@ export const createIdbqDb = (model, version) => {
147
147
  idbDatabase: idb_,
148
148
  idbql: idb_,
149
149
  idbqlState: createIdbqlState(idb_).state,
150
- idbqModel: model
150
+ idbqModel: model,
151
151
  };
152
- }
152
+ },
153
153
  };
154
154
  };
155
155
  // main export is here ?
package/dist/index.d.ts CHANGED
@@ -4,4 +4,4 @@ export * from './scripts/types.js';
4
4
  export * from './path/pathResolver.js';
5
5
  export * from './idbqlCore/idbqlSchema.js';
6
6
  export * from './idbqlCore/idbqlCore.js';
7
- export * from './collection/collection.js';
7
+ export * from './collection/collection.svelte.js';
package/dist/index.js CHANGED
@@ -5,4 +5,4 @@ export * from './scripts/types.js';
5
5
  export * from './path/pathResolver.js';
6
6
  export * from './idbqlCore/idbqlSchema.js';
7
7
  export * from './idbqlCore/idbqlCore.js';
8
- export * from './collection/collection.js';
8
+ export * from './collection/collection.svelte.js';
@@ -1,12 +1,11 @@
1
- type EventType = 'add' | 'put' | 'update' | 'updateWhere' | 'delete' | 'deleteWhere' | 'set';
1
+ type EventType = "add" | "put" | "update" | "updateWhere" | "delete" | "deleteWhere" | "set";
2
2
  interface EventData<T = any> {
3
3
  collection: string;
4
4
  data: T;
5
5
  keyPath: string;
6
6
  }
7
7
  declare class IdbqlStateEvent {
8
- #private;
9
- get dataState(): Record<string, any[]>;
8
+ dataState: Record<string, any[]>;
10
9
  registerEvent(event: EventType, eventData: EventData): void;
11
10
  }
12
11
  export declare const idbqlEvent: IdbqlStateEvent;
@@ -1,59 +1,111 @@
1
1
  class IdbqlStateEvent {
2
- #dataState = $state({});
3
- get dataState() {
4
- return this.#dataState;
5
- }
2
+ // main application shared state
3
+ dataState = $state({});
6
4
  registerEvent(event, eventData) {
7
5
  const { collection, data, keyPath } = eventData;
8
6
  if (!collection) {
9
7
  console.error(`Collection is mandatory`);
10
8
  return;
11
9
  }
12
- if (!this.#dataState[collection]) {
13
- this.#dataState[collection] = [];
10
+ if (!this.dataState[collection]) {
11
+ this.dataState[collection] = [];
14
12
  }
15
13
  switch (event) {
16
- case 'set':
14
+ case "set":
17
15
  if (data) {
18
- this.#dataState[collection] = Array.isArray(data) ? data : [data];
16
+ this.dataState[collection] = Array.isArray(data) ? data : [data];
19
17
  }
20
18
  break;
21
- case 'add':
19
+ case "add":
22
20
  if (data) {
23
- this.#dataState[collection].push(data);
21
+ this.dataState[collection].push(data);
24
22
  }
25
23
  break;
26
- case 'put':
27
- case 'update':
24
+ case "put":
25
+ case "update":
28
26
  if (data && keyPath) {
29
- const index = this.#dataState[collection].findIndex((item) => item[keyPath] === data[keyPath]);
30
- if (index !== -1) {
31
- this.#dataState[collection][index] = {
32
- ...this.#dataState[collection][index],
33
- ...data
34
- };
35
- }
36
- else {
37
- this.#dataState[collection].push(data);
27
+ let itemFound = false;
28
+ this.dataState[collection].forEach((item, index) => {
29
+ if (item[keyPath] === data[keyPath]) {
30
+ this.dataState[collection][index] = {
31
+ ...item,
32
+ ...data,
33
+ };
34
+ itemFound = true;
35
+ }
36
+ });
37
+ if (!itemFound) {
38
+ this.dataState[collection].push(data);
38
39
  }
39
40
  }
41
+ /* if (data && keyPath) {
42
+ const index = this.dataState[collection].findIndex(
43
+ (item) => item[keyPath] === data[keyPath],
44
+ );
45
+ if (index !== -1) {
46
+ this.dataState[collection][index] = {
47
+ ...this.dataState[collection][index],
48
+ ...data,
49
+ };
50
+ } else {
51
+ this.dataState[collection].push(data);
52
+ }
53
+ } */
40
54
  break;
41
- case 'updateWhere':
42
- if (data && typeof data === 'object') {
43
- this.#dataState[collection] = this.#dataState[collection].map((item) => Object.entries(data).every(([key, value]) => item[key] === value)
44
- ? { ...item, ...data }
45
- : item);
55
+ case "updateWhere":
56
+ if (data && typeof data === "object") {
57
+ this.dataState[collection].forEach((item, index) => {
58
+ if (Object.entries(data).every(([key, value]) => item[key] === value)) {
59
+ this.dataState[collection][index] = { ...item, ...data };
60
+ }
61
+ });
46
62
  }
63
+ /* if (data && typeof data === "object") {
64
+ this.dataState[collection] = this.dataState[collection].map((item) =>
65
+ Object.entries(data).every(([key, value]) => item[key] === value)
66
+ ? { ...item, ...data }
67
+ : item,
68
+ );
69
+ } */
47
70
  break;
48
- case 'delete':
71
+ case "delete":
49
72
  if (data && keyPath) {
50
- this.#dataState[collection] = this.#dataState[collection].filter((item) => item[keyPath] !== data[keyPath]);
73
+ const indicesToRemove = [];
74
+ this.dataState[collection].forEach((item, index) => {
75
+ if (item[keyPath] === data[keyPath]) {
76
+ indicesToRemove.push(index);
77
+ }
78
+ });
79
+ for (let i = indicesToRemove.length - 1; i >= 0; i--) {
80
+ this.dataState[collection].splice(indicesToRemove[i], 1);
81
+ }
51
82
  }
83
+ /* if (data && keyPath) {
84
+ this.dataState[collection] = this.dataState[collection].filter(
85
+ (item) => item[keyPath] !== data[keyPath],
86
+ );
87
+ } */
52
88
  break;
53
- case 'deleteWhere':
54
- if (data && typeof data === 'object') {
55
- this.#dataState[collection] = this.#dataState[collection].filter((item) => !Object.entries(data).every(([key, value]) => item[key] === value));
89
+ case "deleteWhere":
90
+ if (data && typeof data === "object") {
91
+ const indicesToRemove = [];
92
+ this.dataState[collection].forEach((item, index) => {
93
+ if (Object.entries(data).every(([key, value]) => item[key] === value)) {
94
+ indicesToRemove.push(index);
95
+ }
96
+ });
97
+ for (let i = indicesToRemove.length - 1; i >= 0; i--) {
98
+ this.dataState[collection].splice(indicesToRemove[i], 1);
99
+ }
56
100
  }
101
+ /* if (data && typeof data === "object") {
102
+ this.dataState[collection] = this.dataState[collection].filter(
103
+ (item) =>
104
+ !Object.entries(data).every(
105
+ ([key, value]) => item[key] === value,
106
+ ),
107
+ );
108
+ } */
57
109
  break;
58
110
  default:
59
111
  console.error(`Unhandled event type: ${event}`);
@@ -1,4 +1,8 @@
1
1
  import type { IdbqlIndexedCore } from "../idbqlCore/idbqlCore.js";
2
+ interface IdbqlIndexedCore {
3
+ [key: string]: CollectionCore<any>;
4
+ }
5
+ import type { CollectionCore } from "../collection/collection.svelte.js";
2
6
  import { type Where, type ResultSet, type ResultsetOptions } from "@medyll/idae-query";
3
7
  /**
4
8
  * Main entry point.
@@ -20,23 +24,23 @@ export declare const createIdbqlState: (idbBase: IdbqlIndexedCore) => {
20
24
  */
21
25
  export declare class StateCollectionDyn<T> {
22
26
  private collectionName;
23
- private state;
24
27
  private idbBase;
25
28
  constructor(collectionName: string, idbBase: IdbqlIndexedCore);
26
- get collectionState(): any[];
29
+ get collectionState(): ResultSet<T>;
27
30
  private testIdbql;
28
31
  private feed;
29
32
  _where(qy: Where<T>, options?: ResultsetOptions): ResultSet<T>;
30
33
  get where(): (qy: Where<T>, options?: ResultsetOptions) => ResultSet<T>;
31
- update(keyPathValue: string | number, data: Partial<T>): void;
32
- get(value: any, pathKey?: string): T[];
34
+ get(value: any, pathKey?: string): ResultSet<T>;
33
35
  getOne(value: any, pathKey?: string): T;
34
- getAll(): ResultSet<T>;
35
- updateWhere(where: Where<T>, data: Partial<T>): void;
36
- put(value: Partial<T>): void;
37
- add(data: T): void;
38
- delete(keyPathValue: string | number): boolean | undefined;
36
+ getAll: () => ResultSet<T>;
37
+ update(keyPathValue: string | number, data: Partial<T>): Promise<boolean | undefined>;
38
+ updateWhere(where: Where<T>, data: Partial<T>): Promise<boolean | undefined>;
39
+ put(value: Partial<T>): Promise<T | undefined>;
40
+ add(data: T): Promise<T | undefined>;
41
+ delete(keyPathValue: string | number): Promise<boolean | undefined>;
39
42
  /** @deprecated */
40
- del(keyPathValue: string | number): boolean | undefined;
41
- deleteWhere(where: Where<T>): boolean | undefined;
43
+ del(keyPathValue: string | number): Promise<boolean | undefined>;
44
+ deleteWhere(where: Where<T>): Promise<boolean | undefined>;
42
45
  }
46
+ export {};
@@ -46,11 +46,11 @@ export const createIdbqlState = (idbBase) => {
46
46
  */
47
47
  export class StateCollectionDyn {
48
48
  collectionName;
49
- state = idbqlEvent.dataState; // svelte state ;)
50
49
  idbBase;
51
50
  constructor(collectionName, idbBase) {
52
- if (!idbqlEvent.dataState?.[collectionName])
51
+ if (!idbqlEvent.dataState?.[collectionName]) {
53
52
  idbqlEvent.dataState[collectionName] = [];
53
+ }
54
54
  this.collectionName = collectionName;
55
55
  this.idbBase = idbBase;
56
56
  this.feed();
@@ -65,13 +65,18 @@ export class StateCollectionDyn {
65
65
  feed() {
66
66
  if (this.idbBase && this.testIdbql(this.collectionName)) {
67
67
  this.idbBase[this.collectionName].getAll().then((data) => {
68
- idbqlEvent.dataState[this.collectionName] = data;
68
+ idbqlEvent.dataState[this.collectionName] = getResultset(data);
69
+ /* idbqlEvent.dataState = {
70
+ ...idbqlEvent.dataState,
71
+ [this.collectionName]: getResultset(data),
72
+ }; */
69
73
  });
70
74
  }
71
75
  }
76
+ /* READ OP */
72
77
  _where(qy, options) {
73
78
  let dts = this.collectionState;
74
- let c = Operators.parse(dts ?? [], qy);
79
+ let c = Operators.parse(dts ?? getResultset([]), qy);
75
80
  const r = getResultset(c);
76
81
  if (options)
77
82
  r.setOptions(options);
@@ -80,49 +85,57 @@ export class StateCollectionDyn {
80
85
  get where() {
81
86
  return (qy, options) => this._where(qy, options);
82
87
  }
83
- update(keyPathValue, data) {
84
- if (this.idbBase && this.testIdbql(this.collectionName)) {
85
- this.idbBase[this.collectionName].update(keyPathValue, data);
86
- }
87
- }
88
88
  get(value, pathKey = "id") {
89
89
  return this.collectionState.filter((d) => d[pathKey] === value);
90
90
  }
91
91
  getOne(value, pathKey = "id") {
92
- return this.collectionState.filter((d) => d[pathKey] === value)?.[0];
92
+ return this.collectionState.filter((d) => d?.[pathKey] === value)?.[0];
93
93
  }
94
- getAll() {
95
- return getResultset(this.collectionState);
94
+ getAll = () => {
95
+ return this.collectionState;
96
+ };
97
+ /* WRITE OP */
98
+ async update(keyPathValue, data) {
99
+ if (this.idbBase && this.testIdbql(this.collectionName)) {
100
+ return (await this.idbBase[this.collectionName].update(keyPathValue, data));
101
+ }
102
+ return undefined;
96
103
  }
97
- updateWhere(where, data) {
104
+ async updateWhere(where, data) {
98
105
  if (this.idbBase && this.testIdbql(this.collectionName)) {
99
- this.idbBase[this.collectionName].updateWhere(where, data);
106
+ return (await this.idbBase[this.collectionName].updateWhere(where, data));
100
107
  }
108
+ return undefined;
101
109
  }
102
- put(value) {
110
+ async put(value) {
103
111
  if (this.idbBase && this.testIdbql(this.collectionName)) {
104
- this.idbBase[this.collectionName].put(value);
112
+ return this.idbBase[this.collectionName].put(value).then((data) => {
113
+ return data;
114
+ });
105
115
  }
116
+ return undefined;
106
117
  }
107
- add(data) {
118
+ async add(data) {
108
119
  if (this.idbBase && this.testIdbql(this.collectionName)) {
109
- this.idbBase[this.collectionName].add(data);
120
+ return (await this.idbBase[this.collectionName].add(data));
110
121
  }
122
+ return undefined;
111
123
  }
112
- delete(keyPathValue) {
124
+ async delete(keyPathValue) {
113
125
  if (this.idbBase && this.testIdbql(this.collectionName)) {
114
- return this.idbBase[this.collectionName].delete(keyPathValue);
126
+ return await this.idbBase[this.collectionName].delete(keyPathValue);
115
127
  }
128
+ return undefined;
116
129
  }
117
130
  /** @deprecated */
118
- del(keyPathValue) {
131
+ async del(keyPathValue) {
119
132
  if (this.idbBase && this.testIdbql(this.collectionName)) {
120
- return this.idbBase[this.collectionName].delete(keyPathValue);
133
+ return await this.idbBase[this.collectionName].delete(keyPathValue);
121
134
  }
122
135
  }
123
- deleteWhere(where) {
136
+ async deleteWhere(where) {
124
137
  if (this.idbBase && this.testIdbql(this.collectionName)) {
125
- return this.idbBase[this.collectionName].deleteWhere(where);
138
+ return await this.idbBase[this.collectionName].deleteWhere(where);
126
139
  }
127
140
  }
128
141
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@medyll/idae-idbql",
3
3
  "scope": "@medyll",
4
- "version": "0.44.0",
4
+ "version": "0.55.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",
@@ -17,7 +17,8 @@
17
17
  "exports": {
18
18
  ".": {
19
19
  "types": "./dist/index.d.ts",
20
- "svelte": "./dist/index.js"
20
+ "svelte": "./dist/index.js",
21
+ "import": "./dist/index.js"
21
22
  }
22
23
  },
23
24
  "files": [
@@ -30,20 +31,20 @@
30
31
  },
31
32
  "devDependencies": {
32
33
  "@medyll/idae-prettier-config": "^1.1.0",
33
- "@sveltejs/adapter-auto": "^3.2.2",
34
- "@sveltejs/kit": "^2.5.20",
35
- "@sveltejs/package": "^2.3.2",
36
- "@sveltejs/vite-plugin-svelte": "^3.1.1",
37
- "svelte": "^5.0.0-next.218",
38
- "svelte-check": "^3.8.5",
39
- "tslib": "^2.6.3",
40
- "typescript": "^5.5.4",
41
- "vite": "^5.3.5"
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",
39
+ "svelte-check": "^4.1.4",
40
+ "tslib": "^2.8.1",
41
+ "typescript": "^5.8.2",
42
+ "vite": "^6.2.0"
42
43
  },
43
44
  "svelte": "./dist/index.js",
44
45
  "types": "./dist/index.d.ts",
45
46
  "type": "module",
46
47
  "dependencies": {
47
- "@medyll/idae-query": "^0.45.0"
48
+ "@medyll/idae-query": "^0.56.0"
48
49
  }
49
50
  }
@@ -1,34 +0,0 @@
1
- import { type Where, type ResultsetOptions, type ResultSet } from '@medyll/idae-query';
2
- export declare class CollectionCore<T = any> {
3
- protected _store: string;
4
- private version?;
5
- private dbName;
6
- private dBOpenRequest;
7
- private keyPath;
8
- constructor(store: string, keyPath: string, args: {
9
- dbName: string;
10
- version?: number;
11
- });
12
- get name(): string;
13
- private getDatabase;
14
- /** get the collection */
15
- private getCollection;
16
- /**
17
- * Retrieves the data from the collection based on the provided query.
18
- * @param qy - The query object specifying the conditions for filtering the data.
19
- * @param options - The options object specifying additional operations to be applied on the result set.
20
- * @returns A promise that resolves to the filtered result set.
21
- * @throws If an error occurs while retrieving the data.
22
- */
23
- where(qy: Where<T>, options?: ResultsetOptions): Promise<ResultSet<T>>;
24
- get(value: any): Promise<T>;
25
- getAll(): Promise<T[]>;
26
- update(keyPathValue: string | number, data: Partial<T>): Promise<unknown>;
27
- updateWhere(where: Where<T>, data: Partial<T>): Promise<unknown>;
28
- put(value: Partial<T>): Promise<unknown>;
29
- /** ok add data to the store */
30
- add(data: T): Promise<T | boolean>;
31
- delete(keyPathValue: string | number): Promise<boolean>;
32
- deleteWhere(where: Where<T>): Promise<boolean>;
33
- }
34
- export declare const Collection: CollectionCore;