This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from FunnyLabz/master
Fix 'require.js' load issue while loading web page + add explanation on how to use on web + make it easier to integrate this lib on a project
- Loading branch information
Showing
15 changed files
with
264 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,63 @@ | ||
# sqflite_web | ||
|
||
These are the first steps to start a web version of [sqflite](https://pub.dev/packages/sqflite). Right now, it already runs and creates a test database and writes a few records. | ||
This is a web version of [sqflite](https://pub.dev/packages/sqflite). | ||
|
||
Please, note that this is experimental. It's not on pub.dev and not automatically endorsed by Sqflite as the web implementation because these are just the first steps to see if it's feasible at all. | ||
There is no persistence and copies of the same app running in different browser tabs would see different, separate databases. | ||
Use it if it fits your requirements and suggest solutions to the missing functionality if you have ideas but don't expect it to be a full Sqflite implementation on the web. | ||
|
||
While the code itself is functional, the database stays in the memory. There is no persistence and copies of the same app running in different browser tabs would see different, separate databases. | ||
Use it if it fits your requirements and suggest solutions to the missing functionality if you have ideas but don't expect it to be a full Sqflite implementation on the web. | ||
Please, note that this is experimental. It's not on pub.dev and not automatically endorsed by Sqflite as the web implementation because of the persistency issue. | ||
|
||
The example runs and creates a test database and writes a few records on web console (F12->console on most web browser or directly on shell if running in debug). | ||
|
||
|
||
## Install | ||
|
||
`pubspec.yaml`: | ||
|
||
```yaml | ||
dependencies: | ||
# Database handling (if you want to support other platforms than web too) | ||
sqflite: ^1.3.2+2 | ||
|
||
dev_dependencies: | ||
# For sqflite web compatibility (will save the database IN MEMORY => not stored) | ||
sqflite_web: | ||
git: | ||
url: https://github.com/FunnyLabz/sqflite_web.git | ||
ref: master | ||
``` | ||
`main.dart` (or any other dart file) | ||
|
||
```dart | ||
import 'package:sqflite/sqflite.dart'; | ||
import 'package:sqflite_web/sqflite_web.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
void main() { | ||
(...) | ||
// Open the database and do whatever you want | ||
Database appDatabase; | ||
if (kIsWeb) { | ||
// Use the database from memory (no persistency on web...) | ||
var databaseFactory = databaseFactoryWeb; | ||
appDatabase = await databaseFactory.openDatabase(inMemoryDatabasePath); | ||
} else { | ||
// Other platforms (store on real file) | ||
appDatabase = await databaseFactory.openDatabase((await getApplicationDocumentsDirectory()).path + '/app.db'); | ||
} | ||
(...) | ||
} | ||
``` | ||
|
||
Add this line in `web/index.html`: | ||
```html | ||
<body> | ||
<script src="assets/packages/sqflite_web/assets/require.js" type="application/javascript"></script> | ||
(...) | ||
</body> | ||
``` | ||
Note: The `require.js` library will be added dynamically if you forget to add this line in `index.html` | ||
(but this will imply users do a 'refresh' of the web page every time they want to access it because of dynamic include issues). |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,2 @@ | ||
import 'dart:async'; | ||
import 'dart:html' as html; | ||
|
||
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; | ||
import 'package:sqflite_common/sqlite_api.dart'; | ||
import 'package:sqflite_web/src/database_factory.dart'; | ||
|
||
/// The database factory to use for Web. | ||
/// | ||
/// Check support documentation. | ||
DatabaseFactory get databaseFactoryWeb => databaseFactoryWebImpl; | ||
|
||
/// The Web plugin registration. | ||
class SqflitePluginWeb extends PlatformInterface { | ||
static final _readyCompleter = Completer<bool>(); | ||
|
||
/// Shows if the Sql.js library has been loaded | ||
static Future<bool> isReady; | ||
|
||
/// Registers the Web database factory. | ||
static void registerWith(Registrar registrar) { | ||
isReady = _readyCompleter.future; | ||
html.window.addEventListener('sqflite_web_ready', (_) => _readyCompleter.complete(true)); | ||
|
||
final body = html.window.document.querySelector('body'); | ||
// Hot reload would add it again | ||
// ignore: omit_local_variable_types | ||
for (html.ScriptElement script in body.querySelectorAll('script')) { | ||
if (script.src.contains('sqflite_web')) { | ||
script.remove(); | ||
} | ||
} | ||
|
||
body.append(html.ScriptElement() | ||
..src = 'assets/packages/sqflite_web/assets/require.js' | ||
..type = 'application/javascript'); | ||
body.append(html.ScriptElement() | ||
..src = 'assets/packages/sqflite_web/assets/sqflite_web.js' | ||
..type = 'application/javascript'); | ||
} | ||
} | ||
export 'src/sqflite_web_stub.dart' | ||
if (dart.library.html) 'src/sqflite_web.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'dart:async'; | ||
import 'dart:html' as html; | ||
|
||
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; | ||
|
||
import 'package:sqflite_common/sqlite_api.dart'; | ||
|
||
import 'database_factory.dart'; | ||
|
||
/// The database factory to use for Web. | ||
/// | ||
/// Check support documentation. | ||
DatabaseFactory get databaseFactoryWeb => databaseFactoryWebImpl; | ||
|
||
/// The Web plugin registration. | ||
class SqflitePluginWeb extends PlatformInterface { | ||
static final _readyCompleter = Completer<bool>(); | ||
|
||
/// Shows if the Sql.js library has been loaded | ||
static Future<bool> isReady; | ||
|
||
/// Registers the Web database factory. | ||
static void registerWith(Registrar registrar) { | ||
isReady = _readyCompleter.future; | ||
html.window.addEventListener( | ||
'sqflite_web_ready', (_) => _readyCompleter.complete(true)); | ||
|
||
final body = html.window.document.querySelector('body'); | ||
|
||
// Add 'sqflite_web.js' (and 'require.js') dynamically if not already imported | ||
// (this is needed to prevent hot reload or refresh to import it again and again) | ||
var foundRequireJs = false; | ||
var foundSqfliteWebJs = false; | ||
for (html.ScriptElement script in body.querySelectorAll('script')) { | ||
if (script.src | ||
.contains('assets/packages/sqflite_web/assets/require.js')) { | ||
foundRequireJs = true; | ||
} | ||
if (script.src | ||
.contains('assets/packages/sqflite_web/assets/sqflite_web.js')) { | ||
foundSqfliteWebJs = true; | ||
} | ||
} | ||
|
||
if (!foundRequireJs) { | ||
print( | ||
"<!> WARNING: Importing 'require.js' from sqlite_web, considere importing it directlty from your html file like this: '<script src=\"assets/packages/sqflite_web/assets/require.js\" type=\"application/javascript\"></script>'"); | ||
body.append(html.ScriptElement() | ||
..src = 'assets/packages/sqflite_web/assets/require.js' | ||
..type = 'application/javascript'); | ||
} | ||
if (!foundSqfliteWebJs) { | ||
body.append(html.ScriptElement() | ||
..src = 'assets/packages/sqflite_web/assets/sqflite_web.js' | ||
..type = 'application/javascript'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.