mirror of https://github.com/AMT-Cheif/drift.git
Return the last insert id in the web backend
This commit is contained in:
parent
0e6ed25cd5
commit
544db67d17
|
@ -22,8 +22,7 @@ class Database extends _$Database {
|
|||
return select(todoEntries).watch();
|
||||
}
|
||||
|
||||
void insert(String text) async {
|
||||
print('inserting with $text');
|
||||
await into(todoEntries).insert(TodoEntriesCompanion(content: Value(text)));
|
||||
Future<int> insert(String text) {
|
||||
return into(todoEntries).insert(TodoEntriesCompanion(content: Value(text)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ void main() async {
|
|||
final content = querySelector('#description') as InputElement;
|
||||
e.preventDefault();
|
||||
|
||||
db.insert(content.value);
|
||||
db.insert(content.value).then((insertId) => print('inserted #$insertId'));
|
||||
content.value = '';
|
||||
});
|
||||
}
|
||||
|
|
|
@ -138,7 +138,8 @@ class WebDatabase extends QueryExecutor {
|
|||
if (variables.isEmpty) {
|
||||
_database.callMethod('run', [sql]);
|
||||
} else {
|
||||
_database.callMethod('run', [sql, JsArray.from(variables)]);
|
||||
final ar = JsArray.from(variables);
|
||||
_database.callMethod('run', [sql, ar]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,6 +171,8 @@ class WebDatabase extends QueryExecutor {
|
|||
return _handlePotentialUpdate();
|
||||
}
|
||||
|
||||
/// Saves the database if the last statement changed rows. As a side-effect,
|
||||
/// saving the database resets the `last_insert_id` counter in sqlite.
|
||||
Future<int> _handlePotentialUpdate() {
|
||||
final modified = _getModifiedRows();
|
||||
if (modified > 0) {
|
||||
|
@ -179,11 +182,18 @@ class WebDatabase extends QueryExecutor {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<int> runInsert(String statement, List args) {
|
||||
// todo get last insert id
|
||||
Future<int> runInsert(String statement, List args) async {
|
||||
_runSimple(statement, args);
|
||||
_handlePotentialUpdate();
|
||||
return Future.value(42);
|
||||
|
||||
// load insert id. Will return [{columns: [...], values: [[id]]}]
|
||||
final results = _database
|
||||
.callMethod('exec', const ['SELECT last_insert_rowid();']) as JsArray;
|
||||
final row = results.first as JsObject;
|
||||
final data = (row['values'] as JsArray).first as JsArray;
|
||||
|
||||
await _handlePotentialUpdate();
|
||||
|
||||
return Future.value(data.first as int);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
Loading…
Reference in New Issue