8.3.0 ā€¢ Published 6 years ago

@hoodie/store-client v8.3.0

Weekly downloads
49
License
Apache-2.0
Repository
github
Last release
6 years ago

hoodie-store-client

Hoodie Client for data persistence & offline sync

Build Status Coverage Status Dependency Status devDependency Status

Example

var Store = require('@hoodie/store-client')
var store = new Store('mydbname', {
  PouchDB: require('pouchdb'),
  remote: 'http://localhost:5984/mydbname'
})

// or
var PresetStore = Store.defaults({
  PouchDB: require('pouchdb'),
  remoteBaseUrl: 'http://localhost:5984'
})
var store = new PresetStore('mydb')

API

Store.defaults

Store.defaults(options)
ArgumentTypeDescriptionRequired
options.remoteBaseUrlStringBase url to CouchDB. Will be used as remote prefix for store instancesNo
options.PouchDBConstructorPouchDB custom buildsYes

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

Constructor

new Store(dbName, options)
ArgumentTypeDescriptionRequired
dbNameStringname of the databaseYes
options.remoteStringname or URL of remote databaseYes (unless remoteBaseUrl is preset, see Store.defaults)
options.remoteObjectPouchDB instanceYes (ignores remoteBaseUrl from Store.defaults)
options.remotePromiseResolves to either string or PouchDB instancesee above
options.PouchDBConstructorPouchDB custom buildsYes (unless preset using Store.defaults))
options.validateFunction(doc)Validation function to execute before DB operations (Can return promise for async validation)No

Returns store API.

Example

var store = new Store('mydb', {
  PouchDB: PouchDB,
  remote: 'http://localhost:5984/mydb'
})
store.sync() // will sync with http://localhost:5984/mydb

Example with dynamic remote URL and ajax headers

var loadAccount = require('./load-account')
var store = new Store('mydb', {
  PouchDB: PouchDB,
  get remote () {
    return loadAccount.then(function (account) {
      return new PouchDB('http://localhost:5984/' + encodeURIComponent('user/' + account.id), {
        ajax: {
          headers: {
            authorization: 'session ' + account.session.id
          }
        }
      })
    })
  }
})
store.sync() // will sync with http://localhost:5984/mydb

store.add(properties)

store.add(properties)
ArgumentTypeDescriptionRequired
propertiesObjectproperties of documentYes
properties.idStringIf set, the document will be stored at given idNo

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


NameDescription
Error...

Example

store.add({foo: 'bar'}).then(function (doc) {
  alert(doc.foo) // bar
}).catch(function (error) {
  alert(error)
})

store.add(arrayOfProperties)

ArgumentTypeDescriptionRequired
arrayOfPropertiesArrayArray 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


NameDescription
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)
})

store.find(id)

ArgumentTypeDescriptionRequired
idStringUnique id of documentYes

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


NameDescription
Error...

Example

store.find('12345678-1234-1234-1234-123456789ABC').then(function (doc) {
  alert(doc.id)
}).catch(function (error) {
  alert(error)
})

store.find(doc)

ArgumentTypeDescriptionRequired
docObjectdocument with id propertyYes

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


NameDescription
Error...

Example

store.find(doc).then(function (doc) {
  alert(doc.id)
}).catch(function (error) {
  alert(error)
})

store.find(idsOrDocs)

ArgumentTypeDescriptionRequired
idsOrDocsArrayArray of id (String) or doc (Object) itemsYes

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


NameDescription
Error...

Example

store.find(doc).then(function (doc) {
  alert(doc.id)
}).catch(function (error) {
  alert(error)
})

store.findOrAdd(id, doc)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.findOrAdd(doc)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.findOrAdd(idsOrDocs)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.findAll()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.update(id, changedProperties)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.update(id, updateFunction)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.update(doc)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.update(arrayOfDocs)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.updateOrAdd(id, doc)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.updateOrAdd(doc)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.updateOrAdd(arrayOfDocs)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.updateAll(changedProperties)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.updateAll(updateFunction)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.remove(id)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.remove(doc)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.remove(idsOrDocs)


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.removeAll()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.pull()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.push()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.sync()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.connect()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.disconnect()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Resolves with :

{

}

Rejects with:

NameDescription
Error...

Example

store.isConnected()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Returns true / false

Example

store.on()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Returns store API.

Example

store.one()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Returns store API.

Example

store.off()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Returns store API.

Example

store.withIdPrefix()


šŸ• Complete README: #102


ArgumentTypeDescriptionRequired

Returns subset of store API with _id property implicitly prefixed by passed string

Example

Events


šŸ• Complete README: #102


EventDescriptionArguments

Testing

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

Contributing

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.

License

Apache 2.0

8.3.0

6 years ago

8.2.0

7 years ago

8.1.4

7 years ago

8.1.3

7 years ago

8.1.2

7 years ago

8.1.1

7 years ago

8.1.0

7 years ago

8.0.1

7 years ago

8.0.0

7 years ago

7.0.1

7 years ago

7.0.0

7 years ago

6.0.2

7 years ago

6.0.1

7 years ago

6.0.0

7 years ago

5.0.12

8 years ago

5.0.11

8 years ago

5.0.10

8 years ago

5.0.9

8 years ago

5.0.8

8 years ago

5.0.7

8 years ago

5.0.6

8 years ago

5.0.5

8 years ago

5.0.4

8 years ago

5.0.3

8 years ago

5.0.2

8 years ago

5.0.1

8 years ago

5.0.0

8 years ago

4.1.0

8 years ago

1.0.0

8 years ago