libsql_dart

Last updated:

0 purchases

libsql_dart Image
libsql_dart Images
Add to Cart

Description:

libsql dart

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:
copied to clipboard
Create the client #

In memory

final client = LibsqlClient(":memory:");
copied to clipboard

Local

final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';
final client = LibsqlClient(path);
copied to clipboard

Remote

final client = LibsqlClient('<TURSO_OR_LIBSQL_URL>')..authToken = '<TOKEN>';
copied to clipboard

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;
copied to clipboard
Connect #
await client.connect();
copied to clipboard
Call sync if necessary when using embedded replica #
await client.sync();
copied to clipboard
Run SQL statements #

Create table

await client.execute("create table if not exists customers (id integer primary key, name text);");
copied to clipboard

Insert query

await client.query("insert into customers(name) values ('John Doe')");
copied to clipboard

Select query

print(await client.query("select * from customers"));
copied to clipboard

Batch transaction

await client.batch("""insert into customers (name) values ('Jane Doe'); insert into customers (name) values ('Jake Doe');""");
copied to clipboard

Prepared statement

final statement = await client
.prepare("select * from customers where id = ?");
await statement.query(positional: [1])
copied to clipboard

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();
copied to clipboard

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);
copied to clipboard
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 #

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.