Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:schultek/stormberry into view_ex…
Browse files Browse the repository at this point in the history
…tends
  • Loading branch information
abitofevrything committed Jan 15, 2023
2 parents 9dd4eb2 + df51e84 commit 5b217db
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 104 deletions.
28 changes: 14 additions & 14 deletions example/lib/model.schema.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'model.dart';

extension Repositories on Database {
extension ModelRepositories on Database {
ARepository get as => ARepository._(this);
BRepository get bs => BRepository._(this);
}
Expand Down Expand Up @@ -92,8 +92,8 @@ class _BRepository extends BaseRepository

var values = QueryValues();
await db.query(
'INSERT INTO "bs" ( "a_id", "id" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.aId)}, ${values.add(r.id)} )').join(', ')}\n',
'INSERT INTO "bs" ( "id", "a_id" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.id)}, ${values.add(r.aId)} )').join(', ')}\n',
values.values,
);
}
Expand All @@ -105,8 +105,8 @@ class _BRepository extends BaseRepository
await db.query(
'UPDATE "bs"\n'
'SET "a_id" = COALESCE(UPDATED."a_id"::text, "bs"."a_id")\n'
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.aId)}, ${values.add(r.id)} )').join(', ')} )\n'
'AS UPDATED("a_id", "id")\n'
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.id)}, ${values.add(r.aId)} )').join(', ')} )\n'
'AS UPDATED("id", "a_id")\n'
'WHERE "bs"."id" = UPDATED."id"',
values.values,
);
Expand All @@ -125,12 +125,12 @@ class AInsertRequest {

class BInsertRequest {
BInsertRequest({
this.aId,
required this.id,
this.aId,
});

String? aId;
String id;
String? aId;
}

class AUpdateRequest {
Expand All @@ -145,12 +145,12 @@ class AUpdateRequest {

class BUpdateRequest {
BUpdateRequest({
this.aId,
required this.id,
this.aId,
});

String? aId;
String id;
String? aId;
}

class AQueryable extends KeyedViewQueryable<A, String> {
Expand All @@ -170,7 +170,7 @@ class AQueryable extends KeyedViewQueryable<A, String> {
String get tableAlias => 'as';

@override
A decode(TypedMap map) => AView(id: map.get('id', TextEncoder.i.decode), b: map.get('b', BQueryable().decoder));
A decode(TypedMap map) => AView(id: map.get('id'), b: map.get('b', BQueryable().decoder));
}

class AView with A {
Expand Down Expand Up @@ -202,17 +202,17 @@ class BQueryable extends KeyedViewQueryable<B, String> {
String get tableAlias => 'bs';

@override
B decode(TypedMap map) => BView(a: map.getOpt('a', AQueryable().decoder), id: map.get('id', TextEncoder.i.decode));
B decode(TypedMap map) => BView(id: map.get('id'), a: map.getOpt('a', AQueryable().decoder));
}

class BView with B {
BView({
this.a,
required this.id,
this.a,
});

@override
final A? a;
@override
final String id;
@override
final A? a;
}
2 changes: 1 addition & 1 deletion example/lib/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'latlng.dart';
import 'party.dart';

part 'account.schema.dart';

//a
@Model(views: [#Full, #User, #Company])
abstract class Account {
@PrimaryKey()
Expand Down
37 changes: 19 additions & 18 deletions example/lib/models/account.schema.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'account.dart';

extension Repositories on Database {
extension AccountRepositories on Database {
AccountRepository get accounts => AccountRepository._(this);
}

Expand Down Expand Up @@ -66,18 +66,19 @@ class _AccountRepository extends BaseRepository

var values = QueryValues();
await db.query(
'INSERT INTO "accounts" ( "id", "first_name", "last_name", "location", "company_id" )\n'
'VALUES ${requests.map((r) => '( ${values.add(autoIncrements[requests.indexOf(r)]['id'])}, ${values.add(r.firstName)}, ${values.add(r.lastName)}, ${values.add(LatLngConverter().tryEncode(r.location))}, ${values.add(r.companyId)} )').join(', ')}\n',
'INSERT INTO "accounts" ( "company_id", "id", "first_name", "last_name", "location" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.companyId)}, ${values.add(autoIncrements[requests.indexOf(r)]['id'])}, ${values.add(r.firstName)}, ${values.add(r.lastName)}, ${values.add(LatLngConverter().tryEncode(r.location))} )').join(', ')}\n',
values.values,
);
await db.billingAddresses.insertMany(requests.where((r) => r.billingAddress != null).map((r) {
return BillingAddressInsertRequest(
accountId: TextEncoder.i.decode(autoIncrements[requests.indexOf(r)]['id']),
companyId: null,
city: r.billingAddress!.city,
postcode: r.billingAddress!.postcode,
name: r.billingAddress!.name,
street: r.billingAddress!.street);
companyId: null,
accountId: TextEncoder.i.decode(autoIncrements[requests.indexOf(r)]['id']),
city: r.billingAddress!.city,
postcode: r.billingAddress!.postcode,
name: r.billingAddress!.name,
street: r.billingAddress!.street,
);
}).toList());

return autoIncrements.map<int>((m) => TextEncoder.i.decode(m['id'])).toList();
Expand All @@ -89,55 +90,55 @@ class _AccountRepository extends BaseRepository
var values = QueryValues();
await db.query(
'UPDATE "accounts"\n'
'SET "first_name" = COALESCE(UPDATED."first_name"::text, "accounts"."first_name"), "last_name" = COALESCE(UPDATED."last_name"::text, "accounts"."last_name"), "location" = COALESCE(UPDATED."location"::point, "accounts"."location"), "company_id" = COALESCE(UPDATED."company_id"::text, "accounts"."company_id")\n'
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.id)}, ${values.add(r.firstName)}, ${values.add(r.lastName)}, ${values.add(LatLngConverter().tryEncode(r.location))}, ${values.add(r.companyId)} )').join(', ')} )\n'
'AS UPDATED("id", "first_name", "last_name", "location", "company_id")\n'
'SET "company_id" = COALESCE(UPDATED."company_id"::text, "accounts"."company_id"), "first_name" = COALESCE(UPDATED."first_name"::text, "accounts"."first_name"), "last_name" = COALESCE(UPDATED."last_name"::text, "accounts"."last_name"), "location" = COALESCE(UPDATED."location"::point, "accounts"."location")\n'
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.companyId)}, ${values.add(r.id)}, ${values.add(r.firstName)}, ${values.add(r.lastName)}, ${values.add(LatLngConverter().tryEncode(r.location))} )').join(', ')} )\n'
'AS UPDATED("company_id", "id", "first_name", "last_name", "location")\n'
'WHERE "accounts"."id" = UPDATED."id"',
values.values,
);
await db.billingAddresses.updateMany(requests.where((r) => r.billingAddress != null).map((r) {
return BillingAddressUpdateRequest(
accountId: r.id,
city: r.billingAddress!.city,
postcode: r.billingAddress!.postcode,
name: r.billingAddress!.name,
street: r.billingAddress!.street);
street: r.billingAddress!.street,
accountId: r.id);
}).toList());
}
}

class AccountInsertRequest {
AccountInsertRequest({
this.companyId,
required this.firstName,
required this.lastName,
required this.location,
this.billingAddress,
this.companyId,
});

String? companyId;
String firstName;
String lastName;
LatLng location;
BillingAddress? billingAddress;
String? companyId;
}

class AccountUpdateRequest {
AccountUpdateRequest({
this.companyId,
required this.id,
this.firstName,
this.lastName,
this.location,
this.billingAddress,
this.companyId,
});

String? companyId;
int id;
String? firstName;
String? lastName;
LatLng? location;
BillingAddress? billingAddress;
String? companyId;
}

class FullAccountViewQueryable extends KeyedViewQueryable<FullAccountView, int> {
Expand Down
25 changes: 11 additions & 14 deletions example/lib/models/address.schema.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'address.dart';

extension Repositories on Database {
extension AddressRepositories on Database {
BillingAddressRepository get billingAddresses => BillingAddressRepository._(this);
}

Expand Down Expand Up @@ -30,8 +30,8 @@ class _BillingAddressRepository extends BaseRepository

var values = QueryValues();
await db.query(
'INSERT INTO "billing_addresses" ( "account_id", "company_id", "city", "postcode", "name", "street" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.accountId)}, ${values.add(r.companyId)}, ${values.add(r.city)}, ${values.add(r.postcode)}, ${values.add(r.name)}, ${values.add(r.street)} )').join(', ')}\n',
'INSERT INTO "billing_addresses" ( "company_id", "account_id", "city", "postcode", "name", "street" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.companyId)}, ${values.add(r.accountId)}, ${values.add(r.city)}, ${values.add(r.postcode)}, ${values.add(r.name)}, ${values.add(r.street)} )').join(', ')}\n',
values.values,
);
}
Expand All @@ -43,26 +43,26 @@ class _BillingAddressRepository extends BaseRepository
await db.query(
'UPDATE "billing_addresses"\n'
'SET "city" = COALESCE(UPDATED."city"::text, "billing_addresses"."city"), "postcode" = COALESCE(UPDATED."postcode"::text, "billing_addresses"."postcode"), "name" = COALESCE(UPDATED."name"::text, "billing_addresses"."name"), "street" = COALESCE(UPDATED."street"::text, "billing_addresses"."street")\n'
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.accountId)}, ${values.add(r.companyId)}, ${values.add(r.city)}, ${values.add(r.postcode)}, ${values.add(r.name)}, ${values.add(r.street)} )').join(', ')} )\n'
'AS UPDATED("account_id", "company_id", "city", "postcode", "name", "street")\n'
'WHERE "billing_addresses"."account_id" = UPDATED."account_id" AND "billing_addresses"."company_id" = UPDATED."company_id"',
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.companyId)}, ${values.add(r.accountId)}, ${values.add(r.city)}, ${values.add(r.postcode)}, ${values.add(r.name)}, ${values.add(r.street)} )').join(', ')} )\n'
'AS UPDATED("company_id", "account_id", "city", "postcode", "name", "street")\n'
'WHERE "billing_addresses"."company_id" = UPDATED."company_id" AND "billing_addresses"."account_id" = UPDATED."account_id"',
values.values,
);
}
}

class BillingAddressInsertRequest {
BillingAddressInsertRequest({
this.accountId,
this.companyId,
this.accountId,
required this.city,
required this.postcode,
required this.name,
required this.street,
});

int? accountId;
String? companyId;
int? accountId;
String city;
String postcode;
String name;
Expand All @@ -71,16 +71,16 @@ class BillingAddressInsertRequest {

class BillingAddressUpdateRequest {
BillingAddressUpdateRequest({
this.accountId,
this.companyId,
this.accountId,
this.city,
this.postcode,
this.name,
this.street,
});

int? accountId;
String? companyId;
int? accountId;
String? city;
String? postcode;
String? name;
Expand All @@ -97,10 +97,7 @@ class BillingAddressQueryable extends ViewQueryable<BillingAddress> {

@override
BillingAddress decode(TypedMap map) => BillingAddressView(
city: map.get('city', TextEncoder.i.decode),
postcode: map.get('postcode', TextEncoder.i.decode),
name: map.get('name', TextEncoder.i.decode),
street: map.get('street', TextEncoder.i.decode));
city: map.get('city'), postcode: map.get('postcode'), name: map.get('name'), street: map.get('street'));
}

class BillingAddressView with BillingAddress {
Expand Down
12 changes: 6 additions & 6 deletions example/lib/models/company.schema.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'company.dart';

extension Repositories on Database {
extension CompanyRepositories on Database {
CompanyRepository get companies => CompanyRepository._(this);
}

Expand Down Expand Up @@ -58,7 +58,7 @@ class _CompanyRepository extends BaseRepository
);
await db.billingAddresses.insertMany(requests.expand((r) {
return r.addresses.map((rr) => BillingAddressInsertRequest(
accountId: null, companyId: r.id, city: rr.city, postcode: rr.postcode, name: rr.name, street: rr.street));
companyId: r.id, accountId: null, city: rr.city, postcode: rr.postcode, name: rr.name, street: rr.street));
}).toList());
}

Expand All @@ -76,7 +76,7 @@ class _CompanyRepository extends BaseRepository
);
await db.billingAddresses.updateMany(requests.where((r) => r.addresses != null).expand((r) {
return r.addresses!.map((rr) => BillingAddressUpdateRequest(
companyId: r.id, city: rr.city, postcode: rr.postcode, name: rr.name, street: rr.street));
city: rr.city, postcode: rr.postcode, name: rr.name, street: rr.street, companyId: r.id));
}).toList());
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ class FullCompanyViewQueryable extends KeyedViewQueryable<FullCompanyView, Strin
invoices: map.getListOpt('invoices', OwnerInvoiceViewQueryable().decoder) ?? const [],
id: map.get('id', TextEncoder.i.decode),
name: map.get('name', TextEncoder.i.decode),
addresses: map.getListOpt('addresses', BillingAddressQueryable().decoder) ?? const []);
addresses: map.getListOpt('addresses', BillingAddressQueryable().decoder) ?? const [],);
}

class FullCompanyView {
Expand Down Expand Up @@ -199,8 +199,8 @@ class MemberCompanyViewQueryable extends KeyedViewQueryable<MemberCompanyView, S

@override
MemberCompanyView decode(TypedMap map) => MemberCompanyView(
id: map.get('id', TextEncoder.i.decode),
name: map.get('name', TextEncoder.i.decode),
id: map.get('id'),
name: map.get('name'),
addresses: map.getListOpt('addresses', BillingAddressQueryable().decoder) ?? const []);
}

Expand Down
8 changes: 3 additions & 5 deletions example/lib/models/invoice.schema.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'invoice.dart';

extension Repositories on Database {
extension InvoiceRepositories on Database {
InvoiceRepository get invoices => InvoiceRepository._(this);
}

Expand Down Expand Up @@ -108,10 +108,8 @@ class OwnerInvoiceViewQueryable extends KeyedViewQueryable<OwnerInvoiceView, Str
String get tableAlias => 'invoices';

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

class OwnerInvoiceView {
Expand Down
Loading

0 comments on commit 5b217db

Please sign in to comment.