fix: toJSON with null association (#260)

This commit is contained in:
ChengLei Shao 2022-04-03 19:45:59 +08:00 committed by GitHub
parent afa807951a
commit 036baaa443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,60 @@
import { Collection, Database, mockDatabase } from '@nocobase/database';
describe('update', () => {
let db: Database;
let User: Collection;
let Tag: Collection;
afterEach(async () => {
await db.close();
});
beforeEach(async () => {
db = mockDatabase();
User = db.collection({
name: 'posts',
fields: [
{ type: 'string', name: 'title' },
{
type: 'belongsToMany',
name: 'tags',
},
],
});
Tag = db.collection({
name: 'tags',
fields: [
{
type: 'string',
name: 'name',
},
{
type: 'belongsToMany',
name: 'posts',
},
],
});
await db.sync();
});
it('should update tags to null', async () => {
await db.getRepository('posts').create({
values: {
title: 'p1',
tags: [{ name: 't1' }],
},
});
const [p1] = await db.getRepository('posts').update({
values: {
tags: null,
},
filter: {
title: 'p1',
},
});
expect(p1.toJSON()['tags']).toEqual([]);
});
});

View File

@ -40,7 +40,7 @@ export class Model<TModelAttributes extends {} = any, TCreationAttributes extend
const handleArray = (arrayOfObj, options: JSONTransformerOptions) => { const handleArray = (arrayOfObj, options: JSONTransformerOptions) => {
const handles = [this.sortAssociations]; const handles = [this.sortAssociations];
return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj); return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
}; };
const opts = { const opts = {