ormBuilder function

Builder ormBuilder(
  1. BuilderOptions options
)

Factory used by build_runner's build.yaml registration.

Use models for a recursive model source directory, database for the target engine, and default_namespace for PostgreSQL's default physical namespace. Individual libraries can instead be selected with generate_for. Models must not depend on their generated destinations, including inactive conditional imports/exports. build_runner owns output cleanup; application sources must never occupy those declared output slots.

Implementation

builder.Builder ormBuilder(builder.BuilderOptions options) {
  if (options.config.keys.any(
    (key) => !{'database', 'models', 'default_namespace'}.contains(key),
  )) {
    throw ArgumentError(
      'Use database, models and default_namespace builder options, or generate_for for individual libraries.',
    );
  }
  final database = options.config['database'];
  if (database != null && !SqlDialect.values.any((d) => d.name == database)) {
    throw ArgumentError('database must be sqlite, postgres, mysql or mariadb.');
  }
  final dialect = database == null
      ? null
      : SqlDialect.values.byName(database as String);
  final defaultNamespace = options.config['default_namespace'];
  if (defaultNamespace != null && defaultNamespace is! String) {
    throw ArgumentError('default_namespace must be a string.');
  }
  final models = options.config['models'];
  if (models != null) {
    if (models is! String) {
      throw ArgumentError('models must be a source root under lib/.');
    }
    final root = SchemaLayout.stem(models, paths: p.url);
    if (!p.url.isWithin('lib', root)) {
      throw ArgumentError('Put the model source root under lib/.');
    }
    return _OrmDirectoryBuilder(
      root,
      dialect,
      defaultNamespace: defaultNamespace as String?,
    );
  }
  return _OrmBuilder(
    dialect: dialect,
    defaultNamespace: defaultNamespace as String?,
  );
}