0
I am programming an API for use in a small Windows application and I had some questions when it came to implementing the API. When choosing the database to use in my application, I found a problem in selecting what would be easy to implement, but I did not leave anything to be desired in the performance part. It was between 'Quick.db' (Uses Better-sqlite3), Mongoose(Mongodb) or Mysql / Sqlserver. For the convenience and simplicity of implementation I chose to use Quick.db, however, it does not have a Models function like Mongoose for example. To remedy this problem I followed some tutorials and made the following implementation:
Object code Account.
const db = require('quick.db');
const md5 = require('md5');
String.prototype.ReplaceAll = function (stringToFind, stringToReplace) {
var temp = this;
var index = temp.indexOf(stringToFind);
while (index != -1) {
temp = temp.replace(stringToFind, stringToReplace);
index = temp.indexOf(stringToFind);
}
return temp;
};
var Conta = new Object ({
email: "",
password: "",
token: "",
avatar: "",
findByEmail: async function (email) {
let e = email.ReplaceAll(" ", "").toLowerCase();
let contas = await db.get(`arka.usuarios`);
if (contas == null) return null;
let jsonString = JSON.stringify(contas);
let conta = JSON.parse(jsonString);
for (var c in conta) {
if (e === conta[c].email) {
return conta[c];
}
}
return null;
},
createAccount: async function (email, password, avatar = "") {
let e = email.ReplaceAll(" ", "").toLowerCase();
if (await this.findByEmail(e) != null) return null;
let p = password.ReplaceAll(" ", "").toLowerCase();
let token = md5('arkatokengenerate' + e + p);
let user = {
"email":e,
"password": p,
"token": token,
"avatar": avatar
};
await db.set(`arka.usuarios.${token}`, user);
let ret = Object.create(Conta);
ret.email = e;
ret.password = p;
ret.token = token;
ret.avatar = avatar;
console.log(ret);
return ret;
}
});
module.exports = Conta;
Example of how I am using the object
const Conta = require('../model/conta');
exports.createAccount = async function (req, res, next) {
let json = req.body;
if (!json) return res.status(400).send({status: 0, erro: "Envie os dados para realizar a requisição", data: []});
if (!json.email) return res.status(400).send({status: 0, erro: "Informe o email para realizar a requisição", data: []});
if (!json.password) return res.status(400).send({status: 0, erro: "Informe a senha para realizar a requisição", data: []});
if (!json.rpassword) return res.status(400).send({status: 0, erro: "Repita a senha para realizar a requisição", data: []});
let u = Object.create(Conta);
u = await u.createAccount(json.email, json.password, json.avata != null ? json.avatar : "");
res.status(200).send({status: 1, erro: "", data: [u]});
};
This implementation using the database 'Quick.db' with a possible POO implementation would be the best solution to have a simple and efficient system or in the future I may have performance problems with the use of it ?
I was giving a read on the prism after your recommendation and I’m going to adopt it. Really it would take a lot of time to manually implement my own Models, taking into account that will have more complex models in the future. Grateful for the help and clarification.
– Diasz