From fb8e12f8d2fd8af42db09307603b8dc163035013 Mon Sep 17 00:00:00 2001 From: MrFry Date: Tue, 7 Apr 2020 09:41:00 +0200 Subject: [PATCH] dbSetup import cids --- modules/api/apiDBStruct.json | 6 ++++- utils/dbSetup.js | 52 +++++++++++++++++++++++++++--------- utils/dbtools.js | 3 ++- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/modules/api/apiDBStruct.json b/modules/api/apiDBStruct.json index 2782a24..d810509 100644 --- a/modules/api/apiDBStruct.json +++ b/modules/api/apiDBStruct.json @@ -11,6 +11,9 @@ "notNull": true, "unique": true }, + "oldCID": { + "type": "text" + }, "lastIP": { "type": "text" }, @@ -18,7 +21,8 @@ "type": "text" }, "loginCount": { - "type": "number" + "type": "number", + "defaultZero": true }, "lastLogin": { "type": "text" diff --git a/utils/dbSetup.js b/utils/dbSetup.js index ec14b52..ae86647 100644 --- a/utils/dbSetup.js +++ b/utils/dbSetup.js @@ -1,12 +1,15 @@ const utils = require('../utils/utils.js') +const logger = require('../utils/logger.js') const dbtools = require('../utils/dbtools.js') const dbStructPath = '../modules/api/apiDBStruct.json' const usersDBPath = '../data/dbs/users.db' +const uuidv4 = require('uuid/v4') // TODO: deprecated, but imports are not supported let authDB console.clear() +CreateDB() function CreateDB () { const dbStruct = utils.ReadJSON(dbStructPath) @@ -18,22 +21,45 @@ function CreateDB () { }) try { - // TODO: fill with data - dbtools.Insert(authDB, 'users', { - pw: 2, - notes: 'hemnlo' - }) - dbtools.Insert(authDB, 'users', { - pw: 1, - notes: 'hemnlo' + const uids = utils.ReadFile('../dbUsers/keys').split('\n') + + uids.forEach((cid, i) => { + logger.Log(`[ ${i} / ${uids.length} ]`) + try { + dbtools.Insert(authDB, 'users', { + pw: uuidv4(), + oldCID: cid + }) + } catch (e) { + logger.Log('Error during inserting', logger.GetColor('redbg')) + console.error(e) + } }) } catch (e) { console.error(e) } - // Object.keys(dbStruct).forEach((key) => { - // console.log(key) - // console.log(dbtools.TableInfo(authDB, key)) - // }) + const dir = `./dbSetupResult/${GetDateString().replace(/ /g, '_')}` + utils.CreatePath(dir) + Object.keys(dbStruct).forEach((key) => { + const path = `${dir}/${key}.json` + logger.Log(`Writing ${path}...`) + utils.WriteFile(JSON.stringify({ + tableInfo: dbtools.TableInfo(authDB, key), + tableRows: dbtools.SelectAll(authDB, key) + }), path) + }) + + logger.Log('Done') +} + +function GetDateString () { + const m = new Date() + const d = m.getFullYear() + '-' + + ('0' + (m.getMonth() + 1)).slice(-2) + '-' + + ('0' + m.getDate()).slice(-2) + ' ' + + ('0' + m.getHours()).slice(-2) + ':' + + ('0' + m.getMinutes()).slice(-2) + ':' + + ('0' + m.getSeconds()).slice(-2) + return d } -CreateDB() diff --git a/utils/dbtools.js b/utils/dbtools.js index a1a0ea3..30b6365 100644 --- a/utils/dbtools.js +++ b/utils/dbtools.js @@ -124,7 +124,8 @@ function CreateTable (db, name, columns, foreignKeys) { primary: 'PRIMARY KEY', notNull: 'NOT NULL', unique: 'UNIQUE', - autoIncrement: 'AUTOINCREMENT' + autoIncrement: 'AUTOINCREMENT', + defaultZero: 'DEFAULT 0' } Object.keys(toCheck).forEach((key) => { if (item[key]) {