inibase 1.0.0-rc.114 → 1.0.0-rc.116

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/file.d.ts CHANGED
@@ -107,7 +107,7 @@ export declare const remove: (filePath: string, linesToDelete: number | number[]
107
107
  *
108
108
  * Note: Decodes each line for comparison and can handle complex queries with multiple conditions.
109
109
  */
110
- export declare const search: (filePath: string, operator: ComparisonOperator | ComparisonOperator[], comparedAtValue: string | number | boolean | null | (string | number | boolean | null)[], logicalOperator?: "and" | "or", fieldType?: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[] | Schema, limit?: number, offset?: number, readWholeFile?: boolean, secretKey?: string | Buffer) => Promise<[Record<number, string | number | boolean | null | (string | number | boolean | null)[]> | null, number, Set<number> | null]>;
110
+ export declare const search: (filePath: string, operator: ComparisonOperator | ComparisonOperator[], comparedAtValue: string | number | boolean | null | (string | number | boolean | null)[], logicalOperator?: "and" | "or", searchIn?: Set<number>, fieldType?: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[] | Schema, limit?: number, offset?: number, readWholeFile?: boolean, secretKey?: string | Buffer) => Promise<[Record<number, string | number | boolean | null | (string | number | boolean | null)[]> | null, number, Set<number> | null]>;
111
111
  /**
112
112
  * Asynchronously calculates the sum of numerical values from specified lines in a file.
113
113
  *
package/dist/file.js CHANGED
@@ -505,7 +505,7 @@ export const remove = async (filePath, linesToDelete) => {
505
505
  *
506
506
  * Note: Decodes each line for comparison and can handle complex queries with multiple conditions.
507
507
  */
508
- export const search = async (filePath, operator, comparedAtValue, logicalOperator, fieldType, fieldChildrenType, limit, offset, readWholeFile, secretKey) => {
508
+ export const search = async (filePath, operator, comparedAtValue, logicalOperator, searchIn, fieldType, fieldChildrenType, limit, offset, readWholeFile, secretKey) => {
509
509
  // Initialize a Map to store the matching lines with their line numbers.
510
510
  const matchingLines = {};
511
511
  // Initialize counters for line number, found items, and processed items.
@@ -521,6 +521,9 @@ export const search = async (filePath, operator, comparedAtValue, logicalOperato
521
521
  for await (const line of rl) {
522
522
  // Increment the line count for each line.
523
523
  linesCount++;
524
+ // Search only in provided linesNumbers
525
+ if (searchIn && !searchIn.has(linesCount))
526
+ continue;
524
527
  // Decode the line for comparison.
525
528
  const decodedLine = decode(line, fieldType, fieldChildrenType, secretKey);
526
529
  // Check if the line meets the specified conditions based on comparison and logical operators.
package/dist/index.js CHANGED
@@ -401,7 +401,7 @@ export default class Inibase {
401
401
  const field = Utils.getField(key, schema);
402
402
  if (!field)
403
403
  continue;
404
- const [searchResult, totalLines] = await File.search(join(tablePath, `${key}${this.getFileExtension(tableName)}`), Array.isArray(values) ? "=" : "[]", values, undefined, field.type, field.children, 1, undefined, false, this.salt);
404
+ const [searchResult, totalLines] = await File.search(join(tablePath, `${key}${this.getFileExtension(tableName)}`), Array.isArray(values) ? "=" : "[]", values, undefined, undefined, field.type, field.children, 1, undefined, false, this.salt);
405
405
  if (searchResult && totalLines > 0)
406
406
  throw this.Error("FIELD_UNIQUE", [
407
407
  field.key,
@@ -743,30 +743,24 @@ export default class Inibase {
743
743
  }
744
744
  }
745
745
  }
746
- async applyCriteria(tableName, schema, options, criteria, allTrue) {
746
+ async applyCriteria(tableName, schema, options, criteria, allTrue, searchIn) {
747
747
  const tablePath = join(this.databasePath, tableName);
748
- let RETURN = {}, RETURN_LineNumbers = null;
748
+ let RETURN = {};
749
749
  if (!criteria)
750
750
  return [null, null];
751
751
  if (criteria.and && Utils.isObject(criteria.and)) {
752
- const [searchResult, lineNumbers] = await this.applyCriteria(tableName, schema, options, criteria.and, true);
752
+ const [searchResult, lineNumbers] = await this.applyCriteria(tableName, schema, options, criteria.and, true, searchIn);
753
753
  if (searchResult) {
754
- RETURN = Utils.deepMerge(RETURN, Object.fromEntries(Object.entries(searchResult).filter(([_k, v], _i) => Object.keys(v).length ===
755
- Object.keys(criteria.and ?? {}).length)));
754
+ RETURN = Utils.deepMerge(RETURN, Object.fromEntries(Object.entries(searchResult).filter(([_k, v], _i) => Object.keys(v).filter((key) => Object.keys(criteria.and).includes(key)).length)));
756
755
  delete criteria.and;
757
- RETURN_LineNumbers = lineNumbers;
756
+ searchIn = lineNumbers;
758
757
  }
759
758
  else
760
759
  return [null, null];
761
760
  }
762
- if (criteria.or && Utils.isObject(criteria.or)) {
763
- const [searchResult, lineNumbers] = await this.applyCriteria(tableName, schema, options, criteria.or, false);
761
+ const criteriaOR = criteria.or;
762
+ if (criteriaOR)
764
763
  delete criteria.or;
765
- if (searchResult) {
766
- RETURN = Utils.deepMerge(RETURN, searchResult);
767
- RETURN_LineNumbers = lineNumbers;
768
- }
769
- }
770
764
  if (Object.keys(criteria).length > 0) {
771
765
  if (allTrue === undefined)
772
766
  allTrue = true;
@@ -828,7 +822,7 @@ export default class Inibase {
828
822
  searchOperator = "=";
829
823
  searchComparedAtValue = value;
830
824
  }
831
- const [searchResult, totalLines, linesNumbers] = await File.search(join(tablePath, `${key}${this.getFileExtension(tableName)}`), searchOperator ?? "=", searchComparedAtValue ?? null, searchLogicalOperator, field?.type, field?.children, options.perPage, (options.page - 1) * options.perPage + 1, true, this.salt);
825
+ const [searchResult, totalLines, linesNumbers] = await File.search(join(tablePath, `${key}${this.getFileExtension(tableName)}`), searchOperator ?? "=", searchComparedAtValue ?? null, searchLogicalOperator, allTrue ? searchIn : undefined, field?.type, field?.children, options.perPage, (options.page - 1) * options.perPage + 1, true, this.salt);
832
826
  if (searchResult) {
833
827
  RETURN = Utils.deepMerge(RETURN, Object.fromEntries(Object.entries(searchResult).map(([id, value]) => [
834
828
  id,
@@ -837,18 +831,32 @@ export default class Inibase {
837
831
  },
838
832
  ])));
839
833
  this.totalItems[`${tableName}-${key}`] = totalLines;
840
- RETURN_LineNumbers = linesNumbers;
841
- }
842
- if (allTrue && index > 0) {
843
- if (!Object.keys(RETURN).length)
844
- RETURN = {};
845
- RETURN = Object.fromEntries(Object.entries(RETURN).filter(([_index, item]) => Object.keys(item).length > index));
846
- if (!Object.keys(RETURN).length)
847
- RETURN = {};
834
+ if (linesNumbers?.size) {
835
+ if (searchIn) {
836
+ for (const lineNumber of linesNumbers)
837
+ searchIn.add(lineNumber);
838
+ }
839
+ else
840
+ searchIn = linesNumbers;
841
+ }
848
842
  }
843
+ else if (allTrue)
844
+ return [null, null];
845
+ }
846
+ }
847
+ if (criteriaOR && Utils.isObject(criteriaOR)) {
848
+ const [searchResult, lineNumbers] = await this.applyCriteria(tableName, schema, options, criteriaOR, false, searchIn);
849
+ if (searchResult) {
850
+ RETURN = Utils.deepMerge(RETURN, searchResult);
851
+ if (!Object.keys(RETURN).length)
852
+ RETURN = {};
853
+ RETURN = Object.fromEntries(Object.entries(RETURN).filter(([_index, item]) => Object.keys(item).filter((key) => Object.keys(criteriaOR).includes(key)).length));
854
+ if (!Object.keys(RETURN).length)
855
+ RETURN = {};
856
+ searchIn = lineNumbers;
849
857
  }
850
858
  }
851
- return [Object.keys(RETURN).length ? RETURN : null, RETURN_LineNumbers];
859
+ return [Object.keys(RETURN).length ? RETURN : null, searchIn];
852
860
  }
853
861
  _filterSchemaByColumns(schema, columns) {
854
862
  return schema
@@ -949,7 +957,7 @@ export default class Inibase {
949
957
  if (!(await File.isExists(path)))
950
958
  return null;
951
959
  // Construct the paste command to merge files and filter lines by IDs
952
- const pasteCommand = `paste ${filesPathes.join(" ")}`;
960
+ const pasteCommand = `paste '${filesPathes.join("' '")}'`;
953
961
  // Construct the sort command dynamically based on the number of files for sorting
954
962
  const index = 2;
955
963
  const sortColumns = sortArray
@@ -962,15 +970,15 @@ export default class Inibase {
962
970
  return "";
963
971
  })
964
972
  .join(" ");
965
- const sortCommand = `sort ${sortColumns} -T=${join(tablePath, ".tmp")}`;
973
+ const sortCommand = `sort ${sortColumns} -T='${join(tablePath, ".tmp")}'`;
966
974
  try {
967
975
  if (cacheKey)
968
976
  await File.lock(join(tablePath, ".tmp"), cacheKey);
969
977
  // Combine && Execute the commands synchronously
970
978
  let lines = (await UtilsServer.exec(this.tables[tableName].config.cache
971
979
  ? (await File.isExists(join(tablePath, ".cache", `${cacheKey}${this.fileExtension}`)))
972
- ? `${awkCommand} ${join(tablePath, ".cache", `${cacheKey}${this.fileExtension}`)}`
973
- : `${pasteCommand} | ${sortCommand} -o ${join(tablePath, ".cache", `${cacheKey}${this.fileExtension}`)} && ${awkCommand} ${join(tablePath, ".cache", `${cacheKey}${this.fileExtension}`)}`
980
+ ? `${awkCommand} '${join(tablePath, ".cache", `${cacheKey}${this.fileExtension}`)}'`
981
+ : `${pasteCommand} | ${sortCommand} -o '${join(tablePath, ".cache", `${cacheKey}${this.fileExtension}`)}' && ${awkCommand} '${join(tablePath, ".cache", `${cacheKey}${this.fileExtension}`)}'`
974
982
  : `${pasteCommand} | ${sortCommand} | ${awkCommand}`, {
975
983
  encoding: "utf-8",
976
984
  })).stdout
@@ -1035,7 +1043,7 @@ export default class Inibase {
1035
1043
  let Ids = where;
1036
1044
  if (!Array.isArray(Ids))
1037
1045
  Ids = [Ids];
1038
- const [lineNumbers, countItems] = await File.search(join(tablePath, `id${this.getFileExtension(tableName)}`), "[]", Ids.map((id) => Utils.isNumber(id) ? Number(id) : UtilsServer.decodeID(id, this.salt)), undefined, "number", undefined, Ids.length, 0, !this.totalItems[`${tableName}-*`], this.salt);
1046
+ const [lineNumbers, countItems] = await File.search(join(tablePath, `id${this.getFileExtension(tableName)}`), "[]", Ids.map((id) => Utils.isNumber(id) ? Number(id) : UtilsServer.decodeID(id, this.salt)), undefined, undefined, "number", undefined, Ids.length, 0, !this.totalItems[`${tableName}-*`], this.salt);
1039
1047
  if (!lineNumbers)
1040
1048
  return null;
1041
1049
  if (!this.totalItems[`${tableName}-*`])
@@ -210,7 +210,8 @@ export const isEqual = (originalValue, comparedValue, fieldType) => {
210
210
  case "boolean":
211
211
  return Number(originalValue) === Number(comparedValue);
212
212
  default:
213
- return originalValue == comparedValue;
213
+ return ((!String(comparedValue).length && originalValue === undefined) ||
214
+ originalValue == comparedValue);
214
215
  }
215
216
  };
216
217
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.0.0-rc.114",
3
+ "version": "1.0.0-rc.116",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Karim Amahtil",