inibase 1.0.0-rc.57 → 1.0.0-rc.59
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 +320 -127
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +88 -0
- package/dist/file.js +58 -38
- package/dist/index.d.ts +5 -1
- package/dist/index.js +141 -122
- package/dist/utils.d.ts +4 -0
- package/dist/utils.js +16 -0
- package/package.json +8 -5
package/dist/file.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { open, access, writeFile, readFile, constants as fsConstants, unlink, copyFile, appendFile, } from "node:fs/promises";
|
|
1
|
+
import { open, access, writeFile, readFile, constants as fsConstants, unlink, copyFile, appendFile, mkdir, } 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";
|
|
5
5
|
import { createGzip, createGunzip, gunzipSync, gzipSync } from "node:zlib";
|
|
6
|
-
import { join } from "node:path";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
7
|
import { detectFieldType, isArrayOfObjects, isJSON, isNumber, isObject, } from "./utils.js";
|
|
8
|
-
import { encodeID, compare } from "./utils.server.js";
|
|
8
|
+
import { encodeID, compare, exec } from "./utils.server.js";
|
|
9
9
|
import * as Config from "./config.js";
|
|
10
10
|
import Inison from "inison";
|
|
11
11
|
export const lock = async (folderPath, prefix) => {
|
|
@@ -30,6 +30,7 @@ export const unlock = async (folderPath, prefix) => {
|
|
|
30
30
|
catch { }
|
|
31
31
|
};
|
|
32
32
|
export const write = async (filePath, data, disableCompression = false) => {
|
|
33
|
+
await mkdir(dirname(filePath), { recursive: true });
|
|
33
34
|
await writeFile(filePath, Config.isCompressionEnabled && !disableCompression
|
|
34
35
|
? gzipSync(String(data))
|
|
35
36
|
: String(data));
|
|
@@ -203,18 +204,28 @@ export async function get(filePath, lineNumbers, fieldType, fieldChildrenType, s
|
|
|
203
204
|
lines[linesCount] = decode(lastLine, fieldType, fieldChildrenType, secretKey);
|
|
204
205
|
}
|
|
205
206
|
else {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
207
|
+
lineNumbers = Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers];
|
|
208
|
+
if (readWholeFile) {
|
|
209
|
+
const lineNumbersArray = new Set(lineNumbers);
|
|
210
|
+
for await (const line of rl) {
|
|
211
|
+
linesCount++;
|
|
212
|
+
if (!lineNumbersArray.has(linesCount))
|
|
213
|
+
continue;
|
|
214
|
+
lines[linesCount] = decode(line, fieldType, fieldChildrenType, secretKey);
|
|
215
|
+
lineNumbersArray.delete(linesCount);
|
|
216
|
+
}
|
|
217
|
+
return [lines, linesCount];
|
|
218
|
+
}
|
|
219
|
+
const command = Config.isCompressionEnabled
|
|
220
|
+
? `zcat ${filePath} | sed -n '${lineNumbers.join("p;")}p'`
|
|
221
|
+
: `sed -n '${lineNumbers.join("p;")}p' ${filePath}`, foundedLines = (await exec(command)).stdout.trim().split(/\r?\n/);
|
|
222
|
+
let index = 0;
|
|
223
|
+
for (const line of foundedLines) {
|
|
224
|
+
lines[lineNumbers[index]] = decode(line, fieldType, fieldChildrenType, secretKey);
|
|
225
|
+
index++;
|
|
215
226
|
}
|
|
216
227
|
}
|
|
217
|
-
return
|
|
228
|
+
return lines;
|
|
218
229
|
}
|
|
219
230
|
finally {
|
|
220
231
|
// Ensure that file handles are closed, even if an error occurred
|
|
@@ -334,32 +345,41 @@ export const append = async (filePath, data) => {
|
|
|
334
345
|
* Note: Creates a temporary file during the process and replaces the original file with it after removing lines.
|
|
335
346
|
*/
|
|
336
347
|
export const remove = async (filePath, linesToDelete) => {
|
|
337
|
-
|
|
338
|
-
let deletedCount = 0;
|
|
339
|
-
const fileHandle = await open(filePath, "r");
|
|
340
|
-
const fileTempPath = filePath.replace(/([^/]+)\/?$/, ".tmp/$1");
|
|
341
|
-
const fileTempHandle = await open(fileTempPath, "w");
|
|
342
|
-
const linesToDeleteArray = new Set(Array.isArray(linesToDelete)
|
|
348
|
+
linesToDelete = Array.isArray(linesToDelete)
|
|
343
349
|
? linesToDelete.map(Number)
|
|
344
|
-
: [Number(linesToDelete)]
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
350
|
+
: [Number(linesToDelete)];
|
|
351
|
+
const fileTempPath = filePath.replace(/([^/]+)\/?$/, ".tmp/$1");
|
|
352
|
+
if (linesToDelete.length < 1000) {
|
|
353
|
+
const command = Config.isCompressionEnabled
|
|
354
|
+
? `zcat ${filePath} | sed "${linesToDelete.join("d;")}d" | gzip > ${fileTempPath}`
|
|
355
|
+
: `sed "${linesToDelete.join("d;")}d" ${filePath} > ${fileTempPath}`;
|
|
356
|
+
await exec(command);
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
let linesCount = 0;
|
|
360
|
+
let deletedCount = 0;
|
|
361
|
+
const fileHandle = await open(filePath, "r");
|
|
362
|
+
const fileTempHandle = await open(fileTempPath, "w");
|
|
363
|
+
const linesToDeleteArray = new Set(linesToDelete);
|
|
364
|
+
const rl = readLineInternface(fileHandle);
|
|
365
|
+
await _pipeline(rl, fileTempHandle.createWriteStream(), new Transform({
|
|
366
|
+
transform(line, _, callback) {
|
|
367
|
+
linesCount++;
|
|
368
|
+
if (linesToDeleteArray.has(linesCount)) {
|
|
369
|
+
deletedCount++;
|
|
370
|
+
return callback();
|
|
371
|
+
}
|
|
372
|
+
return callback(null, `${line}\n`);
|
|
373
|
+
},
|
|
374
|
+
final(callback) {
|
|
375
|
+
if (deletedCount === linesCount)
|
|
376
|
+
this.push("\n");
|
|
351
377
|
return callback();
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
this.push("\n");
|
|
358
|
-
return callback();
|
|
359
|
-
},
|
|
360
|
-
}));
|
|
361
|
-
await fileTempHandle.close();
|
|
362
|
-
await fileHandle.close();
|
|
378
|
+
},
|
|
379
|
+
}));
|
|
380
|
+
await fileTempHandle.close();
|
|
381
|
+
await fileHandle.close();
|
|
382
|
+
}
|
|
363
383
|
return [fileTempPath, filePath];
|
|
364
384
|
};
|
|
365
385
|
/**
|
|
@@ -447,7 +467,7 @@ export const search = async (filePath, operator, comparedAtValue, logicalOperato
|
|
|
447
467
|
* Note: Reads through the file line by line to count the total number of lines.
|
|
448
468
|
*/
|
|
449
469
|
export const count = async (filePath) => {
|
|
450
|
-
//
|
|
470
|
+
// Number((await exec(`wc -l < ${filePath}`)).stdout.trim());
|
|
451
471
|
let linesCount = 0;
|
|
452
472
|
if (await isExists(filePath)) {
|
|
453
473
|
let fileHandle = null;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import "dotenv/config";
|
|
2
3
|
export interface Data {
|
|
3
4
|
id?: number | string;
|
|
4
5
|
[key: string]: any;
|
|
@@ -47,12 +48,13 @@ export default class Inibase {
|
|
|
47
48
|
database: string;
|
|
48
49
|
table: string | null;
|
|
49
50
|
pageInfo: Record<string, pageInfo>;
|
|
51
|
+
private fileExtension;
|
|
50
52
|
private checkIFunique;
|
|
51
|
-
private isThreadEnabled;
|
|
52
53
|
private totalItems;
|
|
53
54
|
salt: Buffer;
|
|
54
55
|
constructor(database: string, mainFolder?: string, _table?: string | null, _totalItems?: Record<string, number>, _pageInfo?: Record<string, pageInfo>, _isThreadEnabled?: boolean);
|
|
55
56
|
private throwError;
|
|
57
|
+
private getFileExtension;
|
|
56
58
|
private _schemaToIdsPath;
|
|
57
59
|
setTableSchema(tableName: string, schema: Schema): Promise<void>;
|
|
58
60
|
getTableSchema(tableName: string, encodeIDs?: boolean): Promise<Schema | undefined>;
|
|
@@ -81,6 +83,8 @@ export default class Inibase {
|
|
|
81
83
|
put(tableName: string, data: Data[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true): Promise<Data[] | null>;
|
|
82
84
|
delete(tableName: string, where?: number | string, _id?: string | string[]): Promise<string | null>;
|
|
83
85
|
delete(tableName: string, where?: (number | string)[] | Criteria, _id?: string | string[]): Promise<string[] | null>;
|
|
86
|
+
delete(tableName: string, where?: number, _id?: string | string[]): Promise<number | null>;
|
|
87
|
+
delete(tableName: string, where?: number[], _id?: string | string[]): Promise<number[] | null>;
|
|
84
88
|
sum(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
|
|
85
89
|
sum(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
|
|
86
90
|
max(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
|