libsql_dart 0.4.6 copy "libsql_dart: ^0.4.6" to clipboard
libsql_dart: ^0.4.6 copied to clipboard

LibSQL Dart client library to interact with LibSQL/Turso database instance.

libsql_dart #

LibSQL Dart client library to interact with LibSQL/Turso database instance.

Supported Features #

  • Local, Remote, and Embedded replica
  • Running execute and query SQL statement with named or positional params

Getting Started #

Add it to your pubspec.yaml. #

libsql_dart:

Create the client #

  • In memory
final client = LibsqlClient(":memory:");
  • Local
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';
final client = LibsqlClient(path);
  • Remote
final client = LibsqlClient('<TURSO_OR_LIBSQL_URL>')..authToken = '<TOKEN>';
  • Embedded replica
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';
final client = LibsqlClient(path)
	..authToken = '<TOKEN>'
	..syncUrl = '<TURSO_OR_LIBSQL_URL>'
	..syncIntervalSeconds = 5
	..readYourWrites = true;

Connect #

await client.connect();

Call sync if necessary when using embedded replica #

await client.sync();

Run SQL statements #

  • Create table
await client.execute("create table if not exists customers (id integer primary key, name text);");
  • Insert query
await client.query("insert into customers(name) values ('John Doe')");
  • Select query
print(await client.query("select * from customers"));
  • Batch transaction
await client.batch("""insert into customers (name) values ('Jane Doe'); insert into customers (name) values ('Jake Doe');""");
  • Prepared statement
final statement = await client
	.prepare("select * from customers where id = ?");
await statement.query(positional: [1])
  • Transaction
final tx = await client.transaction();
await tx
	.execute("update customers set name = 'John Noe' where id = 1");
await tx
	.execute("update customers set name = 'Jane Noe' where id = 2");
print(await tx
	.query("select * from customers where id = ?", positional: [1]));
await tx.commit();
  • Read the locally replicated db using sqflite when using embedded replica
final db = await openDatabase(path, readOnly: true);
final result = await db.rawQuery('select * from customers');
print(result);

Note Code snippets above also use path_provider and sqflite packages. When using other sqlite libraries to read the file, you need to make sure that it is done in read only mode, because the replication process assumes exclusive write lock over the file.

Demo #

Demo