inibase 1.0.0-rc.33 → 1.0.0-rc.35

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
@@ -1,4 +1,4 @@
1
- import { open, access, writeFile, readFile, constants as fsConstants, unlink, } from "node:fs/promises";
1
+ import { open, access, writeFile, readFile, constants as fsConstants, unlink, stat, } from "node:fs/promises";
2
2
  import { createInterface } from "node:readline";
3
3
  import { Transform } from "node:stream";
4
4
  import { pipeline } from "node:stream/promises";
@@ -367,7 +367,7 @@ export const replace = async (filePath, replacements) => {
367
367
  */
368
368
  export const append = async (filePath, data) => {
369
369
  const fileTempPath = filePath.replace(/([^/]+)\/?$/, `.tmp/$1`);
370
- if (await isExists(filePath)) {
370
+ if ((await isExists(filePath)) && (await stat(filePath)).size) {
371
371
  let fileHandle, fileTempHandle, rl;
372
372
  try {
373
373
  fileHandle = await open(filePath, "r");
@@ -406,25 +406,18 @@ export const append = async (filePath, data) => {
406
406
  * Note: Creates a temporary file during the process and replaces the original file with it after removing lines.
407
407
  */
408
408
  export const remove = async (filePath, linesToDelete) => {
409
- let linesCount = 0, deletedCount = 0;
409
+ let linesCount = 0;
410
410
  const fileHandle = await open(filePath, "r"), fileTempPath = filePath.replace(/([^/]+)\/?$/, `.tmp/$1`), fileTempHandle = await open(fileTempPath, "w"), linesToDeleteArray = new Set(Array.isArray(linesToDelete)
411
411
  ? linesToDelete.map(Number)
412
412
  : [Number(linesToDelete)]), rl = readLineInternface(fileHandle);
413
413
  await _pipeline(rl, fileTempHandle.createWriteStream(), new Transform({
414
414
  transform(line, encoding, callback) {
415
415
  linesCount++;
416
- if (linesToDeleteArray.has(linesCount)) {
417
- deletedCount++;
416
+ if (linesToDeleteArray.has(linesCount))
418
417
  return callback();
419
- }
420
418
  else
421
419
  return callback(null, `${line}\n`);
422
420
  },
423
- final(callback) {
424
- if (deletedCount === linesCount)
425
- this.push("\n");
426
- return callback();
427
- },
428
421
  }));
429
422
  await fileTempHandle.close();
430
423
  await fileHandle.close();
package/dist/index.d.ts CHANGED
@@ -77,6 +77,7 @@ export default class Inibase {
77
77
  private getItemsFromSchema;
78
78
  private applyCriteria;
79
79
  private _filterSchemaByColumns;
80
+ clearCache(tablePath: string): Promise<void>;
80
81
  get(tableName: string, where?: string | number | (string | number)[] | Criteria | undefined, options?: Options | undefined, onlyOne?: true, onlyLinesNumbers?: undefined, tableSchema?: Schema): Promise<Data | null>;
81
82
  get(tableName: string, where?: string | number | (string | number)[] | Criteria | undefined, options?: Options | undefined, onlyOne?: boolean | undefined, onlyLinesNumbers?: true, tableSchema?: Schema): Promise<number[] | null>;
82
83
  post(tableName: string, data: Data | Data[], options?: Options, returnPostedData?: boolean): Promise<void | null>;
package/dist/index.js CHANGED
@@ -589,6 +589,11 @@ export default class Inibase {
589
589
  })
590
590
  .filter((i) => i);
591
591
  }
592
+ async clearCache(tablePath) {
593
+ await Promise.all((await readdir(join(tablePath, ".tmp")))
594
+ ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
595
+ .map(async (file) => unlink(join(tablePath, ".tmp", file))));
596
+ }
592
597
  async get(tableName, where, options = {
593
598
  page: 1,
594
599
  perPage: 15,
@@ -632,6 +637,7 @@ export default class Inibase {
632
637
  }
633
638
  else if ((Array.isArray(where) && where.every(Utils.isNumber)) ||
634
639
  (Utils.isNumber(where) && !onlyLinesNumbers)) {
640
+ // "where" in this case, is the lineNumbers instead of IDs
635
641
  let lineNumbers = where;
636
642
  if (!Array.isArray(lineNumbers))
637
643
  lineNumbers = [lineNumbers];
@@ -752,12 +758,12 @@ export default class Inibase {
752
758
  await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => renameList.push(await File.append(path, content))));
753
759
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
754
760
  renameList = [];
755
- if (Config.isCacheEnabled)
761
+ if (Config.isCacheEnabled) {
762
+ await this.clearCache(tablePath);
756
763
  await File.write(join(tablePath, ".tmp", "pagination.inib"), `${lastId},${totalItems + (Array.isArray(RETURN) ? RETURN.length : 1)}`, true);
764
+ }
757
765
  if (returnPostedData)
758
- return this.get(tableName, Utils.isArrayOfObjects(RETURN)
759
- ? RETURN.map((data) => Number(data.id))
760
- : RETURN.id, options, !Utils.isArrayOfObjects(data), // return only one item if data is not array of objects
766
+ return this.get(tableName, Array.isArray(RETURN) ? RETURN.map((_, index) => index + 1) : 1, options, !Utils.isArrayOfObjects(data), // return only one item if data is not array of objects
761
767
  undefined, schema);
762
768
  }
763
769
  finally {
@@ -806,9 +812,7 @@ export default class Inibase {
806
812
  await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => renameList.push(await File.replace(path, content))));
807
813
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
808
814
  if (Config.isCacheEnabled)
809
- await Promise.all((await readdir(join(tablePath, ".tmp")))
810
- ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
811
- .map(async (file) => unlink(join(tablePath, ".tmp", file))));
815
+ await this.clearCache(join(tablePath, ".tmp"));
812
816
  if (returnPostedData)
813
817
  return this.get(tableName, where, options, undefined, undefined, schema);
814
818
  }
@@ -849,9 +853,7 @@ export default class Inibase {
849
853
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
850
854
  renameList = [];
851
855
  if (Config.isCacheEnabled)
852
- await Promise.all((await readdir(join(tablePath, ".tmp")))
853
- ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
854
- .map(async (file) => unlink(join(tablePath, ".tmp", file))));
856
+ await this.clearCache(tablePath);
855
857
  if (returnPostedData)
856
858
  return this.get(tableName, where, options, !Array.isArray(where), undefined, schema);
857
859
  }
@@ -886,9 +888,7 @@ export default class Inibase {
886
888
  ?.filter((fileName) => fileName.endsWith(".inib"))
887
889
  .map(async (file) => unlink(join(tablePath, file))));
888
890
  if (Config.isCacheEnabled)
889
- await Promise.all((await readdir(join(tablePath, ".tmp")))
890
- ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
891
- .map(async (file) => unlink(join(tablePath, ".tmp", file))));
891
+ await this.clearCache(tablePath);
892
892
  }
893
893
  finally {
894
894
  await File.unlock(join(tablePath, ".tmp"));
@@ -920,9 +920,7 @@ export default class Inibase {
920
920
  await Promise.all(files.map(async (file) => renameList.push(await File.remove(join(tablePath, file), where))));
921
921
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
922
922
  if (Config.isCacheEnabled) {
923
- await Promise.all((await readdir(join(tablePath, ".tmp")))
924
- ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
925
- .map(async (file) => unlink(join(tablePath, ".tmp", file))));
923
+ await this.clearCache(tablePath);
926
924
  if (await File.isExists(join(tablePath, ".tmp", "pagination.inib"))) {
927
925
  let [lastId, totalItems] = (await File.read(join(tablePath, ".tmp", "pagination.inib"), true))
928
926
  .split(",")
@@ -960,7 +958,7 @@ export default class Inibase {
960
958
  if (!Array.isArray(columns))
961
959
  columns = [columns];
962
960
  for await (const column of columns) {
963
- const columnPath = join(this.folder, this.database, tableName, column + ".inib");
961
+ const columnPath = join(tablePath, column + ".inib");
964
962
  if (await File.isExists(columnPath)) {
965
963
  if (where) {
966
964
  const lineNumbers = await this.get(tableName, where, undefined, undefined, true, schema);
@@ -984,7 +982,7 @@ export default class Inibase {
984
982
  if (!Array.isArray(columns))
985
983
  columns = [columns];
986
984
  for await (const column of columns) {
987
- const columnPath = join(this.folder, this.database, tableName, column + ".inib");
985
+ const columnPath = join(tablePath, column + ".inib");
988
986
  if (await File.isExists(columnPath)) {
989
987
  if (where) {
990
988
  const lineNumbers = await this.get(tableName, where, undefined, undefined, true, schema);
@@ -1008,7 +1006,7 @@ export default class Inibase {
1008
1006
  if (!Array.isArray(columns))
1009
1007
  columns = [columns];
1010
1008
  for await (const column of columns) {
1011
- const columnPath = join(this.folder, this.database, tableName, column + ".inib");
1009
+ const columnPath = join(tablePath, column + ".inib");
1012
1010
  if (await File.isExists(columnPath)) {
1013
1011
  if (where) {
1014
1012
  const lineNumbers = await this.get(tableName, where, undefined, undefined, true, schema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.0.0-rc.33",
3
+ "version": "1.0.0-rc.35",
4
4
  "author": {
5
5
  "name": "Karim Amahtil",
6
6
  "email": "karim.amahtil@gmail.com"