How to quickly add cloud synchronization to a project

Today, I will take a beautiful open source billing project as an example to give you an idea of the simplest way to turn a local storage project into a cloud synchronous project.

This App is called “out”, address at https://github.com/Livinglist/Churu. Why did you choose this project? Since the project itself uses local SQLite for data storage, it is easy to convert it to an online PostGRE database with cloud synchronization.

Projects that don’t use SQLite can also be remodeled, just by rewriting the storage logic. If a project has features you particularly like and need, it’s worth the time.

Without further ado, let’s look at the project.

We found the db_provider.dart file in the project, and the next main task was to transform the local database functionality.

First, we introduce the PostGRE driver for DART and initialize the database.

import 'package:postgres/postgres.dart'; . connection = PostgreSQLConnection(dbIp, dbPort, dbName, username: dbAccount, password: dbPasswd);awaitconnection.open(); .Copy the code

Here you need to prepare a public IP database, recommend a convenient cloud database, one key to create, one key query, very convenient, and is free oh. Go to MemFireDB and sign up for an account.

Send a wave of benefits, share a few invitation code: JdpFEu, UTNKdm;

The next step is to create the database in postGRE mode.

CREATE TABLE $tableName ("" "$colId SERIAL PRIMARY KEY,
                  $colUuid TEXT,
                  $colTimestamp BIGINT,
                  $colDescription TEXT,
                  $colType INTEGER,
                  $colAmountREAL); "" ");
Copy the code

Once that was done, we moved the database to the cloud. Next, change all of its business logic for manipulating the database to postGRE mode.

You can see a couple of these methods in the source code.

updateTransaction
deleteTransaction
addTransaction
getAllTransactions
Copy the code

If we look at one of the ways to add transactions, dart’s SQLite library encapsulates SQL statements so they can be inserted directly into a map, making it easier to use.

Future<int> addTransaction(Transaction transaction) async{...return db.insert('Transactions', transaction.toMap());
}
Copy the code

But the PostGRE library doesn’t provide that, and you need to do a little wrapping yourself. Here is the method I encapsulated to facilitate inserting and updating data.

static String getInsertSql(String table, Map<String.dynamic> values,
    {List<String> ignores}) {
  if(ignores ! =null && ignores.length > 0) {
    ignores = ignores.map((e) => e.toLowerCase()).toList();
  }

  final insert = StringBuffer(a); insert.write('INSERT');
  insert.write(' INTO ');
  insert.write(_escapeName(table));
  insert.write('(');

  finalsize = (values ! =null)? values.length :0;

  if (size > 0) {
    final sbValues = StringBuffer(') VALUES (');

    var i = 0;
    values.forEach((String colName, dynamic value) {
      if (ignores == null| |! ignores.contains(colName.toLowerCase())) {if (i++ > 0) {
          insert.write(', ');
          sbValues.write(', ');
        }

        /// This should be just a column nameinsert.write(_escapeName(colName)); sbValues.write(PostgreSQLFormat.id(colName)); }}); insert.write(sbValues); } insert.write(') ');

  var sql = insert.toString();
  return sql;
}

Copy the code

There are other encapsulation, which is written in the pg_helper.dart file.

Here our method for adding transactions becomes.

Future<int> addTransaction(Transaction transaction) async{...var sql = PgHelper.getInsertSql(tableName, transaction.toMap(), ignores: [colId]);
  var res = await db.execute(sql, substitutionValues: transaction.toMap());
  return res;
}

Copy the code

All the other methods have to be modified as well, and when they are all modified, they are finished.

If your application needs to be available to others, or if you want to be credited with your object (if you have one), write a configuration database interface that makes it easy to configure the database dynamically.

I wrote one here for reference.

Here, you can happily with your object bookkeeping! [head]

The App is still pretty good, but now with cloud syncing, it’s even more powerful.

Here is just to provide you with a train of thought, more play everyone play their imagination.

Complete code (github.com/aliyoge/Chu…)