Dart: Database
Run SQL with bound parameters and select, insert, update, delete and call database functions with the Dart and Flutter SDK.
Run SQL with bound parameters and select, insert, update, delete and call database functions. Every call runs as the client's signed-in user, so your project's policies and row-level security apply. Types and members are listed in source order; each one links to its HTTP endpoint where it maps to one.
#Types and members
SovariaDatabase
The project's Postgres database. Every value is a bound parameter ($1, $2, …), so user input never becomes SQL; table and column names are checked and quoted. Row security and access policies apply to app users exactly as on the web.
class SovariaDatabaseSovariaDatabase.SovariaDatabase
Create an instance.
SovariaDatabase(this._client)SovariaDatabase.sql
Run one statement: sql('SELECT * FROM todos WHERE owner = $1', userId).
Future<SqlResult> sql(String statement, [List<Object?> params = const []])SovariaDatabase.sqlReadOnly
Like sql, in a read-only transaction: the server refuses any write, including one made through a function the query calls.
Future<SqlResult> sqlReadOnly(String statement, [List<Object?> params = const []])SovariaDatabase.rpc
Call one of your Postgres functions: with the arguments by name. A function that returns one value resolves to that value; one that returns rows resolves to a list of maps.
Future<dynamic> rpc(String name,
{Map<String, Object?> args = const {}, bool get = false, Map<String, Object?>? query})App users need an access policy that allows fn:<name>.
get calls with GET and the arguments in the query string (functions that only read); query adds PostgREST parameters for functions that return rows (select, order, limit, filters like {'status': 'eq.open'}).
final n = await sovaria.db.rpc('post_count', args: {'u': userId});SovariaDatabase.select
SELECT with equality filters: select('todos', where: {'done': false}, orderBy: 'created_at', limit: 20).
Future<List<Map<String, Object?>>> select(String table,
{List<String> columns = const [""], Map<String, Object?> where = const {}, String? orderBy,
bool descending = false, int? limit, int? offset})SovariaDatabase.insert
INSERT … RETURNING *.
Future<List<Map<String, Object?>>> insert(String table, Map<String, Object?> values)SovariaDatabase.update
UPDATE … SET … WHERE … RETURNING * (a filter is required).
Future<List<Map<String, Object?>>> update(String table, Map<String, Object?> values, {required Map<String, Object?> where})SovariaDatabase.delete
DELETE … WHERE … RETURNING * (a filter is required).
Future<List<Map<String, Object?>>> delete(String table, {required Map<String, Object?> where})SovariaDatabase.quote
A safe identifier (schema.table allowed), double-quoted.
static String quote(String name)SqlResult
class SqlResultSqlResult.SqlResult
Create an instance.
SqlResult(this.rows, this.raw)SqlResult.rows
A property.
final List<Map<String, Object?>> rowsSqlResult.raw
A property.
final Map<String, Object?> rawSqlResult.truncated
True when the server stopped reading at the plan's sql.max_rows: page with LIMIT/OFFSET.
bool get truncatedSqlResult.SqlResult.fromJson
Create an instance.
factory SqlResult.fromJson(Object? json)