From c5286ea623fbb5299583c8a0a7fc0064e66c1aaa Mon Sep 17 00:00:00 2001 From: thomashii Date: Sun, 2 Jan 2022 15:27:06 +0800 Subject: [PATCH] Removed Test Folder --- .../orm/orm_builder/analysis_options.yaml | 1 - .../orm_builder/lib/src/belongs_to_test.dart | 175 --- .../orm/orm_builder/lib/src/models/book.dart | 28 - .../orm_builder/lib/src/models/book.g.dart | 617 ---------- .../orm/orm_builder/lib/src/models/user.dart | 38 - .../orm_builder/lib/src/models/user.g.dart | 1002 ----------------- packages/orm/orm_builder/lib/src/util.dart | 12 - packages/orm/orm_builder/pubspec.yaml | 31 - 8 files changed, 1904 deletions(-) delete mode 100644 packages/orm/orm_builder/analysis_options.yaml delete mode 100644 packages/orm/orm_builder/lib/src/belongs_to_test.dart delete mode 100644 packages/orm/orm_builder/lib/src/models/book.dart delete mode 100644 packages/orm/orm_builder/lib/src/models/book.g.dart delete mode 100644 packages/orm/orm_builder/lib/src/models/user.dart delete mode 100644 packages/orm/orm_builder/lib/src/models/user.g.dart delete mode 100644 packages/orm/orm_builder/lib/src/util.dart delete mode 100644 packages/orm/orm_builder/pubspec.yaml diff --git a/packages/orm/orm_builder/analysis_options.yaml b/packages/orm/orm_builder/analysis_options.yaml deleted file mode 100644 index ea2c9e94..00000000 --- a/packages/orm/orm_builder/analysis_options.yaml +++ /dev/null @@ -1 +0,0 @@ -include: package:lints/recommended.yaml \ No newline at end of file diff --git a/packages/orm/orm_builder/lib/src/belongs_to_test.dart b/packages/orm/orm_builder/lib/src/belongs_to_test.dart deleted file mode 100644 index 73d1fd52..00000000 --- a/packages/orm/orm_builder/lib/src/belongs_to_test.dart +++ /dev/null @@ -1,175 +0,0 @@ -import 'dart:async'; -import 'package:angel3_orm/angel3_orm.dart'; -import 'package:test/test.dart'; -import 'models/book.dart'; - -import 'util.dart'; - -void belongsToTests(FutureOr Function() createExecutor, - {FutureOr Function(QueryExecutor)? close}) { - late QueryExecutor executor; - Author? jkRowling; - Author? jameson; - Book? deathlyHallows; - close ??= (_) => null; - - setUp(() async { - executor = await createExecutor(); - - // Insert an author - var query = AuthorQuery()..values.name = 'J.K. Rowling'; - jkRowling = (await query.insert(executor)).value; - - query.values.name = 'J.K. Jameson'; - jameson = (await query.insert(executor)).value; - - // And a book - var bookQuery = BookQuery(); - bookQuery.values - ..authorId = jkRowling!.idAsInt - ..partnerAuthorId = jameson!.idAsInt - ..name = 'Deathly Hallows'; - - deathlyHallows = (await bookQuery.insert(executor)).value; - }); - - tearDown(() => close!(executor)); - - group('selects', () { - test('select all', () async { - var query = BookQuery(); - var books = await query.get(executor); - expect(books, hasLength(1)); - - var book = books.first; - //print(book.toJson()); - expect(book.id, deathlyHallows!.id); - expect(book.name, deathlyHallows!.name); - - var author = book.author!; - //print(AuthorSerializer.toMap(author)); - expect(author.id, jkRowling!.id); - expect(author.name, jkRowling!.name); - }); - - test('select one', () async { - var query = BookQuery(); - query.where!.id.equals(int.parse(deathlyHallows!.id!)); - //print(query.compile({})); - - var bookOpt = await query.getOne(executor); - expect(bookOpt.isPresent, true); - bookOpt.ifPresent((book) { - //print(book.toJson()); - expect(book.id, deathlyHallows!.id); - expect(book.name, deathlyHallows!.name); - - var author = book.author!; - //print(AuthorSerializer.toMap(author)); - expect(author.id, jkRowling!.id); - expect(author.name, jkRowling!.name); - }); - }); - - test('where clause', () async { - var query = BookQuery() - ..where!.name.equals('Goblet of Fire') - ..orWhere((w) => w.authorId.equals(jkRowling!.idAsInt)); - //print(query.compile({})); - - var books = await query.get(executor); - expect(books, hasLength(1)); - - var book = books.first; - //print(book.toJson()); - expect(book.id, deathlyHallows!.id); - expect(book.name, deathlyHallows!.name); - - var author = book.author!; - //print(AuthorSerializer.toMap(author)); - expect(author.id, jkRowling!.id); - expect(author.name, jkRowling!.name); - }); - - test('union', () async { - var query1 = BookQuery()..where!.name.like('Deathly%'); - var query2 = BookQuery()..where!.authorId.equals(-1); - var query3 = BookQuery() - ..where!.name.isIn(['Goblet of Fire', 'Order of the Phoenix']); - query1 - ..union(query2) - ..unionAll(query3); - //print(query1.compile({})); - - var books = await query1.get(executor); - expect(books, hasLength(1)); - - var book = books.first; - //print(book.toJson()); - expect(book.id, deathlyHallows!.id); - expect(book.name, deathlyHallows!.name); - - var author = book.author!; - //print(AuthorSerializer.toMap(author)); - expect(author.id, jkRowling!.id); - expect(author.name, jkRowling!.name); - }); - - test('order by', () async { - var query = AuthorQuery()..orderBy(AuthorFields.name, descending: true); - var authors = await query.get(executor); - expect(authors, [jkRowling, jameson]); - }); - }); - - test('insert sets relationship', () { - expect(deathlyHallows!.author, jkRowling); - //expect(deathlyHallows.author, isNotNull); - //expect(deathlyHallows.author.name, rowling.name); - }); - - test('delete stream', () async { - //printSeparator('Delete stream test'); - var query = BookQuery()..where!.name.equals(deathlyHallows!.name!); - //print(query.compile({}, preamble: 'DELETE', withFields: false)); - var books = await query.delete(executor); - expect(books, hasLength(1)); - - var book = books.first; - expect(book.id, deathlyHallows?.id); - expect(book.author, isNotNull); - expect(book.author!.name, jkRowling!.name); - }); - - test('update book', () async { - var cloned = deathlyHallows!.copyWith(name: "Sorcerer's Stone"); - var query = BookQuery() - ..where?.id.equals(int.parse(cloned.id!)) - ..values.copyFrom(cloned); - var bookOpt = await (query.updateOne(executor)); - expect(bookOpt.isPresent, true); - bookOpt.ifPresent((book) { - //print(book.toJson()); - expect(book.name, cloned.name); - expect(book.author, isNotNull); - expect(book.author!.name, jkRowling!.name); - }); - }); - - group('joined subquery', () { - // To verify that the joined subquery is correct, - // we test both a query that return empty, and one - // that should return correctly. - test('returns empty on false subquery', () async { - printSeparator('False subquery test'); - var query = BookQuery()..author.where!.name.equals('Billie Jean'); - expect(await query.get(executor), isEmpty); - }); - - test('returns values on true subquery', () async { - printSeparator('True subquery test'); - var query = BookQuery()..author.where!.name.like('%Rowling%'); - expect(await query.get(executor), [deathlyHallows]); - }); - }); -} diff --git a/packages/orm/orm_builder/lib/src/models/book.dart b/packages/orm/orm_builder/lib/src/models/book.dart deleted file mode 100644 index 30a0c5c1..00000000 --- a/packages/orm/orm_builder/lib/src/models/book.dart +++ /dev/null @@ -1,28 +0,0 @@ -library angel_orm3.generator.models.book; - -import 'package:angel3_migration/angel3_migration.dart'; -import 'package:angel3_orm/angel3_orm.dart'; -import 'package:angel3_serialize/angel3_serialize.dart'; -import 'package:optional/optional.dart'; - -part 'book.g.dart'; - -@serializable -@orm -class _Book extends Model { - @BelongsTo(joinType: JoinType.inner) - _Author? author; - - @BelongsTo(localKey: 'partner_author_id', joinType: JoinType.inner) - _Author? partnerAuthor; - - String? name; -} - -@serializable -@orm -abstract class _Author extends Model { - @Column(length: 255, indexType: IndexType.unique) - @SerializableField(defaultValue: 'Tobe Osakwe') - String? get name; -} diff --git a/packages/orm/orm_builder/lib/src/models/book.g.dart b/packages/orm/orm_builder/lib/src/models/book.g.dart deleted file mode 100644 index 27c20d09..00000000 --- a/packages/orm/orm_builder/lib/src/models/book.g.dart +++ /dev/null @@ -1,617 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of angel_orm3.generator.models.book; - -// ************************************************************************** -// MigrationGenerator -// ************************************************************************** - -class BookMigration extends Migration { - @override - void up(Schema schema) { - schema.create('books', (table) { - table.serial('id').primaryKey(); - table.timeStamp('created_at'); - table.timeStamp('updated_at'); - table.varChar('name', length: 255); - table.declare('author_id', ColumnType('int')).references('authors', 'id'); - table - .declare('partner_author_id', ColumnType('int')) - .references('authors', 'id'); - }); - } - - @override - void down(Schema schema) { - schema.drop('books'); - } -} - -class AuthorMigration extends Migration { - @override - void up(Schema schema) { - schema.create('authors', (table) { - table.serial('id').primaryKey(); - table.timeStamp('created_at'); - table.timeStamp('updated_at'); - table.varChar('name', length: 255) - ..defaultsTo('Tobe Osakwe') - ..unique(); - }); - } - - @override - void down(Schema schema) { - schema.drop('authors'); - } -} - -// ************************************************************************** -// OrmGenerator -// ************************************************************************** - -class BookQuery extends Query { - BookQuery({Query? parent, Set? trampoline}) : super(parent: parent) { - trampoline ??= {}; - trampoline.add(tableName); - _where = BookQueryWhere(this); - join(_author = AuthorQuery(trampoline: trampoline, parent: this), - 'author_id', 'id', - additionalFields: const ['id', 'created_at', 'updated_at', 'name'], - trampoline: trampoline); - join(_partnerAuthor = AuthorQuery(trampoline: trampoline, parent: this), - 'partner_author_id', 'id', - additionalFields: const ['id', 'created_at', 'updated_at', 'name'], - trampoline: trampoline); - } - - @override - final BookQueryValues values = BookQueryValues(); - - BookQueryWhere? _where; - - late AuthorQuery _author; - - late AuthorQuery _partnerAuthor; - - @override - Map get casts { - return {}; - } - - @override - String get tableName { - return 'books'; - } - - @override - List get fields { - return const [ - 'id', - 'created_at', - 'updated_at', - 'author_id', - 'partner_author_id', - 'name' - ]; - } - - @override - BookQueryWhere? get where { - return _where; - } - - @override - BookQueryWhere newWhereClause() { - return BookQueryWhere(this); - } - - static Optional parseRow(List row) { - if (row.every((x) => x == null)) { - return Optional.empty(); - } - var model = Book( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - name: (row[5] as String?)); - if (row.length > 6) { - var modelOpt = AuthorQuery.parseRow(row.skip(6).take(4).toList()); - modelOpt.ifPresent((m) { - model = model.copyWith(author: m); - }); - } - if (row.length > 10) { - var modelOpt = AuthorQuery.parseRow(row.skip(10).take(4).toList()); - modelOpt.ifPresent((m) { - model = model.copyWith(partnerAuthor: m); - }); - } - return Optional.of(model); - } - - @override - Optional deserialize(List row) { - return parseRow(row); - } - - AuthorQuery get author { - return _author; - } - - AuthorQuery get partnerAuthor { - return _partnerAuthor; - } -} - -class BookQueryWhere extends QueryWhere { - BookQueryWhere(BookQuery query) - : id = NumericSqlExpressionBuilder(query, 'id'), - createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), - updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), - authorId = NumericSqlExpressionBuilder(query, 'author_id'), - partnerAuthorId = - NumericSqlExpressionBuilder(query, 'partner_author_id'), - name = StringSqlExpressionBuilder(query, 'name'); - - final NumericSqlExpressionBuilder id; - - final DateTimeSqlExpressionBuilder createdAt; - - final DateTimeSqlExpressionBuilder updatedAt; - - final NumericSqlExpressionBuilder authorId; - - final NumericSqlExpressionBuilder partnerAuthorId; - - final StringSqlExpressionBuilder name; - - @override - List get expressionBuilders { - return [id, createdAt, updatedAt, authorId, partnerAuthorId, name]; - } -} - -class BookQueryValues extends MapQueryValues { - @override - Map get casts { - return {}; - } - - String? get id { - return (values['id'] as String?); - } - - set id(String? value) => values['id'] = value; - DateTime? get createdAt { - return (values['created_at'] as DateTime?); - } - - set createdAt(DateTime? value) => values['created_at'] = value; - DateTime? get updatedAt { - return (values['updated_at'] as DateTime?); - } - - set updatedAt(DateTime? value) => values['updated_at'] = value; - int get authorId { - return (values['author_id'] as int); - } - - set authorId(int value) => values['author_id'] = value; - int get partnerAuthorId { - return (values['partner_author_id'] as int); - } - - set partnerAuthorId(int value) => values['partner_author_id'] = value; - String? get name { - return (values['name'] as String?); - } - - set name(String? value) => values['name'] = value; - void copyFrom(Book model) { - createdAt = model.createdAt; - updatedAt = model.updatedAt; - name = model.name; - if (model.author != null) { - values['author_id'] = model.author?.id; - } - if (model.partnerAuthor != null) { - values['partner_author_id'] = model.partnerAuthor?.id; - } - } -} - -class AuthorQuery extends Query { - AuthorQuery({Query? parent, Set? trampoline}) - : super(parent: parent) { - trampoline ??= {}; - trampoline.add(tableName); - _where = AuthorQueryWhere(this); - } - - @override - final AuthorQueryValues values = AuthorQueryValues(); - - AuthorQueryWhere? _where; - - @override - Map get casts { - return {}; - } - - @override - String get tableName { - return 'authors'; - } - - @override - List get fields { - return const ['id', 'created_at', 'updated_at', 'name']; - } - - @override - AuthorQueryWhere? get where { - return _where; - } - - @override - AuthorQueryWhere newWhereClause() { - return AuthorQueryWhere(this); - } - - static Optional parseRow(List row) { - if (row.every((x) => x == null)) { - return Optional.empty(); - } - var model = Author( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - name: (row[3] as String?)); - return Optional.of(model); - } - - @override - Optional deserialize(List row) { - return parseRow(row); - } -} - -class AuthorQueryWhere extends QueryWhere { - AuthorQueryWhere(AuthorQuery query) - : id = NumericSqlExpressionBuilder(query, 'id'), - createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), - updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), - name = StringSqlExpressionBuilder(query, 'name'); - - final NumericSqlExpressionBuilder id; - - final DateTimeSqlExpressionBuilder createdAt; - - final DateTimeSqlExpressionBuilder updatedAt; - - final StringSqlExpressionBuilder name; - - @override - List get expressionBuilders { - return [id, createdAt, updatedAt, name]; - } -} - -class AuthorQueryValues extends MapQueryValues { - @override - Map get casts { - return {}; - } - - String? get id { - return (values['id'] as String?); - } - - set id(String? value) => values['id'] = value; - DateTime? get createdAt { - return (values['created_at'] as DateTime?); - } - - set createdAt(DateTime? value) => values['created_at'] = value; - DateTime? get updatedAt { - return (values['updated_at'] as DateTime?); - } - - set updatedAt(DateTime? value) => values['updated_at'] = value; - String? get name { - return (values['name'] as String?); - } - - set name(String? value) => values['name'] = value; - void copyFrom(Author model) { - createdAt = model.createdAt; - updatedAt = model.updatedAt; - name = model.name; - } -} - -// ************************************************************************** -// JsonModelGenerator -// ************************************************************************** - -@generatedSerializable -class Book extends _Book { - Book( - {this.id, - this.createdAt, - this.updatedAt, - this.author, - this.partnerAuthor, - this.name}); - - /// A unique identifier corresponding to this item. - @override - String? id; - - /// The time at which this item was created. - @override - DateTime? createdAt; - - /// The last time at which this item was updated. - @override - DateTime? updatedAt; - - @override - _Author? author; - - @override - _Author? partnerAuthor; - - @override - String? name; - - Book copyWith( - {String? id, - DateTime? createdAt, - DateTime? updatedAt, - _Author? author, - _Author? partnerAuthor, - String? name}) { - return Book( - id: id ?? this.id, - createdAt: createdAt ?? this.createdAt, - updatedAt: updatedAt ?? this.updatedAt, - author: author ?? this.author, - partnerAuthor: partnerAuthor ?? this.partnerAuthor, - name: name ?? this.name); - } - - @override - bool operator ==(other) { - return other is _Book && - other.id == id && - other.createdAt == createdAt && - other.updatedAt == updatedAt && - other.author == author && - other.partnerAuthor == partnerAuthor && - other.name == name; - } - - @override - int get hashCode { - return hashObjects([id, createdAt, updatedAt, author, partnerAuthor, name]); - } - - @override - String toString() { - return 'Book(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, author=$author, partnerAuthor=$partnerAuthor, name=$name)'; - } - - Map toJson() { - return BookSerializer.toMap(this); - } -} - -@generatedSerializable -class Author extends _Author { - Author({this.id, this.createdAt, this.updatedAt, this.name = 'Tobe Osakwe'}); - - /// A unique identifier corresponding to this item. - @override - String? id; - - /// The time at which this item was created. - @override - DateTime? createdAt; - - /// The last time at which this item was updated. - @override - DateTime? updatedAt; - - @override - String? name; - - Author copyWith( - {String? id, DateTime? createdAt, DateTime? updatedAt, String? name}) { - return Author( - id: id ?? this.id, - createdAt: createdAt ?? this.createdAt, - updatedAt: updatedAt ?? this.updatedAt, - name: name ?? this.name); - } - - @override - bool operator ==(other) { - return other is _Author && - other.id == id && - other.createdAt == createdAt && - other.updatedAt == updatedAt && - other.name == name; - } - - @override - int get hashCode { - return hashObjects([id, createdAt, updatedAt, name]); - } - - @override - String toString() { - return 'Author(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, name=$name)'; - } - - Map toJson() { - return AuthorSerializer.toMap(this); - } -} - -// ************************************************************************** -// SerializerGenerator -// ************************************************************************** - -const BookSerializer bookSerializer = BookSerializer(); - -class BookEncoder extends Converter { - const BookEncoder(); - - @override - Map convert(Book model) => BookSerializer.toMap(model); -} - -class BookDecoder extends Converter { - const BookDecoder(); - - @override - Book convert(Map map) => BookSerializer.fromMap(map); -} - -class BookSerializer extends Codec { - const BookSerializer(); - - @override - BookEncoder get encoder => const BookEncoder(); - @override - BookDecoder get decoder => const BookDecoder(); - static Book fromMap(Map map) { - return Book( - id: map['id'] as String?, - createdAt: map['created_at'] != null - ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'].toString())) - : null, - updatedAt: map['updated_at'] != null - ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'].toString())) - : null, - author: map['author'] != null - ? AuthorSerializer.fromMap(map['author'] as Map) - : null, - partnerAuthor: map['partner_author'] != null - ? AuthorSerializer.fromMap(map['partner_author'] as Map) - : null, - name: map['name'] as String?); - } - - static Map toMap(_Book? model) { - if (model == null) { - return {}; - } - return { - 'id': model.id, - 'created_at': model.createdAt?.toIso8601String(), - 'updated_at': model.updatedAt?.toIso8601String(), - 'author': AuthorSerializer.toMap(model.author), - 'partner_author': AuthorSerializer.toMap(model.partnerAuthor), - 'name': model.name - }; - } -} - -abstract class BookFields { - static const List allFields = [ - id, - createdAt, - updatedAt, - author, - partnerAuthor, - name - ]; - - static const String id = 'id'; - - static const String createdAt = 'created_at'; - - static const String updatedAt = 'updated_at'; - - static const String author = 'author'; - - static const String partnerAuthor = 'partner_author'; - - static const String name = 'name'; -} - -const AuthorSerializer authorSerializer = AuthorSerializer(); - -class AuthorEncoder extends Converter { - const AuthorEncoder(); - - @override - Map convert(Author model) => AuthorSerializer.toMap(model); -} - -class AuthorDecoder extends Converter { - const AuthorDecoder(); - - @override - Author convert(Map map) => AuthorSerializer.fromMap(map); -} - -class AuthorSerializer extends Codec { - const AuthorSerializer(); - - @override - AuthorEncoder get encoder => const AuthorEncoder(); - @override - AuthorDecoder get decoder => const AuthorDecoder(); - static Author fromMap(Map map) { - return Author( - id: map['id'] as String?, - createdAt: map['created_at'] != null - ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'].toString())) - : null, - updatedAt: map['updated_at'] != null - ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'].toString())) - : null, - name: map['name'] as String? ?? 'Tobe Osakwe'); - } - - static Map toMap(_Author? model) { - if (model == null) { - return {}; - } - return { - 'id': model.id, - 'created_at': model.createdAt?.toIso8601String(), - 'updated_at': model.updatedAt?.toIso8601String(), - 'name': model.name - }; - } -} - -abstract class AuthorFields { - static const List allFields = [ - id, - createdAt, - updatedAt, - name - ]; - - static const String id = 'id'; - - static const String createdAt = 'created_at'; - - static const String updatedAt = 'updated_at'; - - static const String name = 'name'; -} diff --git a/packages/orm/orm_builder/lib/src/models/user.dart b/packages/orm/orm_builder/lib/src/models/user.dart deleted file mode 100644 index 52bb257e..00000000 --- a/packages/orm/orm_builder/lib/src/models/user.dart +++ /dev/null @@ -1,38 +0,0 @@ -library angel3_orm_generator.test.models.user; - -import 'package:angel3_migration/angel3_migration.dart'; -import 'package:angel3_orm/angel3_orm.dart'; -import 'package:angel3_serialize/angel3_serialize.dart'; -import 'package:optional/optional.dart'; - -part 'user.g.dart'; - -@serializable -@orm -abstract class _User extends Model { - String? get username; - String? get password; - String? get email; - - @ManyToMany(_RoleUser) - List<_Role> get roles; -} - -@serializable -@orm -abstract class _RoleUser { - @belongsTo - _Role? get role; - - @belongsTo - _User? get user; -} - -@serializable -@orm -abstract class _Role extends Model { - String? name; - - @ManyToMany(_RoleUser) - List<_User>? get users; -} diff --git a/packages/orm/orm_builder/lib/src/models/user.g.dart b/packages/orm/orm_builder/lib/src/models/user.g.dart deleted file mode 100644 index 514c1b5f..00000000 --- a/packages/orm/orm_builder/lib/src/models/user.g.dart +++ /dev/null @@ -1,1002 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of angel3_orm_generator.test.models.user; - -// ************************************************************************** -// MigrationGenerator -// ************************************************************************** - -class UserMigration extends Migration { - @override - void up(Schema schema) { - schema.create('users', (table) { - table.serial('id').primaryKey(); - table.timeStamp('created_at'); - table.timeStamp('updated_at'); - table.varChar('username', length: 255); - table.varChar('password', length: 255); - table.varChar('email', length: 255); - }); - } - - @override - void down(Schema schema) { - schema.drop('users', cascade: true); - } -} - -class RoleUserMigration extends Migration { - @override - void up(Schema schema) { - schema.create('role_users', (table) { - table.declare('role_id', ColumnType('int')).references('roles', 'id'); - table.declare('user_id', ColumnType('int')).references('users', 'id'); - }); - } - - @override - void down(Schema schema) { - schema.drop('role_users'); - } -} - -class RoleMigration extends Migration { - @override - void up(Schema schema) { - schema.create('roles', (table) { - table.serial('id').primaryKey(); - table.timeStamp('created_at'); - table.timeStamp('updated_at'); - table.varChar('name', length: 255); - }); - } - - @override - void down(Schema schema) { - schema.drop('roles', cascade: true); - } -} - -// ************************************************************************** -// OrmGenerator -// ************************************************************************** - -class UserQuery extends Query { - UserQuery({Query? parent, Set? trampoline}) : super(parent: parent) { - trampoline ??= {}; - trampoline.add(tableName); - _where = UserQueryWhere(this); - leftJoin( - '(SELECT role_users.user_id, roles.id, roles.created_at, roles.updated_at, roles.name FROM roles LEFT JOIN role_users ON role_users.role_id=roles.id)', - 'id', - 'user_id', - additionalFields: const ['id', 'created_at', 'updated_at', 'name'], - trampoline: trampoline); - } - - @override - final UserQueryValues values = UserQueryValues(); - - UserQueryWhere? _where; - - @override - Map get casts { - return {}; - } - - @override - String get tableName { - return 'users'; - } - - @override - List get fields { - return const [ - 'id', - 'created_at', - 'updated_at', - 'username', - 'password', - 'email' - ]; - } - - @override - UserQueryWhere? get where { - return _where; - } - - @override - UserQueryWhere newWhereClause() { - return UserQueryWhere(this); - } - - static Optional parseRow(List row) { - if (row.every((x) => x == null)) { - return Optional.empty(); - } - var model = User( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - username: (row[3] as String?), - password: (row[4] as String?), - email: (row[5] as String?)); - if (row.length > 6) { - var modelOpt = RoleQuery.parseRow(row.skip(6).take(4).toList()); - modelOpt.ifPresent((m) { - model = model.copyWith(roles: [m]); - }); - } - return Optional.of(model); - } - - @override - Optional deserialize(List row) { - return parseRow(row); - } - - @override - bool canCompile(trampoline) { - return (!(trampoline.contains('users') && - trampoline.contains('role_users'))); - } - - @override - Future> get(QueryExecutor executor) { - return super.get(executor).then((result) { - return result.fold>([], (out, model) { - var idx = out.indexWhere((m) => m.id == model.id); - - if (idx == -1) { - return out..add(model); - } else { - var l = out[idx]; - return out - ..[idx] = l.copyWith( - roles: List<_Role>.from(l.roles ?? []) - ..addAll(model.roles ?? [])); - } - }); - }); - } - - @override - Future> update(QueryExecutor executor) { - return super.update(executor).then((result) { - return result.fold>([], (out, model) { - var idx = out.indexWhere((m) => m.id == model.id); - - if (idx == -1) { - return out..add(model); - } else { - var l = out[idx]; - return out - ..[idx] = l.copyWith( - roles: List<_Role>.from(l.roles ?? []) - ..addAll(model.roles ?? [])); - } - }); - }); - } - - @override - Future> delete(QueryExecutor executor) { - return super.delete(executor).then((result) { - return result.fold>([], (out, model) { - var idx = out.indexWhere((m) => m.id == model.id); - - if (idx == -1) { - return out..add(model); - } else { - var l = out[idx]; - return out - ..[idx] = l.copyWith( - roles: List<_Role>.from(l.roles ?? []) - ..addAll(model.roles ?? [])); - } - }); - }); - } -} - -class UserQueryWhere extends QueryWhere { - UserQueryWhere(UserQuery query) - : id = NumericSqlExpressionBuilder(query, 'id'), - createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), - updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), - username = StringSqlExpressionBuilder(query, 'username'), - password = StringSqlExpressionBuilder(query, 'password'), - email = StringSqlExpressionBuilder(query, 'email'); - - final NumericSqlExpressionBuilder id; - - final DateTimeSqlExpressionBuilder createdAt; - - final DateTimeSqlExpressionBuilder updatedAt; - - final StringSqlExpressionBuilder username; - - final StringSqlExpressionBuilder password; - - final StringSqlExpressionBuilder email; - - @override - List get expressionBuilders { - return [id, createdAt, updatedAt, username, password, email]; - } -} - -class UserQueryValues extends MapQueryValues { - @override - Map get casts { - return {}; - } - - String? get id { - return (values['id'] as String?); - } - - set id(String? value) => values['id'] = value; - DateTime? get createdAt { - return (values['created_at'] as DateTime?); - } - - set createdAt(DateTime? value) => values['created_at'] = value; - DateTime? get updatedAt { - return (values['updated_at'] as DateTime?); - } - - set updatedAt(DateTime? value) => values['updated_at'] = value; - String? get username { - return (values['username'] as String?); - } - - set username(String? value) => values['username'] = value; - String? get password { - return (values['password'] as String?); - } - - set password(String? value) => values['password'] = value; - String? get email { - return (values['email'] as String?); - } - - set email(String? value) => values['email'] = value; - void copyFrom(User model) { - createdAt = model.createdAt; - updatedAt = model.updatedAt; - username = model.username; - password = model.password; - email = model.email; - } -} - -class RoleUserQuery extends Query { - RoleUserQuery({Query? parent, Set? trampoline}) - : super(parent: parent) { - trampoline ??= {}; - trampoline.add(tableName); - _where = RoleUserQueryWhere(this); - leftJoin(_role = RoleQuery(trampoline: trampoline, parent: this), 'role_id', - 'id', - additionalFields: const ['id', 'created_at', 'updated_at', 'name'], - trampoline: trampoline); - leftJoin(_user = UserQuery(trampoline: trampoline, parent: this), 'user_id', - 'id', - additionalFields: const [ - 'id', - 'created_at', - 'updated_at', - 'username', - 'password', - 'email' - ], - trampoline: trampoline); - } - - @override - final RoleUserQueryValues values = RoleUserQueryValues(); - - RoleUserQueryWhere? _where; - - late RoleQuery _role; - - late UserQuery _user; - - @override - Map get casts { - return {}; - } - - @override - String get tableName { - return 'role_users'; - } - - @override - List get fields { - return const ['role_id', 'user_id']; - } - - @override - RoleUserQueryWhere? get where { - return _where; - } - - @override - RoleUserQueryWhere newWhereClause() { - return RoleUserQueryWhere(this); - } - - static Optional parseRow(List row) { - if (row.every((x) => x == null)) { - return Optional.empty(); - } - var model = RoleUser(); - if (row.length > 2) { - var modelOpt = RoleQuery.parseRow(row.skip(2).take(4).toList()); - modelOpt.ifPresent((m) { - model = model.copyWith(role: m); - }); - } - if (row.length > 6) { - var modelOpt = UserQuery.parseRow(row.skip(6).take(6).toList()); - modelOpt.ifPresent((m) { - model = model.copyWith(user: m); - }); - } - return Optional.of(model); - } - - @override - Optional deserialize(List row) { - return parseRow(row); - } - - RoleQuery get role { - return _role; - } - - UserQuery get user { - return _user; - } -} - -class RoleUserQueryWhere extends QueryWhere { - RoleUserQueryWhere(RoleUserQuery query) - : roleId = NumericSqlExpressionBuilder(query, 'role_id'), - userId = NumericSqlExpressionBuilder(query, 'user_id'); - - final NumericSqlExpressionBuilder roleId; - - final NumericSqlExpressionBuilder userId; - - @override - List get expressionBuilders { - return [roleId, userId]; - } -} - -class RoleUserQueryValues extends MapQueryValues { - @override - Map get casts { - return {}; - } - - int get roleId { - return (values['role_id'] as int); - } - - set roleId(int value) => values['role_id'] = value; - int get userId { - return (values['user_id'] as int); - } - - set userId(int value) => values['user_id'] = value; - void copyFrom(RoleUser model) { - if (model.role != null) { - values['role_id'] = model.role?.id; - } - if (model.user != null) { - values['user_id'] = model.user?.id; - } - } -} - -class RoleQuery extends Query { - RoleQuery({Query? parent, Set? trampoline}) : super(parent: parent) { - trampoline ??= {}; - trampoline.add(tableName); - _where = RoleQueryWhere(this); - leftJoin( - '(SELECT role_users.role_id, users.id, users.created_at, users.updated_at, users.username, users.password, users.email FROM users LEFT JOIN role_users ON role_users.user_id=users.id)', - 'id', - 'role_id', - additionalFields: const [ - 'id', - 'created_at', - 'updated_at', - 'username', - 'password', - 'email' - ], - trampoline: trampoline); - } - - @override - final RoleQueryValues values = RoleQueryValues(); - - RoleQueryWhere? _where; - - @override - Map get casts { - return {}; - } - - @override - String get tableName { - return 'roles'; - } - - @override - List get fields { - return const ['id', 'created_at', 'updated_at', 'name']; - } - - @override - RoleQueryWhere? get where { - return _where; - } - - @override - RoleQueryWhere newWhereClause() { - return RoleQueryWhere(this); - } - - static Optional parseRow(List row) { - if (row.every((x) => x == null)) { - return Optional.empty(); - } - var model = Role( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - name: (row[3] as String?)); - if (row.length > 4) { - var modelOpt = UserQuery.parseRow(row.skip(4).take(6).toList()); - modelOpt.ifPresent((m) { - model = model.copyWith(users: [m]); - }); - } - return Optional.of(model); - } - - @override - Optional deserialize(List row) { - return parseRow(row); - } - - @override - bool canCompile(trampoline) { - return (!(trampoline.contains('roles') && - trampoline.contains('role_users'))); - } - - @override - Future> get(QueryExecutor executor) { - return super.get(executor).then((result) { - return result.fold>([], (out, model) { - var idx = out.indexWhere((m) => m.id == model.id); - - if (idx == -1) { - return out..add(model); - } else { - var l = out[idx]; - return out - ..[idx] = l.copyWith( - users: List<_User>.from(l.users)..addAll(model.users)); - } - }); - }); - } - - @override - Future> update(QueryExecutor executor) { - return super.update(executor).then((result) { - return result.fold>([], (out, model) { - var idx = out.indexWhere((m) => m.id == model.id); - - if (idx == -1) { - return out..add(model); - } else { - var l = out[idx]; - return out - ..[idx] = l.copyWith( - users: List<_User>.from(l.users)..addAll(model.users)); - } - }); - }); - } - - @override - Future> delete(QueryExecutor executor) { - return super.delete(executor).then((result) { - return result.fold>([], (out, model) { - var idx = out.indexWhere((m) => m.id == model.id); - - if (idx == -1) { - return out..add(model); - } else { - var l = out[idx]; - return out - ..[idx] = l.copyWith( - users: List<_User>.from(l.users)..addAll(model.users)); - } - }); - }); - } -} - -class RoleQueryWhere extends QueryWhere { - RoleQueryWhere(RoleQuery query) - : id = NumericSqlExpressionBuilder(query, 'id'), - createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), - updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), - name = StringSqlExpressionBuilder(query, 'name'); - - final NumericSqlExpressionBuilder id; - - final DateTimeSqlExpressionBuilder createdAt; - - final DateTimeSqlExpressionBuilder updatedAt; - - final StringSqlExpressionBuilder name; - - @override - List get expressionBuilders { - return [id, createdAt, updatedAt, name]; - } -} - -class RoleQueryValues extends MapQueryValues { - @override - Map get casts { - return {}; - } - - String? get id { - return (values['id'] as String?); - } - - set id(String? value) => values['id'] = value; - DateTime? get createdAt { - return (values['created_at'] as DateTime?); - } - - set createdAt(DateTime? value) => values['created_at'] = value; - DateTime? get updatedAt { - return (values['updated_at'] as DateTime?); - } - - set updatedAt(DateTime? value) => values['updated_at'] = value; - String? get name { - return (values['name'] as String?); - } - - set name(String? value) => values['name'] = value; - void copyFrom(Role model) { - createdAt = model.createdAt; - updatedAt = model.updatedAt; - name = model.name; - } -} - -// ************************************************************************** -// JsonModelGenerator -// ************************************************************************** - -@generatedSerializable -class User extends _User { - User( - {this.id, - this.createdAt, - this.updatedAt, - this.username, - this.password, - this.email, - List<_Role> roles = const []}) - : roles = List.unmodifiable(roles); - - /// A unique identifier corresponding to this item. - @override - String? id; - - /// The time at which this item was created. - @override - DateTime? createdAt; - - /// The last time at which this item was updated. - @override - DateTime? updatedAt; - - @override - String? username; - - @override - String? password; - - @override - String? email; - - @override - List<_Role> roles; - - User copyWith( - {String? id, - DateTime? createdAt, - DateTime? updatedAt, - String? username, - String? password, - String? email, - List<_Role>? roles}) { - return User( - id: id ?? this.id, - createdAt: createdAt ?? this.createdAt, - updatedAt: updatedAt ?? this.updatedAt, - username: username ?? this.username, - password: password ?? this.password, - email: email ?? this.email, - roles: roles ?? this.roles); - } - - @override - bool operator ==(other) { - return other is _User && - other.id == id && - other.createdAt == createdAt && - other.updatedAt == updatedAt && - other.username == username && - other.password == password && - other.email == email && - ListEquality<_Role>(DefaultEquality<_Role>()) - .equals(other.roles, roles); - } - - @override - int get hashCode { - return hashObjects( - [id, createdAt, updatedAt, username, password, email, roles]); - } - - @override - String toString() { - return 'User(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, username=$username, password=$password, email=$email, roles=$roles)'; - } - - Map toJson() { - return UserSerializer.toMap(this); - } -} - -@generatedSerializable -class RoleUser implements _RoleUser { - RoleUser({this.role, this.user}); - - @override - _Role? role; - - @override - _User? user; - - RoleUser copyWith({_Role? role, _User? user}) { - return RoleUser(role: role ?? this.role, user: user ?? this.user); - } - - @override - bool operator ==(other) { - return other is _RoleUser && other.role == role && other.user == user; - } - - @override - int get hashCode { - return hashObjects([role, user]); - } - - @override - String toString() { - return 'RoleUser(role=$role, user=$user)'; - } - - Map toJson() { - return RoleUserSerializer.toMap(this); - } -} - -@generatedSerializable -class Role extends _Role { - Role( - {this.id, - this.createdAt, - this.updatedAt, - this.name, - List<_User>? users = const []}) - : users = List.unmodifiable(users ?? []); - - /// A unique identifier corresponding to this item. - @override - String? id; - - /// The time at which this item was created. - @override - DateTime? createdAt; - - /// The last time at which this item was updated. - @override - DateTime? updatedAt; - - @override - String? name; - - @override - List<_User>? users; - - Role copyWith( - {String? id, - DateTime? createdAt, - DateTime? updatedAt, - String? name, - List<_User>? users}) { - return Role( - id: id ?? this.id, - createdAt: createdAt ?? this.createdAt, - updatedAt: updatedAt ?? this.updatedAt, - name: name ?? this.name, - users: users ?? this.users); - } - - @override - bool operator ==(other) { - return other is _Role && - other.id == id && - other.createdAt == createdAt && - other.updatedAt == updatedAt && - other.name == name && - ListEquality<_User>(DefaultEquality<_User>()) - .equals(other.users, users); - } - - @override - int get hashCode { - return hashObjects([id, createdAt, updatedAt, name, users]); - } - - @override - String toString() { - return 'Role(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, name=$name, users=$users)'; - } - - Map toJson() { - return RoleSerializer.toMap(this); - } -} - -// ************************************************************************** -// SerializerGenerator -// ************************************************************************** - -const UserSerializer userSerializer = UserSerializer(); - -class UserEncoder extends Converter { - const UserEncoder(); - - @override - Map convert(User model) => UserSerializer.toMap(model); -} - -class UserDecoder extends Converter { - const UserDecoder(); - - @override - User convert(Map map) => UserSerializer.fromMap(map); -} - -class UserSerializer extends Codec { - const UserSerializer(); - - @override - UserEncoder get encoder => const UserEncoder(); - @override - UserDecoder get decoder => const UserDecoder(); - static User fromMap(Map map) { - return User( - id: map['id'] as String?, - createdAt: map['created_at'] != null - ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'].toString())) - : null, - updatedAt: map['updated_at'] != null - ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'].toString())) - : null, - username: map['username'] as String?, - password: map['password'] as String?, - email: map['email'] as String?, - roles: map['roles'] is Iterable - ? List.unmodifiable(((map['roles'] as Iterable).whereType()) - .map(RoleSerializer.fromMap)) - : []); - } - - static Map toMap(_User? model) { - if (model == null) { - return {}; - } - return { - 'id': model.id, - 'created_at': model.createdAt?.toIso8601String(), - 'updated_at': model.updatedAt?.toIso8601String(), - 'username': model.username, - 'password': model.password, - 'email': model.email, - 'roles': model.roles.map((m) => RoleSerializer.toMap(m)).toList() - }; - } -} - -abstract class UserFields { - static const List allFields = [ - id, - createdAt, - updatedAt, - username, - password, - email, - roles - ]; - - static const String id = 'id'; - - static const String createdAt = 'created_at'; - - static const String updatedAt = 'updated_at'; - - static const String username = 'username'; - - static const String password = 'password'; - - static const String email = 'email'; - - static const String roles = 'roles'; -} - -const RoleUserSerializer roleUserSerializer = RoleUserSerializer(); - -class RoleUserEncoder extends Converter { - const RoleUserEncoder(); - - @override - Map convert(RoleUser model) => RoleUserSerializer.toMap(model); -} - -class RoleUserDecoder extends Converter { - const RoleUserDecoder(); - - @override - RoleUser convert(Map map) => RoleUserSerializer.fromMap(map); -} - -class RoleUserSerializer extends Codec { - const RoleUserSerializer(); - - @override - RoleUserEncoder get encoder => const RoleUserEncoder(); - @override - RoleUserDecoder get decoder => const RoleUserDecoder(); - static RoleUser fromMap(Map map) { - return RoleUser( - role: map['role'] != null - ? RoleSerializer.fromMap(map['role'] as Map) - : null, - user: map['user'] != null - ? UserSerializer.fromMap(map['user'] as Map) - : null); - } - - static Map toMap(_RoleUser? model) { - if (model == null) { - return {}; - } - return { - 'role': RoleSerializer.toMap(model.role), - 'user': UserSerializer.toMap(model.user) - }; - } -} - -abstract class RoleUserFields { - static const List allFields = [role, user]; - - static const String role = 'role'; - - static const String user = 'user'; -} - -const RoleSerializer roleSerializer = RoleSerializer(); - -class RoleEncoder extends Converter { - const RoleEncoder(); - - @override - Map convert(Role model) => RoleSerializer.toMap(model); -} - -class RoleDecoder extends Converter { - const RoleDecoder(); - - @override - Role convert(Map map) => RoleSerializer.fromMap(map); -} - -class RoleSerializer extends Codec { - const RoleSerializer(); - - @override - RoleEncoder get encoder => const RoleEncoder(); - @override - RoleDecoder get decoder => const RoleDecoder(); - static Role fromMap(Map map) { - return Role( - id: map['id'] as String?, - createdAt: map['created_at'] != null - ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'].toString())) - : null, - updatedAt: map['updated_at'] != null - ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'].toString())) - : null, - name: map['name'] as String?, - users: map['users'] is Iterable - ? List.unmodifiable(((map['users'] as Iterable).whereType()) - .map(UserSerializer.fromMap)) - : []); - } - - static Map toMap(_Role? model) { - if (model == null) { - return {}; - } - return { - 'id': model.id, - 'created_at': model.createdAt?.toIso8601String(), - 'updated_at': model.updatedAt?.toIso8601String(), - 'name': model.name, - 'users': model.users?.map((m) => UserSerializer.toMap(m)).toList() - }; - } -} - -abstract class RoleFields { - static const List allFields = [ - id, - createdAt, - updatedAt, - name, - users - ]; - - static const String id = 'id'; - - static const String createdAt = 'created_at'; - - static const String updatedAt = 'updated_at'; - - static const String name = 'name'; - - static const String users = 'users'; -} diff --git a/packages/orm/orm_builder/lib/src/util.dart b/packages/orm/orm_builder/lib/src/util.dart deleted file mode 100644 index 897050af..00000000 --- a/packages/orm/orm_builder/lib/src/util.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'dart:io'; -import 'package:io/ansi.dart'; - -void printSeparator(String title) { - var b = StringBuffer('===' + title.toUpperCase()); - for (var i = b.length; i < stdout.terminalColumns - 3; i++) { - b.write('='); - } - for (var i = 0; i < 3; i++) { - print(magenta.wrap(b.toString())); - } -} diff --git a/packages/orm/orm_builder/pubspec.yaml b/packages/orm/orm_builder/pubspec.yaml deleted file mode 100644 index d54b2bbe..00000000 --- a/packages/orm/orm_builder/pubspec.yaml +++ /dev/null @@ -1,31 +0,0 @@ -name: orm_builder -version: 1.0.0 -description: ORM Builder Testing -published_to: none -environment: - sdk: '>=2.12.0 <3.0.0' -dependencies: - angel3_migration: ^4.0.0 - angel3_model: ^3.1.0 - angel3_orm: ^4.0.0 - angel3_serialize: ^4.1.0 - io: ^1.0.0 - test: ^1.17.4 - collection: ^1.15.0 - optional: ^6.0.0 -dev_dependencies: - angel3_orm_generator: ^4.1.0 - angel3_framework: ^4.2.0 - build_runner: ^2.0.1 - lints: ^1.0.0 -dependency_overrides: -# angel3_migration_runner: -# path: ../angel_migration_runner - angel3_orm: - path: ../angel_orm - angel3_migration: - path: ../angel_migration - angel3_orm_generator: - path: ../angel_orm_generator - angel3_serialize: - path: ../../serialize/angel_serialize