generateSchema function
Resolves one annotated Dart library and emits its typed client and frozen schema.
Values, constructor fields and selections are validated before any file write.
schemaPath and outputPath may be relative to the current directory.
Implementation
Future<GeneratedSources> generateSchema({
required String schemaPath,
required String outputPath,
String databaseName = 'AppDatabase',
required Engine engine,
}) async {
if (!RegExp(r'^[A-Z][a-zA-Z0-9_]*$').hasMatch(databaseName)) {
throw const FormatException(
'Database name must be a public Dart class identifier.',
);
}
final input = p.normalize(p.absolute(schemaPath));
final output = p.normalize(p.absolute(outputPath));
await _validateOutputPaths(input, output);
final model = await readSchema(input, engine: engine);
_validateGeneratedNames(model, databaseName);
freezeSnapshot(
SchemaSnapshot(
engine: engine,
tables: [
for (final table in model.tables)
TableDefinition(table.name, [
for (final field in table.fields) field.column,
]),
],
),
);
final formatter = DartFormatter(
languageVersion: DartFormatter.latestLanguageVersion,
);
final import = Uri(
pathSegments: p.split(p.relative(input, from: p.dirname(output))),
).toString();
return GeneratedSources._(
database: formatter.format(
emitDatabase(model, modelImport: import, databaseName: databaseName),
),
snapshot: formatter.format(emitSnapshot(model, engine: engine)),
schemaPath: input,
outputPath: output,
);
}