Skip to content

Commit

Permalink
➕ FLush handler #9
Browse files Browse the repository at this point in the history
Also renames table_name to tableName for consistency between options and to not encourage snake case
  • Loading branch information
jrans committed Oct 27, 2016
1 parent 15ad9cc commit e9b9d55
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 26 deletions.
2 changes: 1 addition & 1 deletion example_schema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = {
table_name: 'user_data', // eslint-disable-line
tableName: 'user_data',
fields: {
email: {
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion lib/config_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions lib/db_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
6 changes: 5 additions & 1 deletion lib/sql_gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -120,3 +120,7 @@ exports.delete = function _delete (options) {

return [query, values];
};

exports.dropTable = function dropTable (options) {
return 'DROP TABLE "' + options.tableName + '";';
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 7 additions & 7 deletions test/config_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
33 changes: 29 additions & 4 deletions test/db_handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
}];

Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
});
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/istantiate_db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 20 additions & 9 deletions test/sql_gen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
});

Expand All @@ -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' }
});
Expand All @@ -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: '[email protected]' } }
{ tableName: schema.tableName, fields: { email: '[email protected]' } }
);

t.equal(
Expand All @@ -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],
Expand All @@ -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: '[email protected]' }
});

Expand All @@ -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],
Expand All @@ -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: '[email protected]' },
where: { foo: 'bar' }
});
Expand All @@ -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' }
});

Expand All @@ -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' }
});

Expand All @@ -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();
});

0 comments on commit e9b9d55

Please sign in to comment.