inibase 1.0.0-rc.11 → 1.0.0-rc.12

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/.env.example ADDED
@@ -0,0 +1 @@
1
+ INIBASE_SECRET=
package/README.md CHANGED
@@ -385,6 +385,56 @@ await db.put("user", { isActive: false });
385
385
 
386
386
  </details>
387
387
 
388
+ <details>
389
+ <summary>SUM</summary>
390
+
391
+ ```js
392
+ import Inibase from "inibase";
393
+ const db = new Inibase("/database_name");
394
+
395
+ // get the sum of column "age" in "user" table
396
+ await db.sum("user", "age");
397
+
398
+ // get the sum of column "age" by criteria (where "isActive" is equal to "false") in "user" table
399
+ await db.sum("user", ["age", ...], { isActive: false });
400
+ ```
401
+
402
+ </details>
403
+
404
+ <details>
405
+ <summary>MAX</summary>
406
+
407
+ ```js
408
+ import Inibase from "inibase";
409
+ const db = new Inibase("/database_name");
410
+
411
+ // get the biggest number of column "age" in "user" table
412
+ await db.max("user", "age");
413
+
414
+ // get the biggest number of column "age" by criteria (where "isActive" is equal to "false") in "user" table
415
+ await db.max("user", ["age", ...], { isActive: false });
416
+ ```
417
+
418
+ </details>
419
+
420
+ <details>
421
+ <summary>MIN</summary>
422
+
423
+ ```js
424
+ import Inibase from "inibase";
425
+ const db = new Inibase("/database_name");
426
+
427
+ // get the smallest number of column "age" in "user" table
428
+ await db.min("user", "age");
429
+
430
+ // get the smallest number of column "age" by criteria (where "isActive" is equal to "false") in "user" table
431
+ await db.min("user", ["age", ...], { isActive: false });
432
+ ```
433
+
434
+ </details>
435
+
436
+
437
+
388
438
  ## Roadmap
389
439
 
390
440
  - [x] Actions:
package/file.ts CHANGED
@@ -3,8 +3,13 @@ import { open, unlink, rename, stat } from "node:fs/promises";
3
3
  import { Interface, createInterface } from "node:readline";
4
4
  import { parse } from "node:path";
5
5
  import { ComparisonOperator, FieldType } from ".";
6
- import { detectFieldType, isArrayOfArrays, isNumber } from "./utils";
7
- import { encodeID, comparePassword } from "./utils.server";
6
+ import {
7
+ detectFieldType,
8
+ isArrayOfArrays,
9
+ isNumber,
10
+ encodeID,
11
+ comparePassword,
12
+ } from "./utils";
8
13
 
9
14
  const doesSupportReadLines = () => {
10
15
  const [major, minor, patch] = process.versions.node.split(".").map(Number);
@@ -223,9 +228,8 @@ export const get = async (
223
228
  for await (const line of rl) {
224
229
  lineCount++;
225
230
  if (!lineNumbersArray.includes(lineCount)) continue;
226
- const indexOfLineCount = lineNumbersArray.indexOf(lineCount);
227
231
  lines[lineCount] = decode(line, fieldType, fieldChildrenType, secretKey);
228
- lineNumbersArray[indexOfLineCount] = 0;
232
+ lineNumbersArray[lineNumbersArray.indexOf(lineCount)] = 0;
229
233
  if (!lineNumbersArray.filter((lineN) => lineN !== 0).length) break;
230
234
  }
231
235
  }
@@ -532,6 +536,129 @@ export const search = async (
532
536
  } else return [null, 0];
533
537
  };
534
538
 
539
+ export const sum = async (
540
+ filePath: string,
541
+ lineNumbers?: number | number[]
542
+ ): Promise<number> => {
543
+ let rl: Interface;
544
+ if (doesSupportReadLines()) rl = (await open(filePath)).readLines();
545
+ else
546
+ rl = createInterface({
547
+ input: createReadStream(filePath),
548
+ crlfDelay: Infinity,
549
+ });
550
+ let sum = 0;
551
+
552
+ if (lineNumbers) {
553
+ let lineCount = 0;
554
+
555
+ let lineNumbersArray = [
556
+ ...(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]),
557
+ ];
558
+ for await (const line of rl) {
559
+ lineCount++;
560
+ if (!lineNumbersArray.includes(lineCount)) continue;
561
+ sum += +decode(line, "number");
562
+ lineNumbersArray[lineNumbersArray.indexOf(lineCount)] = 0;
563
+ if (!lineNumbersArray.filter((lineN) => lineN !== 0).length) break;
564
+ }
565
+ } else for await (const line of rl) sum += +decode(line, "number");
566
+
567
+ return sum;
568
+ };
569
+
570
+ export const max = async (
571
+ filePath: string,
572
+ lineNumbers?: number | number[]
573
+ ): Promise<number> => {
574
+ let rl: Interface;
575
+ if (doesSupportReadLines()) rl = (await open(filePath)).readLines();
576
+ else
577
+ rl = createInterface({
578
+ input: createReadStream(filePath),
579
+ crlfDelay: Infinity,
580
+ });
581
+ let max = 0;
582
+
583
+ if (lineNumbers) {
584
+ let lineCount = 0;
585
+
586
+ let lineNumbersArray = [
587
+ ...(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]),
588
+ ];
589
+ for await (const line of rl) {
590
+ lineCount++;
591
+ if (!lineNumbersArray.includes(lineCount)) continue;
592
+ const lineContentNum = +decode(line, "number");
593
+ if (lineContentNum > max) max = lineContentNum;
594
+ lineNumbersArray[lineNumbersArray.indexOf(lineCount)] = 0;
595
+ if (!lineNumbersArray.filter((lineN) => lineN !== 0).length) break;
596
+ }
597
+ } else
598
+ for await (const line of rl) {
599
+ const lineContentNum = +decode(line, "number");
600
+ if (lineContentNum > max) max = lineContentNum;
601
+ }
602
+
603
+ return max;
604
+ };
605
+
606
+ export const min = async (
607
+ filePath: string,
608
+ lineNumbers?: number | number[]
609
+ ): Promise<number> => {
610
+ let rl: Interface;
611
+ if (doesSupportReadLines()) rl = (await open(filePath)).readLines();
612
+ else
613
+ rl = createInterface({
614
+ input: createReadStream(filePath),
615
+ crlfDelay: Infinity,
616
+ });
617
+ let min = 0;
618
+
619
+ if (lineNumbers) {
620
+ let lineCount = 0;
621
+
622
+ let lineNumbersArray = [
623
+ ...(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]),
624
+ ];
625
+ for await (const line of rl) {
626
+ lineCount++;
627
+ if (!lineNumbersArray.includes(lineCount)) continue;
628
+ const lineContentNum = +decode(line, "number");
629
+ if (lineContentNum < min) min = lineContentNum;
630
+ lineNumbersArray[lineNumbersArray.indexOf(lineCount)] = 0;
631
+ if (!lineNumbersArray.filter((lineN) => lineN !== 0).length) break;
632
+ }
633
+ } else
634
+ for await (const line of rl) {
635
+ const lineContentNum = +decode(line, "number");
636
+ if (lineContentNum < min) min = lineContentNum;
637
+ }
638
+
639
+ return min;
640
+ };
641
+
642
+ export const sort = async (
643
+ filePath: string,
644
+ sortDirection: 1 | -1 | "asc" | "desc",
645
+ lineNumbers?: number | number[],
646
+ _lineNumbersPerChunk: number = 100000
647
+ ): Promise<void> => {
648
+ let rl: Interface;
649
+ if (doesSupportReadLines()) rl = (await open(filePath)).readLines();
650
+ else
651
+ rl = createInterface({
652
+ input: createReadStream(filePath),
653
+ crlfDelay: Infinity,
654
+ });
655
+ let lineCount = 0;
656
+
657
+ for await (const line of rl) {
658
+ lineCount++;
659
+ }
660
+ };
661
+
535
662
  export default class File {
536
663
  static get = get;
537
664
  static remove = remove;
@@ -541,4 +668,7 @@ export default class File {
541
668
  static encode = encode;
542
669
  static decode = decode;
543
670
  static isExists = isExists;
671
+ static sum = sum;
672
+ static min = min;
673
+ static max = max;
544
674
  }
package/index.test.ts CHANGED
@@ -200,7 +200,8 @@ try {
200
200
  // const DATA = await db.post("database", data_2);
201
201
  // const DATA = await db.delete("database", 2);
202
202
  // const DATA = await db.post("database", {
203
- // slug: "demo",
203
+ // slug: "iptv",
204
+ // allowed_domains: ['https://iptv.kamatil.com'],
204
205
  // tables: [
205
206
  // {
206
207
  // id: 1,