Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Commit

Permalink
adds optional values argument to Query constructor (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjoeconway authored Sep 26, 2018
1 parent fcc4b1c commit c341701
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions aqueduct/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Fix regression when generating OpenAPI documentation for `ManagedObject`s
- Adds `--resolve-relative-urls` flag to `document` commands to improve client applications
- Adds `Serializable.documentSchema` instance method. Removes `Serializable.document` static method.
- Adds optional `values` argument to `Query` constructor

## 3.0.1

Expand Down
2 changes: 1 addition & 1 deletion aqueduct/lib/src/db/persistent_store/persistent_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class PersistentStore {
/// specific to this type. Objects returned from this method must implement [Query]. They
/// should mixin [QueryMixin] to most of the behavior provided by a query.
Query<T> newQuery<T extends ManagedObject>(
ManagedContext context, ManagedEntity entity);
ManagedContext context, ManagedEntity entity, {T values});

/// Executes an arbitrary command.
Future execute(String sql, {Map<String, dynamic> substitutionValues});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ class PostgreSQLPersistentStore extends PersistentStore

@override
Query<T> newQuery<T extends ManagedObject>(
ManagedContext context, ManagedEntity entity) {
return PostgresQuery<T>.withEntity(context, entity);
ManagedContext context, ManagedEntity entity, {T values}) {
final query = PostgresQuery<T>.withEntity(context, entity);
if (values != null) {
query.values = values;
}
return query;
}

@override
Expand Down
5 changes: 5 additions & 0 deletions aqueduct/lib/src/db/query/mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ abstract class QueryMixin<InstanceType extends ManagedObject>

@override
set values(InstanceType obj) {
if (obj == null) {
_valueObject = null;
return;
}

_valueObject = entity.instanceOf(
backing: ManagedBuilderBacking.from(entity, obj.backing));
}
Expand Down
7 changes: 5 additions & 2 deletions aqueduct/lib/src/db/query/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ abstract class Query<InstanceType extends ManagedObject> {
/// Creates a new [Query].
///
/// The query will be sent to the database described by [context].
factory Query(ManagedContext context) {
/// For insert or update queries, you may provide [values] through this constructor
/// or set the field of the same name later. If set in the constructor,
/// [InstanceType] is inferred.
factory Query(ManagedContext context, {InstanceType values}) {
final entity = context.dataModel.entityForType(InstanceType);
if (entity == null) {
throw ArgumentError(
"Invalid context. The data model of 'context' does not contain '$InstanceType'.");
}

return context.persistentStore.newQuery<InstanceType>(context, entity);
return context.persistentStore.newQuery<InstanceType>(context, entity, values: values);
}

/// Creates a new [Query] without a static type.
Expand Down
24 changes: 24 additions & 0 deletions aqueduct/test/db/postgresql/insert_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ void main() {
expect(q.values.id, 1);
});

test("May set values to null, but the query will fail", () async {
context = await contextWithModels([TestModel]);

final q = Query<TestModel>(context);
q.values = null;

try {
await q.insert();
fail('unreachable');
} on QueryException catch (e) {
expect(e.message, contains("non_null_violation"));
}
});

test("Insert Bad Key", () async {
context = await contextWithModels([TestModel]);

Expand Down Expand Up @@ -298,6 +312,16 @@ void main() {
var result = await q.insert();
expect(result.enumValues, isNull);
});

test("Can infer query from values in constructor", () async {
context = await contextWithModels([TestModel]);

final tm = TestModel()..id = 1..name = "Fred";
final q = Query(context, values: tm);
final t = await q.insert();
expect(t.id, 1);
expect(t.name, "Fred");
});
}

class TestModel extends ManagedObject<_TestModel> implements _TestModel {}
Expand Down
8 changes: 6 additions & 2 deletions aqueduct/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,12 @@ class InMemoryAuthStorage extends AuthServerDelegate {
class DefaultPersistentStore extends PersistentStore {
@override
Query<T> newQuery<T extends ManagedObject>(
ManagedContext context, ManagedEntity entity) {
return _MockQuery<T>.withEntity(context, entity);
ManagedContext context, ManagedEntity entity, {T values}) {
final q = _MockQuery<T>.withEntity(context, entity);
if (values != null) {
q.values = values;
}
return q;
}

@override
Expand Down

0 comments on commit c341701

Please sign in to comment.