all relations work
This commit is contained in:
parent
43501c80b8
commit
21a430717c
6 changed files with 62 additions and 15 deletions
|
@ -1,3 +1,9 @@
|
|||
# 2.0.0-dev.14
|
||||
* Remove obsolete `@belongsToMany`.
|
||||
|
||||
# 2.0.0-dev.13
|
||||
* Push for consistency with orm_gen @ `2.0.0-dev`.
|
||||
|
||||
# 2.0.0-dev.12
|
||||
* Always apply `toSql` escapes.
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ abstract class RelationshipType {
|
|||
static const int hasMany = 0;
|
||||
static const int hasOne = 1;
|
||||
static const int belongsTo = 2;
|
||||
static const int belongsToMany = 3;
|
||||
}
|
||||
|
||||
class Relationship {
|
||||
|
@ -58,14 +57,3 @@ class BelongsTo extends Relationship {
|
|||
}
|
||||
|
||||
const BelongsTo belongsTo = const BelongsTo();
|
||||
|
||||
class BelongsToMany extends Relationship {
|
||||
const BelongsToMany(
|
||||
{String localKey: 'id', String foreignKey, String foreignTable})
|
||||
: super(RelationshipType.belongsToMany,
|
||||
localKey: localKey,
|
||||
foreignKey: foreignKey,
|
||||
foreignTable: foreignTable);
|
||||
}
|
||||
|
||||
const BelongsToMany belongsToMany = const BelongsToMany();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel_orm
|
||||
version: 2.0.0-dev.12
|
||||
version: 2.0.0-dev.14
|
||||
description: Runtime support for Angel's ORM. Includes base classes for queries.
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/orm
|
||||
|
|
|
@ -30,7 +30,7 @@ class PostgresExecutor extends QueryExecutor {
|
|||
query = '$query $returning';
|
||||
}
|
||||
|
||||
print('Running: $query');
|
||||
if (!Platform.environment.containsKey('STFU')) print('Running: $query');
|
||||
return connection.query(query);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
library angel_orm_generator.test;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:angel_orm/angel_orm.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'models/user.dart';
|
||||
|
@ -7,8 +8,56 @@ import 'common.dart';
|
|||
|
||||
main() {
|
||||
QueryExecutor executor;
|
||||
Role canPub, canSub;
|
||||
User thosakwe;
|
||||
|
||||
setUp(() async {
|
||||
executor = await connectToPostgres(['user', 'role', 'user_role']);
|
||||
|
||||
var canPubQuery = new RoleQuery()..values.name = 'can_pub';
|
||||
var canSubQuery = new RoleQuery()..values.name = 'can_sub';
|
||||
canPub = await canPubQuery.insert(executor);
|
||||
canSub = await canSubQuery.insert(executor);
|
||||
|
||||
var thosakweQuery = new UserQuery();
|
||||
thosakweQuery.values
|
||||
..username = 'thosakwe'
|
||||
..password = 'Hahahahayoureallythoughtiwasstupidenoughtotypethishere'
|
||||
..email = 'thosakwe AT gmail.com';
|
||||
thosakwe = await thosakweQuery.insert(executor);
|
||||
|
||||
// Allow thosakwe to publish...
|
||||
var thosakwePubQuery = new UserRoleQuery();
|
||||
thosakwePubQuery.values
|
||||
..userId = int.parse(thosakwe.id)
|
||||
..roleId = int.parse(canPub.id);
|
||||
await thosakwePubQuery.insert(executor);
|
||||
|
||||
// Allow thosakwe to subscribe...
|
||||
var thosakweSubQuery = new UserRoleQuery();
|
||||
thosakweSubQuery.values
|
||||
..userId = int.parse(thosakwe.id)
|
||||
..roleId = int.parse(canSub.id);
|
||||
await thosakweSubQuery.insert(executor);
|
||||
});
|
||||
}
|
||||
|
||||
Future<User> fetchThosakwe() async {
|
||||
var query = new UserQuery()..where.id.equals(int.parse(thosakwe.id));
|
||||
return await query.getOne(executor);
|
||||
}
|
||||
|
||||
test('fetch roles for user', () async {
|
||||
var user = await fetchThosakwe();
|
||||
expect(user.roles, hasLength(2));
|
||||
expect(user.roles, contains(canPub));
|
||||
expect(user.roles, contains(canSub));
|
||||
});
|
||||
|
||||
test('fetch users for role', () async {
|
||||
for (var role in [canPub, canSub]) {
|
||||
var query = new RoleQuery()..where.id.equals(int.parse(role.id));
|
||||
var r = await query.getOne(executor);
|
||||
expect(r.users, [thosakwe]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ abstract class _User extends Model {
|
|||
|
||||
@hasMany
|
||||
List<_UserRole> get userRoles;
|
||||
|
||||
List<_Role> get roles => userRoles.map((m) => m.role).toList();
|
||||
}
|
||||
|
||||
@serializable
|
||||
|
@ -24,6 +26,8 @@ abstract class _Role extends Model {
|
|||
|
||||
@hasMany
|
||||
List<_UserRole> get userRoles;
|
||||
|
||||
List<_User> get users => userRoles.map((m) => m.user).toList();
|
||||
}
|
||||
|
||||
@Serializable(autoIdAndDateFields: false)
|
||||
|
|
Loading…
Reference in a new issue