Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
feat: #4 define odata entity set name
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jul 29, 2020
1 parent 79fc040 commit e178efb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
27 changes: 26 additions & 1 deletion src/lib/typeorm/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,41 @@ import { Edm } from '..';
import { NotImplementedError, ServerInternalError } from '../error';
import { BaseODataModel } from './model';

const KEY_ODATA_ENTITY_SET = 'odata.entity:entity_set_name';

/**
* set entity set name for odata entity
*
* @param entitySetName
*/
export function ODataEntitySetName(entitySetName: string) {
return function(target) {
Reflect.defineMetadata(KEY_ODATA_ENTITY_SET, entitySetName, target);
};
}

/**
* set entity set name for odata entity
*
* @param target
*/
export function getODataEntitySetName(target: any): string {
return Reflect.getMetadata(KEY_ODATA_ENTITY_SET, target);
}

/**
* ODataModel
*
* decorator wrapper of the typeorm `Entity` decorator
*
* @param options
*/
export function ODataModel(options: EntityOptions = {}) {
export function ODataModel(options: EntityOptions = {}, entitySetName?: string) {
return function(target: any): void {
Entity(options)(target);
if (entitySetName) {
ODataEntitySetName(entitySetName)(target);
}
};
}

Expand Down
7 changes: 4 additions & 3 deletions src/lib/typeorm/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { odata } from '..';
import { ODataServer } from '../server';
import { withConnection } from './connection';
import { TypedController } from './controller';
import { getODataEntitySetName } from './decorators';
import { TypedODataServer } from './server';

export async function createTypedODataServer(connectionOpt: ConnectionOptions, ...entities: (typeof BaseEntity)[]): Promise<typeof ODataServer>;
Expand Down Expand Up @@ -31,10 +32,10 @@ export async function createTypedODataServer(connection: any, ...entities: (type

entities.forEach((entity) => {
const ct = class extends TypedController { };
const entitySet = `${entity.name}s`;
Object.defineProperty(ct, 'name', { value: `${entitySet}Controller` }); // define controller name to use decorator
const entitySetName = getODataEntitySetName(entity) || `${entity.name}s`;
Object.defineProperty(ct, 'name', { value: `${entitySetName}Controller` }); // define controller name to use decorator
withConnection(connName)(ct);
odata.withController(ct, entitySet, entity)(server); // default public controller
odata.withController(ct, entitySetName, entity)(server); // default public controller
});

return server;
Expand Down
5 changes: 3 additions & 2 deletions src/test/typeorm/hook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OData } from 'light-odata';
import "light-odata/lib/polyfill";
import { Connection } from 'typeorm';
import { isArray } from 'util';
import { BaseHookProcessor, BaseODataModel, beforeCreate, createHookProcessor, createTypedODataServer, findHooks, HookContext, HookProcessor, HookType, ODataColumn, ODataModel, registerHook } from "../../lib";
import { BaseHookProcessor, BaseODataModel, beforeCreate, createHookProcessor, createTypedODataServer, findHooks, HookContext, HookProcessor, HookType, ODataColumn, ODataEntitySetName, ODataModel, registerHook } from "../../lib";
import { randomPort } from '../utils/randomPort';
import { ready, shutdown } from '../utils/server';
import { createTmpConnection } from './utils';
Expand Down Expand Up @@ -188,6 +188,7 @@ describe('Hooks Test Suite', () => {


@ODataModel()
@ODataEntitySetName("Students2")
class Student2 extends BaseODataModel {

// generated id
Expand Down Expand Up @@ -226,7 +227,7 @@ describe('Hooks Test Suite', () => {

const { server, client } = await createServerAndClient(conn, ...entities)

const es = client.getEntitySet<Student2>("Student2s")
const es = client.getEntitySet<Student2>("Students2")

await expect(async () => { await es.create({ name2: "second" }) }).rejects.toThrowError("something wrong!")

Expand Down

0 comments on commit e178efb

Please sign in to comment.