persql 0.1.1
persql: ^0.1.1 copied to clipboard
PerSQL Dart and Flutter SDK. Query your edge SQLite databases, and sign a user in with OAuth + PKCE so each one gets their own private cloud database.
example/persql_example.dart
import 'package:persql/persql.dart';
/// Minimal end-to-end sketch of the `database`-scope flow. In a real Flutter
/// app, open `req.url` with `flutter_web_auth_2` and read the `code` + `state`
/// off the returned redirect URI.
Future<void> main() async {
const clientId = 'psqlrp_s00_yourclient';
const redirectUri = 'https://app.example.com/callback';
// 1. Begin sign-in. Open req.url; keep req.codeVerifier + req.state.
final req = PerSQL.beginConnect(
clientId: clientId,
redirectUri: redirectUri,
scope: 'openid database',
);
print('Open: ${req.url}');
// 2. After the redirect (verify state == req.state), exchange the code.
const codeFromRedirect = '...';
final persql = await PerSQL.completeConnect(
clientId: clientId,
redirectUri: redirectUri,
code: codeFromRedirect,
codeVerifier: req.codeVerifier,
);
// 3. Use the database provisioned in the user's own account.
final db = persql.database(persql.grant!.database);
await db.query(
'CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, body TEXT)',
);
await db.query('INSERT INTO notes (body) VALUES (?)', params: ['first note']);
final res = await db.query('SELECT id, body FROM notes');
for (final row in res.data) {
print('${row['id']}: ${row['body']}');
}
persql.close();
}