|
| 1 | +import 'dart:io' as io; |
| 2 | +import 'package:moor/moor.dart'; |
| 3 | +import 'package:moor/ffi.dart'; |
| 4 | +import 'package:uuid/uuid.dart'; |
| 5 | +import 'package:netcoresync_moor/netcoresync_moor.dart'; |
| 6 | + |
| 7 | +part 'main.g.dart'; |
| 8 | + |
| 9 | +// The following is just an example of how to "syntactically" use the library, |
| 10 | +// technically the example won't synchronize correctly because the server-side |
| 11 | +// component needs to be configured first. For more complete example, visit the |
| 12 | +// project example on: |
| 13 | +// https://github.com/aldycool/NETCoreSync/tree/master/Samples/ServerTimeStamp/clientsample |
| 14 | + |
| 15 | +@NetCoreSyncTable() |
| 16 | +class Employees extends Table { |
| 17 | + TextColumn get id => |
| 18 | + text().withLength(max: 36).clientDefault(() => Uuid().v4())(); |
| 19 | + TextColumn get name => text()(); |
| 20 | + DateTimeColumn get birthday => |
| 21 | + dateTime().clientDefault(() => DateTime.now())(); |
| 22 | + |
| 23 | + // these are the "synchronization" fields |
| 24 | + TextColumn get syncId => |
| 25 | + text().withLength(max: 36).withDefault(Constant(""))(); |
| 26 | + TextColumn get knowledgeId => |
| 27 | + text().withLength(max: 36).withDefault(Constant(""))(); |
| 28 | + BoolColumn get synced => boolean().withDefault(const Constant(false))(); |
| 29 | + BoolColumn get deleted => boolean().withDefault(const Constant(false))(); |
| 30 | + |
| 31 | + @override |
| 32 | + Set<Column> get primaryKey => { |
| 33 | + id, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +@UseMoor( |
| 38 | + tables: [ |
| 39 | + Employees, |
| 40 | + NetCoreSyncKnowledges, |
| 41 | + ], |
| 42 | +) |
| 43 | +class MyDatabase extends _$MyDatabase |
| 44 | + with NetCoreSyncClient, NetCoreSyncClientUser { |
| 45 | + MyDatabase(QueryExecutor queryExecutor) : super(queryExecutor); |
| 46 | + |
| 47 | + @override |
| 48 | + int get schemaVersion => 1; |
| 49 | + |
| 50 | + @override |
| 51 | + MigrationStrategy get migration => MigrationStrategy( |
| 52 | + onCreate: (Migrator m) { |
| 53 | + return m.createAll(); |
| 54 | + }, |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +void main() async { |
| 59 | + final myDatabase = MyDatabase(LazyDatabase(() async { |
| 60 | + final file = io.File("my_database_file.db"); |
| 61 | + return VmDatabase(file, logStatements: true); |
| 62 | + })); |
| 63 | + await myDatabase.netCoreSyncInitialize(); |
| 64 | + |
| 65 | + // The server-side setup needs to be configured first, or else the |
| 66 | + // synchronization will fail. For more complete example, visit the project |
| 67 | + // example on: |
| 68 | + // https://github.com/aldycool/NETCoreSync/tree/master/Samples/ServerTimeStamp/clientsample |
| 69 | + await myDatabase.netCoreSyncSynchronize( |
| 70 | + url: "wss://localhost:5001/netcoresyncserver"); |
| 71 | +} |
0 commit comments