Initializing a Database
To create a database, call firebase.initialize({projectName: 'myProjectName'})
.
<script>
firebase.initialize({
projectName: 'myProjectName'
});
</script>
This will create a new database you can save data in.
Getting a Reference
To interact with your database, you'll need to get a Reference
to it.
To get a reference to your database, call firebase.database.ref()
.
<script>
firebase.initialize({
projectName: 'myProjectName'
});
var reference = firebase.database().ref();
</script>
This will create a reference to the top level of your database.
To specify a sub-folder in your database, you can pass a path to ref()
.
<script>
firebase.initialize({
projectName: 'myProjectName'
});
var subReference = firebase.database().ref('folder1/folder2');
</script>
You can also use Reference.child()
to specify a sub-folder of an existing reference.
<script>
firebase.initialize({
projectName: 'myProjectName'
});
var reference = firebase.database().ref();
reference.child('folder1');
</script>
Setting Data in a Database
To set data in your database, use Referece.set(data, callback)
<script>
firebase.initialize({
projectName: 'myProjectName'
});
firebase.database().ref('folder1').set('hello from folder 1!');
firebase.database().ref('folder2').set('hello from folder 2!');
</script>
Getting Data from a Database
To get data in your database, use Referece.on('value', function(data){})
<script>
firebase.initialize({
projectName: 'myProjectName'
});
firebase.database().ref('folder1').on('value', function(data) {
console.log(data);
});
// => 'hello from folder 1!'
</script>
Pushing Data to a Database
To push data in your database, use Referece.push(data, callback)
.
Pushing data will add it to the reference without overwriting any existing data.
Every value you .push
will be given a unique hash.
<script>
firebase.initialize({
projectName: 'myProjectName'
});
var folder3 = firebase.database().ref('folder3');
folder3.push('hello from folder 3!', function() {
folder3.on('value', function(data) {
console.log(data);
// => {
// -M79DN0mwM7weeUc0TbA: "hello from folder 3!"
// }
});
});
</script>
Publishing and Sharing Your Project
If you want other users to be able to write data, you need to specify an ownerID
when initializing your project.
Your ownerID is:
<script>
firebase.initialize({
projectName: 'myProjectName',
ownerID: ''
});
</script>
Then, in order to let other users write to your database, .publish
your database.
<script>
firebase.initialize({
projectName: 'myProjectName',
ownerID: ''
});
firebase.database().publish();
</script>
Changelog
0.1.0
Introduce the publish operator for letting others use projects
.ref() returns a Reference which can be interacted with later, via .child() etc.
Introduces ownerIDs for specifying the user whose database you'd like to write to