inibase 1.0.0-rc.32 → 1.0.0-rc.33

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.
Files changed (2) hide show
  1. package/dist/index.js +49 -42
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -711,7 +711,6 @@ export default class Inibase {
711
711
  perPage: 15,
712
712
  };
713
713
  const tablePath = join(this.folder, this.database, tableName);
714
- await File.lock(join(tablePath, ".tmp"));
715
714
  if (!returnPostedData)
716
715
  returnPostedData = false;
717
716
  const schema = await this.getTableSchema(tableName);
@@ -719,34 +718,35 @@ export default class Inibase {
719
718
  if (!schema)
720
719
  throw this.throwError("NO_SCHEMA", tableName);
721
720
  let lastId = 0, totalItems = 0, renameList = [];
722
- if (await File.isExists(join(tablePath, "id.inib"))) {
723
- if (Config.isCacheEnabled &&
724
- (await File.isExists(join(tablePath, ".tmp", "pagination.inib"))))
725
- [lastId, totalItems] = (await File.read(join(tablePath, ".tmp", "pagination.inib"), true))
726
- .split(",")
727
- .map(Number);
728
- else {
729
- let lastIdObj;
730
- [lastIdObj, totalItems] = await File.get(join(tablePath, "id.inib"), -1, "number", undefined, this.salt, true);
731
- if (lastIdObj)
732
- lastId = Number(Object.keys(lastIdObj)[0] ?? 0);
733
- }
734
- }
735
- if (Utils.isArrayOfObjects(data))
736
- RETURN = data.map(({ id, updatedAt, createdAt, ...rest }) => ({
737
- id: ++lastId,
738
- ...rest,
739
- createdAt: Date.now(),
740
- }));
741
- else
742
- RETURN = (({ id, updatedAt, createdAt, ...rest }) => ({
743
- id: ++lastId,
744
- ...rest,
745
- createdAt: Date.now(),
746
- }))(data);
747
- if (!RETURN)
748
- throw this.throwError("NO_DATA");
749
721
  try {
722
+ await File.lock(join(tablePath, ".tmp"));
723
+ if (await File.isExists(join(tablePath, "id.inib"))) {
724
+ if (Config.isCacheEnabled &&
725
+ (await File.isExists(join(tablePath, ".tmp", "pagination.inib"))))
726
+ [lastId, totalItems] = (await File.read(join(tablePath, ".tmp", "pagination.inib"), true))
727
+ .split(",")
728
+ .map(Number);
729
+ else {
730
+ let lastIdObj;
731
+ [lastIdObj, totalItems] = await File.get(join(tablePath, "id.inib"), -1, "number", undefined, this.salt, true);
732
+ if (lastIdObj)
733
+ lastId = Number(Object.keys(lastIdObj)[0] ?? 0);
734
+ }
735
+ }
736
+ if (Utils.isArrayOfObjects(data))
737
+ RETURN = data.map(({ id, updatedAt, createdAt, ...rest }) => ({
738
+ id: ++lastId,
739
+ ...rest,
740
+ createdAt: Date.now(),
741
+ }));
742
+ else
743
+ RETURN = (({ id, updatedAt, createdAt, ...rest }) => ({
744
+ id: ++lastId,
745
+ ...rest,
746
+ createdAt: Date.now(),
747
+ }))(data);
748
+ if (!RETURN)
749
+ throw this.throwError("NO_DATA");
750
750
  RETURN = this.formatData(RETURN, schema);
751
751
  const pathesContents = this.joinPathesContents(join(tablePath), Array.isArray(RETURN) ? RETURN.toReversed() : RETURN);
752
752
  await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => renameList.push(await File.append(path, content))));
@@ -761,9 +761,9 @@ export default class Inibase {
761
761
  undefined, schema);
762
762
  }
763
763
  finally {
764
- await File.unlock(join(tablePath, ".tmp"));
765
764
  if (renameList.length)
766
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
765
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
766
+ await File.unlock(join(tablePath, ".tmp"));
767
767
  }
768
768
  }
769
769
  async put(tableName, data, where, options = {
@@ -778,7 +778,6 @@ export default class Inibase {
778
778
  if (!(await File.isExists(join(tablePath, "id.inib"))))
779
779
  throw this.throwError("NO_ITEMS", tableName);
780
780
  data = this.formatData(data, schema, true);
781
- await File.lock(join(tablePath, ".tmp"));
782
781
  if (!where) {
783
782
  if (Utils.isArrayOfObjects(data)) {
784
783
  if (!data.every((item) => item.hasOwnProperty("id") && Utils.isValidID(item.id)))
@@ -803,6 +802,7 @@ export default class Inibase {
803
802
  updatedAt: Date.now(),
804
803
  });
805
804
  try {
805
+ await File.lock(join(tablePath, ".tmp"));
806
806
  await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => renameList.push(await File.replace(path, content))));
807
807
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
808
808
  if (Config.isCacheEnabled)
@@ -814,7 +814,7 @@ export default class Inibase {
814
814
  }
815
815
  finally {
816
816
  if (renameList.length)
817
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
817
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
818
818
  await File.unlock(join(tablePath, ".tmp"));
819
819
  }
820
820
  }
@@ -844,6 +844,7 @@ export default class Inibase {
844
844
  }), {}),
845
845
  ]));
846
846
  try {
847
+ await File.lock(join(tablePath, ".tmp"));
847
848
  await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => renameList.push(await File.replace(path, content))));
848
849
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
849
850
  renameList = [];
@@ -856,7 +857,7 @@ export default class Inibase {
856
857
  }
857
858
  finally {
858
859
  if (renameList.length)
859
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
860
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
860
861
  await File.unlock(join(tablePath, ".tmp"));
861
862
  }
862
863
  }
@@ -873,20 +874,25 @@ export default class Inibase {
873
874
  async delete(tableName, where, _id) {
874
875
  let renameList = [];
875
876
  const tablePath = join(this.folder, this.database, tableName);
876
- await File.lock(join(tablePath, ".tmp"));
877
877
  const schema = await this.getTableSchema(tableName);
878
878
  if (!schema)
879
879
  throw this.throwError("NO_SCHEMA", tableName);
880
880
  if (!(await File.isExists(join(tablePath, "id.inib"))))
881
881
  throw this.throwError("NO_ITEMS", tableName);
882
882
  if (!where) {
883
- await Promise.all((await readdir(join(tablePath)))
884
- ?.filter((fileName) => fileName.endsWith(".inib"))
885
- .map(async (file) => unlink(join(tablePath, file))));
886
- if (Config.isCacheEnabled)
887
- await Promise.all((await readdir(join(tablePath, ".tmp")))
888
- ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
889
- .map(async (file) => unlink(join(tablePath, ".tmp", file))));
883
+ try {
884
+ await File.lock(join(tablePath, ".tmp"));
885
+ await Promise.all((await readdir(join(tablePath)))
886
+ ?.filter((fileName) => fileName.endsWith(".inib"))
887
+ .map(async (file) => unlink(join(tablePath, file))));
888
+ 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))));
892
+ }
893
+ finally {
894
+ await File.unlock(join(tablePath, ".tmp"));
895
+ }
890
896
  return "*";
891
897
  }
892
898
  else if ((Array.isArray(where) &&
@@ -910,6 +916,7 @@ export default class Inibase {
910
916
  if (!_id.length)
911
917
  throw this.throwError("NO_ITEMS", tableName);
912
918
  try {
919
+ await File.lock(join(tablePath, ".tmp"));
913
920
  await Promise.all(files.map(async (file) => renameList.push(await File.remove(join(tablePath, file), where))));
914
921
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
915
922
  if (Config.isCacheEnabled) {
@@ -927,7 +934,7 @@ export default class Inibase {
927
934
  }
928
935
  finally {
929
936
  if (renameList.length)
930
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
937
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
931
938
  await File.unlock(join(tablePath, ".tmp"));
932
939
  }
933
940
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.0.0-rc.32",
3
+ "version": "1.0.0-rc.33",
4
4
  "author": {
5
5
  "name": "Karim Amahtil",
6
6
  "email": "karim.amahtil@gmail.com"