jexidb 1.0.3 → 1.0.5

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.
package/dist/Database.cjs CHANGED
@@ -581,8 +581,10 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
581
581
  return _context8.abrupt("return");
582
582
  case 11:
583
583
  if (!Array.isArray(map)) {
584
- if (map && _typeof(map) === 'object') {
585
- map = _this.indexManager.query(map, options.matchAny);
584
+ if (map instanceof Set) {
585
+ map = _toConsumableArray(map);
586
+ } else if (map && _typeof(map) === 'object') {
587
+ map = _toConsumableArray(_this.indexManager.query(map, options));
586
588
  } else {
587
589
  map = _toConsumableArray(Array(_this.offsets.length).keys());
588
590
  }
@@ -727,7 +729,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
727
729
  return _context9.abrupt("return", results);
728
730
  case 19:
729
731
  _context9.next = 21;
730
- return this.indexManager.query(criteria, options.matchAny);
732
+ return this.indexManager.query(criteria, options);
731
733
  case 21:
732
734
  matchingLines = _context9.sent;
733
735
  if (!(!matchingLines || !matchingLines.size)) {
@@ -799,7 +801,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
799
801
  return this.save()["catch"](console.error);
800
802
  case 11:
801
803
  _context11.next = 13;
802
- return this.indexManager.query(criteria, options.matchAny);
804
+ return this.indexManager.query(criteria, options);
803
805
  case 13:
804
806
  matchingLines = _context11.sent;
805
807
  if (!(!matchingLines || !matchingLines.size)) {
@@ -942,7 +944,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
942
944
  return this.save()["catch"](console.error);
943
945
  case 11:
944
946
  _context12.next = 13;
945
- return this.indexManager.query(criteria, options.matchAny);
947
+ return this.indexManager.query(criteria, options);
946
948
  case 13:
947
949
  matchingLines = _context12.sent;
948
950
  if (!(!matchingLines || !matchingLines.size)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jexidb",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "JexiDB is a pure JS NPM library for managing data on disk using JSONL efficiently, without the need for a server.",
5
5
  "main": "./dist/Database.cjs",
6
6
  "module": "./src/Database.mjs",
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "scripts": {
14
14
  "test": "node --expose-gc test/test.mjs && exit 1",
15
- "build": "npx babel src/Database.mjs --plugins @babel/plugin-transform-async-generator-functions --out-file-extension .cjs --out-dir dist"
15
+ "prepare": "npx babel src/Database.mjs --plugins @babel/plugin-transform-async-generator-functions --out-file-extension .cjs --out-dir dist"
16
16
  },
17
17
  "author": "EdenwareApps",
18
18
  "license": "MIT",
package/src/Database.mjs CHANGED
@@ -214,8 +214,10 @@ export class Database extends EventEmitter {
214
214
  this.shouldSave && await this.save().catch(console.error)
215
215
  if(this.indexOffset === 0) return
216
216
  if(!Array.isArray(map)) {
217
- if(map && typeof map === 'object') {
218
- map = this.indexManager.query(map, options.matchAny)
217
+ if (map instanceof Set) {
218
+ map = [...map]
219
+ } else if(map && typeof map === 'object') {
220
+ map = [...this.indexManager.query(map, options)]
219
221
  } else {
220
222
  map = [...Array(this.offsets.length).keys()]
221
223
  }
@@ -266,7 +268,7 @@ export class Database extends EventEmitter {
266
268
  }
267
269
  return results
268
270
  } else {
269
- const matchingLines = await this.indexManager.query(criteria, options.matchAny)
271
+ const matchingLines = await this.indexManager.query(criteria, options)
270
272
  if (!matchingLines || !matchingLines.size) {
271
273
  return []
272
274
  }
@@ -282,7 +284,7 @@ export class Database extends EventEmitter {
282
284
  if(this.destroyed) throw new Error('Database is destroyed')
283
285
  if(!this.initialized) await this.init()
284
286
  this.shouldSave && await this.save().catch(console.error)
285
- const matchingLines = await this.indexManager.query(criteria, options.matchAny)
287
+ const matchingLines = await this.indexManager.query(criteria, options)
286
288
  if (!matchingLines || !matchingLines.size) {
287
289
  return []
288
290
  }
@@ -329,7 +331,7 @@ export class Database extends EventEmitter {
329
331
  if(this.destroyed) throw new Error('Database is destroyed')
330
332
  if(!this.initialized) await this.init()
331
333
  this.shouldSave && await this.save().catch(console.error)
332
- const matchingLines = await this.indexManager.query(criteria, options.matchAny)
334
+ const matchingLines = await this.indexManager.query(criteria, options)
333
335
  if (!matchingLines || !matchingLines.size) {
334
336
  return 0
335
337
  }
@@ -83,23 +83,29 @@ export default class IndexManager {
83
83
  }
84
84
  }
85
85
 
86
- query(criteria, matchAny=false) {
87
- if (!criteria) throw new Error('No query criteria provided')
88
- const fields = Object.keys(criteria)
89
- if (!fields.length) throw new Error('No valid query criteria provided')
90
- let matchingLines = matchAny ? new Set() : null
86
+ query(criteria, options = {}) {
87
+ if (typeof options === 'boolean') {
88
+ options = { matchAny: options };
89
+ }
90
+ const { matchAny = false, caseInsensitive = false } = options;
91
+ if (!criteria) throw new Error('No query criteria provided');
92
+ const fields = Object.keys(criteria);
93
+ if (!fields.length) throw new Error('No valid query criteria provided');
94
+ let matchingLines = matchAny ? new Set() : null;
95
+
91
96
  for (const field of fields) {
92
- if (typeof(this.index.data[field]) == 'undefined') continue
93
- const criteriaValue = criteria[field]
94
- let lineNumbersForField = new Set()
95
- const isNumericField = this.opts.indexes[field] === 'number'
96
- if (typeof(criteriaValue) === 'object' && !Array.isArray(criteriaValue)) {
97
+ if (typeof this.index.data[field] === 'undefined') continue;
98
+ const criteriaValue = criteria[field];
99
+ let lineNumbersForField = new Set();
100
+ const isNumericField = this.opts.indexes[field] === 'number';
101
+
102
+ if (typeof criteriaValue === 'object' && !Array.isArray(criteriaValue)) {
97
103
  const fieldIndex = this.index.data[field];
98
104
  for (const value in fieldIndex) {
99
- let includeValue = true
105
+ let includeValue = true;
100
106
  if (isNumericField) {
101
107
  const numericValue = parseFloat(value);
102
- if (!isNaN(numericValue)) {
108
+ if (!isNaN(numericValue)) {
103
109
  if (criteriaValue['>'] !== undefined && numericValue <= criteriaValue['>']) {
104
110
  includeValue = false;
105
111
  }
@@ -113,7 +119,9 @@ export default class IndexManager {
113
119
  includeValue = false;
114
120
  }
115
121
  if (criteriaValue['!='] !== undefined) {
116
- const excludeValues = Array.isArray(criteriaValue['!=']) ? criteriaValue['!='] : [criteriaValue['!=']];
122
+ const excludeValues = Array.isArray(criteriaValue['!='])
123
+ ? criteriaValue['!=']
124
+ : [criteriaValue['!=']];
117
125
  if (excludeValues.includes(numericValue)) {
118
126
  includeValue = false;
119
127
  }
@@ -121,24 +129,45 @@ export default class IndexManager {
121
129
  }
122
130
  } else {
123
131
  if (criteriaValue['contains'] !== undefined && typeof value === 'string') {
124
- if (!value.includes(criteriaValue['contains'])) {
125
- includeValue = false;
132
+ const term = String(criteriaValue['contains']);
133
+ if (caseInsensitive) {
134
+ if (!value.toLowerCase().includes(term.toLowerCase())) {
135
+ includeValue = false;
136
+ }
137
+ } else {
138
+ if (!value.includes(term)) {
139
+ includeValue = false;
140
+ }
126
141
  }
127
142
  }
128
- if (criteriaValue['regex'] !== undefined && typeof value === 'string') {
129
- const regex = new RegExp(criteriaValue['regex']);
130
- if (!regex.test(value)) {
143
+ if (criteriaValue['regex'] !== undefined) {
144
+ let regex;
145
+ if (typeof criteriaValue['regex'] === 'string') {
146
+ regex = new RegExp(criteriaValue['regex'], caseInsensitive ? 'i' : '');
147
+ } else if (criteriaValue['regex'] instanceof RegExp) {
148
+ if (caseInsensitive && !criteriaValue['regex'].ignoreCase) {
149
+ const flags = criteriaValue['regex'].flags.includes('i')
150
+ ? criteriaValue['regex'].flags
151
+ : criteriaValue['regex'].flags + 'i';
152
+ regex = new RegExp(criteriaValue['regex'].source, flags);
153
+ } else {
154
+ regex = criteriaValue['regex'];
155
+ }
156
+ }
157
+ if (regex && !regex.test(value)) {
131
158
  includeValue = false;
132
159
  }
133
160
  }
134
161
  if (criteriaValue['!='] !== undefined) {
135
- const excludeValues = Array.isArray(criteriaValue['!=']) ? criteriaValue['!='] : [criteriaValue['!=']];
162
+ const excludeValues = Array.isArray(criteriaValue['!='])
163
+ ? criteriaValue['!=']
164
+ : [criteriaValue['!=']];
136
165
  if (excludeValues.includes(value)) {
137
166
  includeValue = false;
138
167
  }
139
168
  }
140
169
  }
141
-
170
+
142
171
  if (includeValue) {
143
172
  for (const lineNumber of fieldIndex[value]) {
144
173
  lineNumbersForField.add(lineNumber);
@@ -146,31 +175,46 @@ export default class IndexManager {
146
175
  }
147
176
  }
148
177
  } else {
178
+ // Comparação simples de igualdade
149
179
  const values = Array.isArray(criteriaValue) ? criteriaValue : [criteriaValue];
150
- for (const value of values) {
151
- if (this.index.data[field][value]) {
152
- for (const lineNumber of this.index.data[field][value]) {
153
- lineNumbersForField.add(lineNumber);
180
+ const fieldData = this.index.data[field];
181
+ for (const searchValue of values) {
182
+ for (const key in fieldData) {
183
+ let match = false;
184
+ if (isNumericField) {
185
+ // Converter ambas as partes para número
186
+ match = Number(key) === Number(searchValue);
187
+ } else {
188
+ match = caseInsensitive
189
+ ? key.toLowerCase() === String(searchValue).toLowerCase()
190
+ : key === searchValue;
191
+ }
192
+ if (match) {
193
+ for (const lineNumber of fieldData[key]) {
194
+ lineNumbersForField.add(lineNumber);
195
+ }
154
196
  }
155
197
  }
156
198
  }
157
199
  }
200
+
201
+ // Consolida os resultados de cada campo
158
202
  if (matchAny) {
159
203
  matchingLines = new Set([...matchingLines, ...lineNumbersForField]);
160
204
  } else {
161
205
  if (matchingLines === null) {
162
- matchingLines = lineNumbersForField
206
+ matchingLines = lineNumbersForField;
163
207
  } else {
164
208
  matchingLines = new Set([...matchingLines].filter(n => lineNumbersForField.has(n)));
165
209
  }
166
210
  if (!matchingLines.size) {
167
- return new Set()
211
+ return new Set();
168
212
  }
169
213
  }
170
214
  }
171
215
  return matchingLines || new Set();
172
- }
173
-
216
+ }
217
+
174
218
  load(index) {
175
219
  for(const field in index.data) {
176
220
  for(const term in index.data[field]) {
Binary file
Binary file
Binary file