Write column getter into schema file (#1739)

This commit is contained in:
Simon Binder 2022-03-09 23:02:52 +01:00
parent 5def49b1fa
commit b25921a362
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 16 additions and 3 deletions

View File

@ -97,6 +97,7 @@ class SchemaWriter {
Map _columnData(MoorColumn column) {
return {
'name': column.name.name,
'getter_name': column.dartGetterName,
'moor_type': column.type.toString(),
'nullable': column.nullable,
'customConstraints': column.customConstraints,
@ -286,12 +287,13 @@ class SchemaReader {
for (final feature in data['dsl_features'] as List<dynamic>)
_columnFeature(feature)
];
final getterName = data['getter_name'] as String?;
// Note: Not including client default code because that usually depends on
// imports from the database.
return MoorColumn(
name: ColumnName.explicitly(name),
dartGetterName: ReCase(name).camelCase,
dartGetterName: getterName ?? ReCase(name).camelCase,
type: moorType,
nullable: nullable,
defaultArgument: data['default_dart'] as String?,

View File

@ -47,7 +47,7 @@ import 'package:drift/drift.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
TextColumn get settings => text().map(const SettingsConverter())();
TextColumn get settings => text().named('setting').map(const SettingsConverter())();
}
class Settings {}
@ -104,6 +104,7 @@ const expected = r'''
"columns":[
{
"name":"id",
"getter_name": "id",
"moor_type":"ColumnType.integer",
"nullable":false,
"customConstraints":"NOT NULL PRIMARY KEY AUTOINCREMENT",
@ -116,6 +117,7 @@ const expected = r'''
},
{
"name":"name",
"getter_name": "name",
"moor_type":"ColumnType.text",
"nullable":false,
"customConstraints":"NOT NULL",
@ -142,6 +144,7 @@ const expected = r'''
"columns":[
{
"name":"id",
"getter_name": "id",
"moor_type":"ColumnType.integer",
"nullable":false,
"customConstraints":null,
@ -154,6 +157,7 @@ const expected = r'''
},
{
"name":"name",
"getter_name": "name",
"moor_type":"ColumnType.text",
"nullable":false,
"customConstraints":null,
@ -164,7 +168,8 @@ const expected = r'''
]
},
{
"name":"settings",
"name":"setting",
"getter_name": "settings",
"moor_type":"ColumnType.text",
"nullable":false,
"customConstraints":null,
@ -195,6 +200,7 @@ const expected = r'''
"columns":[
{
"name":"group",
"getter_name": "group",
"moor_type":"ColumnType.integer",
"nullable":false,
"customConstraints":"NOT NULL REFERENCES \"groups\"(id)",
@ -206,6 +212,7 @@ const expected = r'''
},
{
"name":"user",
"getter_name": "user",
"moor_type":"ColumnType.integer",
"nullable":false,
"customConstraints":"NOT NULL REFERENCES users(id)",
@ -217,6 +224,7 @@ const expected = r'''
},
{
"name":"is_admin",
"getter_name": "isAdmin",
"moor_type":"ColumnType.boolean",
"nullable":false,
"customConstraints":"NOT NULL DEFAULT FALSE",
@ -276,6 +284,7 @@ const expected = r'''
"columns": [
{
"name": "sender",
"getter_name": "sender",
"moor_type": "ColumnType.text",
"nullable": false,
"customConstraints": "",
@ -285,6 +294,7 @@ const expected = r'''
},
{
"name": "title",
"getter_name": "title",
"moor_type": "ColumnType.text",
"nullable": false,
"customConstraints": "",
@ -294,6 +304,7 @@ const expected = r'''
},
{
"name": "body",
"getter_name": "body",
"moor_type": "ColumnType.text",
"nullable": false,
"customConstraints": "",