DSFirebaseAdapter
firebase adapter for js-data
See the Demos:
Quick Start
npm install --save js-data js-data-firebase
or bower install --save js-data js-data-firebase
.
Load firebase.js
.
Load js-data-firebase.js
after js-data.js
.
var adapter = new DSFirebaseAdapter({
basePath: 'https://my-app.firebase.io'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
// "store" will now use the firebase adapter for all async operations
Community & Links
- Slack Channel - Ask your questions!
- Mailing List - Ask your questions!
- Issues - Found a bug? Feature request? Submit an issue!
- GitHub - View the source code for js-data.
- Contributing Guide
- Changelog
API
new DSFirebaseAdapter(options)
Need help?
Want more examples or have a question? Post on the Slack channel or the mailing list then we'll get your question answered and probably update this wiki.
Default Settings
When you create a new instance of DSFirebaseAdapter
it will come with some defaults. Here they are:
basePath
Type: string
Base path. You should set this to your firebase url.
Default: ""
create(resourceConfig, attrs[, options])
Creates a new item in Firebase.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The Resource definition created by DS#defineResource . |
attrs | object | The attributes from which to create the item. |
options | object | Optional configuration. |
options.basePath | string | Override the basePath. |
options.endpoint | string | Override the endpoint. |
Examples
var adapter = new DSFirebaseAdapter({
basePath: 'https://my-firebase.firebaseio.com'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
var Document = store.defineResource('document');
// bypass the data store
adapter.create(Document, { author: 'John' }).then(function (document) {
document; // { id: 5, author: 'John' }
// The new document has NOT been injected into the data store because we bypassed the data store
Document.get(document.id); // undefined
});
// Normally you would just go through the data store
Document.create({ author: 'John' }).then(function (document) {
document; // { id: 5, author: 'John' }
// the new document has been injected into the data store
Document.get(document.id); // { id: 5, author: 'John' }
});
destroy(resourceConfig, id[, options])
Delete an item from Firebase.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The resource definition created by DS#defineResource . |
id | string or number | The primary key of the item to destroy. |
options | object | Optional configuration. |
options.basePath | string | Override the basePath. |
options.endpoint | string | Override the endpoint. |
Examples
var adapter = new DSFirebaseAdapter({
basePath: 'https://my-firebase.firebaseio.com'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
var Document = store.defineResource('document');
Document.inject({ id: 5, author: 'John' });
// bypass the data store
adapter.destroy(Document, 5).then(function () {
// the document is still in the data store because we bypassed the data store
Document.get(document.id); // { id: 5, author: 'John' }
});
// Normally you would just go through the data store
Document.destroy(5).then(function () {
// the document has been ejected from the data store
Document.get(document.id); // undefined
});
destroyAll(resourceConfig, params[, options])
Delete a collection of items from Firebase.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The resource definition created by DS#defineResource . |
params | object | Query parameters for selecting the items to destroy. See Query Syntax. Default: {} . |
options | object | Optional configuration. |
options.basePath | string | Override the basePath. |
options.endpoint | string | Override the endpoint. |
Examples
var adapter = new DSFirebaseAdapter({
basePath: 'https://my-firebase.firebaseio.com'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
var Document = store.defineResource('document');
var params = {
author: 'John'
};
// bypass the data store
adapter.destroyAll(Document, params).then(function () {
// the documents have NOT been ejected from the data store because we bypassed the data store
Document.filter(params); // [{...}, {...}, ...]
});
// normally you would go through the data store
Document.findAll(params).then(function () {
// the documents have been ejected from the data store
Document.filter(params); // []
});
find(resourceConfig, id[, options])
Get an item from Firebase.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The resource definition created by DS#defineResource . |
id | string or number | The primary key of the item to retrieve. |
options | object | Optional configuration. |
options.basePath | string | Override the basePath. |
options.endpoint | string | Override the endpoint. |
options.with | array of strings | Array of relation names or relation field names to eager load with the retrieved item. Supports nested relations, e.g. adapter.find(Post, 1, { with: ['user.organization'] }) |
Examples
var adapter = new DSFirebaseAdapter({
basePath: 'https://my-firebase.firebaseio.com'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
var Document = store.defineResource({
name: 'document',
relations: {
belongsTo: {
user: {
localKey: 'userId',
localField: 'user'
}
}
}
});
var User = store.defineResource({
name: 'user',
relations: {
hasMany: {
document: {
foreignKey: 'userId',
localField: 'documents'
}
},
belongsTo: {
organization: {
localKey: 'organizationId',
localField: 'organization'
}
}
}
});
var Organization = store.defineResource({
name: 'organization',
relations: {
hasMany: {
user: {
foreignKey: 'organizationId',
localField: 'users'
}
}
}
});
// bypass the data store
adapter.find(Document, 5, { with: ['user'] }).then(function (document) {
document; // { id: 5, content: 'foo', userId: 1, user: {...} }
document.user; // { id: 1, name: 'John Anderson' }
});
// Normally you would just go through the data store
Document.find(5, { with: ['user', 'user.organization'] }).then(function (document) {
document; // { id: 5, content: 'foo', userId: 1, user: {...} }
document.user; // { id: 1, name: 'John Anderson', organizationId: 10 }
document.user.organization; // { id: 10, name: 'Company' }
});
findAll(resourceConfig, params[, options])
Get a collection of items from Firebase.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The resource definition created by DS#defineResource . |
params | object | Query parameters for selecting the items to retrieve. See Query Syntax. Default: {} . |
options | object | Optional configuration. |
options.basePath | string | Override the basePath. |
options.endpoint | string | Override the endpoint. |
options.with | array of strings | Array of relation names or relation field names to eager load with the retrieved items. Supports nested relations, e.g. adapter.findAll(Post, null, { with: ['user.organization'] }) |
Examples
var adapter = new DSFirebaseAdapter({
basePath: 'https://my-firebase.firebaseio.com'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
var Document = store.defineResource({
name: 'document',
relations: {
belongsTo: {
user: {
localKey: 'userId',
localField: 'user'
}
}
}
});
var User = store.defineResource({
name: 'user',
relations: {
hasMany: {
document: {
foreignKey: 'userId',
localField: 'documents'
}
},
belongsTo: {
organization: {
localKey: 'organizationId',
localField: 'organization'
}
}
}
});
var Organization = store.defineResource({
name: 'organization',
relations: {
hasMany: {
user: {
foreignKey: 'organizationId',
localField: 'users'
}
}
}
});
var params = {
size: {
'>': 15000000
}
};
// bypass the data store
adapter.findAll(Document, params).then(function (documents) {
documents[0].size; 15063940
});
// normally you would go through the data store
Document.findAll(params).then(function (documents) {
documents[0].size; 94839687
});
adapter.findAll(Document, params, { with: ['user', 'user.organization'] }).then(function (documents) {
documents[0].size;
documents[0].user; // {...}
documents[0].user.organization; // {...}
});
update(resourceConfig, id, attrs[, options])
Update an item in Firebase.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The resource definition created by DS#defineResource . |
id | string or number | The primary key of the item to retrieve. |
attrs | object | The attributes with which to update the item. |
options | object | Optional configuration. |
options.basePath | string | Override the basePath. |
options.endpoint | string | Override the endpoint. |
Examples
var adapter = new DSFirebaseAdapter({
basePath: 'https://my-firebase.firebaseio.com'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
var Document = store.defineResource('document');
Document.inject({ id: 5, author: 'John' });
// bypass the data store
adapter.update(Document, 5, { author: 'Johnny' }).then(function (document) {
document; // { id: 5, author: 'Johnny' }
// The updated document has NOT been injected into the data store because we bypassed the data store
Document.get(document.id); // { id: 5, author: 'John' }
});
// Normally you would just go through the data store
Document.update(5, { author: 'Johnny' }).then(function (document) {
document; // { id: 5, author: 'Johnny' }
// the updated document has been injected into the data store
Document.get(document.id); // { id: 5, author: 'Johnny' }
});
updateAll(resourceConfig, attrs, params[, options])
Update a collection of items in Firebase.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The resource definition created by DS#defineResource . |
attrs | object | The attributes with which to update the items. |
params | object | Query parameters for selecting the items to update. See Query Syntax. Default: {} . |
options | object | Optional configuration. |
options.basePath | string | Override the basePath. |
options.endpoint | string | Override the endpoint. |
Examples
var adapter = new DSLocalStorageAdapter({
basePath: 'https://my-firebase.firebaseio.com'
});
var store = new JSData.DS();
store.registerAdapter('firebase', adapter, { default: true });
var Document = store.defineResource('document');
Document.inject({ id: 5, author: 'John' });
Document.inject({ id: 6, author: 'John' });
// bypass the data store
adapter.updateAll(Document, { author: 'Johnny' }, { author: 'John' }).then(function (documents) {
documents[0]; // { id: 5, author: 'Johnny' }
// The updated documents have NOT been injected into the data store because we bypassed the data store
Document.filter({ author: 'John' }); // [{...}, {...}]
Document.filter({ author: 'Johnny' }); // []
});
// Normally you would just go through the data store
Document.updateAll({ author: 'Johnny' }, { author: 'John' }).then(function (documents) {
documents[0]; // { id: 5, author: 'Johnny' }
// the updated documents have been injected into the data store
Document.filter({ author: 'John' }); // []
Document.filter({ author: 'Johnny' }); // [{...}, {...}]
});
License
The MIT License (MIT)
Copyright (c) 2014-2015 Jason Dobry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Updated less than a minute ago