-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sequelize): adds integration with sequelize
Relates to #8
- Loading branch information
Showing
10 changed files
with
955 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@ucast/sql-sequelize", | ||
"private": true, | ||
"typings": "../dist/types/lib/sequelize.d.ts", | ||
"main": "../dist/es6c/lib/sequelize.js", | ||
"es2015": "../dist/es6m/lib/sequelize.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { FieldCondition } from '@ucast/core' | ||
import { Model, Sequelize, DataTypes } from 'sequelize' | ||
import { interpret } from '../src/lib/sequelize' | ||
import { expect } from './specHelper' | ||
|
||
describe('Condition interpreter for Sequelize', () => { | ||
const { User } = configureORM() | ||
|
||
it('returns an object with `where` and `include` keys', () => { | ||
const condition = new FieldCondition('eq', 'name', 'test') | ||
const query = interpret(condition, User) | ||
|
||
expect(query).to.be.an('object') | ||
expect(query.where.val).to.equal('`name` = \'test\'') | ||
expect(query.include).to.be.an('array').that.is.empty | ||
}) | ||
|
||
it('properly binds parameters for "IN" operator', () => { | ||
const condition = new FieldCondition('in', 'age', [1, 2, 3]) | ||
const query = interpret(condition, User) | ||
|
||
expect(query.where.val).to.equal('`age` in(1, 2, 3)') | ||
}) | ||
|
||
it('automatically inner joins relation when condition is set on relation field', () => { | ||
const condition = new FieldCondition('eq', 'projects.name', 'test') | ||
const query = interpret(condition, User) | ||
|
||
expect(query.include).to.deep.equal([ | ||
{ association: 'projects', required: true } | ||
]) | ||
expect(query.where.val).to.equal('`projects`.`name` = \'test\'') | ||
}) | ||
}) | ||
|
||
function configureORM() { | ||
const sequelize = new Sequelize('sqlite::memory:') | ||
|
||
class User extends Model {} | ||
class Project extends Model {} | ||
|
||
User.init({ | ||
name: { type: DataTypes.STRING }, | ||
email: { type: DataTypes.STRING }, | ||
}, { sequelize, modelName: 'user' }) | ||
|
||
Project.init({ | ||
name: { type: DataTypes.STRING }, | ||
active: { type: DataTypes.BOOLEAN } | ||
}, { sequelize, modelName: 'project' }) | ||
|
||
Project.belongsTo(User) | ||
User.hasMany(Project) | ||
|
||
return { User, Project } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Condition } from '@ucast/core'; | ||
import { ModelType, Utils, literal } from 'sequelize'; | ||
import { | ||
createSqlInterpreter, | ||
allInterpreters, | ||
SqlOperator, | ||
createDialects, | ||
mysql | ||
} from '../index'; | ||
|
||
function joinRelation(relationName: string, Model: ModelType) { | ||
return Model.associations.hasOwnProperty(relationName); | ||
} | ||
|
||
const dialects = createDialects({ | ||
joinRelation, | ||
paramPlaceholder: mysql.paramPlaceholder, | ||
}); | ||
|
||
export function createInterpreter(interpreters: Record<string, SqlOperator<any>>) { | ||
const interpretSQL = createSqlInterpreter(interpreters); | ||
|
||
return (condition: Condition, Model: ModelType) => { | ||
const dialect = Model.sequelize!.getDialect() as keyof typeof dialects; | ||
const options = dialects[dialect]; | ||
|
||
if (!options) { | ||
throw new Error(`Unsupported database dialect: ${dialect}`); | ||
} | ||
|
||
const [sql, params, joins] = interpretSQL(condition, options, Model); | ||
return { | ||
include: joins.map(association => ({ association, required: true })), | ||
where: literal(Utils.format([sql, ...(params as string[])], dialect)), | ||
}; | ||
}; | ||
} | ||
|
||
export const interpret = createInterpreter(allInterpreters); |
Oops, something went wrong.