chore: relation repository response when source model not found (#1546)

* chore: relation repository response when source model not found

* fix: test

* fix: test

* fix: test
This commit is contained in:
ChengLei Shao 2023-03-07 22:10:51 +08:00 committed by GitHub
parent 313217a671
commit c86b91c586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 6 deletions

View File

@ -10,6 +10,9 @@
"@nocobase/database": "0.9.1-alpha.1", "@nocobase/database": "0.9.1-alpha.1",
"@nocobase/resourcer": "0.9.1-alpha.1" "@nocobase/resourcer": "0.9.1-alpha.1"
}, },
"devDependencies": {
"@nocobase/test": "0.9.1-alpha.1"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/nocobase/nocobase.git", "url": "git+https://github.com/nocobase/nocobase.git",

View File

@ -1,4 +1,4 @@
import { MockServer, mockServer } from './index'; import { MockServer, mockServer } from '@nocobase/test';
import { registerActions } from '@nocobase/actions'; import { registerActions } from '@nocobase/actions';
describe('get action', () => { describe('get action', () => {
@ -10,7 +10,9 @@ describe('get action', () => {
let Profile; let Profile;
beforeEach(async () => { beforeEach(async () => {
app = mockServer(); app = mockServer({
dataWrapping: false,
});
registerActions(app); registerActions(app);
PostTag = app.collection({ PostTag = app.collection({
@ -120,11 +122,13 @@ describe('get action', () => {
const postProfile = await Profile.repository.findOne(); const postProfile = await Profile.repository.findOne();
const response = await app const response = await app.agent().resource('posts.profile', p1.get('id')).get();
.agent()
.resource('posts.profile', p1.get('id'))
.get();
expect(response.body['id']).toEqual(postProfile.get('id')); expect(response.body['id']).toEqual(postProfile.get('id'));
}); });
it('should return null when source model not found', async () => {
const response = await app.agent().resource('posts.profile', 999).get();
expect(response.status).toEqual(200);
});
}); });

View File

@ -118,4 +118,14 @@ describe('list action', () => {
expect(body.count).toEqual(2); expect(body.count).toEqual(2);
expect(body.rows).toEqual([{ id: 1 }, { id: 2 }]); expect(body.rows).toEqual([{ id: 1 }, { id: 2 }]);
}); });
it('should return empty error when relation not exists', async () => {
const response = await app
.agent()
.resource('posts.tags', 999)
.list({ fields: ['id', 'postsTags.createdAt'], sort: ['id'] });
expect(response.status).toEqual(200);
expect(response.body.count).toEqual(0);
});
}); });

View File

@ -16,6 +16,7 @@ export async function get(ctx: Context, next) {
}); });
ctx.body = instance || null; ctx.body = instance || null;
ctx.status = 200;
await next(); await next();
} }

View File

@ -42,6 +42,8 @@ export abstract class MultipleRelationRepository extends RelationRepository {
const getAccessor = this.accessors().get; const getAccessor = this.accessors().get;
const sourceModel = await this.getSourceModel(transaction); const sourceModel = await this.getSourceModel(transaction);
if (!sourceModel) return [];
if (findOptions.include && findOptions.include.length > 0) { if (findOptions.include && findOptions.include.length > 0) {
const ids = ( const ids = (
await sourceModel[getAccessor]({ await sourceModel[getAccessor]({
@ -103,6 +105,8 @@ export abstract class MultipleRelationRepository extends RelationRepository {
const transaction = await this.getTransaction(options); const transaction = await this.getTransaction(options);
const sourceModel = await this.getSourceModel(transaction); const sourceModel = await this.getSourceModel(transaction);
if (!sourceModel) return 0;
const queryOptions = this.buildQueryOptions(options); const queryOptions = this.buildQueryOptions(options);
const count = await sourceModel[this.accessors().get]({ const count = await sourceModel[this.accessors().get]({

View File

@ -54,6 +54,8 @@ export abstract class SingleRelationRepository extends RelationRepository {
const getAccessor = this.accessors().get; const getAccessor = this.accessors().get;
const sourceModel = await this.getSourceModel(transaction); const sourceModel = await this.getSourceModel(transaction);
if (!sourceModel) return null;
if (findOptions?.include?.length > 0) { if (findOptions?.include?.length > 0) {
const templateModel = await sourceModel[getAccessor]({ const templateModel = await sourceModel[getAccessor]({
...findOptions, ...findOptions,

View File

@ -320,6 +320,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
const handleRequest = (req, res) => { const handleRequest = (req, res) => {
const ctx = this.createContext(req, res); const ctx = this.createContext(req, res);
// @ts-ignore // @ts-ignore
return this.handleRequest(ctx, fn); return this.handleRequest(ctx, fn);
}; };