Skip to content

Commit

Permalink
.init({}, fn) for initializing db schema
Browse files Browse the repository at this point in the history
  • Loading branch information
kc-dot-io committed May 30, 2017
1 parent 4c0cb15 commit 0816b43
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import isPlainObject from 'is-plain-object';
import { errors } from 'feathers-errors';
import errorHandler from './error-handler';

const debug = require('debug')('feathers-knex');

const METHODS = {
$or: 'orWhere',
$ne: 'whereNot',
Expand Down Expand Up @@ -51,6 +53,17 @@ class Service {
return Proto.extend(obj, this);
}

init (opts = {}, cb) {
let k = this.knex;
let table = this.table;

return k.schema.dropTableIfExists(table)
.then(function () {
debug(`dropped ${table} table`);
return k.schema.createTable(table, cb);
});
}

knexify (query, params, parentKey) {
Object.keys(params || {}).forEach(key => {
const value = params[key];
Expand Down
61 changes: 34 additions & 27 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,44 @@ const db = knex({
}
});

const people = service({
Model: db,
name: 'people',
events: [ 'testing' ]
});

const peopleId = service({
Model: db,
id: 'customid',
name: 'people-customid',
events: [ 'testing' ]
});

function clean () {
return db.schema.dropTableIfExists('people')
.then(() => db.schema.dropTableIfExists('people-customid'))
.then(() =>
db.schema.createTable('people', table => {
table.increments('id');
table.string('name');
table.integer('age');
table.integer('time');
table.boolean('created');
}).then(() => db.schema.createTable('people-customid', table => {
table.increments('customid');
table.string('name');
table.integer('age');
table.integer('time');
table.boolean('created');
}))
);
return people.init({}, (table) => {
table.increments('id');
table.string('name');
table.integer('age');
table.integer('time');
table.boolean('created');
return table;
})
.then(() => {
return peopleId.init({}, (table) => {
table.increments('customid');
table.string('name');
table.integer('age');
table.integer('time');
table.boolean('created');
return table;
});
});
}

describe('Feathers Knex Service', () => {
const app = feathers().use('/people', service({
Model: db,
name: 'people',
events: [ 'testing' ]
})).use('/people-customid', service({
Model: db,
id: 'customid',
name: 'people-customid',
events: [ 'testing' ]
}));
const app = feathers()
.use('/people', people)
.use('people-customid', peopleId);

before(clean);
after(clean);
Expand Down

0 comments on commit 0816b43

Please sign in to comment.