Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
codekeyz committed Aug 3, 2024
1 parent 74f9389 commit cc86c5c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 30 deletions.
34 changes: 22 additions & 12 deletions lib/src/builder/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,22 @@ class EntityGenerator extends GeneratorForAnnotation<entity.Table> {
..fields.addAll(fieldsRequiredForCreate.map((f) => Field(
(fb) => fb
..name = f.name
..type = refer('value<${f.type.withNullability}>')
..type = refer('value<${f.type.withNullability}>${!f.type.isNullable ? '?' : ''}')
..modifier = FieldModifier.final$,
)))
..constructors.add(
Constructor(
(c) => c
..constant = true
..optionalParameters.addAll(fieldsRequiredForCreate.map(
(field) => Parameter((p) => p
..named = true
..name = field.name
..toThis = true
..defaultTo = Code('const NoValue()')),
(field) => Parameter((p) {
p
..named = true
..name = field.name
..toThis = true;

if (field.type.isNullable) p.defaultTo = Code('const NoValue()');
}),
)),
),
)
Expand All @@ -276,7 +279,12 @@ class EntityGenerator extends GeneratorForAnnotation<entity.Table> {
..annotations.add(CodeExpression(Code('override')))
..lambda = true
..body = Code('''{
${entity.normalFields.map((e) => 'if (${e.name} is! NoValue) #${e.name}: ${e.name}.val${!e.type.isNullable ? '!' : ''}'.trim()).join(',')},
${entity.normalFields.map((e) {
if (e.type.isNullable) {
return 'if (${e.name} is! NoValue) #${e.name}: ${e.name}.val';
}
return 'if (${e.name} != null) #${e.name}: ${e.name}!.val';
}).join(',')},
}''')))),
];
}
Expand Down Expand Up @@ -417,11 +425,13 @@ class EntityGenerator extends GeneratorForAnnotation<entity.Table> {
)''';
}

return '''DBEntityField(
$requiredOpts
${field.type.isNullable ? 'nullable: true, ' : ''}
${unique ? 'unique: true, ' : ''}
)''';
final args = [
requiredOpts,
if (field.type.isNullable) 'nullable: true',
if (unique) 'unique: true',
].join(',');

return '''DBEntityField($args)''';
}

/// Generate JOIN for BelongsTo getters on Entity
Expand Down
3 changes: 0 additions & 3 deletions lib/src/builder/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ Stream<(ResolvedLibraryResult, String, String)> _libraries(AnalysisContextCollec
final futures =
analyzedFiles.where((path) => path.endsWith('.dart') && !path.endsWith('_test.dart')).map((filePath) async {
final library = await context.currentSession.getResolvedLibrary(filePath);

print(library);

if (library is! ResolvedLibraryResult) return null;
return (library, filePath, context.contextRoot.root.path);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cli/model/migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ part 'migration.g.dart';

@Table(name: 'migrations')
class MigrationEntity extends Entity<MigrationEntity> {
@autoIncrementPrimary
@primaryKey
final int id;

final String migration;
Expand Down
12 changes: 6 additions & 6 deletions lib/src/cli/model/migration.g.dart

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

11 changes: 3 additions & 8 deletions lib/src/database/entity/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,11 @@ class bindTo {
const bindTo(this.type, {this.on, this.onUpdate, this.onDelete});
}

const autoIncrementPrimary = PrimaryKey(autoIncrement: true);
const table = Table();
const createdAtCol = CreatedAtColumn();
const updatedAtCol = UpdatedAtColumn();

class value<T extends Object> {
final T? val;
class value<T> {
final T val;
const value(this.val);
}

class NoValue<T extends Object> extends value<T> {
class NoValue<T> extends value<T?> {
const NoValue() : super(null);
}
7 changes: 7 additions & 0 deletions lib/yaroorm.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library;

import 'src/database/entity/entity.dart';

export 'src/query/query.dart';
export 'src/database/driver/driver.dart';
export 'src/database/entity/entity.dart' hide entityMapToDbData, entityToDbData, dbDataToEntity, combineConverters;
Expand All @@ -8,3 +10,8 @@ export 'src/config.dart';
export 'src/reflection.dart'
hide getEntityTableName, getEntityPrimaryKey, EntityInstanceReflector, EntityInstanceBuilder;
export 'src/migration.dart';

const primaryKey = PrimaryKey(autoIncrement: true);
const table = Table();
const createdAtCol = CreatedAtColumn();
const updatedAtCol = UpdatedAtColumn();

0 comments on commit cc86c5c

Please sign in to comment.