@signaldb/core 1.1.0 → 1.2.1

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.
@@ -100,6 +100,7 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
100
100
  private isDisposed;
101
101
  private postBatchCallbacks;
102
102
  private fieldTracking;
103
+ private persistenceReadyPromise;
103
104
  /**
104
105
  * Initializes a new instance of the `Collection` class with optional configuration.
105
106
  * Sets up memory, persistence, reactivity, and indices as specified in the options.
@@ -154,6 +155,20 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
154
155
  * @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) field tracking.
155
156
  */
156
157
  setFieldTracking(enable: boolean): void;
158
+ /**
159
+ * Resolves when the persistence adapter finished initializing
160
+ * and the collection is ready to be used.
161
+ * @returns A promise that resolves when the collection is ready.
162
+ * @example
163
+ * ```ts
164
+ * const collection = new Collection({
165
+ * persistence: // ...
166
+ * })
167
+ * await collection.isReady()
168
+ *
169
+ * collection.insert({ name: 'Item 1' })
170
+ */
171
+ isReady(): Promise<void>;
157
172
  private profile;
158
173
  private executeInDebugMode;
159
174
  private rebuildIndices;
package/dist/index.cjs.js CHANGED
@@ -13,7 +13,10 @@ const modify = require("./index.cjs11.js");
13
13
  const randomId = require("./index.cjs12.js");
14
14
  const EventEmitter = require("./index.cjs13.js");
15
15
  const createIndex = require("./index.cjs14.js");
16
- devtools();
16
+ /* istanbul ignore if -- @preserve */
17
+ if (process.env.NODE_ENV !== "production") {
18
+ devtools();
19
+ }
17
20
  exports.loadDeveloperTools = devtools;
18
21
  exports.Collection = index.default;
19
22
  exports.AutoFetchCollection = AutoFetchCollection;
@@ -1,26 +1,4 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
2
  const index = require("./index.cjs3.js");
25
3
  var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
26
4
  const _interopNamespaceDefaultOnly = (e) => Object.freeze(Object.defineProperty({ __proto__: null, default: e }, Symbol.toStringTag, { value: "Module" }));
@@ -35,6 +13,7 @@ Please move @signaldb/devtools to development dependencies to disable it in prod
35
13
 
36
14
 
37
15
  `;
16
+ const packageName = "@signaldb/devtools";
38
17
  function isNodeErrorWithCode(error) {
39
18
  return typeof error === "object" && error !== null && "code" in error && typeof error.code === "string";
40
19
  }
@@ -47,14 +26,17 @@ async function checkDevtoolsAvailability() {
47
26
  }
48
27
  if (isCommonJS()) {
49
28
  try {
50
- require.resolve("@signaldb/devtools");
29
+ require.resolve(packageName);
51
30
  return true;
52
31
  } catch {
53
32
  return false;
54
33
  }
55
34
  }
56
35
  try {
57
- await import("@signaldb/devtools");
36
+ await import(
37
+ /* @vite-ignore */
38
+ packageName
39
+ );
58
40
  return true;
59
41
  } catch (error) {
60
42
  if (!isNodeErrorWithCode(error) || error.code !== "MODULE_NOT_FOUND") {
@@ -65,7 +47,7 @@ async function checkDevtoolsAvailability() {
65
47
  const modulePackage = await Promise.resolve().then(() => /* @__PURE__ */ _interopNamespaceDefaultOnly(require("./index.cjs15.js")));
66
48
  const createRequireFunction = modulePackage.default.createRequire;
67
49
  const cjsRequire = createRequireFunction(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.cjs2.js", document.baseURI).href);
68
- cjsRequire.resolve("@signaldb/devtools");
50
+ cjsRequire.resolve(packageName);
69
51
  return true;
70
52
  } catch {
71
53
  return false;
@@ -77,7 +59,7 @@ async function checkAndImportDevtools() {
77
59
  return false;
78
60
  if (isCommonJS()) {
79
61
  try {
80
- require("@signaldb/devtools");
62
+ require(packageName);
81
63
  return true;
82
64
  } catch (error) {
83
65
  if (isNodeErrorWithCode(error) && error.code === "MODULE_NOT_FOUND") {
@@ -87,7 +69,10 @@ async function checkAndImportDevtools() {
87
69
  }
88
70
  }
89
71
  try {
90
- await import("@signaldb/devtools");
72
+ await import(
73
+ /* @vite-ignore */
74
+ packageName
75
+ );
91
76
  return true;
92
77
  } catch (error) {
93
78
  if (!isNodeErrorWithCode(error) || error.code !== "MODULE_NOT_FOUND") {
@@ -98,7 +83,7 @@ async function checkAndImportDevtools() {
98
83
  const modulePackage = await Promise.resolve().then(() => /* @__PURE__ */ _interopNamespaceDefaultOnly(require("./index.cjs15.js")));
99
84
  const createRequireFunction = modulePackage.default.createRequire;
100
85
  const cjsRequire = createRequireFunction(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.cjs2.js", document.baseURI).href);
101
- cjsRequire("@signaldb/devtools");
86
+ cjsRequire(packageName);
102
87
  return true;
103
88
  } catch (error) {
104
89
  if (isNodeErrorWithCode(error) && error.code === "MODULE_NOT_FOUND") {
@@ -2,22 +2,26 @@
2
2
  const isFieldExpression = require("./index.cjs29.js");
3
3
  const serializeValue = require("./index.cjs18.js");
4
4
  function getMatchingKeys(field, selector) {
5
- if (selector[field] instanceof RegExp)
5
+ const fieldSelector = selector[field];
6
+ if (fieldSelector instanceof RegExp)
6
7
  return null;
7
- if (selector[field] != null) {
8
- if (isFieldExpression(selector[field])) {
9
- const is$in = isFieldExpression(selector[field]) && Array.isArray(selector[field].$in) && selector[field].$in.length > 0;
8
+ if (fieldSelector != null) {
9
+ if (isFieldExpression(fieldSelector)) {
10
+ const is$in = isFieldExpression(fieldSelector) && Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0;
10
11
  if (is$in) {
11
- const optimizedSelector = { ...selector, [field]: { ...selector[field] } };
12
+ const optimizedSelector = {
13
+ ...selector,
14
+ [field]: { ...fieldSelector }
15
+ };
12
16
  delete optimizedSelector[field].$in;
13
17
  if (Object.keys(optimizedSelector[field]).length === 0) {
14
18
  delete optimizedSelector[field];
15
19
  }
16
- return selector[field].$in.map(serializeValue);
20
+ return fieldSelector.$in.map(serializeValue);
17
21
  }
18
22
  return null;
19
23
  }
20
- return [serializeValue(selector[field])];
24
+ return [serializeValue(fieldSelector)];
21
25
  }
22
26
  return null;
23
27
  }
@@ -69,6 +69,7 @@ const _Collection = class _Collection extends EventEmitter {
69
69
  __publicField(this, "isDisposed", false);
70
70
  __publicField(this, "postBatchCallbacks", /* @__PURE__ */ new Set());
71
71
  __publicField(this, "fieldTracking", false);
72
+ __publicField(this, "persistenceReadyPromise");
72
73
  _Collection.collections.push(this);
73
74
  this.name = (options == null ? void 0 : options.name) ?? `${this.constructor.name}-${randomId()}`;
74
75
  this.options = {
@@ -221,6 +222,12 @@ const _Collection = class _Collection extends EventEmitter {
221
222
  this.emit("persistence.error", error instanceof Error ? error : new Error(error));
222
223
  });
223
224
  }
225
+ this.persistenceReadyPromise = new Promise((resolve, reject) => {
226
+ if (!this.persistenceAdapter)
227
+ return resolve();
228
+ this.once("persistence.init", resolve);
229
+ this.once("persistence.error", reject);
230
+ });
224
231
  _Collection.onCreationCallbacks.forEach((callback) => callback(this));
225
232
  }
226
233
  static getCollections() {
@@ -295,6 +302,22 @@ const _Collection = class _Collection extends EventEmitter {
295
302
  setFieldTracking(enable) {
296
303
  this.fieldTracking = enable;
297
304
  }
305
+ /**
306
+ * Resolves when the persistence adapter finished initializing
307
+ * and the collection is ready to be used.
308
+ * @returns A promise that resolves when the collection is ready.
309
+ * @example
310
+ * ```ts
311
+ * const collection = new Collection({
312
+ * persistence: // ...
313
+ * })
314
+ * await collection.isReady()
315
+ *
316
+ * collection.insert({ name: 'Item 1' })
317
+ */
318
+ async isReady() {
319
+ return this.persistenceReadyPromise;
320
+ }
298
321
  profile(fn, measureFunction) {
299
322
  if (!this.debugMode)
300
323
  return fn();
@@ -346,7 +369,8 @@ const _Collection = class _Collection extends EventEmitter {
346
369
  const indexInfo = this.getIndexInfo(selector);
347
370
  const items = indexInfo.matched ? indexInfo.positions.map((index2) => memory[index2]) : memory;
348
371
  const item = items.find((document) => match(document, selector));
349
- const index = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item) || memory.findIndex((document) => document === item);
372
+ const foundInIndex = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item);
373
+ const index = foundInIndex || memory.findIndex((document) => document === item);
350
374
  if (item == null)
351
375
  return { item: null, index: -1 };
352
376
  if (index === -1)
package/dist/index.mjs CHANGED
@@ -11,7 +11,10 @@ import { default as default10 } from "./index11.mjs";
11
11
  import { default as default11 } from "./index12.mjs";
12
12
  import { default as default12 } from "./index13.mjs";
13
13
  import { default as default13 } from "./index14.mjs";
14
- loadDeveloperTools();
14
+ /* istanbul ignore if -- @preserve */
15
+ if (process.env.NODE_ENV !== "production") {
16
+ loadDeveloperTools();
17
+ }
15
18
  export {
16
19
  default3 as AutoFetchCollection,
17
20
  default2 as Collection,
package/dist/index2.mjs CHANGED
@@ -10,6 +10,7 @@ Please move @signaldb/devtools to development dependencies to disable it in prod
10
10
 
11
11
 
12
12
  `;
13
+ const packageName = "@signaldb/devtools";
13
14
  function isNodeErrorWithCode(error) {
14
15
  return typeof error === "object" && error !== null && "code" in error && typeof error.code === "string";
15
16
  }
@@ -22,14 +23,17 @@ async function checkDevtoolsAvailability() {
22
23
  }
23
24
  if (isCommonJS()) {
24
25
  try {
25
- require.resolve("@signaldb/devtools");
26
+ require.resolve(packageName);
26
27
  return true;
27
28
  } catch {
28
29
  return false;
29
30
  }
30
31
  }
31
32
  try {
32
- await import("@signaldb/devtools");
33
+ await import(
34
+ /* @vite-ignore */
35
+ packageName
36
+ );
33
37
  return true;
34
38
  } catch (error) {
35
39
  if (!isNodeErrorWithCode(error) || error.code !== "MODULE_NOT_FOUND") {
@@ -40,7 +44,7 @@ async function checkDevtoolsAvailability() {
40
44
  const modulePackage = await import("./index15.mjs");
41
45
  const createRequireFunction = modulePackage.default.createRequire;
42
46
  const cjsRequire = createRequireFunction(import.meta.url);
43
- cjsRequire.resolve("@signaldb/devtools");
47
+ cjsRequire.resolve(packageName);
44
48
  return true;
45
49
  } catch {
46
50
  return false;
@@ -52,7 +56,7 @@ async function checkAndImportDevtools() {
52
56
  return false;
53
57
  if (isCommonJS()) {
54
58
  try {
55
- require("@signaldb/devtools");
59
+ require(packageName);
56
60
  return true;
57
61
  } catch (error) {
58
62
  if (isNodeErrorWithCode(error) && error.code === "MODULE_NOT_FOUND") {
@@ -62,7 +66,10 @@ async function checkAndImportDevtools() {
62
66
  }
63
67
  }
64
68
  try {
65
- await import("@signaldb/devtools");
69
+ await import(
70
+ /* @vite-ignore */
71
+ packageName
72
+ );
66
73
  return true;
67
74
  } catch (error) {
68
75
  if (!isNodeErrorWithCode(error) || error.code !== "MODULE_NOT_FOUND") {
@@ -73,7 +80,7 @@ async function checkAndImportDevtools() {
73
80
  const modulePackage = await import("./index15.mjs");
74
81
  const createRequireFunction = modulePackage.default.createRequire;
75
82
  const cjsRequire = createRequireFunction(import.meta.url);
76
- cjsRequire("@signaldb/devtools");
83
+ cjsRequire(packageName);
77
84
  return true;
78
85
  } catch (error) {
79
86
  if (isNodeErrorWithCode(error) && error.code === "MODULE_NOT_FOUND") {
package/dist/index28.mjs CHANGED
@@ -1,22 +1,26 @@
1
1
  import isFieldExpression from "./index29.mjs";
2
2
  import serializeValue from "./index18.mjs";
3
3
  function getMatchingKeys(field, selector) {
4
- if (selector[field] instanceof RegExp)
4
+ const fieldSelector = selector[field];
5
+ if (fieldSelector instanceof RegExp)
5
6
  return null;
6
- if (selector[field] != null) {
7
- if (isFieldExpression(selector[field])) {
8
- const is$in = isFieldExpression(selector[field]) && Array.isArray(selector[field].$in) && selector[field].$in.length > 0;
7
+ if (fieldSelector != null) {
8
+ if (isFieldExpression(fieldSelector)) {
9
+ const is$in = isFieldExpression(fieldSelector) && Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0;
9
10
  if (is$in) {
10
- const optimizedSelector = { ...selector, [field]: { ...selector[field] } };
11
+ const optimizedSelector = {
12
+ ...selector,
13
+ [field]: { ...fieldSelector }
14
+ };
11
15
  delete optimizedSelector[field].$in;
12
16
  if (Object.keys(optimizedSelector[field]).length === 0) {
13
17
  delete optimizedSelector[field];
14
18
  }
15
- return selector[field].$in.map(serializeValue);
19
+ return fieldSelector.$in.map(serializeValue);
16
20
  }
17
21
  return null;
18
22
  }
19
- return [serializeValue(selector[field])];
23
+ return [serializeValue(fieldSelector)];
20
24
  }
21
25
  return null;
22
26
  }
package/dist/index3.mjs CHANGED
@@ -68,6 +68,7 @@ const _Collection = class _Collection extends EventEmitter {
68
68
  __publicField(this, "isDisposed", false);
69
69
  __publicField(this, "postBatchCallbacks", /* @__PURE__ */ new Set());
70
70
  __publicField(this, "fieldTracking", false);
71
+ __publicField(this, "persistenceReadyPromise");
71
72
  _Collection.collections.push(this);
72
73
  this.name = (options == null ? void 0 : options.name) ?? `${this.constructor.name}-${randomId()}`;
73
74
  this.options = {
@@ -220,6 +221,12 @@ const _Collection = class _Collection extends EventEmitter {
220
221
  this.emit("persistence.error", error instanceof Error ? error : new Error(error));
221
222
  });
222
223
  }
224
+ this.persistenceReadyPromise = new Promise((resolve, reject) => {
225
+ if (!this.persistenceAdapter)
226
+ return resolve();
227
+ this.once("persistence.init", resolve);
228
+ this.once("persistence.error", reject);
229
+ });
223
230
  _Collection.onCreationCallbacks.forEach((callback) => callback(this));
224
231
  }
225
232
  static getCollections() {
@@ -294,6 +301,22 @@ const _Collection = class _Collection extends EventEmitter {
294
301
  setFieldTracking(enable) {
295
302
  this.fieldTracking = enable;
296
303
  }
304
+ /**
305
+ * Resolves when the persistence adapter finished initializing
306
+ * and the collection is ready to be used.
307
+ * @returns A promise that resolves when the collection is ready.
308
+ * @example
309
+ * ```ts
310
+ * const collection = new Collection({
311
+ * persistence: // ...
312
+ * })
313
+ * await collection.isReady()
314
+ *
315
+ * collection.insert({ name: 'Item 1' })
316
+ */
317
+ async isReady() {
318
+ return this.persistenceReadyPromise;
319
+ }
297
320
  profile(fn, measureFunction) {
298
321
  if (!this.debugMode)
299
322
  return fn();
@@ -345,7 +368,8 @@ const _Collection = class _Collection extends EventEmitter {
345
368
  const indexInfo = this.getIndexInfo(selector);
346
369
  const items = indexInfo.matched ? indexInfo.positions.map((index2) => memory[index2]) : memory;
347
370
  const item = items.find((document) => match(document, selector));
348
- const index = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item) || memory.findIndex((document) => document === item);
371
+ const foundInIndex = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item);
372
+ const index = foundInIndex || memory.findIndex((document) => document === item);
349
373
  if (item == null)
350
374
  return { item: null, index: -1 };
351
375
  if (index === -1)
@@ -29,10 +29,17 @@ export interface FieldExpression<T> {
29
29
  $bitsAnyClear?: any;
30
30
  $bitsAnySet?: any;
31
31
  }
32
+ type IsObject<T> = T extends object ? (T extends Array<any> ? false : true) : false;
33
+ type DotNotationKeys<T> = {
34
+ [K in keyof T & (string | number)]: T[K] extends Array<infer U> ? `${K}` | `${K}.$` | `${K}.${DotNotationKeys<U>}` : IsObject<T[K]> extends true ? `${K}` | `${K}.${DotNotationKeys<T[K]>}` : `${K}`;
35
+ }[keyof T & (string | number)];
36
+ type Split<S extends string, Delimiter extends string> = S extends `${infer Head}${Delimiter}${infer Tail}` ? [Head, ...Split<Tail, Delimiter>] : [S];
37
+ type GetTypeByParts<T, Parts extends readonly string[]> = Parts extends [infer Head, ...infer Tail] ? Head extends keyof T ? GetTypeByParts<T[Head], Extract<Tail, string[]>> : Head extends '$' ? T extends Array<infer U> ? GetTypeByParts<U, Extract<Tail, string[]>> : never : never : T;
38
+ type Get<T, Path extends string> = GetTypeByParts<T, Split<Path, '.'>>;
32
39
  type Flatten<T> = T extends any[] ? T[0] : T;
33
40
  type FlatQuery<T> = {
34
- [P in keyof T]?: Flatten<T[P]> | RegExp | FieldExpression<Flatten<T[P]>>;
35
- } & Record<string, any>;
41
+ [P in DotNotationKeys<T>]?: Flatten<Get<T, P>> | RegExp | FieldExpression<Flatten<Get<T, P>>>;
42
+ };
36
43
  type Query<T> = FlatQuery<T> & {
37
44
  $or?: Query<T>[] | undefined;
38
45
  $and?: Query<T>[] | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signaldb/core",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON interface to places such as localStorage.",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && vite build"