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

Es6 #22

Merged
merged 8 commits into from
Dec 21, 2015
Merged

Es6 #22

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
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
.babelrc
src/
test/
!lib/
49 changes: 40 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ npm install feathers-knex --save

## Getting Started

You can create a \*SQL Knex service like this:
You can create a SQL Knex service like this:

```js
var knex = require('feathers-knex');
Expand All @@ -42,16 +42,23 @@ var feathers = require('feathers');
var bodyParser = require('body-parser');
var knex = require('feathers-knex');

var todos = knex('todos', {
var options = {
name: 'todos',
dialect: 'sqlite3',
connection: {
filename: './data.db'
},
paginate: {
default: 2,
max: 4
}
});
};

var todos = knex(options);

// This drops and creates table every time
todos.knex.schema.dropTableIfExists('todos').then(() => {
people.knex.schema.createTable('todos', function(table) {
todos.knex.schema.dropTableIfExists('todos').then(function() {
return todos.knex.schema.createTable('todos', function(table) {
table.increments('id');
table.string('text');
table.boolean('complete');
Expand All @@ -75,18 +82,42 @@ var app = feathers()
app.use('/todos', todos);

// Start the server.
var port = 8080;
var port = 3030;
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!

You can run this example by using `node example/app` and going to [localhost:3030/todos](http://localhost:3030/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!

## Options

// Todo
Creating a new Knex service currently offers the following options:

- `name` (**required**) - this is the table name for your resource.
- `id` (default: `id`) [optional] - The name of the id property
- `paginate` [optional] - A pagination object containing a `default` and `max` page size (see below)

_You will also need to pass additional Knex connection options. [See Knex docs](http://knexjs.org/#Installation-client)._

## Pagination

When initializing the service you can set the following pagination options in the `paginate` object:

- `default` - Sets the default number of items
- `max` - Sets the maximum allowed number of items per page (even if the `$limit` query parameter is set higher)

When `paginate.default` is set, `find` will return an object (instead of the normal array) in the following form:

```
{
"total": "<total number of records>",
"limit": "<max number of items per page>",
"skip": "<number of skipped items (offset)>",
"data": [/* data */]
}
```


## Extending

Expand Down
53 changes: 53 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var feathers = require('feathers');
var bodyParser = require('body-parser');
var knex = require('../lib');
var path = require('path');

var options = {
name: 'todos',
client: 'sqlite3',
connection: {
filename: './db.sqlite'
},
paginate: {
default: 2,
max: 4
}
};

var todos = knex(options);

todos.knex.schema.dropTableIfExists('todos').then(function() {
console.log('Dropped todos table');

return todos.knex.schema.createTable('todos', function(table) {
console.log('Creating todos table');
table.increments('id');
table.string('text');
table.boolean('complete');
});
});

// Create a feathers instance.
var app = feathers()
// Enable Socket.io
.configure(feathers.socketio())
// Enable REST services
.configure(feathers.rest())
// Turn on JSON parser for REST services
.use(bodyParser.json())
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({ extended: true }));

// Create an in-memory Feathers service with a default page size of 2 items
// and a maximum size of 4
app.use('/todos', todos);

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');
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"release:minor": "npm version minor && npm publish",
"release:major": "npm version major && npm publish",
"compile": "rm -rf lib/ && babel -d lib/ src/",
"watch": "babel --watch -d lib/ src/",
"jshint": "jshint src/. test/. --config",
"mocha": "mocha test/ --compilers js:babel-core/register",
"test": "npm run compile && npm run jshint && npm run mocha"
Expand All @@ -45,22 +46,23 @@
"lib": "lib"
},
"dependencies": {
"feathers-errors": "^0.2.5",
"feathers-query-filters": "^1.1.1",
"babel-polyfill": "^6.3.14",
"feathers-errors": "^1.1.5",
"feathers-query-filters": "^1.3.0",
"is-plain-object": "^2.0.1",
"knex": "^0.8.6",
"uberproto": "^1.1.2"
"uberproto": "^1.2.0"
},
"devDependencies": {
"babel-cli": "^6.1.4",
"babel-core": "^6.1.4",
"babel-plugin-add-module-exports": "^0.1.1",
"babel-plugin-transform-object-assign": "^6.1.18",
"babel-preset-es2015": "^6.1.4",
"chai": "^3.4.0",
"database-cleaner": "^0.10.1",
"feathers": "^1.1.1",
"feathers-service-tests": "^0.3.0",
"body-parser": "^1.14.1",
"chai": "^3.4.1",
"feathers": "^1.3.0",
"feathers-service-tests": "^0.5.2",
"jshint": "^2.8.0",
"mocha": "^2.3.3",
"sqlite3": "^3.1.0"
Expand Down
37 changes: 37 additions & 0 deletions src/error-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import errors from 'feathers-errors';

export default function errorHandler(error) {
let feathersError = error;

//TODO (EK): Map PG, MySQL, Oracle, etc. errors

// NOTE (EK): Error codes taken from
// https://www.sqlite.org/c3ref/c_abort.html

if (error.code === 'SQLITE_ERROR') {
switch(error.errno) {
case 1:
case 8:
case 18:
case 19:
case 20:
feathersError = new errors.BadRequest(error);
break;
case 2:
feathersError = new errors.Unavailable(error);
break;
case 3:
case 23:
feathersError = new errors.Forbidden(error);
break;
case 12:
feathersError = new errors.NotFound(error);
break;
default:
feathersError = new errors.GeneralError(error);
break;
}
}

throw feathersError;
}
Loading