feathers-memory
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -1,11 +0,10 @@ | ||
var feathers = require('feathers'); | ||
var bodyParser = require('body-parser'); | ||
var memory = require('../lib'); | ||
import feathers from 'feathers'; | ||
import bodyParser from 'body-parser'; | ||
import rest from 'feathers-rest'; | ||
import memory from '../lib'; | ||
// 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 | ||
@@ -28,2 +27,2 @@ .use(bodyParser.json()) | ||
console.log('Feathers Todo memory service running on 127.0.0.1:3030'); | ||
console.log('Feathers Todo memory service running on 127.0.0.1:3030'); |
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -38,3 +38,3 @@ Object.defineProperty(exports, "__esModule", { | ||
var Service = (function () { | ||
var Service = function () { | ||
function Service() { | ||
@@ -197,3 +197,3 @@ var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
return Service; | ||
})(); | ||
}(); | ||
@@ -200,0 +200,0 @@ function init(options) { |
{ | ||
"name": "feathers-memory", | ||
"description": "An in memory service store", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"homepage": "https://github.com/feathersjs/feathers-memory", | ||
@@ -11,8 +11,3 @@ "main": "lib/", | ||
], | ||
"license": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/feathersjs/feathers-memory/blob/master/LICENSE" | ||
} | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
@@ -44,3 +39,4 @@ "type": "git", | ||
"mocha": "mocha test/ --compilers js:babel-core/register", | ||
"test": "npm run compile && npm run jshint && npm run mocha" | ||
"test": "npm run compile && npm run jshint && npm run mocha && nsp check", | ||
"example": "babel-node example/app.js" | ||
}, | ||
@@ -52,4 +48,6 @@ "directories": { | ||
"babel-polyfill": "^6.2.0", | ||
"feathers": "^2.0.0-pre.4", | ||
"feathers-errors": "^1.1.4", | ||
"feathers-query-filters": "^1.1.1", | ||
"feathers-rest": "^1.1.1", | ||
"lodash": "^3.10.1", | ||
@@ -64,15 +62,7 @@ "uberproto": "^1.2.0" | ||
"body-parser": "^1.14.1", | ||
"feathers": "^1.1.1", | ||
"feathers-service-tests": "^0.5.0", | ||
"jshint": "^2.8.0", | ||
"mocha": "^2.3.3" | ||
}, | ||
"babel": { | ||
"plugins": [ | ||
"add-module-exports" | ||
], | ||
"presets": [ | ||
"es2015" | ||
] | ||
"mocha": "^2.3.3", | ||
"nsp": "^2.2.0" | ||
} | ||
} |
300
README.md
@@ -14,14 +14,11 @@ # feathers-memory | ||
## Documentation | ||
## Getting Started | ||
Please refer to the [Feathers database adapter documentation](http://docs.feathersjs.com/databases/readme.html) for more details or directly at: | ||
You can create an in-memory service with no options: | ||
- [In Memory](http://docs.feathersjs.com/databases/memory.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 memory = require('feathers-memory'); | ||
app.use('/todos', memory()); | ||
``` | ||
This will create a `todos` datastore with the default configuration. | ||
## Complete Example | ||
@@ -32,13 +29,11 @@ | ||
```js | ||
// app.js | ||
var feathers = require('feathers'); | ||
var bodyParser = require('body-parser'); | ||
var memory = require('feathers-memory'); | ||
import feathers from 'feathers'; | ||
import bodyParser from 'body-parser'; | ||
import rest from 'feathers-rest'; | ||
import memory from '../lib'; | ||
// 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 | ||
@@ -70,276 +65,9 @@ .use(bodyParser.json()) | ||
app.listen(port, function() { | ||
console.log('Feathers server listening on port ' + port); | ||
console.log(`Feathers server listening on port ${port}`); | ||
}); | ||
``` | ||
You can run this example by using `node examples/app` and going to [localhost:3030/todos](http://localhost:3030/todos). You will see the test Todo that we created at the end of that file. | ||
You can run this example by using `npm run example` and going to [localhost:3030/todos](http://localhost:3030/todos). You will see the test Todo that we created at the end of that file. | ||
## 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, `createdAt` and `updatedAt` timestamps could be added like this: | ||
```js | ||
var feathers = require('feathers'); | ||
var hooks = require('feathers-hooks'); | ||
var memory = require('feathers-memory'); | ||
var app = feathers() | ||
.configure(hooks()) | ||
.use('/todos', memory({ | ||
paginate: { | ||
default: 2, | ||
max: 4 | ||
} | ||
})); | ||
var stripIds = function() { | ||
delete hook.data.id; | ||
next(); | ||
} | ||
var updateTimestamp = function(hook, next) { | ||
hook.data.updatedAt = new Date(); | ||
next(); | ||
} | ||
app.service('todos').before({ | ||
// You can create a single hook like this | ||
create: function(hook, next) { | ||
hook.data.createdAt = new Date(); | ||
next(); | ||
}, | ||
// Or you can chain multiple hooks like this | ||
update: [stripIds, updateTimeStamp] | ||
}); | ||
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 { Service } from 'feathers-memory'; | ||
class MyService extends Service { | ||
create(data, params) { | ||
data.created_at = new Date(); | ||
return super.create(data, params).then(todo); | ||
} | ||
} | ||
app.use('/todos', new MyService({ | ||
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({ | ||
paginate: { | ||
default: 2, | ||
max: 4 | ||
} | ||
}).extend({ | ||
create: function(data) { | ||
data.created_at = new Date(); | ||
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._ | ||
## Options | ||
The following options can be passed when creating a new memory service: | ||
- `idField` - The name of the id field property. Default is `id` | ||
- `startId` - An id number to start with that will be incremented for new record (default: `0`) | ||
- `store` - An object with id to item assignments to pre-initialize the data store | ||
- `paginate` - 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 */] | ||
} | ||
``` | ||
## 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 | ||
@@ -346,0 +74,0 @@ |
14750
7
6
111
+ Addedfeathers@^2.0.0-pre.4
+ Addedfeathers-rest@^1.1.1
+ Addedaccepts@1.3.8(transitive)
+ Addedarray-flatten@1.1.1(transitive)
+ Addedbody-parser@1.20.3(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookie@0.7.1(transitive)
+ Addedcookie-signature@1.0.6(transitive)
+ Addeddebug@3.2.7(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.22.0.0(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedetag@1.8.1(transitive)
+ Addedevents@1.1.1(transitive)
+ Addedexpress@4.21.2(transitive)
+ Addedfeathers@2.2.4(transitive)
+ Addedfeathers-commons@0.8.7(transitive)
+ Addedfeathers-errors@2.9.2(transitive)
+ Addedfeathers-rest@1.8.1(transitive)
+ Addedfinalhandler@1.3.1(transitive)
+ Addedforwarded@0.2.0(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmerge-descriptors@1.0.3(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.1.3(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-to-regexp@0.1.12(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedrange-parser@1.2.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedrubberduck@1.1.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsend@0.19.0(transitive)
+ Addedserve-static@1.16.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedutils-merge@1.0.1(transitive)
+ Addedvary@1.1.2(transitive)