Skip to content

Commit

Permalink
Merge pull request #1 from simolus3/web
Browse files Browse the repository at this point in the history
Support latest sqlite3 package
  • Loading branch information
muhleder committed May 5, 2022
2 parents 47346a5 + 322240c commit ed8e6c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
24 changes: 17 additions & 7 deletions sqflite_common_ffi/lib/src/sqflite_ffi_impl_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@ import 'package:http/http.dart' as http;

import 'package:sqlite3/wasm.dart';

const _dbName = 'sqflite_databases';

/// Opens the database using a wasm implementation
Future<CommonDatabase> handleOpenPlatform(Map argumentsMap) async {
var path = argumentsMap['path'] as String;
var readOnly = (argumentsMap['readOnly'] as bool?) ?? false;
final response = await http.get(Uri.parse('sqlite3.wasm'));
final fs = await IndexedDbFileSystem.load('/');
var sqlite = await WasmSqlite3.load(response.bodyBytes, SqliteEnvironment(fileSystem: fs));
final fs = await IndexedDbFileSystem.open(dbName: _dbName);
var sqlite = await WasmSqlite3.load(
response.bodyBytes, SqliteEnvironment(fileSystem: fs));
var mode = readOnly ? OpenMode.readOnly : OpenMode.readWriteCreate;
var db = sqlite.open(path, mode: mode);
return db;
}

/// Delete the database file.
Future<void> deleteDatabasePlatform(String path) async {
final fs = await IndexedDbFileSystem.load('/');
fs.deleteFile(path);
final fs = await IndexedDbFileSystem.open(dbName: _dbName);
try {
fs.deleteFile(path);
await fs.flush();
} finally {
await fs.close();
}
}

/// Check if database file exists
Future<bool> handleDatabaseExistsPlatform(String path) async {
// Ignore failure
try {
final fs = await IndexedDbFileSystem.load('/');
return fs.exists(path);
final fs = await IndexedDbFileSystem.open(dbName: _dbName);
final exists = fs.exists(path);
await fs.close();
return exists;
} catch (_) {
return false;
}
}
}
5 changes: 2 additions & 3 deletions sqflite_common_ffi/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ environment:
sdk: '>=2.16.0 <3.0.0'

dependencies:
sqlite3: '>=1.0.0 <3.0.0'
sqlite3: '>=1.7.0 <3.0.0'
sqflite_common: '>=2.2.1 <4.0.0'
synchronized: '>=3.0.0 <5.0.0'
path: '>=1.8.0 <3.0.0'
meta: '>=1.3.0 <3.0.0'
http: ^0.13.4

dev_dependencies:
lints:
test: '>=1.16.2'
process_run: '>=0.12.0'
pub_semver: '>=2.0.0'
archive:
http:

0 comments on commit ed8e6c6

Please sign in to comment.