Today, a beautiful open source notebook/diary will give you an idea of the simplest way to turn a local storage project into a cloud-synced project.

This App original address at https://github.com/bimsina/notes-app. Why did you choose this project? Because the project itself uses local SQLite for data storage, it’s easy to convert it to an online PostGRE database for cloud synchronization.

Projects that don’t use SQLite can also be modified, but the storage logic needs to be rewritten. If a project has features that you particularly like and need, it’s worth the time.

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

Dart file in the project. The next major task is to modify the local database functionality.

Since the project was using SQLite, we are going to change it to Postgres.

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 register an account to use it.

Then change the database creation mode to postGRE mode.

await connection.execute("""
        CREATE TABLE $noteTable (
                  $colId SERIAL PRIMARY KEY,
                  $colTitle TEXT,
                  $colDescription TEXT,
                  $colPriority INTEGER,
                  $colColor INTEGER,
                  $colDateTEXT); "" ");
Copy the code

Once that was done, we moved the database to the cloud. Next, change its business logic for all operations on the database to PostGRE mode.

You can see that there are several methods in the source code.

getNoteMapList
insertNote
updateNote
deleteNote
getCount
getNoteList
Copy the code

In one of the note-adding methods, dart’s SQLite library encapsulates SQL statements and allows you to insert a map directly, making it easier to use.

Future<int> insertNote(Note note) async {
  Database db = await this.database;
  var result = await db.insert(noteTable, note.toMap());
  return result;
}
Copy the code

But the PostGRE library doesn’t provide that, so you need to do a bit of wrapping yourself.

Here are my encapsulated methods for 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 a couple of other packages that you’ll see in your project, and you’ll see the full code address at the end.

So the way we add notes here becomes.

Future<int> insertNote(Note note) async{...var sql = getInsertSql(noteTable, note.toMap());
  var res = await db.execute(sql, substitutionValues: note.toMap());
  return res;
}
Copy the code

The same other methods are also to be transformed, the transformation of these methods are completed.

If your app needs to be made available to others, or you want to keep a journal with your partner (if you have one) 🐶.

So write a configuration database interface, convenient dynamic configuration database.

I wrote one here for your reference.

Here, you can write diary happily with your partner! 🐶

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

Here is just to give you an idea, more play you play your imagination.

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