|
| 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(); |
0 commit comments