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 5b217db + fc8d94e commit 1b1582c
Show file tree
Hide file tree
Showing 53 changed files with 324 additions and 234 deletions.
11 changes: 6 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ Future<void> main() async {
await db.accounts.deleteMany([0, 1, 2]);

var accountId = await db.accounts.insertOne(AccountInsertRequest(
firstName: 'Test',
lastName: 'User',
location: LatLng(1, 2),
billingAddress: BillingAddress(name: 'Test User', street: 'SomeRoad 1', city: 'New York', postcode: '123'),
companyId: 'abc',
firstName: 'Test',
lastName: 'User',
location: LatLng(1, 2),
billingAddress:
BillingAddress(name: 'Test User', street: 'SomeRoad 1', city: 'New York', postcode: '123'),
companyId: 'abc',
));

var account = await db.accounts.queryUserView(accountId);
Expand Down
2 changes: 1 addition & 1 deletion example/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ abstract class B {
String get id;

A? get a;
}
}
10 changes: 8 additions & 2 deletions example/lib/model.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ abstract class ARepository
}

class _ARepository extends BaseRepository
with RepositoryInsertMixin<AInsertRequest>, RepositoryUpdateMixin<AUpdateRequest>, RepositoryDeleteMixin<String>
with
RepositoryInsertMixin<AInsertRequest>,
RepositoryUpdateMixin<AUpdateRequest>,
RepositoryDeleteMixin<String>
implements ARepository {
_ARepository(super.db) : super(tableName: 'as', keyName: 'id');

Expand Down Expand Up @@ -72,7 +75,10 @@ abstract class BRepository
}

class _BRepository extends BaseRepository
with RepositoryInsertMixin<BInsertRequest>, RepositoryUpdateMixin<BUpdateRequest>, RepositoryDeleteMixin<String>
with
RepositoryInsertMixin<BInsertRequest>,
RepositoryUpdateMixin<BUpdateRequest>,
RepositoryDeleteMixin<String>
implements BRepository {
_BRepository(super.db) : super(tableName: 'bs', keyName: 'id');

Expand Down
1 change: 1 addition & 0 deletions example/lib/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'latlng.dart';
import 'party.dart';

part 'account.schema.dart';

//a
@Model(views: [#Full, #User, #Company])
abstract class Account {
Expand Down
43 changes: 21 additions & 22 deletions example/lib/models/account.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,18 @@ class _AccountRepository extends BaseRepository

var values = QueryValues();
await db.query(
'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',
'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',
values.values,
);
await db.billingAddresses.insertMany(requests.where((r) => r.billingAddress != null).map((r) {
return BillingAddressInsertRequest(
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,
);
city: r.billingAddress!.city,
postcode: r.billingAddress!.postcode,
name: r.billingAddress!.name,
street: r.billingAddress!.street,
accountId: TextEncoder.i.decode(autoIncrements[requests.indexOf(r)]['id']),
companyId: null);
}).toList());

return autoIncrements.map<int>((m) => TextEncoder.i.decode(m['id'])).toList();
Expand All @@ -90,9 +89,9 @@ class _AccountRepository extends BaseRepository
var values = QueryValues();
await db.query(
'UPDATE "accounts"\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'
'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'
'WHERE "accounts"."id" = UPDATED."id"',
values.values,
);
Expand All @@ -109,36 +108,36 @@ class _AccountRepository extends BaseRepository

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 Expand Up @@ -178,9 +177,9 @@ class FullAccountViewQueryable extends KeyedViewQueryable<FullAccountView, int>

@override
FullAccountView decode(TypedMap map) => FullAccountView(
id: map.get('id', TextEncoder.i.decode),
firstName: map.get('first_name', TextEncoder.i.decode),
lastName: map.get('last_name', TextEncoder.i.decode),
id: map.get('id'),
firstName: map.get('first_name'),
lastName: map.get('last_name'),
location: map.get('location', LatLngConverter().decode),
billingAddress: map.getOpt('billingAddress', BillingAddressQueryable().decoder),
invoices: map.getListOpt('invoices', OwnerInvoiceViewQueryable().decoder) ?? const [],
Expand Down Expand Up @@ -247,9 +246,9 @@ class UserAccountViewQueryable extends KeyedViewQueryable<UserAccountView, int>

@override
UserAccountView decode(TypedMap map) => UserAccountView(
id: map.get('id', TextEncoder.i.decode),
firstName: map.get('first_name', TextEncoder.i.decode),
lastName: map.get('last_name', TextEncoder.i.decode),
id: map.get('id'),
firstName: map.get('first_name'),
lastName: map.get('last_name'),
location: map.get('location', LatLngConverter().decode),
billingAddress: map.getOpt('billingAddress', BillingAddressQueryable().decoder),
invoices: map.getListOpt('invoices', OwnerInvoiceViewQueryable().decoder) ?? const [],
Expand Down
35 changes: 20 additions & 15 deletions example/lib/models/address.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ abstract class BillingAddressRepository
}

class _BillingAddressRepository extends BaseRepository
with RepositoryInsertMixin<BillingAddressInsertRequest>, RepositoryUpdateMixin<BillingAddressUpdateRequest>
with
RepositoryInsertMixin<BillingAddressInsertRequest>,
RepositoryUpdateMixin<BillingAddressUpdateRequest>
implements BillingAddressRepository {
_BillingAddressRepository(super.db) : super(tableName: 'billing_addresses');

Expand All @@ -30,8 +32,8 @@ class _BillingAddressRepository extends BaseRepository

var values = QueryValues();
await db.query(
'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',
'INSERT INTO "billing_addresses" ( "city", "postcode", "name", "street", "account_id", "company_id" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.city)}, ${values.add(r.postcode)}, ${values.add(r.name)}, ${values.add(r.street)}, ${values.add(r.accountId)}, ${values.add(r.companyId)} )').join(', ')}\n',
values.values,
);
}
Expand All @@ -43,48 +45,48 @@ 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.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"',
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.city)}, ${values.add(r.postcode)}, ${values.add(r.name)}, ${values.add(r.street)}, ${values.add(r.accountId)}, ${values.add(r.companyId)} )').join(', ')} )\n'
'AS UPDATED("city", "postcode", "name", "street", "account_id", "company_id")\n'
'WHERE "billing_addresses"."account_id" = UPDATED."account_id" AND "billing_addresses"."company_id" = UPDATED."company_id"',
values.values,
);
}
}

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

String? companyId;
int? accountId;
String city;
String postcode;
String name;
String street;
int? accountId;
String? companyId;
}

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

String? companyId;
int? accountId;
String? city;
String? postcode;
String? name;
String? street;
int? accountId;
String? companyId;
}

class BillingAddressQueryable extends ViewQueryable<BillingAddress> {
Expand All @@ -97,7 +99,10 @@ class BillingAddressQueryable extends ViewQueryable<BillingAddress> {

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

class BillingAddressView with BillingAddress {
Expand Down
3 changes: 1 addition & 2 deletions example/lib/models/company.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ part 'company.schema.dart';

@Model(views: [#Full, #Member])
abstract class Company {

@PrimaryKey()
String get id;

Expand All @@ -28,4 +27,4 @@ abstract class Company {
@HiddenIn(#Member)
@ViewedIn(#Full, as: #Company)
List<Party> get parties;
}
}
10 changes: 5 additions & 5 deletions example/lib/models/company.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class _CompanyRepository extends BaseRepository
);
await db.billingAddresses.insertMany(requests.expand((r) {
return r.addresses.map((rr) => BillingAddressInsertRequest(
companyId: r.id, accountId: null, city: rr.city, postcode: rr.postcode, name: rr.name, street: rr.street));
city: rr.city, postcode: rr.postcode, name: rr.name, street: rr.street, accountId: null, companyId: r.id));
}).toList());
}

Expand Down Expand Up @@ -150,12 +150,12 @@ class FullCompanyViewQueryable extends KeyedViewQueryable<FullCompanyView, Strin

@override
FullCompanyView decode(TypedMap map) => FullCompanyView(
id: map.get('id'),
name: map.get('name'),
addresses: map.getListOpt('addresses', BillingAddressQueryable().decoder) ?? const [],
members: map.getListOpt('members', CompanyAccountViewQueryable().decoder) ?? const [],
parties: map.getListOpt('parties', CompanyPartyViewQueryable().decoder) ?? const [],
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 [],);
parties: map.getListOpt('parties', CompanyPartyViewQueryable().decoder) ?? const []);
}

class FullCompanyView {
Expand Down
3 changes: 1 addition & 2 deletions example/lib/models/invoice.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:stormberry/stormberry.dart';

import 'account.dart';
Expand All @@ -18,4 +17,4 @@ abstract class Invoice {

@HiddenIn(#Owner)
Company? get company;
}
}
18 changes: 9 additions & 9 deletions example/lib/models/invoice.schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class _InvoiceRepository extends BaseRepository

var values = QueryValues();
await db.query(
'INSERT INTO "invoices" ( "account_id", "id", "title", "invoice_id", "company_id" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.accountId)}, ${values.add(r.id)}, ${values.add(r.title)}, ${values.add(r.invoiceId)}, ${values.add(r.companyId)} )').join(', ')}\n',
'INSERT INTO "invoices" ( "id", "title", "invoice_id", "account_id", "company_id" )\n'
'VALUES ${requests.map((r) => '( ${values.add(r.id)}, ${values.add(r.title)}, ${values.add(r.invoiceId)}, ${values.add(r.accountId)}, ${values.add(r.companyId)} )').join(', ')}\n',
values.values,
);
}
Expand All @@ -52,9 +52,9 @@ class _InvoiceRepository extends BaseRepository
var values = QueryValues();
await db.query(
'UPDATE "invoices"\n'
'SET "account_id" = COALESCE(UPDATED."account_id"::int8, "invoices"."account_id"), "title" = COALESCE(UPDATED."title"::text, "invoices"."title"), "invoice_id" = COALESCE(UPDATED."invoice_id"::text, "invoices"."invoice_id"), "company_id" = COALESCE(UPDATED."company_id"::text, "invoices"."company_id")\n'
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.accountId)}, ${values.add(r.id)}, ${values.add(r.title)}, ${values.add(r.invoiceId)}, ${values.add(r.companyId)} )').join(', ')} )\n'
'AS UPDATED("account_id", "id", "title", "invoice_id", "company_id")\n'
'SET "title" = COALESCE(UPDATED."title"::text, "invoices"."title"), "invoice_id" = COALESCE(UPDATED."invoice_id"::text, "invoices"."invoice_id"), "account_id" = COALESCE(UPDATED."account_id"::int8, "invoices"."account_id"), "company_id" = COALESCE(UPDATED."company_id"::text, "invoices"."company_id")\n'
'FROM ( VALUES ${requests.map((r) => '( ${values.add(r.id)}, ${values.add(r.title)}, ${values.add(r.invoiceId)}, ${values.add(r.accountId)}, ${values.add(r.companyId)} )').join(', ')} )\n'
'AS UPDATED("id", "title", "invoice_id", "account_id", "company_id")\n'
'WHERE "invoices"."id" = UPDATED."id"',
values.values,
);
Expand All @@ -63,33 +63,33 @@ class _InvoiceRepository extends BaseRepository

class InvoiceInsertRequest {
InvoiceInsertRequest({
this.accountId,
required this.id,
required this.title,
required this.invoiceId,
this.accountId,
this.companyId,
});

int? accountId;
String id;
String title;
String invoiceId;
int? accountId;
String? companyId;
}

class InvoiceUpdateRequest {
InvoiceUpdateRequest({
this.accountId,
required this.id,
this.title,
this.invoiceId,
this.accountId,
this.companyId,
});

int? accountId;
String id;
String? title;
String? invoiceId;
int? accountId;
String? companyId;
}

Expand Down
1 change: 0 additions & 1 deletion example/lib/models/latlng.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:stormberry/stormberry.dart';

class LatLng {
Expand Down
Loading

0 comments on commit 1b1582c

Please sign in to comment.