Skip to content
This repository was archived by the owner on May 27, 2021. It is now read-only.

TypeScript - corrections and improvements #4

Open
wants to merge 11 commits into
base: typescript
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ _But the output was bar!_
_Output, either JSON or SQL_


__Dialect:__ mysql / postgres / sqlite / mssql / any
__Dialect:__ mysql / postgres / sqlite / mssql / oracle / any
__Dialect version:__ XXX
__Database version:__ XXX
__Sequelize version:__ XXX
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ npm-debug.log
*~
src/test/binary/tmp/*
src/test/tmp/*
.directory
src/test/dialects/sqlite/test.sqlite
src/test/sqlite/test.sqlite
coverage-*
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ typedoc.js
tslint.json
tsconfig.json
typings.json
.directory
Dockerfile
node_modules

7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ branches:
only:
- typescript
- develop
- docdeployment
- /^greenkeeper/.*$/
except:
- /^v\d+\.\d+\.\d+$/
@@ -96,11 +97,11 @@ jobs:
- npm run docs
deploy:
provider: surge
project: ./esdoc/
domain: docs.sequelizejs.com
project: ./site/
domain: docs.ts-sequelizejs.com
skip_cleanup: true

stages:
- test
- name: release
if: branch = master AND type = push AND fork = false
if: type = push AND fork = false
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
"url": "https://github.com/konnecteam/ts-sequelize/issues"
},
"dependencies": {
"bignumber.js": "4.0.4",
"bluebird": "^3.4.6",
"cls-bluebird": "^2.0.1",
"debug": "^3.0.0",
@@ -57,7 +58,8 @@
"istanbul": "^0.4.5",
"lcov-result-merger": "^1.2.0",
"mocha": "^5.2.0",
"mysql2": "^1.5.1",
"mysql2": "1.5.3",
"oracledb": "^2.2.0",
"pg": "^6.1.0",
"pg-cursor": "^1.3.0",
"pg-hstore": "^2.3.2",
@@ -78,9 +80,6 @@
"uuid-validate": "^0.0.2",
"validate-commit-msg": "^2.12.2"
},
"optionalDependencies": {
"oracledb": "^2.2.0"
},
"keywords": [
"mysql",
"sqlite",
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// 'use strict';

/**
@@ -6,3 +7,4 @@
* @module Sequelize
*/
export {Sequelize} from './lib/sequelize';
export { Transaction } from './lib/transaction';
91 changes: 47 additions & 44 deletions src/lib/dialects/abstract/abstract-query-generator.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import { Transaction } from '../../transaction';
import * as AllUtils from '../../utils';
import { AbstractDialect } from './abstract-dialect';
const Utils = AllUtils.Utils;
const SequelizeMethod = AllUtils.SequelizeMethod;

export abstract class AbstractQueryGenerator {
public _templateSettings = _.runInContext().templateSettings;
@@ -414,15 +415,15 @@ export abstract class AbstractQueryGenerator {
return _.template(query, this._templateSettings)(replacements);
}

/**
* Returns an update query.
* @param tableName -> Name of the table
* @param values -> A hash with attribute-value-pairs
* @param where -> A hash with conditions (e.g. {name: 'foo'})
* OR an ID as integer
* OR a string with conditions (e.g. 'name="foo"').
* If you use a string, you have to escape it on your own.
*/
/**
* Returns an update query.
* @param tableName -> Name of the table
* @param values -> A hash with attribute-value-pairs
* @param where -> A hash with conditions (e.g. {name: 'foo'})
* OR an ID as integer
* OR a string with conditions (e.g. 'name="foo"').
* If you use a string, you have to escape it on your own.
*/
public updateQuery(tableName : string, attrValueHash : {}, where : { length? }, options : {
fields? : string[],
hasTrigger? : boolean,
@@ -550,16 +551,16 @@ export abstract class AbstractQueryGenerator {
return _.template(query, this._templateSettings)(replacements).trim();
}

/**
* Returns an update query.
* @param operator -> String with the arithmetic operator (e.g. '+' or '-')
* @param tableName -> Name of the table
* @param values -> A hash with attribute-value-pairs
* @param where -> A hash with conditions (e.g. {name: 'foo'})
* OR an ID as integer
* OR a string with conditions (e.g. 'name="foo"').
* If you use a string, you have to escape it on your own.
*/
/**
* Returns an update query.
* @param operator -> String with the arithmetic operator (e.g. '+' or '-')
* @param tableName -> Name of the table
* @param values -> A hash with attribute-value-pairs
* @param where -> A hash with conditions (e.g. {name: 'foo'})
* OR an ID as integer
* OR a string with conditions (e.g. 'name="foo"').
* If you use a string, you have to escape it on your own.
*/
public arithmeticQuery(operator : string, tableName : string, attrValueHash : { number? : number}, where : { id? : number}, options : {
/** Array<String>|Object, A list of the attributes that you want to select, or an object with `include` and `exclude` keys. */
attributes? : {} | any[],
@@ -1607,6 +1608,7 @@ export abstract class AbstractQueryGenerator {
/** A hash of search attributes. */
where? : {}
}, mainTableAs : string) : any[] {

return attributes && attributes.map(attr => {
let addTable = true;

@@ -2742,6 +2744,7 @@ export abstract class AbstractQueryGenerator {
prefix? : string,
json? : {}
}) : any {

options = options || {};
if (key && typeof key === 'string' && key.indexOf('.') !== -1 && options.model) {
const keyParts = key.split('.');
@@ -2775,10 +2778,10 @@ export abstract class AbstractQueryGenerator {
}

if (!value) {
return this._joinKeyValue(key, this.escape(value, field), value === null ? this.OperatorMap[Op.is] : this.OperatorMap[Op.eq], options.prefix);
return this._joinKeyValue(key, this.escape(value, field), value === null ? this.OperatorMap[Op.is as any] : this.OperatorMap[Op.eq as any], options.prefix);
}

if (value instanceof AllUtils.SequelizeMethod && !(key !== undefined && value instanceof AllUtils.Fn)) {
if (value instanceof SequelizeMethod && !(key !== undefined && value instanceof AllUtils.Fn)) {
return this.handleSequelizeMethod(value);
}

@@ -2797,23 +2800,23 @@ export abstract class AbstractQueryGenerator {


if (value[Op.or]) {
return this._whereBind(this.OperatorMap[Op.or], key, value[Op.or], options);
return this._whereBind(this.OperatorMap[Op.or as any], key, value[Op.or], options);
}

if (value[Op.and]) {
return this._whereBind(this.OperatorMap[Op.and], key, value[Op.and], options);
return this._whereBind(this.OperatorMap[Op.and as any], key, value[Op.and], options);
}

if (isArray && fieldType instanceof DataTypes.ARRAY) {
return this._joinKeyValue(key, this.escape(value, field), this.OperatorMap[Op.eq], options.prefix);
return this._joinKeyValue(key, this.escape(value, field), this.OperatorMap[Op.eq as any], options.prefix);
}

if (isPlainObject && fieldType instanceof DataTypes.JSON && options.json !== false) {
return this._whereJSON(key, value, options);
}
// If multiple keys we combine the different logic conditions
if (isPlainObject && valueKeys.length > 1) {
return this._whereBind(this.OperatorMap[Op.and], key, value, options);
return this._whereBind(this.OperatorMap[Op.and as any], key, value, options);
}

if (isArray) {
@@ -2823,15 +2826,15 @@ export abstract class AbstractQueryGenerator {
if (this.OperatorMap[valueKeys[0]]) {
return this._whereParseSingleValueObject(key, field, valueKeys[0], value[valueKeys[0]], options);
} else {
return this._whereParseSingleValueObject(key, field, this.OperatorMap[Op.eq], value, options);
return this._whereParseSingleValueObject(key, field, this.OperatorMap[Op.eq as any], value, options);
}
}

if (key === Op.placeholder) {
return this._joinKeyValue(this.OperatorMap[key], this.escape(value, field), this.OperatorMap[Op.eq], options.prefix);
return this._joinKeyValue(this.OperatorMap[key], this.escape(value, field), this.OperatorMap[Op.eq as any], options.prefix);
}
return this._joinKeyValue(key, this.escape(value, field), this.OperatorMap[Op.eq as any], options.prefix);

return this._joinKeyValue(key, this.escape(value, field), this.OperatorMap[Op.eq], options.prefix);
}

/**
@@ -2871,7 +2874,7 @@ export abstract class AbstractQueryGenerator {
}
});

_.forOwn(orig, (item, prop) => {
_.forOwn(orig, (item : any, prop) => {
prop = this.OperatorsAliasMap[prop] || prop;
if (_.isPlainObject(item)) {
item = this._replaceAliases(item);
@@ -2886,12 +2889,12 @@ export abstract class AbstractQueryGenerator {
* @hidden
*/
private _whereGroupBind(key : any, value : any, options : { model? : typeof Model, prefix? : string } ) : string {
const binding = key === Op.or ? this.OperatorMap[Op.or] : this.OperatorMap[Op.and];
const binding = key === Op.or ? this.OperatorMap[Op.or as any] : this.OperatorMap[Op.and as any];
const outerBinding = key === Op.not ? 'NOT ' : '';

if (Array.isArray(value)) {
value = value.map(item => {
let itemQuery = this.whereItemsQuery(item, options, this.OperatorMap[Op.and]);
let itemQuery = this.whereItemsQuery(item, options, this.OperatorMap[Op.and as any]);
if (itemQuery && itemQuery.length && (Array.isArray(item) || _.isPlainObject(item)) && Utils.getComplexSize(item) > 1) {
itemQuery = '(' + itemQuery + ')';
}
@@ -2953,7 +2956,7 @@ export abstract class AbstractQueryGenerator {
this._traverseJSON(items, baseKey, prop, item, [prop]);
});

const result = items.join(this.OperatorMap[Op.and]);
const result = items.join(this.OperatorMap[Op.and as any]);
return items.length > 1 ? '(' + result + ')' : result;
}

@@ -3148,7 +3151,7 @@ export abstract class AbstractQueryGenerator {
}
}

let comparator = this.OperatorMap[prop] || this.OperatorMap[Op.eq];
let comparator = this.OperatorMap[prop] || this.OperatorMap[Op.eq as any];

switch (prop) {
case Op.in:
@@ -3161,14 +3164,14 @@ export abstract class AbstractQueryGenerator {
return this._joinKeyValue(key, `(${value.map(item => this.escape(item, field)).join(', ')})`, comparator, options.prefix);
}

if (comparator === this.OperatorMap[Op.in]) {
if (comparator === this.OperatorMap[Op.in as any]) {
return this._joinKeyValue(key, '(NULL)', comparator, options.prefix);
}

return '';
case Op.any:
case Op.all:
comparator = `${this.OperatorMap[Op.eq]} ${comparator}`;
comparator = `${this.OperatorMap[Op.eq as any]} ${comparator}`;
if (value[Op.values]) {
return this._joinKeyValue(key, `(VALUES ${value[Op.values].map(item => `(${this.escape(item)})`).join(', ')})`, comparator, options.prefix);
}
@@ -3180,7 +3183,7 @@ export abstract class AbstractQueryGenerator {
case Op.raw:
throw new Error('The `$raw` where property is no longer supported. Use `sequelize.literal` instead.');
case Op.col:
comparator = this.OperatorMap[Op.eq];
comparator = this.OperatorMap[Op.eq as any];
value = value.split('.');

if (value.length > 2) {
@@ -3198,7 +3201,7 @@ export abstract class AbstractQueryGenerator {
acceptStrings? : boolean,
isList? : boolean
} = {
acceptStrings: comparator.indexOf(this.OperatorMap[Op.like]) !== -1
acceptStrings: comparator.indexOf(this.OperatorMap[Op.like as any]) !== -1
};

if (_.isPlainObject(value)) {
@@ -3207,22 +3210,22 @@ export abstract class AbstractQueryGenerator {
}
if (value[Op.any]) {
escapeOptions.isList = true;
return this._joinKeyValue(key, `(${this.escape(value[Op.any], field, escapeOptions)})`, `${comparator} ${this.OperatorMap[Op.any]}`, options.prefix);
return this._joinKeyValue(key, `(${this.escape(value[Op.any], field, escapeOptions)})`, `${comparator} ${this.OperatorMap[Op.any as any]}`, options.prefix);
}
if (value[Op.all]) {
escapeOptions.isList = true;
return this._joinKeyValue(key, `(${this.escape(value[Op.all], field, escapeOptions)})`, `${comparator} ${this.OperatorMap[Op.all]}`, options.prefix);
return this._joinKeyValue(key, `(${this.escape(value[Op.all], field, escapeOptions)})`, `${comparator} ${this.OperatorMap[Op.all as any]}`, options.prefix);
}
}

if (comparator.indexOf(this.OperatorMap[Op.regexp]) !== -1) {
if (comparator.indexOf(this.OperatorMap[Op.regexp as any]) !== -1) {
return this._joinKeyValue(key, `'${value}'`, comparator, options.prefix);
}

if (value === null && comparator === this.OperatorMap[Op.eq]) {
return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.is], options.prefix);
} else if (value === null && comparator === this.OperatorMap[Op.ne]) {
return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.not], options.prefix);
if (value === null && comparator === this.OperatorMap[Op.eq as any]) {
return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.is as any], options.prefix);
} else if (value === null && comparator === this.OperatorMap[Op.ne as any]) {
return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.not as any], options.prefix);
}

return this._joinKeyValue(key, this.escape(value, field, escapeOptions), comparator, options.prefix);
10 changes: 5 additions & 5 deletions src/lib/dialects/mysql/mysql-query-generator.ts
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@ export class MysqlQueryGenerator extends AbstractQueryGenerator {
constructor(options : { sequelize? : Sequelize, options : ISequelizeOption, _dialect? : MysqlDialect }) {
super(options);
this.dialect = 'mysql';
this.OperatorMap[Op.regexp] = 'REGEXP';
this.OperatorMap[Op.notRegexp] = 'NOT REGEXP';
this.OperatorMap[Op.regexp as any] = 'REGEXP';
this.OperatorMap[Op.notRegexp as any] = 'NOT REGEXP';
}


@@ -176,11 +176,11 @@ export class MysqlQueryGenerator extends AbstractQueryGenerator {

if (options.uniqueKeys) {
_.each(options.uniqueKeys, (columns, indexName) => {
if (!columns.singleField) { // If it's a single field it's handled in column def, not as an index
if (!columns['singleField']) { // If it's a single field it's handled in column def, not as an index
if (!_.isString(indexName)) {
indexName = 'uniq_' + tableName + '_' + columns.fields.join('_');
indexName = 'uniq_' + tableName + '_' + (columns as any).fields.join('_');
}
values.attributes += ', UNIQUE ' + this.quoteIdentifier(indexName) + ' (' + _.map(columns.fields, this.quoteIdentifier).join(', ') + ')';
values.attributes += ', UNIQUE ' + this.quoteIdentifier(indexName) + ' (' + _.map((columns as any).fields, this.quoteIdentifier).join(', ') + ')';
}
});
}
4 changes: 4 additions & 0 deletions src/lib/dialects/oracle/oracle-data-types.ts
Original file line number Diff line number Diff line change
@@ -103,6 +103,10 @@ export class TEXT extends BaseTypes.TEXT {
}
return 'CLOB';
}

public _stringify(value : any) : number {
return value;
}
}

export class BOOLEAN extends BaseTypes.BOOLEAN {
Loading