Skip to content

Commit

Permalink
non-null-refs getter for non-null references
Browse files Browse the repository at this point in the history
  • Loading branch information
dickermoshe committed Oct 27, 2024
1 parent 6d31b37 commit 15994a3
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 50 deletions.
2 changes: 1 addition & 1 deletion drift/example/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion drift/test/generated/todos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Product extends Table {
class Listing extends Table {
IntColumn get id => integer().autoIncrement()();
@ReferenceName('listings')
TextColumn get product => text().references(Product, #sku).nullable()();
TextColumn get product => text().references(Product, #sku)();
@ReferenceName('listings')
IntColumn get store => integer().references(Store, #id).nullable()();
RealColumn get price => real().nullable()();
Expand Down
47 changes: 23 additions & 24 deletions drift/test/generated/todos.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions drift/test/manager/manager_computed_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ void main() {

listings = await _listingsData.mapAsyncAndAwait(
(p0) => db.managers.listing.createReturning((o) => o(
product: Value(p0.product),
store: Value(p0.store),
price: Value(p0.price))),
product: p0.product, store: Value(p0.store), price: Value(p0.price))),
);
});

Expand Down
8 changes: 2 additions & 6 deletions drift/test/manager/manager_prefetch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ void main() {
await db.managers.listing.bulkCreate(
(o) {
return listingsData.map((e) => o(
product: Value(e.product),
store: Value(e.store),
price: Value(e.price)));
product: e.product, store: Value(e.store), price: Value(e.price)));
},
);

Expand Down Expand Up @@ -163,9 +161,7 @@ void main() {
await db.managers.listing.bulkCreate(
(o) {
return listingsData.map((e) => o(
product: Value(e.product),
store: Value(e.store),
price: Value(e.price)));
product: e.product, store: Value(e.store), price: Value(e.price)));
},
);

Expand Down
17 changes: 7 additions & 10 deletions drift/test/manager/manager_referenced_filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ void main() {

listings = await _listingsData.mapAsyncAndAwait(
(p0) => db.managers.listing.createReturning((o) => o(
product: Value(p0.product),
store: Value(p0.store),
price: Value(p0.price))),
product: p0.product, store: Value(p0.store), price: Value(p0.price))),
);

final categories =
Expand Down Expand Up @@ -582,13 +580,12 @@ void main() {
final listingsWithProducts = <ProductData, List<ListingData>>{};
for (final i
in await db.managers.listing.withReferences().get(distinct: true)) {
final product = await i.$2.product?.getSingle();
if (product != null) {
if (!listingsWithProducts.containsKey(product)) {
listingsWithProducts[product] = [i.$1];
} else {
listingsWithProducts[product]!.add(i.$1);
}
final product = await i.$2.product.getSingle();

if (!listingsWithProducts.containsKey(product)) {
listingsWithProducts[product] = [i.$1];
} else {
listingsWithProducts[product]!.add(i.$1);
}
}
expect(listingsWithProducts.length, 9);
Expand Down
4 changes: 1 addition & 3 deletions drift/test/manager/manager_selectable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ void main() {

final listings = await _listingsData.mapAsyncAndAwait(
(p0) => db.managers.listing.createReturning((o) => o(
product: Value(p0.product),
store: Value(p0.store),
price: Value(p0.price))),
product: p0.product, store: Value(p0.store), price: Value(p0.price))),
);

final getAllStores = db.managers.store;
Expand Down
2 changes: 1 addition & 1 deletion drift_dev/lib/src/writer/manager/manager_templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ class _ManagerCodeTemplates {
return """
$aliasedTableMethod
${processedTableManagerTypedefName(relation.referencedTable)}? get ${relation.fieldName} {
${processedTableManagerTypedefName(relation.referencedTable)}${relation.currentColumn.nullable ? "?" : ""} get ${relation.fieldName} {
if (\$_item.${relation.currentColumn.nameInDart} == null) return null;
final manager = ${rootTableManagerWithPrefix(relation.referencedTable, leaf)}(\$_db, \$_db.${relation.referencedTable.dbGetterName}).filter((f) => f.${relation.referencedColumn.nameInDart}(\$_item.${relation.currentColumn.nameInDart}!));
final item = \$_typedResult.readTableOrNull(_${relation.fieldName}Table(\$_db));
Expand Down
2 changes: 1 addition & 1 deletion examples/migrations_example/lib/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 15994a3

Please sign in to comment.