-
Notifications
You must be signed in to change notification settings - Fork 75
Remove ModelWithScope and add handleError
#450
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
Changes from 4 commits
cfc0fb4
cd5dbb8
f8f291a
408a47e
f167ea3
e473e9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ A [Feathers](https://feathersjs.com) database adapter for [Sequelize](http://seq | |
| - [`service(options)`](#serviceoptions) | ||
| - [params.sequelize](#paramssequelize) | ||
| - [operators](#operatormap) | ||
| - [Modifying the model](#modifyModel) | ||
| - [Caveats](#caveats) | ||
| - [Sequelize `raw` queries](#sequelize-raw-queries) | ||
| - [Working with MSSQL](#working-with-mssql) | ||
|
|
@@ -154,6 +155,27 @@ app.service('users').find({ | |
| GET /users?name[$like]=Dav% | ||
| ``` | ||
|
|
||
| ## Modifying the Model | ||
|
|
||
| Sequelize allows you to call methods like `Model.scope()`, `Model.schema()`, and others. To use these methods, extend the class to overwrite the `getModel` method. | ||
|
|
||
| ```js | ||
| const { SequelizeService } = require('feathers-sequelize'); | ||
|
|
||
| class Service extends SequelizeService { | ||
| getModel(params) { | ||
| let Model = this.options.Model; | ||
| if (params?.sequelize?.scope) { | ||
| Model = Model.scope(params?.sequelize?.scope); | ||
| } | ||
| if (params?.sequelize?.schema) { | ||
| Model = Model.schema(params?.sequelize?.schema); | ||
|
||
| } | ||
| return Model; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Caveats | ||
|
|
||
| ### Sequelize `raw` queries | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,13 +114,6 @@ export class SequelizeAdapter< | |
| return this.options.Model; | ||
| } | ||
|
|
||
| ModelWithScope (params: ServiceParams) { | ||
| const Model = this.getModel(params); | ||
| if (params?.sequelize?.scope) { | ||
| return Model.scope(params.sequelize.scope); | ||
| } | ||
| return Model; | ||
| } | ||
|
|
||
| convertOperators (q: any): Query { | ||
| if (Array.isArray(q)) { | ||
|
|
@@ -222,23 +215,27 @@ export class SequelizeAdapter< | |
| return sequelize; | ||
| } | ||
|
|
||
| handleError (error: any) { | ||
| return errorHandler(error); | ||
| } | ||
|
||
|
|
||
| async _find (params?: ServiceParams & { paginate?: PaginationOptions }): Promise<Paginated<Result>> | ||
| async _find (params?: ServiceParams & { paginate: false }): Promise<Result[]> | ||
| async _find (params?: ServiceParams): Promise<Paginated<Result> | Result[]> | ||
| async _find (params: ServiceParams = {} as ServiceParams): Promise<Paginated<Result> | Result[]> { | ||
| const Model = this.ModelWithScope(params); | ||
| const Model = this.getModel(params); | ||
| const { paginate } = this.filterQuery(params); | ||
| const sequelizeOptions = this.paramsToAdapter(null, params); | ||
|
|
||
| if (!paginate || !paginate.default) { | ||
| const result = await Model.findAll(sequelizeOptions).catch(errorHandler); | ||
| const result = await Model.findAll(sequelizeOptions).catch(this.handleError); | ||
| return result; | ||
| } | ||
|
|
||
| if (sequelizeOptions.limit === 0) { | ||
| const total = (await Model | ||
| .count({ ...sequelizeOptions, attributes: undefined }) | ||
| .catch(errorHandler)) as any as number; | ||
| .catch(this.handleError)) as any as number; | ||
|
|
||
| return { | ||
| total, | ||
|
|
@@ -248,7 +245,7 @@ export class SequelizeAdapter< | |
| } | ||
| } | ||
|
|
||
| const result = await Model.findAndCountAll(sequelizeOptions).catch(errorHandler); | ||
| const result = await Model.findAndCountAll(sequelizeOptions).catch(this.handleError); | ||
|
|
||
| return { | ||
| total: result.count, | ||
|
|
@@ -259,9 +256,9 @@ export class SequelizeAdapter< | |
| } | ||
|
|
||
| async _get (id: Id, params: ServiceParams = {} as ServiceParams): Promise<Result> { | ||
| const Model = this.ModelWithScope(params); | ||
| const Model = this.getModel(params); | ||
| const sequelizeOptions = this.paramsToAdapter(id, params); | ||
| const result = await Model.findAll(sequelizeOptions).catch(errorHandler); | ||
| const result = await Model.findAll(sequelizeOptions).catch(this.handleError); | ||
| if (result.length === 0) { | ||
| throw new NotFound(`No record found for id '${id}'`); | ||
| } | ||
|
|
@@ -283,13 +280,13 @@ export class SequelizeAdapter< | |
| return [] | ||
| } | ||
|
|
||
| const Model = this.ModelWithScope(params); | ||
| const Model = this.getModel(params); | ||
| const sequelizeOptions = this.paramsToAdapter(null, params); | ||
|
|
||
| if (isArray) { | ||
| const instances = await Model | ||
| .bulkCreate(data as any[], sequelizeOptions) | ||
| .catch(errorHandler); | ||
| .catch(this.handleError); | ||
|
|
||
| if (sequelizeOptions.returning === false) { | ||
| return []; | ||
|
|
@@ -318,7 +315,7 @@ export class SequelizeAdapter< | |
|
|
||
| const result = await Model | ||
| .create(data as any, sequelizeOptions as CreateOptions) | ||
| .catch(errorHandler); | ||
| .catch(this.handleError); | ||
|
|
||
| if (sequelizeOptions.raw) { | ||
| return select((result as Model).toJSON()) | ||
|
|
@@ -334,7 +331,7 @@ export class SequelizeAdapter< | |
| throw new MethodNotAllowed('Can not patch multiple entries') | ||
| } | ||
|
|
||
| const Model = this.ModelWithScope(params); | ||
| const Model = this.getModel(params); | ||
| const sequelizeOptions = this.paramsToAdapter(id, params); | ||
| const select = selector(params, this.id); | ||
| const values = _.omit(data, this.id); | ||
|
|
@@ -361,7 +358,7 @@ export class SequelizeAdapter< | |
| raw: false, | ||
| where: { [this.id]: ids.length === 1 ? ids[0] : { [Op.in]: ids } } | ||
| } as UpdateOptions) | ||
| .catch(errorHandler) as [number, Model[]?]; | ||
| .catch(this.handleError) as [number, Model[]?]; | ||
|
|
||
| if (sequelizeOptions.returning === false) { | ||
| return [] | ||
|
|
@@ -436,7 +433,7 @@ export class SequelizeAdapter< | |
| await instance | ||
| .set(values) | ||
| .update(values, sequelizeOptions) | ||
| .catch(errorHandler); | ||
| .catch(this.handleError); | ||
|
|
||
| if (isPresent(sequelizeOptions.include)) { | ||
| return this._get(id, { | ||
|
|
@@ -462,7 +459,7 @@ export class SequelizeAdapter< | |
| } | ||
|
|
||
| async _update (id: Id, data: Data, params: ServiceParams = {} as ServiceParams): Promise<Result> { | ||
| const Model = this.ModelWithScope(params); | ||
| const Model = this.getModel(params); | ||
| const sequelizeOptions = this.paramsToAdapter(id, params); | ||
| const select = selector(params, this.id); | ||
|
|
||
|
|
@@ -484,7 +481,7 @@ export class SequelizeAdapter< | |
| await instance | ||
| .set(values) | ||
| .update(values, sequelizeOptions) | ||
| .catch(errorHandler); | ||
| .catch(this.handleError); | ||
|
|
||
| if (isPresent(sequelizeOptions.include)) { | ||
| return this._get(id, { | ||
|
|
@@ -516,7 +513,7 @@ export class SequelizeAdapter< | |
| throw new MethodNotAllowed('Can not remove multiple entries') | ||
| } | ||
|
|
||
| const Model = this.ModelWithScope(params); | ||
| const Model = this.getModel(params); | ||
| const sequelizeOptions = this.paramsToAdapter(id, params); | ||
|
|
||
| if (id === null) { | ||
|
|
@@ -539,7 +536,7 @@ export class SequelizeAdapter< | |
| await Model.destroy({ | ||
| ...params.sequelize, | ||
| where: { [this.id]: ids.length === 1 ? ids[0] : { [Op.in]: ids } } | ||
| }).catch(errorHandler); | ||
| }).catch(this.handleError); | ||
|
|
||
| if (sequelizeOptions.returning === false) { | ||
| return []; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could remove the optional chaining (
?.). I think, if you use the chains, ts complains that you can't passundefinedtoModel.scope()even though it should know that the optional chaining is useless.It's just docs and everyone should be capable to fix this for themselves, but yeah.