inibase 1.0.0-rc.30 → 1.0.0-rc.31

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.js CHANGED
@@ -30,12 +30,16 @@ const _pipeline = async (rl, writeStream, transform) => {
30
30
  * @returns A readline.Interface instance configured with the provided file stream.
31
31
  */
32
32
  const readLineInternface = (fileHandle) => {
33
- return createInterface({
34
- input: Config.isCompressionEnabled
35
- ? fileHandle.createReadStream().pipe(createGunzip())
36
- : fileHandle.createReadStream(),
37
- crlfDelay: Infinity,
38
- });
33
+ const [major, minor, patch] = process.versions.node.split(".").map(Number);
34
+ return major > 18 ||
35
+ (major === 18 && minor >= 11 && !Config.isCompressionEnabled)
36
+ ? fileHandle.readLines()
37
+ : createInterface({
38
+ input: Config.isCompressionEnabled
39
+ ? fileHandle.createReadStream().pipe(createGunzip())
40
+ : fileHandle.createReadStream(),
41
+ crlfDelay: Infinity,
42
+ });
39
43
  };
40
44
  /**
41
45
  * Checks if a file or directory exists at the specified path.
@@ -190,7 +194,6 @@ const decodeHelper = (value, fieldType, fieldChildrenType, secretKey) => {
190
194
  if (Array.isArray(value) && fieldType !== "array")
191
195
  return value.map((v) => decodeHelper(v, fieldType, fieldChildrenType, secretKey));
192
196
  switch (fieldType) {
193
- case "table":
194
197
  case "number":
195
198
  return isNumber(value) ? Number(value) : null;
196
199
  case "boolean":
@@ -204,6 +207,7 @@ const decodeHelper = (value, fieldType, fieldChildrenType, secretKey) => {
204
207
  ? detectFieldType(v, fieldChildrenType)
205
208
  : fieldChildrenType, undefined, secretKey))
206
209
  : value;
210
+ case "table":
207
211
  case "id":
208
212
  return isNumber(value) && secretKey
209
213
  ? encodeID(value, secretKey)
@@ -227,8 +231,8 @@ export const decode = (input, fieldType, fieldChildrenType, secretKey) => {
227
231
  return null;
228
232
  if (input === null || input === "")
229
233
  return null;
234
+ // Detect the fieldType based on the input and the provided array of possible types.
230
235
  if (Array.isArray(fieldType))
231
- // Detect the fieldType based on the input and the provided array of possible types.
232
236
  fieldType = detectFieldType(String(input), fieldType);
233
237
  // Decode the input using the decodeHelper function.
234
238
  return decodeHelper(typeof input === "string"
package/dist/index.d.ts CHANGED
@@ -86,7 +86,7 @@ export default class Inibase {
86
86
  put(tableName: string, data: Data, where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnPostedData: true): Promise<Data | null>;
87
87
  put(tableName: string, data: Data[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnPostedData: true): Promise<Data[] | null>;
88
88
  delete(tableName: string, where?: number | string, _id?: string | string[]): Promise<string | null>;
89
- delete(tableName: string, where?: (number | string)[], _id?: string | string[]): Promise<string[] | null>;
89
+ delete(tableName: string, where?: (number | string)[] | Criteria, _id?: string | string[]): Promise<string[] | null>;
90
90
  sum(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
91
91
  sum(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
92
92
  max(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
package/dist/index.js CHANGED
@@ -673,6 +673,8 @@ export default class Inibase {
673
673
  if (Config.isCacheEnabled && (await File.isExists(cachedFilePath))) {
674
674
  const cachedItems = (await File.read(cachedFilePath, true)).split(",");
675
675
  this.totalItems[tableName + "-*"] = cachedItems.length;
676
+ if (onlyLinesNumbers)
677
+ return cachedItems.map(Number);
676
678
  return this.get(tableName, cachedItems
677
679
  .slice((options.page - 1) * options.perPage, options.page * options.perPage)
678
680
  .map(Number), options, undefined, undefined, schema);
@@ -712,16 +714,11 @@ export default class Inibase {
712
714
  page: 1,
713
715
  perPage: 15,
714
716
  };
715
- if (!returnPostedData)
716
- returnPostedData = false;
717
- const schema = await this.getTableSchema(tableName);
718
- let RETURN;
719
- if (!schema)
720
- throw this.throwError("NO_SCHEMA", tableName);
721
717
  const idFilePath = join(this.folder, this.database, tableName, "id.inib"), cashFolderPath = join(this.folder, this.database, tableName, ".tmp");
722
- let testFileHandle;
718
+ let testFileHandle, renameList = [];
723
719
  try {
724
720
  testFileHandle = await open(join(cashFolderPath, "id.inib"), "wx");
721
+ renameList = [[join(cashFolderPath, "id.inib"), ""]];
725
722
  }
726
723
  catch ({ message }) {
727
724
  if (message.split(":")[0] === "EEXIST")
@@ -730,6 +727,12 @@ export default class Inibase {
730
727
  finally {
731
728
  await testFileHandle?.close();
732
729
  }
730
+ if (!returnPostedData)
731
+ returnPostedData = false;
732
+ const schema = await this.getTableSchema(tableName);
733
+ let RETURN;
734
+ if (!schema)
735
+ throw this.throwError("NO_SCHEMA", tableName);
733
736
  let lastId = 0, totalItems = 0, lastIdObj;
734
737
  if (await File.isExists(idFilePath)) {
735
738
  if (await File.isExists(join(cashFolderPath, "lastId.inib"))) {
@@ -759,17 +762,24 @@ export default class Inibase {
759
762
  }))(data);
760
763
  if (!RETURN)
761
764
  throw this.throwError("NO_DATA");
762
- RETURN = this.formatData(RETURN, schema);
763
- const pathesContents = this.joinPathesContents(join(this.folder, this.database, tableName), Array.isArray(RETURN) ? RETURN.toReversed() : RETURN);
764
- const renameList = await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => File.append(path, content)));
765
- await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
766
- await File.write(join(cashFolderPath, "lastId.inib"), lastId.toString(), true);
767
- await File.write(join(cashFolderPath, "totalItems.inib"), String(totalItems + (Array.isArray(RETURN) ? RETURN.length : 1)), true);
768
- if (returnPostedData)
769
- return this.get(tableName, Utils.isArrayOfObjects(RETURN)
770
- ? RETURN.map((data) => Number(data.id))
771
- : RETURN.id, options, !Utils.isArrayOfObjects(data), // return only one item if data is not array of objects
772
- undefined, schema);
765
+ try {
766
+ RETURN = this.formatData(RETURN, schema);
767
+ const pathesContents = this.joinPathesContents(join(this.folder, this.database, tableName), Array.isArray(RETURN) ? RETURN.toReversed() : RETURN);
768
+ renameList = await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => File.append(path, content)));
769
+ await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
770
+ renameList = [];
771
+ await File.write(join(cashFolderPath, "lastId.inib"), lastId.toString(), true);
772
+ await File.write(join(cashFolderPath, "totalItems.inib"), String(totalItems + (Array.isArray(RETURN) ? RETURN.length : 1)), true);
773
+ if (returnPostedData)
774
+ return this.get(tableName, Utils.isArrayOfObjects(RETURN)
775
+ ? RETURN.map((data) => Number(data.id))
776
+ : RETURN.id, options, !Utils.isArrayOfObjects(data), // return only one item if data is not array of objects
777
+ undefined, schema);
778
+ }
779
+ finally {
780
+ if (renameList.length)
781
+ await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
782
+ }
773
783
  }
774
784
  async put(tableName, data, where, options = {
775
785
  page: 1,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.0.0-rc.30",
3
+ "version": "1.0.0-rc.31",
4
4
  "author": {
5
5
  "name": "Karim Amahtil",
6
6
  "email": "karim.amahtil@gmail.com"