mirror of https://github.com/AMT-Cheif/drift.git
Early insert tests
This commit is contained in:
parent
65ccd49d9a
commit
4ee7e84e8c
|
@ -7,10 +7,13 @@ environment:
|
|||
dependencies:
|
||||
moor: ^1.4.0
|
||||
|
||||
dev_dependencies:
|
||||
moor_generator:
|
||||
build_runner: ^1.1.2
|
||||
build_web_compilers: ^1.0.0
|
||||
|
||||
dependency_overrides:
|
||||
moor:
|
||||
path: ../
|
||||
|
||||
dev_dependencies:
|
||||
build_runner: ^1.1.2
|
||||
build_web_compilers: ^1.0.0
|
||||
moor_generator:
|
||||
path: ../../moor_generator
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import 'package:moor/moor.dart';
|
||||
|
||||
part 'database.g.dart';
|
||||
|
||||
@DataClassName('TodoEntry')
|
||||
class TodoEntries extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
|
||||
TextColumn get content => text()();
|
||||
DateTimeColumn get creationDate =>
|
||||
dateTime().withDefault(currentDateAndTime)();
|
||||
}
|
||||
|
||||
@UseMoor(tables: [TodoEntries])
|
||||
class Database extends _$Database {
|
||||
Database(QueryExecutor e) : super(e);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
||||
Stream<List<TodoEntry>> watchEntries() {
|
||||
return select(todoEntries).watch();
|
||||
}
|
||||
|
||||
void insert(String text) async {
|
||||
await into(todoEntries).insert(TodoEntriesCompanion(content: Value(text)));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,206 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'database.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// MoorGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// ignore_for_file: unnecessary_brace_in_string_interps
|
||||
class TodoEntry extends DataClass implements Insertable<TodoEntry> {
|
||||
final int id;
|
||||
final String content;
|
||||
final DateTime creationDate;
|
||||
TodoEntry(
|
||||
{@required this.id, @required this.content, @required this.creationDate});
|
||||
factory TodoEntry.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||
{String prefix}) {
|
||||
final effectivePrefix = prefix ?? '';
|
||||
final intType = db.typeSystem.forDartType<int>();
|
||||
final stringType = db.typeSystem.forDartType<String>();
|
||||
final dateTimeType = db.typeSystem.forDartType<DateTime>();
|
||||
return TodoEntry(
|
||||
id: intType.mapFromDatabaseResponse(data['${effectivePrefix}id']),
|
||||
content:
|
||||
stringType.mapFromDatabaseResponse(data['${effectivePrefix}content']),
|
||||
creationDate: dateTimeType
|
||||
.mapFromDatabaseResponse(data['${effectivePrefix}creation_date']),
|
||||
);
|
||||
}
|
||||
factory TodoEntry.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return TodoEntry(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
content: serializer.fromJson<String>(json['content']),
|
||||
creationDate: serializer.fromJson<DateTime>(json['creationDate']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson(
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return {
|
||||
'id': serializer.toJson<int>(id),
|
||||
'content': serializer.toJson<String>(content),
|
||||
'creationDate': serializer.toJson<DateTime>(creationDate),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
T createCompanion<T extends UpdateCompanion<TodoEntry>>(bool nullToAbsent) {
|
||||
return TodoEntriesCompanion(
|
||||
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||
content: content == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(content),
|
||||
creationDate: creationDate == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(creationDate),
|
||||
) as T;
|
||||
}
|
||||
|
||||
TodoEntry copyWith({int id, String content, DateTime creationDate}) =>
|
||||
TodoEntry(
|
||||
id: id ?? this.id,
|
||||
content: content ?? this.content,
|
||||
creationDate: creationDate ?? this.creationDate,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('TodoEntry(')
|
||||
..write('id: $id, ')
|
||||
..write('content: $content, ')
|
||||
..write('creationDate: $creationDate')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => $mrjf($mrjc(
|
||||
$mrjc($mrjc(0, id.hashCode), content.hashCode), creationDate.hashCode));
|
||||
@override
|
||||
bool operator ==(other) =>
|
||||
identical(this, other) ||
|
||||
(other is TodoEntry &&
|
||||
other.id == id &&
|
||||
other.content == content &&
|
||||
other.creationDate == creationDate);
|
||||
}
|
||||
|
||||
class TodoEntriesCompanion extends UpdateCompanion<TodoEntry> {
|
||||
final Value<int> id;
|
||||
final Value<String> content;
|
||||
final Value<DateTime> creationDate;
|
||||
const TodoEntriesCompanion({
|
||||
this.id = const Value.absent(),
|
||||
this.content = const Value.absent(),
|
||||
this.creationDate = const Value.absent(),
|
||||
});
|
||||
}
|
||||
|
||||
class $TodoEntriesTable extends TodoEntries
|
||||
with TableInfo<$TodoEntriesTable, TodoEntry> {
|
||||
final GeneratedDatabase _db;
|
||||
final String _alias;
|
||||
$TodoEntriesTable(this._db, [this._alias]);
|
||||
final VerificationMeta _idMeta = const VerificationMeta('id');
|
||||
GeneratedIntColumn _id;
|
||||
@override
|
||||
GeneratedIntColumn get id => _id ??= _constructId();
|
||||
GeneratedIntColumn _constructId() {
|
||||
return GeneratedIntColumn('id', $tableName, false, hasAutoIncrement: true);
|
||||
}
|
||||
|
||||
final VerificationMeta _contentMeta = const VerificationMeta('content');
|
||||
GeneratedTextColumn _content;
|
||||
@override
|
||||
GeneratedTextColumn get content => _content ??= _constructContent();
|
||||
GeneratedTextColumn _constructContent() {
|
||||
return GeneratedTextColumn(
|
||||
'content',
|
||||
$tableName,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
final VerificationMeta _creationDateMeta =
|
||||
const VerificationMeta('creationDate');
|
||||
GeneratedDateTimeColumn _creationDate;
|
||||
@override
|
||||
GeneratedDateTimeColumn get creationDate =>
|
||||
_creationDate ??= _constructCreationDate();
|
||||
GeneratedDateTimeColumn _constructCreationDate() {
|
||||
return GeneratedDateTimeColumn('creation_date', $tableName, false,
|
||||
defaultValue: currentDateAndTime);
|
||||
}
|
||||
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [id, content, creationDate];
|
||||
@override
|
||||
$TodoEntriesTable get asDslTable => this;
|
||||
@override
|
||||
String get $tableName => _alias ?? 'todo_entries';
|
||||
@override
|
||||
final String actualTableName = 'todo_entries';
|
||||
@override
|
||||
VerificationContext validateIntegrity(TodoEntriesCompanion d,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
if (d.id.present) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
} else if (id.isRequired && isInserting) {
|
||||
context.missing(_idMeta);
|
||||
}
|
||||
if (d.content.present) {
|
||||
context.handle(_contentMeta,
|
||||
content.isAcceptableValue(d.content.value, _contentMeta));
|
||||
} else if (content.isRequired && isInserting) {
|
||||
context.missing(_contentMeta);
|
||||
}
|
||||
if (d.creationDate.present) {
|
||||
context.handle(
|
||||
_creationDateMeta,
|
||||
creationDate.isAcceptableValue(
|
||||
d.creationDate.value, _creationDateMeta));
|
||||
} else if (creationDate.isRequired && isInserting) {
|
||||
context.missing(_creationDateMeta);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
TodoEntry map(Map<String, dynamic> data, {String tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
|
||||
return TodoEntry.fromData(data, _db, prefix: effectivePrefix);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Variable> entityToSql(TodoEntriesCompanion d) {
|
||||
final map = <String, Variable>{};
|
||||
if (d.id.present) {
|
||||
map['id'] = Variable<int, IntType>(d.id.value);
|
||||
}
|
||||
if (d.content.present) {
|
||||
map['content'] = Variable<String, StringType>(d.content.value);
|
||||
}
|
||||
if (d.creationDate.present) {
|
||||
map['creation_date'] =
|
||||
Variable<DateTime, DateTimeType>(d.creationDate.value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
$TodoEntriesTable createAlias(String alias) {
|
||||
return $TodoEntriesTable(_db, alias);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _$Database extends GeneratedDatabase {
|
||||
_$Database(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e);
|
||||
$TodoEntriesTable _todoEntries;
|
||||
$TodoEntriesTable get todoEntries => _todoEntries ??= $TodoEntriesTable(this);
|
||||
@override
|
||||
List<TableInfo> get allTables => [todoEntries];
|
||||
}
|
|
@ -18,5 +18,10 @@
|
|||
|
||||
<div id="output"></div>
|
||||
|
||||
<form id="add_todo_form">
|
||||
<input type="text" id="description" />
|
||||
<input type="submit">
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,22 +1,19 @@
|
|||
import 'dart:html';
|
||||
|
||||
import 'package:moor/moor_web.dart';
|
||||
|
||||
class TestDb extends GeneratedDatabase {
|
||||
TestDb(QueryExecutor executor)
|
||||
: super(const SqlTypeSystem.withDefaults(), executor);
|
||||
|
||||
@override
|
||||
List<TableInfo<Table, DataClass>> get allTables => const [];
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
}
|
||||
import 'database.dart';
|
||||
|
||||
void main() async {
|
||||
final executor = AlaSqlDatabase('database');
|
||||
executor.databaseInfo = TestDb(executor);
|
||||
final db = Database(AlaSqlDatabase('database'));
|
||||
db.watchEntries().listen(print);
|
||||
|
||||
final result = await executor.doWhenOpened((e) {
|
||||
return e.runSelect('SELECT 1', const []);
|
||||
final content = querySelector('#description');
|
||||
|
||||
(querySelector('#add_todo_form') as FormElement).onSubmit.listen((e) {
|
||||
e.preventDefault();
|
||||
|
||||
db.insert(content.text);
|
||||
content.text = '';
|
||||
});
|
||||
print(result);
|
||||
}
|
||||
|
|
|
@ -19,3 +19,4 @@ import 'moor.dart';
|
|||
export 'moor.dart';
|
||||
|
||||
part 'src/web/alasql.dart';
|
||||
part 'src/web/functions.dart';
|
||||
|
|
|
@ -11,6 +11,8 @@ class AlaSqlDatabase extends QueryExecutor {
|
|||
Completer<bool> _opening;
|
||||
|
||||
AlaSqlDatabase(this.name, {this.logStatements = false}) {
|
||||
_registerFunctions();
|
||||
|
||||
if (_alasql == null) {
|
||||
throw UnsupportedError('Could not access the alasql javascript library. '
|
||||
'The moor documentation contains instructions on how to setup moor '
|
||||
|
@ -22,7 +24,8 @@ class AlaSqlDatabase extends QueryExecutor {
|
|||
|
||||
@override
|
||||
TransactionExecutor beginTransaction() {
|
||||
throw StateError('Transactions are not currently supported with AlaSQL');
|
||||
throw StateError(
|
||||
'Transactions are not currently supported with the AlaSQL backend');
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
part of 'package:moor/moor_web.dart';
|
||||
|
||||
final functions = _alasql['fn'] as JsObject;
|
||||
|
||||
void _registerFunctions() {
|
||||
functions['strftime'] = allowInterop(_strftime);
|
||||
}
|
||||
|
||||
int _strftime(String format, int i) {
|
||||
// todo properly implement this
|
||||
return DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
}
|
Loading…
Reference in New Issue