UNPKG

suma-db

Version:

A Brazilian database that supports JSON(JavaScript Object Notation)

532 lines (491 loc) 21.8 kB
async referenceAsync(pathObj, saveValue = null) { return this.reference(pathObj, saveValue, 'async') } get events() { return this.file._events; } reference(pathObj, saveValue = null, id = 'sync') { if (!pathObj) throw new Error('O caminho de reference não está definido.') if (pathObj.path || pathObj.key) { saveValue = pathObj.saveValue ?? pathObj.savevalue ?? saveValue pathObj = pathObj.path || pathObj.key || pathObj } if (typeof pathObj !== 'string') throw new Error('O caminho de reference não é uma string.') this.emit('reference', { id, pathObj, saveValue }); this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Nova referência!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: `Caminho ${pathObj}, ID: ${id}`, color: 'blue' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Refêrencia feita: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } } ], { blocksColor: 'yellow' }, this.debugMode); const protoObj = this, pathFormated = this.prepare(pathObj, this.readFile(), null), file = this.readFile(), obj = { saveValue, pathObj, collection: function () { new Collection(Object.entries(this.values())) }, pathFormated, database: new Collection(Object.entries(this.readFile())), now: Date.now(), values(collection = false) { collection = !!collection return collection ? this.collection() : this.pathFormated }, __proto__: protoObj, keys: Object.keys(file), valuesFile: Object.values(file), entries: Object.entries(file), db() { return new Collection(Object.entries(JSON.parse(readFileSync(this.file.path))), 'utf-8') } } Object.defineProperties(obj, { "valuesAndKeys": { enumerable: true, value: function () { let db = this.db().toObject(), Db = db, DB = {}, key = pathObj.split(/\:/g), keyTwo = pathObj.split(":") for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } stringParaObjectDividoPeloPrefixo(DB, keyTwo.join(":"), Db[key], ':') this.emit('get', { id: 'valuesAndKeys', data: DB }) return DB } }, "val": { enumerable: true, value: function () { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } this.emit('get', { id: 'val', data: Db[key] }) this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valores da referência acessados`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valores acessados: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } } ], {}, this.debugMode) return Db[key] } }, "set": { enumerable: true, value: function (val = this.saveValue) { if (val === undefined) throw new Error(`Sem valor de set`) this.saveValue = val this.emit('set', { val, id: 'set' }) this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor setado!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor setado: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) return this } }, "save": { enumerable: true, value: function (value = this.saveValue) { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } Db[key] = value this.saveValueInDatabaseFile(db) this.emit('save', { val: Db[key] }) this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor salvo!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor salvo: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) return this } }, "remove": { enumerable: true, value: function () { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g); for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } Db[key] = null this.saveValue = Db[key] this.emit('remove', { val: Db[key] }); this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor removido!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor removido: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) return this } }, "delete": { enumerable: true, value: function () { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g); for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } delete Db[key] writeFileSync(this.file.path, JSON.stringify(db, null, 4), 'utf-8') this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor deletado!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor deletado: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('delete', { val: Db[key] }); return this } }, "add": { enumerable: true, value: function (value) { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } Db[key] = Number(Db[key]) + Number(value) if (isNaN(Db[key])) Db[key] = value; this.saveValue = Db[key] this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor adicionado!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor: ' + value, color: 'blue' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor adicionado: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('set', { id: 'add', val: Db[key], value }); return this }, }, sub: { enumerable: true, value: function (value) { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } Db[key] = Number(Db[key]) - Number(value) if (isNaN(Db[key])) Db[key] = 0; this.saveValue = Db[key] const valueSalvado = this.saveValue this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor subtraído!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor: ' + valueSalvado, color: 'blue' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor subtraído: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('set', { id: 'sub', val: Db[key], value }); return this } }, push: { enumerable: true, value: function (...values) { let val = this.val() if (!Array.isArray(val)) val = [] val.push(...values) this.saveValue = val this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor "push"!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor "push": ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('set', { id: 'push', val, values }); return this } }, exists: { enumerable: true, value: function () { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor validado(método exist da reference)!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Existe: ' + Db[key] ? "Sim" : "Não", color: 'blue' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor validado: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) return !!Db[key] } }, destroy: { enumerable: true, value: function () { const key = pathObj.split(":")[0], database = this.db() database.delete(key) writeFileSync(this.file.path, JSON.stringify(database.toObject() || {}, null, 4)) this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Caminho destruído!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Caminho destruído: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('destroy', { database: this.db().toObject(), initialKey: key }) return this } }, insert: { enumerable: true, value: function (value) { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor inserido!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Valor inserido: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) if (!Db[key]) { this.saveValue = value; this.emit('set', { val: value, id: 'insert' }) return this } else { let val = Db[key].concat(value) this.saveValue = val; this.emit('set', { val, id: 'insert' }) return this } } }, saveIfNull: { enumerable: true, value: function (value) { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } if (Db[key] == null) this.save(value) return this } }, safeIfNotNull: { enumerable: true, value: function (value) { let db = this.db().toObject(), Db = db, key = pathObj.split(/\:/g) for (false; key.length > 1; false) { const shiftKey = key.shift() Db = Db[shiftKey] = Db[shiftKey] || {} } if (Db[key] != null) this.save(value) return this } } }) obj.externsMethods = { __proto__: protoObj, put(value) { writeFileSync(this.file.path, JSON.stringify(value, null, 4)) this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Valor postado de forma externa de uma referência!`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Data: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('put', value) return this }, clear() { writeFileSync(this.file.path, JSON.stringify({}, null, 4)) this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Databse limpa`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Data: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('clear', {time: Date.now()}) return this }, map(callback) { const arr = [], database = new Collection(Object.entries(this.readFile())) for (let [key, value] of database) { arr.push(callback(value, key, database)) } this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Database mapeada`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Data: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) this.emit('map', arr) return arr }, forEach(callback) { const database = new Collection(Object.entries(this.readFile())) for (let [key, value] of database) { callback(value, key, database) } this.debugger([ { blocksColor: 'blue', title: { content: "SUMA-DB", color: 'white' }, description: { content: `Database percorrida`, color: 'red' } }, { blocksColor: 'white', title: { content: "SUMA-DB", color: 'green' }, description: { content: 'Data: ' + moment().format('L') + ' às ' + moment().format('LTS'), color: 'blue' } }], {}, this.debugMode) return this }, Collection } return obj } ref(path, saveValue = null) { return this.reference(path, saveValue) } all(filter = () => true) { let databaseValuesInArray = [] for (let [key, value] of Object.entries(this.readFile())) { databaseValuesInArray.unshift({ _ID: key, data: value, __v: databaseValuesInArray.length }); } databaseValuesInArray = databaseValuesInArray.filter(filter) this.emit('all', { data: databaseValuesInArray, dataCollection: new Collection(Object.entries(databaseValuesInArray)) }) return databaseValuesInArray } size() { return Object.entries(this.readFile()).length; } } module.exports = DB function stringParaObjectDividoPeloPrefixo(objeto, keys, v, prefix) { if (keys.endsWith(prefix)) keys = keys.slice(0, keys.length - 1); let chaves = keys.split(prefix) const transfer = function (obj, key) { if (key.length > 1) { const objInTransformer = obj[key[0]] = {} key.shift() transfer(objInTransformer, key) } else obj[key[0]] = v } transfer(objeto, chaves) return new Map(Object.entries(objeto)) }