@medyll/idae-idbql 0.66.0 → 0.68.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,76 +1,11 @@
1
- import { CollectionCore } from "../collection/collection.svelte.js";
2
- import { type CollectionState } from "../state/idbstate.svelte.js";
3
- export declare enum enumPrimitive {
4
- id = "id",
5
- any = "any",
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]: CollectionState<T[K]["ts"]>;
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
- * Sets the version of the database.
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.
@@ -125,4 +61,3 @@ export declare const idbqBase: <T extends IdbqModel>(model: T, version: number)
125
61
  idbqModel: T;
126
62
  };
127
63
  };
128
- export {};
@@ -1,29 +1,7 @@
1
1
  /* src\lib\scripts\idbqlCore\idbqlCore.ts */
2
- import { Collection, CollectionCore } from "../collection/collection.svelte.js";
3
- import { createIdbqlState, } from "../state/idbstate.svelte.js";
4
- import { Schema } from "./idbqlSchema.js";
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
- * Sets the version of the database.
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 !== "undefined") {
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
- m.createSchema(db, args);
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("Database not initialized");
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, // @ts-ignore
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.
@@ -149,9 +131,9 @@ export const createIdbqDb = (model, version) => {
149
131
  idbql: idb_,
150
132
  idbqlState: idbqlState.collectionState,
151
133
  qolie: idbqlState.qolie,
152
- idbqModel: model,
134
+ idbqModel: model
153
135
  };
154
- },
136
+ }
155
137
  };
156
138
  };
157
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
  }
@@ -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<TplFieldFk>;
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,22 @@
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
+ 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';
@@ -1,9 +1,9 @@
1
- import type { IdbqlIndexedCore } from "../idbqlCore/idbqlCore.js";
1
+ import type { IdbqlIndexedCore } from '../idbqlCore/idbqlCore.js';
2
2
  interface IdbqlIndexedCore {
3
3
  [key: string]: CollectionCore<any>;
4
4
  }
5
- import type { CollectionCore } from "../collection/collection.svelte.js";
6
- import { type Where, type ResultSet, type ResultsetOptions } from "@medyll/idae-query";
5
+ import type { CollectionCore } from '../collection/collection.svelte.js';
6
+ import { type Where, type ResultSet, type ResultsetOptions } from '@medyll/idae-query';
7
7
  /**
8
8
  * Main entry point.
9
9
  * Creates a state object with indexedDB synchronization.
@@ -11,11 +11,11 @@ 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 collectionState: Record<string, CollectionState<any>>;
15
- readonly qolie: (collection: string) => {
16
- get: (value: any, pathKey?: string) => ResultSet<any>;
14
+ collectionState: Record<string, CollectionState<any>>;
15
+ qolie: (collection: string) => {
16
+ get: any;
17
17
  getBy: (value: any, pathKey?: string) => ResultSet<any>;
18
- getOne: (value: any, pathKey?: string) => any;
18
+ getOne: any;
19
19
  getAll: () => ResultSet<any>;
20
20
  create: (value: Partial<any>) => Promise<any>;
21
21
  delete: (keyPathValue: string | number) => Promise<boolean | undefined>;
@@ -73,9 +73,9 @@ export declare class CollectionState<T> {
73
73
  * Gets items from the collection by a specific value and key.
74
74
  * @param {any} value - The value to search for.
75
75
  * @param {string} [pathKey="id"] - The key to search by.
76
- * @returns {ResultSet<T>} The result set of the query.
76
+ * @returns {T | undefined} The item found, or undefined if not found.
77
77
  */
78
- get(value: any, pathKey?: string): ResultSet<T>;
78
+ get get(): T | undefined;
79
79
  /**
80
80
  * Gets items from the collection by a specific value and key using a "where" query.
81
81
  * @param {any} value - The value to search for.
@@ -90,7 +90,7 @@ export declare class CollectionState<T> {
90
90
  * @param {string} [pathKey="id"] - The key to search by.
91
91
  * @returns {T | undefined} The item found, or undefined if not found.
92
92
  */
93
- getOne(value: any, pathKey?: string): T | undefined;
93
+ get getOne(): T | undefined;
94
94
  /**
95
95
  * Gets all items from the collection.
96
96
  * @returns {ResultSet<T>} The result set of all items.
@@ -1,6 +1,6 @@
1
- import { idbqlEvent } from "./idbqlEvent.svelte.js";
1
+ import { idbqlEvent } from './idbqlEvent.svelte.js';
2
2
  //
3
- import { Operators, getResultset, } from "@medyll/idae-query";
3
+ import { Operators, getResultset } from '@medyll/idae-query';
4
4
  /**
5
5
  * Main entry point.
6
6
  * Creates a state object with indexedDB synchronization.
@@ -25,7 +25,7 @@ export const createIdbqlState = (idbBase) => {
25
25
  deleteWhere: collections[collection].deleteWhere,
26
26
  update: collections[collection].update,
27
27
  where: collections[collection].where,
28
- updateWhere: collections[collection].updateWhere,
28
+ updateWhere: collections[collection].updateWhere
29
29
  };
30
30
  };
31
31
  function addCollection(collectionName) {
@@ -38,14 +38,10 @@ export const createIdbqlState = (idbBase) => {
38
38
  });
39
39
  }
40
40
  return {
41
- get collectionState() {
42
- return collections;
43
- },
44
- get qolie() {
45
- return qolie;
46
- },
41
+ collectionState: collections,
42
+ qolie: qolie,
47
43
  onCollection: addCollection,
48
- addCollection: addCollection,
44
+ addCollection: addCollection
49
45
  };
50
46
  };
51
47
  /**
@@ -122,10 +118,12 @@ export class CollectionState {
122
118
  * Gets items from the collection by a specific value and key.
123
119
  * @param {any} value - The value to search for.
124
120
  * @param {string} [pathKey="id"] - The key to search by.
125
- * @returns {ResultSet<T>} The result set of the query.
121
+ * @returns {T | undefined} The item found, or undefined if not found.
126
122
  */
127
- get(value, pathKey = "id") {
128
- return this.collectionState?.filter((d) => d[pathKey] === value);
123
+ get get() {
124
+ return (value, pathKey = 'id') => {
125
+ return this.where({ [pathKey]: value })?.[0];
126
+ };
129
127
  }
130
128
  /**
131
129
  * Gets items from the collection by a specific value and key using a "where" query.
@@ -133,7 +131,7 @@ export class CollectionState {
133
131
  * @param {string} [pathKey="id"] - The key to search by.
134
132
  * @returns {ResultSet<T>} The result set of the query.
135
133
  */
136
- getBy(value, pathKey = "id") {
134
+ getBy(value, pathKey = 'id') {
137
135
  return this.where({ [pathKey]: value });
138
136
  }
139
137
  /**
@@ -143,8 +141,10 @@ export class CollectionState {
143
141
  * @param {string} [pathKey="id"] - The key to search by.
144
142
  * @returns {T | undefined} The item found, or undefined if not found.
145
143
  */
146
- getOne(value, pathKey = "id") {
147
- return this.where({ [pathKey]: value })?.[0];
144
+ get getOne() {
145
+ return (value, pathKey = 'id') => {
146
+ return this.where({ [pathKey]: value })?.[0];
147
+ };
148
148
  }
149
149
  /**
150
150
  * Gets all items from the collection.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@medyll/idae-idbql",
3
3
  "scope": "@medyll",
4
- "version": "0.66.0",
4
+ "version": "0.68.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",
@@ -44,6 +44,6 @@
44
44
  "types": "./dist/index.d.ts",
45
45
  "type": "module",
46
46
  "dependencies": {
47
- "@medyll/idae-query": "^0.67.0"
47
+ "@medyll/idae-query": "^0.69.0"
48
48
  }
49
49
  }