fix: updatedBy field in bulkUpdate hook (#54)

* fix: updatedBy field in bulkUpdate hook

* fix: test cases

* add db.sync() after each test case

Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
Junyi 2021-01-07 14:49:39 +08:00 committed by GitHub
parent 80acf5f199
commit 8289f96dd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 10 deletions

View File

@ -9,10 +9,10 @@ describe('user fields', () => {
app = await getApp(); app = await getApp();
agent = getAgent(app); agent = getAgent(app);
db = app.database; db = app.database;
await db.sync({ force: true });
}); });
afterEach(async () => { afterEach(async () => {
await db.sync();
await db.close(); await db.close();
}); });
@ -283,5 +283,39 @@ describe('user fields', () => {
const post2 = await Post.findByPk(post.id); const post2 = await Post.findByPk(post.id);
expect(post2.updated_by_id).toBe(user1.id); expect(post2.updated_by_id).toBe(user1.id);
}); });
it('bulkUpdate', async () => {
const Collection = db.getModel('collections');
const postCollection = await Collection.create({ name: 'posts', createdBy: false, updatedBy: true });
await postCollection.updateAssociations({
fields: [
{
type: 'string',
name: 'title'
}
]
});
const User = db.getModel('users');
const user1 = await User.create();
const user2 = await User.create();
const Post = db.getModel('posts');
let context = { state: { currentUser: user2 } };
await Post.bulkCreate([
{ title: 'title1' },
{ title: 'title2' },
]);
await Post.update({ title: 'title3' }, {
where: { title: 'title1' },
context
});
const posts = await Post.findAll({ order: [['id', 'ASC']] });
expect(posts.map(({ title, updated_by_id }) => ({ title, updated_by_id }))).toEqual([
{ title: 'title3', updated_by_id: 2 },
{ title: 'title2', updated_by_id: null },
]);
});
}); });
}); });

View File

@ -5,7 +5,7 @@ import bodyParser from 'koa-bodyparser';
import { Dialect } from 'sequelize'; import { Dialect } from 'sequelize';
import Database from '@nocobase/database'; import Database from '@nocobase/database';
import { actions, middlewares } from '@nocobase/actions'; import { actions, middlewares } from '@nocobase/actions';
import { Application, middleware } from '@nocobase/server'; import { Application, middleware } from '../../../server/src';
import plugin from '../server'; import plugin from '../server';
function getTestKey() { function getTestKey() {
@ -61,16 +61,13 @@ export async function getApp() {
app.registerPlugin('collections', [path.resolve(__dirname, '../../../plugin-collections')]); app.registerPlugin('collections', [path.resolve(__dirname, '../../../plugin-collections')]);
app.registerPlugin('file-manager', [plugin]); app.registerPlugin('file-manager', [plugin]);
await app.loadPlugins(); await app.loadPlugins();
await app.database.sync({ await app.database.sync();
force: true,
});
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
ctx.db = app.database; ctx.db = app.database;
await next(); await next();
}); });
app.use(bodyParser()); app.use(bodyParser());
app.use(middleware({ app.use(middleware({
prefix: '/api',
resourcer: app.resourcer, resourcer: app.resourcer,
database: app.database, database: app.database,
})); }));

View File

@ -13,10 +13,16 @@ export default class UpdatedBy extends BELONGSTO {
}); });
} }
static beforeBulkUpdateHook(this: UpdatedBy, models, { context }) { static beforeBulkUpdateHook(this: UpdatedBy, { attributes, fields, context }) {
models.forEach(model => { if (!context) {
setUserValue.call(this, model, { context }); return;
}); }
const { currentUser } = context.state;
if (!currentUser) {
return;
}
fields.push(this.options.foreignKey);
attributes[this.options.foreignKey] = currentUser.get(this.options.targetKey);
} }
constructor({ type, ...options }: UpdatedByOptions, context: FieldContext) { constructor({ type, ...options }: UpdatedByOptions, context: FieldContext) {