inibase 1.0.0-rc.61 → 1.0.0-rc.62

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/cli.js CHANGED
@@ -2,10 +2,10 @@
2
2
  import "dotenv/config";
3
3
  import { createInterface } from "node:readline/promises";
4
4
  import { parseArgs } from "node:util";
5
- import Inibase from "./index.js";
6
5
  import { basename } from "node:path";
7
- import { isJSON, isNumber } from "./utils.js";
8
6
  import Inison from "inison";
7
+ import Inibase from "./index.js";
8
+ import { isJSON, isNumber } from "./utils.js";
9
9
  const { path } = parseArgs({
10
10
  options: {
11
11
  path: { type: "string", short: "p" },
@@ -17,18 +17,18 @@ const db = new Inibase(basename(path));
17
17
  const rl = createInterface({
18
18
  input: process.stdin,
19
19
  output: process.stdout,
20
+ prompt: "\u001b[1;36m> \u001b[0m",
20
21
  });
22
+ console.clear();
21
23
  rl.prompt();
22
24
  rl.on("line", async (input) => {
23
25
  const trimedInput = input.trim();
24
26
  if (trimedInput === "clear") {
25
- console.clear();
27
+ process.stdout.write("\x1Bc");
26
28
  rl.prompt();
27
29
  }
28
- if (trimedInput === "info") {
29
- console.warn("war");
30
- console.error("err");
31
- }
30
+ if (trimedInput === "exit")
31
+ process.exit();
32
32
  const splitedInput = trimedInput.match(/[^\s"']+|"([^"]*)"|'([^']*)'/g);
33
33
  if (["get", "post", "delete", "put"].includes(splitedInput[0].toLocaleLowerCase())) {
34
34
  const table = splitedInput[1];
@@ -45,14 +45,16 @@ rl.on("line", async (input) => {
45
45
  },
46
46
  }).values;
47
47
  if (where) {
48
- if (isNumber(where))
48
+ if (where === "'-1'" || where === '"-1"')
49
+ where = -1;
50
+ else if (isNumber(where))
49
51
  where = Number(where);
50
52
  else if (isJSON(where))
51
53
  where = Inison.unstringify(where);
52
54
  }
53
55
  if (data) {
54
56
  if (isJSON(data))
55
- where = Inison.unstringify(data);
57
+ data = Inison.unstringify(data);
56
58
  else
57
59
  data = undefined;
58
60
  }
@@ -85,4 +87,5 @@ rl.on("line", async (input) => {
85
87
  break;
86
88
  }
87
89
  }
90
+ rl.prompt();
88
91
  });
package/dist/file.js CHANGED
@@ -178,11 +178,9 @@ export const decode = (input, fieldType, fieldChildrenType, secretKey) => {
178
178
  };
179
179
  export async function get(filePath, lineNumbers, fieldType, fieldChildrenType, secretKey, readWholeFile = false) {
180
180
  let fileHandle = null;
181
- let rl = null;
182
181
  try {
183
182
  fileHandle = await open(filePath, "r");
184
- rl = createReadLineInternface(filePath, fileHandle);
185
- const lines = {};
183
+ const rl = createReadLineInternface(filePath, fileHandle), lines = {};
186
184
  let linesCount = 0;
187
185
  if (!lineNumbers) {
188
186
  for await (const line of rl) {
@@ -225,7 +223,6 @@ export async function get(filePath, lineNumbers, fieldType, fieldChildrenType, s
225
223
  }
226
224
  finally {
227
225
  // Ensure that file handles are closed, even if an error occurred
228
- rl?.close();
229
226
  await fileHandle?.close();
230
227
  }
231
228
  }
@@ -244,12 +241,11 @@ export const replace = async (filePath, replacements) => {
244
241
  if (await isExists(filePath)) {
245
242
  let fileHandle = null;
246
243
  let fileTempHandle = null;
247
- let rl = null;
248
244
  try {
249
245
  let linesCount = 0;
250
246
  fileHandle = await open(filePath, "r");
251
247
  fileTempHandle = await open(fileTempPath, "w");
252
- rl = createReadLineInternface(filePath, fileHandle);
248
+ const rl = createReadLineInternface(filePath, fileHandle);
253
249
  await _pipeline(filePath, rl, fileTempHandle.createWriteStream(), new Transform({
254
250
  transform(line, encoding, callback) {
255
251
  linesCount++;
@@ -265,7 +261,6 @@ export const replace = async (filePath, replacements) => {
265
261
  }
266
262
  finally {
267
263
  // Ensure that file handles are closed, even if an error occurred
268
- rl?.close();
269
264
  await fileHandle?.close();
270
265
  await fileTempHandle?.close();
271
266
  }
@@ -303,11 +298,10 @@ export const append = async (filePath, data, prepend) => {
303
298
  else {
304
299
  let fileHandle = null;
305
300
  let fileTempHandle = null;
306
- let rl = null;
307
301
  try {
308
302
  fileHandle = await open(filePath, "r");
309
303
  fileTempHandle = await open(fileTempPath, "w");
310
- rl = createReadLineInternface(filePath, fileHandle);
304
+ const rl = createReadLineInternface(filePath, fileHandle);
311
305
  let isAppended = false;
312
306
  await _pipeline(filePath, rl, fileTempHandle.createWriteStream(), new Transform({
313
307
  transform(line, _, callback) {
@@ -321,7 +315,6 @@ export const append = async (filePath, data, prepend) => {
321
315
  }
322
316
  finally {
323
317
  // Ensure that file handles are closed, even if an error occurred
324
- rl?.close();
325
318
  await fileHandle?.close();
326
319
  await fileTempHandle?.close();
327
320
  }
@@ -347,37 +340,10 @@ export const remove = async (filePath, linesToDelete) => {
347
340
  if (linesToDelete.some(Number.isNaN))
348
341
  throw new Error("UNVALID_LINE_NUMBERS");
349
342
  const fileTempPath = filePath.replace(/([^/]+)\/?$/, ".tmp/$1");
350
- if (linesToDelete.length < 1000) {
351
- const command = filePath.endsWith(".gz")
352
- ? `zcat ${filePath} | sed "${linesToDelete.join("d;")}d" | gzip > ${fileTempPath}`
353
- : `sed "${linesToDelete.join("d;")}d" ${filePath} > ${fileTempPath}`;
354
- await exec(command);
355
- }
356
- else {
357
- let linesCount = 0;
358
- let deletedCount = 0;
359
- const fileHandle = await open(filePath, "r");
360
- const fileTempHandle = await open(fileTempPath, "w");
361
- const linesToDeleteArray = new Set(linesToDelete);
362
- const rl = createReadLineInternface(filePath, fileHandle);
363
- await _pipeline(filePath, rl, fileTempHandle.createWriteStream(), new Transform({
364
- transform(line, _, callback) {
365
- linesCount++;
366
- if (linesToDeleteArray.has(linesCount)) {
367
- deletedCount++;
368
- return callback();
369
- }
370
- return callback(null, `${line}\n`);
371
- },
372
- final(callback) {
373
- if (deletedCount === linesCount)
374
- this.push("\n");
375
- return callback();
376
- },
377
- }));
378
- await fileTempHandle.close();
379
- await fileHandle.close();
380
- }
343
+ const command = filePath.endsWith(".gz")
344
+ ? `zcat ${filePath} | sed "${linesToDelete.join("d;")}d" | gzip > ${fileTempPath}`
345
+ : `sed "${linesToDelete.join("d;")}d" ${filePath} > ${fileTempPath}`;
346
+ await exec(command);
381
347
  return [fileTempPath, filePath];
382
348
  };
383
349
  /**
@@ -407,12 +373,11 @@ export const search = async (filePath, operator, comparedAtValue, logicalOperato
407
373
  let foundItems = 0;
408
374
  const linesNumbers = new Set();
409
375
  let fileHandle = null;
410
- let rl = null;
411
376
  try {
412
377
  // Open the file for reading.
413
378
  fileHandle = await open(filePath, "r");
414
379
  // Create a Readline interface to read the file line by line.
415
- rl = createReadLineInternface(filePath, fileHandle);
380
+ const rl = createReadLineInternface(filePath, fileHandle);
416
381
  // Iterate through each line in the file.
417
382
  for await (const line of rl) {
418
383
  // Increment the line count for each line.
@@ -452,7 +417,6 @@ export const search = async (filePath, operator, comparedAtValue, logicalOperato
452
417
  }
453
418
  finally {
454
419
  // Close the file handle in the finally block to ensure it is closed even if an error occurs.
455
- rl?.close();
456
420
  await fileHandle?.close();
457
421
  }
458
422
  };
@@ -469,15 +433,13 @@ export const count = async (filePath) => {
469
433
  let linesCount = 0;
470
434
  if (await isExists(filePath)) {
471
435
  let fileHandle = null;
472
- let rl = null;
473
436
  try {
474
437
  fileHandle = await open(filePath, "r");
475
- rl = createReadLineInternface(filePath, fileHandle);
438
+ const rl = createReadLineInternface(filePath, fileHandle);
476
439
  for await (const _ of rl)
477
440
  linesCount++;
478
441
  }
479
442
  finally {
480
- rl?.close();
481
443
  await fileHandle?.close();
482
444
  }
483
445
  }
@@ -493,27 +455,31 @@ export const count = async (filePath) => {
493
455
  * Note: Decodes each line as a number using the 'decode' function. Non-numeric lines contribute 0 to the sum.
494
456
  */
495
457
  export const sum = async (filePath, lineNumbers) => {
496
- let sum = 0;
497
- const fileHandle = await open(filePath, "r");
498
- const rl = createReadLineInternface(filePath, fileHandle);
499
- if (lineNumbers) {
500
- let linesCount = 0;
501
- const lineNumbersArray = new Set(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]);
502
- for await (const line of rl) {
503
- linesCount++;
504
- if (!lineNumbersArray.has(linesCount))
505
- continue;
506
- sum += +(decode(line, "number") ?? 0);
507
- lineNumbersArray.delete(linesCount);
508
- if (!lineNumbersArray.size)
509
- break;
458
+ let sum = 0, fileHandle = null;
459
+ try {
460
+ fileHandle = await open(filePath, "r");
461
+ const rl = createReadLineInternface(filePath, fileHandle);
462
+ if (lineNumbers) {
463
+ let linesCount = 0;
464
+ const lineNumbersArray = new Set(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]);
465
+ for await (const line of rl) {
466
+ linesCount++;
467
+ if (!lineNumbersArray.has(linesCount))
468
+ continue;
469
+ sum += +(decode(line, "number") ?? 0);
470
+ lineNumbersArray.delete(linesCount);
471
+ if (!lineNumbersArray.size)
472
+ break;
473
+ }
510
474
  }
475
+ else
476
+ for await (const line of rl)
477
+ sum += +(decode(line, "number") ?? 0);
478
+ return sum;
479
+ }
480
+ finally {
481
+ await fileHandle?.close();
511
482
  }
512
- else
513
- for await (const line of rl)
514
- sum += +(decode(line, "number") ?? 0);
515
- await fileHandle.close();
516
- return sum;
517
483
  };
518
484
  /**
519
485
  * Asynchronously finds the maximum numerical value from specified lines in a file.
@@ -525,32 +491,36 @@ export const sum = async (filePath, lineNumbers) => {
525
491
  * Note: Decodes each line as a number using the 'decode' function. Considers only numerical values for determining the maximum.
526
492
  */
527
493
  export const max = async (filePath, lineNumbers) => {
528
- let max = 0;
529
- const fileHandle = await open(filePath, "r");
530
- const rl = createReadLineInternface(filePath, fileHandle);
531
- if (lineNumbers) {
532
- let linesCount = 0;
533
- const lineNumbersArray = new Set(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]);
534
- for await (const line of rl) {
535
- linesCount++;
536
- if (!lineNumbersArray.has(linesCount))
537
- continue;
538
- const lineContentNum = +(decode(line, "number") ?? 0);
539
- if (lineContentNum > max)
540
- max = lineContentNum;
541
- lineNumbersArray.delete(linesCount);
542
- if (!lineNumbersArray.size)
543
- break;
494
+ let max = 0, fileHandle = null, rl = null;
495
+ try {
496
+ fileHandle = await open(filePath, "r");
497
+ rl = createReadLineInternface(filePath, fileHandle);
498
+ if (lineNumbers) {
499
+ let linesCount = 0;
500
+ const lineNumbersArray = new Set(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]);
501
+ for await (const line of rl) {
502
+ linesCount++;
503
+ if (!lineNumbersArray.has(linesCount))
504
+ continue;
505
+ const lineContentNum = +(decode(line, "number") ?? 0);
506
+ if (lineContentNum > max)
507
+ max = lineContentNum;
508
+ lineNumbersArray.delete(linesCount);
509
+ if (!lineNumbersArray.size)
510
+ break;
511
+ }
544
512
  }
513
+ else
514
+ for await (const line of rl) {
515
+ const lineContentNum = +(decode(line, "number") ?? 0);
516
+ if (lineContentNum > max)
517
+ max = lineContentNum;
518
+ }
519
+ return max;
520
+ }
521
+ finally {
522
+ await fileHandle?.close();
545
523
  }
546
- else
547
- for await (const line of rl) {
548
- const lineContentNum = +(decode(line, "number") ?? 0);
549
- if (lineContentNum > max)
550
- max = lineContentNum;
551
- }
552
- await fileHandle.close();
553
- return max;
554
524
  };
555
525
  /**
556
526
  * Asynchronously finds the minimum numerical value from specified lines in a file.
@@ -562,30 +532,34 @@ export const max = async (filePath, lineNumbers) => {
562
532
  * Note: Decodes each line as a number using the 'decode' function. Considers only numerical values for determining the minimum.
563
533
  */
564
534
  export const min = async (filePath, lineNumbers) => {
565
- let min = 0;
566
- const fileHandle = await open(filePath, "r");
567
- const rl = createReadLineInternface(filePath, fileHandle);
568
- if (lineNumbers) {
569
- let linesCount = 0;
570
- const lineNumbersArray = new Set(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]);
571
- for await (const line of rl) {
572
- linesCount++;
573
- if (!lineNumbersArray.has(linesCount))
574
- continue;
575
- const lineContentNum = +(decode(line, "number") ?? 0);
576
- if (lineContentNum < min)
577
- min = lineContentNum;
578
- lineNumbersArray.delete(linesCount);
579
- if (!lineNumbersArray.size)
580
- break;
535
+ let min = 0, fileHandle = null;
536
+ try {
537
+ fileHandle = await open(filePath, "r");
538
+ const rl = createReadLineInternface(filePath, fileHandle);
539
+ if (lineNumbers) {
540
+ let linesCount = 0;
541
+ const lineNumbersArray = new Set(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]);
542
+ for await (const line of rl) {
543
+ linesCount++;
544
+ if (!lineNumbersArray.has(linesCount))
545
+ continue;
546
+ const lineContentNum = +(decode(line, "number") ?? 0);
547
+ if (lineContentNum < min)
548
+ min = lineContentNum;
549
+ lineNumbersArray.delete(linesCount);
550
+ if (!lineNumbersArray.size)
551
+ break;
552
+ }
581
553
  }
554
+ else
555
+ for await (const line of rl) {
556
+ const lineContentNum = +(decode(line, "number") ?? 0);
557
+ if (lineContentNum < min)
558
+ min = lineContentNum;
559
+ }
560
+ return min;
561
+ }
562
+ finally {
563
+ await fileHandle?.close();
582
564
  }
583
- else
584
- for await (const line of rl) {
585
- const lineContentNum = +(decode(line, "number") ?? 0);
586
- if (lineContentNum < min)
587
- min = lineContentNum;
588
- }
589
- await fileHandle.close();
590
- return min;
591
565
  };
package/dist/index.js CHANGED
@@ -85,11 +85,8 @@ export default class Inibase {
85
85
  const tablePath = join(this.databasePath, tableName);
86
86
  if (await File.isExists(tablePath))
87
87
  this.throwError("TABLE_EXISTS", tableName);
88
- await Promise.all([
89
- mkdir(tablePath, { recursive: true }),
90
- mkdir(join(tablePath, ".tmp")),
91
- mkdir(join(tablePath, ".cache")),
92
- ]);
88
+ await mkdir(join(tablePath, ".tmp"), { recursive: true });
89
+ await mkdir(join(tablePath, ".cache"));
93
90
  if (config) {
94
91
  if (config.compression)
95
92
  await open(join(tablePath, ".compression.config"), "w");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.0.0-rc.61",
3
+ "version": "1.0.0-rc.62",
4
4
  "author": {
5
5
  "name": "Karim Amahtil",
6
6
  "email": "karim.amahtil@gmail.com"