Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deprecate accessing DataTypes directly on Sequelize class #14373

Merged
merged 4 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/associations/belongs-to-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { Op } = require('../operators');
*
* ```js
* UserProject = sequelize.define('user_project', {
* role: Sequelize.STRING
* role: DataTypes.STRING
* });
* User.belongsToMany(Project, { through: UserProject });
* Project.belongsToMany(User, { through: UserProject });
Expand Down
2 changes: 1 addition & 1 deletion src/deferrable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type AbstractQueryGenerator = any;
* class MyModel extends Model {}
* MyModel.init({
* foreign_id: {
* type: Sequelize.INTEGER,
* type: DataTypes.INTEGER,
* references: {
* model: OtherModel,
* key: 'id',
Expand Down
16 changes: 8 additions & 8 deletions src/dialects/abstract/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,26 @@ export class QueryInterface {
* 'nameOfTheNewTable',
* {
* id: {
* type: Sequelize.INTEGER,
* type: DataTypes.INTEGER,
* primaryKey: true,
* autoIncrement: true
* },
* createdAt: {
* type: Sequelize.DATE
* type: DataTypes.DATE
* },
* updatedAt: {
* type: Sequelize.DATE
* type: DataTypes.DATE
* },
* attr1: Sequelize.STRING,
* attr2: Sequelize.INTEGER,
* attr1: DataTypes.STRING,
* attr2: DataTypes.INTEGER,
* attr3: {
* type: Sequelize.BOOLEAN,
* type: DataTypes.BOOLEAN,
* defaultValue: false,
* allowNull: false
* },
* //foreign key usage
* attr4: {
* type: Sequelize.INTEGER,
* type: DataTypes.INTEGER,
* references: {
* model: 'another_table_name',
* key: 'id'
Expand Down Expand Up @@ -395,7 +395,7 @@ export class QueryInterface {
* Add a new column to a table
*
* ```js
* queryInterface.addColumn('tableA', 'columnC', Sequelize.STRING, {
* queryInterface.addColumn('tableA', 'columnC', DataTypes.STRING, {
* after: 'columnB' // after option is only supported by MySQL
* });
* ```
Expand Down
4 changes: 2 additions & 2 deletions src/sequelize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ export class Sequelize extends Hooks {
* class MyModel extends Model {}
* MyModel.init({
* columnA: {
* type: Sequelize.BOOLEAN,
* type: DataTypes.BOOLEAN,
* validate: {
* is: ["[a-z]",'i'], // will only allow letters
* max: 23, // only allow values <= 23
Expand All @@ -1177,7 +1177,7 @@ export class Sequelize extends Hooks {
* field: 'column_a'
* // Other attributes here
* },
* columnB: Sequelize.STRING,
* columnB: DataTypes.STRING,
* columnC: 'MY VERY OWN COLUMN TYPE'
* }, { sequelize })
*
Expand Down
15 changes: 11 additions & 4 deletions src/sequelize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import { noSequelizeDataType } from './utils/deprecations';
import { isSameInitialModel, isModelStatic } from './utils/model-utils';

const url = require('url');
Expand Down Expand Up @@ -439,7 +440,7 @@ export class Sequelize {
* @example
* sequelize.define('modelName', {
* columnA: {
* type: Sequelize.BOOLEAN,
* type: DataTypes.BOOLEAN,
* validate: {
* is: ["[a-z]",'i'], // will only allow letters
* max: 23, // only allow values <= 23
Expand All @@ -450,7 +451,7 @@ export class Sequelize {
* },
* field: 'column_a'
* },
* columnB: Sequelize.STRING,
* columnB: DataTypes.STRING,
* columnC: 'MY VERY OWN COLUMN TYPE'
* });
*
Expand Down Expand Up @@ -1269,8 +1270,14 @@ Sequelize.HasMany = HasMany;
Sequelize.BelongsToMany = BelongsToMany;

Sequelize.DataTypes = DataTypes;
for (const dataType in DataTypes) {
Sequelize[dataType] = DataTypes[dataType];
for (const dataTypeName in DataTypes) {
Object.defineProperty(Sequelize, dataTypeName, {
get() {
noSequelizeDataType();

return DataTypes[dataTypeName];
},
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/utils/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const unsupportedEngine = deprecate(noop, 'This database engine version i
export const useErrorCause = deprecate(noop, 'The "parent" and "original" properties in Sequelize errors have been replaced with the native "cause" property. Use that one instead.', 'SEQUELIZE0007');
export const scopeRenamedToWithScope = deprecate(noop, 'Model.scope has been renamed to Model.withScope, and Model.unscoped has been renamed to Model.withoutScope', 'SEQUELIZE0008');
export const schemaRenamedToWithSchema = deprecate(noop, 'Model.schema has been renamed to Model.withSchema', 'SEQUELIZE0009');
export const noSequelizeDataType = deprecate(noop, `Accessing DataTypes on the Sequelize constructor is deprecated. Use the DataTypes object instead.
e.g, instead of using Sequelize.STRING, use DataTypes.STRING`, 'SEQUELIZE0010');
40 changes: 20 additions & 20 deletions test/integration/associations/belongs-to-many.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
const User = this.sequelize.define('user', {});
const Group = this.sequelize.define('group', {});
const UserGroups = this.sequelize.define('user_groups', {
isAdmin: Sequelize.BOOLEAN,
isAdmin: DataTypes.BOOLEAN,
});

User.belongsToMany(Group, { through: UserGroups });
Expand Down Expand Up @@ -1978,7 +1978,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
const Task = sequelize.define('Task', { title: DataTypes.STRING });

const UserTask = sequelize.define('UserTask', {
status: Sequelize.STRING,
status: DataTypes.STRING,
});

User.belongsToMany(Task, { through: UserTask });
Expand Down Expand Up @@ -2181,16 +2181,16 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
describe('through model validations', () => {
beforeEach(async function () {
const Project = this.sequelize.define('Project', {
name: Sequelize.STRING,
name: DataTypes.STRING,
});

const Employee = this.sequelize.define('Employee', {
name: Sequelize.STRING,
name: DataTypes.STRING,
});

const Participation = this.sequelize.define('Participation', {
role: {
type: Sequelize.STRING,
type: DataTypes.STRING,
allowNull: false,
validate: {
len: {
Expand Down Expand Up @@ -2620,14 +2620,14 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {

this.UserTasks = this.sequelize.define('usertasks', {
id: {
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
autoincrement: true,
primaryKey: true,
},
});
this.UserTasks2 = this.sequelize.define('usertasks2', {
userTasksId: {
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
autoincrement: true,
primaryKey: true,
},
Expand Down Expand Up @@ -2927,9 +2927,9 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
});

it('should be able to create an instance along with its many-to-many association which has an extra column in the junction table', async function () {
const Foo = this.sequelize.define('foo', { name: Sequelize.STRING });
const Bar = this.sequelize.define('bar', { name: Sequelize.STRING });
const FooBar = this.sequelize.define('foobar', { baz: Sequelize.STRING });
const Foo = this.sequelize.define('foo', { name: DataTypes.STRING });
const Bar = this.sequelize.define('bar', { name: DataTypes.STRING });
const FooBar = this.sequelize.define('foobar', { baz: DataTypes.STRING });
Foo.belongsToMany(Bar, { through: FooBar });
Bar.belongsToMany(Foo, { through: FooBar });

Expand Down Expand Up @@ -3093,8 +3093,8 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {

describe('belongsTo and hasMany at once', () => {
beforeEach(function () {
this.A = this.sequelize.define('a', { name: Sequelize.STRING });
this.B = this.sequelize.define('b', { name: Sequelize.STRING });
this.A = this.sequelize.define('a', { name: DataTypes.STRING });
this.B = this.sequelize.define('b', { name: DataTypes.STRING });
});

describe('source belongs to target', () => {
Expand Down Expand Up @@ -3207,8 +3207,8 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {

describe('multiple hasMany', () => {
beforeEach(function () {
this.User = this.sequelize.define('user', { name: Sequelize.STRING });
this.Project = this.sequelize.define('project', { projectName: Sequelize.STRING });
this.User = this.sequelize.define('user', { name: DataTypes.STRING });
this.Project = this.sequelize.define('project', { projectName: DataTypes.STRING });
});

describe('project has owners and users and owners and users have projects', () => {
Expand Down Expand Up @@ -3411,7 +3411,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
const Project = this.sequelize.define('project', {});
const User = this.sequelize.define('user', {
uid: {
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
primaryKey: true,
},
});
Expand All @@ -3426,7 +3426,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {

it('should throw an error if foreignKey and as result in a name clash', function () {
const User = this.sequelize.define('user', {
user: Sequelize.INTEGER,
user: DataTypes.INTEGER,
});

expect(User.belongsToMany.bind(User, User, { as: 'user', through: 'UserUser' })).to
Expand All @@ -3437,7 +3437,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
describe('thisAssociations', () => {
it('should work with this reference', async function () {
const User = this.sequelize.define('User', {
name: Sequelize.STRING(100),
name: DataTypes.STRING(100),
});
const Follow = this.sequelize.define('Follow');

Expand All @@ -3461,7 +3461,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {

it('should work with custom this reference', async function () {
const User = this.sequelize.define('User', {
name: Sequelize.STRING(100),
name: DataTypes.STRING(100),
});
const UserFollowers = this.sequelize.define('UserFollower');

Expand Down Expand Up @@ -3527,10 +3527,10 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
describe('Eager loading', () => {
beforeEach(function () {
this.Individual = this.sequelize.define('individual', {
name: Sequelize.STRING,
name: DataTypes.STRING,
});
this.Hat = this.sequelize.define('hat', {
name: Sequelize.STRING,
name: DataTypes.STRING,
});
this.Event = this.sequelize.define('event', {});
this.Individual.belongsToMany(this.Hat, {
Expand Down
40 changes: 20 additions & 20 deletions test/integration/associations/belongs-to.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
}

it('should be able to handle a where object that\'s a first class citizen.', async function () {
const User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING });
const Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING });
const User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING, gender: DataTypes.STRING });
const Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING, status: DataTypes.STRING });

Task.belongsTo(User);

Expand All @@ -104,8 +104,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
});

it('supports schemas', async function () {
const User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING }).schema('archive');
const Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive');
const User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING, gender: DataTypes.STRING }).schema('archive');
const Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING, status: DataTypes.STRING }).schema('archive');

Task.belongsTo(User);

Expand All @@ -132,15 +132,15 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
it('supports schemas when defining custom foreign key attribute #9029', async function () {
const User = this.sequelize.define('UserXYZ', {
uid: {
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
}).schema('archive');
const Task = this.sequelize.define('TaskXYZ', {
user_id: {
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
references: { model: User, key: 'uid' },
},
}).schema('archive');
Expand Down Expand Up @@ -365,8 +365,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {

describe('foreign key', () => {
it('should setup underscored field with foreign keys when using underscored', function () {
const User = this.sequelize.define('User', { username: Sequelize.STRING }, { underscored: true });
const Account = this.sequelize.define('Account', { name: Sequelize.STRING }, { underscored: true });
const User = this.sequelize.define('User', { username: DataTypes.STRING }, { underscored: true });
const Account = this.sequelize.define('Account', { name: DataTypes.STRING }, { underscored: true });

User.belongsTo(Account);

Expand All @@ -375,8 +375,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
});

it('should use model name when using camelcase', function () {
const User = this.sequelize.define('User', { username: Sequelize.STRING }, { underscored: false });
const Account = this.sequelize.define('Account', { name: Sequelize.STRING }, { underscored: false });
const User = this.sequelize.define('User', { username: DataTypes.STRING }, { underscored: false });
const Account = this.sequelize.define('Account', { name: DataTypes.STRING }, { underscored: false });

User.belongsTo(Account);

Expand All @@ -385,8 +385,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
});

it('should support specifying the field of a foreign key', async function () {
const User = this.sequelize.define('User', { username: Sequelize.STRING }, { underscored: false });
const Account = this.sequelize.define('Account', { title: Sequelize.STRING }, { underscored: false });
const User = this.sequelize.define('User', { username: DataTypes.STRING }, { underscored: false });
const Account = this.sequelize.define('Account', { title: DataTypes.STRING }, { underscored: false });

User.belongsTo(Account, {
foreignKey: {
Expand Down Expand Up @@ -552,8 +552,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
});

it('should be possible to disable them', async function () {
const Task = this.sequelize.define('Task', { title: Sequelize.STRING });
const User = this.sequelize.define('User', { username: Sequelize.STRING });
const Task = this.sequelize.define('Task', { title: DataTypes.STRING });
const User = this.sequelize.define('User', { username: DataTypes.STRING });

Task.belongsTo(User, { constraints: false });

Expand Down Expand Up @@ -828,13 +828,13 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
it('works when taking a column directly from the object', function () {
const User = this.sequelize.define('user', {
uid: {
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
primaryKey: true,
},
});
const Profile = this.sequelize.define('project', {
user_id: {
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
allowNull: false,
},
});
Expand All @@ -851,7 +851,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
const Task = this.sequelize.define('task', {
projectId: {
defaultValue: 42,
type: Sequelize.INTEGER,
type: DataTypes.INTEGER,
},
});
const Project = this.sequelize.define('project', {});
Expand All @@ -875,7 +875,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
it('should throw an error if an association clashes with the name of an already define attribute', function () {
const Person = this.sequelize.define('person', {});
const Car = this.sequelize.define('car', {
person: Sequelize.INTEGER,
person: DataTypes.INTEGER,
});

expect(Car.belongsTo.bind(Car, Person, { as: 'person' })).to
Expand All @@ -886,10 +886,10 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
describe('Eager loading', () => {
beforeEach(function () {
this.Individual = this.sequelize.define('individual', {
name: Sequelize.STRING,
name: DataTypes.STRING,
});
this.Hat = this.sequelize.define('hat', {
name: Sequelize.STRING,
name: DataTypes.STRING,
});
this.Individual.belongsTo(this.Hat, {
as: 'personwearinghat',
Expand Down