Skip to content

Commit

Permalink
Remove field adapters for knex and mongoose (#5273)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 30, 2021
1 parent c28e765 commit 4fa66ac
Show file tree
Hide file tree
Showing 34 changed files with 112 additions and 847 deletions.
8 changes: 8 additions & 0 deletions .changeset/silly-cows-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@keystone-next/fields-document': major
'@keystone-next/fields-legacy': major
'@keystone-next/fields-auto-increment-legacy': major
'@keystone-next/fields-cloudinary-image-legacy': major
---

Removed support for the `knex` and `mongoose` field adapters.
2 changes: 0 additions & 2 deletions packages-next/fields-document/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
"@babel/runtime": "^7.13.10",
"@braintree/sanitize-url": "^5.0.0",
"@emotion/weak-memoize": "^0.2.5",
"@keystone-next/adapter-knex-legacy": "^13.2.3",
"@keystone-next/adapter-mongoose-legacy": "^11.1.3",
"@keystone-next/adapter-prisma-legacy": "^4.0.1",
"@keystone-next/admin-ui": "^12.0.1",
"@keystone-next/admin-ui-utils": "^3.0.2",
Expand Down
47 changes: 5 additions & 42 deletions packages-next/fields-document/src/Implementation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { MongooseFieldAdapter } from '@keystone-next/adapter-mongoose-legacy';
import { KnexFieldAdapter } from '@keystone-next/adapter-knex-legacy';
import { PrismaFieldAdapter } from '@keystone-next/adapter-prisma-legacy';
import { Implementation } from '@keystone-next/fields-legacy';
// eslint-disable-next-line import/no-unresolved
Expand Down Expand Up @@ -72,10 +70,6 @@ export class DocumentImplementation extends Implementation {
return undefined;
}
const nodes = this.config.___validateAndNormalize(data);
if (this.adapter.constructor === KnexDocumentInterface) {
// knex expects the json to be stringified
return JSON.stringify(nodes);
}
if (this.adapter.listAdapter.parentAdapter.provider === 'sqlite') {
// we store document data as a string on sqlite because Prisma doesn't support Json on sqlite
// https://github.com/prisma/prisma/issues/3786
Expand All @@ -95,42 +89,7 @@ export class DocumentImplementation extends Implementation {
}
}

const CommonDocumentInterface = superclass =>
class extends superclass {
getQueryConditions() {
return {};
}
};

export class MongoDocumentInterface extends CommonDocumentInterface(MongooseFieldAdapter) {
addToMongooseSchema(schema) {
const schemaOptions = { type: Object };
schema.add({ [this.path]: this.mergeSchemaOptions(schemaOptions, this.config) });
}
}

export class KnexDocumentInterface extends CommonDocumentInterface(KnexFieldAdapter) {
constructor() {
super(...arguments);

// Error rather than ignoring invalid config
// We totally can index these values, it's just not trivial. See issue #1297
if (this.config.isIndexed) {
throw new Error(
`The Document field type doesn't support indexes on Knex. ` +
`Check the config for ${this.path} on the ${this.field.listKey} list`
);
}
}

addToTableSchema(table) {
const column = table.jsonb(this.path);
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
}
}

export class PrismaDocumentInterface extends CommonDocumentInterface(PrismaFieldAdapter) {
export class PrismaDocumentInterface extends PrismaFieldAdapter {
constructor() {
super(...arguments);
// Error rather than ignoring invalid config
Expand All @@ -155,4 +114,8 @@ export class PrismaDocumentInterface extends CommonDocumentInterface(PrismaField
}),
];
}

getQueryConditions() {
return {};
}
}
9 changes: 1 addition & 8 deletions packages-next/fields-document/src/base-field-type.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import {
DocumentImplementation,
PrismaDocumentInterface,
KnexDocumentInterface,
MongoDocumentInterface,
} from './Implementation';
import { DocumentImplementation, PrismaDocumentInterface } from './Implementation';

export const DocumentFieldType = {
type: 'Document',
implementation: DocumentImplementation,
adapters: {
prisma: PrismaDocumentInterface,
knex: KnexDocumentInterface,
mongoose: MongoDocumentInterface,
},
};
1 change: 0 additions & 1 deletion packages/fields-auto-increment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
},
"dependencies": {
"@babel/runtime": "^7.13.10",
"@keystone-next/adapter-knex-legacy": "^13.2.3",
"@keystone-next/adapter-prisma-legacy": "^4.0.1",
"@keystone-next/fields-legacy": "^24.0.0"
},
Expand Down
57 changes: 0 additions & 57 deletions packages/fields-auto-increment/src/Implementation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Implementation } from '@keystone-next/fields-legacy';
import { KnexFieldAdapter } from '@keystone-next/adapter-knex-legacy';
import { PrismaFieldAdapter } from '@keystone-next/adapter-prisma-legacy';

export class AutoIncrementImplementation extends Implementation {
Expand Down Expand Up @@ -56,62 +55,6 @@ export class AutoIncrementImplementation extends Implementation {
}
}

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

// Default isUnique to true if not specified
this.isUnique = typeof this.config.isUnique === 'undefined' ? true : !!this.config.isUnique;
this.isIndexed = !!this.config.isIndexed && !this.config.isUnique;
}

// Override isNotNullable defaulting logic; default to true if not specified
get isNotNullable() {
if (this._isNotNullable) return this._isNotNullable;
return (this._isNotNullable = !!(typeof this.knexOptions.isNotNullable === 'undefined'
? true
: this.knexOptions.isNotNullable));
}

addToTableSchema(table) {
// The knex `increments()` schema building function always uses the column as the primary key
// If not isPrimaryKey use a raw `serial` instead
// This will only work on PostgreSQL; see README.md
if (this.field.isPrimaryKey) {
// Fair to say primary keys are always non-nullable and uniqueness is implied by primary()
table.increments(this.path).notNullable();
// TODO: Warning on invalid primary key config options?
} else {
const column = table.specificType(this.path, 'serial');
if (this.isUnique) column.unique();
else if (this.isIndexed) column.index();
if (this.isNotNullable) column.notNullable();
}
}

addToForeignTableSchema(table, { path, isUnique, isIndexed, isNotNullable }) {
if (!this.field.isPrimaryKey) {
throw (
`Can't create foreign key '${path}' on table "${table._tableName}"; ` +
`'${this.path}' on list '${this.field.listKey}' as is not the primary key.`
);
}

const column = table.integer(path).unsigned();
if (isUnique) column.unique();
else if (isIndexed) column.index();
if (isNotNullable) column.notNullable();
}

getQueryConditions(dbPath) {
return {
...this.equalityConditions(dbPath),
...this.orderingConditions(dbPath),
...this.inConditions(dbPath),
};
}
}

export class PrismaAutoIncrementInterface extends PrismaFieldAdapter {
constructor() {
super(...arguments);
Expand Down
11 changes: 1 addition & 10 deletions packages/fields-auto-increment/src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import {
AutoIncrementImplementation,
KnexAutoIncrementInterface,
PrismaAutoIncrementInterface,
} from './Implementation';
import { AutoIncrementImplementation, PrismaAutoIncrementInterface } from './Implementation';

export const AutoIncrement = {
type: 'AutoIncrement',
implementation: AutoIncrementImplementation,
adapters: {
knex: KnexAutoIncrementInterface,
prisma: PrismaAutoIncrementInterface,
},

primaryKeyDefaults: {
knex: {
// Uniqueness, non-nullability and GraphQL type are implied
getConfig: () => ({ type: AutoIncrement }),
},
prisma: {
// Uniqueness, non-nullability and GraphQL type are implied
getConfig: () => ({ type: AutoIncrement }),
Expand Down
2 changes: 0 additions & 2 deletions packages/fields-cloudinary-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"node": ">=10.0.0"
},
"dependencies": {
"@keystone-next/adapter-knex-legacy": "^13.2.3",
"@keystone-next/adapter-mongoose-legacy": "^11.1.3",
"@keystone-next/fields-legacy": "^24.0.0"
},
"main": "dist/fields-cloudinary-image-legacy.cjs.js",
Expand Down
9 changes: 1 addition & 8 deletions packages/fields-cloudinary-image/src/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ class CloudinaryImage extends File.implementation {
}
}

const MongoCloudinaryImageInterface = File.adapters.mongoose;
const KnexCloudinaryImageInterface = File.adapters.knex;
const PrismaCloudinaryImageInterface = File.adapters.prisma;

export {
CloudinaryImage,
MongoCloudinaryImageInterface,
KnexCloudinaryImageInterface,
PrismaCloudinaryImageInterface,
};
export { CloudinaryImage, PrismaCloudinaryImageInterface };
4 changes: 0 additions & 4 deletions packages/fields-cloudinary-image/src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import {
CloudinaryImage as Implementation,
MongoCloudinaryImageInterface,
KnexCloudinaryImageInterface,
PrismaCloudinaryImageInterface,
} from './Implementation';

export const CloudinaryImage = {
type: 'CloudinaryImage',
implementation: Implementation,
adapters: {
mongoose: MongoCloudinaryImageInterface,
knex: KnexCloudinaryImageInterface,
prisma: PrismaCloudinaryImageInterface,
},
};
3 changes: 0 additions & 3 deletions packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
"dependencies": {
"@babel/runtime": "^7.13.10",
"@keystone-next/access-control-legacy": "^9.0.1",
"@keystone-next/adapter-knex-legacy": "^13.2.3",
"@keystone-next/adapter-mongoose-legacy": "^11.1.3",
"@keystone-next/adapter-prisma-legacy": "^4.0.1",
"@keystone-next/fields": "5.4.0",
"@keystone-next/utils-legacy": "^8.0.0",
Expand All @@ -27,7 +25,6 @@
"dumb-passwords": "^0.2.1",
"inflection": "^1.12.0",
"lodash.groupby": "^4.6.0",
"luxon": "^1.26.0",
"mongoose": "^5.12.2",
"p-settle": "^4.1.1"
},
Expand Down
33 changes: 0 additions & 33 deletions packages/fields/src/types/Checkbox/Implementation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { MongooseFieldAdapter } from '@keystone-next/adapter-mongoose-legacy';
import { KnexFieldAdapter } from '@keystone-next/adapter-knex-legacy';
import { PrismaFieldAdapter } from '@keystone-next/adapter-prisma-legacy';
import { Implementation } from '../../Implementation';

Expand Down Expand Up @@ -34,37 +32,6 @@ export class Checkbox extends Implementation {
}
}

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 KnexFieldAdapter {
constructor() {
super(...arguments);

// Error rather than ignoring invalid config
if (this.config.isIndexed) {
throw (
`The Checkbox field type doesn't support indexes on Knex. ` +
`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 (typeof this.defaultTo !== 'undefined') column.defaultTo(this.defaultTo);
}
getQueryConditions(dbPath) {
return this.equalityConditions(dbPath);
}
}

export class PrismaCheckboxInterface extends PrismaFieldAdapter {
constructor() {
super(...arguments);
Expand Down
9 changes: 1 addition & 8 deletions packages/fields/src/types/Checkbox/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import {
Checkbox,
MongoCheckboxInterface,
KnexCheckboxInterface,
PrismaCheckboxInterface,
} from './Implementation';
import { Checkbox, PrismaCheckboxInterface } from './Implementation';

export default {
type: 'Checkbox',
implementation: Checkbox,
adapters: {
mongoose: MongoCheckboxInterface,
knex: KnexCheckboxInterface,
prisma: PrismaCheckboxInterface,
},
};
52 changes: 0 additions & 52 deletions packages/fields/src/types/DateTimeUtc/Implementation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { DateTime } from 'luxon';
import { KnexFieldAdapter } from '@keystone-next/adapter-knex-legacy';
import { PrismaFieldAdapter } from '@keystone-next/adapter-prisma-legacy';
import { MongooseFieldAdapter } from '@keystone-next/adapter-mongoose-legacy';
import { Implementation } from '../../Implementation';

export class DateTimeUtcImplementation extends Implementation {
Expand Down Expand Up @@ -43,55 +40,6 @@ export class DateTimeUtcImplementation extends Implementation {
}
}

// All values must have an offset
const toDate = str => {
if (str === null) {
return null;
}
if (!str.match(/([zZ]|[\+\-][0-9]+(\:[0-9]+)?)$/)) {
throw `Value supplied (${str}) is not a valid date time with offset.`;
}
return DateTime.fromISO(str).toJSDate();
};

export class MongoDateTimeUtcInterface extends MongooseFieldAdapter {
addToMongooseSchema(schema) {
schema.add({ [this.path]: this.mergeSchemaOptions({ type: Date }, this.config) });
}
getQueryConditions(dbPath) {
return {
...this.equalityConditions(dbPath, toDate),
...this.orderingConditions(dbPath, toDate),
...this.inConditions(dbPath, toDate),
};
}
}

export class KnexDateTimeUtcInterface extends KnexFieldAdapter {
constructor() {
super(...arguments);
this.isUnique = !!this.config.isUnique;
this.isIndexed = !!this.config.isIndexed && !this.config.isUnique;
}
addToTableSchema(table) {
// It's important we don't exceed the precision of native Date
// objects (ms) or JS will silently round values down.
const column = table.timestamp(this.path, { useTz: true, precision: 3 });

if (this.isUnique) column.unique();
else if (this.isIndexed) column.index();
if (this.isNotNullable) column.notNullable();
if (this.defaultTo) column.defaultTo(this.defaultTo);
}
getQueryConditions(dbPath) {
return {
...this.equalityConditions(dbPath, toDate),
...this.orderingConditions(dbPath, toDate),
...this.inConditions(dbPath, toDate),
};
}
}

export class PrismaDateTimeUtcInterface extends PrismaFieldAdapter {
constructor() {
super(...arguments);
Expand Down
Loading

1 comment on commit 4fa66ac

@vercel
Copy link

@vercel vercel bot commented on 4fa66ac Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.