From 9145610ae78992193efc8a5ed6cadf234edc78c2 Mon Sep 17 00:00:00 2001 From: "(AJ) Zin Kyaw" Date: Fri, 15 Dec 2023 14:08:13 +0630 Subject: [PATCH] bug fix on query builder save (#79) --- packages/dox-builder/lib/src/dox_builder.dart | 8 +++++++- packages/dox-query-builder/lib/src/model.dart | 17 ++++++++++++++--- .../dox-query-builder/test/model_test.dart | 2 +- .../test/models/artist/artist.model.dart | 16 +++++++++------- .../test/models/artist/artist.model.g.dart | 11 ++++++++++- .../artist_song/artist_song.model.g.dart | 6 +++++- .../test/models/blog/blog.model.g.dart | 18 +++++++++++++++++- .../models/blog_info/blog_info.model.g.dart | 11 ++++++++++- .../test/models/song/song.model.g.dart | 11 ++++++++++- .../test/models/user/user.model.g.dart | 6 +++++- 10 files changed, 88 insertions(+), 18 deletions(-) diff --git a/packages/dox-builder/lib/src/dox_builder.dart b/packages/dox-builder/lib/src/dox_builder.dart index acec221..9e12067 100644 --- a/packages/dox-builder/lib/src/dox_builder.dart +++ b/packages/dox-builder/lib/src/dox_builder.dart @@ -85,7 +85,10 @@ class DoxModelBuilder extends GeneratorForAnnotation { ${_primaryKeySetterAndGetter(primaryKey)} - $className get newQuery => $className(); + $className query() => $className(); + + @override + List get tableColumns => [${map['tableColumns'].join(',')}]; @override List get preloadList => [ @@ -224,6 +227,7 @@ class DoxModelBuilder extends GeneratorForAnnotation { _getCodeForToMap(ModelVisitor visitor) { String jsonMapper = ''; String parseContent = ''; + List tableColumns = []; String className = visitor.className; visitor.columns.forEach((filedName, values) { String? beforeSave = values['beforeSave']; @@ -240,6 +244,7 @@ class DoxModelBuilder extends GeneratorForAnnotation { parseContent += "map['$jsonKey'] = $className.$beforeSave(map);\n"; } jsonMapper += "'$jsonKey' : $setValue,\n"; + tableColumns.add("'$jsonKey'"); }); String toMapRelation = "\nList preload = getPreload();\n"; @@ -262,6 +267,7 @@ class DoxModelBuilder extends GeneratorForAnnotation { return { 'jsonMapper': jsonMapper, 'parseContent': parseContent, + 'tableColumns': tableColumns, }; } } diff --git a/packages/dox-query-builder/lib/src/model.dart b/packages/dox-query-builder/lib/src/model.dart index b79ba01..3c264cc 100644 --- a/packages/dox-query-builder/lib/src/model.dart +++ b/packages/dox-query-builder/lib/src/model.dart @@ -7,6 +7,8 @@ class Model extends QueryBuilder { List hidden = []; + List tableColumns = []; + bool _debug = SqlQueryBuilder().debug; @override @@ -40,7 +42,7 @@ class Model extends QueryBuilder { String? createdAtColumn = timestampsColumn['created_at']; String? updatedAtColumn = timestampsColumn['updated_at']; - Map values = toMap(); + Map values = toMap(removeRelations: true); if (values[primaryKey] == null) { values.removeWhere((String key, dynamic value) => value == null); @@ -106,8 +108,17 @@ class Model extends QueryBuilder { /// Map m = blog?.toMap(); /// Map m = blog?.toMap(original: true); /// ``` - Map toMap( - {bool original = false, bool removeHiddenField = false}) { + Map toMap({ + bool original = false, + bool removeHiddenField = false, + bool removeRelations = false, + }) { + if (removeRelations == true) { + List columns = tableColumns; + Map data = convertToMap(this); + data.removeWhere((String key, _) => !columns.contains(key)); + return data; + } if (original == true && originalMap.isNotEmpty) { return originalMap; } diff --git a/packages/dox-query-builder/test/model_test.dart b/packages/dox-query-builder/test/model_test.dart index 692d6a4..8bf1374 100644 --- a/packages/dox-query-builder/test/model_test.dart +++ b/packages/dox-query-builder/test/model_test.dart @@ -196,7 +196,7 @@ void main() { blog.description = "something"; await blog.save(); - Blog? check = await blog.newQuery.find(blog.uid); + Blog? check = await blog.query().find(blog.uid); expect(check?.uid, blog.uid); }); diff --git a/packages/dox-query-builder/test/models/artist/artist.model.dart b/packages/dox-query-builder/test/models/artist/artist.model.dart index 1ab5f38..8816746 100644 --- a/packages/dox-query-builder/test/models/artist/artist.model.dart +++ b/packages/dox-query-builder/test/models/artist/artist.model.dart @@ -9,13 +9,15 @@ class Artist extends ArtistGenerator { @Column() String? name; - @ManyToMany(Song, - localKey: 'id', - relatedKey: 'id', - pivotForeignKey: 'artist_id', - pivotRelatedForeignKey: 'song_id', - pivotTable: 'artist_song', - onQuery: onQuery) + @ManyToMany( + Song, + localKey: 'id', + relatedKey: 'id', + pivotForeignKey: 'artist_id', + pivotRelatedForeignKey: 'song_id', + pivotTable: 'artist_song', + onQuery: onQuery, + ) List songs = []; static Model onQuery(Song q) { diff --git a/packages/dox-query-builder/test/models/artist/artist.model.g.dart b/packages/dox-query-builder/test/models/artist/artist.model.g.dart index 8dd2408..5a182ff 100644 --- a/packages/dox-query-builder/test/models/artist/artist.model.g.dart +++ b/packages/dox-query-builder/test/models/artist/artist.model.g.dart @@ -22,7 +22,11 @@ class ArtistGenerator extends Model { set id(dynamic val) => tempIdValue = val; - Artist get newQuery => Artist(); + Artist query() => Artist(); + + @override + List get tableColumns => + ['id', 'name', 'created_at', 'updated_at']; @override List get preloadList => []; @@ -80,6 +84,11 @@ class ArtistGenerator extends Model { 'updated_at': instance.updatedAt?.toIso8601String(), }; + List preload = getPreload(); + if (preload.contains('songs')) { + map['songs'] = toMap(instance.songs); + } + return map; } } diff --git a/packages/dox-query-builder/test/models/artist_song/artist_song.model.g.dart b/packages/dox-query-builder/test/models/artist_song/artist_song.model.g.dart index 7c8674d..3907100 100644 --- a/packages/dox-query-builder/test/models/artist_song/artist_song.model.g.dart +++ b/packages/dox-query-builder/test/models/artist_song/artist_song.model.g.dart @@ -22,7 +22,11 @@ class ArtistSongGenerator extends Model { set id(dynamic val) => tempIdValue = val; - ArtistSong get newQuery => ArtistSong(); + ArtistSong query() => ArtistSong(); + + @override + List get tableColumns => + ['id', 'blog_id', 'artist_id', 'created_at', 'updated_at']; @override List get preloadList => []; diff --git a/packages/dox-query-builder/test/models/blog/blog.model.g.dart b/packages/dox-query-builder/test/models/blog/blog.model.g.dart index 187d30c..30fc89b 100644 --- a/packages/dox-query-builder/test/models/blog/blog.model.g.dart +++ b/packages/dox-query-builder/test/models/blog/blog.model.g.dart @@ -22,7 +22,18 @@ class BlogGenerator extends Model with SoftDeletes { set uid(dynamic val) => tempIdValue = val; - Blog get newQuery => Blog(); + Blog query() => Blog(); + + @override + List get tableColumns => [ + 'uid', + 'title', + 'status', + 'body', + 'deleted_at', + 'created_at', + 'updated_at' + ]; @override List get preloadList => [ @@ -103,8 +114,13 @@ class BlogGenerator extends Model with SoftDeletes { 'deleted_at': instance.deletedAt?.toIso8601String(), 'created_at': instance.createdAt?.toIso8601String(), 'updated_at': instance.updatedAt?.toIso8601String(), + 'blog_info': toMap(instance.blogInfo), + 'blog_infos': toMap(instance.blogInfos), }; map['title'] = Blog.slugTitle(map); + + List preload = getPreload(); + return map; } } diff --git a/packages/dox-query-builder/test/models/blog_info/blog_info.model.g.dart b/packages/dox-query-builder/test/models/blog_info/blog_info.model.g.dart index 5a3f34b..c58dd1e 100644 --- a/packages/dox-query-builder/test/models/blog_info/blog_info.model.g.dart +++ b/packages/dox-query-builder/test/models/blog_info/blog_info.model.g.dart @@ -22,7 +22,11 @@ class BlogInfoGenerator extends Model { set id(dynamic val) => tempIdValue = val; - BlogInfo get newQuery => BlogInfo(); + BlogInfo query() => BlogInfo(); + + @override + List get tableColumns => + ['id', 'info', 'blog_id', 'created_at', 'updated_at']; @override List get preloadList => []; @@ -77,6 +81,11 @@ class BlogInfoGenerator extends Model { 'updated_at': instance.updatedAt?.toIso8601String(), }; + List preload = getPreload(); + if (preload.contains('blog')) { + map['blog'] = toMap(instance.blog); + } + return map; } } diff --git a/packages/dox-query-builder/test/models/song/song.model.g.dart b/packages/dox-query-builder/test/models/song/song.model.g.dart index 185298e..aa9134f 100644 --- a/packages/dox-query-builder/test/models/song/song.model.g.dart +++ b/packages/dox-query-builder/test/models/song/song.model.g.dart @@ -22,7 +22,11 @@ class SongGenerator extends Model { set id(dynamic val) => tempIdValue = val; - Song get newQuery => Song(); + Song query() => Song(); + + @override + List get tableColumns => + ['id', 'title', 'created_at', 'updated_at']; @override List get preloadList => []; @@ -74,6 +78,11 @@ class SongGenerator extends Model { 'updated_at': instance.updatedAt?.toIso8601String(), }; + List preload = getPreload(); + if (preload.contains('artists')) { + map['artists'] = toMap(instance.artists); + } + return map; } } diff --git a/packages/dox-query-builder/test/models/user/user.model.g.dart b/packages/dox-query-builder/test/models/user/user.model.g.dart index 4f8836a..f39c493 100644 --- a/packages/dox-query-builder/test/models/user/user.model.g.dart +++ b/packages/dox-query-builder/test/models/user/user.model.g.dart @@ -25,7 +25,11 @@ class UserGenerator extends Model with SoftDeletes { set id(dynamic val) => tempIdValue = val; - User get newQuery => User(); + User query() => User(); + + @override + List get tableColumns => + ['id', 'name', 'created_at', 'updated_at']; @override List get preloadList => [];