不要怂,就是干,撸起袖子干!

You need to sign in or sign up before continuing.
Commit 1d3714a2 by Simon Schick Committed by Sushant

chore: misc cleanup (#10483)

1 parent 24291467
......@@ -235,7 +235,7 @@ const sequelize = new Sequelize('database', 'username', 'password', {
### MSSQL
The library for MSSQL is`tedious@^1.7.0` You'll just need to define the dialect:
The library for MSSQL is`tedious@^3.0.0` You'll just need to define the dialect:
```js
const sequelize = new Sequelize('database', 'username', 'password', {
......
......@@ -132,6 +132,10 @@ class Association {
});
}
[Symbol.for('nodejs.util.inspect.custom')]() {
return this.as;
}
inspect() {
return this.as;
}
......
......@@ -96,7 +96,7 @@ module.exports = BaseTypes => {
if (!(this instanceof DATEONLY)) {
return new DATEONLY();
}
BaseTypes.DATEONLY.apply(this, arguments);
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
......@@ -109,7 +109,7 @@ module.exports = BaseTypes => {
if (!(this instanceof UUID)) {
return new UUID();
}
BaseTypes.UUID.apply(this, arguments);
BaseTypes.UUID.call(this);
}
inherits(UUID, BaseTypes.UUID);
......@@ -156,7 +156,7 @@ module.exports = BaseTypes => {
if (!(this instanceof JSONTYPE)) {
return new JSONTYPE();
}
BaseTypes.JSON.apply(this, arguments);
BaseTypes.JSON.call(this);
}
inherits(JSONTYPE, BaseTypes.JSON);
......
......@@ -116,7 +116,7 @@ module.exports = BaseTypes => {
function BOOLEAN() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments);
BaseTypes.BOOLEAN.call(this);
}
inherits(BOOLEAN, BaseTypes.BOOLEAN);
......@@ -126,7 +126,7 @@ module.exports = BaseTypes => {
function UUID() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
BaseTypes.UUID.call(this);
}
inherits(UUID, BaseTypes.UUID);
......@@ -136,7 +136,7 @@ module.exports = BaseTypes => {
function NOW() {
if (!(this instanceof NOW)) return new NOW();
BaseTypes.NOW.apply(this, arguments);
BaseTypes.NOW.call(this);
}
inherits(NOW, BaseTypes.NOW);
......@@ -156,7 +156,7 @@ module.exports = BaseTypes => {
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
BaseTypes.DATEONLY.apply(this, arguments);
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
......
......@@ -94,7 +94,7 @@ module.exports = BaseTypes => {
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
BaseTypes.DATEONLY.apply(this, arguments);
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
......@@ -104,7 +104,7 @@ module.exports = BaseTypes => {
function UUID() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
BaseTypes.UUID.call(this);
}
inherits(UUID, BaseTypes.UUID);
......@@ -164,7 +164,7 @@ module.exports = BaseTypes => {
function JSONTYPE() {
if (!(this instanceof JSONTYPE)) return new JSONTYPE();
BaseTypes.JSON.apply(this, arguments);
BaseTypes.JSON.call(this);
}
inherits(JSONTYPE, BaseTypes.JSON);
......
......@@ -41,7 +41,7 @@ module.exports = BaseTypes => {
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
BaseTypes.DATEONLY.apply(this, arguments);
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
......@@ -134,7 +134,7 @@ module.exports = BaseTypes => {
function CITEXT() {
if (!(this instanceof CITEXT)) return new CITEXT();
BaseTypes.CITEXT.apply(this, arguments);
BaseTypes.CITEXT.call(this);
}
inherits(CITEXT, BaseTypes.CITEXT);
......@@ -159,7 +159,7 @@ module.exports = BaseTypes => {
function BOOLEAN() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments);
BaseTypes.BOOLEAN.call(this);
}
inherits(BOOLEAN, BaseTypes.BOOLEAN);
......
......@@ -47,7 +47,7 @@ module.exports = BaseTypes => {
function JSONTYPE() {
if (!(this instanceof JSONTYPE)) return new JSONTYPE();
BaseTypes.JSON.apply(this, arguments);
BaseTypes.JSON.call(this);
}
inherits(JSONTYPE, BaseTypes.JSON);
......@@ -71,7 +71,7 @@ module.exports = BaseTypes => {
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
BaseTypes.DATEONLY.apply(this, arguments);
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
......@@ -108,7 +108,7 @@ module.exports = BaseTypes => {
function CITEXT() {
if (!(this instanceof CITEXT)) return new CITEXT();
BaseTypes.CITEXT.apply(this, arguments);
BaseTypes.CITEXT.call(this);
}
inherits(CITEXT, BaseTypes.CITEXT);
......
......@@ -27,9 +27,7 @@ class ModelManager {
attribute: 'name'
});
const model = this.models.filter(model => model[options.attribute] === against);
return model ? model[0] : null;
return this.models.find(model => model[options.attribute] === against);
}
get all() {
......
......@@ -3068,6 +3068,10 @@ class Model {
this._defaultsOptions(options, scope);
}
static [Symbol.for('nodejs.util.inspect.custom')]() {
return this.name;
}
static inspect() {
return this.name;
}
......
......@@ -50,9 +50,9 @@
"devDependencies": {
"@commitlint/cli": "^7.3.2",
"@commitlint/config-angular": "^7.3.1",
"@types/bluebird": "^3.5.25",
"@types/node": "^10.12.18",
"@types/validator": "^9.4.4",
"@types/bluebird": "^3.5.26",
"@types/node": "^10.12.27",
"@types/validator": "^10.9.0",
"chai": "^4.x",
"chai-as-promised": "^7.x",
"chai-datetime": "^1.x",
......@@ -72,18 +72,18 @@
"lcov-result-merger": "^3.0.0",
"lint-staged": "^8.1.0",
"mariadb": "^2.0.3",
"mocha": "^5.x",
"mysql2": "^1.6.4",
"mocha": "^6.0.2",
"mysql2": "^1.6.5",
"nyc": "^13.3.0",
"pg": "^7.8.0",
"pg-hstore": "^2.x",
"pg-types": "^2.0.0",
"rimraf": "^2.x",
"sinon": "^6.3.5",
"rimraf": "^2.6.3",
"sinon": "^7.2.4",
"sinon-chai": "^3.2.0",
"sqlite3": "^4.0.6",
"tedious": "^3.0.1",
"typescript": "^3.2.2"
"typescript": "^3.3.3333"
},
"keywords": [
"mysql",
......@@ -121,7 +121,7 @@
"test-docker-integration": "env-cmd $npm_package_options_env_cmd npm run test-integration",
"docs": "esdoc && cp docs/ROUTER esdoc/ROUTER && cp docs/favicon.ico esdoc/favicon.ico",
"teaser": "node scripts/teaser",
"test-unit": "mocha --require scripts/mocha-bootload --globals setImmediate,clearImmediate --ui tdd --exit --check-leaks --colors -t 30000 --reporter spec \"test/unit/**/*.js\"",
"test-unit": "mocha --require scripts/mocha-bootload --globals setImmediate,clearImmediate --exit --check-leaks --colors -t 30000 --reporter spec \"test/unit/**/*.js\"",
"test-unit-mariadb": "cross-env DIALECT=mariadb npm run test-unit",
"test-unit-mysql": "cross-env DIALECT=mysql npm run test-unit",
"test-unit-postgres": "cross-env DIALECT=postgres npm run test-unit",
......@@ -129,7 +129,7 @@
"test-unit-sqlite": "cross-env DIALECT=sqlite npm run test-unit",
"test-unit-mssql": "cross-env DIALECT=mssql npm run test-unit",
"test-unit-all": "npm run test-unit-mariadb && npm run test-unit-mysql && npm run test-unit-postgres && npm run test-unit-postgres-native && npm run test-unit-mssql && npm run test-unit-sqlite",
"test-integration": "mocha --require scripts/mocha-bootload --globals setImmediate,clearImmediate --ui tdd --exit --check-leaks --colors -t 30000 --reporter spec \"test/integration/**/*.test.js\"",
"test-integration": "mocha --require scripts/mocha-bootload --globals setImmediate,clearImmediate --exit --check-leaks --colors -t 30000 --reporter spec \"test/integration/**/*.test.js\"",
"test-integration-mariadb": "cross-env DIALECT=mariadb npm run test-integration",
"test-integration-mysql": "cross-env DIALECT=mysql npm run test-integration",
"test-integration-postgres": "cross-env DIALECT=postgres npm run test-integration",
......@@ -148,8 +148,8 @@
"test-all": "npm run test-mariadb && npm run test-mysql && npm run test-sqlite && npm run test-postgres && npm run test-postgres-native && npm run test-mssql",
"test-typings": "tsc -b types/tsconfig.json && tsc -b types/test/tsconfig.json",
"cover": "rimraf coverage && npm run teaser && npm run cover-integration && npm run cover-unit && npm run merge-coverage",
"cover-integration": "cross-env COVERAGE=true nyc --reporter=lcovonly mocha --require scripts/mocha-bootload -t 30000 --exit --ui tdd \"test/integration/**/*.test.js\" && node -e \"require('fs').renameSync('coverage/lcov.info', 'coverage/integration.info')\"",
"cover-unit": "cross-env COVERAGE=true nyc --reporter=lcovonly mocha --require scripts/mocha-bootload -t 30000 --exit --ui tdd \"test/unit/**/*.test.js\" && node -e \"require('fs').renameSync('coverage/lcov.info', 'coverage/unit.info')\"",
"cover-integration": "cross-env COVERAGE=true nyc --reporter=lcovonly mocha --require scripts/mocha-bootload -t 30000 --exit \"test/integration/**/*.test.js\" && node -e \"require('fs').renameSync('coverage/lcov.info', 'coverage/integration.info')\"",
"cover-unit": "cross-env COVERAGE=true nyc --reporter=lcovonly mocha --require scripts/mocha-bootload -t 30000 --exit \"test/unit/**/*.test.js\" && node -e \"require('fs').renameSync('coverage/lcov.info', 'coverage/unit.info')\"",
"merge-coverage": "lcov-result-merger \"coverage/*.info\" \"coverage/lcov.info\"",
"sscce": "env-cmd $npm_package_options_env_cmd node sscce.js",
"sscce-mariadb": "cross-env DIALECT=mariadb npm run sscce",
......
......@@ -101,7 +101,7 @@ export interface AssociationScope {
/**
* The name of the column that will be used for the associated scope and it's value
*/
[scopeName: string]: any;
[scopeName: string]: unknown;
}
/**
......
......@@ -116,7 +116,7 @@ export interface BelongsToCreateAssociationMixinOptions extends CreateOptions, B
* @see Instance
*/
export type BelongsToCreateAssociationMixin<TModel> = (
values?: { [attribute: string]: any },
values?: { [attribute: string]: unknown },
options?: BelongsToCreateAssociationMixinOptions
) => Promise<TModel>;
......
......@@ -205,7 +205,7 @@ export interface HasManyCreateAssociationMixinOptions extends CreateOptions {}
* @see Instance
*/
export type HasManyCreateAssociationMixin<TModel> = (
values?: { [attribute: string]: any },
values?: { [attribute: string]: unknown },
options?: HasManyCreateAssociationMixinOptions
) => Promise<TModel>;
......
......@@ -108,6 +108,6 @@ export interface HasOneCreateAssociationMixinOptions extends HasOneSetAssociatio
* @see Instance
*/
export type HasOneCreateAssociationMixin<TModel> = (
values?: { [attribute: string]: any },
values?: { [attribute: string]: unknown },
options?: HasOneCreateAssociationMixinOptions
) => Promise<TModel>;
......@@ -78,7 +78,7 @@ interface StringDataTypeConstructor extends AbstractDataTypeConstructor {
export interface StringDataType extends AbstractDataType {
options?: StringDataTypeOptions;
BINARY: this;
validate(value: any): boolean;
validate(value: unknown): boolean;
}
export interface StringDataTypeOptions {
......@@ -119,7 +119,7 @@ interface TextDataTypeConstructor extends AbstractDataTypeConstructor {
export interface TextDataType extends AbstractDataType {
options: TextDataTypeOptions;
validate(value: any): boolean;
validate(value: unknown): boolean;
}
export interface TextDataTypeOptions {
......@@ -134,14 +134,14 @@ interface NumberDataTypeConstructor extends AbstractDataTypeConstructor {
ZEROFILL: this;
new (options?: NumberDataTypeOptions): NumberDataType;
(options?: NumberDataTypeOptions): NumberDataType;
validate(value: any): boolean;
validate(value: unknown): boolean;
}
export interface NumberDataType extends AbstractDataType {
options: NumberDataTypeOptions;
UNSIGNED: this;
ZEROFILL: this;
validate(value: any): boolean;
validate(value: unknown): boolean;
}
export interface NumberDataTypeOptions {
......@@ -297,9 +297,9 @@ export const TIME: AbstractDataTypeConstructor;
export const DATE: DateDataTypeConstructor;
interface DateDataTypeConstructor extends AbstractDataTypeConstructor {
new (length?: any): DateDataType;
new (length?: string | number): DateDataType;
new (options?: DateDataTypeOptions): DateDataType;
(length?: any): DateDataType;
(length?: string | number): DateDataType;
(options?: DateDataTypeOptions): DateDataType;
}
......@@ -308,7 +308,7 @@ export interface DateDataType extends AbstractDataTypeConstructor {
}
export interface DateDataTypeOptions {
length?: any;
length?: string | number;
}
/**
......@@ -317,19 +317,13 @@ export interface DateDataTypeOptions {
export const DATEONLY: DateOnlyDataTypeConstructor;
interface DateOnlyDataTypeConstructor extends AbstractDataTypeConstructor {
new (length: any): DateOnlyDataType;
new (options: DateOnlyDataTypeOptions): DateOnlyDataType;
(length: any): DateOnlyDataType;
(options: DateOnlyDataTypeOptions): DateOnlyDataType;
new (): DateOnlyDataType;
(): DateOnlyDataType;
}
export interface DateOnlyDataType extends AbstractDataType {
options: DateOnlyDataTypeOptions;
}
export interface DateOnlyDataTypeOptions {
length?: any;
}
/**
* A key / value column. Only available in postgres.
......@@ -510,7 +504,7 @@ interface ArrayDataTypeConstructor extends AbstractDataTypeConstructor {
new <T extends AbstractDataTypeConstructor | AbstractDataType>(options: ArrayDataTypeOptions<T>): ArrayDataType<T>;
<T extends AbstractDataTypeConstructor | AbstractDataType>(type: T): ArrayDataType<T>;
<T extends AbstractDataTypeConstructor | AbstractDataType>(options: ArrayDataTypeOptions<T>): ArrayDataType<T>;
is<T extends AbstractDataTypeConstructor | AbstractDataType>(obj: any, type: T): obj is ArrayDataType<T>;
is<T extends AbstractDataTypeConstructor | AbstractDataType>(obj: unknown, type: T): obj is ArrayDataType<T>;
}
export interface ArrayDataType<T extends AbstractDataTypeConstructor | AbstractDataType> extends AbstractDataType {
......
......@@ -93,7 +93,7 @@ export interface UniqueConstraintErrorOptions {
parent?: Error;
message?: string;
errors?: ValidationErrorItem[];
fields?: { [key: string]: any };
fields?: { [key: string]: unknown };
original?: Error;
}
......@@ -104,7 +104,7 @@ export class UniqueConstraintError extends ValidationError implements CommonErro
public readonly parent: Error;
public readonly original: Error;
public readonly sql: string;
public readonly fields: { [key: string]: any };
public readonly fields: { [key: string]: unknown };
constructor(options?: UniqueConstraintErrorOptions);
}
......@@ -114,7 +114,7 @@ export class UniqueConstraintError extends ValidationError implements CommonErro
export class ForeignKeyConstraintError extends DatabaseError {
public table: string;
public fields: { [field: string]: string };
public value: any;
public value: unknown;
public index: string;
constructor(options: { parent?: Error; message?: string; index?: string; fields?: string[]; table?: string });
}
......
......@@ -54,7 +54,7 @@ export interface AllModelHooks extends ModelHookOptions {
export interface SequelizeHooks extends AllModelHooks {
beforeConnect(config: Config): HookReturn;
afterConnect(connection: any, config: Config): HookReturn;
afterConnect(connection: unknown, config: Config): HookReturn;
}
/**
......
......@@ -9,7 +9,7 @@ export class ModelManager {
constructor(sequelize: Sequelize);
public addModel<T extends typeof Model>(model: T): T;
public removeModel(model: typeof Model): void;
public getModel(against: any, options?: { attribute?: string }): typeof Model;
public getModel(against: unknown, options?: { attribute?: string }): typeof Model;
}
export default ModelManager;
import { DataType } from './data-types';
import { Logging, Model, ModelAttributeColumnOptions, ModelAttributes, Transactionable, WhereOptions } from './model';
import { Logging, Model, ModelAttributeColumnOptions, ModelAttributes, Transactionable, WhereOptions, Filterable } from './model';
import { Promise } from './promise';
import QueryTypes = require('./query-types');
import { Sequelize } from './sequelize';
......@@ -57,6 +57,10 @@ export interface QueryOptions extends Logging, Transactionable {
instance?: Model;
}
export interface QueryOptionsWithWhere extends QueryOptions, Filterable {
}
export interface QueryOptionsWithModel {
/**
* A sequelize model used to build the returned model instances (used to be called callee)
......@@ -73,9 +77,9 @@ export interface QueryOptionsWithType<T extends QueryTypes> extends QueryOptions
}
/**
* Most of the methods accept options and use only the logger property of the options. That's why the most used
* interface type for options in a method is separated here as another interface.
*/
* Most of the methods accept options and use only the logger property of the options. That's why the most used
* interface type for options in a method is separated here as another interface.
*/
export interface QueryInterfaceOptions extends Logging, Transactionable {}
export interface CollateCharsetOptions {
......@@ -126,7 +130,7 @@ export interface AddUniqueConstraintOptions {
export interface AddDefaultConstraintOptions {
type: 'default';
name?: string;
defaultValue?: any;
defaultValue?: unknown;
}
export interface AddCheckConstraintOptions {
......@@ -152,29 +156,35 @@ export interface AddForeignKeyConstraintOptions {
}
export type AddConstraintOptions =
| AddUniqueConstraintOptions
| AddDefaultConstraintOptions
| AddCheckConstraintOptions
| AddPrimaryKeyConstraintOptions
| AddForeignKeyConstraintOptions;
| AddUniqueConstraintOptions
| AddDefaultConstraintOptions
| AddCheckConstraintOptions
| AddPrimaryKeyConstraintOptions
| AddForeignKeyConstraintOptions;
export interface CreateDatabaseOptions extends CollateCharsetOptions, QueryOptions {
encoding?: string;
}
export interface FunctionParam {
type: string;
name?: string;
direction?: string;
}
/**
* The interface that Sequelize uses to talk to all databases.
*
* This interface is available through sequelize.QueryInterface. It should not be commonly used, but it's
* referenced anyway, so it can be used.
*/
* The interface that Sequelize uses to talk to all databases.
*
* This interface is available through sequelize.QueryInterface. It should not be commonly used, but it's
* referenced anyway, so it can be used.
*/
export class QueryInterface {
/**
* Returns the dialect-specific sql generator.
*
* We don't have a definition for the QueryGenerator, because I doubt it is commonly in use separately.
*/
public QueryGenerator: any;
public QueryGenerator: unknown;
/**
* Returns the current sequelize instance.
......@@ -217,9 +227,9 @@ export class QueryInterface {
/**
* Creates a table with specified attributes.
*
* @param tableName Name of table to create
* @param attributes Hash of attributes, key is attribute name, value is data type
* @param options Table options.
* @param tableName Name of table to create
* @param attributes Hash of attributes, key is attribute name, value is data type
* @param options Table options.
*/
public createTable(
tableName: string | { schema?: string; tableName?: string },
......@@ -389,7 +399,7 @@ export class QueryInterface {
instance: Model,
tableName: string,
values: object,
identifier: object,
identifier: WhereOptions,
options?: QueryOptions
): Promise<object>;
......@@ -399,7 +409,7 @@ export class QueryInterface {
public bulkUpdate(
tableName: string,
values: object,
identifier: object,
identifier: WhereOptions,
options?: QueryOptions,
attributes?: string[] | string
): Promise<object>;
......@@ -407,14 +417,14 @@ export class QueryInterface {
/**
* Deletes a row
*/
public delete(instance: Model, tableName: string, identifier: object, options?: QueryOptions): Promise<object>;
public delete(instance: Model | null, tableName: string, identifier: WhereOptions, options?: QueryOptions): Promise<object>;
/**
* Deletes multiple rows at once
*/
public bulkDelete(
tableName: string,
identifier: object,
identifier: WhereOptions,
options?: QueryOptions,
model?: typeof Model
): Promise<object>;
......@@ -422,7 +432,7 @@ export class QueryInterface {
/**
* Returns selected rows
*/
public select(model: typeof Model, tableName: string, options?: QueryOptions): Promise<object[]>;
public select(model: typeof Model | null, tableName: string, options?: QueryOptionsWithWhere): Promise<object[]>;
/**
* Increments a row value
......@@ -431,7 +441,7 @@ export class QueryInterface {
instance: Model,
tableName: string,
values: object,
identifier: object,
identifier: WhereOptions,
options?: QueryOptions
): Promise<object>;
......@@ -440,7 +450,7 @@ export class QueryInterface {
*/
public rawSelect(
tableName: string,
options: QueryOptions,
options: QueryOptionsWithWhere,
attributeSelector: string | string[],
model?: typeof Model
): Promise<string[]>;
......@@ -453,9 +463,11 @@ export class QueryInterface {
tableName: string,
triggerName: string,
timingType: string,
fireOnArray: any[],
fireOnArray: {
[key: string]: unknown;
}[],
functionName: string,
functionParams: any[],
functionParams: FunctionParam[],
optionsArray: string[],
options?: QueryInterfaceOptions
): Promise<void>;
......@@ -480,7 +492,7 @@ export class QueryInterface {
*/
public createFunction(
functionName: string,
params: any[],
params: FunctionParam[],
returnType: string,
language: string,
body: string,
......@@ -490,14 +502,14 @@ export class QueryInterface {
/**
* Postgres only. Drops a function
*/
public dropFunction(functionName: string, params: any[], options?: QueryInterfaceOptions): Promise<void>;
public dropFunction(functionName: string, params: FunctionParam[], options?: QueryInterfaceOptions): Promise<void>;
/**
* Postgres only. Rename a function
*/
public renameFunction(
oldFunctionName: string,
params: any[],
params: FunctionParam[],
newFunctionName: string,
options?: QueryInterfaceOptions
): Promise<void>;
......
......@@ -83,7 +83,7 @@ export interface PoolOptions {
* A function that validates a connection. Called with client. The default function checks that client is an
* object, and that its state is not disconnected
*/
validate?(client?: any): boolean;
validate?(client?: unknown): boolean;
}
/**
......@@ -131,7 +131,7 @@ export interface Config {
};
readonly protocol: 'tcp';
readonly native: boolean;
readonly ssl: any;
readonly ssl: boolean;
readonly replication: boolean;
readonly dialectModulePath: null | string;
readonly keepDefaultTimezone?: boolean;
......@@ -1052,14 +1052,14 @@ export class Sequelize extends Hooks {
* @param sql
* @param options Query options
*/
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.UPDATE>): Promise<[undefined, number]>;
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.BULKUPDATE>): Promise<number>;
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.INSERT>): Promise<[number, number]>;
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.UPSERT>): Promise<number>;
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.DELETE>): Promise<void>;
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.BULKDELETE>): Promise<number>;
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.SHOWTABLES>): Promise<string[]>;
public query(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.DESCRIBE>): Promise<{
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.UPDATE>): Promise<[undefined, number]>;
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.BULKUPDATE>): Promise<number>;
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.INSERT>): Promise<[number, number]>;
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.UPSERT>): Promise<number>;
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.DELETE>): Promise<void>;
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.BULKDELETE>): Promise<number>;
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SHOWTABLES>): Promise<string[]>;
public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.DESCRIBE>): Promise<{
[key: string]: {
type: string;
allowNull: boolean;
......@@ -1070,11 +1070,11 @@ export class Sequelize extends Hooks {
}
}>;
public query<M extends Model>(
sql: string | { query: string; values: any[] },
sql: string | { query: string; values: unknown[] },
options: QueryOptionsWithModel
): Promise<M[]>;
public query<T extends object>(sql: string | { query: string; values: any[] }, options: QueryOptionsWithType<QueryTypes.SELECT>): Promise<T[]>;
public query(sql: string | { query: string; values: any[] }, options?: QueryOptions | QueryOptionsWithType<QueryTypes.RAW>): Promise<unknown[]>;
public query<T extends object>(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SELECT>): Promise<T[]>;
public query(sql: string | { query: string; values: unknown[] }, options?: QueryOptions | QueryOptionsWithType<QueryTypes.RAW>): Promise<unknown[]>;
/**
* Execute a query which would set an environment or user variable. The variables are set per connection,
......@@ -1251,7 +1251,7 @@ export class Sequelize extends Hooks {
* @param fn The function you want to call
* @param args All further arguments will be passed as arguments to the function
*/
export function fn(fn: string, ...args: any[]): Fn;
export function fn(fn: string, ...args: unknown[]): Fn;
/**
* Creates a object representing a column in the DB. This is often useful in conjunction with
......@@ -1267,14 +1267,14 @@ export function col(col: string): Col;
* @param val The value to cast
* @param type The type to cast it to
*/
export function cast(val: any, type: string): Cast;
export function cast(val: unknown, type: string): Cast;
/**
* Creates a object representing a literal, i.e. something that will not be escaped.
*
* @param val
*/
export function literal(val: any): Literal;
export function literal(val: string): Literal;
/**
* An AND query
......
export type Escapable = undefined | null | boolean | number | string | Date;
export function escapeId(val: string, forbidQualified?: boolean): string;
export function escape(val: Escapable | Escapable[], timeZone?: string, dialect?: string, format?: string): string;
export function format(sql: string, values: any[], timeZone?: string, dialect?: string): string;
export function formatNamedParameters(sql: string, values: any[], timeZone?: string, dialect?: string): string;
export function format(sql: string, values: unknown[], timeZone?: string, dialect?: string): string;
export function formatNamedParameters(sql: string, values: unknown[], timeZone?: string, dialect?: string): string;
import { DataType } from './data-types';
import { Model, WhereOptions } from './model';
export type Primitive = 'string' | 'number' | 'boolean';
......@@ -21,17 +22,22 @@ export function format(arr: string[], dialect: string): string;
export function formatNamedParameters(sql: string, parameters: {
[key: string]: string | number | boolean;
}, dialect: string): string;
export function cloneDeep<T>(obj: T, fn?: (el: any) => any): T;
export function cloneDeep<T>(obj: T, fn?: (el: unknown) => unknown): T;
export interface OptionsForMapping {
attributes?: string[];
where?: WhereOptions;
}
/** Expand and normalize finder options */
export function mapFinderOptions(options: any, Model: any): any;
export function mapFinderOptions<T extends OptionsForMapping>(options: T, model: typeof Model): T;
/* Used to map field names in attributes and where conditions */
export function mapOptionFieldNames(options: any, Model: any): any;
export function mapOptionFieldNames<T extends OptionsForMapping>(options: T, model: typeof Model): T;
export function mapWhereFieldNames(attributes: any, Model: any): any;
export function mapWhereFieldNames(attributes: object, model: typeof Model): object;
/** Used to map field names in values */
export function mapValueFieldNames(dataValues: any, fields: any, Model: any): any;
export function mapValueFieldNames(dataValues: object, fields: string[], model: typeof Model): object;
export function isColString(value: string): boolean;
export function canTreatArrayAsAnd(arr: unknown[]): boolean;
......@@ -40,7 +46,7 @@ export function combineTableNames(tableName1: string, tableName2: string): strin
export function singularize(s: string): string;
export function pluralize(s: string): string;
export function toDefaultValue(value: any): any;
export function toDefaultValue<T>(value: unknown): unknown;
/**
* Determine if the default value provided exists and can be described
......@@ -67,7 +73,7 @@ export class SequelizeMethod {
* Please do not use these functions directly, use Sequelize.fn and Sequelize.col instead.
*/
export class Fn extends SequelizeMethod {
constructor(fn: string, args: any);
constructor(fn: string, args: unknown[]);
public clone(): this;
}
......@@ -77,14 +83,14 @@ export class Col extends SequelizeMethod {
}
export class Cast extends SequelizeMethod {
public val: any;
public val: unknown;
public type: string;
constructor(val: any, type?: string);
constructor(val: unknown, type?: string);
}
export class Literal extends SequelizeMethod {
public val: any;
constructor(val: any);
public val: unknown;
constructor(val: unknown);
}
export class Json extends SequelizeMethod {
......
......@@ -14,7 +14,7 @@ interface User extends Model {
type UserModel = {
new (): User
customStaticMethod(): any
customStaticMethod(): unknown
} & typeof Model;
const User = sequelize.define('User', { firstName: DataTypes.STRING }, { tableName: 'users' }) as UserModel;
......
......@@ -9,6 +9,9 @@ MyModel.findAll({
{
limit: 1,
model: AssociatedModel,
on: {
a: 1,
},
order: [['id', 'DESC']],
separate: true,
where: { state: Sequelize.col('project.state') },
......
import { Association, HasOne, Model } from 'sequelize';
class MyModel extends Model {
public static associations: {
other: HasOne;
};
public static async customStuff() {
return this.sequelize!.query('select 1');
}
}
const assoc: Association = MyModel.associations.other;
import {
BelongsTo,
BelongsToCreateAssociationMixin,
BelongsToGetAssociationMixin,
BelongsToSetAssociationMixin,
DataTypes,
FindOptions,
Model,
BelongsTo,
BelongsToCreateAssociationMixin,
BelongsToGetAssociationMixin,
BelongsToSetAssociationMixin,
DataTypes,
FindOptions,
Model,
} from 'sequelize';
import { sequelize } from '../connection';
export class User extends Model {
public static associations: {
group: BelongsTo
};
public static associations: {
group: BelongsTo
};
public id: number;
public username: string;
public firstName: string;
public lastName: string;
public createdAt: Date;
public updatedAt: Date;
public id: number;
public username: string;
public firstName: string;
public lastName: string;
public createdAt: Date;
public updatedAt: Date;
// mixins for association (optional)
public groupId: number;
public group: UserGroup;
public getGroup: BelongsToGetAssociationMixin<UserGroup>;
public setGroup: BelongsToSetAssociationMixin<UserGroup, number>;
public createGroup: BelongsToCreateAssociationMixin<UserGroup>;
// mixins for association (optional)
public groupId: number;
public group: UserGroup;
public getGroup: BelongsToGetAssociationMixin<UserGroup>;
public setGroup: BelongsToSetAssociationMixin<UserGroup, number>;
public createGroup: BelongsToCreateAssociationMixin<UserGroup>;
}
User.init(
{
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
username: DataTypes.STRING,
{
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
username: DataTypes.STRING,
},
{
getterMethods: {
a() {
return 1;
},
{
scopes: {
custom(a: number) {
return {
where: {
firstName: a,
},
};
}
},
sequelize,
}
},
setterMethods: {
b(val: string) {
(<User>this).username = val;
},
},
scopes: {
custom(a: number) {
return {
where: {
firstName: a,
},
};
}
},
sequelize,
}
);
// Hooks
User.afterFind((users, options) => {
console.log('found');
console.log('found');
});
// TODO: VSCode shows the typing being correctly narrowed but doesn't do it correctly
User.addHook('beforeFind', 'test', (options: FindOptions) => {
return undefined;
return undefined;
});
// associate
// it is important to import _after_ the model above is already exported so the circular reference works.
......
......@@ -5,48 +5,48 @@ import { QueryInterface } from 'sequelize/lib/query-interface';
declare let queryInterface: QueryInterface;
queryInterface.createTable(
'nameOfTheNewTable',
{
attr1: DataTypes.STRING,
attr2: DataTypes.INTEGER,
attr3: {
allowNull: false,
defaultValue: false,
type: DataTypes.BOOLEAN,
},
// foreign key usage
attr4: {
onDelete: 'cascade',
onUpdate: 'cascade',
references: {
key: 'id',
model: 'another_table_name',
},
type: DataTypes.INTEGER,
},
createdAt: {
type: DataTypes.DATE,
},
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
updatedAt: {
type: DataTypes.DATE,
},
'nameOfTheNewTable',
{
attr1: DataTypes.STRING,
attr2: DataTypes.INTEGER,
attr3: {
allowNull: false,
defaultValue: false,
type: DataTypes.BOOLEAN,
},
{
charset: 'latin1', // default: null
collate: 'latin1_general_ci',
engine: 'MYISAM', // default: 'InnoDB'
uniqueKeys: {
test: {
customIndex: true,
fields: ['attr2', 'attr3'],
}
}
// foreign key usage
attr4: {
onDelete: 'cascade',
onUpdate: 'cascade',
references: {
key: 'id',
model: 'another_table_name',
},
type: DataTypes.INTEGER,
},
createdAt: {
type: DataTypes.DATE,
},
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
updatedAt: {
type: DataTypes.DATE,
},
},
{
charset: 'latin1', // default: null
collate: 'latin1_general_ci',
engine: 'MYISAM', // default: 'InnoDB'
uniqueKeys: {
test: {
customIndex: true,
fields: ['attr2', 'attr3'],
}
}
}
);
queryInterface.dropTable('nameOfTheExistingTable');
......@@ -56,25 +56,25 @@ queryInterface.dropAllTables();
queryInterface.renameTable('Person', 'User');
queryInterface.showAllTables().then(tableNames => {
// do nothing
// do nothing
});
queryInterface.describeTable('Person').then(attributes => {
/*
attributes will be something like:
{
name: {
type: 'VARCHAR(255)', // this will be 'CHARACTER VARYING' for pg!
allowNull: true,
defaultValue: null
},
isBetaMember: {
type: 'TINYINT(1)', // this will be 'BOOLEAN' for pg!
allowNull: false,
defaultValue: false
}
/*
attributes will be something like:
{
name: {
type: 'VARCHAR(255)', // this will be 'CHARACTER VARYING' for pg!
allowNull: true,
defaultValue: null
},
isBetaMember: {
type: 'TINYINT(1)', // this will be 'BOOLEAN' for pg!
allowNull: false,
defaultValue: false
}
}
*/
});
......@@ -83,16 +83,16 @@ queryInterface.addColumn('nameOfAnExistingTable', 'nameOfTheNewAttribute', DataT
// or
queryInterface.addColumn(
{ tableName: 'nameOfAnExistingTable', schema: 'nameOfSchema' },
'nameOfTheNewAttribute',
DataTypes.STRING
{ tableName: 'nameOfAnExistingTable', schema: 'nameOfSchema' },
'nameOfTheNewAttribute',
DataTypes.STRING
);
// or
queryInterface.addColumn('nameOfAnExistingTable', 'nameOfTheNewAttribute', {
allowNull: false,
type: DataTypes.STRING,
allowNull: false,
type: DataTypes.STRING,
});
queryInterface.removeColumn('Person', 'signature');
......@@ -102,21 +102,21 @@ queryInterface.removeColumn('Person', 'signature');
queryInterface.removeColumn({ tableName: 'Person', schema: 'nameOfSchema' }, 'signature');
queryInterface.changeColumn('nameOfAnExistingTable', 'nameOfAnExistingAttribute', {
allowNull: false,
defaultValue: 0.0,
type: DataTypes.FLOAT,
allowNull: false,
defaultValue: 0.0,
type: DataTypes.FLOAT,
});
// or
queryInterface.changeColumn(
{ tableName: 'nameOfAnExistingTable', schema: 'nameOfSchema' },
'nameOfAnExistingAttribute',
{
allowNull: false,
defaultValue: 0.0,
type: DataTypes.FLOAT,
}
{ tableName: 'nameOfAnExistingTable', schema: 'nameOfSchema' },
'nameOfAnExistingAttribute',
{
allowNull: false,
defaultValue: 0.0,
type: DataTypes.FLOAT,
}
);
queryInterface.renameColumn('Person', 'signature', 'sig');
......@@ -132,8 +132,8 @@ queryInterface.addIndex('Person', ['firstname', 'lastname']);
// - indexType: Set a type for the index, e.g. BTREE. See the documentation of the used dialect
// - logging: A function that receives the sql query, e.g. console.log
queryInterface.addIndex('Person', ['firstname', 'lastname'], {
indexName: 'SuperDuperIndex',
indicesType: 'UNIQUE',
indexName: 'SuperDuperIndex',
indicesType: 'UNIQUE',
});
queryInterface.removeIndex('Person', 'SuperDuperIndex');
......@@ -143,8 +143,20 @@ queryInterface.removeIndex('Person', 'SuperDuperIndex');
queryInterface.removeIndex('Person', ['firstname', 'lastname']);
queryInterface.addConstraint('Person', ['firstname', 'lastname'], {
name: 'firstnamexlastname',
type: 'unique',
name: 'firstnamexlastname',
type: 'unique',
});
queryInterface.removeConstraint('Person', 'firstnamexlastname');
queryInterface.select(null, 'Person', {
where: {
a: 1,
},
});
queryInterface.delete(null, 'Person', {
where: {
a: 1,
},
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!