jexidb 1.0.4 → 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
@@ -584,7 +584,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
584
584
  if (map instanceof Set) {
585
585
  map = _toConsumableArray(map);
586
586
  } else if (map && _typeof(map) === 'object') {
587
- map = _toConsumableArray(_this.indexManager.query(map, options.matchAny));
587
+ map = _toConsumableArray(_this.indexManager.query(map, options));
588
588
  } else {
589
589
  map = _toConsumableArray(Array(_this.offsets.length).keys());
590
590
  }
@@ -729,7 +729,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
729
729
  return _context9.abrupt("return", results);
730
730
  case 19:
731
731
  _context9.next = 21;
732
- return this.indexManager.query(criteria, options.matchAny);
732
+ return this.indexManager.query(criteria, options);
733
733
  case 21:
734
734
  matchingLines = _context9.sent;
735
735
  if (!(!matchingLines || !matchingLines.size)) {
@@ -801,7 +801,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
801
801
  return this.save()["catch"](console.error);
802
802
  case 11:
803
803
  _context11.next = 13;
804
- return this.indexManager.query(criteria, options.matchAny);
804
+ return this.indexManager.query(criteria, options);
805
805
  case 13:
806
806
  matchingLines = _context11.sent;
807
807
  if (!(!matchingLines || !matchingLines.size)) {
@@ -944,7 +944,7 @@ var Database = exports.Database = /*#__PURE__*/function (_EventEmitter) {
944
944
  return this.save()["catch"](console.error);
945
945
  case 11:
946
946
  _context12.next = 13;
947
- return this.indexManager.query(criteria, options.matchAny);
947
+ return this.indexManager.query(criteria, options);
948
948
  case 13:
949
949
  matchingLines = _context12.sent;
950
950
  if (!(!matchingLines || !matchingLines.size)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jexidb",
3
- "version": "1.0.4",
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",
package/src/Database.mjs CHANGED
@@ -217,7 +217,7 @@ export class Database extends EventEmitter {
217
217
  if (map instanceof Set) {
218
218
  map = [...map]
219
219
  } else if(map && typeof map === 'object') {
220
- map = [...this.indexManager.query(map, options.matchAny)]
220
+ map = [...this.indexManager.query(map, options)]
221
221
  } else {
222
222
  map = [...Array(this.offsets.length).keys()]
223
223
  }
@@ -268,7 +268,7 @@ export class Database extends EventEmitter {
268
268
  }
269
269
  return results
270
270
  } else {
271
- const matchingLines = await this.indexManager.query(criteria, options.matchAny)
271
+ const matchingLines = await this.indexManager.query(criteria, options)
272
272
  if (!matchingLines || !matchingLines.size) {
273
273
  return []
274
274
  }
@@ -284,7 +284,7 @@ export class Database extends EventEmitter {
284
284
  if(this.destroyed) throw new Error('Database is destroyed')
285
285
  if(!this.initialized) await this.init()
286
286
  this.shouldSave && await this.save().catch(console.error)
287
- const matchingLines = await this.indexManager.query(criteria, options.matchAny)
287
+ const matchingLines = await this.indexManager.query(criteria, options)
288
288
  if (!matchingLines || !matchingLines.size) {
289
289
  return []
290
290
  }
@@ -331,7 +331,7 @@ export class Database extends EventEmitter {
331
331
  if(this.destroyed) throw new Error('Database is destroyed')
332
332
  if(!this.initialized) await this.init()
333
333
  this.shouldSave && await this.save().catch(console.error)
334
- const matchingLines = await this.indexManager.query(criteria, options.matchAny)
334
+ const matchingLines = await this.indexManager.query(criteria, options)
335
335
  if (!matchingLines || !matchingLines.size) {
336
336
  return 0
337
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
package/test/test-v8.jdb CHANGED
Binary file