yolodb 1.1.0 → 1.2.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/{index.d.ts → index.d.mts} +6 -4
- package/dist/{index.js → index.mjs} +13 -20
- package/dist/index.mjs.map +1 -0
- package/package.json +18 -25
- package/dist/index.js.map +0 -1
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
//#region src/yolodb.d.ts
|
|
2
|
-
|
|
2
|
+
declare const logger: Console;
|
|
3
|
+
type YoloDbDebugLogger = typeof logger.debug;
|
|
3
4
|
declare class YoloDbError extends Error {
|
|
4
5
|
constructor(message: string);
|
|
5
6
|
}
|
|
6
7
|
type YoloDbTableOptions = {
|
|
7
|
-
|
|
8
|
+
superjsonEnabled?: boolean;
|
|
9
|
+
debugLogger?: YoloDbDebugLogger;
|
|
8
10
|
};
|
|
9
11
|
/**
|
|
10
12
|
* Simple file-based JSON database built on top of SuperJSON.
|
|
@@ -42,5 +44,5 @@ declare class YoloDbRepository<R extends Record<string, any>> {
|
|
|
42
44
|
*/
|
|
43
45
|
declare function yolodb<R extends Record<string, any>>(filePath: string, pkField: keyof R, initialData: R[], options?: YoloDbTableOptions): YoloDbTable<R>;
|
|
44
46
|
//#endregion
|
|
45
|
-
export {
|
|
46
|
-
//# sourceMappingURL=index.d.
|
|
47
|
+
export { YoloDbDebugLogger, YoloDbError, YoloDbRepository, YoloDbTable, YoloDbTableOptions, yolodb };
|
|
48
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -3,6 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import superjson from "superjson";
|
|
4
4
|
|
|
5
5
|
//#region src/yolodb.ts
|
|
6
|
+
const logger = console;
|
|
6
7
|
var YoloDbError = class extends Error {
|
|
7
8
|
constructor(message) {
|
|
8
9
|
super(`[YoloDB] ${message}`);
|
|
@@ -13,10 +14,6 @@ var YoloDbError = class extends Error {
|
|
|
13
14
|
* Simple file-based JSON database built on top of SuperJSON.
|
|
14
15
|
*/
|
|
15
16
|
var YoloDbTable = class {
|
|
16
|
-
tableName;
|
|
17
|
-
pkField;
|
|
18
|
-
filePath;
|
|
19
|
-
options;
|
|
20
17
|
constructor(absoluteFilePath, pkField, initialData = [], options) {
|
|
21
18
|
const dirName = path.dirname(absoluteFilePath);
|
|
22
19
|
if (!fs.existsSync(dirName)) fs.mkdirSync(dirName, { recursive: true });
|
|
@@ -24,13 +21,14 @@ var YoloDbTable = class {
|
|
|
24
21
|
this.pkField = pkField;
|
|
25
22
|
this.filePath = absoluteFilePath;
|
|
26
23
|
this.options = {
|
|
27
|
-
|
|
24
|
+
debugLogger: (message, ctx) => logger.debug(`[YoloDB] ${this.tableName} - ${message}`, ctx),
|
|
25
|
+
superjsonEnabled: true,
|
|
28
26
|
...options
|
|
29
27
|
};
|
|
30
28
|
if (!fs.existsSync(absoluteFilePath)) this.saveFile(initialData);
|
|
31
29
|
}
|
|
32
30
|
get log() {
|
|
33
|
-
return this.options.
|
|
31
|
+
return this.options.debugLogger;
|
|
34
32
|
}
|
|
35
33
|
getData() {
|
|
36
34
|
const db = this.readFile();
|
|
@@ -47,31 +45,27 @@ var YoloDbTable = class {
|
|
|
47
45
|
return [];
|
|
48
46
|
}
|
|
49
47
|
const fileContent = fs.readFileSync(this.filePath, "utf8");
|
|
50
|
-
return superjson.parse(fileContent);
|
|
48
|
+
return this.options.superjsonEnabled ? superjson.parse(fileContent) : JSON.parse(fileContent);
|
|
51
49
|
}
|
|
52
50
|
saveFile(db) {
|
|
53
51
|
this.log(`Saving table to ${this.filePath}`);
|
|
54
|
-
|
|
52
|
+
const serializedDb = this.options.superjsonEnabled ? JSON.stringify(superjson.serialize(db), null, 2) : JSON.stringify(db, null, 2);
|
|
53
|
+
fs.writeFileSync(this.filePath, serializedDb);
|
|
55
54
|
}
|
|
56
55
|
findById(id) {
|
|
57
|
-
|
|
58
|
-
return db.find((record) => record[this.pkField] === id);
|
|
56
|
+
return this.getData().find((record) => record[this.pkField] === id);
|
|
59
57
|
}
|
|
60
58
|
findBy(field, value) {
|
|
61
|
-
|
|
62
|
-
return db.filter((record) => record[field] === value);
|
|
59
|
+
return this.getData().filter((record) => record[field] === value);
|
|
63
60
|
}
|
|
64
61
|
findFirstBy(field, value) {
|
|
65
|
-
|
|
66
|
-
return db.find((record) => record[field] === value);
|
|
62
|
+
return this.getData().find((record) => record[field] === value);
|
|
67
63
|
}
|
|
68
64
|
search(filterFn) {
|
|
69
|
-
|
|
70
|
-
return db.filter((record) => filterFn(record));
|
|
65
|
+
return this.getData().filter((record) => filterFn(record));
|
|
71
66
|
}
|
|
72
67
|
insert(record) {
|
|
73
|
-
|
|
74
|
-
if (!pk) throw new YoloDbError(`Record does not have a primary key: ${this.tableName}.${String(this.pkField)}`);
|
|
68
|
+
if (!record[this.pkField]) throw new YoloDbError(`Record does not have a primary key: ${this.tableName}.${String(this.pkField)}`);
|
|
75
69
|
const db = this.getData();
|
|
76
70
|
db.push(record);
|
|
77
71
|
this.saveFile(db);
|
|
@@ -107,7 +101,6 @@ var YoloDbTable = class {
|
|
|
107
101
|
}
|
|
108
102
|
};
|
|
109
103
|
var YoloDbRepository = class {
|
|
110
|
-
table;
|
|
111
104
|
constructor(dataPath, pkField) {
|
|
112
105
|
this.table = yolodb(dataPath, pkField, []);
|
|
113
106
|
}
|
|
@@ -124,4 +117,4 @@ function yolodb(filePath, pkField, initialData, options) {
|
|
|
124
117
|
|
|
125
118
|
//#endregion
|
|
126
119
|
export { YoloDbError, YoloDbRepository, YoloDbTable, yolodb };
|
|
127
|
-
//# sourceMappingURL=index.
|
|
120
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/yolodb.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\nimport superjson from 'superjson'\n\nconst logger = console\n\nexport type YoloDbDebugLogger = typeof logger.debug\n\nexport class YoloDbError extends Error {\n constructor(message: string) {\n super(`[YoloDB] ${message}`)\n this.name = 'YoloDbError'\n }\n}\n\nexport type YoloDbTableOptions = {\n superjsonEnabled?: boolean\n debugLogger?: YoloDbDebugLogger\n}\n\n/**\n * Simple file-based JSON database built on top of SuperJSON.\n */\nexport class YoloDbTable<R extends Record<string, any>> {\n private readonly tableName: string\n private readonly pkField: keyof R\n private readonly filePath: string\n private readonly options: Required<YoloDbTableOptions>\n\n constructor(absoluteFilePath: string, pkField: keyof R, initialData: R[] = [], options?: YoloDbTableOptions) {\n const dirName = path.dirname(absoluteFilePath)\n\n if (!fs.existsSync(dirName)) {\n fs.mkdirSync(dirName, { recursive: true })\n }\n\n this.tableName = path.basename(absoluteFilePath).replace('.json', '')\n this.pkField = pkField\n this.filePath = absoluteFilePath\n\n this.options = {\n debugLogger: (message, ctx) => logger.debug(`[YoloDB] ${this.tableName} - ${message}`, ctx),\n superjsonEnabled: true,\n ...options,\n }\n\n if (!fs.existsSync(absoluteFilePath)) {\n this.saveFile(initialData)\n }\n }\n\n private get log(): YoloDbDebugLogger {\n return this.options.debugLogger\n }\n\n private getData(): R[] {\n const db = this.readFile()\n if (!Array.isArray(db)) {\n throw new YoloDbError(`Invalid data in ${this.filePath}`)\n }\n return db\n }\n\n all(): R[] {\n return this.getData()\n }\n\n readFile(): R[] {\n this.log(`Reading table from ${this.filePath}`)\n\n if (!fs.existsSync(this.filePath)) {\n this.saveFile([])\n return []\n }\n\n const fileContent = fs.readFileSync(this.filePath, 'utf8')\n return this.options.superjsonEnabled ? superjson.parse<R[]>(fileContent) : JSON.parse(fileContent)\n }\n\n saveFile(db: R[]): void {\n this.log(`Saving table to ${this.filePath}`)\n const serializedDb = this.options.superjsonEnabled\n ? JSON.stringify(superjson.serialize(db), null, 2)\n : JSON.stringify(db, null, 2)\n fs.writeFileSync(this.filePath, serializedDb)\n }\n\n findById(id: string): R | undefined {\n const db = this.getData()\n return db.find((record) => record[this.pkField] === id)\n }\n\n findBy(field: keyof R, value: any): R[] {\n const db = this.getData()\n return db.filter((record) => record[field] === value)\n }\n\n findFirstBy(field: keyof R, value: any): R | undefined {\n const db = this.getData()\n return db.find((record) => record[field] === value)\n }\n\n search(filterFn: (record: R) => boolean): R[] {\n const db = this.getData()\n return db.filter((record) => filterFn(record))\n }\n\n insert(record: R): void {\n const pk = record[this.pkField]\n if (!pk) {\n throw new YoloDbError(`Record does not have a primary key: ${this.tableName}.${String(this.pkField)}`)\n }\n\n const db = this.getData()\n db.push(record)\n\n this.saveFile(db)\n }\n\n insertMany(records: R[]): void {\n const db = this.getData()\n db.push(...records)\n\n this.saveFile(db)\n }\n\n update(record: Partial<R>): void {\n const pk = record[this.pkField]\n if (!pk) {\n throw new YoloDbError(`Record not found in ${this.tableName}: ${pk}`)\n }\n\n const db = this.getData()\n\n for (const currentRecord of db) {\n if (currentRecord[this.pkField] === pk) {\n Object.assign(currentRecord, record)\n break\n }\n }\n\n this.saveFile(db)\n }\n\n updateMany(records: Array<Partial<R>>): void {\n records.forEach((record) => this.update(record))\n }\n\n delete(id: string): void {\n this.deleteMany([id])\n }\n\n deleteMany(ids: string[]): void {\n this.log(`Deleting ${ids.length} records`)\n const db = this.getData().filter((record) => !ids.includes(record[this.pkField]))\n\n this.saveFile(db)\n }\n\n truncate(): void {\n this.saveFile([])\n }\n}\n\nexport class YoloDbRepository<R extends Record<string, any>> {\n protected table: YoloDbTable<R>\n\n constructor(dataPath: string, pkField: keyof R) {\n this.table = yolodb<R>(dataPath, pkField, [])\n }\n}\n\n// In-memory cache of YoloDB table instances\nconst _yoloDbTables: Record<string, YoloDbTable<any>> = {}\n\n/**\n * Helper function to create a new YoloDB table.\n * It reuses the same table instance if the same file path is used.\n */\nexport function yolodb<R extends Record<string, any>>(\n filePath: string,\n pkField: keyof R,\n initialData: R[],\n options?: YoloDbTableOptions,\n): YoloDbTable<R> {\n if (!_yoloDbTables[filePath]) {\n _yoloDbTables[filePath] = new YoloDbTable<R>(filePath, pkField, initialData, options)\n }\n return _yoloDbTables[fileP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AC9B,OAAK,IAAI,YAAY,IAAI,OAAO,UAAU;EAC1C,MAAM,KAAK,KAAK,SAAS,CAAC,QAAQ,WAAW,CAAC,IAAI,SAAS,OAAO,KAAK,SAAS,CAAC;AAEjF,OAAK,SAAS,GAAG;;CAGnB,WAAiB;AACf,OAAK,SAAS,EAAE,CAAC;;;AAIrB,IAAa,mBAAb,MAA6D;CAG3D,YAAY,UAAkB,SAAkB;AAC9C,OAAK,QAAQ,OAAU,UAAU,SAAS,EAAE,CAAC;;;AAKjD,MAAM,gBAAkD,EAAE;;;;;AAM1D,SAAgB,OACd,UACA,SACA,aACA,SACgB;AAChB,KAAI,CAAC,cAAc,UACjB,eAAc,YAAY,IAAI,YAAe,UAAU,SAAS,aAAa,QAAQ;AAEvF,QAAO,cAAc"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yolodb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"homepage": "https://itsjavi.com/yolodb",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,40 +10,33 @@
|
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
-
"import": "./dist/index.
|
|
14
|
-
"require": "./dist/index.
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.mjs"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
|
-
"main": "./dist/index.
|
|
18
|
-
"module": "./dist/index.
|
|
19
|
-
"types": "./dist/index.d.
|
|
17
|
+
"main": "./dist/index.mjs",
|
|
18
|
+
"module": "./dist/index.mjs",
|
|
19
|
+
"types": "./dist/index.d.mts",
|
|
20
20
|
"files": [
|
|
21
21
|
"dist",
|
|
22
22
|
"README.md",
|
|
23
23
|
"LICENSE"
|
|
24
24
|
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"superjson": "^2.2.6"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^25.0.3",
|
|
30
|
+
"prettier": "^3.7.4",
|
|
31
|
+
"publint": "^0.3.16",
|
|
32
|
+
"sort-package-json": "^3.6.0",
|
|
33
|
+
"tsdown": "0.19.0-beta.2",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
},
|
|
25
36
|
"scripts": {
|
|
26
37
|
"build": "tsdown src/index.ts --dts --sourcemap --format esm --clean",
|
|
27
38
|
"format": "sort-package-json && prettier --write ./src README.md tsconfig.json",
|
|
28
39
|
"lint": "pnpm run typecheck && prettier --check ./src README.md tsconfig.json && publint",
|
|
29
|
-
"prepare": "sort-package-json",
|
|
30
|
-
"prepublishOnly": "pnpm run lint && pnpm run build",
|
|
31
40
|
"typecheck": "tsc --noEmit"
|
|
32
|
-
},
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"superjson": "^2.2.2"
|
|
35
|
-
},
|
|
36
|
-
"devDependencies": {
|
|
37
|
-
"@types/node": "^24.0.12",
|
|
38
|
-
"prettier": "^3.6.2",
|
|
39
|
-
"publint": "^0.3.12",
|
|
40
|
-
"sort-package-json": "^3.4.0",
|
|
41
|
-
"tsdown": "^0.12.9",
|
|
42
|
-
"typescript": "^5.8.3"
|
|
43
|
-
},
|
|
44
|
-
"pnpm": {
|
|
45
|
-
"onlyBuiltDependencies": [
|
|
46
|
-
"esbuild"
|
|
47
|
-
]
|
|
48
41
|
}
|
|
49
|
-
}
|
|
42
|
+
}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["message: string","absoluteFilePath: string","pkField: keyof R","initialData: R[]","options?: YoloDbTableOptions","db: R[]","id: string","field: keyof R","value: any","filterFn: (record: R) => boolean","record: R","records: R[]","record: Partial<R>","records: Array<Partial<R>>","ids: string[]","dataPath: string","_yoloDbTables: Record<string, YoloDbTable<any>>","filePath: string"],"sources":["../src/yolodb.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\nimport superjson from 'superjson'\n\nexport type YoloDbLogger = (...args: any[]) => void\n\nexport class YoloDbError extends Error {\n constructor(message: string) {\n super(`[YoloDB] ${message}`)\n this.name = 'YoloDbError'\n }\n}\n\nexport type YoloDbTableOptions = {\n logger?: YoloDbLogger\n}\n\n/**\n * Simple file-based JSON database built on top of SuperJSON.\n */\nexport class YoloDbTable<R extends Record<string, any>> {\n private readonly tableName: string\n private readonly pkField: keyof R\n private readonly filePath: string\n private readonly options: Required<YoloDbTableOptions>\n\n constructor(absoluteFilePath: string, pkField: keyof R, initialData: R[] = [], options?: YoloDbTableOptions) {\n const dirName = path.dirname(absoluteFilePath)\n\n if (!fs.existsSync(dirName)) {\n fs.mkdirSync(dirName, { recursive: true })\n }\n\n this.tableName = path.basename(absoluteFilePath).replace('.json', '')\n this.pkField = pkField\n this.filePath = absoluteFilePath\n\n this.options = {\n logger: (...args) => console.debug(`[YoloDB] ${this.tableName}`, ...args),\n ...options,\n }\n\n if (!fs.existsSync(absoluteFilePath)) {\n this.saveFile(initialData)\n }\n }\n\n private get log(): YoloDbLogger {\n return this.options.logger\n }\n\n private getData(): R[] {\n const db = this.readFile()\n if (!Array.isArray(db)) {\n throw new YoloDbError(`Invalid data in ${this.filePath}`)\n }\n return db\n }\n\n all(): R[] {\n return this.getData()\n }\n\n readFile(): R[] {\n this.log(`Reading table from ${this.filePath}`)\n\n if (!fs.existsSync(this.filePath)) {\n this.saveFile([])\n return []\n }\n\n const fileContent = fs.readFileSync(this.filePath, 'utf8')\n return superjson.parse<R[]>(fileContent)\n }\n\n saveFile(db: R[]): void {\n this.log(`Saving table to ${this.filePath}`)\n fs.writeFileSync(this.filePath, JSON.stringify(superjson.serialize(db), null, 2))\n }\n\n findById(id: string): R | undefined {\n const db = this.getData()\n return db.find((record) => record[this.pkField] === id)\n }\n\n findBy(field: keyof R, value: any): R[] {\n const db = this.getData()\n return db.filter((record) => record[field] === value)\n }\n\n findFirstBy(field: keyof R, value: any): R | undefined {\n const db = this.getData()\n return db.find((record) => record[field] === value)\n }\n\n search(filterFn: (record: R) => boolean): R[] {\n const db = this.getData()\n return db.filter((record) => filterFn(record))\n }\n\n insert(record: R): void {\n const pk = record[this.pkField]\n if (!pk) {\n throw new YoloDbError(`Record does not have a primary key: ${this.tableName}.${String(this.pkField)}`)\n }\n\n const db = this.getData()\n db.push(record)\n\n this.saveFile(db)\n }\n\n insertMany(records: R[]): void {\n const db = this.getData()\n db.push(...records)\n\n this.saveFile(db)\n }\n\n update(record: Partial<R>): void {\n const pk = record[this.pkField]\n if (!pk) {\n throw new YoloDbError(`Record not found in ${this.tableName}: ${pk}`)\n }\n\n const db = this.getData()\n\n for (const currentRecord of db) {\n if (currentRecord[this.pkField] === pk) {\n Object.assign(currentRecord, record)\n break\n }\n }\n\n this.saveFile(db)\n }\n\n updateMany(records: Array<Partial<R>>): void {\n records.forEach((record) => this.update(record))\n }\n\n delete(id: string): void {\n this.deleteMany([id])\n }\n\n deleteMany(ids: string[]): void {\n this.log(`Deleting ${ids.length} records`)\n const db = this.getData().filter((record) => !ids.includes(record[this.pkField]))\n\n this.saveFile(db)\n }\n\n truncate(): void {\n this.saveFile([])\n }\n}\n\nexport class YoloDbRepository<R extends Record<string, any>> {\n protected table: YoloDbTable<R>\n\n constructor(dataPath: string, pkField: keyof R) {\n this.table = yolodb<R>(dataPath, pkField, [])\n }\n}\n\n// In-memory cache of YoloDB table instances\nconst _yoloDbTables: Record<string, YoloDbTable<any>> = {}\n\n/**\n * Helper function to create a new YoloDB table.\n * It reuses the same table instance if the same file path is used.\n */\nexport function yolodb<R extends Record<string, any>>(\n filePath: string,\n pkField: keyof R,\n initialData: R[],\n options?: YoloDbTableOptions,\n): YoloDbTable<R> {\n if (!_yoloDbTables[filePath]) {\n _yoloDbTables[filePath] = new YoloDbTable<R>(filePath, pkField, initialData, options)\n }\n return _yoloDbTables[filePath]\n}\n"],"mappings":";;;;;AAMA,IAAa,cAAb,c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