Skip to content

Commit 1f966a0

Browse files
committed
create example.
1 parent a78d81d commit 1f966a0

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
## 1.0.0
22

3-
- Initial version, created by Stagehand
3+
- Initial version.

example/lib/main.dart

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import 'package:logging_appenders/logging_appenders.dart';
12
import 'package:postgres/postgres.dart';
23
import 'package:postgres_utils/postgres_utils.dart';
4+
import 'package:uuid/uuid.dart';
5+
import 'package:uuid/uuid_util.dart';
6+
7+
import 'package:logging/logging.dart';
8+
9+
final _logger = Logger('main');
10+
11+
const Uuid _uuid = Uuid(options: <String, dynamic>{'grng': UuidUtil.cryptoRNG});
312

413
class DatabaseTransaction extends DatabaseTransactionBase<MyTables> {
514
DatabaseTransaction(PostgreSQLExecutionContext conn, MyTables tables)
@@ -26,6 +35,7 @@ class MyTables extends TablesBase {
2635
MyTables();
2736

2837
late final UserTable user = UserTable();
38+
2939
@override
3040
List<TableBase> get tables => [
3141
user,
@@ -35,7 +45,7 @@ class MyTables extends TablesBase {
3545
class UserTable extends TableBase {
3646
UserTable();
3747

38-
static const TABLE_USER = 'user';
48+
static const TABLE_USER = 'example_user';
3949

4050
@override
4151
List<String> get tables => [
@@ -47,6 +57,13 @@ class UserTable extends TableBase {
4757
CREATE TABLE $TABLE_USER (id uuid primary key, username varchar)
4858
''');
4959
}
60+
61+
Future<void> createUser(DatabaseTransactionBase db, String userName) async {
62+
await db.executeInsert(TABLE_USER, {
63+
'id': _uuid.v4(),
64+
'username': userName,
65+
});
66+
}
5067
}
5168

5269
class MyMigrationsProvider
@@ -62,3 +79,29 @@ class MyMigrationsProvider
6279
];
6380
}
6481
}
82+
83+
Future<void> main() async {
84+
PrintAppender.setupLogging();
85+
const dbName = 'example_tmp';
86+
final config = DatabaseConfig.fromEnvironment();
87+
await _createDb(dbName, config);
88+
89+
final access = DatabaseAccess(config: config.copyWith(databaseName: dbName));
90+
await access.prepareDatabase();
91+
await access.run((db) async {
92+
await db.tables.user.createUser(db, 'foo');
93+
_logger.info('Successfully created user.');
94+
});
95+
await access.dispose();
96+
}
97+
98+
Future<void> _createDb(String dbName, DatabaseConfig config) async {
99+
final tmp = DatabaseAccess(
100+
config: config,
101+
);
102+
// ignore: invalid_use_of_visible_for_testing_member
103+
await tmp.forTestDropDatabase(dbName, ifExists: true);
104+
// ignore: invalid_use_of_visible_for_testing_member
105+
await tmp.forTestCreateDatabase(dbName);
106+
await tmp.dispose();
107+
}

example/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ environment:
88

99
dependencies:
1010
postgres: ^2.4.1+2
11+
uuid: ^3.0.4
12+
logging_appenders: ^1.0.0
1113
postgres_utils:
1214
path: ../

lib/src/config.dart

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class DatabaseConfig {
1818
factory DatabaseConfig.fromJson(Map<String, dynamic> json) =>
1919
_$DatabaseConfigFromJson(json);
2020

21+
/// load database configuration from the `DBCONFIG` environment variables.
22+
/// this environment variable must be a json.
2123
factory DatabaseConfig.fromEnvironment({DatabaseConfig? defaults}) =>
2224
DatabaseConfig.fromJson(_jsonFromEnvironment(defaults));
2325

lib/src/database_access.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,10 @@ abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
256256
}
257257

258258
@visibleForTesting
259-
Future<void> forTestDropDatabase(String databaseName) async {
260-
await (await _connection()).execute('DROP DATABASE $databaseName');
259+
Future<void> forTestDropDatabase(String databaseName,
260+
{bool ifExists = false}) async {
261+
await (await _connection()).execute(
262+
'DROP DATABASE ${ifExists ? ' IF EXISTS ' : ''} $databaseName');
261263
}
262264

263265
Future<void> dispose() async {

0 commit comments

Comments
 (0)