feathers-postgres
Create a Postgres service for FeathersJS
Getting Started
To install feathers-hooks from npm, run:
$ npm install feathers-postgres --save
Creating an Postgres service is this simple:
var mongoService = ;app;
Complete Example
Here's a complete example of a Feathers server with a todos
postgres-service.
// server.jsvar feathers = bodyParser = mongoService = ; // Create a feathers instance.var app = // Setup the public folder. // Enable Socket.io // Enable REST services // Turn on JSON parser for REST services // Turn on URL-encoded parser for REST services // Connect to the db, create and register a Feathers service.app; // Start the server.var port = 8080;app;
You can run this example by using node examples/basic
and going to 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!
Options
The following options can be passed when creating a new Postgres service:
General options:
connectionString
- A Postgres connection string_id
- The id property (default:"_id"
)
Connection options: (when connectionString
is not set)
db
- The name of the database (default:"feathers"
)host
- The Postgres host (default:"localhost"
)port
- The Postgres port (default:27017
)username
- Postgres usernamepassword
- Postgres passwordreconnect
- Whether the connection should automatically reconnect (default:true
)
Postgres options:
w
- Write acknowledgments (default:1
)journal
- Don't wait for journal before acknowledgment (default:false
)fsync
- Don't wait for syncing to disk before acknowledgment (default:false
)safe
- Safe mode (default:false
)
Sharing a Postgres connection between services
When creating a new service, the default behavior is to create a new connection to the specified database. If you would rather share a database connection between multiple services, connect to the database then pass an already-connected collection object in on options.collection. For example:
var feathers = mongo = mongoService = app = ; // First, make the connection.var db = mongo; // Use the same db connection in both of these services.app;app; app;
Extending Postgres services
To extend the basic Postgres service there are two options. Either through using Uberproto's inheritance mechanism or by using feathers-hooks.
With Uberproto
The basic Postgres Feathers service is implemented using Uberproto, a small EcmaScript 5 inheritance library so you can use the Uberproto syntax to add your custom functionality.
For example, you might want update
and patch
to behave the same (the basic implementation of update
replaces the entire object instead of merging it) and add an updatedAt
and createdAt
flag to your data:
// myservice.jsvar mongodb = ;var Proto = ; var TimestampPatchService = mongodbService; // Export a simple function that instantiates this new service like// var myservice = require('myservice');// app.use('/users', myservice(options));module { // We need to call `Proto.create` explicitly here since we are overriding // the original `create` method return Protocreate;} moduleexportsService = TimestampPatchService;
With hooks
Another option is to weave functionality into your existing services using feathers-hooks, for example the above createdAt
and updatedAt
functionality:
var feathers = ;var hooks = ;var mongodb = ; // Initialize a Postgres service with the users collection on a local Postgres instancevar app = ; app; app;
Special Query Params
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:
// Find all recipes that include salt, limit to 10, only include name field."ingredients":"salt" "$limit":10 "$select":"name:1" // JSONGET /?ingredients=salt&%24limit=10&%24select=name%3A1 // 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. Note: you can use the include syntax or the exclude syntax, not both together. See the section on Select
in the Mongoose docs.
// Only retrieve name.
query: {
name: 'Alice',
$select: {'name': 1}
}
// Retrieve everything except age.
query: {
name: 'Alice',
$select: {'age': 0}
}
API
feathers-mongodb
services comply with the standard FeathersJS API.
Changelog
1.0.0
- makes this adapter consistent with the others in terms of documentation and file structure
- updates mongoskin dependency to the latest
- adds support for special query filters
- $sort
- $select
- $skip
- $limit
- Closes #8 by making sure that we autoreconnect by default when not passing a connection string
0.3.0
- Implement
.patch
support (#5) - Better documentation
- Refactoring that removes pre-implemented MongoSkin options
0.2.x
- Pre-releases