inibase 1.0.0-rc.32 → 1.0.0-rc.34

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/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];
@@ -711,7 +717,6 @@ export default class Inibase {
711
717
  perPage: 15,
712
718
  };
713
719
  const tablePath = join(this.folder, this.database, tableName);
714
- await File.lock(join(tablePath, ".tmp"));
715
720
  if (!returnPostedData)
716
721
  returnPostedData = false;
717
722
  const schema = await this.getTableSchema(tableName);
@@ -719,51 +724,52 @@ export default class Inibase {
719
724
  if (!schema)
720
725
  throw this.throwError("NO_SCHEMA", tableName);
721
726
  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
727
  try {
728
+ await File.lock(join(tablePath, ".tmp"));
729
+ if (await File.isExists(join(tablePath, "id.inib"))) {
730
+ if (Config.isCacheEnabled &&
731
+ (await File.isExists(join(tablePath, ".tmp", "pagination.inib"))))
732
+ [lastId, totalItems] = (await File.read(join(tablePath, ".tmp", "pagination.inib"), true))
733
+ .split(",")
734
+ .map(Number);
735
+ else {
736
+ let lastIdObj;
737
+ [lastIdObj, totalItems] = await File.get(join(tablePath, "id.inib"), -1, "number", undefined, this.salt, true);
738
+ if (lastIdObj)
739
+ lastId = Number(Object.keys(lastIdObj)[0] ?? 0);
740
+ }
741
+ }
742
+ if (Utils.isArrayOfObjects(data))
743
+ RETURN = data.map(({ id, updatedAt, createdAt, ...rest }) => ({
744
+ id: ++lastId,
745
+ ...rest,
746
+ createdAt: Date.now(),
747
+ }));
748
+ else
749
+ RETURN = (({ id, updatedAt, createdAt, ...rest }) => ({
750
+ id: ++lastId,
751
+ ...rest,
752
+ createdAt: Date.now(),
753
+ }))(data);
754
+ if (!RETURN)
755
+ throw this.throwError("NO_DATA");
750
756
  RETURN = this.formatData(RETURN, schema);
751
757
  const pathesContents = this.joinPathesContents(join(tablePath), Array.isArray(RETURN) ? RETURN.toReversed() : RETURN);
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 {
764
- await File.unlock(join(tablePath, ".tmp"));
765
770
  if (renameList.length)
766
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
771
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
772
+ await File.unlock(join(tablePath, ".tmp"));
767
773
  }
768
774
  }
769
775
  async put(tableName, data, where, options = {
@@ -778,7 +784,6 @@ export default class Inibase {
778
784
  if (!(await File.isExists(join(tablePath, "id.inib"))))
779
785
  throw this.throwError("NO_ITEMS", tableName);
780
786
  data = this.formatData(data, schema, true);
781
- await File.lock(join(tablePath, ".tmp"));
782
787
  if (!where) {
783
788
  if (Utils.isArrayOfObjects(data)) {
784
789
  if (!data.every((item) => item.hasOwnProperty("id") && Utils.isValidID(item.id)))
@@ -803,18 +808,17 @@ export default class Inibase {
803
808
  updatedAt: Date.now(),
804
809
  });
805
810
  try {
811
+ await File.lock(join(tablePath, ".tmp"));
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
  }
815
819
  finally {
816
820
  if (renameList.length)
817
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
821
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
818
822
  await File.unlock(join(tablePath, ".tmp"));
819
823
  }
820
824
  }
@@ -844,19 +848,18 @@ export default class Inibase {
844
848
  }), {}),
845
849
  ]));
846
850
  try {
851
+ await File.lock(join(tablePath, ".tmp"));
847
852
  await Promise.all(Object.entries(pathesContents).map(async ([path, content]) => renameList.push(await File.replace(path, content))));
848
853
  await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
849
854
  renameList = [];
850
855
  if (Config.isCacheEnabled)
851
- await Promise.all((await readdir(join(tablePath, ".tmp")))
852
- ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
853
- .map(async (file) => unlink(join(tablePath, ".tmp", file))));
856
+ await this.clearCache(tablePath);
854
857
  if (returnPostedData)
855
858
  return this.get(tableName, where, options, !Array.isArray(where), undefined, schema);
856
859
  }
857
860
  finally {
858
861
  if (renameList.length)
859
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
862
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
860
863
  await File.unlock(join(tablePath, ".tmp"));
861
864
  }
862
865
  }
@@ -873,20 +876,23 @@ export default class Inibase {
873
876
  async delete(tableName, where, _id) {
874
877
  let renameList = [];
875
878
  const tablePath = join(this.folder, this.database, tableName);
876
- await File.lock(join(tablePath, ".tmp"));
877
879
  const schema = await this.getTableSchema(tableName);
878
880
  if (!schema)
879
881
  throw this.throwError("NO_SCHEMA", tableName);
880
882
  if (!(await File.isExists(join(tablePath, "id.inib"))))
881
883
  throw this.throwError("NO_ITEMS", tableName);
882
884
  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))));
885
+ try {
886
+ await File.lock(join(tablePath, ".tmp"));
887
+ await Promise.all((await readdir(join(tablePath)))
888
+ ?.filter((fileName) => fileName.endsWith(".inib"))
889
+ .map(async (file) => unlink(join(tablePath, file))));
890
+ if (Config.isCacheEnabled)
891
+ await this.clearCache(tablePath);
892
+ }
893
+ finally {
894
+ await File.unlock(join(tablePath, ".tmp"));
895
+ }
890
896
  return "*";
891
897
  }
892
898
  else if ((Array.isArray(where) &&
@@ -910,12 +916,11 @@ 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) {
916
- await Promise.all((await readdir(join(tablePath, ".tmp")))
917
- ?.filter((fileName) => !["pagination.inib", "locked.inib"].includes(fileName))
918
- .map(async (file) => unlink(join(tablePath, ".tmp", file))));
923
+ await this.clearCache(tablePath);
919
924
  if (await File.isExists(join(tablePath, ".tmp", "pagination.inib"))) {
920
925
  let [lastId, totalItems] = (await File.read(join(tablePath, ".tmp", "pagination.inib"), true))
921
926
  .split(",")
@@ -927,7 +932,7 @@ export default class Inibase {
927
932
  }
928
933
  finally {
929
934
  if (renameList.length)
930
- await Promise.all(renameList.map(async ([tempPath, _]) => (await File.isExists(tempPath)) ? unlink(tempPath) : undefined));
935
+ await Promise.allSettled(renameList.map(async ([tempPath, _]) => unlink(tempPath)));
931
936
  await File.unlock(join(tablePath, ".tmp"));
932
937
  }
933
938
  }
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.34",
4
4
  "author": {
5
5
  "name": "Karim Amahtil",
6
6
  "email": "karim.amahtil@gmail.com"