Skip to content

Commit 07bbb91

Browse files
Copilotgraknol
andauthored
Remove sqlite_common_ffi from documentation, focus on Flutter/sqflite (#37)
* Initial plan * Remove sqlite_common_ffi references from documentation, focus on Flutter/sqflite Co-authored-by: graknol <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: graknol <[email protected]>
1 parent 507eee2 commit 07bbb91

File tree

5 files changed

+94
-168
lines changed

5 files changed

+94
-168
lines changed

declarative_sqlite/CURRENT_STATE.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,10 @@ The package provides excellent testing support with in-memory databases:
246246
```dart
247247
import 'package:test/test.dart';
248248
import 'package:declarative_sqlite/declarative_sqlite.dart';
249-
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
250249
251250
void main() {
252251
late DeclarativeDatabase database;
253252
254-
setUpAll(() {
255-
sqfliteFfiInit();
256-
databaseFactory = databaseFactoryFfi;
257-
});
258-
259253
setUp(() async {
260254
database = DeclarativeDatabase.memory(buildTestSchema);
261255
});

declarative_sqlite/README.md

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@ Add the package to your `pubspec.yaml`:
1919

2020
```yaml
2121
dependencies:
22+
flutter:
23+
sdk: flutter
24+
# Core library
2225
declarative_sqlite: ^1.0.1
23-
# For standalone Dart apps, you need a native library loader.
24-
sqflite_common_ffi: ^2.3.3
26+
# Flutter integration package
27+
declarative_sqlite_flutter: ^1.0.1
28+
# Standard SQLite plugin for Flutter (Android/iOS)
29+
sqflite: ^2.3.3
2530
```
2631
27-
### Example Usage (Standalone Dart)
32+
### Example Usage (Flutter)
2833
2934
```dart
30-
import 'package:declarative_sqlite/declarative_sqlite.dart';
31-
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
35+
import 'package:flutter/material.dart';
36+
import 'package:declarative_sqlite_flutter/declarative_sqlite_flutter.dart';
3237

3338
// 1. Define your database schema
3439
void buildSchema(SchemaBuilder builder) {
@@ -40,48 +45,68 @@ void buildSchema(SchemaBuilder builder) {
4045
});
4146
}
4247

43-
Future<void> main() async {
44-
// 2. Initialize the FFI driver (for desktop apps)
45-
sqfliteFfiInit();
46-
47-
// 3. Create schema
48-
final schemaBuilder = SchemaBuilder();
49-
buildSchema(schemaBuilder);
50-
final schema = schemaBuilder.build();
51-
52-
// 4. Open the database
53-
final database = await DeclarativeDatabase.open(
54-
'my_app.db',
55-
schema: schema,
56-
fileRepository: FilesystemFileRepository('files'),
48+
void main() {
49+
runApp(
50+
// 2. Wrap your app with DatabaseProvider
51+
DatabaseProvider(
52+
databaseName: 'my_app.db',
53+
schema: buildSchema,
54+
child: const MyApp(),
55+
),
5756
);
57+
}
5858

59-
// 5. Insert data
60-
await database.insert('users', {
61-
'id': 'a1b2c3d4',
62-
'name': 'Alice',
63-
'age': 30,
64-
});
65-
66-
// 6. Query data with the query builder
67-
final users = await database.query((q) =>
68-
q.from('users').where('age').isGreaterThan(25)
69-
);
70-
print('Users older than 25: $users');
71-
72-
// 7. Use streaming queries for reactive UIs
73-
final userStream = database.streamQuery((q) => q.from('users'));
74-
final subscription = userStream.stream.listen((userList) {
75-
print('Current users: ${userList.length}');
76-
});
59+
class MyApp extends StatelessWidget {
60+
const MyApp({super.key});
7761

78-
// Changes will automatically be pushed to the stream
79-
await database.update('users', {'age': 31},
80-
where: 'name = ?', whereArgs: ['Alice']);
62+
@override
63+
Widget build(BuildContext context) {
64+
return MaterialApp(
65+
home: const HomeScreen(),
66+
);
67+
}
68+
}
8169

82-
// Clean up
83-
await subscription.cancel();
84-
await database.close();
70+
class HomeScreen extends StatelessWidget {
71+
const HomeScreen({super.key});
72+
73+
@override
74+
Widget build(BuildContext context) {
75+
// 3. Access the database from any descendant widget
76+
final database = DatabaseProvider.of(context);
77+
78+
return Scaffold(
79+
appBar: AppBar(title: const Text('Declarative SQLite Demo')),
80+
body: Column(
81+
children: [
82+
ElevatedButton(
83+
onPressed: () async {
84+
// 4. Insert data
85+
await database.insert('users', {
86+
'id': 'a1b2c3d4',
87+
'name': 'Alice',
88+
'age': 30,
89+
});
90+
},
91+
child: const Text('Add User'),
92+
),
93+
// 5. Use QueryListView for reactive data display
94+
Expanded(
95+
child: QueryListView<DbRecord>(
96+
query: (q) => q.from('users').where('age').isGreaterThan(25),
97+
mapper: (row, db) => DbRecord(row, 'users', db),
98+
itemBuilder: (context, user) {
99+
return ListTile(
100+
title: Text(user.getValue('name')!),
101+
subtitle: Text('Age: ${user.getValue('age')}'),
102+
);
103+
},
104+
),
105+
),
106+
],
107+
),
108+
);
109+
}
85110
}
86111
```
87112

docs/docs/getting-started/defining-a-schema.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,22 @@ void buildAppSchema(SchemaBuilder builder) {
9090

9191
## Usage in Your Application
9292

93-
Once you've defined your schema function, you'll use it when opening the database:
94-
95-
```dart
96-
final schemaBuilder = SchemaBuilder();
97-
buildAppSchema(schemaBuilder);
98-
final schema = schemaBuilder.build();
99-
100-
final database = await DeclarativeDatabase.open(
101-
'app.db',
102-
databaseFactory: databaseFactoryFfi,
103-
schema: schema,
104-
fileRepository: FilesystemFileRepository('files'),
105-
);
93+
Once you've defined your schema function, you'll use it with the `DatabaseProvider` widget:
94+
95+
```dart title="lib/main.dart"
96+
import 'package:flutter/material.dart';
97+
import 'package:declarative_sqlite_flutter/declarative_sqlite_flutter.dart';
98+
import 'database/schema.dart';
99+
100+
void main() {
101+
runApp(
102+
DatabaseProvider(
103+
databaseName: 'app.db',
104+
schema: buildAppSchema,
105+
child: const MyApp(),
106+
),
107+
);
108+
}
106109
```
107110

108111
## Next Steps

docs/docs/getting-started/initializing-the-database.md

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ sidebar_position: 3
44

55
# Initializing the Database
66

7-
Once you have defined your schema, the next step is to create and initialize a `DeclarativeDatabase` instance. This process involves connecting to the database file, analyzing the existing schema, and automatically applying any necessary migrations.
7+
Once you have defined your schema, the next step is to create and initialize a `DeclarativeDatabase` instance using the `DatabaseProvider` widget. This process involves connecting to the database file, analyzing the existing schema, and automatically applying any necessary migrations.
88

9-
## Using `DatabaseProvider` (Flutter)
9+
## Using `DatabaseProvider`
1010

1111
In a Flutter application, the easiest way to manage the database lifecycle is with the `DatabaseProvider` widget. It handles initialization, closing the connection, and making the database instance available to the entire widget tree.
1212

@@ -51,61 +51,12 @@ class HomeScreen extends StatelessWidget {
5151
final database = DatabaseProvider.of(context);
5252
5353
return Scaffold(
54-
// ...
54+
// Your app UI here...
5555
);
5656
}
5757
}
5858
```
5959

60-
## Manual Initialization (Standalone Dart)
61-
62-
In a standalone Dart application, you create and initialize the `DeclarativeDatabase` instance manually.
63-
64-
You'll need to provide:
65-
- `path`: The path to the database file.
66-
- `schema`: A reference to your schema builder function.
67-
- `dbFactory`: The database factory from `sqflite_common_ffi`.
68-
69-
```dart title="bin/my_app.dart"
70-
import 'package:declarative_sqlite/declarative_sqlite.dart';
71-
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
72-
import 'path/to/your/schema.dart';
73-
74-
Future<void> main() async {
75-
// 1. Initialize the FFI driver
76-
sqfliteFfiInit();
77-
78-
// 2. Build the schema
79-
final schemaBuilder = SchemaBuilder();
80-
buildAppSchema(schemaBuilder);
81-
final schema = schemaBuilder.build();
82-
83-
// 3. Open the database
84-
final database = await DeclarativeDatabase.open(
85-
'my_app.db',
86-
databaseFactory: databaseFactoryFfi,
87-
schema: schema,
88-
fileRepository: FilesystemFileRepository('files'),
89-
);
90-
91-
print('Database opened successfully!');
92-
93-
// Your application logic here...
94-
await database.insert('users', {
95-
'id': '1',
96-
'name': 'Alice',
97-
'email': '[email protected]',
98-
});
99-
100-
// Query data
101-
final users = await database.queryMaps((q) => q.from('users'));
102-
print('Users: $users');
103-
104-
// 4. Close the database when done
105-
await database.close();
106-
}
107-
```
108-
10960
## Automatic Migrations
11061

11162
The first time you initialize the database, `declarative_sqlite` will:
@@ -124,7 +75,6 @@ This process is fully automatic. You only need to update your schema in your Dar
12475

12576
You can enable detailed logging for the migration process by setting the `logStatements` flag to `true`. This is useful for debugging what happens during initialization.
12677

127-
**In Flutter:**
12878
```dart
12979
DatabaseProvider(
13080
databaseName: 'app.db',
@@ -134,17 +84,6 @@ DatabaseProvider(
13484
)
13585
```
13686

137-
**In Dart:**
138-
```dart
139-
final database = DeclarativeDatabase(
140-
path: 'my_app.db',
141-
schema: appSchema,
142-
dbFactory: dbFactory,
143-
logStatements: true, // Enable logging
144-
);
145-
await database.init();
146-
```
147-
14887
## Next Steps
14988

15089
With your database initialized, you are now ready to perform CRUD operations and run queries.

docs/docs/getting-started/installation.md

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@ sidebar_position: 1
44

55
# Installation
66

7-
Setting up `declarative_sqlite` involves adding the required packages to your `pubspec.yaml` and choosing the appropriate SQLite driver for your target platform.
7+
Setting up `declarative_sqlite` for Flutter involves adding the required packages to your `pubspec.yaml`. The library uses the standard `sqflite` plugin for Android and iOS compatibility.
88

9-
## 1. Add Dependencies
9+
## Add Dependencies
1010

11-
The ecosystem is split into multiple packages. Add the ones you need for your project.
12-
13-
### For Flutter Projects
14-
15-
For a standard Flutter application, you'll need the core library, the Flutter integration package, and the `sqflite` driver. If you plan to use code generation (recommended), you'll also need the generator and `build_runner`.
11+
For a Flutter application, you'll need the core library, the Flutter integration package, and the `sqflite` driver. If you plan to use code generation (recommended), you'll also need the generator and `build_runner`.
1612

1713
```yaml title="pubspec.yaml"
1814
dependencies:
@@ -22,7 +18,7 @@ dependencies:
2218
declarative_sqlite: ^1.0.1
2319
# Flutter-specific widgets and helpers
2420
declarative_sqlite_flutter: ^1.0.1
25-
# Standard SQLite plugin for Flutter
21+
# Standard SQLite plugin for Flutter (Android/iOS)
2622
sqflite: ^2.3.3
2723

2824
dev_dependencies:
@@ -32,42 +28,11 @@ dev_dependencies:
3228
build_runner: ^2.4.10
3329
```
3430
35-
### For Standalone Dart Projects
36-
37-
For command-line or server-side Dart applications, you'll need the core library and the `sqflite_common_ffi` driver.
31+
After adding the dependencies, run `flutter pub get` to install them.
3832

39-
```yaml title="pubspec.yaml"
40-
dependencies:
41-
# Core library
42-
declarative_sqlite: ^1.0.1
43-
# FFI-based SQLite driver for Dart
44-
sqflite_common_ffi: ^2.3.3
45-
46-
dev_dependencies:
47-
# Code generator for DbRecord classes
48-
declarative_sqlite_generator: ^1.0.1
49-
# Standard Dart build tool
50-
build_runner: ^2.4.10
51-
```
52-
53-
After adding the dependencies, run `flutter pub get` or `dart pub get` to install them.
54-
55-
## 2. Initialize the Database Driver (Dart Only)
56-
57-
For standalone Dart applications using FFI, you need to initialize the `sqflite_common_ffi` driver at the beginning of your application's entry point.
58-
59-
```dart title="bin/my_app.dart"
60-
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
61-
62-
void main() {
63-
// Initialize FFI
64-
sqfliteFfiInit();
65-
66-
// Your application logic here...
67-
}
68-
```
33+
## Database Initialization
6934

70-
Flutter projects using the standard `sqflite` package do **not** need this step, as initialization is handled automatically.
35+
Flutter projects using the `sqflite` package do **not** need any special initialization steps. The SQLite driver is automatically available on Android and iOS platforms.
7136

7237
## Next Steps
7338

0 commit comments

Comments
 (0)