PRE-REQUISITES

For the following code examples to work, you will need to add Fliplet’s Data Sources API to your app. To add it follow the steps referenced here.

Saving new data to the data source

The example code below connects to a data source with an ID of ‘18798’. Once it has connected, it then inserts a new row and adds the value “Bill” to the column titled ‘name’. If the column doesn’t already exist it will be created automatically to ensure the data is saved.

Fliplet.DataSources.connect(18798)
  .then(function (connection) {
    return connection.append([
      {
        name: 'Nick',
        email: 'nick@example.org'
      }
    ]);
  })
  .then(function (results) {
    // Will return the added entries data
  });

You can also save multiple entries in one go:

Fliplet.DataSources.connect(18798)
  .then(function (connection) {
    return connection.append([
      {
        name: 'Nick',
        email: 'nick@example.org'
      },
      {
        name: 'Ian',
        email: 'ian@example.org'
      }
    ]);
  })
  .then(function (results) {
    // Will return the added entries data
  });

Overwriting existing data in the data source

If you are overwriting existing data, you can find an existing row entry (aka a record) then change one of the fields in it. In the example below we are finding a record where the name is “John” and changing the email to be “johnsmith@example.org”.

var dataSourceConnection;

Fliplet.DataSources.connect(265)
  .then(function (connection) {
    dataSourceConnection = connection;

    return connection.findOne({
      where: { name: 'John' }
    });
  })
  .then(function (user) {
    if (!user) {
      return Promise.reject('User not found');
    }

    // Update email
    user.data.email = 'johnsmith@example.org';

    return dataSourceConnection.update(user.id, user.data);
  });
Was this article helpful?
YesNo