inibase 1.1.23 → 1.1.25

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
@@ -155,15 +155,15 @@ const decodeHelper = (value, field) => {
155
155
  }));
156
156
  break;
157
157
  case "table":
158
+ case "id":
158
159
  return isNumber(value) &&
159
160
  (!field.table ||
160
161
  !field.databasePath ||
162
+ field.key !== "id" ||
161
163
  !globalConfig[field.databasePath].tables?.get(field.table)?.config
162
164
  .decodeID)
163
165
  ? encodeID(value)
164
166
  : value;
165
- case "id":
166
- return isNumber(value) ? encodeID(value) : value;
167
167
  default:
168
168
  return value;
169
169
  }
package/dist/index.d.ts CHANGED
@@ -23,7 +23,7 @@ export interface Options {
23
23
  columns?: string[] | string;
24
24
  sort?: Record<string, 1 | -1 | "asc" | "ASC" | "desc" | "DESC"> | string[] | string;
25
25
  }
26
- export interface Config {
26
+ export interface TableConfig {
27
27
  compression?: boolean;
28
28
  cache?: boolean;
29
29
  prepend?: boolean;
@@ -31,7 +31,7 @@ export interface Config {
31
31
  }
32
32
  export interface TableObject {
33
33
  schema?: Schema;
34
- config: Config;
34
+ config: TableConfig;
35
35
  }
36
36
  export type ComparisonOperator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "*" | "!*" | "[]" | "![]";
37
37
  export type pageInfo = {
@@ -70,7 +70,6 @@ export default class Inibase {
70
70
  constructor(database: string, mainFolder?: string, language?: ErrorLang);
71
71
  private static errorMessages;
72
72
  createError(name: ErrorCodes, variable?: string | number | (string | number)[]): Error;
73
- clear(): void;
74
73
  private getFileExtension;
75
74
  private _schemaToIdsPath;
76
75
  /**
@@ -78,18 +77,18 @@ export default class Inibase {
78
77
  *
79
78
  * @param {string} tableName
80
79
  * @param {Schema} [schema]
81
- * @param {Config} [config]
80
+ * @param {TableConfig} [config]
82
81
  */
83
- createTable(tableName: string, schema?: Schema, config?: Config): Promise<void>;
82
+ createTable(tableName: string, schema?: Schema, config?: TableConfig): Promise<void>;
84
83
  private replaceStringInFile;
85
84
  /**
86
85
  * Update table schema or config
87
86
  *
88
87
  * @param {string} tableName
89
88
  * @param {Schema} [schema]
90
- * @param {(Config&{name?: string})} [config]
89
+ * @param {(TableConfig&{name?: string})} [config]
91
90
  */
92
- updateTable(tableName: string, schema?: Schema, config?: Config & {
91
+ updateTable(tableName: string, schema?: Schema, config?: TableConfig & {
93
92
  name?: string;
94
93
  }): Promise<void>;
95
94
  /**
@@ -99,7 +98,7 @@ export default class Inibase {
99
98
  * @return {*} {Promise<TableObject>}
100
99
  */
101
100
  getTable(tableName: string, encodeIDs?: boolean): Promise<TableObject>;
102
- getTableSchema(tableName: string, encodeIDs?: boolean): Promise<Schema | undefined>;
101
+ getTableSchema(tableName: string, encodeIDs?: boolean): Promise<Schema>;
103
102
  private throwErrorIfTableEmpty;
104
103
  private _validateData;
105
104
  private validateData;
package/dist/index.js CHANGED
@@ -20,8 +20,12 @@ export default class Inibase {
20
20
  totalItems;
21
21
  constructor(database, mainFolder = ".", language = "en") {
22
22
  this.databasePath = join(mainFolder, database);
23
- this.clear();
24
23
  this.language = language;
24
+ this.pageInfo = {};
25
+ this.totalItems = new Map();
26
+ this.uniqueMap = new Map();
27
+ if (!globalConfig[this.databasePath])
28
+ globalConfig[this.databasePath] = { tables: new Map() };
25
29
  if (!process.env.INIBASE_SECRET) {
26
30
  if (existsSync(".env") &&
27
31
  readFileSync(".env").includes("INIBASE_SECRET="))
@@ -110,12 +114,6 @@ export default class Inibase {
110
114
  error.name = name;
111
115
  return error;
112
116
  }
113
- clear() {
114
- globalConfig[this.databasePath] = { tables: new Map() };
115
- this.totalItems = new Map();
116
- this.pageInfo = {};
117
- this.uniqueMap = new Map();
118
- }
119
117
  getFileExtension(tableName) {
120
118
  let mainExtension = this.fileExtension;
121
119
  // TODO: ADD ENCRYPTION
@@ -141,7 +139,7 @@ export default class Inibase {
141
139
  *
142
140
  * @param {string} tableName
143
141
  * @param {Schema} [schema]
144
- * @param {Config} [config]
142
+ * @param {TableConfig} [config]
145
143
  */
146
144
  async createTable(tableName, schema, config) {
147
145
  const tablePath = join(this.databasePath, tableName);
@@ -187,7 +185,7 @@ export default class Inibase {
187
185
  *
188
186
  * @param {string} tableName
189
187
  * @param {Schema} [schema]
190
- * @param {(Config&{name?: string})} [config]
188
+ * @param {(TableConfig&{name?: string})} [config]
191
189
  */
192
190
  async updateTable(tableName, schema, config) {
193
191
  const table = await this.getTable(tableName);
@@ -937,16 +935,9 @@ export default class Inibase {
937
935
  let RETURN = {};
938
936
  if (!criteria)
939
937
  return [null, null];
940
- if (criteria.and && Utils.isObject(criteria.and)) {
941
- const [searchResult, lineNumbers] = await this.applyCriteria(tableName, options, criteria.and, true, searchIn);
942
- if (searchResult) {
943
- 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)));
944
- delete criteria.and;
945
- searchIn = lineNumbers;
946
- }
947
- else
948
- return [null, null];
949
- }
938
+ const criteriaAND = criteria.and;
939
+ if (criteriaAND)
940
+ delete criteria.and;
950
941
  const criteriaOR = criteria.or;
951
942
  if (criteriaOR)
952
943
  delete criteria.or;
@@ -1014,7 +1005,11 @@ export default class Inibase {
1014
1005
  searchOperator = "=";
1015
1006
  searchComparedAtValue = value;
1016
1007
  }
1017
- const [searchResult, totalLines, linesNumbers] = await File.search(join(tablePath, `${key}${this.getFileExtension(tableName)}`), searchOperator ?? "=", searchComparedAtValue ?? null, searchLogicalOperator, allTrue ? searchIn : undefined, { ...field, databasePath: this.databasePath }, options.perPage, (options.page - 1) * options.perPage + 1, true);
1008
+ const [searchResult, totalLines, linesNumbers] = await File.search(join(tablePath, `${key}${this.getFileExtension(tableName)}`), searchOperator ?? "=", searchComparedAtValue ?? null, searchLogicalOperator, searchIn, {
1009
+ ...field,
1010
+ databasePath: this.databasePath,
1011
+ table: field.table ?? tableName,
1012
+ }, options.perPage, (options.page - 1) * options.perPage + 1, true);
1018
1013
  if (searchResult) {
1019
1014
  const formatedSearchResult = Object.fromEntries(Object.entries(searchResult).map(([id, value]) => {
1020
1015
  const nestedObj = {};
@@ -1025,8 +1020,8 @@ export default class Inibase {
1025
1020
  ? formatedSearchResult
1026
1021
  : Utils.deepMerge(RETURN, formatedSearchResult);
1027
1022
  this.totalItems.set(`${tableName}-${key}`, totalLines);
1028
- if (linesNumbers?.size) {
1029
- if (searchIn && !allTrue)
1023
+ if (linesNumbers?.size && allTrue) {
1024
+ if (searchIn)
1030
1025
  for (const lineNumber of linesNumbers)
1031
1026
  searchIn.add(lineNumber);
1032
1027
  else
@@ -1034,11 +1029,19 @@ export default class Inibase {
1034
1029
  }
1035
1030
  }
1036
1031
  else if (allTrue)
1037
- return [null, null];
1032
+ return null;
1038
1033
  }
1039
1034
  }
1035
+ if (criteriaAND && Utils.isObject(criteriaAND)) {
1036
+ const searchResult = await this.applyCriteria(tableName, options, criteriaAND, true, searchIn);
1037
+ if (searchResult) {
1038
+ RETURN = Utils.deepMerge(RETURN, Object.fromEntries(Object.entries(searchResult).filter(([_k, v], _i) => Object.keys(v).filter((key) => Object.keys(criteriaAND).includes(key)).length)));
1039
+ }
1040
+ else
1041
+ return null;
1042
+ }
1040
1043
  if (criteriaOR && Utils.isObject(criteriaOR)) {
1041
- const [searchResult, lineNumbers] = await this.applyCriteria(tableName, options, criteriaOR, false, searchIn);
1044
+ const searchResult = await this.applyCriteria(tableName, options, criteriaOR, false, searchIn);
1042
1045
  if (searchResult) {
1043
1046
  RETURN = Utils.deepMerge(RETURN, searchResult);
1044
1047
  if (!Object.keys(RETURN).length)
@@ -1047,10 +1050,9 @@ export default class Inibase {
1047
1050
  Object.keys(criteriaOR).some((criteriaKey) => criteriaKey.startsWith(`${key}.`))).length));
1048
1051
  if (!Object.keys(RETURN).length)
1049
1052
  RETURN = {};
1050
- searchIn = lineNumbers;
1051
1053
  }
1052
1054
  }
1053
- return [Object.keys(RETURN).length ? RETURN : null, searchIn];
1055
+ return Object.keys(RETURN).length ? RETURN : null;
1054
1056
  }
1055
1057
  _filterSchemaByColumns(schema, columns) {
1056
1058
  return schema
@@ -1284,22 +1286,22 @@ export default class Inibase {
1284
1286
  .map(Number), options, onlyOne, undefined, true);
1285
1287
  }
1286
1288
  }
1287
- const [LineNumberDataMap, linesNumbers] = await this.applyCriteria(tableName, options, where);
1288
- if (LineNumberDataMap && linesNumbers?.size) {
1289
+ const LineNumberDataObj = await this.applyCriteria(tableName, options, where);
1290
+ if (LineNumberDataObj) {
1289
1291
  if (!this.totalItems.has(`${tableName}-*`))
1290
- this.totalItems.set(`${tableName}-*`, linesNumbers.size);
1292
+ this.totalItems.set(`${tableName}-*`, Object.keys(LineNumberDataObj).length);
1291
1293
  if (onlyLinesNumbers)
1292
1294
  return onlyOne
1293
- ? linesNumbers.values().next().value
1294
- : Array.from(linesNumbers);
1295
- const alreadyExistsColumns = Object.keys(Object.values(LineNumberDataMap)[0]);
1295
+ ? Number(Object.keys(LineNumberDataObj)[0])
1296
+ : Object.keys(LineNumberDataObj).map(Number);
1297
+ const alreadyExistsColumns = Object.keys(Object.values(LineNumberDataObj)[0]);
1296
1298
  const alreadyExistsColumnsIDs = Utils.flattenSchema(schema)
1297
1299
  .filter(({ key }) => alreadyExistsColumns.includes(key))
1298
1300
  .map(({ id }) => id);
1299
- RETURN = Object.values(Utils.deepMerge(LineNumberDataMap, await this.processSchemaData(tableName, Utils.filterSchema(schema, (field) => !alreadyExistsColumnsIDs.includes(field.id) ||
1300
- Utils.isFieldType(field, "table")), Object.keys(LineNumberDataMap).map(Number), options)));
1301
+ RETURN = Object.values(Utils.deepMerge(LineNumberDataObj, await this.processSchemaData(tableName, Utils.filterSchema(schema, (field) => !alreadyExistsColumnsIDs.includes(field.id) ||
1302
+ Utils.isFieldType(field, "table")), Object.keys(LineNumberDataObj).map(Number), options)));
1301
1303
  if (globalConfig[this.databasePath].tables.get(tableName).config.cache)
1302
- await writeFile(cachedFilePath, Array.from(linesNumbers).join(","));
1304
+ await writeFile(cachedFilePath, Object.keys(LineNumberDataObj).join(","));
1303
1305
  }
1304
1306
  }
1305
1307
  if (!RETURN ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.1.23",
3
+ "version": "1.1.25",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Karim Amahtil",