inibase 1.0.0-rc.60 → 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/README.md CHANGED
@@ -64,44 +64,6 @@ const users = await db.get("user", { favoriteFoods: "![]Pizza,Burger" });
64
64
 
65
65
  This structure ensures efficient storage, retrieval, and updates, making our system scalable and high-performing for diverse datasets and applications.
66
66
 
67
- ## Config (.env)
68
-
69
- The `.env` file supports the following parameters
70
-
71
- ```ini
72
- # Don't add this line, it's an auto generated secret key, will be using for encrypting the IDs
73
- INIBASE_SECRET=
74
-
75
- INIBASE_COMPRESSION=false
76
- INIBASE_CACHE=false
77
-
78
- # Prepend new items to the beginning of file
79
- INIBASE_REVERSE=false
80
- ```
81
-
82
- ## Benchmark
83
-
84
- ### Bulk
85
-
86
- | | 10 | 100 | 1000 |
87
- |--------|-----------------|-----------------|-----------------|
88
- | POST | 11 ms (0.65 mb) | 19 ms (1.00 mb) | 85 ms (4.58 mb) |
89
- | GET | 14 ms (2.77 mb) | 12 ms (3.16 mb) | 34 ms (1.38 mb) |
90
- | PUT | 6 ms (1.11 mb) | 5 ms (1.37 mb) | 10 ms (1.12 mb) |
91
- | DELETE | 17 ms (1.68 mb) | 14 ms (5.45 mb) | 25 ms (5.94 mb) |
92
-
93
- ### Single
94
-
95
- | | 10 | 100 | 1000 |
96
- |--------|-------------------|--------------------|--------------------|
97
- | POST | 43 ms (4.70 mb) | 387 ms (6.36 mb) | 5341 ms (24.73 mb) |
98
- | GET | 99 ms (12.51 mb) | 846 ms (30.68 mb) | 7103 ms (30.86 mb) |
99
- | PUT | 33 ms (10.29 mb) | 312 ms (11.06 mb) | 3539 ms (14.87 mb) |
100
- | DELETE | 134 ms (13.50 mb) | 1224 ms (16.57 mb) | 7339 ms (11.46 mb) |
101
-
102
- > Testing by default with `user` table, with username, email, password fields _so results include password encryption process_ <br>
103
- > To run benchmarks, install *typescript* & *tsx* globally and run `benchmark` `benchmark:bulk` `benchmark:single`
104
-
105
67
  ## Inibase CLI
106
68
 
107
69
  ```shell
@@ -145,48 +107,75 @@ delete <tableName> -w <ID|LineNumber|Criteria>
145
107
  ## Examples
146
108
 
147
109
  <details>
148
- <summary>Schema</summary>
110
+ <summary>Tables</summary>
149
111
  <blockquote>
150
112
 
151
113
  <details>
152
- <summary>Create Schema</summary>
114
+ <summary>Config</summary>
153
115
  <blockquote>
154
116
 
117
+ ```ts
118
+ interface {
119
+ compression: boolean;
120
+ cache: boolean;
121
+ prepend: boolean;
122
+ }
123
+ ```
124
+ </blockquote>
125
+ </details>
126
+
155
127
  <details>
156
- <summary>Using schema.json file</summary>
128
+ <summary>Schema</summary>
157
129
  <blockquote>
158
- Inside the table folder
159
130
 
160
- 1. Create empty folders `.cache` `.tmp`
161
- 2. Create `schema.json` file
162
-
163
- ```jsonc
164
- [
165
- {
166
- // Give a unique ID number for each field
167
- "id": 1,
168
- "key": "username",
169
- "type": "string"
170
- },
171
- {
172
- "id": 2,
173
- "key": "email",
174
- "type": "email"
175
- },
176
- ]
131
+ ```ts
132
+ interface {
133
+ id: number; // stored as a Number but displayed as a hashed ID
134
+ key: string;
135
+ required?: boolean;
136
+ unique?: boolean;
137
+ type: "string" | "number" | "boolean" | "date" | "email" | "url" | "password" | "html" | "ip" | "json" | "id";
138
+ }
139
+ interface Table {
140
+ id: number;
141
+ key: string;
142
+ required?: boolean;
143
+ type: "table";
144
+ table: string;
145
+ }
146
+ interface Array {
147
+ id: number;
148
+ key: string;
149
+ required?: boolean;
150
+ type: "array";
151
+ children: string|string[];
152
+ }
153
+ interface ObjectOrArrayOfObjects {
154
+ id: number;
155
+ key: string;
156
+ required?: boolean;
157
+ type: "object" | "array";
158
+ children: Schema;
159
+ }
177
160
  ```
178
161
  </blockquote>
179
162
  </details>
180
163
 
181
164
  <details>
182
- <summary>Using built-in function</summary>
165
+ <summary>Create Table</summary>
183
166
  <blockquote>
184
167
 
185
168
  ```js
186
169
  import Inibase from "inibase";
187
170
  const db = new Inibase("/databaseName");
188
171
 
189
- const userSchema = [
172
+ const userTableConfig = {
173
+ compression: true,
174
+ cache: true,
175
+ prepend: false
176
+ }
177
+
178
+ const userTableSchema = [
190
179
  {
191
180
  key: "username",
192
181
  type: "string",
@@ -252,14 +241,15 @@ const userSchema = [
252
241
  },
253
242
  ];
254
243
 
255
- await db.setTableSchema("user", userSchema);
244
+ await db.createTable("user", userTableSchema, userTableConfig);
256
245
  ```
257
246
  </blockquote>
258
247
  </details>
259
248
 
260
- </blockquote>
261
- </details>
262
-
249
+ <details>
250
+ <summary>Update Table</summary>
251
+ <blockquote>
252
+
263
253
  <details>
264
254
  <summary>Add field</summary>
265
255
  <blockquote>
@@ -268,10 +258,10 @@ await db.setTableSchema("user", userSchema);
268
258
  import Inibase from "inibase";
269
259
  const db = new Inibase("/databaseName");
270
260
 
271
- const userSchema = await db.getTableSchema("user");
272
- const newUserSchema = [...userSchema, {key: "phone2", type: "number", required: false}];
261
+ const userTableSchema = (await db.getTable("user")).schema;
262
+ const newUserTableSchema = [...userTableSchema, {key: "phone2", type: "number", required: false}];
273
263
 
274
- await db.setTableSchema("user", newUserSchema);
264
+ await db.updateTable("user", newUserTableSchema);
275
265
  ```
276
266
  </blockquote>
277
267
  </details>
@@ -286,13 +276,33 @@ import { setField } from "inibase/utils";
286
276
 
287
277
  const db = new Inibase("/databaseName");
288
278
 
289
- const userSchema = await db.getTableSchema("user");
290
- setField("username", userSchema, {key: "full_name"});
291
- await db.setTableSchema("user", newUserSchema);
279
+ const userTableSchema = (await db.getTable("user")).schema;
280
+ setField("username", userTableSchema, {key: "fullName"});
281
+ await db.updateTable("user", newUserTableSchema);
292
282
  ```
293
283
  </blockquote>
294
284
  </details>
295
285
 
286
+ <details>
287
+ <summary>Remove field</summary>
288
+ <blockquote>
289
+
290
+ ```js
291
+ import Inibase from "inibase";
292
+ import { unsetField } from "inibase/utils";
293
+
294
+ const db = new Inibase("/databaseName");
295
+
296
+ const userTableSchema = (await db.getTable("user")).schema;
297
+ unsetField("fullName", userTableSchema);
298
+ await db.updateTable("user", newUserTableSchema);
299
+ ```
300
+ </blockquote>
301
+ </details>
302
+
303
+ </blockquote>
304
+ </details>
305
+
296
306
  <details>
297
307
  <summary>Join Tables</summary>
298
308
  <blockquote>
@@ -301,7 +311,7 @@ await db.setTableSchema("user", newUserSchema);
301
311
  import Inibase from "inibase";
302
312
  const db = new Inibase("/databaseName");
303
313
 
304
- const productSchema = [
314
+ const productTableSchema = [
305
315
  {
306
316
  key: "title",
307
317
  type: "string",
@@ -319,9 +329,9 @@ const productSchema = [
319
329
  },
320
330
  ];
321
331
 
322
- await db.setTableSchema("product", productSchema);
332
+ await db.createTable("product", productTableSchema);
323
333
 
324
- const productData = [
334
+ const productTableData = [
325
335
  {
326
336
  title: "Product 1",
327
337
  price: 16,
@@ -334,7 +344,7 @@ const productData = [
334
344
  },
335
345
  ];
336
346
 
337
- const product = await db.post("product", productData);
347
+ const product = await db.post("product", productTableData);
338
348
  // [
339
349
  // {
340
350
  // "id": "1d88385d4b1581f8fb059334dec30f4c",
@@ -373,7 +383,7 @@ const product = await db.post("product", productData);
373
383
  import Inibase from "inibase";
374
384
  const db = new Inibase("/databaseName");
375
385
 
376
- const userData = [
386
+ const userTableData = [
377
387
  {
378
388
  username: "user1",
379
389
  email: "user1@example.com",
@@ -408,7 +418,7 @@ const userData = [
408
418
  },
409
419
  ];
410
420
 
411
- const users = await db.post("user", userData);
421
+ const users = await db.post("user", userTableData);
412
422
  // [
413
423
  // {
414
424
  // "id": "1d88385d4b1581f8fb059334dec30f4c",
@@ -664,6 +674,29 @@ await db.sort("user", {age: -1, username: "asc"});
664
674
  </blockquote>
665
675
  </details>
666
676
 
677
+ ## Benchmark
678
+
679
+ ### Bulk
680
+
681
+ | | 10 | 100 | 1000 |
682
+ |--------|-----------------|-----------------|-----------------|
683
+ | POST | 11 ms (0.65 mb) | 19 ms (1.00 mb) | 85 ms (4.58 mb) |
684
+ | GET | 14 ms (2.77 mb) | 12 ms (3.16 mb) | 34 ms (1.38 mb) |
685
+ | PUT | 6 ms (1.11 mb) | 5 ms (1.37 mb) | 10 ms (1.12 mb) |
686
+ | DELETE | 17 ms (1.68 mb) | 14 ms (5.45 mb) | 25 ms (5.94 mb) |
687
+
688
+ ### Single
689
+
690
+ | | 10 | 100 | 1000 |
691
+ |--------|-------------------|--------------------|--------------------|
692
+ | POST | 43 ms (4.70 mb) | 387 ms (6.36 mb) | 5341 ms (24.73 mb) |
693
+ | GET | 99 ms (12.51 mb) | 846 ms (30.68 mb) | 7103 ms (30.86 mb) |
694
+ | PUT | 33 ms (10.29 mb) | 312 ms (11.06 mb) | 3539 ms (14.87 mb) |
695
+ | DELETE | 134 ms (13.50 mb) | 1224 ms (16.57 mb) | 7339 ms (11.46 mb) |
696
+
697
+ > Testing by default with `user` table, with username, email, password fields _so results include password encryption process_ <br>
698
+ > To run benchmarks, install *typescript* & *[tsx](https://github.com/privatenumber/tsx)* globally and run `benchmark` `benchmark:bulk` `benchmark:single`
699
+
667
700
  ## Roadmap
668
701
 
669
702
  - [x] Actions:
@@ -671,7 +704,7 @@ await db.sort("user", {age: -1, username: "asc"});
671
704
  - [x] Pagination
672
705
  - [x] Criteria
673
706
  - [x] Columns
674
- - [x] Sort (using UNIX commands)
707
+ - [x] Sort
675
708
  - [x] POST
676
709
  - [x] PUT
677
710
  - [x] DELETE
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.d.ts CHANGED
@@ -2,8 +2,8 @@
2
2
  import type { ComparisonOperator, FieldType, Schema } from "./index.js";
3
3
  export declare const lock: (folderPath: string, prefix?: string) => Promise<void>;
4
4
  export declare const unlock: (folderPath: string, prefix?: string) => Promise<void>;
5
- export declare const write: (filePath: string, data: any, disableCompression?: boolean) => Promise<void>;
6
- export declare const read: (filePath: string, disableCompression?: boolean) => Promise<string>;
5
+ export declare const write: (filePath: string, data: any) => Promise<void>;
6
+ export declare const read: (filePath: string) => Promise<string>;
7
7
  /**
8
8
  * Checks if a file or directory exists at the specified path.
9
9
  *
@@ -68,7 +68,7 @@ export declare const replace: (filePath: string, replacements: string | number |
68
68
  * @returns Promise<string[]>. Modifies the file by appending data.
69
69
  *
70
70
  */
71
- export declare const append: (filePath: string, data: string | number | (string | number)[]) => Promise<string[]>;
71
+ export declare const append: (filePath: string, data: string | number | (string | number)[], prepend?: boolean) => Promise<string[]>;
72
72
  /**
73
73
  * Asynchronously removes specified lines from a file.
74
74
  *