Skip to content

Commit

Permalink
Fixing field config application for knex field adapters (keystonejs#1422
Browse files Browse the repository at this point in the history
)

* Fixing dumb bug; falsey defaults not being applied correctly

* Re-enable explicit nullness for singular relationship on Knex

* Changeset
  • Loading branch information
molomby authored and jesstelford committed Jul 18, 2019
1 parent 1a980b0 commit 898265c
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .changeset/mighty-tools-worry/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"releases": [
{ "name": "@keystone-alpha/adapter-knex", "type": "patch" },
{ "name": "@keystone-alpha/fields", "type": "patch" }
],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/mighty-tools-worry/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixing application of some field config on knex
2 changes: 1 addition & 1 deletion packages/adapter-knex/lib/adapter-knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ class KnexFieldAdapter extends BaseFieldAdapter {
if (this._isNotNullable) return this._isNotNullable;

return (this._isNotNullable = !!(typeof this.knexOptions.isNotNullable === 'undefined'
? this.field.isRequired
? this.field.isRequired || (typeof this.defaultTo !== 'undefined' && this.defaultTo !== null)
: this.knexOptions.isNotNullable));
}

Expand Down
20 changes: 9 additions & 11 deletions packages/fields/src/types/Checkbox/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@ export class Checkbox extends Implementation {
}
}

const CommonCheckboxInterface = superclass =>
class extends superclass {
getQueryConditions(dbPath) {
return this.equalityConditions(dbPath);
}
};

export class MongoCheckboxInterface extends CommonCheckboxInterface(MongooseFieldAdapter) {
export class MongoCheckboxInterface extends MongooseFieldAdapter {
addToMongooseSchema(schema) {
schema.add({ [this.path]: this.mergeSchemaOptions({ type: Boolean }, this.config) });
}
getQueryConditions(dbPath) {
return this.equalityConditions(dbPath);
}
}

export class KnexCheckboxInterface extends CommonCheckboxInterface(KnexFieldAdapter) {
export class KnexCheckboxInterface extends KnexFieldAdapter {
constructor() {
super(...arguments);

Expand All @@ -48,10 +44,12 @@ export class KnexCheckboxInterface extends CommonCheckboxInterface(KnexFieldAdap
`Check the config for ${this.path} on the ${this.field.listKey} list`;
}
}

addToTableSchema(table) {
const column = table.boolean(this.path);
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
if (typeof this.defaultTo !== 'undefined') column.defaultTo(this.defaultTo);
}
getQueryConditions(dbPath) {
return this.equalityConditions(dbPath);
}
}
2 changes: 1 addition & 1 deletion packages/fields/src/types/Decimal/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class KnexDecimalInterface extends KnexFieldAdapter {
if (this.isUnique) column.unique();
else if (this.isIndexed) column.index();
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
if (typeof this.defaultTo !== 'undefined') column.defaultTo(this.defaultTo);
}

getQueryConditions(dbPath) {
Expand Down
2 changes: 1 addition & 1 deletion packages/fields/src/types/Float/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ export class KnexFloatInterface extends CommonFloatInterface(KnexFieldAdapter) {
if (this.isUnique) column.unique();
else if (this.isIndexed) column.index();
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
if (typeof this.defaultTo !== 'undefined') column.defaultTo(this.defaultTo);
}
}
2 changes: 1 addition & 1 deletion packages/fields/src/types/Integer/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ export class KnexIntegerInterface extends CommonIntegerInterface(KnexFieldAdapte
if (this.isUnique) column.unique();
else if (this.isIndexed) column.index();
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
if (typeof this.defaultTo !== 'undefined') column.defaultTo(this.defaultTo);
}
}
2 changes: 1 addition & 1 deletion packages/fields/src/types/Relationship/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export class KnexRelationshipInterface extends KnexFieldAdapter {
path: this.path,
isUnique: this.isUnique,
isIndexed: this.isIndexed,
isNotNullable: false, // See validation above
isNotNullable: this.isNotNullable,
};
refId.adapter.addToForeignTableSchema(table, foreignKeyConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fields/src/types/Select/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ export class KnexSelectInterface extends CommonSelectInterface(KnexFieldAdapter)
if (this.isUnique) column.unique();
else if (this.isIndexed) column.index();
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
if (typeof this.defaultTo !== 'undefined') column.defaultTo(this.defaultTo);
}
}
2 changes: 1 addition & 1 deletion packages/fields/src/types/Text/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ export class KnexTextInterface extends CommonTextInterface(KnexFieldAdapter) {
if (this.isUnique) column.unique();
else if (this.isIndexed) column.index();
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
if (typeof this.defaultTo !== 'undefined') column.defaultTo(this.defaultTo);
}
}

0 comments on commit 898265c

Please sign in to comment.