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

feature: Support schema in params.sequelize #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export class SequelizeAdapter<

ModelWithScope (params: ServiceParams) {
return this.applyScope(params);
};

ModelWithSequelizeParams (params: ServiceParams) {
let Model = this.getModel(params)

if (params?.sequelize?.scope) Model = Model.scope(params.sequelize.scope)
if (params?.sequelize?.schema) Model = Model.schema(params.sequelize.schema)

return Model
}

convertOperators (q: any): Query {
Expand Down Expand Up @@ -231,7 +240,7 @@ export class SequelizeAdapter<
async _find (params: ServiceParams = {} as ServiceParams): Promise<Paginated<Result> | Result[]> {
const { paginate } = this.filterQuery(params);

const Model = this.ModelWithScope(params);
const Model = this.ModelWithSequelizeParams(params);
const q = this.paramsToAdapter(null, params);

try {
Expand All @@ -253,7 +262,7 @@ export class SequelizeAdapter<
}

async _get (id: Id, params: ServiceParams = {} as ServiceParams): Promise<Result> {
const Model = this.ModelWithScope(params);
const Model = this.ModelWithSequelizeParams(params);
const q = this.paramsToAdapter(id, params);

// findById calls findAll under the hood. We use findAll so that
Expand Down Expand Up @@ -291,7 +300,7 @@ export class SequelizeAdapter<
returning: true
}, options, { raw: ignoreSetters });
const isArray = Array.isArray(data);
const Model = this.ModelWithScope(params);
const Model = this.ModelWithSequelizeParams(params);

try {
const result = isArray
Expand All @@ -318,7 +327,7 @@ export class SequelizeAdapter<
throw new MethodNotAllowed('Can not patch multiple entries')
}

const Model = this.ModelWithScope(params);
const Model = this.ModelWithSequelizeParams(params);

// Get a list of ids that match the id/query. Overwrite the
// $select because only the id is needed for this idList
Expand Down Expand Up @@ -404,7 +413,7 @@ export class SequelizeAdapter<
throw new MethodNotAllowed('Can not remove multiple entries')
}

const Model = this.ModelWithScope(params);
const Model = this.ModelWithSequelizeParams(params);

const findParams = { ...params };
if (params.$returning === false) {
Expand Down
11 changes: 7 additions & 4 deletions test/connection.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Sequelize } from 'sequelize';

export default (DB?: 'postgres' | 'mysql') => {
export default (DB?: 'postgres' | 'mysql', schema = 'default') => {
if (DB === 'postgres') {
return new Sequelize('sequelize', 'postgres', 'password', {
host: 'localhost',
dialect: 'postgres'
dialect: 'postgres',
schema
});
} else if (DB === 'mysql') {
return new Sequelize('sequelize', 'root', '', {
host: '127.0.0.1',
dialect: 'mysql'
dialect: 'mysql',
schema
});
} else {
return new Sequelize('sequelize', '', '', {
dialect: 'sqlite',
storage: './db.sqlite',
logging: false
logging: false,
schema
});
}
};
39 changes: 39 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,45 @@ describe('Feathers Sequelize Service', () => {

await people.remove(person.id);
});

it('can set the schema', async () => {
const ModelAlt = sequelize.define('people', {
name: {
type: Sequelize.STRING,
allowNull: false
},
age: {
type: Sequelize.INTEGER
},
created: {
type: Sequelize.BOOLEAN
},
time: {
type: Sequelize.BIGINT
},
status: {
type: Sequelize.STRING,
defaultValue: 'pending'
}
}, {
schema: 'alternative',
freezeTableName: true
});
await ModelAlt.sync({ force: true })

const people= app.service('people')
const data = { name: 'Jiri Schema' };
const SCHEMA_DEFAULT = { sequelize: { schema: 'default' } };
const SCHEMA_ALTERNATIVE = { sequelize: { schema: 'alternative' } };
const personDefault = await people.create(data, SCHEMA_DEFAULT);
const personAlternative = await people.create(data, SCHEMA_ALTERNATIVE);

assert.ok(personDefault)
assert.ok(personAlternative)

await people.remove(personDefault.id, SCHEMA_DEFAULT);
await people.remove(personAlternative.id, SCHEMA_ALTERNATIVE);
})
});

describe('ORM functionality', () => {
Expand Down
Loading