inibase 1.0.0-rc.98 → 1.1.0

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/utils.js CHANGED
@@ -318,16 +318,17 @@ export const validateFieldType = (value, fieldType, fieldChildrenType) => {
318
318
  fieldType = detectedFieldType;
319
319
  }
320
320
  if (fieldType === "array" && fieldChildrenType)
321
- return value.every((v) => {
322
- let _fieldChildrenType = fieldChildrenType;
323
- if (Array.isArray(_fieldChildrenType)) {
324
- const detectedFieldType = detectFieldType(v, _fieldChildrenType);
325
- if (!detectedFieldType)
326
- return false;
327
- _fieldChildrenType = detectedFieldType;
328
- }
329
- return validateFieldType(v, _fieldChildrenType);
330
- });
321
+ return (Array.isArray(value) &&
322
+ value.every((v) => {
323
+ let _fieldChildrenType = fieldChildrenType;
324
+ if (Array.isArray(_fieldChildrenType)) {
325
+ const detectedFieldType = detectFieldType(v, _fieldChildrenType);
326
+ if (!detectedFieldType)
327
+ return false;
328
+ _fieldChildrenType = detectedFieldType;
329
+ }
330
+ return validateFieldType(v, _fieldChildrenType);
331
+ }));
331
332
  switch (fieldType) {
332
333
  case "string":
333
334
  return isString(value);
@@ -1,6 +1,7 @@
1
1
  import { exec as execSync, execFile as execFileSync } from "node:child_process";
2
2
  import { gunzip as gunzipSync, gzip as gzipSync } from "node:zlib";
3
3
  import type { ComparisonOperator, Field, FieldType, Schema } from "./index.js";
4
+ import RE2 from "re2";
4
5
  export declare const exec: typeof execSync.__promisify__;
5
6
  export declare const execFile: typeof execFileSync.__promisify__;
6
7
  export declare const gzip: typeof gzipSync.__promisify__;
@@ -35,12 +36,14 @@ export declare const findLastIdNumber: (schema: Schema, secretKeyOrSalt: string
35
36
  * Adds or updates IDs in a schema, encoding them using a provided secret key or salt.
36
37
  *
37
38
  * @param schema - The schema to update, defined as an array of schema objects.
38
- * @param oldIndex - The starting index for generating new IDs, defaults to 0.
39
+ * @param startWithID - An object containing the starting ID for generating new IDs.
39
40
  * @param secretKeyOrSalt - The secret key or salt for encoding IDs, can be a string, number, or Buffer.
40
41
  * @param encodeIDs - If true, IDs will be encoded, else they will remain as numbers.
41
42
  * @returns The updated schema with encoded IDs.
42
43
  */
43
- export declare const addIdToSchema: (schema: Schema, startWithID: number, secretKeyOrSalt: string | number | Buffer, encodeIDs?: boolean) => Field[];
44
+ export declare const addIdToSchema: (schema: Schema, startWithID: {
45
+ value: number;
46
+ }, secretKeyOrSalt: string | number | Buffer, encodeIDs?: boolean) => Field[];
44
47
  export declare const encodeSchemaID: (schema: Schema, secretKeyOrSalt: string | number | Buffer) => Schema;
45
48
  export declare const hashString: (str: string) => string;
46
49
  /**
@@ -81,3 +84,15 @@ export declare const isArrayEqual: (originalValue: string | number | boolean | n
81
84
  * @returns boolean - Result of the wildcard pattern matching.
82
85
  */
83
86
  export declare const isWildcardMatch: (originalValue: string | number | boolean | null | (string | number | boolean | null)[], comparedValue: string | number | boolean | null | (string | number | boolean | null)[]) => boolean;
87
+ /**
88
+ * Retrieves a cached compiled regex or compiles and caches a new one.
89
+ *
90
+ * This function checks if a given regex pattern is already compiled and cached.
91
+ * If it is, the cached instance is returned. If not, the function attempts to compile
92
+ * the regex using RE2, caches the compiled instance, and then returns it. If the pattern
93
+ * is invalid, it returns a fallback object with a `test` method that always returns `false`.
94
+ *
95
+ * @param {string} pattern - The regex pattern to compile or retrieve from the cache.
96
+ * @returns {RE2} - The compiled regex instance or a fallback object on error.
97
+ */
98
+ export declare const getCachedRegex: (pattern: string) => RE2;
@@ -3,6 +3,7 @@ import { createCipheriv, createDecipheriv, createHash, randomBytes, scryptSync,
3
3
  import { gunzip as gunzipSync, gzip as gzipSync } from "node:zlib";
4
4
  import { promisify } from "node:util";
5
5
  import { detectFieldType, isArrayOfObjects, isNumber, isPassword, isValidID, } from "./utils.js";
6
+ import RE2 from "re2";
6
7
  export const exec = promisify(execSync);
7
8
  export const execFile = promisify(execFileSync);
8
9
  export const gzip = promisify(gzipSync);
@@ -88,7 +89,7 @@ export const findLastIdNumber = (schema, secretKeyOrSalt) => Math.max(...extract
88
89
  * Adds or updates IDs in a schema, encoding them using a provided secret key or salt.
89
90
  *
90
91
  * @param schema - The schema to update, defined as an array of schema objects.
91
- * @param oldIndex - The starting index for generating new IDs, defaults to 0.
92
+ * @param startWithID - An object containing the starting ID for generating new IDs.
92
93
  * @param secretKeyOrSalt - The secret key or salt for encoding IDs, can be a string, number, or Buffer.
93
94
  * @param encodeIDs - If true, IDs will be encoded, else they will remain as numbers.
94
95
  * @returns The updated schema with encoded IDs.
@@ -96,10 +97,10 @@ export const findLastIdNumber = (schema, secretKeyOrSalt) => Math.max(...extract
96
97
  export const addIdToSchema = (schema, startWithID, secretKeyOrSalt, encodeIDs) => {
97
98
  function _addIdToField(field) {
98
99
  if (!field.id) {
99
- startWithID++;
100
+ startWithID.value++;
100
101
  field.id = encodeIDs
101
- ? encodeID(startWithID, secretKeyOrSalt)
102
- : startWithID;
102
+ ? encodeID(startWithID.value, secretKeyOrSalt)
103
+ : startWithID.value;
103
104
  }
104
105
  else {
105
106
  if (isValidID(field.id)) {
@@ -209,8 +210,20 @@ export const isEqual = (originalValue, comparedValue, fieldType) => {
209
210
  : false;
210
211
  case "boolean":
211
212
  return Number(originalValue) === Number(comparedValue);
212
- default:
213
- return originalValue == comparedValue;
213
+ default: {
214
+ // Fast checks for null-like values
215
+ const isOriginalNullLike = originalValue === null ||
216
+ originalValue === undefined ||
217
+ originalValue === "";
218
+ const isComparedNullLike = comparedValue === null ||
219
+ comparedValue === undefined ||
220
+ comparedValue === "";
221
+ // If both are null-like, treat as equivalent
222
+ if (isOriginalNullLike && isComparedNullLike)
223
+ return true;
224
+ // Direct equality check for other cases
225
+ return originalValue === comparedValue;
226
+ }
214
227
  }
215
228
  };
216
229
  /**
@@ -246,3 +259,28 @@ export const isWildcardMatch = (originalValue, comparedValue) => {
246
259
  const wildcardPattern = `^${(comparedValueStr.includes("%") ? comparedValueStr : `%${comparedValueStr}%`).replace(/%/g, ".*")}$`;
247
260
  return new RegExp(wildcardPattern, "i").test(originalValueStr);
248
261
  };
262
+ const regexCache = new Map();
263
+ /**
264
+ * Retrieves a cached compiled regex or compiles and caches a new one.
265
+ *
266
+ * This function checks if a given regex pattern is already compiled and cached.
267
+ * If it is, the cached instance is returned. If not, the function attempts to compile
268
+ * the regex using RE2, caches the compiled instance, and then returns it. If the pattern
269
+ * is invalid, it returns a fallback object with a `test` method that always returns `false`.
270
+ *
271
+ * @param {string} pattern - The regex pattern to compile or retrieve from the cache.
272
+ * @returns {RE2} - The compiled regex instance or a fallback object on error.
273
+ */
274
+ export const getCachedRegex = (pattern) => {
275
+ if (regexCache.has(pattern)) {
276
+ return regexCache.get(pattern);
277
+ }
278
+ try {
279
+ const compiledRegex = new RE2(pattern);
280
+ regexCache.set(pattern, compiledRegex);
281
+ return compiledRegex;
282
+ }
283
+ catch {
284
+ return { test: (_str) => false };
285
+ }
286
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.0.0-rc.98",
3
+ "version": "1.1.0",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Karim Amahtil",
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "description": "A file-based & memory-efficient, serverless, ACID compliant, relational database management system",
21
21
  "engines": {
22
- "node": ">=16"
22
+ "node": ">=22"
23
23
  },
24
24
  "files": [
25
25
  "/dist"
@@ -68,18 +68,22 @@
68
68
  }
69
69
  },
70
70
  "devDependencies": {
71
+ "@biomejs/biome": "1.9.4",
72
+ "@evilmartians/lefthook": "^1.10.1",
71
73
  "@types/bun": "^1.1.10",
72
74
  "@types/node": "^22.7.4",
73
- "tinybench": "^2.6.0",
74
- "typescript": "^5.6.2"
75
+ "tinybench": "^3.0.7",
76
+ "typescript": "^5.7.2"
75
77
  },
76
78
  "dependencies": {
77
79
  "dotenv": "^16.4.5",
78
- "inison": "1.0.0-rc.4"
80
+ "inison": "latest",
81
+ "re2": "^1.21.4"
79
82
  },
80
83
  "scripts": {
81
- "prepublish": "npx tsc",
82
- "build": "npx tsc",
83
- "benchmark": "./benchmark/run.js"
84
+ "prepublish": "npx -q tsc",
85
+ "build": "npx -q tsc",
86
+ "benchmark": "./benchmark/run.js",
87
+ "test": "npx -q tsx ./tests/inibase.test.ts"
84
88
  }
85
89
  }