Skip to content

Commit

Permalink
➕ Except single schema or array #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jrans committed Oct 26, 2016
1 parent bb889a4 commit 1eeacd1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/config_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var fieldSchema = Joi.object()
.keys({ type: Joi.any().valid(fieldTypes) })
.unknown()
;
var configSchema = Joi.object().keys({
var tableSchema = Joi.object().keys({
table_name: Joi.string()
.regex(dbNameRegEx)
.required(),
Expand All @@ -21,6 +21,8 @@ var configSchema = Joi.object().keys({
.required()
});

var configSchema = [tableSchema, Joi.array().items(tableSchema)];

module.exports = function (config) {
return Joi.assert(config, configSchema);
};
Expand Down
19 changes: 16 additions & 3 deletions lib/db_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

var sqlGen = require('./sql_gen.js');

var methods = {
init: function (client, config, _, cb) {
return client.query(sqlGen.init(config), cb);
var methods = {};

function multipleQuery (client, queries, cb) {
function nextQuery () {
var last = queries.length === 1;

return client.query(queries.pop(), !last ? nextQuery : cb);
}

return nextQuery();
}

methods.init = function (client, config, _, cb) {
var tables = [].concat(config);
var queries = tables.map(sqlGen.init);

return multipleQuery(client, queries, cb);
};

['select', 'update', 'delete', 'insert'].forEach(function (method) {
Expand Down
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": "1.0.0",
"version": "0.0.1",
"description": "A little experiment in defining models in Joi and creating PostgreSQL Tables",
"main": "lib/index.js",
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions test/config_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ test('config validator', function (t) {
t.end();
});

test('config validator, multiple tables', function (t) {
t.doesNotThrow(
validator([{
table_name: 'test', // eslint-disable-line
fields: { email: { type: 'string' } }
}, {
table_name: 'test_2', // eslint-disable-line
fields: { email: { type: 'string' } }
}]),
'handles multiple tables'
);

t.end();
});

test('dbNameRegEx', function (t) {
t.ok(
dbNameRegEx.exec('_a1pha_Numer1c'),
Expand Down
37 changes: 30 additions & 7 deletions test/db_handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ var dbConn = require('./test_pg_client.js');
var db = require('../lib/db_handlers.js');
var schema = require('../example_schema.js');

var client = dbConn.client;

var multipleSchema = [{
table_name: 'table_1', // eslint-disable-line
fields: { field: { type: 'string', email: true } }
}, {
table_name: 'table_2', // eslint-disable-line
fields: { field: { type: 'string', email: true } }
}];

var testInsert = {
email: '[email protected]',
Expand All @@ -16,17 +21,17 @@ var testInsert = {
};
var testTab = schema.table_name;

var client = dbConn.client;

test('init test client', function (t) {
client.connect(function () {
client.query('DROP TABLE IF EXISTS ' + schema.table_name, t.end);
client.query('DROP TABLE IF EXISTS ' + schema.table_name);
client.query('DROP TABLE IF EXISTS table_1');
client.query('DROP TABLE IF EXISTS table_2', t.end);
});
});

test('db.init', function (t) {
t.throws(
function () { db.init(client, { rubbish: 'schema' }) },
'error thrown when given when using invalid schema'
);
db.init(client, schema)
.then(function () { return client.query('SELECT * from user_data') })
.then(function (res) {
Expand All @@ -41,6 +46,24 @@ test('db.init', function (t) {
;
});

test('db.init multiple tables', function (t) {
function checkFieldExist (res) {
t.ok(
res.fields
.map(function (field) { return field.name })
.indexOf('field') > -1
, 'table created with a correct field'
);
}
db.init(client, multipleSchema)
.then(function () { return client.query('SELECT * from table_1') })
.then(checkFieldExist)
.then(function () { return client.query('SELECT * from table_2') })
.then(checkFieldExist)
.then(t.end)
;
});


test('db.insert & default select w custom where', function (t) {
db.insert(client, schema, { fields: testInsert, tableName: testTab })
Expand Down

0 comments on commit 1eeacd1

Please sign in to comment.