Skip to content

Commit 21f0c29

Browse files
committed
init
0 parents  commit 21f0c29

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

db.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
const Ajv = require('ajv');
4+
const pluralize = require('pluralize');
5+
const transliterate = require('../../../libs/transliterate');
6+
const uuid = require('uuid/v4');
7+
8+
const dbPath = path.join(__dirname, '../data/db.json');
9+
const schemaPath = path.join(__dirname, '../db.schemas.js');
10+
11+
class Db {
12+
13+
constructor(filePath, schemasPath) {
14+
this.filePath = filePath;
15+
let schemas = require(schemasPath);
16+
this.ajv = new Ajv({
17+
schemas,
18+
allErrors: true,
19+
// verbose: true
20+
});
21+
}
22+
23+
static instance() {
24+
if (!this._instance) {
25+
this._instance = new Db(dbPath, schemaPath);
26+
this._instance.load();
27+
}
28+
return this._instance;
29+
}
30+
31+
getAll() {
32+
return this.data;
33+
}
34+
35+
get(key) {
36+
return this.data[key];
37+
}
38+
39+
getById(collection, id) {
40+
collection = this.data[collection];
41+
if (Array.isArray(collection)) {
42+
return collection.find(item => item.id == id);
43+
}
44+
}
45+
46+
set(key, value) {
47+
this.data[key] = value;
48+
}
49+
50+
update(key, value) {
51+
Object.assign(this.data[key], value);
52+
}
53+
54+
load() {
55+
if (!fs.existsSync(this.filePath)) {
56+
this.data = {};
57+
return;
58+
}
59+
this.data = this.deserialize(fs.readFileSync(this.filePath, 'utf-8'));
60+
}
61+
62+
save() {
63+
if (!process.env.DB_SAVE_DISABLED) {
64+
fs.writeFileSync(this.filePath, this.serialize(this.data));
65+
}
66+
}
67+
68+
deserialize(json) {
69+
return JSON.parse(json, (key, value) => {
70+
if (key === 'createdAt' || key === 'modifiedAt') {
71+
return new Date(value);
72+
} else {
73+
return value;
74+
}
75+
});
76+
}
77+
78+
serialize(json) {
79+
return JSON.stringify(json, null, 2);
80+
}
81+
82+
// product/db/...
83+
getValidate(name) {
84+
return this.ajv.getSchema(`https://javascript.info/schemas/${name}.json`);
85+
}
86+
87+
validateSelf() {
88+
let validate = this.ajv.getSchema(`https://javascript.info/schemas/db.json`);
89+
if (!validate({db: this.data})) {
90+
console.error(validate.errors);
91+
throw new Error("Validation error");
92+
}
93+
}
94+
95+
// autogenerate id
96+
createId(collection, resource) {
97+
if (resource.title) {
98+
return transliterate(newResource.title);
99+
} else {
100+
return uuid();
101+
}
102+
}
103+
104+
// returns a function that gets required field from value, including subfields
105+
// getter = createGetter('category')
106+
// getter(product) // gets product.category
107+
// getter = createGetter('category.name')
108+
// getter(product) // gets product.category.name (finds category in db)
109+
createGetter(field) {
110+
// category.name -> ['category','name']
111+
const parts = field.split('.');
112+
113+
return value => {
114+
for (let i = 0; i < parts.length; i++) {
115+
value = value[parts[i]]; // from product -> get product.category (id)
116+
if (value === undefined) return undefined;
117+
if (i < parts.length - 1) {
118+
// we have category id, let's get category instead
119+
let collection = this.get(pluralize(parts[i]));
120+
value = collection.find(v => v.id == value);
121+
}
122+
}
123+
return value;
124+
};
125+
126+
}
127+
128+
}
129+
130+
131+
module.exports = Db.instance();

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@javascriptru/json-db",
3+
"version": "1.0.0",
4+
"main": "db.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/javascriptru/json-db.git"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"bugs": {
15+
"url": "https://github.com/javascriptru/json-db/issues"
16+
},
17+
"homepage": "https://github.com/javascriptru/json-db#readme",
18+
"description": "",
19+
"dependencies": {
20+
"@javascriptru/transliterate": "github:javascriptru/transliterate",
21+
"ajv": "^6.12.0",
22+
"pluralize": "^8.0.0",
23+
"uuid": "^7.0.1"
24+
}
25+
}

0 commit comments

Comments
 (0)