inibase 1.0.0-rc.25 → 1.0.0-rc.27

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 CHANGED
@@ -4,17 +4,20 @@
4
4
 
5
5
  [![npmjs](https://img.shields.io/npm/dm/inibase.svg?style=flat)](https://www.npmjs.org/package/inibase) [![License](https://img.shields.io/github/license/inicontent/inibase.svg?style=flat&colorA=18181B&colorB=28CF8D)](./LICENSE) [![Activity](https://img.shields.io/github/commit-activity/m/inicontent/inibase)](https://github.com/inicontent/inibase/pulse) [![GitHub stars](https://img.shields.io/github/stars/inicontent/inibase?style=social)](https://github.com/inicontent/inibase)
6
6
 
7
- > File-based relational database, simple to use and can handle large data :fire:
7
+ > A file-based & memory-efficient, serverless relational database management system :fire:
8
8
 
9
9
  ## Features
10
10
 
11
11
  - **Lightweight** 🪶
12
- - **Minimalist** :white_circle:
12
+ - **Minimalist** :white_circle: (but powerful)
13
13
  - **TypeScript** :large_blue_diamond:
14
- - **Super-Fast** :zap:
15
- - **Form-validation** included :sunglasses:
16
- - **Suitable for large data** :page_with_curl:
17
- - **Safe** :lock:
14
+ - **Super-Fast** :zap: (built-in caching system)
15
+ - **Built-in form-validation** included :sunglasses:
16
+ - **Suitable for large data** :page_with_curl: (tested with 200K row)
17
+ - **Support Compression** :eight_spoked_asterisk: (using built-in nodejs zlib)
18
+ - **Support Table Joins** :link:
19
+ - **Low memory-usage** :chart_with_downwards_trend: (3-5mb)
20
+ - **Safe** :lock: (no sql or javascript injections)
18
21
  - **Easy to use** :bread:
19
22
  - **...** and much more :rocket:
20
23
 
@@ -35,8 +38,8 @@ const users = await db.get("user", undefined, {
35
38
  columns: ["username", "address.street", "hobbies.name"],
36
39
  });
37
40
 
38
- // Get items from "user" table where "favoriteFoods" does not includes "Pizza"
39
- const users = await db.get("user", { favoriteFoods: "![]Pizza" });
41
+ // Get items from "user" table where "favoriteFoods" does not includes "Pizza" or "Burger"
42
+ const users = await db.get("user", { favoriteFoods: "![]Pizza,Burger" });
40
43
  ```
41
44
 
42
45
  If you like Inibase, please sponsor: [GitHub Sponsors](https://github.com/sponsors/inicontent) || [Paypal](https://paypal.me/KarimAmahtil).
@@ -49,7 +52,7 @@ If you like Inibase, please sponsor: [GitHub Sponsors](https://github.com/sponso
49
52
 
50
53
  ## How it works?
51
54
 
52
- To simplify the idea, each database has tables, each table has columns, each column will be stored in a seperated file. When POSTing new data, it will be appended to the end of each file as new line. When GETing data, the file will be readed line-by-line so it can handle large data (without consuming a lot of resources), when PUTing(updating) in a specific column, only one file will be opened and updated
55
+ To simplify the idea, each database has tables, each table has columns, each column will be stored in a seperated file. When **POST**ing new data, it will be appended to the _head_ of each file as new line. When **GET**ing data, the file will be readed line-by-line so it can handle large data (without consuming a lot of resources), when **PUT**ing(updating) in a specific column, only one file will be opened and updated
53
56
 
54
57
  ## Benchmark
55
58
 
@@ -71,6 +74,45 @@ To simplify the idea, each database has tables, each table has columns, each col
71
74
  | PUT | 33 ms (10.29 mb) | 312 ms (11.06 mb) | 3539 ms (14.87 mb) |
72
75
  | DELETE | 134 ms (13.50 mb) | 1224 ms (16.57 mb) | 7339 ms (11.46 mb) |
73
76
 
77
+
78
+ ## Roadmap
79
+
80
+ - [x] Actions:
81
+ - [x] GET:
82
+ - [x] Pagination
83
+ - [x] Criteria
84
+ - [x] Columns
85
+ - [ ] Order By
86
+ - [x] POST
87
+ - [x] PUT
88
+ - [x] DELETE
89
+ - [x] SUM
90
+ - [x] MAX
91
+ - [x] MIN
92
+ - [ ] Schema supported types:
93
+ - [x] String
94
+ - [x] Number
95
+ - [x] Boolean
96
+ - [x] Date
97
+ - [x] Email
98
+ - [x] Url
99
+ - [x] Table
100
+ - [x] Object
101
+ - [x] Array
102
+ - [x] Password
103
+ - [x] IP
104
+ - [x] HTML
105
+ - [x] Id
106
+ - [ ] TO-DO:
107
+ - [x] Improve caching
108
+ - [x] Commenting the code
109
+ - [ ] Features:
110
+ - [ ] Encryption
111
+ - [x] Data Compression
112
+ - [x] Caching System
113
+ - [ ] Suggest [new feature +](https://github.com/inicontent/inibase/discussions/new?category=ideas)
114
+
115
+
74
116
  ## Examples
75
117
 
76
118
  <details>
@@ -447,40 +489,17 @@ await db.min("user", ["age", ...], { isActive: false });
447
489
 
448
490
  </details>
449
491
 
492
+ ## Config
450
493
 
494
+ The `.env` file supports the following parameters (make sure to run command with flag --env-file=.env)
451
495
 
452
- ## Roadmap
496
+ ```ini
497
+ # Auto generated secret key, will be using for encrypting the IDs
498
+ INIBASE_SECRET=
453
499
 
454
- - [x] Actions:
455
- - [x] GET:
456
- - [x] Pagination
457
- - [x] Criteria
458
- - [x] Columns
459
- - [ ] Order By
460
- - [x] POST
461
- - [x] PUT
462
- - [x] DELETE
463
- - [ ] Schema supported types:
464
- - [x] String
465
- - [x] Number
466
- - [x] Boolean
467
- - [x] Date
468
- - [x] Email
469
- - [x] Url
470
- - [x] Table
471
- - [x] Object
472
- - [x] Array
473
- - [x] Password
474
- - [x] IP
475
- - [x] HTML
476
- - [x] Id
477
- - [ ] TO-DO:
478
- - [ ] Improve caching
479
- - [ ] Commenting the code
480
- - [ ] Features:
481
- - [ ] Encryption
482
- - [ ] Compress data
483
- - [ ] Suggest [new feature +](https://github.com/inicontent/inibase/discussions/new?category=ideas)
500
+ INIBASE_COMPRESSION=true
501
+ INIBASE_CACHE=true
502
+ ```
484
503
 
485
504
  ## License
486
505
 
@@ -0,0 +1,4 @@
1
+ export default class Config {
2
+ static isCompressionEnabled: boolean;
3
+ static isCacheEnabled: boolean;
4
+ }
package/dist/config.js ADDED
@@ -0,0 +1,4 @@
1
+ export default class Config {
2
+ static isCompressionEnabled = process.env.INIBASE_COMPRESSION === "true";
3
+ static isCacheEnabled = process.env.INIBASE_CACHE === "true";
4
+ }
package/dist/file.d.ts CHANGED
@@ -1,35 +1,171 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import { ComparisonOperator, FieldType } from "./index.js";
3
+ export declare const write: (filePath: string, data: any, disableCompression?: boolean) => Promise<void>;
4
+ export declare const read: (filePath: string, disableCompression?: boolean) => Promise<string>;
5
+ /**
6
+ * Checks if a file or directory exists at the specified path.
7
+ *
8
+ * @param path - The path to the file or directory.
9
+ * @returns A Promise that resolves to true if the file/directory exists, false otherwise.
10
+ */
3
11
  export declare const isExists: (path: string) => Promise<boolean>;
12
+ /**
13
+ * Encodes the input using 'secureString' and 'joinMultidimensionalArray' functions.
14
+ * If the input is an array, it is first secured and then joined into a string.
15
+ * If the input is a single value, it is directly secured.
16
+ *
17
+ * @param input - A value or array of values (string, number, boolean, null).
18
+ * @param secretKey - Optional secret key for encoding, can be a string or Buffer.
19
+ * @returns The secured and/or joined string.
20
+ */
4
21
  export declare const encode: (input: string | number | boolean | null | (string | number | boolean | null)[], secretKey?: string | Buffer) => string | number | boolean | null;
22
+ /**
23
+ * Decodes the input based on the specified field type(s) and an optional secret key.
24
+ * Handles different formats of input, including strings, numbers, and their array representations.
25
+ *
26
+ * @param input - The input to be decoded, can be a string, number, or null.
27
+ * @param fieldType - Optional type of the field to guide decoding (e.g., 'number', 'boolean').
28
+ * @param fieldChildrenType - Optional type for child elements in array inputs.
29
+ * @param secretKey - Optional secret key for decoding, can be a string or Buffer.
30
+ * @returns Decoded value as a string, number, boolean, or array of these, or null if no fieldType or input is null/empty.
31
+ */
5
32
  export declare const decode: (input: string | null | number, fieldType?: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[], secretKey?: string | Buffer) => string | number | boolean | null | (string | number | null | boolean)[];
6
- export declare const get: (filePath: string, lineNumbers?: number | number[], fieldType?: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[], secretKey?: string | Buffer) => Promise<[
33
+ /**
34
+ * Asynchronously reads and decodes data from a file at specified line numbers.
35
+ * Decodes each line based on specified field types and an optional secret key.
36
+ *
37
+ * @param filePath - Path of the file to be read.
38
+ * @param lineNumbers - Optional line number(s) to read from the file. If -1, reads the last line.
39
+ * @param fieldType - Optional type of the field to guide decoding (e.g., 'number', 'boolean').
40
+ * @param fieldChildrenType - Optional type for child elements in array inputs.
41
+ * @param secretKey - Optional secret key for decoding, can be a string or Buffer.
42
+ * @returns Promise resolving to a tuple:
43
+ * 1. Record of line numbers and their decoded content or null if no lines are read.
44
+ * 2. Total count of lines processed.
45
+ */
46
+ export declare function get(filePath: string, lineNumbers?: number | number[], fieldType?: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[], secretKey?: string | Buffer): Promise<Record<number, string | number | boolean | null | (string | number | boolean | (string | number | boolean)[] | null)[]> | null>;
47
+ export declare function get(filePath: string, lineNumbers: undefined | number | number[], fieldType: undefined | FieldType | FieldType[], fieldChildrenType: undefined | FieldType | FieldType[], secretKey: undefined | string | Buffer, readWholeFile: true): Promise<[
7
48
  Record<number, string | number | boolean | null | (string | number | boolean | (string | number | boolean)[] | null)[]> | null,
8
49
  number
9
50
  ]>;
10
- export declare const replace: (filePath: string, replacements: string | number | boolean | null | (string | number | boolean | null)[] | Record<number, string | boolean | number | null | (string | boolean | number | null)[]> | Map<number, string | number | boolean | (string | number | boolean)[]>) => Promise<void>;
11
- export declare const append: (filePath: string, data: string | number | (string | number)[], startsAt?: number) => Promise<void>;
12
- export declare const remove: (filePath: string, linesToDelete: number | number[]) => Promise<void>;
13
- export declare const count: (filePath: string) => Promise<number>;
51
+ /**
52
+ * Asynchronously replaces specific lines in a file based on the provided replacements map or string.
53
+ *
54
+ * @param filePath - Path of the file to modify.
55
+ * @param replacements - Map of line numbers to replacement values, or a single replacement value for all lines.
56
+ * Can be a string, number, boolean, null, array of these types, or a Record/Map of line numbers to these types.
57
+ * @returns Promise<string[]>
58
+ *
59
+ * Note: If the file doesn't exist and replacements is an object, it creates a new file with the specified replacements.
60
+ */
61
+ export declare const replace: (filePath: string, replacements: string | number | boolean | null | (string | number | boolean | null)[] | Record<number, string | boolean | number | null | (string | boolean | number | null)[]>) => Promise<string[]>;
62
+ /**
63
+ * Asynchronously appends data to the beginning of a file.
64
+ *
65
+ * @param filePath - Path of the file to append to.
66
+ * @param data - Data to append. Can be a string, number, or an array of strings/numbers.
67
+ * @returns Promise<string[]>. Modifies the file by appending data.
68
+ *
69
+ */
70
+ export declare const append: (filePath: string, data: string | number | (string | number)[]) => Promise<string[]>;
71
+ /**
72
+ * Asynchronously removes specified lines from a file.
73
+ *
74
+ * @param filePath - Path of the file from which lines are to be removed.
75
+ * @param linesToDelete - A single line number or an array of line numbers to be deleted.
76
+ * @returns Promise<string[]>. Modifies the file by removing specified lines.
77
+ *
78
+ * Note: Creates a temporary file during the process and replaces the original file with it after removing lines.
79
+ */
80
+ export declare const remove: (filePath: string, linesToDelete: number | number[]) => Promise<string[]>;
81
+ /**
82
+ * Asynchronously searches a file for lines matching specified criteria, using comparison and logical operators.
83
+ *
84
+ * @param filePath - Path of the file to search.
85
+ * @param operator - Comparison operator(s) for evaluation (e.g., '=', '!=', '>', '<').
86
+ * @param comparedAtValue - Value(s) to compare each line against.
87
+ * @param logicalOperator - Optional logical operator ('and' or 'or') for combining multiple comparisons.
88
+ * @param fieldType - Optional type of the field to guide comparison.
89
+ * @param fieldChildrenType - Optional type for child elements in array inputs.
90
+ * @param limit - Optional limit on the number of results to return.
91
+ * @param offset - Optional offset to start returning results from.
92
+ * @param readWholeFile - Flag to indicate whether to continue reading the file after reaching the limit.
93
+ * @param secretKey - Optional secret key for decoding, can be a string or Buffer.
94
+ * @returns Promise resolving to a tuple:
95
+ * 1. Record of line numbers and their content that match the criteria or null if none.
96
+ * 2. The count of found items or processed items based on the 'readWholeFile' flag.
97
+ *
98
+ * Note: Decodes each line for comparison and can handle complex queries with multiple conditions.
99
+ */
14
100
  export declare const search: (filePath: string, operator: ComparisonOperator | ComparisonOperator[], comparedAtValue: string | number | boolean | null | (string | number | boolean | null)[], logicalOperator?: "and" | "or", fieldType?: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[], limit?: number, offset?: number, readWholeFile?: boolean, secretKey?: string | Buffer) => Promise<[
15
- Record<number, string | number | boolean | (string | number | boolean | null)[] | null> | null,
16
- number
101
+ Record<number, string | number | boolean | null | (string | number | boolean | null)[]> | null,
102
+ number,
103
+ Set<number> | null
17
104
  ]>;
105
+ /**
106
+ * Asynchronously counts the number of lines in a file.
107
+ *
108
+ * @param filePath - Path of the file to count lines in.
109
+ * @returns Promise<number>. The number of lines in the file.
110
+ *
111
+ * Note: Reads through the file line by line to count the total number of lines.
112
+ */
113
+ export declare const count: (filePath: string) => Promise<number>;
114
+ /**
115
+ * Asynchronously calculates the sum of numerical values from specified lines in a file.
116
+ *
117
+ * @param filePath - Path of the file to read.
118
+ * @param lineNumbers - Optional specific line number(s) to include in the sum. If not provided, sums all lines.
119
+ * @returns Promise<number>. The sum of numerical values from the specified lines.
120
+ *
121
+ * Note: Decodes each line as a number using the 'decode' function. Non-numeric lines contribute 0 to the sum.
122
+ */
18
123
  export declare const sum: (filePath: string, lineNumbers?: number | number[]) => Promise<number>;
124
+ /**
125
+ * Asynchronously finds the maximum numerical value from specified lines in a file.
126
+ *
127
+ * @param filePath - Path of the file to read.
128
+ * @param lineNumbers - Optional specific line number(s) to consider for finding the maximum value. If not provided, considers all lines.
129
+ * @returns Promise<number>. The maximum numerical value found in the specified lines.
130
+ *
131
+ * Note: Decodes each line as a number using the 'decode' function. Considers only numerical values for determining the maximum.
132
+ */
19
133
  export declare const max: (filePath: string, lineNumbers?: number | number[]) => Promise<number>;
134
+ /**
135
+ * Asynchronously finds the minimum numerical value from specified lines in a file.
136
+ *
137
+ * @param filePath - Path of the file to read.
138
+ * @param lineNumbers - Optional specific line number(s) to consider for finding the minimum value. If not provided, considers all lines.
139
+ * @returns Promise<number>. The minimum numerical value found in the specified lines.
140
+ *
141
+ * Note: Decodes each line as a number using the 'decode' function. Considers only numerical values for determining the minimum.
142
+ */
20
143
  export declare const min: (filePath: string, lineNumbers?: number | number[]) => Promise<number>;
144
+ /**
145
+ * Asynchronously sorts the lines in a file in the specified direction.
146
+ *
147
+ * @param filePath - Path of the file to be sorted.
148
+ * @param sortDirection - Direction for sorting: 1 or 'asc' for ascending, -1 or 'desc' for descending.
149
+ * @param lineNumbers - Optional specific line numbers to sort. If not provided, sorts all lines.
150
+ * @param _lineNumbersPerChunk - Optional parameter for handling large files, specifying the number of lines per chunk.
151
+ * @returns Promise<void>. Modifies the file by sorting specified lines.
152
+ *
153
+ * Note: The sorting is applied either to the entire file or to the specified lines. Large files are handled in chunks.
154
+ */
21
155
  export declare const sort: (filePath: string, sortDirection: 1 | -1 | "asc" | "desc", lineNumbers?: number | number[], _lineNumbersPerChunk?: number) => Promise<void>;
22
156
  export default class File {
23
- static get: (filePath: string, lineNumbers?: number | number[] | undefined, fieldType?: FieldType | FieldType[] | undefined, fieldChildrenType?: FieldType | FieldType[] | undefined, secretKey?: string | Buffer | undefined) => Promise<[Record<number, string | number | boolean | (string | number | boolean | (string | number | boolean)[] | null)[] | null> | null, number]>;
24
- static remove: (filePath: string, linesToDelete: number | number[]) => Promise<void>;
25
- static search: (filePath: string, operator: ComparisonOperator | ComparisonOperator[], comparedAtValue: string | number | boolean | (string | number | boolean | null)[] | null, logicalOperator?: "and" | "or" | undefined, fieldType?: FieldType | FieldType[] | undefined, fieldChildrenType?: FieldType | FieldType[] | undefined, limit?: number | undefined, offset?: number | undefined, readWholeFile?: boolean | undefined, secretKey?: string | Buffer | undefined) => Promise<[Record<number, string | number | boolean | (string | number | boolean | null)[] | null> | null, number]>;
26
- static replace: (filePath: string, replacements: string | number | boolean | (string | number | boolean | null)[] | Record<number, string | number | boolean | (string | number | boolean | null)[] | null> | Map<number, string | number | boolean | (string | number | boolean)[]> | null) => Promise<void>;
27
- static count: (filePath: string) => Promise<number>;
157
+ static get: typeof get;
158
+ static remove: (filePath: string, linesToDelete: number | number[]) => Promise<string[]>;
159
+ static search: (filePath: string, operator: ComparisonOperator | ComparisonOperator[], comparedAtValue: string | number | boolean | (string | number | boolean | null)[] | null, logicalOperator?: "and" | "or" | undefined, fieldType?: FieldType | FieldType[] | undefined, fieldChildrenType?: FieldType | FieldType[] | undefined, limit?: number | undefined, offset?: number | undefined, readWholeFile?: boolean | undefined, secretKey?: string | Buffer | undefined) => Promise<[Record<number, string | number | boolean | (string | number | boolean | null)[] | null> | null, number, Set<number> | null]>;
160
+ static replace: (filePath: string, replacements: string | number | boolean | (string | number | boolean | null)[] | Record<number, string | number | boolean | (string | number | boolean | null)[] | null> | null) => Promise<string[]>;
28
161
  static encode: (input: string | number | boolean | (string | number | boolean | null)[] | null, secretKey?: string | Buffer | undefined) => string | number | boolean | null;
29
162
  static decode: (input: string | number | null, fieldType?: FieldType | FieldType[] | undefined, fieldChildrenType?: FieldType | FieldType[] | undefined, secretKey?: string | Buffer | undefined) => string | number | boolean | (string | number | boolean | null)[] | null;
30
163
  static isExists: (path: string) => Promise<boolean>;
31
164
  static sum: (filePath: string, lineNumbers?: number | number[] | undefined) => Promise<number>;
32
165
  static min: (filePath: string, lineNumbers?: number | number[] | undefined) => Promise<number>;
33
166
  static max: (filePath: string, lineNumbers?: number | number[] | undefined) => Promise<number>;
34
- static append: (filePath: string, data: string | number | (string | number)[], startsAt?: number) => Promise<void>;
167
+ static append: (filePath: string, data: string | number | (string | number)[]) => Promise<string[]>;
168
+ static count: (filePath: string) => Promise<number>;
169
+ static write: (filePath: string, data: any, disableCompression?: boolean) => Promise<void>;
170
+ static read: (filePath: string, disableCompression?: boolean) => Promise<string>;
35
171
  }