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

Commit

Permalink
feat: #4 typeorm support enhance
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jul 14, 2020
1 parent 89a1e1a commit 9f47a2c
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 190 deletions.
222 changes: 56 additions & 166 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"express": "^4.17.1",
"qs": "^6.9.4",
"reflect-metadata": "^0.1.13",
"tslib": "^2.0.0"
"tslib": "^2.0.0",
"typeorm": "^0.2.25"
},
"devDependencies": {
"@types/benchmark": "^1.0.33",
Expand Down Expand Up @@ -80,7 +81,6 @@
"stream-buffers": "^3.0.2",
"ts-jest": "^26.1.1",
"ts-node": "^8.10.2",
"typeorm": "^0.2.25",
"typescript": "^3.9.6",
"xml-beautifier": "^0.4.3"
}
Expand Down
32 changes: 12 additions & 20 deletions src/example/typed_simple_server.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
import { Entity, BaseEntity, PrimaryColumn, Column, createConnection, PrimaryGeneratedColumn } from 'typeorm';
import { Edm } from '../lib';
import { createTypedODataServer } from '../lib/typeorm';
import { createTypedODataServer, ODataColumn, ODataModel } from '../lib/typeorm';
import { randomPort } from '../test/utils/randomPort';

@Entity()
@ODataModel()
class Student extends BaseEntity {

@Edm.Key
@Edm.Int32
@PrimaryGeneratedColumn() // generated
@ODataColumn({ primary: true, generated: "increment" })
// generated
id: number;

@Edm.String
@Column()
@ODataColumn()
name: string;

@Edm.Int32
@Column()
@ODataColumn()
age: number;

}

@Entity()
@ODataModel()
class Class extends BaseEntity {

@Edm.Key
@Edm.Int32
@PrimaryGeneratedColumn()
@ODataColumn({ primary: true, generated: "increment" })
id: number;

@Edm.String
@Column()
@ODataColumn()
name: string;

@Edm.String
@Column()
@ODataColumn()
desc: string;

}


const run = async() => {
const run = async () => {
const conn = await createConnection({
name: 'default',
type: 'sqljs',
synchronize: true,
// logging: true,
logging: true,
entities: [Student, Class]
});
const server = createTypedODataServer(conn.name, Student, Class);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'reflect-metadata';

// exports Edm decorator system
export * from './edm';
import * as _Edm from './edm';
Expand Down
55 changes: 55 additions & 0 deletions src/lib/typeorm/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ColumnOptions, Column, PrimaryColumn, EntityOptions, Entity } from 'typeorm';
import { Edm } from '..';
import { NotImplementedError } from '../error';


/**
* ODataModel
*
* decorator wrapper of the typeorm `Entity` decorator
*
* @param options
*/
export function ODataModel(options: EntityOptions = {}) {
return function(target) {
Entity(options)(target);
};
}

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

Column(options)(object, propertyName);

if (primary) {
Edm.Key(object, propertyName);
}

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

switch (reflectType) {
case String:
Edm.String(object, propertyName);
break;
case Number:
Edm.Int32(object, propertyName);
break;
case Boolean:
Edm.Boolean(object, propertyName);
break;
case Date:
Edm.DateTimeOffset(object, propertyName);
break;
default:
throw new NotImplementedError(`Not support the type of field '${propertyName}'.`);
}
};
}
1 change: 1 addition & 0 deletions src/lib/typeorm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
export * from './typed_controller';
export * from './visitor';
export * from './service';
export * from './decorators';
27 changes: 25 additions & 2 deletions src/test/typeorm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-nocheck
import "reflect-metadata"
import "light-odata/lib/polyfill"
import { TypedController, transformQueryAst, transformFilterAst, FieldNameMapper, withConnection, createTypedODataServer } from "../lib/typeorm";
import { createConnection, Entity, PrimaryColumn, Column, ConnectionOptions, getConnection, BaseEntity, PrimaryGeneratedColumn } from "typeorm";
import { Edm, odata, ODataServer, withController } from "../lib/index"
import { Edm, odata, ODataServer, withController, ODataColumn, TypedController, transformQueryAst, transformFilterAst, FieldNameMapper, withConnection, createTypedODataServer } from "../lib/index"
import { randomPort } from './utils/randomPort';
import { ready, shutdown } from './utils/server';
import * as req from 'request-promise';
Expand Down Expand Up @@ -210,5 +210,28 @@ describe('Typeorm Integration Test Suite', () => {

});

it('should works with decorator', () => {

class E1 {

@ODataColumn()
f1: string

@ODataColumn()
f2: boolean

@ODataColumn()
f3: number

@ODataColumn()
f4: Date

@ODataColumn()
f5: 'a' | 'b'

}

});


});

0 comments on commit 9f47a2c

Please sign in to comment.