You are viewing Skygear v1 Documentation.Switch to Skygear v0 Documentation

Basic CRUD

Before working with the Skygear records, we suggest you reading the basics.

Creating records

Creating a record

You can save a public record to server as the following: Note({
  'content': 'Hello World!'
})).then((record) => {
}, (error) => {

Here we created a new record in 'Note' and under the 'content' column, we added the record value: 'Hello World!'. Your data browser should look similar to the following, but with the 'note' record type instead of the 'test' record type. Web database viewer

Creating multiple records

You can also batch save multiple records at one time.[goodNote1, goodNote2, badNote3, goodNote4, badNote5])
.then((result) => {
  // [goodNote1, goodNote2, undefined, goodNote4, undefined]
  // [undefined, undefined, error3, undefined, error5]
}, (error) => { /* request error */ })


By default, "good" records are still saved while "bad" records are not. Use the atomic option if you don't want partial saves, so either all or none of the records will be saved.[goodNote, badNote], { atomic: true });
// neither of the notes are saved

Updating records

const query = new skygear.Query(Note);
query.equalTo('_id', '<your-note-_id>');

.then((records) => {
  const note = records[0];
  note['content'] = 'Hello New World';
}).then((record) => {
  console.log('update success');
}, (error) => {
  • After saving a record, any attributes modified from the server side will be updated on the saved record object in place.
  • The local transient fields of the records are merged with any remote transient fields applied on the server side.
  • There is a shorter way for updating records, but only use it when you know what you are doing. (Unspecified field or column will not be changed, so in the case below only content field will be changed) Note({
  _id: 'note/<your-note-_id>',
  content: 'Hello New World'

Querying records

You can construct a Query object by providing a Record Type. You can config the query by mutating its state. Read the section More About Queries to learn more.

const query = new skygear.Query(Blog);
query.greaterThan('popular', 10);
query.limit = 10;

skygear.publicDB.query(query).then((records) => {
}, (error) => {

Deleting records

Deleting a record

  id: 'note/<your-note-_id>'
}).then((record) => {
}, (error) => {

Deleting multiple records

You can also delete multiple records at one time.

const query = new skygear.Query(Note);
query.lessThan('rating', 3);

let foundNotes = [];
.then((notes) => {
  console.log(`Found ${notes.length} notes, going to delete them.`);
  foundNotes = notes;
  return skygear.publicDB.delete(notes); // return a Promise object
.then((errors) => {
  errors.forEach((perError, idx) => {
    if (perError) {
      console.error('Fail to delete', foundNotes[idx]);
}, (reqError) => {
  console.error('Request error', reqError);