Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use extends instead of implements for view classes #41

Merged
merged 5 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/lib/model.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class AQueryable extends KeyedViewQueryable<A, String> {
A decode(TypedMap map) => AView(id: map.get('id'), b: map.get('b', BQueryable().decoder));
}

class AView implements A {
class AView with A {
AView({
required this.id,
required this.b,
Expand Down Expand Up @@ -211,7 +211,7 @@ class BQueryable extends KeyedViewQueryable<B, String> {
B decode(TypedMap map) => BView(id: map.get('id'), a: map.getOpt('a', AQueryable().decoder));
}

class BView implements B {
class BView with B {
BView({
required this.id,
this.a,
Expand Down
9 changes: 4 additions & 5 deletions example/lib/models/account.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class _AccountRepository extends BaseRepository
@override
Future<List<int>> insert(List<AccountInsertRequest> requests) async {
if (requests.isEmpty) return [];
var rows = await db.query(
requests.map((r) => "SELECT nextval('accounts_id_seq') as \"id\"").join('\nUNION ALL\n'));
var rows = await db.query(requests.map((r) => "SELECT nextval('accounts_id_seq') as \"id\"").join('\nUNION ALL\n'));
var autoIncrements = rows.map((r) => r.toColumnMap()).toList();

var values = QueryValues();
Expand Down Expand Up @@ -305,9 +304,9 @@ class CompanyAccountViewQueryable extends KeyedViewQueryable<CompanyAccountView,

@override
CompanyAccountView decode(TypedMap map) => CompanyAccountView(
id: map.get('id'),
firstName: map.get('first_name'),
lastName: map.get('last_name'),
id: map.get('id', TextEncoder.i.decode),
firstName: map.get('first_name', TextEncoder.i.decode),
lastName: map.get('last_name', TextEncoder.i.decode),
location: map.get('location', LatLngConverter().decode),
parties: map.getListOpt('parties', CompanyPartyViewQueryable().decoder) ?? const []);
}
Expand Down
2 changes: 1 addition & 1 deletion example/lib/models/address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Address {
}

@Model()
abstract class BillingAddress extends Address {
abstract class BillingAddress implements Address {
String get city;
String get postcode;

Expand Down
2 changes: 1 addition & 1 deletion example/lib/models/address.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class BillingAddressQueryable extends ViewQueryable<BillingAddress> {
street: map.get('street'));
}

class BillingAddressView implements BillingAddress {
class BillingAddressView with BillingAddress {
BillingAddressView({
required this.city,
required this.postcode,
Expand Down
47 changes: 21 additions & 26 deletions example/lib/models/company.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ class _CompanyRepository extends BaseRepository
);
await db.billingAddresses.insertMany(requests.expand((r) {
return r.addresses.map((rr) => BillingAddressInsertRequest(
city: rr.city,
postcode: rr.postcode,
name: rr.name,
street: rr.street,
accountId: null,
companyId: r.id));
city: rr.city, postcode: rr.postcode, name: rr.name, street: rr.street, accountId: null, companyId: r.id));
}).toList());
}

Expand Down Expand Up @@ -119,36 +114,36 @@ class FullCompanyViewQueryable extends KeyedViewQueryable<FullCompanyView, Strin

@override
String get query =>
'SELECT "companies".*, "addresses"."data" as "addresses", "members"."data" as "members", "invoices"."data" as "invoices", "parties"."data" as "parties"'
'SELECT "companies".*, "members"."data" as "members", "parties"."data" as "parties", "invoices"."data" as "invoices", "addresses"."data" as "addresses"'
'FROM "companies"'
'LEFT JOIN ('
' SELECT "billing_addresses"."company_id",'
' to_jsonb(array_agg("billing_addresses".*)) as data'
' FROM (${BillingAddressQueryable().query}) "billing_addresses"'
' GROUP BY "billing_addresses"."company_id"'
') "addresses"'
'ON "companies"."id" = "addresses"."company_id"'
'LEFT JOIN ('
' SELECT "accounts"."company_id",'
' to_jsonb(array_agg("accounts".*)) as data'
' FROM (${CompanyAccountViewQueryable().query}) "accounts"'
' GROUP BY "accounts"."company_id"'
') "members"'
'ON "companies"."id" = "members"."company_id"'
'LEFT JOIN ('
' SELECT "parties"."sponsor_id",'
' to_jsonb(array_agg("parties".*)) as data'
' FROM (${CompanyPartyViewQueryable().query}) "parties"'
' GROUP BY "parties"."sponsor_id"'
') "parties"'
'ON "companies"."id" = "parties"."sponsor_id"'
'LEFT JOIN ('
' SELECT "invoices"."company_id",'
' to_jsonb(array_agg("invoices".*)) as data'
' FROM (${OwnerInvoiceViewQueryable().query}) "invoices"'
' GROUP BY "invoices"."company_id"'
') "invoices"'
'ON "companies"."id" = "invoices"."company_id"'
'LEFT JOIN ('
' SELECT "parties"."sponsor_id",'
' to_jsonb(array_agg("parties".*)) as data'
' FROM (${CompanyPartyViewQueryable().query}) "parties"'
' GROUP BY "parties"."sponsor_id"'
') "parties"'
'ON "companies"."id" = "parties"."sponsor_id"';
' SELECT "billing_addresses"."company_id",'
' to_jsonb(array_agg("billing_addresses".*)) as data'
' FROM (${BillingAddressQueryable().query}) "billing_addresses"'
' GROUP BY "billing_addresses"."company_id"'
') "addresses"'
'ON "companies"."id" = "addresses"."company_id"';

@override
String get tableAlias => 'companies';
Expand All @@ -165,20 +160,20 @@ class FullCompanyViewQueryable extends KeyedViewQueryable<FullCompanyView, Strin

class FullCompanyView {
FullCompanyView({
required this.members,
required this.parties,
required this.invoices,
required this.id,
required this.name,
required this.addresses,
required this.members,
required this.invoices,
required this.parties,
});

final List<CompanyAccountView> members;
final List<CompanyPartyView> parties;
final List<OwnerInvoiceView> invoices;
final String id;
final String name;
final List<BillingAddress> addresses;
final List<CompanyAccountView> members;
final List<OwnerInvoiceView> invoices;
final List<CompanyPartyView> parties;
}

class MemberCompanyViewQueryable extends KeyedViewQueryable<MemberCompanyView, String> {
Expand Down
4 changes: 2 additions & 2 deletions example/lib/models/invoice.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class OwnerInvoiceViewQueryable extends KeyedViewQueryable<OwnerInvoiceView, Str
String get tableAlias => 'invoices';

@override
OwnerInvoiceView decode(TypedMap map) => OwnerInvoiceView(
id: map.get('id'), title: map.get('title'), invoiceId: map.get('invoice_id'));
OwnerInvoiceView decode(TypedMap map) =>
OwnerInvoiceView(id: map.get('id'), title: map.get('title'), invoiceId: map.get('invoice_id'));
}

class OwnerInvoiceView {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/builder/generators/view_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ViewGenerator {
}

${view.table.annotateWith ?? ''}
class ${view.className}${implementsBase ? ' implements ${view.table.element.name}' : ''} {
class ${view.className}${implementsBase ? ' with ${view.table.element.name}' : ''} {
${view.className}(${view.columns.isEmpty ? '' : '{${view.columns.map((c) => '${c.isNullable ? '' : 'required '}this.${c.paramName}').join(', ')},}'});

${view.columns.map((c) => '${implementsBase ? '@override ' : ''}final ${c.dartType} ${c.paramName};').join('\n')}
Expand Down