From e9b9d558968686181ed25cbbbc12c6cbdfd0d843 Mon Sep 17 00:00:00 2001 From: Jack Rans Date: Thu, 27 Oct 2016 00:13:23 -0500 Subject: [PATCH] :heavy_plus_sign: FLush handler #9 Also renames table_name to tableName for consistency between options and to not encourage snake case --- example_schema.js | 2 +- lib/config_validator.js | 2 +- lib/db_handlers.js | 7 +++++++ lib/sql_gen.js | 6 +++++- package.json | 2 +- test/config_validator.test.js | 14 +++++++------- test/db_handlers.test.js | 33 +++++++++++++++++++++++++++++---- test/index.test.js | 2 +- test/istantiate_db.test.js | 2 +- test/sql_gen.test.js | 29 ++++++++++++++++++++--------- 10 files changed, 73 insertions(+), 26 deletions(-) diff --git a/example_schema.js b/example_schema.js index 8b78c70..915c5b7 100644 --- a/example_schema.js +++ b/example_schema.js @@ -1,7 +1,7 @@ 'use strict'; module.exports = { - table_name: 'user_data', // eslint-disable-line + tableName: 'user_data', fields: { email: { type: 'string', diff --git a/lib/config_validator.js b/lib/config_validator.js index 5307758..524ce84 100644 --- a/lib/config_validator.js +++ b/lib/config_validator.js @@ -13,7 +13,7 @@ var fieldSchema = Joi.object() .unknown() ; var tableSchema = Joi.object().keys({ - table_name: Joi.string() + tableName: Joi.string() .regex(dbNameRegEx) .required(), fields: Joi.object() diff --git a/lib/db_handlers.js b/lib/db_handlers.js index be9dedf..9918b05 100644 --- a/lib/db_handlers.js +++ b/lib/db_handlers.js @@ -21,6 +21,13 @@ methods.init = function (client, config, _, cb) { return multipleQuery(client, queries, cb); }; +methods.flush = function (client, config, options, cb) { + var tables = [].concat(options || config); + var queries = tables.map(sqlGen.dropTable); + + return multipleQuery(client, queries, cb); +}; + ['select', 'update', 'delete', 'insert'].forEach(function (method) { methods[method] = function (client, _, options, cb) { var args = sqlGen[method](options).concat([cb]); diff --git a/lib/sql_gen.js b/lib/sql_gen.js index 7e78f22..1c5597b 100644 --- a/lib/sql_gen.js +++ b/lib/sql_gen.js @@ -30,7 +30,7 @@ function processWhere (where, query, values) { exports.init = function init (config) { - var tableName = config.table_name; + var tableName = config.tableName; var fields = config.fields; var columns = Object.keys(fields).map(function (key) { @@ -120,3 +120,7 @@ exports.delete = function _delete (options) { return [query, values]; }; + +exports.dropTable = function dropTable (options) { + return 'DROP TABLE "' + options.tableName + '";'; +}; diff --git a/package.json b/package.json index 3de1491..4e41806 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "abase-db", - "version": "0.0.2", + "version": "0.1.0", "description": "A little experiment in defining models in Joi and creating PostgreSQL Tables", "main": "lib/index.js", "devDependencies": { diff --git a/test/config_validator.test.js b/test/config_validator.test.js index bbb00f9..616b391 100644 --- a/test/config_validator.test.js +++ b/test/config_validator.test.js @@ -14,29 +14,29 @@ function validator (config) { test('config validator', function (t) { t.throws( validator({ fields: {} }), - 'error if no table_name property' + 'error if no tableName property' ); t.throws( - validator({ table_name: 'test' }), // eslint-disable-line + validator({ tableName: 'test' }), // eslint-disable-line 'error if no fields property' ); t.throws( validator({ - table_name: '2test', // eslint-disable-line + tableName: '2test', fields: {} }), 'error if table name doesn\t pass db name regex' ); t.throws( validator({ - table_name: 'test', // eslint-disable-line + tableName: 'test', fields: { '2field': { type: 'string' } } }), 'error if field name doesn\'t pass db name regex' ); t.doesNotThrow( validator({ - table_name: 'test', // eslint-disable-line + tableName: 'test', fields: { email: { type: 'string', unknown: 'allowed' } } }), 'no error when extra options unknown' @@ -48,10 +48,10 @@ test('config validator', function (t) { test('config validator, multiple tables', function (t) { t.doesNotThrow( validator([{ - table_name: 'test', // eslint-disable-line + tableName: 'test', fields: { email: { type: 'string' } } }, { - table_name: 'test_2', // eslint-disable-line + tableName: 'test_2', fields: { email: { type: 'string' } } }]), 'handles multiple tables' diff --git a/test/db_handlers.test.js b/test/db_handlers.test.js index 180ca68..c29ca23 100644 --- a/test/db_handlers.test.js +++ b/test/db_handlers.test.js @@ -7,10 +7,10 @@ var db = require('../lib/db_handlers.js'); var schema = require('../example_schema.js'); var multipleSchema = [{ - table_name: 'table_1', // eslint-disable-line + tableName: 'table_1', // eslint-disable-line fields: { field: { type: 'string', email: true } } }, { - table_name: 'table_2', // eslint-disable-line + tableName: 'table_2', // eslint-disable-line fields: { field: { type: 'string', email: true } } }]; @@ -19,13 +19,13 @@ var testInsert = { dob: '2001-09-27', username: 'test' }; -var testTab = schema.table_name; +var testTab = schema.tableName; var client = dbConn.client; test('init test client', function (t) { client.connect(function () { - client.query('DROP TABLE IF EXISTS ' + schema.table_name); + client.query('DROP TABLE IF EXISTS ' + schema.tableName); client.query('DROP TABLE IF EXISTS table_1'); client.query('DROP TABLE IF EXISTS table_2', t.end); }); @@ -152,6 +152,31 @@ test('db.delete w db.select', function (t) { ; }); +test('db.flush all via config', function (t) { + t.plan(1); + db.init(client, schema, null) + .then(function () { return db.flush(client, schema) }) + .then(function () { return client.query('SELECT * FROM ' + testTab + ';') }) + .catch(function (err) { return t.ok(err, 'selectin flushed table errors') }) + ; +}); + +test('db.flush all via options', function (t) { + t.plan(2); + db.init(client, multipleSchema, null) + .then(function () { + return db.flush(client, null, { tableName: 'table_2' }); + }) + .then(function () { return client.query('SELECT * FROM table_1;') }) + .then(function (res) { + t.ok(res, 'table_1 remians'); + + return client.query('SELECT * FROM table_2;'); + }) + .catch(function (err) { return t.ok(err, 'selectin flushed table errors') }) + ; +}); + test('close test DB connections', function (t) { client.end(t.end); }); diff --git a/test/index.test.js b/test/index.test.js index a403c08..321e605 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -51,7 +51,7 @@ test('Can register DB plugin with `schemaPath` option', function (t) { test('db handlers exposed', function (t) { var handlers = Object.keys(plugin.handlers); - var wanted = ['insert', 'select', 'delete', 'update', 'init']; + var wanted = ['insert', 'select', 'delete', 'update', 'init', 'flush']; t.ok( wanted.reduce(function (truth, handler) { diff --git a/test/istantiate_db.test.js b/test/istantiate_db.test.js index 603b9b1..0c1bf69 100644 --- a/test/istantiate_db.test.js +++ b/test/istantiate_db.test.js @@ -13,7 +13,7 @@ var testInsert = { dob: '2001-09-27', username: 'test' }; -var testTab = schema.table_name; +var testTab = schema.tableName; test('instantiateDb gives obj w/ methods bound to pg.Pool to cb', function (t) { instantiateDb(pool, schema, function (err, db) { diff --git a/test/sql_gen.test.js b/test/sql_gen.test.js index 1c65784..180f3f2 100644 --- a/test/sql_gen.test.js +++ b/test/sql_gen.test.js @@ -29,7 +29,7 @@ tape('::init - generate SQL to create a table if none exists', function (t) { tape('::select - generate SQL to select columns from a table', function (t) { var query = sqlGen.select({ - tableName: schema.table_name, + tableName: schema.tableName, select: ['email', 'dob'] }); @@ -44,7 +44,7 @@ tape('::select - generate SQL to select columns from a table', function (t) { tape('::select - gen. SQL to select cols from table w/ where', function (t) { var query = sqlGen.select({ - tableName: schema.table_name, + tableName: schema.tableName, select: ['email', 'dob'], where: { foo: 'bar' } }); @@ -60,7 +60,7 @@ tape('::select - gen. SQL to select cols from table w/ where', function (t) { tape('::insert - generate SQL to insert a column into a table', function (t) { var query = sqlGen.insert( - { tableName: schema.table_name, fields: { email: 'me@poop.com' } } + { tableName: schema.tableName, fields: { email: 'me@poop.com' } } ); t.equal( @@ -77,7 +77,7 @@ tape('::insert - generate SQL to insert a column into a table', function (t) { }); tape('::insert - generate SQL to insert blank col into table', function (t) { - var query = sqlGen.insert({ tableName: schema.table_name }); + var query = sqlGen.insert({ tableName: schema.tableName }); t.equal( query[0], @@ -94,7 +94,7 @@ tape('::insert - generate SQL to insert blank col into table', function (t) { tape('::update - generate SQL to update a column in a table', function (t) { var query = sqlGen.update({ - tableName: schema.table_name, + tableName: schema.tableName, fields: { email: 'me@poop.com' } }); @@ -112,7 +112,7 @@ tape('::update - generate SQL to update a column in a table', function (t) { }); tape('::update - generate SQL to update no fields of column', function (t) { - var query = sqlGen.update({ tableName: schema.table_name }); + var query = sqlGen.update({ tableName: schema.tableName }); t.equal( query[0], @@ -129,7 +129,7 @@ tape('::update - generate SQL to update no fields of column', function (t) { tape('::update - gen. SQL to update a col in table w/ where', function (t) { var query = sqlGen.update({ - tableName: schema.table_name, + tableName: schema.tableName, fields: { email: 'me@poop.com' }, where: { foo: 'bar' } }); @@ -149,7 +149,7 @@ tape('::update - gen. SQL to update a col in table w/ where', function (t) { tape('::delete should generate SQL to delete a row from a table', function (t) { var query = sqlGen.delete({ - tableName: schema.table_name, + tableName: schema.tableName, where: { username: 'bob' } }); @@ -168,7 +168,7 @@ tape('::delete should generate SQL to delete a row from a table', function (t) { tape('::delete should gen SQL to delete row w/ multiple where', function (t) { var query = sqlGen.delete({ - tableName: schema.table_name, + tableName: schema.tableName, where: { username: 'bob', dob: '20/04/1988' } }); @@ -184,3 +184,14 @@ tape('::delete should gen SQL to delete row w/ multiple where', function (t) { ); t.end(); }); + +tape('::dropTable should gen SQL to drop table', function (t) { + var query = sqlGen.dropTable({ tableName: schema.tableName }); + + t.equal( + query, + 'DROP TABLE "user_data";', + 'Generate parameterised query' + ); + t.end(); +});