inibase 1.0.0-rc.5 → 1.0.0-rc.50
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 +160 -98
- package/dist/config.d.ts +5 -0
- package/dist/config.js +5 -0
- package/dist/file.d.ts +176 -0
- package/dist/file.js +609 -0
- package/dist/file.thread.d.ts +1 -0
- package/dist/file.thread.js +5 -0
- package/dist/index.d.ts +95 -0
- package/dist/index.js +1228 -0
- package/dist/index.thread.d.ts +1 -0
- package/dist/index.thread.js +6 -0
- package/dist/utils.d.ts +209 -0
- package/dist/utils.js +439 -0
- package/dist/utils.server.d.ts +109 -0
- package/dist/utils.server.js +271 -0
- package/package.json +74 -18
- package/file.ts +0 -477
- package/index.test.ts +0 -248
- package/index.ts +0 -1541
- package/tsconfig.json +0 -7
- package/utils.ts +0 -165
package/file.ts
DELETED
|
@@ -1,477 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createWriteStream,
|
|
3
|
-
unlinkSync,
|
|
4
|
-
renameSync,
|
|
5
|
-
existsSync,
|
|
6
|
-
createReadStream,
|
|
7
|
-
WriteStream,
|
|
8
|
-
} from "fs";
|
|
9
|
-
import { open } from "fs/promises";
|
|
10
|
-
import { Interface, createInterface } from "readline";
|
|
11
|
-
import { parse } from "path";
|
|
12
|
-
import { ComparisonOperator, FieldType } from ".";
|
|
13
|
-
import Utils from "./utils";
|
|
14
|
-
|
|
15
|
-
const doesSupportReadLines = () => {
|
|
16
|
-
const [major, minor, patch] = process.versions.node.split(".").map(Number);
|
|
17
|
-
return major >= 18 && minor >= 11;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const encodeFileName = (fileName: string, extension?: string) => {
|
|
21
|
-
return (
|
|
22
|
-
fileName.replaceAll("%", "%25").replaceAll("*", "%") +
|
|
23
|
-
(extension ? `.${extension}` : "")
|
|
24
|
-
);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const decodeFileName = (fileName: string) => {
|
|
28
|
-
return fileName.replaceAll("%", "*").replaceAll("*25", "%");
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const encode = (
|
|
32
|
-
input: string | number | boolean | null | (string | number | boolean | null)[]
|
|
33
|
-
) => {
|
|
34
|
-
const secureString = (input: string | number | boolean | null) => {
|
|
35
|
-
if (["true", "false"].includes(String(input))) return input ? 1 : 0;
|
|
36
|
-
return typeof input === "string"
|
|
37
|
-
? decodeURIComponent(input)
|
|
38
|
-
.replaceAll("<", "<")
|
|
39
|
-
.replaceAll(">", ">")
|
|
40
|
-
.replaceAll(",", "%2C")
|
|
41
|
-
.replaceAll("|", "%7C")
|
|
42
|
-
.replaceAll("\n", "\\n")
|
|
43
|
-
.replaceAll("\r", "\\r")
|
|
44
|
-
: input;
|
|
45
|
-
};
|
|
46
|
-
return Array.isArray(input)
|
|
47
|
-
? Utils.isArrayOfArrays(input)
|
|
48
|
-
? (input as any[])
|
|
49
|
-
.map((_input) => _input.map(secureString).join(","))
|
|
50
|
-
.join("|")
|
|
51
|
-
: input.map(secureString).join(",")
|
|
52
|
-
: secureString(input);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const decode = (
|
|
56
|
-
input: string | null | number,
|
|
57
|
-
fieldType?: FieldType | FieldType[],
|
|
58
|
-
fieldChildrenType?: FieldType | FieldType[]
|
|
59
|
-
): string | number | boolean | null | (string | number | null | boolean)[] => {
|
|
60
|
-
if (!fieldType) return null;
|
|
61
|
-
const unSecureString = (input: string) =>
|
|
62
|
-
decodeURIComponent(input)
|
|
63
|
-
.replaceAll("<", "<")
|
|
64
|
-
.replaceAll(">", ">")
|
|
65
|
-
.replaceAll("%2C", ",")
|
|
66
|
-
.replaceAll("%7C", "|")
|
|
67
|
-
.replaceAll("\\n", "\n")
|
|
68
|
-
.replaceAll("\\r", "\r") || null;
|
|
69
|
-
if (input === null || input === "") return null;
|
|
70
|
-
if (Array.isArray(fieldType))
|
|
71
|
-
fieldType = Utils.detectFieldType(String(input), fieldType);
|
|
72
|
-
const decodeHelper = (value: string | number | any[]) => {
|
|
73
|
-
if (Array.isArray(value) && fieldType !== "array")
|
|
74
|
-
return value.map(decodeHelper);
|
|
75
|
-
switch (fieldType as FieldType) {
|
|
76
|
-
case "table":
|
|
77
|
-
case "number":
|
|
78
|
-
return isNaN(Number(value)) ? null : Number(value);
|
|
79
|
-
case "boolean":
|
|
80
|
-
return typeof value === "string" ? value === "true" : Boolean(value);
|
|
81
|
-
case "array":
|
|
82
|
-
if (!Array.isArray(value)) return null;
|
|
83
|
-
if (fieldChildrenType)
|
|
84
|
-
return value.map(
|
|
85
|
-
(v) =>
|
|
86
|
-
decode(
|
|
87
|
-
v,
|
|
88
|
-
Array.isArray(fieldChildrenType)
|
|
89
|
-
? Utils.detectFieldType(v, fieldChildrenType)
|
|
90
|
-
: fieldChildrenType
|
|
91
|
-
) as string | number | boolean | null
|
|
92
|
-
);
|
|
93
|
-
else return value;
|
|
94
|
-
default:
|
|
95
|
-
return value;
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
return decodeHelper(
|
|
99
|
-
typeof input === "string"
|
|
100
|
-
? input.includes(",")
|
|
101
|
-
? input.includes("|")
|
|
102
|
-
? input
|
|
103
|
-
.split("|")
|
|
104
|
-
.map((_input) => _input.split(",").map(unSecureString))
|
|
105
|
-
: input.split(",").map(unSecureString)
|
|
106
|
-
: unSecureString(input)
|
|
107
|
-
: input
|
|
108
|
-
);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export const get = async (
|
|
112
|
-
filePath: string,
|
|
113
|
-
lineNumbers?: number | number[],
|
|
114
|
-
fieldType?: FieldType | FieldType[],
|
|
115
|
-
fieldChildrenType?: FieldType | FieldType[]
|
|
116
|
-
) => {
|
|
117
|
-
let rl: Interface;
|
|
118
|
-
if (doesSupportReadLines()) rl = (await open(filePath)).readLines();
|
|
119
|
-
else
|
|
120
|
-
rl = createInterface({
|
|
121
|
-
input: createReadStream(filePath),
|
|
122
|
-
crlfDelay: Infinity,
|
|
123
|
-
});
|
|
124
|
-
let lines: Record<
|
|
125
|
-
number,
|
|
126
|
-
| string
|
|
127
|
-
| number
|
|
128
|
-
| boolean
|
|
129
|
-
| (string | number | boolean | (string | number | boolean)[] | null)[]
|
|
130
|
-
| null
|
|
131
|
-
> = {},
|
|
132
|
-
lineCount = 0;
|
|
133
|
-
|
|
134
|
-
if (!lineNumbers) {
|
|
135
|
-
for await (const line of rl)
|
|
136
|
-
lineCount++,
|
|
137
|
-
(lines[lineCount] = decode(line, fieldType, fieldChildrenType));
|
|
138
|
-
} else if (lineNumbers === -1) {
|
|
139
|
-
let lastLine: string;
|
|
140
|
-
for await (const line of rl) lineCount++, (lastLine = line);
|
|
141
|
-
if (lastLine)
|
|
142
|
-
lines = { [lineCount]: decode(lastLine, fieldType, fieldChildrenType) };
|
|
143
|
-
} else {
|
|
144
|
-
let lineNumbersArray = [
|
|
145
|
-
...(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]),
|
|
146
|
-
];
|
|
147
|
-
for await (const line of rl) {
|
|
148
|
-
lineCount++;
|
|
149
|
-
if (!lineNumbersArray.includes(lineCount)) continue;
|
|
150
|
-
const indexOfLineCount = lineNumbersArray.indexOf(lineCount);
|
|
151
|
-
lines[lineCount] = decode(line, fieldType, fieldChildrenType);
|
|
152
|
-
lineNumbersArray[indexOfLineCount] = 0;
|
|
153
|
-
if (!lineNumbersArray.filter((lineN) => lineN !== 0).length) break;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return lines ?? null;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
export const replace = async (
|
|
161
|
-
filePath: string,
|
|
162
|
-
replacements:
|
|
163
|
-
| string
|
|
164
|
-
| number
|
|
165
|
-
| boolean
|
|
166
|
-
| null
|
|
167
|
-
| (string | number | boolean | null)[]
|
|
168
|
-
| Record<
|
|
169
|
-
number,
|
|
170
|
-
string | boolean | number | null | (string | boolean | number | null)[]
|
|
171
|
-
>
|
|
172
|
-
) => {
|
|
173
|
-
if (existsSync(filePath)) {
|
|
174
|
-
let rl: Interface, writeStream: WriteStream;
|
|
175
|
-
if (doesSupportReadLines()) {
|
|
176
|
-
const file = await open(filePath, "w+");
|
|
177
|
-
rl = file.readLines();
|
|
178
|
-
writeStream = file.createWriteStream();
|
|
179
|
-
} else {
|
|
180
|
-
rl = createInterface({
|
|
181
|
-
input: createReadStream(filePath),
|
|
182
|
-
crlfDelay: Infinity,
|
|
183
|
-
});
|
|
184
|
-
writeStream = createWriteStream(filePath);
|
|
185
|
-
}
|
|
186
|
-
if (typeof replacements === "object" && !Array.isArray(replacements)) {
|
|
187
|
-
let lineCount = 0;
|
|
188
|
-
for await (const line of rl) {
|
|
189
|
-
lineCount++;
|
|
190
|
-
writeStream.write(
|
|
191
|
-
(lineCount in replacements ? encode(replacements[lineCount]) : line) +
|
|
192
|
-
"\n"
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
} else
|
|
196
|
-
for await (const _line of rl)
|
|
197
|
-
writeStream.write(encode(replacements) + "\n");
|
|
198
|
-
|
|
199
|
-
writeStream.end();
|
|
200
|
-
} else if (typeof replacements === "object" && !Array.isArray(replacements)) {
|
|
201
|
-
let writeStream: WriteStream;
|
|
202
|
-
if (doesSupportReadLines())
|
|
203
|
-
writeStream = (await open(filePath, "w")).createWriteStream();
|
|
204
|
-
else writeStream = createWriteStream(filePath);
|
|
205
|
-
const largestLinesNumbers =
|
|
206
|
-
Math.max(...Object.keys(replacements).map(Number)) + 1;
|
|
207
|
-
for (let lineCount = 1; lineCount < largestLinesNumbers; lineCount++) {
|
|
208
|
-
writeStream.write(
|
|
209
|
-
(lineCount in replacements ? encode(replacements[lineCount]) : "") +
|
|
210
|
-
"\n"
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
writeStream.end();
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
export const remove = async (
|
|
218
|
-
filePath: string,
|
|
219
|
-
linesToDelete: number | number[]
|
|
220
|
-
): Promise<void> => {
|
|
221
|
-
let lineCount = 0;
|
|
222
|
-
|
|
223
|
-
const tempFilePath = `${filePath}.tmp`,
|
|
224
|
-
linesToDeleteArray = [
|
|
225
|
-
...(Array.isArray(linesToDelete) ? linesToDelete : [linesToDelete]),
|
|
226
|
-
];
|
|
227
|
-
|
|
228
|
-
let rl: Interface, writeStream: WriteStream;
|
|
229
|
-
if (doesSupportReadLines()) {
|
|
230
|
-
rl = (await open(filePath)).readLines();
|
|
231
|
-
writeStream = (await open(tempFilePath, "w+")).createWriteStream();
|
|
232
|
-
} else {
|
|
233
|
-
rl = createInterface({
|
|
234
|
-
input: createReadStream(filePath),
|
|
235
|
-
crlfDelay: Infinity,
|
|
236
|
-
});
|
|
237
|
-
writeStream = createWriteStream(tempFilePath);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
for await (const line of rl) {
|
|
241
|
-
lineCount++;
|
|
242
|
-
if (!linesToDeleteArray.includes(lineCount)) {
|
|
243
|
-
writeStream.write(`${line}\n`);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
writeStream.end();
|
|
247
|
-
writeStream.on("finish", () => {
|
|
248
|
-
unlinkSync(filePath); // Remove the original file
|
|
249
|
-
renameSync(tempFilePath, filePath); // Rename the temp file to the original file name
|
|
250
|
-
});
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
export const count = async (filePath: string): Promise<number> => {
|
|
254
|
-
let lineCount = 0,
|
|
255
|
-
rl: Interface;
|
|
256
|
-
if (doesSupportReadLines()) rl = (await open(filePath)).readLines();
|
|
257
|
-
else
|
|
258
|
-
rl = createInterface({
|
|
259
|
-
input: createReadStream(filePath),
|
|
260
|
-
crlfDelay: Infinity,
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
for await (const line of rl) lineCount++;
|
|
264
|
-
|
|
265
|
-
return lineCount;
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
export const search = async (
|
|
269
|
-
filePath: string,
|
|
270
|
-
operator: ComparisonOperator | ComparisonOperator[],
|
|
271
|
-
comparedAtValue:
|
|
272
|
-
| string
|
|
273
|
-
| number
|
|
274
|
-
| boolean
|
|
275
|
-
| null
|
|
276
|
-
| (string | number | boolean | null)[],
|
|
277
|
-
logicalOperator?: "and" | "or",
|
|
278
|
-
fieldType?: FieldType | FieldType[],
|
|
279
|
-
fieldChildrenType?: FieldType | FieldType[],
|
|
280
|
-
limit?: number,
|
|
281
|
-
offset?: number,
|
|
282
|
-
readWholeFile?: boolean
|
|
283
|
-
): Promise<
|
|
284
|
-
[
|
|
285
|
-
Record<
|
|
286
|
-
number,
|
|
287
|
-
Record<
|
|
288
|
-
string,
|
|
289
|
-
string | number | boolean | (string | number | boolean | null)[] | null
|
|
290
|
-
>
|
|
291
|
-
> | null,
|
|
292
|
-
number
|
|
293
|
-
]
|
|
294
|
-
> => {
|
|
295
|
-
const handleComparisonOperator = (
|
|
296
|
-
operator: ComparisonOperator,
|
|
297
|
-
originalValue:
|
|
298
|
-
| string
|
|
299
|
-
| number
|
|
300
|
-
| boolean
|
|
301
|
-
| null
|
|
302
|
-
| (string | number | boolean | null)[],
|
|
303
|
-
comparedAtValue:
|
|
304
|
-
| string
|
|
305
|
-
| number
|
|
306
|
-
| boolean
|
|
307
|
-
| null
|
|
308
|
-
| (string | number | boolean | null)[],
|
|
309
|
-
fieldType?: FieldType | FieldType[],
|
|
310
|
-
fieldChildrenType?: FieldType | FieldType[]
|
|
311
|
-
): boolean => {
|
|
312
|
-
if (Array.isArray(fieldType))
|
|
313
|
-
fieldType = Utils.detectFieldType(String(originalValue), fieldType);
|
|
314
|
-
// check if not array or object // it can't be array or object!
|
|
315
|
-
switch (operator) {
|
|
316
|
-
case "=":
|
|
317
|
-
switch (fieldType) {
|
|
318
|
-
case "password":
|
|
319
|
-
return typeof originalValue === "string" &&
|
|
320
|
-
typeof comparedAtValue === "string"
|
|
321
|
-
? Utils.comparePassword(originalValue, comparedAtValue)
|
|
322
|
-
: false;
|
|
323
|
-
case "boolean":
|
|
324
|
-
return Number(originalValue) - Number(comparedAtValue) === 0;
|
|
325
|
-
default:
|
|
326
|
-
return Array.isArray(comparedAtValue)
|
|
327
|
-
? comparedAtValue.some((value) => originalValue === value)
|
|
328
|
-
: originalValue === comparedAtValue;
|
|
329
|
-
}
|
|
330
|
-
case "!=":
|
|
331
|
-
return !handleComparisonOperator(
|
|
332
|
-
"=",
|
|
333
|
-
originalValue,
|
|
334
|
-
comparedAtValue,
|
|
335
|
-
fieldType
|
|
336
|
-
);
|
|
337
|
-
case ">":
|
|
338
|
-
return Array.isArray(comparedAtValue)
|
|
339
|
-
? comparedAtValue.some((value) => originalValue > value)
|
|
340
|
-
: originalValue > comparedAtValue;
|
|
341
|
-
case "<":
|
|
342
|
-
return Array.isArray(comparedAtValue)
|
|
343
|
-
? comparedAtValue.some((value) => originalValue < value)
|
|
344
|
-
: originalValue < comparedAtValue;
|
|
345
|
-
case ">=":
|
|
346
|
-
return Array.isArray(comparedAtValue)
|
|
347
|
-
? comparedAtValue.some((value) => originalValue >= value)
|
|
348
|
-
: originalValue >= comparedAtValue;
|
|
349
|
-
case "<=":
|
|
350
|
-
return Array.isArray(comparedAtValue)
|
|
351
|
-
? comparedAtValue.some((value) => originalValue <= value)
|
|
352
|
-
: originalValue <= comparedAtValue;
|
|
353
|
-
case "[]":
|
|
354
|
-
return (
|
|
355
|
-
(Array.isArray(originalValue) &&
|
|
356
|
-
Array.isArray(comparedAtValue) &&
|
|
357
|
-
originalValue.some(comparedAtValue.includes)) ||
|
|
358
|
-
(Array.isArray(originalValue) &&
|
|
359
|
-
!Array.isArray(comparedAtValue) &&
|
|
360
|
-
originalValue.includes(comparedAtValue)) ||
|
|
361
|
-
(!Array.isArray(originalValue) &&
|
|
362
|
-
Array.isArray(comparedAtValue) &&
|
|
363
|
-
comparedAtValue.includes(originalValue))
|
|
364
|
-
);
|
|
365
|
-
case "![]":
|
|
366
|
-
return !handleComparisonOperator(
|
|
367
|
-
"[]",
|
|
368
|
-
originalValue,
|
|
369
|
-
comparedAtValue,
|
|
370
|
-
fieldType
|
|
371
|
-
);
|
|
372
|
-
case "*":
|
|
373
|
-
return Array.isArray(comparedAtValue)
|
|
374
|
-
? comparedAtValue.some((value) =>
|
|
375
|
-
new RegExp(
|
|
376
|
-
`^${(String(value).includes("%")
|
|
377
|
-
? String(value)
|
|
378
|
-
: "%" + String(value) + "%"
|
|
379
|
-
).replace(/%/g, ".*")}$`,
|
|
380
|
-
"i"
|
|
381
|
-
).test(String(originalValue))
|
|
382
|
-
)
|
|
383
|
-
: new RegExp(
|
|
384
|
-
`^${(String(comparedAtValue).includes("%")
|
|
385
|
-
? String(comparedAtValue)
|
|
386
|
-
: "%" + String(comparedAtValue) + "%"
|
|
387
|
-
).replace(/%/g, ".*")}$`,
|
|
388
|
-
"i"
|
|
389
|
-
).test(String(originalValue));
|
|
390
|
-
case "!*":
|
|
391
|
-
return !handleComparisonOperator(
|
|
392
|
-
"*",
|
|
393
|
-
originalValue,
|
|
394
|
-
comparedAtValue,
|
|
395
|
-
fieldType
|
|
396
|
-
);
|
|
397
|
-
default:
|
|
398
|
-
throw new Error(operator);
|
|
399
|
-
}
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
let RETURN: Record<
|
|
403
|
-
number,
|
|
404
|
-
Record<
|
|
405
|
-
string,
|
|
406
|
-
string | number | boolean | null | (string | number | boolean | null)[]
|
|
407
|
-
>
|
|
408
|
-
> = {},
|
|
409
|
-
lineCount = 0,
|
|
410
|
-
foundItems = 0;
|
|
411
|
-
let rl: Interface;
|
|
412
|
-
if (doesSupportReadLines()) rl = (await open(filePath)).readLines();
|
|
413
|
-
else
|
|
414
|
-
rl = createInterface({
|
|
415
|
-
input: createReadStream(filePath),
|
|
416
|
-
crlfDelay: Infinity,
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
const columnName = decodeFileName(parse(filePath).name);
|
|
420
|
-
|
|
421
|
-
for await (const line of rl) {
|
|
422
|
-
lineCount++;
|
|
423
|
-
const decodedLine = decode(line, fieldType, fieldChildrenType);
|
|
424
|
-
if (
|
|
425
|
-
(Array.isArray(operator) &&
|
|
426
|
-
Array.isArray(comparedAtValue) &&
|
|
427
|
-
((logicalOperator &&
|
|
428
|
-
logicalOperator === "or" &&
|
|
429
|
-
operator.some((single_operator, index) =>
|
|
430
|
-
handleComparisonOperator(
|
|
431
|
-
single_operator,
|
|
432
|
-
decodedLine,
|
|
433
|
-
comparedAtValue[index],
|
|
434
|
-
fieldType
|
|
435
|
-
)
|
|
436
|
-
)) ||
|
|
437
|
-
operator.every((single_operator, index) =>
|
|
438
|
-
handleComparisonOperator(
|
|
439
|
-
single_operator,
|
|
440
|
-
decodedLine,
|
|
441
|
-
comparedAtValue[index],
|
|
442
|
-
fieldType
|
|
443
|
-
)
|
|
444
|
-
))) ||
|
|
445
|
-
(!Array.isArray(operator) &&
|
|
446
|
-
handleComparisonOperator(
|
|
447
|
-
operator,
|
|
448
|
-
decodedLine,
|
|
449
|
-
comparedAtValue,
|
|
450
|
-
fieldType
|
|
451
|
-
))
|
|
452
|
-
) {
|
|
453
|
-
foundItems++;
|
|
454
|
-
if (offset && foundItems < offset) continue;
|
|
455
|
-
if (limit && foundItems > limit)
|
|
456
|
-
if (readWholeFile) continue;
|
|
457
|
-
else break;
|
|
458
|
-
if (!RETURN[lineCount]) RETURN[lineCount] = {};
|
|
459
|
-
RETURN[lineCount][columnName] = decodedLine;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
if (foundItems) {
|
|
463
|
-
return [RETURN, readWholeFile ? foundItems : foundItems - 1];
|
|
464
|
-
} else return [null, 0];
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
export default class File {
|
|
468
|
-
static get = get;
|
|
469
|
-
static remove = remove;
|
|
470
|
-
static search = search;
|
|
471
|
-
static replace = replace;
|
|
472
|
-
static count = count;
|
|
473
|
-
static encode = encode;
|
|
474
|
-
static decode = decode;
|
|
475
|
-
static encodeFileName = encodeFileName;
|
|
476
|
-
static decodeFileName = decodeFileName;
|
|
477
|
-
}
|
package/index.test.ts
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import Inibase, { Schema, Data } from "./index";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
// import os from "os";
|
|
4
|
-
// Create a new instance of Inibase with the database path
|
|
5
|
-
const db = new Inibase("inibase", join("..", "inicontent", "databases"));
|
|
6
|
-
|
|
7
|
-
// test with edited schema
|
|
8
|
-
const schema_1: Schema = [
|
|
9
|
-
{
|
|
10
|
-
key: "username",
|
|
11
|
-
type: "string",
|
|
12
|
-
required: true,
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
key: "email",
|
|
16
|
-
type: "string",
|
|
17
|
-
required: true,
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
key: "age",
|
|
21
|
-
type: "number",
|
|
22
|
-
required: true,
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
key: "isActive",
|
|
26
|
-
type: "boolean",
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
key: "hobbies",
|
|
30
|
-
type: "array",
|
|
31
|
-
children: [
|
|
32
|
-
{
|
|
33
|
-
key: "name",
|
|
34
|
-
type: "string",
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
key: "level",
|
|
38
|
-
type: "string",
|
|
39
|
-
},
|
|
40
|
-
],
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
key: "favoriteFoods",
|
|
44
|
-
type: "array",
|
|
45
|
-
children: "string",
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
key: "address",
|
|
49
|
-
type: "object",
|
|
50
|
-
children: [
|
|
51
|
-
{
|
|
52
|
-
key: "street",
|
|
53
|
-
type: "string",
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
key: "city",
|
|
57
|
-
type: "string",
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
key: "country",
|
|
61
|
-
type: "string",
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
},
|
|
65
|
-
];
|
|
66
|
-
|
|
67
|
-
const data_1: Data = [
|
|
68
|
-
{
|
|
69
|
-
id: 1,
|
|
70
|
-
username: "user1",
|
|
71
|
-
email: "user1@example.com",
|
|
72
|
-
age: 25,
|
|
73
|
-
isActive: true,
|
|
74
|
-
hobbies: [
|
|
75
|
-
{ name: "Reading", level: "Intermediate" },
|
|
76
|
-
{ name: "Cooking", level: "Beginner" },
|
|
77
|
-
],
|
|
78
|
-
favoriteFoods: ["Pizza", "Sushi", "Chocolate"],
|
|
79
|
-
address: {
|
|
80
|
-
street: "123 Main St",
|
|
81
|
-
city: "Exampleville",
|
|
82
|
-
country: "Sampleland",
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
id: 2,
|
|
87
|
-
username: "user2",
|
|
88
|
-
email: "user2@example.com",
|
|
89
|
-
age: 30,
|
|
90
|
-
isActive: false,
|
|
91
|
-
hobbies: [
|
|
92
|
-
{ name: "Gardening", level: "Advanced" },
|
|
93
|
-
{ name: "Photography", level: "Intermediate" },
|
|
94
|
-
],
|
|
95
|
-
favoriteFoods: ["Burgers", null, "Salad"],
|
|
96
|
-
address: {
|
|
97
|
-
street: "456 Elm Rd",
|
|
98
|
-
city: "Testington",
|
|
99
|
-
country: "Demo Country",
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
id: 3,
|
|
104
|
-
username: "user3",
|
|
105
|
-
email: "user3@example.com",
|
|
106
|
-
age: 22,
|
|
107
|
-
isActive: true,
|
|
108
|
-
hobbies: [
|
|
109
|
-
{ name: "Painting", level: "Beginner" },
|
|
110
|
-
{ name: "Hiking", level: null },
|
|
111
|
-
],
|
|
112
|
-
favoriteFoods: ["Pasta", "Fruit", "Cakes"],
|
|
113
|
-
address: {
|
|
114
|
-
street: "789 Oak Ave",
|
|
115
|
-
city: "Sampletown",
|
|
116
|
-
country: "Testland",
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
];
|
|
120
|
-
|
|
121
|
-
const schema_2: Schema = [
|
|
122
|
-
{
|
|
123
|
-
key: "title",
|
|
124
|
-
type: "string",
|
|
125
|
-
required: true,
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
key: "price",
|
|
129
|
-
type: "number",
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
key: "user",
|
|
133
|
-
type: "table",
|
|
134
|
-
required: true,
|
|
135
|
-
},
|
|
136
|
-
];
|
|
137
|
-
|
|
138
|
-
const data_2 = [
|
|
139
|
-
{
|
|
140
|
-
title: "Product 1",
|
|
141
|
-
price: 16,
|
|
142
|
-
user: 1,
|
|
143
|
-
},
|
|
144
|
-
{
|
|
145
|
-
title: "Product 2",
|
|
146
|
-
price: 10,
|
|
147
|
-
user: 2,
|
|
148
|
-
},
|
|
149
|
-
];
|
|
150
|
-
|
|
151
|
-
const schema_3: Schema = [
|
|
152
|
-
{
|
|
153
|
-
key: "orderN",
|
|
154
|
-
type: "number",
|
|
155
|
-
},
|
|
156
|
-
{
|
|
157
|
-
key: "isActive",
|
|
158
|
-
type: "boolean",
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
key: "products",
|
|
162
|
-
type: "array",
|
|
163
|
-
children: [
|
|
164
|
-
{
|
|
165
|
-
key: "name",
|
|
166
|
-
type: "string",
|
|
167
|
-
},
|
|
168
|
-
{
|
|
169
|
-
key: "variants",
|
|
170
|
-
type: "array",
|
|
171
|
-
children: [
|
|
172
|
-
{
|
|
173
|
-
key: "name",
|
|
174
|
-
type: "string",
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
key: "price",
|
|
178
|
-
type: "number",
|
|
179
|
-
},
|
|
180
|
-
],
|
|
181
|
-
},
|
|
182
|
-
],
|
|
183
|
-
},
|
|
184
|
-
];
|
|
185
|
-
|
|
186
|
-
const data_3 = {
|
|
187
|
-
orderN: 5,
|
|
188
|
-
isActive: true,
|
|
189
|
-
products: [
|
|
190
|
-
{
|
|
191
|
-
name: "Product 1",
|
|
192
|
-
variants: [
|
|
193
|
-
{
|
|
194
|
-
name: "Variant 1",
|
|
195
|
-
price: 10,
|
|
196
|
-
},
|
|
197
|
-
{
|
|
198
|
-
name: "Variant 2",
|
|
199
|
-
price: 15,
|
|
200
|
-
},
|
|
201
|
-
],
|
|
202
|
-
},
|
|
203
|
-
{
|
|
204
|
-
name: "Product 2",
|
|
205
|
-
variants: [
|
|
206
|
-
{
|
|
207
|
-
name: "Variant 3",
|
|
208
|
-
price: 78,
|
|
209
|
-
},
|
|
210
|
-
{
|
|
211
|
-
name: "Variant 4",
|
|
212
|
-
price: 456,
|
|
213
|
-
},
|
|
214
|
-
],
|
|
215
|
-
},
|
|
216
|
-
],
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
try {
|
|
220
|
-
// const DATA = await db.post("user", data_1);
|
|
221
|
-
// const DATA = await db.get("user", { favoriteFoods: "![]Pizza" });
|
|
222
|
-
// db.setTableSchema("user", schema_1);
|
|
223
|
-
// const DATA = await db.get("product", "4", {
|
|
224
|
-
// columns: [
|
|
225
|
-
// "title",
|
|
226
|
-
// "user",
|
|
227
|
-
// "user.username",
|
|
228
|
-
// "user.address.street",
|
|
229
|
-
// "user.hobbies.*.name",
|
|
230
|
-
// ],
|
|
231
|
-
// });
|
|
232
|
-
// return os.platform();
|
|
233
|
-
// const DATA = await db.get("user", {
|
|
234
|
-
// "hobbies.*.name": "*Reading,",
|
|
235
|
-
// });
|
|
236
|
-
// db.setTableSchema("order", schema_3);
|
|
237
|
-
const DATA = await db.get("product", "7a6f37d12fac6d4dd082a69e92fc1f07", {
|
|
238
|
-
columns: [
|
|
239
|
-
"!user.favoriteFoods",
|
|
240
|
-
"!user.username",
|
|
241
|
-
"*",
|
|
242
|
-
"!user.hobbies.*.name",
|
|
243
|
-
],
|
|
244
|
-
});
|
|
245
|
-
console.log(JSON.stringify(DATA, null, 4));
|
|
246
|
-
} catch (er) {
|
|
247
|
-
console.log(er);
|
|
248
|
-
}
|