Socket
Socket
Sign inDemoInstall

feathers-sequelize

Package Overview
Dependencies
4
Maintainers
2
Versions
84
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.9 to 1.0.10

24

example/app.js

@@ -1,7 +0,9 @@

var feathers = require('feathers');
var bodyParser = require('body-parser');
var Sequelize = require('sequelize');
var path = require('path');
var sequelizeService = require('../lib');
var sequelize = new Sequelize('sequelize', '', '', {
import path from 'path';
import feathers from 'feathers';
import rest from 'feathers-rest';
import bodyParser from 'body-parser';
import Sequelize from 'sequelize';
import service from '../lib';
const sequelize = new Sequelize('sequelize', '', '', {
dialect: 'sqlite',

@@ -11,3 +13,3 @@ storage: path.join(__dirname, 'db.sqlite'),

});
var Todo = sequelize.define('todo', {
const Todo = sequelize.define('todo', {
text: {

@@ -30,6 +32,4 @@ type: Sequelize.STRING,

var app = feathers()
// Enable Socket.io
.configure(feathers.socketio())
// Enable REST services
.configure(feathers.rest())
.configure(rest())
// Turn on JSON parser for REST services

@@ -42,3 +42,3 @@ .use(bodyParser.json())

// and a maximum size of 4
app.use('/todos', sequelizeService({
app.use('/todos', service({
Model: Todo,

@@ -57,4 +57,4 @@ paginate: {

// Start the server
module.exports = app.listen(3030);
export default app.listen(3030);
console.log('Feathers Todo sequelize service running on 127.0.0.1:3030');
{
"name": "feathers-sequelize",
"description": "A service adapter for Sequelize an SQL ORM",
"version": "1.0.9",
"version": "1.0.10",
"homepage": "https://github.com/feathersjs/feathers-sequelize",

@@ -46,3 +46,3 @@ "main": "lib/",

"mocha": "mocha test/ --compilers js:babel-core/register",
"test": "npm run jshint && npm run mocha"
"test": "npm run jshint && npm run mocha && nsp check"
},

@@ -66,6 +66,8 @@ "directories": {

"chai": "^3.4.1",
"feathers": "^1.3.0",
"feathers": "^2.0.0-pre.4",
"feathers-rest": "^1.1.1",
"feathers-service-tests": "^0.5.2",
"jshint": "^2.8.0",
"mocha": "^2.3.3",
"nsp": "^2.2.0",
"sequelize": "^3.14.1",

@@ -72,0 +74,0 @@ "sqlite3": "^3.1.1"

@@ -13,16 +13,11 @@ # feathers-sequelize

## Documentation
## Getting Started
Please refer to the [Feathers database adapter documentation](http://docs.feathersjs.com/databases/readme.html) for more details or directly at:
`feathers-sequelize` hooks a Sequelize Model up as a service.
- [Sequelize](http://docs.feathersjs.com/databases/sequelize.html) - The detailed documentation for this adapter
- [Extending](http://docs.feathersjs.com/databases/extending.html) - How to extend a database adapter
- [Pagination and Sorting](http://docs.feathersjs.com/databases/pagination.html) - How to use pagination and sorting for the database adapter
- [Querying](http://docs.feathersjs.com/databases/querying.html) - The common adapter querying mechanism
```js
var SequelizeModel = require('./models/mymodel');
var sequelize = require('feathers-sequelize');
app.use('/todos', sequelize({
Model: SequelizeModel
}));
```
### Complete Example

@@ -33,8 +28,10 @@

```js
var path = require('path');
var feathers = require('feathers');
var bodyParser = require('body-parser');
var Sequelize = require('sequelize');
var sequelizeService = require('feathers-sequelize');
var sequelize = new Sequelize('sequelize', '', '', {
import path from 'path';
import feathers from 'feathers';
import rest from 'feathers-rest';
import bodyParser from 'body-parser';
import Sequelize from 'sequelize';
import service from 'feathers-sequelize';
const sequelize = new Sequelize('sequelize', '', '', {
dialect: 'sqlite',

@@ -44,9 +41,10 @@ storage: path.join(__dirname, 'db.sqlite'),

});
var Todo = sequelize.define('todo', {
const Todo = sequelize.define('todo', {
text: {
type: Sequelize.STRING
type: Sequelize.STRING,
allowNull: false
},
complete: {
type: Sequelize.BOOLEAN
type: Sequelize.BOOLEAN,
defaultValue: false
}

@@ -57,11 +55,6 @@ }, {

// Removes all database content
Todo.sync({ force: true });
// Create a feathers instance.
var app = feathers()
// Enable Socket.io
.configure(feathers.socketio())
const app = feathers()
// Enable REST services
.configure(feathers.rest())
.configure(rest())
// Turn on JSON parser for REST services

@@ -72,5 +65,8 @@ .use(bodyParser.json())

// Removes all database content
Todo.sync({ force: true });
// Create an in-memory Feathers service with a default page size of 2 items
// and a maximum size of 4
app.use('/todos', sequelizeService({
app.use('/todos', service({
Model: Todo,

@@ -91,263 +87,2 @@ paginate: {

## Options
Creating a new Sequelize service currently offers the following options:
- `Model` (**required**) - The Sequelize model definition
- `id` (default: `id`) [optional] - The name of the id property
- `paginate` [optional] - A pagination object containing a `default` and `max` page size (see below)
## 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
There are several ways to extend the basic CRUD functionality of this service.
_Keep in mind that calling the original service methods will return a Promise that resolves with the value._
### feathers-hooks
The most flexible option is weaving in functionality through [feathers-hooks](https://github.com/feathersjs/feathers-hooks), for example, the
user that made the request could be added like this:
```js
var feathers = require('feathers');
var hooks = require('feathers-hooks');
var sequelize = require('feathers-sequelize');
// Assuming todo.js exports the Sequelize model definition
var Todo = require('./models/todo.js');
var app = feathers()
.configure(hooks())
.use('/todos', sequelize({
Model: Todo,
paginate: {
default: 2,
max: 4
}
}));
app.service('todos').before({
// You can create a single hook like this
create: function(hook, next) {
hook.data.user_id = hook.params.user.id;
next();
}
});
app.listen(3030);
```
### Classes (ES6)
The module also exports a Babel transpiled ES6 class as `Service` that can be directly extended like this:
```js
import Todo from './models/todo';
import { Service } from 'feathers-sequelize';
class MyService extends Service {
create(data, params) {
data.user_id = params.user.id;
return super.create(data, params);
}
}
app.use('/todos', new MyService({
Model: Todo,
paginate: {
default: 2,
max: 4
}
}));
```
### Uberproto (ES5)
You can also use `.extend` on a service instance (extension is provided by [Uberproto](https://github.com/daffl/uberproto)):
```js
var myService = memory({
Model: Todo,
paginate: {
default: 2,
max: 4
}
}).extend({
create: function(data, params) {
data.user_id = params.user.id;
return this._super.apply(this, arguments);
}
});
app.use('/todos', myService);
```
**Note:** _this is more for backwards compatibility. We recommend the usage of hooks as they are easier to test, easier to maintain and are more flexible._
## Query Parameters
The `find` API allows the use of `$limit`, `$skip`, `$sort`, and `$select` in the query. These special parameters can be passed directly inside the query object:
```js
// Find all recipes that include salt, limit to 10, only include name field.
{"ingredients":"salt", "$limit":10, "$select": ["name"] } } // JSON
GET /?ingredients=salt&$limit=10&$select[]=name // HTTP
```
As a result of allowing these to be put directly into the query string, you won't want to use `$limit`, `$skip`, `$sort`, or `$select` as the name of fields in your document schema.
### `$limit`
`$limit` will return only the number of results you specify:
```
// Retrieves the first two records found where age is 37.
query: {
age: 37,
$limit: 2
}
```
### `$skip`
`$skip` will skip the specified number of results:
```
// Retrieves all except the first two records found where age is 37.
query: {
age: 37,
$skip: 2
}
```
### `$sort`
`$sort` will sort based on the object you provide:
```
// Retrieves all where age is 37, sorted ascending alphabetically by name.
query: {
age: 37,
$sort: { name: 1 }
}
// Retrieves all where age is 37, sorted descending alphabetically by name.
query: {
age: 37,
$sort: { name: -1}
}
```
### `$select`
`$select` support in a query allows you to pick which fields to include or exclude in the results.
```
// Only retrieve name.
query: {
name: 'Alice',
$select: {'name': 1}
}
// Retrieve everything except age.
query: {
name: 'Alice',
$select: {'age': 0}
}
```
## Filter criteria
In addition to sorting and pagination, properties can also be filtered by criteria. Standard criteria can just be added to the query. For example, the following find all users with the name `Alice`:
```js
query: {
name: 'Alice'
}
```
Additionally, the following advanced criteria are supported for each property.
### $in, $nin
Find all records where the property does (`$in`) or does not (`$nin`) contain the given values. For example, the following query finds every user with the name of `Alice` or `Bob`:
```js
query: {
name: {
$in: ['Alice', 'Bob']
}
}
```
### $lt, $lte
Find all records where the value is less (`$lt`) or less and equal (`$lte`) to a given value. The following query retrieves all users 25 or younger:
```js
query: {
age: {
$lte: 25
}
}
```
### $gt, $gte
Find all records where the value is more (`$gt`) or more and equal (`$gte`) to a given value. The following query retrieves all users older than 25:
```js
query: {
age: {
$gt: 25
}
}
```
### $ne
Find all records that do not contain the given property value, for example anybody not age 25:
```js
query: {
age: {
$ne: 25
}
}
```
### $or
Find all records that match any of the given objects. For example, find all users name Bob or Alice:
```js
query: {
$or: [
{ name: 'Alice' },
{ name: 'Bob' }
]
}
```
## Changelog

@@ -354,0 +89,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc