mirror of https://github.com/AMT-Cheif/drift.git
Generate getters for DAO as well
This commit is contained in:
parent
5a8dd923d6
commit
a637708591
|
@ -141,21 +141,7 @@ class DatabaseWriter {
|
|||
// Also write implicit DAOs for modular imports
|
||||
if (scope.generationOptions.isModular) {
|
||||
for (final import in input.resolvedAccessor.knownImports) {
|
||||
if (import.hasModularDriftAccessor) {
|
||||
final type = dbScope.modularAccessor(import.ownUri);
|
||||
final getter = ReCase(type.toString()).camelCase;
|
||||
|
||||
dbScope.leaf()
|
||||
..writeDart(type)
|
||||
..write(' get $getter => ')
|
||||
..writeUriRef(
|
||||
ModularAccessorWriter.modularSupport, 'ReadDatabaseContainer')
|
||||
..writeln('(this).accessor<')
|
||||
..writeDart(type)
|
||||
..write('>(')
|
||||
..writeDart(type)
|
||||
..writeln('.new);');
|
||||
}
|
||||
dbScope.writeGetterForIncludedDriftFile(import, isAccessor: false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import '../analysis/results/results.dart';
|
||||
import 'database_writer.dart';
|
||||
import 'modules.dart';
|
||||
import 'queries/query_writer.dart';
|
||||
import 'writer.dart';
|
||||
|
||||
|
@ -35,6 +36,12 @@ class AccessorWriter {
|
|||
QueryWriter(classScope.child()).write(query);
|
||||
}
|
||||
|
||||
if (scope.generationOptions.isModular) {
|
||||
for (final import in input.resolvedAccessor.knownImports) {
|
||||
classScope.writeGetterForIncludedDriftFile(import, isAccessor: true);
|
||||
}
|
||||
}
|
||||
|
||||
classScope.leaf().write('}');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,3 +91,28 @@ class ModularAccessorWriter {
|
|||
static final Uri modularSupport =
|
||||
Uri.parse('package:drift/internal/modular.dart');
|
||||
}
|
||||
|
||||
extension WriteImplicitDaoGetter on Scope {
|
||||
void writeGetterForIncludedDriftFile(FileState import,
|
||||
{required bool isAccessor}) {
|
||||
assert(generationOptions.isModular);
|
||||
|
||||
if (import.hasModularDriftAccessor) {
|
||||
final type = modularAccessor(import.ownUri);
|
||||
final getter = ReCase(type.toString()).camelCase;
|
||||
|
||||
final db = isAccessor ? 'attachedDatabase' : 'this';
|
||||
|
||||
leaf()
|
||||
..writeDart(type)
|
||||
..write(' get $getter => ')
|
||||
..writeUriRef(
|
||||
ModularAccessorWriter.modularSupport, 'ReadDatabaseContainer')
|
||||
..writeln('($db).accessor<')
|
||||
..writeDart(type)
|
||||
..write('>(')
|
||||
..writeDart(type)
|
||||
..writeln('.new);');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'accessor.drift.dart';
|
|||
import 'database.dart';
|
||||
|
||||
@DriftAccessor(
|
||||
include: {'src/users.drift'},
|
||||
include: {'src/user_queries.drift'},
|
||||
queries: {'addUser': r'INSERT INTO users $user;'},
|
||||
)
|
||||
class MyAccessor extends DatabaseAccessor<Database> with $MyAccessorMixin {
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:modular/database.dart' as i1;
|
||||
import 'package:modular/src/users.drift.dart' as i2;
|
||||
import 'package:modular/src/user_queries.drift.dart' as i3;
|
||||
import 'package:drift/internal/modular.dart' as i4;
|
||||
|
||||
mixin $MyAccessorMixin on i0.DatabaseAccessor<i1.Database> {
|
||||
i2.Users get users => attachedDatabase.users;
|
||||
|
@ -18,4 +20,8 @@ mixin $MyAccessorMixin on i0.DatabaseAccessor<i1.Database> {
|
|||
updates: {users},
|
||||
);
|
||||
}
|
||||
|
||||
i3.UserQueriesDrift get userQueriesDrift =>
|
||||
i4.ReadDatabaseContainer(attachedDatabase)
|
||||
.accessor<i3.UserQueriesDrift>(i3.UserQueriesDrift.new);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue