Hoodie Client for data persistence & offline sync
hoodie-store-client
combines pouchdb-hoodie-api and pouchdb-hoodie-sync. It adds
a few other methods like .isPersistent()
or .hasLocalChanges()
and a scoped
store API.
var Store = require('@hoodie/store-client')
var store = new Store('mydbname', { remote: 'http://localhost:5984/mydbname' })
// or
var PresetStore = Store.defaults({remoteBaseUrl: 'http://localhost:5984' })
var store = new PresetStore('mydb')
- Store.defaults
- Constructor
- store.add(properties)
- store.add(arrayOfProperties)
- store.find(id)
- store.find(doc)
- store.find(idsOrDocs)
- store.findOrAdd(id, doc)
- store.findOrAdd(doc)
- store.findOrAdd(idsOrDocs)
- store.findAll()
- store.update(id, changedProperties)
- store.update(id, updateFunction)
- store.update(doc)
- store.update(arrayOfDocs)
- store.updateOrAdd(id, doc)
- store.updateOrAdd(doc)
- store.updateOrAdd(arrayOfDocs)
- store.updateAll(changedProperties)
- store.updateAll(updateFunction)
- store.remove(id)
- store.remove(doc)
- store.remove(idsOrDocs)
- store.removeAll()
- store.pull()
- store.push()
- store.sync()
- store.connect()
- store.disconnect()
- store.isConnected()
- store.hasLocalChanges()
- store.on()
- store.one()
- store.off()
- store()
- Events
Store.defaults(options)
Argument | Type | Description | Required |
---|---|---|---|
remoteBaseUrl |
String | Base url to CouchDB. Will be used as remote prefix for store instances | No |
options.ajax |
Object or Function | Ajax request options (see PouchDB). If function passed, it gets executed and returned values is used | No |
Returns a custom Store Constructor with passed default options.
Example
var PresetStore = Store.defaults({
remoteBaseUrl: 'http://localhost:5984'
})
var store = new PresetStore('mydb')
store.sync() // will sync with http://localhost:5984/mydb
new Store(dbName, options)
Argument | Type | Description | Required |
---|---|---|---|
dbName |
String | name of the database | Yes |
options.remote |
String | name or URL of remote database | Yes (unless remoteBaseUrl is preset, see Store.defaults) |
options.ajax |
Object or Function | Ajax request options (see PouchDB). If function passed, it gets executed and returned values is used | No |
Returns store
API.
Example
var Store = require('@hoodie/store-client')
var store = new Store('mydb', { remote: 'http://localhost:5984/mydb' })
store.sync() // will sync with http://localhost:5984/mydb
store.add(properties)
Argument | Type | Description | Required |
---|---|---|---|
properties |
Object | properties of document | Yes |
properties.id |
String | If set, the document will be stored at given id | No |
Resolves with properties
and adds id
(unless provided), createdAt
and
updatedAt
properties.
{
"id": "12345678-1234-1234-1234-123456789ABC",
"foo": "bar",
"createdAt": "2016-05-09T12:00:00.000Z",
"updatedAt": "2016-05-09T12:00:00.000Z"
}
Rejects with:
🐕 Add expected Errors: #102
Name | Description |
---|---|
Error | ... |
Example
store.add({foo: 'bar'}).then(function (doc) {
alert(doc.foo) // bar
}).catch(function (error) {
alert(error)
})
Argument | Type | Description | Required |
---|---|---|---|
arrayOfProperties |
Array | Array of properties , see store.add(properties) |
Yes |
Resolves with properties
and adds id
(unless provided), createdAt
and
updatedAt
properties. Resolves with array of properties
items if called
with propertiesArray
.
{
"id": "12345678-1234-1234-1234-123456789ABC",
"foo": "bar",
"createdAt": "2016-05-09T12:00:00.000Z",
"updatedAt": "2016-05-09T12:00:00.000Z"
}
Rejects with:
🐕 Add expected Errors: #102
Name | Description |
---|---|
Error | ... |
Example: add single document
store.add({foo: 'bar'}).then(function (doc) {
alert(doc.foo) // bar
}).catch(function (error) {
alert(error)
})
Example: add multiple documents
store.add([{foo: 'bar'}, {bar: 'baz'}]).then(function (docs) {
alert(docs.length) // 2
}).catch(function (error) {
alert(error)
})
Argument | Type | Description | Required |
---|---|---|---|
id |
String | Unique id of document | Yes |
Resolves with properties
{
"id": "12345678-1234-1234-1234-123456789ABC",
"foo": "bar",
"createdAt": "2016-05-09T12:00:00.000Z",
"updatedAt": "2016-05-09T12:00:00.000Z"
}
Rejects with:
🐕 Add expected Errors: #102
Name | Description |
---|---|
Error | ... |
Example
store.find('12345678-1234-1234-1234-123456789ABC').then(function (doc) {
alert(doc.id)
}).catch(function (error) {
alert(error)
})
Argument | Type | Description | Required |
---|---|---|---|
doc |
Object | document with id property |
Yes |
Resolves with properties
{
"id": "12345678-1234-1234-1234-123456789ABC",
"foo": "bar",
"createdAt": "2016-05-09T12:00:00.000Z",
"updatedAt": "2016-05-09T12:00:00.000Z"
}
Rejects with:
🐕 Add expected Errors: #102
Name | Description |
---|---|
Error | ... |
Example
store.find(doc).then(function (doc) {
alert(doc.id)
}).catch(function (error) {
alert(error)
})
Argument | Type | Description | Required |
---|---|---|---|
idsOrDocs |
Array | Array of id (String) or doc (Object) items |
Yes |
Resolves with array of properties
[{
"id": "12345678-1234-1234-1234-123456789ABC",
"foo": "bar",
"createdAt": "2016-05-09T12:00:00.000Z",
"updatedAt": "2016-05-09T12:00:00.000Z"
}]
Rejects with:
🐕 Add expected Errors: #102
Name | Description |
---|---|
Error | ... |
Example
store.find(doc).then(function (doc) {
alert(doc.id)
}).catch(function (error) {
alert(error)
})
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Resolves with ``:
{
}
Rejects with:
Name | Description |
---|---|
Error | ... |
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Returns true
/ false
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Returns true
/ false
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Returns store
API.
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Returns store
API.
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Returns store
API.
Example
🐕 Complete README: #102
Argument | Type | Description | Required |
---|
Returns store
API with type
property preset in all methods to given type name.
Example
🐕 Complete README: #102
Event | Description | Arguments |
---|
Local setup
git clone https://github.com/hoodiehq/hoodie-store-client.git
cd hoodie-store-client
npm install
In Node.js
Run all tests and validate JavaScript Code Style using standard
npm test
To run only the tests
npm run test:node
Run tests in browser
npm run test:browser:local
This will start a local server. All tests and coverage will be run at http://localhost:8080/__zuul
Have a look at the Hoodie project's contribution guidelines. If you want to hang out you can join #hoodie-pouch on our Hoodie Community Slack.