-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate built value example to a modular builder
- Loading branch information
Showing
6 changed files
with
233 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,16 @@ | ||
targets: | ||
$default: | ||
drift: | ||
auto_apply_builders: false | ||
builders: | ||
# disables the SharedPartBuilder in favor of a PartBuilder from drift_dev | ||
drift_dev: | ||
enabled: false | ||
drift_dev:preparing_builder: | ||
enabled: true | ||
drift_dev:not_shared: | ||
drift_dev:modular: | ||
enabled: true | ||
|
||
# Run built_value_generator when drift is done (so after this target) | ||
built_value_generator:built_value: | ||
enabled: false | ||
|
||
run_built_value: | ||
dependencies: ['$default'] | ||
$default: | ||
dependencies: | ||
# run drift's builder first | ||
- ":drift" | ||
builders: | ||
# Disable all auto-applied builders from drift | ||
# This builder is enabled by default, but we're using the modular builder in | ||
# its own target instead. | ||
drift_dev: | ||
enabled: false | ||
drift_dev:preparing_builder: | ||
enabled: false | ||
build_resolvers:transitive_digests: | ||
enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,183 +1,13 @@ | ||
// ignore_for_file: type=lint | ||
part of 'database.dart'; | ||
import 'package:drift/drift.dart' as i0; | ||
import 'package:with_built_value/tables.drift.dart' as i1; | ||
|
||
class Users extends Table with TableInfo<Users, User> { | ||
abstract class $Database extends i0.GeneratedDatabase { | ||
$Database(i0.QueryExecutor e) : super(e); | ||
late final i1.Users users = i1.Users(this); | ||
@override | ||
final GeneratedDatabase attachedDatabase; | ||
final String? _alias; | ||
Users(this.attachedDatabase, [this._alias]); | ||
static const VerificationMeta _idMeta = const VerificationMeta('id'); | ||
late final GeneratedColumn<int> id = GeneratedColumn<int>( | ||
'id', aliasedName, false, | ||
hasAutoIncrement: true, | ||
type: DriftSqlType.int, | ||
requiredDuringInsert: false, | ||
$customConstraints: 'NOT NULL PRIMARY KEY AUTOINCREMENT'); | ||
static const VerificationMeta _nameMeta = const VerificationMeta('name'); | ||
late final GeneratedColumn<String> name = GeneratedColumn<String>( | ||
'name', aliasedName, false, | ||
type: DriftSqlType.string, | ||
requiredDuringInsert: true, | ||
$customConstraints: 'NOT NULL'); | ||
Iterable<i0.TableInfo<i0.Table, Object?>> get allTables => | ||
allSchemaEntities.whereType<i0.TableInfo<i0.Table, Object?>>(); | ||
@override | ||
List<GeneratedColumn> get $columns => [id, name]; | ||
@override | ||
String get aliasedName => _alias ?? actualTableName; | ||
@override | ||
String get actualTableName => $name; | ||
static const String $name = 'users'; | ||
@override | ||
VerificationContext validateIntegrity(Insertable<User> instance, | ||
{bool isInserting = false}) { | ||
final context = VerificationContext(); | ||
final data = instance.toColumns(true); | ||
if (data.containsKey('id')) { | ||
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta)); | ||
} | ||
if (data.containsKey('name')) { | ||
context.handle( | ||
_nameMeta, name.isAcceptableOrUnknown(data['name']!, _nameMeta)); | ||
} else if (isInserting) { | ||
context.missing(_nameMeta); | ||
} | ||
return context; | ||
} | ||
|
||
@override | ||
Set<GeneratedColumn> get $primaryKey => {id}; | ||
@override | ||
User map(Map<String, dynamic> data, {String? tablePrefix}) { | ||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; | ||
return User( | ||
id: attachedDatabase.typeMapping | ||
.read(DriftSqlType.int, data['${effectivePrefix}id'])!, | ||
name: attachedDatabase.typeMapping | ||
.read(DriftSqlType.string, data['${effectivePrefix}name'])!, | ||
); | ||
} | ||
|
||
@override | ||
Users createAlias(String alias) { | ||
return Users(attachedDatabase, alias); | ||
} | ||
|
||
@override | ||
bool get dontWriteConstraints => true; | ||
} | ||
|
||
class User extends DataClass implements Insertable<User> { | ||
final int id; | ||
final String name; | ||
const User({required this.id, required this.name}); | ||
@override | ||
Map<String, Expression> toColumns(bool nullToAbsent) { | ||
final map = <String, Expression>{}; | ||
map['id'] = Variable<int>(id); | ||
map['name'] = Variable<String>(name); | ||
return map; | ||
} | ||
|
||
UsersCompanion toCompanion(bool nullToAbsent) { | ||
return UsersCompanion( | ||
id: Value(id), | ||
name: Value(name), | ||
); | ||
} | ||
|
||
factory User.fromJson(Map<String, dynamic> json, | ||
{ValueSerializer? serializer}) { | ||
serializer ??= driftRuntimeOptions.defaultSerializer; | ||
return User( | ||
id: serializer.fromJson<int>(json['id']), | ||
name: serializer.fromJson<String>(json['name']), | ||
); | ||
} | ||
@override | ||
Map<String, dynamic> toJson({ValueSerializer? serializer}) { | ||
serializer ??= driftRuntimeOptions.defaultSerializer; | ||
return <String, dynamic>{ | ||
'id': serializer.toJson<int>(id), | ||
'name': serializer.toJson<String>(name), | ||
}; | ||
} | ||
|
||
User copyWith({int? id, String? name}) => User( | ||
id: id ?? this.id, | ||
name: name ?? this.name, | ||
); | ||
@override | ||
String toString() { | ||
return (StringBuffer('User(') | ||
..write('id: $id, ') | ||
..write('name: $name') | ||
..write(')')) | ||
.toString(); | ||
} | ||
|
||
@override | ||
int get hashCode => Object.hash(id, name); | ||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
(other is User && other.id == this.id && other.name == this.name); | ||
} | ||
|
||
class UsersCompanion extends UpdateCompanion<User> { | ||
final Value<int> id; | ||
final Value<String> name; | ||
const UsersCompanion({ | ||
this.id = const Value.absent(), | ||
this.name = const Value.absent(), | ||
}); | ||
UsersCompanion.insert({ | ||
this.id = const Value.absent(), | ||
required String name, | ||
}) : name = Value(name); | ||
static Insertable<User> custom({ | ||
Expression<int>? id, | ||
Expression<String>? name, | ||
}) { | ||
return RawValuesInsertable({ | ||
if (id != null) 'id': id, | ||
if (name != null) 'name': name, | ||
}); | ||
} | ||
|
||
UsersCompanion copyWith({Value<int>? id, Value<String>? name}) { | ||
return UsersCompanion( | ||
id: id ?? this.id, | ||
name: name ?? this.name, | ||
); | ||
} | ||
|
||
@override | ||
Map<String, Expression> toColumns(bool nullToAbsent) { | ||
final map = <String, Expression>{}; | ||
if (id.present) { | ||
map['id'] = Variable<int>(id.value); | ||
} | ||
if (name.present) { | ||
map['name'] = Variable<String>(name.value); | ||
} | ||
return map; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return (StringBuffer('UsersCompanion(') | ||
..write('id: $id, ') | ||
..write('name: $name') | ||
..write(')')) | ||
.toString(); | ||
} | ||
} | ||
|
||
abstract class _$Database extends GeneratedDatabase { | ||
_$Database(QueryExecutor e) : super(e); | ||
late final Users users = Users(this); | ||
@override | ||
Iterable<TableInfo<Table, Object?>> get allTables => | ||
allSchemaEntities.whereType<TableInfo<Table, Object?>>(); | ||
@override | ||
List<DatabaseSchemaEntity> get allSchemaEntities => [users]; | ||
List<i0.DatabaseSchemaEntity> get allSchemaEntities => [users]; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.