Support Topics Documentation Slack YouTube Blog

Save new Object Model in Table


(jempis) #1

Hi all,
sorry for my english, i’m new user.

I have a simple question, i see the “quick starter guides”, but i don’t understand how to save a new object in the table.
I have, for example, this table:
Rooms
id - integer
description - string
numberRoom - string

and the relative model

/**

  • @property {Integer} id
  • @property {String} description
  • @property {String} numberRoom
    */
    class Room extends Backendless.ServerCode.PersistenceItem {

}

module.exports = Backendless.ServerCode.addType(Room);

Then i want to create an API in the Service for add new Room.

Can you help me please?
Thank
GF


(Mark Piller) #2

Hi GF,

If the table name is Rooms, then the class should be called Rooms as well.

To save an object in the table is as simple as this:

var rooms = new Rooms();
rooms.id = “foo”;
rooms.description = “bar”;
rooms.numberRoom = “1234”;
Backendless.Data.of( Rooms ).save( rooms );

The documentation is right here: https://backendless.com/docs/js/doc.html#data_saving_data_objects

Mark


(jempis) #3

Hi Mark,

thank’s for your response.
Then, for example, the correct service with an API for add new room is the seguent:

const Rooms = require(’…/models/rooms’);

class MyService {

/**

  • @param {Rooms} rooms
  • @returns {Promise.<void>}
    */
    addRoom(rooms) {
    return Backendless.Data.of( Rooms ).save( rooms );
    }
    }

Backendless.ServerCode.addService( AladorService );

is correct?


(Sergey Chupov) #4

Yes, it seems to be correct in case you pass a valid json into the method.