@keshavsoft/kschema-cli 1.1.1
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/CHANGELOG.md +39 -0
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/bin/cli.js +20 -0
- package/commands/init.js +20 -0
- package/commands/test.js +36 -0
- package/dev.md +74 -0
- package/index.js +1 -0
- package/package.json +16 -0
- package/src/core/configStore.js +10 -0
- package/src/utils/pathBuilder.js +3 -0
- package/src/v1/data/deleteData.js +16 -0
- package/src/v1/data/getData.js +13 -0
- package/src/v1/data/index.js +4 -0
- package/src/v1/data/insertData.js +16 -0
- package/src/v1/data/updateData.js +20 -0
- package/src/v1/index.js +3 -0
- package/src/v2/config/getSchema.js +12 -0
- package/src/v2/data/deleteData.js +16 -0
- package/src/v2/data/getData.js +13 -0
- package/src/v2/data/index.js +4 -0
- package/src/v2/data/insertData copy.js +46 -0
- package/src/v2/data/insertData.js +52 -0
- package/src/v2/data/updateData.js +20 -0
- package/src/v2/index.js +3 -0
- package/src/v3/config/getSchema.js +12 -0
- package/src/v3/data/deleteData.js +16 -0
- package/src/v3/data/getData.js +13 -0
- package/src/v3/data/index.js +4 -0
- package/src/v3/data/insertData.js +27 -0
- package/src/v3/data/updateData.js +20 -0
- package/src/v3/helpers/fileHelper.js +11 -0
- package/src/v3/helpers/pkHelper.js +18 -0
- package/src/v3/helpers/validateHelper.js +20 -0
- package/src/v3/index.js +3 -0
- package/src/v4/config/getSchema.js +12 -0
- package/src/v4/data/deleteData.js +16 -0
- package/src/v4/data/getData.js +13 -0
- package/src/v4/data/index.js +4 -0
- package/src/v4/data/insertData.js +47 -0
- package/src/v4/data/updateData.js +20 -0
- package/src/v4/helpers/fileHelper.js +11 -0
- package/src/v4/helpers/pkHelper.js +18 -0
- package/src/v4/helpers/recordHelper.js +9 -0
- package/src/v4/helpers/validateHelper.js +15 -0
- package/src/v4/index.js +3 -0
- package/src/v5/config/getSchema.js +12 -0
- package/src/v5/data/deleteData.js +16 -0
- package/src/v5/data/getData.js +13 -0
- package/src/v5/data/index.js +8 -0
- package/src/v5/data/insertData.js +56 -0
- package/src/v5/data/insertDataStrict.js +63 -0
- package/src/v5/data/updateData.js +20 -0
- package/src/v5/helpers/fileHelper.js +11 -0
- package/src/v5/helpers/pkHelper.js +18 -0
- package/src/v5/helpers/recordHelper.js +9 -0
- package/src/v5/helpers/validateHelper.js +15 -0
- package/src/v5/index.js +3 -0
- package/src/v6/config/getSchema.js +12 -0
- package/src/v6/data/deleteData.js +16 -0
- package/src/v6/data/getData.js +13 -0
- package/src/v6/data/index.js +8 -0
- package/src/v6/data/insertData.js +56 -0
- package/src/v6/data/insertDataStrict.js +76 -0
- package/src/v6/data/updateData.js +20 -0
- package/src/v6/helpers/fileHelper.js +11 -0
- package/src/v6/helpers/pkHelper.js +18 -0
- package/src/v6/helpers/recordHelper.js +9 -0
- package/src/v6/helpers/validateHelper.js +15 -0
- package/src/v6/index.js +21 -0
- package/src/v7/config/getSchema.js +12 -0
- package/src/v7/data/deleteData.js +16 -0
- package/src/v7/data/filterByColumns.js +19 -0
- package/src/v7/data/filterByPk.js +16 -0
- package/src/v7/data/findByColumns.js +21 -0
- package/src/v7/data/findByPk.js +16 -0
- package/src/v7/data/getData.js +13 -0
- package/src/v7/data/index.js +11 -0
- package/src/v7/data/insertData.js +55 -0
- package/src/v7/data/insertDataStrict.js +73 -0
- package/src/v7/data/updateData.js +20 -0
- package/src/v7/helpers/fileHelper.js +16 -0
- package/src/v7/helpers/pkHelper.js +21 -0
- package/src/v7/helpers/recordHelper.js +15 -0
- package/src/v7/helpers/validateHelper.js +27 -0
- package/src/v7/index.js +27 -0
- package/src/v8/config/getSchema.js +12 -0
- package/src/v8/data/deleteByColumnsData.js +20 -0
- package/src/v8/data/deleteData.js +22 -0
- package/src/v8/data/filterByColumns.js +19 -0
- package/src/v8/data/filterByPk.js +16 -0
- package/src/v8/data/findByColumns.js +21 -0
- package/src/v8/data/findByPk.js +16 -0
- package/src/v8/data/getData.js +13 -0
- package/src/v8/data/index.js +12 -0
- package/src/v8/data/insertData.js +55 -0
- package/src/v8/data/insertDataStrict.js +73 -0
- package/src/v8/data/updateData.js +20 -0
- package/src/v8/helpers/fileHelper.js +16 -0
- package/src/v8/helpers/pkHelper.js +21 -0
- package/src/v8/helpers/recordHelper.js +15 -0
- package/src/v8/helpers/validateHelper.js +27 -0
- package/src/v8/index.js +43 -0
- package/template/Config/Schemas/BillsTable.json +165 -0
- package/template/Config/Schemas/ItemsTable.json +200 -0
- package/template/Config/Schemas/LedgerNames.json +45 -0
- package/template/Config/Schemas/StockItems.json +45 -0
- package/template/Config/api.json +8 -0
- package/template/Config/schema.json +8 -0
- package/template/Config/ui.json +8 -0
- package/template/config.json +4 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# CHANGELOG
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## [1.7.1] - 2026-04-18
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
* Initial release of kschema
|
|
12
|
+
* Config loader (`loadConfig`, `getConfig`)
|
|
13
|
+
* File-based JSON storage
|
|
14
|
+
* Schema-driven table handling
|
|
15
|
+
* Primary key detection (`getPrimaryKey`)
|
|
16
|
+
* Auto-increment primary key (`attachPrimaryKey`)
|
|
17
|
+
* CRUD operations:
|
|
18
|
+
|
|
19
|
+
* insert
|
|
20
|
+
* insertStrict
|
|
21
|
+
* get
|
|
22
|
+
* findByPk
|
|
23
|
+
* update
|
|
24
|
+
* delete
|
|
25
|
+
|
|
26
|
+
### Notes
|
|
27
|
+
|
|
28
|
+
* Primary key is dynamically resolved from schema
|
|
29
|
+
* Data stored as JSON files
|
|
30
|
+
* Minimal, dependency-free design
|
|
31
|
+
|
|
32
|
+
## [1.7.3] - 2026-04-19
|
|
33
|
+
|
|
34
|
+
### filer and find columns perfect
|
|
35
|
+
|
|
36
|
+
## [1.7.4]
|
|
37
|
+
|
|
38
|
+
### delete multi added
|
|
39
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KeshavSoft
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to do so, subject to the
|
|
10
|
+
following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# KeshavSoft
|
|
2
|
+
|
|
3
|
+
Simple tools. Clean architecture. Fast development.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## @keshavsoft/kschema
|
|
8
|
+
|
|
9
|
+
A lightweight schema-driven JSON database for Node.js.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
npm i @keshavsoft/kschema
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
import { kschema } from "@keshavsoft/kschema";
|
|
22
|
+
|
|
23
|
+
kschema.loadConfig({ dataPath: "./data" });
|
|
24
|
+
|
|
25
|
+
const users = kschema.table("users");
|
|
26
|
+
|
|
27
|
+
users.insert({ name: "keshav" });
|
|
28
|
+
|
|
29
|
+
const data = users.get();
|
|
30
|
+
|
|
31
|
+
console.log(data);
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
|
|
37
|
+
Create
|
|
38
|
+
|
|
39
|
+
users.insert(record);
|
|
40
|
+
users.insertStrict(record);
|
|
41
|
+
|
|
42
|
+
Read
|
|
43
|
+
|
|
44
|
+
users.get();
|
|
45
|
+
users.findByPk(id);
|
|
46
|
+
|
|
47
|
+
Filter
|
|
48
|
+
|
|
49
|
+
users.filterByPk(id);
|
|
50
|
+
users.filterByColumns({ name: "keshav" });
|
|
51
|
+
|
|
52
|
+
Update
|
|
53
|
+
|
|
54
|
+
users.update(record);
|
|
55
|
+
|
|
56
|
+
Delete
|
|
57
|
+
|
|
58
|
+
users.delete(id);
|
|
59
|
+
users.deleteByColumns({ name: "keshav" });
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Features
|
|
64
|
+
|
|
65
|
+
* File-based JSON storage
|
|
66
|
+
* Schema-driven design
|
|
67
|
+
* Auto primary key
|
|
68
|
+
* Simple CRUD API
|
|
69
|
+
* Zero dependencies
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Links
|
|
74
|
+
|
|
75
|
+
GitHub: https://github.com/keshavsoft/kschema
|
|
76
|
+
NPM: https://www.npmjs.com/package/@keshavsoft/kschema
|
|
77
|
+
Website: https://keshavsoft.com
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Contact
|
|
82
|
+
|
|
83
|
+
Email: founder@keshavsoft.com
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## About
|
|
88
|
+
|
|
89
|
+
KeshavSoft builds simple and practical developer tools focused on clarity, speed, and real-world usage.
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
MIT License — see LICENSE file
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import init from "../commands/init.js";
|
|
4
|
+
import test from "../commands/test.js";
|
|
5
|
+
|
|
6
|
+
const cmd = process.argv[2];
|
|
7
|
+
const arg = process.argv[3];
|
|
8
|
+
|
|
9
|
+
switch (cmd) {
|
|
10
|
+
case "init":
|
|
11
|
+
init(arg);
|
|
12
|
+
break;
|
|
13
|
+
|
|
14
|
+
case "test":
|
|
15
|
+
test(arg);
|
|
16
|
+
break;
|
|
17
|
+
|
|
18
|
+
default:
|
|
19
|
+
console.log("Usage: kschema <init|test> [name]");
|
|
20
|
+
}
|
package/commands/init.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// commands/init.js
|
|
2
|
+
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
|
|
10
|
+
export default (arg) => {
|
|
11
|
+
const folderName = arg || "KehavSoft1";
|
|
12
|
+
|
|
13
|
+
const source = path.join(__dirname, "../template");
|
|
14
|
+
const destination = path.join(process.cwd(), folderName);
|
|
15
|
+
|
|
16
|
+
fs.mkdirSync(destination, { recursive: true });
|
|
17
|
+
fs.cpSync(source, destination, { recursive: true });
|
|
18
|
+
|
|
19
|
+
console.log(`Project created in ${folderName}`);
|
|
20
|
+
};
|
package/commands/test.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// commands/test.js
|
|
2
|
+
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
|
|
6
|
+
export default (arg) => {
|
|
7
|
+
const projectPath = arg
|
|
8
|
+
? path.resolve(process.cwd(), arg)
|
|
9
|
+
: process.cwd();
|
|
10
|
+
|
|
11
|
+
// 1. Check folder exists
|
|
12
|
+
if (!fs.existsSync(projectPath)) {
|
|
13
|
+
console.log("Invalid path");
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 2. Check required structure
|
|
18
|
+
const hasConfigJson = fs.existsSync(
|
|
19
|
+
path.join(projectPath, "config.json")
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const hasConfigFolder = fs.existsSync(
|
|
23
|
+
path.join(projectPath, "Config")
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const hasSchemasFolder = fs.existsSync(
|
|
27
|
+
path.join(projectPath, "Config", "Schemas")
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// 3. Final decision
|
|
31
|
+
if (hasConfigJson && hasConfigFolder && hasSchemasFolder) {
|
|
32
|
+
console.log("Already initialized");
|
|
33
|
+
} else {
|
|
34
|
+
console.log("Not initialized");
|
|
35
|
+
}
|
|
36
|
+
};
|
package/dev.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @keshavsoft/kschema
|
|
2
|
+
|
|
3
|
+
A minimal config store for loading and accessing JSON configuration across your application.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 🚀 Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @keshavsoft/kschema
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 📦 Usage
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { loadConfig, getConfig } from "@keshavsoft/kschema";
|
|
19
|
+
|
|
20
|
+
loadConfig({ name: "test" });
|
|
21
|
+
|
|
22
|
+
console.log(getConfig());
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 🧠 API
|
|
28
|
+
|
|
29
|
+
### loadConfig(config)
|
|
30
|
+
|
|
31
|
+
Loads the configuration into memory.
|
|
32
|
+
|
|
33
|
+
* `config` → Object
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
### getConfig()
|
|
38
|
+
|
|
39
|
+
Returns the loaded configuration.
|
|
40
|
+
|
|
41
|
+
* Throws error if config is not loaded
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## ⚠️ Notes
|
|
46
|
+
|
|
47
|
+
* Config is stored in-memory
|
|
48
|
+
* Must call `loadConfig` before `getConfig`
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## 📌 Example
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
loadConfig({ user: "keshav" });
|
|
56
|
+
|
|
57
|
+
const data = getConfig();
|
|
58
|
+
|
|
59
|
+
console.log(data.user); // keshav
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 🛠 Future Scope
|
|
65
|
+
|
|
66
|
+
* Validation support
|
|
67
|
+
* Config updates
|
|
68
|
+
* Remote config loading
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 👨💻 Author
|
|
73
|
+
|
|
74
|
+
KeshavSoft
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/v8/index.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@keshavsoft/kschema-cli",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"bin": {
|
|
6
|
+
"kschema": "./bin/cli.js"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://keshavsoft.com",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/keshavsoft/kschema-cli"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/keshavsoft/kschema-cli/issues"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const deleteData = ({ table, key, value }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
const path = buildDataPath(cfg, table);
|
|
8
|
+
|
|
9
|
+
const data = JSON.parse(fs.readFileSync(path));
|
|
10
|
+
|
|
11
|
+
const newData = data.filter(item => item[key] !== value);
|
|
12
|
+
|
|
13
|
+
fs.writeFileSync(path, JSON.stringify(newData, null, 2));
|
|
14
|
+
|
|
15
|
+
return true;
|
|
16
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const getData = ({ table }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
|
|
8
|
+
const path = buildDataPath(cfg, table);
|
|
9
|
+
|
|
10
|
+
const data = fs.readFileSync(path, "utf-8");
|
|
11
|
+
|
|
12
|
+
return JSON.parse(data);
|
|
13
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const insertData = ({ table, record }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
const path = buildDataPath(cfg, table);
|
|
8
|
+
|
|
9
|
+
const data = JSON.parse(fs.readFileSync(path));
|
|
10
|
+
|
|
11
|
+
data.push(record);
|
|
12
|
+
|
|
13
|
+
fs.writeFileSync(path, JSON.stringify(data, null, 2));
|
|
14
|
+
|
|
15
|
+
return record;
|
|
16
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const updateData = ({ table, key, value, updates }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
const path = buildDataPath(cfg, table);
|
|
8
|
+
|
|
9
|
+
const data = JSON.parse(fs.readFileSync(path));
|
|
10
|
+
|
|
11
|
+
const index = data.findIndex(item => item[key] === value);
|
|
12
|
+
|
|
13
|
+
if (index === -1) throw new Error("Record not found");
|
|
14
|
+
|
|
15
|
+
data[index] = { ...data[index], ...updates };
|
|
16
|
+
|
|
17
|
+
fs.writeFileSync(path, JSON.stringify(data, null, 2));
|
|
18
|
+
|
|
19
|
+
return data[index];
|
|
20
|
+
};
|
package/src/v1/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
|
|
4
|
+
export const getSchema = (table) => {
|
|
5
|
+
const config = getConfig();
|
|
6
|
+
|
|
7
|
+
const schemaPath = `${config.SchemaPath}/${table}.json`;
|
|
8
|
+
|
|
9
|
+
const schema = fs.readFileSync(schemaPath, "utf-8");
|
|
10
|
+
|
|
11
|
+
return JSON.parse(schema);
|
|
12
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const deleteData = ({ table, key, value }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
const path = buildDataPath(cfg, table);
|
|
8
|
+
|
|
9
|
+
const data = JSON.parse(fs.readFileSync(path));
|
|
10
|
+
|
|
11
|
+
const newData = data.filter(item => item[key] !== value);
|
|
12
|
+
|
|
13
|
+
fs.writeFileSync(path, JSON.stringify(newData, null, 2));
|
|
14
|
+
|
|
15
|
+
return true;
|
|
16
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const getData = ({ table }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
|
|
8
|
+
const path = buildDataPath(cfg, table);
|
|
9
|
+
|
|
10
|
+
const data = fs.readFileSync(path, "utf-8");
|
|
11
|
+
|
|
12
|
+
return JSON.parse(data);
|
|
13
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
// 🔹 helpers
|
|
6
|
+
|
|
7
|
+
const getPrimaryKey = (columns) => {
|
|
8
|
+
const pk = columns.find(c => c.unique)?.field;
|
|
9
|
+
if (!pk) throw new Error("Primary key not defined");
|
|
10
|
+
return pk;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const readData = (path) => {
|
|
14
|
+
return JSON.parse(fs.readFileSync(path));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const writeData = (path, data) => {
|
|
18
|
+
fs.writeFileSync(path, JSON.stringify(data, null, 2));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const attachPrimaryKey = (record, pk) => {
|
|
22
|
+
return {
|
|
23
|
+
...record,
|
|
24
|
+
[pk]: Date.now()
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// 🔹 main
|
|
29
|
+
|
|
30
|
+
export const insertData = ({ table, record }) => {
|
|
31
|
+
const config = getConfig();
|
|
32
|
+
const schema = config[table];
|
|
33
|
+
|
|
34
|
+
const pk = getPrimaryKey(schema.columns);
|
|
35
|
+
const path = buildDataPath(config, table);
|
|
36
|
+
|
|
37
|
+
const data = readData(path);
|
|
38
|
+
|
|
39
|
+
const newRecord = attachPrimaryKey(record, pk);
|
|
40
|
+
|
|
41
|
+
data.push(newRecord);
|
|
42
|
+
|
|
43
|
+
writeData(path, data);
|
|
44
|
+
|
|
45
|
+
return newRecord;
|
|
46
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
import { getSchema } from "../config/getSchema.js";
|
|
5
|
+
// 🔹 helpers
|
|
6
|
+
|
|
7
|
+
const getPrimaryKey = (columns) => {
|
|
8
|
+
const pk = columns.find(c => c.unique)?.field;
|
|
9
|
+
if (!pk) throw new Error("Primary key not defined");
|
|
10
|
+
return pk;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const readData = (path) => {
|
|
14
|
+
if (!fs.existsSync(path)) return [];
|
|
15
|
+
return JSON.parse(fs.readFileSync(path));
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const writeData = (path, data) => {
|
|
19
|
+
fs.writeFileSync(path, JSON.stringify(data, null, 2));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const attachPrimaryKey = (record, pk, data) => {
|
|
23
|
+
if (data.length === 0) {
|
|
24
|
+
return { ...record, [pk]: 1 };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const maxId = Math.max(...data.map(row => row[pk] || 0));
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
...record,
|
|
31
|
+
[pk]: maxId + 1
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
// 🔹 main
|
|
35
|
+
|
|
36
|
+
export const insertData = ({ table, record }) => {
|
|
37
|
+
const config = getConfig();
|
|
38
|
+
const schema = getSchema(table);
|
|
39
|
+
|
|
40
|
+
const pk = getPrimaryKey(schema.columns);
|
|
41
|
+
const path = buildDataPath(config, table);
|
|
42
|
+
|
|
43
|
+
const data = readData(path);
|
|
44
|
+
|
|
45
|
+
const newRecord = attachPrimaryKey(record, pk, data);
|
|
46
|
+
|
|
47
|
+
data.push(newRecord);
|
|
48
|
+
|
|
49
|
+
writeData(path, data);
|
|
50
|
+
|
|
51
|
+
return newRecord;
|
|
52
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const updateData = ({ table, key, value, updates }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
const path = buildDataPath(cfg, table);
|
|
8
|
+
|
|
9
|
+
const data = JSON.parse(fs.readFileSync(path));
|
|
10
|
+
|
|
11
|
+
const index = data.findIndex(item => item[key] === value);
|
|
12
|
+
|
|
13
|
+
if (index === -1) throw new Error("Record not found");
|
|
14
|
+
|
|
15
|
+
data[index] = { ...data[index], ...updates };
|
|
16
|
+
|
|
17
|
+
fs.writeFileSync(path, JSON.stringify(data, null, 2));
|
|
18
|
+
|
|
19
|
+
return data[index];
|
|
20
|
+
};
|
package/src/v2/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
|
|
4
|
+
export const getSchema = (table) => {
|
|
5
|
+
const config = getConfig();
|
|
6
|
+
|
|
7
|
+
const schemaPath = `${config.SchemaPath}/${table}.json`;
|
|
8
|
+
|
|
9
|
+
const schema = fs.readFileSync(schemaPath, "utf-8");
|
|
10
|
+
|
|
11
|
+
return JSON.parse(schema);
|
|
12
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const deleteData = ({ table, key, value }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
const path = buildDataPath(cfg, table);
|
|
8
|
+
|
|
9
|
+
const data = JSON.parse(fs.readFileSync(path));
|
|
10
|
+
|
|
11
|
+
const newData = data.filter(item => item[key] !== value);
|
|
12
|
+
|
|
13
|
+
fs.writeFileSync(path, JSON.stringify(newData, null, 2));
|
|
14
|
+
|
|
15
|
+
return true;
|
|
16
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { getConfig } from "../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
4
|
+
|
|
5
|
+
export const getData = ({ table }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
|
|
8
|
+
const path = buildDataPath(cfg, table);
|
|
9
|
+
|
|
10
|
+
const data = fs.readFileSync(path, "utf-8");
|
|
11
|
+
|
|
12
|
+
return JSON.parse(data);
|
|
13
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getConfig } from "../../core/configStore.js";
|
|
2
|
+
import { buildDataPath } from "../../utils/pathBuilder.js";
|
|
3
|
+
import { getSchema } from "../config/getSchema.js";
|
|
4
|
+
|
|
5
|
+
import { getPrimaryKey, attachPrimaryKey } from "../helpers/pkHelper.js";
|
|
6
|
+
import { readData, writeData } from "../helpers/fileHelper.js";
|
|
7
|
+
import { validateRecord } from "../helpers/validateHelper.js";
|
|
8
|
+
|
|
9
|
+
export const insertData = ({ table, record }) => {
|
|
10
|
+
const config = getConfig();
|
|
11
|
+
const schema = getSchema(table);
|
|
12
|
+
|
|
13
|
+
const pk = getPrimaryKey(schema.columns);
|
|
14
|
+
const path = buildDataPath(config, table);
|
|
15
|
+
|
|
16
|
+
const data = readData(path);
|
|
17
|
+
|
|
18
|
+
validateRecord(record, schema.columns, data);
|
|
19
|
+
|
|
20
|
+
const newRecord = attachPrimaryKey(record, pk, data);
|
|
21
|
+
|
|
22
|
+
data.push(newRecord);
|
|
23
|
+
|
|
24
|
+
writeData(path, data);
|
|
25
|
+
|
|
26
|
+
return newRecord;
|
|
27
|
+
};
|