Created
August 26, 2019 15:22
-
-
Save simolus3/78706375bd1a9c4376e2f00ec393c18c to your computer and use it in GitHub Desktop.
Unsucessful attempt at reproducing moor#121
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
import 'dart:convert'; | |
import 'package:moor_flutter/moor_flutter.dart'; | |
part 'database.g.dart'; | |
@DataClassName("Journal") | |
class Journals extends Table { | |
IntColumn get id => integer().autoIncrement()(); | |
TextColumn get name => text().withLength(min: 1, max: 50)(); | |
TextColumn get description => text().withLength(min: 1, max: 100)(); | |
DateTimeColumn get createdOn => dateTime().withDefault(currentDateAndTime)(); | |
DateTimeColumn get updatedOn => dateTime().withDefault(currentDateAndTime)(); | |
TextColumn get backgroundImage => text().nullable()(); | |
} | |
@DataClassName("Entry") | |
class Entries extends Table { | |
IntColumn get id => integer().autoIncrement()(); | |
TextColumn get entry => text().map(const RichTextConverter()).nullable()(); | |
DateTimeColumn get addedOn => dateTime().withDefault(currentDateAndTime)(); | |
IntColumn get journalId => integer()(); | |
} | |
class RichTextConverter extends TypeConverter<dynamic, String> { | |
const RichTextConverter(); | |
@override | |
mapToDart(String fromDb) { | |
if (fromDb == null) { | |
return null; | |
} else { | |
return json.decode(fromDb); | |
} | |
} | |
@override | |
String mapToSql(value) { | |
if (value == null) { | |
return null; | |
} else { | |
return json.encode(value); | |
} | |
} | |
} | |
@UseMoor(tables: [Journals, Entries]) | |
class Database extends _$Database { | |
Database() : super(FlutterQueryExecutor.inDatabaseFolder(path: 'test')); | |
@override | |
int get schemaVersion => 1; | |
@override | |
MigrationStrategy get migration { | |
return MigrationStrategy( | |
beforeOpen: (_, details) async { | |
if (details.wasCreated) { | |
await into(journals).insert(JournalsCompanion( | |
name: Value('Some name'), | |
description: Value('Some description'), | |
)); | |
} | |
}, | |
); | |
} | |
Stream<List<Entry>> watchAllJournalEntries({@required int id}) { | |
return (select(entries) | |
..where((t) => t.journalId.equals(id)) | |
..orderBy( | |
([ | |
(t) => OrderingTerm(expression: t.id, mode: OrderingMode.asc), | |
]), | |
)) | |
.watch(); | |
} | |
Future addEntry(int id) { | |
return into(entries).insert(EntriesCompanion( | |
entry: Value('test'), | |
journalId: Value(id), | |
)); | |
} | |
} |
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
import 'package:flutter/material.dart'; | |
import 'package:moor_joins/database.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final db = Database(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
StreamBuilder<List<Entry>>( | |
stream: db.watchAllJournalEntries(id: 1), | |
builder: (context, snapshot) { | |
if (!snapshot.hasData) { | |
return const CircularProgressIndicator(); | |
} else { | |
return Text( | |
snapshot.data.length.toString(), | |
style: Theme.of(context).textTheme.display1, | |
); | |
} | |
}, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => db.addEntry(1), | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment