Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize Database Table #106

Merged
merged 11 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,23 @@ import rest from 'feathers-rest';
import bodyParser from 'body-parser';
import knexService from 'feathers-knex';

// Initialize knex database adapter
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: './db.sqlite'
}
});

// Clean up our data. This is optional and is here
// because of our integration tests
knex.schema.dropTableIfExists('todos').then(function() {
console.log('Dropped todos table');

// Initialize your table
return knex.schema.createTable('todos', function(table) {
console.log('Creating todos table');
table.increments('id');
table.string('text');
table.boolean('complete');
});
// Create Knex Feathers service with a default page size of 2 items
// and a maximum size of 4
var todos = knexService({
Model: knex,
name: 'todos',
paginate: {
default: 2,
max: 4
}
});

// Create a feathers instance.
Expand All @@ -67,26 +65,32 @@ const app = feathers()
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({ extended: true }));

// Create Knex Feathers service with a default page size of 2 items
// and a maximum size of 4
app.use('/todos', knexService({
Model: knex,
name: 'todos',
paginate: {
default: 2,
max: 4
}
}));
// Initialize the database table with a schema
// then mount the service and start the app
todos
.init({}, function(table) {

app.use(function(error, req, res, next){
res.json(error);
});
//define your schema
console.log(`created ${table._tableName} table`);
table.increments('id');
table.string('text');
table.boolean('complete');

// Start the server.
const port = 8080;
app.listen(port, function() {
console.log(`Feathers server listening on port ${port}`);
});
}).then(() => {

app.use('/todos', todos);

app.use(function(error, req, res, next){
res.json(error);
});

// Start the server.
const port = 8080;
app.listen(port, function() {
console.log(`Feathers server listening on port ${port}`);
});

});
```

You can run this example by using `node server` and going to [localhost:8080/todos](http://localhost:8080/todos). You should see an empty array. That's because you don't have any Todos yet but you now have full CRUD for your new todos service!
Expand Down
53 changes: 26 additions & 27 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ import rest from 'feathers-rest';
import bodyParser from 'body-parser';
import knexService from '../lib';

// Initialize knex database adapter
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: './db.sqlite'
}
});

// Clean up our data. This is optional and is here
// because of our integration tests
knex.schema.dropTableIfExists('todos').then(function () {
console.log('Dropped todos table');

// Initialize your table
return knex.schema.createTable('todos', function (table) {
console.log('Creating todos table');
table.increments('id');
table.string('text');
table.boolean('complete');
});
// Create Knex Feathers service with a default page size of 2 items
// and a maximum size of 4
var todos = knexService({
Model: knex,
name: 'todos',
paginate: {
default: 2,
max: 4
}
});

// Create a feathers instance.
Expand All @@ -33,22 +31,23 @@ const app = feathers()
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({ extended: true }));

// Create Knex Feathers service with a default page size of 2 items
// and a maximum size of 4
app.use('/todos', knexService({
Model: knex,
name: 'todos',
paginate: {
default: 2,
max: 4
}
}));
// Initialize the database table with a schema
// then mount the service and start the app
todos
.init({}, function (table) {
// define your schema
console.log(`created ${table._tableName} table`);
table.increments('id');
table.string('text');
table.boolean('complete');
}).then(() => {
console.log('/todos mounted');
app.use('/todos', todos);

app.use(function (error, req, res, next) {
res.json(error);
});
app.use(function (error, req, res, next) {
res.json(error);
});
});

// Start the server
module.exports = app.listen(3030);

console.log('Feathers Todo Knex service running on 127.0.0.1:3030');
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"lib": "lib"
},
"dependencies": {
"debug": "^2.6.8",
"feathers-errors": "^2.0.1",
"feathers-query-filters": "^2.0.0",
"is-plain-object": "^2.0.1",
Expand All @@ -79,6 +80,6 @@
"mocha": "^3.0.0",
"rimraf": "^2.5.4",
"semistandard": "^11.0.0",
"sqlite3": "^3.1.0"
"sqlite3": "^3.1.8"
}
}
19 changes: 19 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,23 @@ class Service {
return Proto.extend(obj, this);
}

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

return new Promise(function (resolve, reject) {
k.schema.hasTable(table).then(exists => {
if (!exists) {
debug(`creating ${table}`);
k.schema.createTable(table, cb).then(resolve);
} else {
debug(`${table} already exists`);
resolve(null);
}
});
});
}

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