feathersjs-ecosystem / feathers-sequelize

A Feathers service adapter for the Sequelize ORM. Supporting MySQL, MariaDB, Postgres, SQLite, and SQL Server
MIT License
208 stars 75 forks source link

Patch/Remove methods mess up Find method #313

Closed boogdanpop closed 5 years ago

boogdanpop commented 5 years ago

Steps to reproduce

I'm trying to build feathers-chat-angular with Sequelize and I encountered some issues. https://github.com/feathersjs-ecosystem/feathers-chat-angular

All good when creating a user, authenticate, adding or displaying the messages, but when I try to edit or delete a message, the messages list disappear (when deleting) or the messages list has only the edited message in it.

Here is the code:

FE

chat.component.html

<button type="button" (click)="editMessage(message)"> Edit</button> <button type="button" (click)="deleteMessage(message)"> Delete</button> chat.component.ts editMessage(message) { this.chatService.editMessage(message._id, {text: 'This message was edited'}); } deleteMessage(message) { this.chatService.deleteMessage(message.id); }

chat.service.ts

editMessage(id: number, body: object) { if (!id) { return; } // feathers-reactive Observables are hot by default, // so we don't need to subscribe to make create() happen. this.feathers .service('messages') .patch(id, body); }

deleteMessage(id: number) { if (!id) { return; } // feathers-reactive Observables are hot by default, // so we don't need to subscribe to make create() happen. this.feathers .service('messages') . .remove(id); } feathers.service.ts

`import { Injectable } from '@angular/core';

import as feathersRx from 'feathers-reactive'; import as io from 'socket.io-client';

import feathers from '@feathersjs/feathers'; import feathersSocketIOClient from '@feathersjs/socketio-client'; import feathersAuthClient from '@feathersjs/authentication-client';

/**

BE messages.model.js

`// See http://docs.sequelizejs.com/en/latest/docs/models-definition/ // for more of what you can do here. const Sequelize = require('sequelize'); const DataTypes = Sequelize.DataTypes;

module.exports = function (app) { const sequelizeClient = app.get('sequelizeClient'); const messages = sequelizeClient.define('messages', { text: { type: DataTypes.STRING, allowNull: false } }, { hooks: { beforeCount(options) { options.raw = true; } } });

// eslint-disable-next-line no-unused-vars messages.associate = function (models) { // Define associations here // See http://docs.sequelizejs.com/en/latest/docs/associations/ messages.belongsTo(models.users); };

return messages; }; `

message.service.js

// Initializes themessagesservice on path/messages` const createService = require('feathers-sequelize'); const createModel = require('../../models/messages.model'); const hooks = require('./messages.hooks');

module.exports = function (app) { const Model = createModel(app); const paginate = app.get('paginate');

const options = { Model, paginate };

// Initialize our service with any options it requires app.use('/messages', createService(options));

// Get our initialized service so that we can register hooks const service = app.service('messages');

service.hooks(hooks); }; `

message.hooks.js

`const { authenticate } = require('@feathersjs/authentication').hooks;

module.exports = { before: { all: [ authenticate('jwt') ], find: [includeUser], get: [includeUser], create: [], update: [], patch: [], remove: [] },

after: { all: [], find: [], get: [], create: [], update: [], patch: [], remove: [] },

error: { all: [], find: [], get: [], create: [], update: [], patch: [], remove: [] } };

function includeUser(hook) { const sequelize = hook.app.get('sequelizeClient'); const { users } = sequelize.models;

hook.params.sequelize = { include: [users], };

return hook; } `

Expected behavior

The deleted message is removed from the list (and from DB), and then the messages are updated in the list. The edited message is edited and the list is updated.

Actual behavior

The deleted message is removed from the list (and from DB), but the list is empty. The edited message is edited the list contains only the currently edited message.

System configuration

"@feathersjs/authentication": "^2.1.16" "@feathersjs/authentication-jwt": "^2.0.10" "@feathersjs/authentication-local": "^1.2.9" "@feathersjs/configuration": "^2.0.6" "@feathersjs/errors": "^3.3.6" "@feathersjs/express": "^1.3.1" "@feathersjs/feathers": "^3.3.1" "@feathersjs/socketio": "^3.2.9" "compression": "^1.7.4" "cors": "^2.8.5" "feathers-sequelize": "^4.1.1" "helmet": "^3.20.0" "mysql2": "^1.6.5" "sequelize": "^6.0.0" "serve-favicon": "^2.5.0" "winston": "^3.2.1"

NodeJS version: 10.15.3 Browser Version: Chrome Version 75.0.3770.142

daffl commented 5 years ago

Your idField: '_id' configuration is probably wrong. Sequelize uses id by default so it should be idField: 'id'. Closing since this is not a feathers-sequelize problem.