Added Files

This commit is contained in:
jusax23 2021-10-23 10:53:22 +02:00
parent 87e5b9f3d0
commit 4c26995d86
5 changed files with 329 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

105
db.js Normal file
View file

@ -0,0 +1,105 @@
//npm install mysql
var mysql = await import('mysql');
const db = function ({host, port, user, password, database}) {
var t= this;
var con = mysql.createConnection({
host: host,
port: port,
user: user,
password: password,
database: database,
multipleStatements: true
});
var connected = false;
t.connect = function(){
return new Promise((res,rej)=>{
con.connect(function(err) {
if (err) {
rej(err);
}else{
connected = true;
res();
};
});
});
};
t.isconnected = function(){
return connected;
};
t.check = function(structure){
return new Promise((res,rej)=>{
if(!connected) return false;
var sqlcode1 = "";
var sqlcode2 = ""; //ALTER TABLE `cars` ADD IF NOT EXISTS `test` INT NOT NULL;
var sqlcode3 = ""; //ALTER TABLE `cars` CHANGE `test` `test` TEXT NOT NULL;
var tablenames = Object.keys(structure);
for (var i = 0; i < tablenames.length; i++) {
let tablename = tablenames[i];
let table = structure[tablename];
let attributes = Object.keys(table);
if(attributes.length>1){
sqlcode1 += `CREATE TABLE IF NOT EXISTS ${con.escapeId(tablename)} (`;
for (var j = 0; j < attributes.length; j++) {
var attributename = attributes[j];
var attribute = table[attributename];
sqlcode1+=`${con.escapeId(attributename)} ${attribute.type||"text"}`;
sqlcode2+=`ALTER TABLE ${con.escapeId(tablename)} ADD IF NOT EXISTS ${con.escapeId(attributename)} ${attribute.type||"text"}`;
sqlcode3+=`ALTER TABLE ${con.escapeId(tablename)} CHANGE ${con.escapeId(attributename)} ${con.escapeId(attributename)} ${attribute.type||"text"}`;
if(!!attribute.A_I){
sqlcode1+=` AUTO_INCREMENT PRIMARY KEY`;
sqlcode2+=` AUTO_INCREMENT PRIMARY KEY`;
sqlcode3+=` AUTO_INCREMENT`;
}else if(!!attribute.default){
sqlcode1+=` DEFAULT ${con.escape(attribute.default)}`;
sqlcode2+=` DEFAULT ${con.escape(attribute.default)}`;
sqlcode3+=` DEFAULT ${con.escape(attribute.default)}`;
}
sqlcode1+=", ";
sqlcode2+="; ";
sqlcode3+="; ";
}
sqlcode1 = sqlcode1.slice(0,-2);
sqlcode1+=`); `;
}
}
con.query(sqlcode1+sqlcode2+sqlcode3,[],function(err,result){
if(err){
rej(err);
}else{
res();
}
});
});
};
t.query = function(sql,insert = []){
return new Promise((res,rej)=>{
if(!connected) return rej("Not Connected to DB!");
con.query(sql,insert,function(err,result){
if(!err){
res(result);
}else{
rej(err);
}
});
});
};
t.disconnect = function(){
connected = false;
return new Promise((res,rej)=>{
con.end((err)=>{
if(err){
rej();
}else{
res();
}
});
});
};
}
export default db;
//CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))

24
main.js Normal file
View file

@ -0,0 +1,24 @@
import wam from "./db.js";
const db_struct = {
accounts: {
ID: {type:"int", A_I:true},
accountId: {type:"text" },
name: {type:"text" },
accountkey: {type:"text" },
},
};
//const host="localhost", port=3306, user="user", password="12345678", database="testDB";
const host="rpi2", port=3306, user="jusax", password="testserver", database="test";
const db = new wam({host, port, user, password, database})
db.connect()
.then(()=>db.check(db_struct))
.then(()=>console.log("db is now ready!"))
.catch(err=>{
console.log("Can not connect properly to the Database: ",err);
process.exit();
});

173
package-lock.json generated Normal file
View file

@ -0,0 +1,173 @@
{
"name": "wam",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "wam",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"mysql": "^2.18.1"
}
},
"node_modules/bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==",
"engines": {
"node": "*"
}
},
"node_modules/core-util-is": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"node_modules/isarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
},
"node_modules/mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
"dependencies": {
"bignumber.js": "9.0.0",
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2",
"sqlstring": "2.3.1"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
},
"node_modules/readable-stream": {
"version": "2.3.7",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
"dependencies": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"node_modules/safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"node_modules/sqlstring": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
"integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"dependencies": {
"safe-buffer": "~5.1.0"
}
},
"node_modules/util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
}
},
"dependencies": {
"bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
},
"core-util-is": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"isarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
},
"mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
"requires": {
"bignumber.js": "9.0.0",
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2",
"sqlstring": "2.3.1"
}
},
"process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
},
"readable-stream": {
"version": "2.3.7",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"sqlstring": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
"integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A="
},
"string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"requires": {
"safe-buffer": "~5.1.0"
}
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
}
}
}

26
package.json Normal file
View file

@ -0,0 +1,26 @@
{
"name": "wam",
"version": "1.0.0",
"description": "What a mess?\r Ensure simple mysql database structures.",
"main": "db.js",
"type":"module",
"files": [
"db.js"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.com/jusax23/wam.git"
},
"author": "jusax23",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/jusax23/wam/issues"
},
"homepage": "https://gitlab.com/jusax23/wam#readme",
"dependencies": {
"mysql": "^2.18.1"
}
}