inibase 1.1.10 → 1.1.11
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/README.md +3 -1
- package/dist/index.d.ts +12 -11
- package/dist/index.js +63 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -97,6 +97,7 @@ interface {
|
|
|
97
97
|
compression: boolean;
|
|
98
98
|
cache: boolean;
|
|
99
99
|
prepend: boolean;
|
|
100
|
+
decodeID: boolean;
|
|
100
101
|
}
|
|
101
102
|
```
|
|
102
103
|
|
|
@@ -236,7 +237,8 @@ const db = new Inibase("/databaseName");
|
|
|
236
237
|
const userTableConfig = {
|
|
237
238
|
compression: true,
|
|
238
239
|
cache: true,
|
|
239
|
-
prepend: false
|
|
240
|
+
prepend: false,
|
|
241
|
+
decodeID: false
|
|
240
242
|
}
|
|
241
243
|
|
|
242
244
|
const userTableSchema = [
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "dotenv/config";
|
|
2
2
|
export interface Data {
|
|
3
|
-
id?: string;
|
|
3
|
+
id?: string | number;
|
|
4
4
|
[key: string]: any;
|
|
5
5
|
createdAt?: number;
|
|
6
6
|
updatedAt?: number;
|
|
@@ -27,6 +27,7 @@ export interface Config {
|
|
|
27
27
|
compression?: boolean;
|
|
28
28
|
cache?: boolean;
|
|
29
29
|
prepend?: boolean;
|
|
30
|
+
decodeID?: boolean;
|
|
30
31
|
}
|
|
31
32
|
export interface TableObject {
|
|
32
33
|
schema?: Schema;
|
|
@@ -134,11 +135,11 @@ export default class Inibase {
|
|
|
134
135
|
* @param {boolean} [onlyLinesNumbers]
|
|
135
136
|
* @return {*} {(Promise<Data | number | (Data | number)[] | null>)}
|
|
136
137
|
*/
|
|
137
|
-
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: true, onlyLinesNumbers?: false): Promise<(Data & TData) | null>;
|
|
138
|
-
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number, options?: Options, onlyOne?: boolean, onlyLinesNumbers?: false): Promise<(Data & TData) | null>;
|
|
139
|
-
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where?: string | number | (string | number)[] | Criteria, options?: Options, onlyOne?: boolean, onlyLinesNumbers?: false): Promise<(Data & TData)[] | null>;
|
|
140
|
-
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: false | undefined, onlyLinesNumbers: true): Promise<number[] | null>;
|
|
141
|
-
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: true, onlyLinesNumbers: true): Promise<number | null>;
|
|
138
|
+
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: true, onlyLinesNumbers?: false, _whereIsLinesNumbers?: boolean): Promise<(Data & TData) | null>;
|
|
139
|
+
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number, options?: Options, onlyOne?: boolean, onlyLinesNumbers?: false, _whereIsLinesNumbers?: boolean): Promise<(Data & TData) | null>;
|
|
140
|
+
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where?: string | number | (string | number)[] | Criteria, options?: Options, onlyOne?: boolean, onlyLinesNumbers?: false, _whereIsLinesNumbers?: boolean): Promise<(Data & TData)[] | null>;
|
|
141
|
+
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: false | undefined, onlyLinesNumbers: true, _whereIsLinesNumbers?: boolean): Promise<number[] | null>;
|
|
142
|
+
get<TData extends Record<string, any> & Partial<Data>>(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: true, onlyLinesNumbers: true, _whereIsLinesNumbers?: boolean): Promise<number | null>;
|
|
142
143
|
/**
|
|
143
144
|
* Create new item(s) in a table
|
|
144
145
|
*
|
|
@@ -161,10 +162,10 @@ export default class Inibase {
|
|
|
161
162
|
* @param {boolean} [returnUpdatedData]
|
|
162
163
|
* @return {*} {Promise<Data | Data[] | null | undefined | void>}
|
|
163
164
|
*/
|
|
164
|
-
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data?: (Data & TData) | (Data & TData)[], where?: number | string | (number | string)[] | Criteria | undefined, options?: Options | undefined, returnUpdatedData?: false): Promise<void>;
|
|
165
|
-
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data: Data & TData, where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true | boolean): Promise<(Data & TData) | null>;
|
|
166
|
-
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data: (Data & TData)[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true | boolean): Promise<(Data & TData)[] | null>;
|
|
167
|
-
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data: (Data & TData) | (Data & TData)[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true | boolean): Promise<(Data & TData) | (Data & TData)[] | null>;
|
|
165
|
+
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data?: (Data & TData) | (Data & TData)[], where?: number | string | (number | string)[] | Criteria | undefined, options?: Options | undefined, returnUpdatedData?: false, _whereIsLinesNumbers?: boolean): Promise<void>;
|
|
166
|
+
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data: Data & TData, where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true | boolean, _whereIsLinesNumbers?: boolean): Promise<(Data & TData) | null>;
|
|
167
|
+
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data: (Data & TData)[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true | boolean, _whereIsLinesNumbers?: boolean): Promise<(Data & TData)[] | null>;
|
|
168
|
+
put<TData extends Record<string, any> & Partial<Data>>(tableName: string, data: (Data & TData) | (Data & TData)[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true | boolean, _whereIsLinesNumbers?: boolean): Promise<(Data & TData) | (Data & TData)[] | null>;
|
|
168
169
|
/**
|
|
169
170
|
* Delete item(s) in a table
|
|
170
171
|
*
|
|
@@ -172,7 +173,7 @@ export default class Inibase {
|
|
|
172
173
|
* @param {(number | string | (number | string)[] | Criteria)} [where]
|
|
173
174
|
* @return {boolean | null} {(Promise<boolean | null>)}
|
|
174
175
|
*/
|
|
175
|
-
delete(tableName: string, where?: number | string | (number | string)[] | Criteria,
|
|
176
|
+
delete(tableName: string, where?: number | string | (number | string)[] | Criteria, _whereIsLinesNumbers?: boolean): Promise<boolean | null>;
|
|
176
177
|
/**
|
|
177
178
|
* Generate sum of column(s) in a table
|
|
178
179
|
*
|
package/dist/index.js
CHANGED
|
@@ -158,6 +158,7 @@ export default class Inibase {
|
|
|
158
158
|
compression: process.env.INIBASE_COMPRESSION == "true",
|
|
159
159
|
cache: process.env.INIBASE_CACHE === "true",
|
|
160
160
|
prepend: process.env.INIBASE_PREPEND === "true",
|
|
161
|
+
decodeID: process.env.INIBASE_ENCODEID === "true",
|
|
161
162
|
};
|
|
162
163
|
if (config) {
|
|
163
164
|
if (config.compression)
|
|
@@ -251,6 +252,13 @@ export default class Inibase {
|
|
|
251
252
|
await unlink(join(tablePath, ".cache.config"));
|
|
252
253
|
}
|
|
253
254
|
}
|
|
255
|
+
if (config.decodeID !== undefined &&
|
|
256
|
+
config.decodeID !== table.config.decodeID) {
|
|
257
|
+
if (config.decodeID)
|
|
258
|
+
await writeFile(join(tablePath, ".decodeID.config"), "");
|
|
259
|
+
else
|
|
260
|
+
await unlink(join(tablePath, ".decodeID.config"));
|
|
261
|
+
}
|
|
254
262
|
if (config.prepend !== undefined &&
|
|
255
263
|
config.prepend !== table.config.prepend) {
|
|
256
264
|
await UtilsServer.execFile("find", [
|
|
@@ -302,6 +310,7 @@ export default class Inibase {
|
|
|
302
310
|
compression: await File.isExists(join(tablePath, ".compression.config")),
|
|
303
311
|
cache: await File.isExists(join(tablePath, ".cache.config")),
|
|
304
312
|
prepend: await File.isExists(join(tablePath, ".prepend.config")),
|
|
313
|
+
decodeID: await File.isExists(join(tablePath, ".decodeID.config")),
|
|
305
314
|
},
|
|
306
315
|
});
|
|
307
316
|
return this.tablesMap.get(tableName);
|
|
@@ -717,7 +726,9 @@ export default class Inibase {
|
|
|
717
726
|
async processSimpleField(tableName, field, linesNumber, RETURN, _options, prefix) {
|
|
718
727
|
const fieldPath = join(this.databasePath, tableName, `${prefix ?? ""}${field.key}${this.getFileExtension(tableName)}`);
|
|
719
728
|
if (await File.isExists(fieldPath)) {
|
|
720
|
-
const items = await File.get(fieldPath, linesNumber, field.
|
|
729
|
+
const items = await File.get(fieldPath, linesNumber, field.key === "id" && this.tablesMap.get(tableName).config.decodeID
|
|
730
|
+
? "number"
|
|
731
|
+
: field.type, field.children, this.salt);
|
|
721
732
|
if (items) {
|
|
722
733
|
for (const [index, item] of Object.entries(items)) {
|
|
723
734
|
if (typeof item === "undefined")
|
|
@@ -1062,7 +1073,7 @@ export default class Inibase {
|
|
|
1062
1073
|
async get(tableName, where, options = {
|
|
1063
1074
|
page: 1,
|
|
1064
1075
|
perPage: 15,
|
|
1065
|
-
}, onlyOne, onlyLinesNumbers) {
|
|
1076
|
+
}, onlyOne, onlyLinesNumbers, _whereIsLinesNumbers) {
|
|
1066
1077
|
const tablePath = join(this.databasePath, tableName);
|
|
1067
1078
|
// Ensure options.columns is an array
|
|
1068
1079
|
if (options.columns) {
|
|
@@ -1193,8 +1204,9 @@ export default class Inibase {
|
|
|
1193
1204
|
if (!this.totalItems.has(`${tableName}-*`))
|
|
1194
1205
|
this.totalItems.set(`${tableName}-*`, pagination[1]);
|
|
1195
1206
|
}
|
|
1196
|
-
else if ((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1197
|
-
Utils.isNumber(where))
|
|
1207
|
+
else if (((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1208
|
+
Utils.isNumber(where)) &&
|
|
1209
|
+
(_whereIsLinesNumbers || !this.tablesMap.get(tableName).config.decodeID)) {
|
|
1198
1210
|
// "where" in this case, is the line(s) number(s) and not id(s)
|
|
1199
1211
|
let lineNumbers = where;
|
|
1200
1212
|
if (!Array.isArray(lineNumbers))
|
|
@@ -1208,7 +1220,11 @@ export default class Inibase {
|
|
|
1208
1220
|
if (RETURN?.length && !Array.isArray(where))
|
|
1209
1221
|
RETURN = RETURN[0];
|
|
1210
1222
|
}
|
|
1211
|
-
else if ((
|
|
1223
|
+
else if ((!_whereIsLinesNumbers &&
|
|
1224
|
+
this.tablesMap.get(tableName).config.decodeID &&
|
|
1225
|
+
((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1226
|
+
Utils.isNumber(where))) ||
|
|
1227
|
+
(Array.isArray(where) && where.every(Utils.isValidID)) ||
|
|
1212
1228
|
Utils.isValidID(where)) {
|
|
1213
1229
|
let Ids = where;
|
|
1214
1230
|
if (!Array.isArray(Ids))
|
|
@@ -1245,7 +1261,7 @@ export default class Inibase {
|
|
|
1245
1261
|
return onlyOne ? Number(cachedItems[0]) : cachedItems.map(Number);
|
|
1246
1262
|
return this.get(tableName, cachedItems
|
|
1247
1263
|
.slice((options.page - 1) * options.perPage, options.page * options.perPage)
|
|
1248
|
-
.map(Number), options, onlyOne);
|
|
1264
|
+
.map(Number), options, onlyOne, undefined, true);
|
|
1249
1265
|
}
|
|
1250
1266
|
const [LineNumberDataMap, linesNumbers] = await this.applyCriteria(tableName, options, where);
|
|
1251
1267
|
if (LineNumberDataMap && linesNumbers?.size) {
|
|
@@ -1279,6 +1295,15 @@ export default class Inibase {
|
|
|
1279
1295
|
totalPages: Math.ceil(greatestTotalItems / options.perPage),
|
|
1280
1296
|
total: greatestTotalItems,
|
|
1281
1297
|
};
|
|
1298
|
+
// if (this.tablesMap.get(tableName).config.decodeID) {
|
|
1299
|
+
// if (Array.isArray(RETURN)) {
|
|
1300
|
+
// for (let index = 0; index < RETURN.length; index++)
|
|
1301
|
+
// RETURN[index].id = UtilsServer.decodeID(
|
|
1302
|
+
// RETURN[index].id as string,
|
|
1303
|
+
// this.salt,
|
|
1304
|
+
// );
|
|
1305
|
+
// } else RETURN.id = UtilsServer.decodeID(RETURN.id as string, this.salt);
|
|
1306
|
+
// }
|
|
1282
1307
|
return onlyOne && Array.isArray(RETURN) ? RETURN[0] : RETURN;
|
|
1283
1308
|
}
|
|
1284
1309
|
async post(tableName, data, options, returnPostedData) {
|
|
@@ -1344,7 +1369,8 @@ export default class Inibase {
|
|
|
1344
1369
|
? clonedData
|
|
1345
1370
|
.map((_, index) => this.totalItems.get(`${tableName}-*`) - index)
|
|
1346
1371
|
.toReversed()
|
|
1347
|
-
: this.totalItems.get(`${tableName}-*`), options, !Utils.isArrayOfObjects(clonedData)
|
|
1372
|
+
: this.totalItems.get(`${tableName}-*`), options, !Utils.isArrayOfObjects(clonedData), // return only one item if data is not array of objects
|
|
1373
|
+
undefined, true);
|
|
1348
1374
|
}
|
|
1349
1375
|
finally {
|
|
1350
1376
|
if (renameList.length)
|
|
@@ -1355,7 +1381,7 @@ export default class Inibase {
|
|
|
1355
1381
|
async put(tableName, data, where, options = {
|
|
1356
1382
|
page: 1,
|
|
1357
1383
|
perPage: 15,
|
|
1358
|
-
}, returnUpdatedData) {
|
|
1384
|
+
}, returnUpdatedData, _whereIsLinesNumbers) {
|
|
1359
1385
|
const renameList = [];
|
|
1360
1386
|
const tablePath = join(this.databasePath, tableName);
|
|
1361
1387
|
await this.throwErrorIfTableEmpty(tableName);
|
|
@@ -1398,13 +1424,9 @@ export default class Inibase {
|
|
|
1398
1424
|
await File.unlock(join(tablePath, ".tmp"));
|
|
1399
1425
|
}
|
|
1400
1426
|
}
|
|
1401
|
-
else if ((Array.isArray(where) && where.every(Utils.
|
|
1402
|
-
Utils.
|
|
1403
|
-
|
|
1404
|
-
return this.put(tableName, clonedData, lineNumbers, options, false);
|
|
1405
|
-
}
|
|
1406
|
-
else if ((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1407
|
-
Utils.isNumber(where)) {
|
|
1427
|
+
else if (((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1428
|
+
Utils.isNumber(where)) &&
|
|
1429
|
+
(_whereIsLinesNumbers || !this.tablesMap.get(tableName).config.decodeID)) {
|
|
1408
1430
|
// "where" in this case, is the line(s) number(s) and not id(s)
|
|
1409
1431
|
await this.validateData(tableName, clonedData, true);
|
|
1410
1432
|
clonedData = this.formatData(clonedData, this.tablesMap.get(tableName).schema, true);
|
|
@@ -1432,7 +1454,7 @@ export default class Inibase {
|
|
|
1432
1454
|
if (this.tablesMap.get(tableName).config.cache)
|
|
1433
1455
|
await this.clearCache(tableName);
|
|
1434
1456
|
if (returnUpdatedData)
|
|
1435
|
-
return this.get(tableName, where, options, !Array.isArray(where));
|
|
1457
|
+
return this.get(tableName, where, options, !Array.isArray(where), undefined, true);
|
|
1436
1458
|
}
|
|
1437
1459
|
finally {
|
|
1438
1460
|
if (renameList.length)
|
|
@@ -1440,10 +1462,19 @@ export default class Inibase {
|
|
|
1440
1462
|
await File.unlock(join(tablePath, ".tmp"), keys);
|
|
1441
1463
|
}
|
|
1442
1464
|
}
|
|
1465
|
+
else if ((!_whereIsLinesNumbers &&
|
|
1466
|
+
this.tablesMap.get(tableName).config.decodeID &&
|
|
1467
|
+
((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1468
|
+
Utils.isNumber(where))) ||
|
|
1469
|
+
(Array.isArray(where) && where.every(Utils.isValidID)) ||
|
|
1470
|
+
Utils.isValidID(where)) {
|
|
1471
|
+
const lineNumbers = await this.get(tableName, where, undefined, undefined, true);
|
|
1472
|
+
return this.put(tableName, clonedData, lineNumbers, options, false, true);
|
|
1473
|
+
}
|
|
1443
1474
|
else if (Utils.isObject(where)) {
|
|
1444
1475
|
const lineNumbers = await this.get(tableName, where, undefined, undefined, true);
|
|
1445
1476
|
if (lineNumbers)
|
|
1446
|
-
return this.put(tableName, clonedData, lineNumbers, options, returnUpdatedData);
|
|
1477
|
+
return this.put(tableName, clonedData, lineNumbers, options, returnUpdatedData, true);
|
|
1447
1478
|
}
|
|
1448
1479
|
else
|
|
1449
1480
|
throw this.createError("INVALID_PARAMETERS");
|
|
@@ -1455,7 +1486,7 @@ export default class Inibase {
|
|
|
1455
1486
|
* @param {(number | string | (number | string)[] | Criteria)} [where]
|
|
1456
1487
|
* @return {boolean | null} {(Promise<boolean | null>)}
|
|
1457
1488
|
*/
|
|
1458
|
-
async delete(tableName, where,
|
|
1489
|
+
async delete(tableName, where, _whereIsLinesNumbers) {
|
|
1459
1490
|
const tablePath = join(this.databasePath, tableName);
|
|
1460
1491
|
await this.throwErrorIfTableEmpty(tableName);
|
|
1461
1492
|
if (!where) {
|
|
@@ -1483,13 +1514,9 @@ export default class Inibase {
|
|
|
1483
1514
|
await File.unlock(join(tablePath, ".tmp"));
|
|
1484
1515
|
}
|
|
1485
1516
|
}
|
|
1486
|
-
if ((Array.isArray(where) && where.every(Utils.
|
|
1487
|
-
Utils.
|
|
1488
|
-
|
|
1489
|
-
return this.delete(tableName, lineNumbers, where);
|
|
1490
|
-
}
|
|
1491
|
-
if ((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1492
|
-
Utils.isNumber(where)) {
|
|
1517
|
+
if (((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1518
|
+
Utils.isNumber(where)) &&
|
|
1519
|
+
(_whereIsLinesNumbers || !this.tablesMap.get(tableName).config.decodeID)) {
|
|
1493
1520
|
// "where" in this case, is the line(s) number(s) and not id(s)
|
|
1494
1521
|
const files = (await readdir(tablePath))?.filter((fileName) => fileName.endsWith(this.getFileExtension(tableName)));
|
|
1495
1522
|
if (files.length) {
|
|
@@ -1529,10 +1556,19 @@ export default class Inibase {
|
|
|
1529
1556
|
}
|
|
1530
1557
|
}
|
|
1531
1558
|
}
|
|
1532
|
-
|
|
1559
|
+
if ((!_whereIsLinesNumbers &&
|
|
1560
|
+
this.tablesMap.get(tableName).config.decodeID &&
|
|
1561
|
+
((Array.isArray(where) && where.every(Utils.isNumber)) ||
|
|
1562
|
+
Utils.isNumber(where))) ||
|
|
1563
|
+
(Array.isArray(where) && where.every(Utils.isValidID)) ||
|
|
1564
|
+
Utils.isValidID(where)) {
|
|
1565
|
+
const lineNumbers = await this.get(tableName, where, undefined, undefined, true);
|
|
1566
|
+
return this.delete(tableName, lineNumbers, true);
|
|
1567
|
+
}
|
|
1568
|
+
if (Utils.isObject(where)) {
|
|
1533
1569
|
const lineNumbers = await this.get(tableName, where, undefined, undefined, true);
|
|
1534
1570
|
if (lineNumbers)
|
|
1535
|
-
return this.delete(tableName, lineNumbers);
|
|
1571
|
+
return this.delete(tableName, lineNumbers, true);
|
|
1536
1572
|
}
|
|
1537
1573
|
else
|
|
1538
1574
|
throw this.createError("INVALID_PARAMETERS");
|