@pixagram/lacerta-db 0.0.1 → 0.0.2

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.
Files changed (2) hide show
  1. package/index.js +87 -41
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -410,6 +410,43 @@ class IndexedDBUtility {
410
410
  request.onerror = event => reject(`Cursor iteration failed: ${event.target.error.message}`);
411
411
  });
412
412
  }
413
+
414
+ static iterateCursorSafe(store, processCallback, options = {}) {
415
+ return new Promise((resolve, reject) => {
416
+ const {
417
+ index = null,
418
+ direction = 'next',
419
+ range = null
420
+ } = options;
421
+
422
+ let source = store;
423
+ if (index && store.indexNames.contains(index)) {
424
+ source = store.index(index);
425
+ }
426
+
427
+ const request = source.openCursor(range, direction);
428
+ const results = [];
429
+
430
+ request.onsuccess = (event) => {
431
+ const cursor = event.target.result;
432
+
433
+ if (cursor) {
434
+ // FIXED: No async operations in cursor callback
435
+ const shouldContinue = processCallback(cursor.value, cursor.key, results);
436
+
437
+ if (shouldContinue !== false) {
438
+ cursor.continue();
439
+ } else {
440
+ resolve(results);
441
+ }
442
+ } else {
443
+ resolve(results);
444
+ }
445
+ };
446
+
447
+ request.onerror = () => reject(request.error);
448
+ });
449
+ }
413
450
 
414
451
  toString() {
415
452
  return '[IndexedDBUtility]';
@@ -1936,10 +1973,9 @@ export class Collection {
1936
1973
  index = null
1937
1974
  } = options;
1938
1975
 
1939
- const results = [];
1940
- let count = 0;
1941
- let skipped = 0;
1942
-
1976
+ // FIXED: Collect raw document data first, then process async operations after transaction
1977
+ const rawDocuments = [];
1978
+
1943
1979
  await IndexedDBUtility.performTransaction(
1944
1980
  this.database.db,
1945
1981
  this.name,
@@ -1956,56 +1992,28 @@ export class Collection {
1956
1992
  : source.openCursor();
1957
1993
 
1958
1994
  return new Promise((resolve, reject) => {
1959
- request.onsuccess = async (event) => {
1995
+ let count = 0;
1996
+ let skipped = 0;
1997
+
1998
+ request.onsuccess = (event) => {
1960
1999
  const cursor = event.target.result;
1961
2000
 
1962
2001
  if (!cursor || count >= limit) {
1963
- resolve(results);
2002
+ resolve();
1964
2003
  return;
1965
2004
  }
1966
2005
 
1967
- const docData = cursor.value;
1968
-
1969
2006
  if (skipped < offset) {
1970
2007
  skipped++;
1971
2008
  cursor.continue();
1972
2009
  return;
1973
2010
  }
1974
2011
 
1975
- let document;
1976
- if (Document.isEncrypted(docData)) {
1977
- if (!encryptionKey) {
1978
- cursor.continue();
1979
- return;
1980
- }
1981
- document = new Document(docData, encryptionKey);
1982
- } else {
1983
- document = new Document(docData);
1984
- }
1985
-
1986
- if (Object.keys(filter).length > 0) {
1987
- const object = await document.objectOutput();
1988
- let match = true;
1989
-
1990
- for (const key in filter) {
1991
- const value = key.includes('.')
1992
- ? getNestedValue(object.data, key)
1993
- : object.data[key];
1994
-
1995
- if (value !== filter[key]) {
1996
- match = false;
1997
- break;
1998
- }
1999
- }
2000
-
2001
- if (!match) {
2002
- cursor.continue();
2003
- return;
2004
- }
2005
- }
2006
-
2007
- results.push(await document.objectOutput());
2012
+ // FIXED: Just collect raw data, no async operations here
2013
+ const docData = cursor.value;
2014
+ rawDocuments.push(docData);
2008
2015
  count++;
2016
+
2009
2017
  cursor.continue();
2010
2018
  };
2011
2019
 
@@ -2014,6 +2022,44 @@ export class Collection {
2014
2022
  }
2015
2023
  );
2016
2024
 
2025
+ // FIXED: Process documents after transaction is complete
2026
+ const results = [];
2027
+
2028
+ for (const docData of rawDocuments) {
2029
+ let document;
2030
+ if (Document.isEncrypted(docData)) {
2031
+ if (!encryptionKey) {
2032
+ continue; // Skip encrypted documents without key
2033
+ }
2034
+ document = new Document(docData, encryptionKey);
2035
+ } else {
2036
+ document = new Document(docData);
2037
+ }
2038
+
2039
+ // Apply filters if any
2040
+ if (Object.keys(filter).length > 0) {
2041
+ const object = await document.objectOutput();
2042
+ let match = true;
2043
+
2044
+ for (const key in filter) {
2045
+ const value = key.includes('.')
2046
+ ? getNestedValue(object.data, key)
2047
+ : object.data[key];
2048
+
2049
+ if (value !== filter[key]) {
2050
+ match = false;
2051
+ break;
2052
+ }
2053
+ }
2054
+
2055
+ if (!match) {
2056
+ continue;
2057
+ }
2058
+ }
2059
+
2060
+ results.push(await document.objectOutput());
2061
+ }
2062
+
2017
2063
  return results;
2018
2064
  }
2019
2065
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixagram/lacerta-db",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Lacerta-DB is a Javascript IndexedDB Database for Web Browsers. Simple, Fast, Secure.",
5
5
  "devDependencies": {
6
6
  "@babel/core": "^7.23.6",