Online database and Web application builder

Create and manage responsive website applications in an online database environment 

5.2 Objects (CRUD)

Data is stored and built around the Simbla.object  that contains key-value pairs of JSON-compatible data. This data is schemaless, so just set any key-value pairs you want and we will store it.
Keys have to be strings. Values can be strings, numbers, Booleans or arrays.

In our example, we will track the stock of cars in a showroom using our "Cars" table as given in the javascript page

Before you get started, create a table with a few columns and manually add a raw to test that the table is working as you want it to.

Make sure all of your permissions of the table are set to public before you start.


A Simbla.object  could contain:

car_model: “Ferarri” , available: true , stock: 2
Before you start building your web application, you will need to create a subclass and use the Simbla.Object.extended   method.
 Any Simbla.query  will react with any Simbla.object  with the same classname :


    // Syntax to create a new subclass of Simbla.object.

var Cars = Simbla.Object.extend("Cars");

    // Create a new instance of the class

var car = new Cars();    

You can also use the new Simbla.Object(className)   to create a new single Simbla object within the class.

var car = new Simbla.Object("Cars");

var query = new Simbla.Query(car);

Create new record in table

In order to create a record and save the data to our table, we will use the next format and set our attributes with the set   command.

var Cars = Simbla.Object.extend("Cars");
var car = new Cars();
car.set("car_year", 2014);
car.set("car_model", "Test car");
car.set("car_code", 12468);
car.set("car_color", "red");
car.set("available", true);
car.set("stock", 5);
car.save(null, {
      success: function(car) {
            alert('New object created with objectId: ' + car.id);
       },
       error: function(car, error) {
             alert('Failed to create new object, with error code: ' + error.message);
       }
});

Here is another example doing the same action. Here we will set our attributes under the  save   command.

var Cars = Simbla.Object.extend("Cars");
var car = new Cars();
car.save({
  car_code: 12468,
  car_model: "Test car",
  car_year: 2014,
  car_color: "red",
  available: true,
  stock: 5,
   }, {
        success: function(car) {
     // Object saved successfully.
       alert('New object created with objectId: ' + car.id);
    },
 
       error: function(car, error) {
        // Object saving failed.
       // error is a Simbla.Error with an error code and message.
          alert('Failed to create new object, with error code: ' + error.message);
      }
 });

After publishing and running the code, you can go to your table in your database and see how a new record has been added with the data you entered. Please make sure you hit the refresh button above your table.

Here is how our table looks after running the code:

As you can see, there are three automatic fields provided by the the system for your convenience and for future use:

1. objectId - a unique string identifying each saved record.

2. createdAt - the exact time and date the record was created.

3. updatedAt - the exact time and date of the last modification made.

Retrieving An Object

After saving the object we would like to call it back and present it on our page. To do so we will use the  objectId  of the record which is in our example "uD3KzzDd2b".  Keep in mind that all  objectId are unique values, so if you are following the steps please copy your objectId from your table.

In order to retrieve our object we are going to use the Simbla.query   that we will explain more about in our next guide.


We wish to present our final result on our page using our HTML element. Here is our HTML code:

<div Style="padding:30px;border:1px solid red">
   <H1 Id="cmodel"></H1></br>
    <h3 id="cyear"></h3></br></br>
    <p id="ccode"></p></br></br>
</div>

Our Javascript code retrieving the record:


var car = Simbla.Object.extend("Cars");
var query = new Simbla.Query(car);
query.get("qxFYhsu14o", {
   success: function(car) {

$("#cmodel").html(car.get("car_model"));
$("#cyear").html(car.get("car_year"));
$("#ccode").html(car.get("car_code"));
     
    },
     error: function(car, error) {

   alert("Error - The object was not retrieved successfully" + error.message);
   
   
  }
});

To retrieve values from Simbla.object , use the get method:

var cyear= car.get("car_year");
var ccode= car.get("car_code");
var ccolor= car.get("car_color");
var cmodel= car.get("car_modle");

The three reserved values are provided as properties and cannot be retrieved using the 'get' method nor modified with the 'set' method:

var objectId = car.id;
var updatedAt = car.updatedAt;
var createdAt = car.createdAt;

Updating Objects

Updating an object is actually saving over the old object, in the next example we are going to use our "WY0DQyj9L6 " record, remember that each record is unique so please check your record before testing it out.

We are basically getting the "WY0DQyj9L6" record, setting new info and saving it:

var car = Simbla.Object.extend("Cars");
var query = new Simbla.Query(car);
query.get("WY0DQyj9L6", {

    success: function(car) {
      car.set("car_year", 2016);
      car.set("car_model", "new car");

        car.save(null, {
            success: function(car) {
                 alert("Update succeeded") ;
            },
            error: function(car, error) {
                 alert("Error - update failed" + error.message);
            }
        });
   },
   error: function(car, error) {
      alert("Error - The object was not retrieved successfully" + error.message);
   }
});

Deleting Objects

To delete an object we will use the object.destroy method:

var car = Simbla.Object.extend("Cars");
var query = new Simbla.Query(car);
query.get("WY0DQyj9L6 ", {
    success: function(car) {
      car.destroy({
          success: function(car) {
            alert("Object deleted");
          },
          error: function(car, error) {
            alert("Object not deleted");
          }
      });
    },
    error: function(car, error) {
       alert("Object not found");
    }
});

About Simbla

Trying to create a website for their own business, Simbla's founders recognized, over the years, a lack of excellent, simple tools available on the market. Therefore, in 2013 Simbla was founded, for the purpose of providing a smart and easy website building solution for all types of businesses. Simbla's founders have more than 15 years of experience with web-based platforms and website building. We are helping SMB's to create a web presence and to evolve easily into a highly competitive market.

In our vision Simbla creates a new environment where websites are meant for everybody!

Social

all rights reserved to simbla inc

2016