createMOdel function
Implementation
Future createMOdel(String dbType) async {
final yamlFile = File(Directory.current.path + '/lib/model_definition.yaml');
final yamlContent = yamlFile.readAsStringSync();
final yamlMap = loadYaml(yamlContent) as List;
for (var yamlItem in yamlMap) {
final dbName = yamlItem['database'] as String;
final className = yamlItem['class'] as String;
var fields = yamlItem['fields'] as List<dynamic>;
final yamlFile = File(Directory.current.path + '/lib/dbconf.yaml');
final yamlContent = yamlFile.readAsStringSync();
final yamlMap = loadYaml(yamlContent);
final host = yamlMap['host'] as String;
final username = yamlMap['username'] as String;
final password = yamlMap['password'] as String;
final port = yamlMap['port'] as int;
final classCode = StringBuffer()
..writeln('import "package:arthur/arthur.dart";')
..writeln()
..writeln()
..writeln('class $className extends Arthur {')
..writeln();
for (final field in fields) {
var fieldName = field.keys.first;
classCode.writeln(' dynamic $fieldName;');
}
classCode
..writeln()
..writeln(' $className({');
for (final field in fields) {
var fieldName = field.keys.first;
classCode.writeln(' this.$fieldName,');
}
classCode..writeln(' }): super(');
Map fieldsMap = {};
for (var field in fields) {
var fieldName = field.keys.first;
List fieldTypeParams = field.values.first;
if (fieldTypeParams.length == 2) {
var dataType = fieldTypeParams[0];
var extraParam = fieldTypeParams[1];
fieldsMap[fieldName] = "$dataType $extraParam";
} else {
var dataType = fieldTypeParams[0];
fieldsMap[fieldName] = "$dataType";
}
}
if (dbType == 'postgres' || dbType == 'postgresql') {
classCode
..writeln(' host: "$host",')
..writeln(' username: "$username", ')
..writeln(' password: "$password", ')
..writeln(' port: $port, ')
..writeln(' db: "postgres",')
..writeln(' dbName: "$dbName",')
..writeln(' tableName: "_$className"');
await createPostgresTable(
fieldsMap, "_$className", dbName, host, port, username, password);
print('[ INFO ] Table created : _$className');
} else {
classCode
..writeln(' db: "sqlite",')
..writeln(' dbName: "$dbName",')
..writeln(' tableName: "_$className"');
await createSqliteTable(fieldsMap, "_$className", dbName);
print('[ INFO ] Table created : _$className');
}
classCode
..writeln()
..writeln(' );')
..writeln()
..writeln()
..writeln(' @override')
..writeln(' Map<String, dynamic> toMap(){')
..writeln()
..writeln(' final map = <String, dynamic>{};');
for (final field in fields) {
var fieldName = field.keys.first;
classCode.writeln(' map["$fieldName"] = $fieldName;');
}
classCode
..writeln('')
..writeln(' return map;')
..writeln(' }')
..writeln();
classCode..writeln('}');
final outputFileName = '$className.dart';
final outputFile = File(Directory.current.path + '/lib/' + outputFileName);
outputFile.writeAsStringSync(classCode.toString());
print('[ INFO ] Model $className generated successfully');
}
}