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

Commit

Permalink
feat: #112 exclude some column types
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Oct 12, 2020
1 parent 592721e commit 50547fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
22 changes: 14 additions & 8 deletions src/type/decorators/odata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isEmpty } from '@newdash/newdash/isEmpty';
import toInteger from '@newdash/newdash/toInteger';
import { BigNumber } from 'bignumber.js';
import 'reflect-metadata';
import { Column, ColumnOptions, Entity, EntityOptions } from 'typeorm';
import { Column, ColumnOptions, ColumnType, Entity, EntityOptions } from 'typeorm';
import * as Edm from '../../edm';
import { NotImplementedError, PropertyDefinitionError, ServerInternalError, StartupError } from '../../error';
import { ODataServer } from '../../server';
Expand All @@ -24,7 +24,13 @@ const KEY_ODATA_ENTITY_TYPE = 'odata.entity:entity_type';
const KEY_TYPEORM_DB_TYPE = 'odata.typeorm:db_type';
const KEY_WITH_ODATA_SERVER = 'odata:with_server';

export interface EColumnOptions extends ColumnOptions {
type ExcludedColumnType = 'float' | 'double' | 'float4' | 'float8' | 'double' | 'double precision';

export interface PropertyOptions extends ColumnOptions {
type?: Exclude<ColumnType, ExcludedColumnType>
}

export interface EColumnOptions extends PropertyOptions {
/**
* reflect metadata type, could be undefined
*/
Expand Down Expand Up @@ -143,14 +149,15 @@ export const getODataColumns = (classOrInstance): Array<EColumnOptions> => {

export const isODataEntityType = (classOrInstance): boolean => (getODataColumns(classOrInstance).length > 0);


/**
* ODataColumn
*
* combine the `Edm` & `typeorm` decorator
*
* @param options
*/
export function ODataColumn(options: ColumnOptions = {}) {
export function ODataColumn(options: PropertyOptions = {}) {
return function (object: any, propertyName: string): void {

const entityColumns = getODataColumns(object);
Expand Down Expand Up @@ -190,14 +197,13 @@ export function ODataColumn(options: ColumnOptions = {}) {
Edm.DefaultValue(options.default)(object, propertyName);
}


const reflectType = Reflect.getMetadata('design:type', object, propertyName);

switch (reflectType) {
case String:
switch (options.type) {
case 'decimal': case 'dec': case 'float': case 'float4': case 'float8':
case 'int2': case 'int4': case 'int8':
case 'tinyint': case 'int2': case 'int4': case 'int8':
case 'int64': case 'bigint':
throw new StartupError(`please use 'BigNumber' to define numeric property.`);
case 'uuid':
Expand Down Expand Up @@ -238,7 +244,7 @@ export function ODataColumn(options: ColumnOptions = {}) {
break;
case Number:
switch (options.type) {
case 'int2': case 'int4': case 'int8':
case 'tinyint': case 'int2': case 'int4': case 'int8':
Edm.Int16(object, propertyName);
break;
case 'integer':
Expand Down Expand Up @@ -300,8 +306,8 @@ export function getPropertyOptions(target: any, propsName: string): EColumnOptio
*
* @param defaultOption
*/
export function createPropertyDecorator(defaultOption: ColumnOptions) {
return function (options?: ColumnOptions): PropertyDecorator {
export function createPropertyDecorator(defaultOption: PropertyOptions) {
return function (options?: PropertyOptions): PropertyDecorator {
return function (target, propName) {
return ODataColumn({ ...defaultOption, ...options })(target, propName as string);
};
Expand Down
17 changes: 1 addition & 16 deletions test/type/server_query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { createServerAndClient, createTmpConnection } from './utils';

describe('server query result Test Suite', () => {


it('should support $orderby', async () => {
const conn = await createTmpConnection({
name: 's_query_conn_1',
Expand Down Expand Up @@ -201,12 +200,6 @@ describe('server query result Test Suite', () => {

it('should support decimal $filter', async () => {

@ODataModel()
class QueryDouble {
@UUIDKeyProperty() id: string;
@Property({ type: 'double' }) value: BigNumber;
}

@ODataModel()
class QueryDecimal {
@UUIDKeyProperty() id: string;
Expand All @@ -222,28 +215,20 @@ describe('server query result Test Suite', () => {

const conn = await createTmpConnection({
name: 's_query_conn_4',
entities: [QueryDecimal, QueryDouble, QueryInteger]
entities: [QueryDecimal, QueryInteger]
});

const { client, shutdownServer } = await createServerAndClient(conn);

try {
const qf = client.getEntitySet<QueryDouble>('QueryDoubles');
const qd = client.getEntitySet<QueryDecimal>('QueryDecimals');
const qi = client.getEntitySet<QueryInteger>('QueryIntegers');

await qf.create({ value: new BigNumber('99.99') });
await qf.create({ value: new BigNumber('100') });
await qd.create({ value: new BigNumber('99.99') });
await qd.create({ value: new BigNumber('100') });
await qi.create({ value: new BigNumber('1000000000000000') });
await qi.create({ value: new BigNumber('1000000000000001') });

expect(await qf.query(client.newFilter().field('value').ge('99.99'))).toHaveLength(2);
expect(await qf.query(client.newFilter().field('value').gt('99.99'))).toHaveLength(1);
expect(await qf.query(client.newFilter().field('value').ge(99.99))).toHaveLength(2);
expect(await qf.query(client.newFilter().field('value').gt(99.99))).toHaveLength(1);

expect(await qd.query(client.newFilter().field('value').ge('99.99'))).toHaveLength(2);
expect(await qd.query(client.newFilter().field('value').gt('99.99'))).toHaveLength(1);
expect(await qd.query(client.newFilter().field('value').ge(99.99))).toHaveLength(2);
Expand Down
2 changes: 2 additions & 0 deletions test/type/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-nocheck
import { sleep } from '@newdash/newdash';
import { OData, ODataV4 } from '@odata/client';
import '@odata/client/lib/polyfill';
import { Server } from 'http';
Expand Down Expand Up @@ -175,6 +176,7 @@ export async function createServerAndClient(conn, ...items: any[]) {
server: httpServer,
client,
shutdownServer: async () => {
await sleep(300);
await shutdown(httpServer);
await s.getConnection().close();
}
Expand Down

0 comments on commit 50547fb

Please sign in to comment.