From 285dbf2b5a853f558bf9481c2826d49aaeb5efdb Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Fri, 21 Feb 2020 11:38:53 +0100 Subject: [PATCH] graphviz to database structure parser --- snippets/db/dot_parser.js | 18 ++-- snippets/db/index.js | 2 +- test/test.js | 191 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 9 deletions(-) create mode 100644 test/test.js diff --git a/snippets/db/dot_parser.js b/snippets/db/dot_parser.js index 32299a8..b16216b 100644 --- a/snippets/db/dot_parser.js +++ b/snippets/db/dot_parser.js @@ -136,7 +136,7 @@ function create_table_function (table) { * * @param {any} knex database connection * @returns {Promise} result - */ + */ function create_${table.name} (knex) { return knex.schema.createTable ('${table.name}', (table) => { ${table.columns @@ -190,23 +190,25 @@ async function create_migration (file_path) { const tables = await get_tables (file_path); const functions = tables.map ((tab) => create_table_function (tab)); - const file = `${functions.join ('\n\n')} + const file = `'use strict'; -/* +${functions.join ('\n\n')} + +/** * run migration * * @param {any} knex db connection */ ${create_up_function (tables)} -/* +/** * revert migration - * - * @param {any} knex db connection */ -function down () {} +function down () { + // noop +} -module.exports = { up, down } +module.exports = { up, down }; `; return file; } diff --git a/snippets/db/index.js b/snippets/db/index.js index fd7f538..e8b9e01 100644 --- a/snippets/db/index.js +++ b/snippets/db/index.js @@ -25,7 +25,7 @@ async function run (folder, args) { const graph = path.join (folder, args[0]); const migration = path.join (folder, args[1]); - const db_migration = dot_parser.create_migration (graph); + const db_migration = await dot_parser.create_migration (graph); await fs.writeFile (migration, db_migration, 'utf-8'); } diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..b5289ba --- /dev/null +++ b/test/test.js @@ -0,0 +1,191 @@ +'use strict'; + +/** + * create table users + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_users (knex) { + return knex.schema.createTable ('users', (table) => { + table.increments ('id'); + table.string ('name'); + table.string ('email'); + table.string ('password'); + table.string ('salt'); + table.boolean ('deleted'); + + table.unique ('name', 'email'); + }); +} + +/** + * create table applications + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_applications (knex) { + return knex.schema.createTable ('applications', (table) => { + table.increments ('id'); + table.string ('name'); + }); +} + +/** + * create table permissions + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_permissions (knex) { + return knex.schema.createTable ('permissions', (table) => { + table.increments ('id'); + table.integer ('user_id'); + table.boolean ('create_app'); + table.boolean ('manage_users'); + table.boolean ('manage_permissions'); + table.boolean ('manage_apps'); + table.boolean ('manage_keys'); + table.boolean ('issue_key'); + + table.foreign ('user_id') + .references ('id') + .inTable ('users'); + }); +} + +/** + * create table user_access + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_user_access (knex) { + return knex.schema.createTable ('user_access', (table) => { + table.increments ('id'); + table.integer ('user_id'); + table.integer ('app_id'); + table.string ('crud'); + + table.foreign ('user_id') + .references ('id') + .inTable ('users'); + table.foreign ('app_id') + .references ('id') + .inTable ('applications'); + }); +} + +/** + * create table auth_keys + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_auth_keys (knex) { + return knex.schema.createTable ('auth_keys', (table) => { + table.increments ('id'); + table.string ('key'); + table.integer ('app_id'); + table.boolean ('read_data'); + table.string ('hash'); + table.string ('name'); + + table.unique ('key', 'hash'); + + table.foreign ('app_id') + .references ('id') + .inTable ('applications'); + }); +} + +/** + * create table categories + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_categories (knex) { + return knex.schema.createTable ('categories', (table) => { + table.increments ('id'); + table.string ('name'); + table.integer ('app_id'); + + table.unique ('name'); + + table.foreign ('app_id') + .references ('id') + .inTable ('applications'); + }); +} + +/** + * create table log + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_log (knex) { + return knex.schema.createTable ('log', (table) => { + table.increments ('id'); + table.integer ('key_id'); + table.integer ('app_id'); + table.integer ('timestamp'); + table.integer ('category_id'); + + table.foreign ('key_id') + .references ('id') + .inTable ('auth_keys'); + table.foreign ('app_id') + .references ('id') + .inTable ('applications'); + table.foreign ('category_id') + .references ('id') + .inTable ('categories'); + }); +} + +/** + * create table data + * + * @param {any} knex database connection + * @returns {Promise} result + */ +function create_data (knex) { + return knex.schema.createTable ('data', (table) => { + table.increments ('id'); + table.integer ('log_id'); + table.string ('key'); + table.string ('value'); + + table.foreign ('log_id') + .references ('id') + .inTable ('log'); + }); +} + +/** + * run migration + * + * @param {any} knex db connection + */ +async function up (knex) { + await create_users (knex); + await create_applications (knex); + await create_permissions (knex); + await create_user_access (knex); + await create_auth_keys (knex); + await create_categories (knex); + await create_log (knex); + await create_data (knex); +} + +/** + * revert migration + */ +function down () { + // noop +} + +module.exports = { up, down };