Skip to content

Commit

Permalink
bug fix on query builder save
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion committed Dec 15, 2023
1 parent 04dae93 commit 6b76fdc
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 18 deletions.
8 changes: 7 additions & 1 deletion packages/dox-builder/lib/src/dox_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ class DoxModelBuilder extends GeneratorForAnnotation<DoxModel> {
${_primaryKeySetterAndGetter(primaryKey)}
$className get newQuery => $className();
$className query() => $className();
@override
List<String> get tableColumns => <String>[${map['tableColumns'].join(',')}];
@override
List<String> get preloadList => <String>[
Expand Down Expand Up @@ -224,6 +227,7 @@ class DoxModelBuilder extends GeneratorForAnnotation<DoxModel> {
_getCodeForToMap(ModelVisitor visitor) {
String jsonMapper = '';
String parseContent = '';
List<String> tableColumns = [];
String className = visitor.className;
visitor.columns.forEach((filedName, values) {
String? beforeSave = values['beforeSave'];
Expand All @@ -240,6 +244,7 @@ class DoxModelBuilder extends GeneratorForAnnotation<DoxModel> {
parseContent += "map['$jsonKey'] = $className.$beforeSave(map);\n";
}
jsonMapper += "'$jsonKey' : $setValue,\n";
tableColumns.add("'$jsonKey'");
});

String toMapRelation = "\nList<String> preload = getPreload();\n";
Expand All @@ -262,6 +267,7 @@ class DoxModelBuilder extends GeneratorForAnnotation<DoxModel> {
return {
'jsonMapper': jsonMapper,
'parseContent': parseContent,
'tableColumns': tableColumns,
};
}
}
17 changes: 14 additions & 3 deletions packages/dox-query-builder/lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Model<T> extends QueryBuilder<T> {

List<String> hidden = <String>[];

List<String> tableColumns = <String>[];

bool _debug = SqlQueryBuilder().debug;

@override
Expand Down Expand Up @@ -40,7 +42,7 @@ class Model<T> extends QueryBuilder<T> {
String? createdAtColumn = timestampsColumn['created_at'];
String? updatedAtColumn = timestampsColumn['updated_at'];

Map<String, dynamic> values = toMap();
Map<String, dynamic> values = toMap(removeRelations: true);
if (values[primaryKey] == null) {
values.removeWhere((String key, dynamic value) => value == null);

Expand Down Expand Up @@ -106,8 +108,17 @@ class Model<T> extends QueryBuilder<T> {
/// Map<String, dynamic> m = blog?.toMap();
/// Map<String, dynamic> m = blog?.toMap(original: true);
/// ```
Map<String, dynamic> toMap(
{bool original = false, bool removeHiddenField = false}) {
Map<String, dynamic> toMap({
bool original = false,
bool removeHiddenField = false,
bool removeRelations = false,
}) {
if (removeRelations == true) {
List<String> columns = tableColumns;
Map<String, dynamic> data = convertToMap(this);
data.removeWhere((String key, _) => !columns.contains(key));
return data;
}
if (original == true && originalMap.isNotEmpty) {
return originalMap;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dox-query-builder/test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
16 changes: 9 additions & 7 deletions packages/dox-query-builder/test/models/artist/artist.model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Song> songs = <Song>[];

static Model<Song> onQuery(Song q) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion packages/dox-query-builder/test/models/blog/blog.model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion packages/dox-query-builder/test/models/song/song.model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b76fdc

Please sign in to comment.