Developer Docs

JavaScript Web Guide

Saving Data

Saving Data

The basic Flybase write operation is set() which saves new data to the specified Flybase collection. To understand set(), we'll build a simple blogging app. The data for our app will be stored at this Flybase reference:


	var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "blog");
			

Let's start by saving some user data to our Flybase app. We'll store each user in Flybase with a unique username, and we'll also store their full name and date of birth in Flybase.

First, we'll create a reference object to our user data. Then we'll use set() to save a user object to Flybase with the user's username, full name, and birthday. We can pass set() a string, number, boolean, array or any JSON object. In this case we'll pass it an object:


	var usersRef = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "users");
	usersRef.set({
		username: "baileys",
		date_of_birth: "June 9, 1978",
		full_name: "Bailey Stringer"
	});
			

You can also use insertDocument or push() in place of set(). These methods are aliased to each other and work the same

When a JSON object is saved to Flybase, the object properties are automatically added to the collection you specified, in this case users. Now if we navigate to our web app in our dashboard, we'll see the value "Bailey Stringer" in the users collection.

The above example will result in the same data being saved to your Flybase app:


	{
		"users": {
			"_id": "uniquedocumentid",
			"username": "baileys",
			"date_of_birth": "July 4, 2004",
			"full_name": "Bailey Stringer"
		}
	}
			

Every record will have a unique _id field, which we use later when we push updates.

Optional callback

You can also attach callbacks to your insert or update, this will show you the status.


	var usersRef = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "users");
	usersRef.set({
		username: "baileys",
		date_of_birth: "June 9, 1978",
		full_name: "Bailey Stringer"
	}, function(data){
		var user = data.value();
		console.log( "New user was saved as " + user._id );
	});
				

New records will return a _id variable inside the .value() method. Updates will return the updated document.

Updating Existing Data

If you want to update documents inside a Flybase collection, then you can use the update() method as shown below:


	var usersRef = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "users");
	usersRef.where( {username: "baileys"} ).on('value', function (response) {
		response.forEach( function( data ){
			var user = data.value();
	
			user.nickname = "Dog";
	
			//	update the user record...
			usersRef.update(user._id,user, function(resp) {
				console.log( "User updated" );
			});
		});
	});
			

In this example, we performed a query to return Bailey's user record, then we updated his data to include his nickname.

If you use set() or push() to save data and the object you are saving contains an _id record, then it will automatically update the document in question rather than saving a new record.

Server Values

Flybase lets you specify variables that are set on the server end. This is handy for dealing with cases such as users in multiple timezones and you want to store a timestamp locally to the Flybase servers.

Server Value Definition
Flybase.ServerValue.TIMESTAMP A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) by the Flybase servers.
Flybase.ServerValue.UTC A placeholder value for auto-populating the current UTC date () by the Flybase servers.
Flybase.ServerValue.UNIQUE A placeholder value for auto-populating the field with a unique id.
Flybase.ServerValue.UUID A placeholder value for auto-populating the field with a unique UUID field.

	var usersRef = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "users");
	usersRef.push({
		username: "baileys",
		date_of_birth: "July 4, 2004",
		full_name: "Bailey Stringer"
		registered_date: "Flybase.ServerValue.TIMESTAMP"
	});
			

In the next section on Reading Data, we'll learn how to read this data from Flybase.