https://github.com/paranoidjk/orm-ts
https://github.com/paranoidjk/orm-ts
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/paranoidjk/orm-ts
- Owner: paranoidjk
- License: mit
- Created: 2018-03-22T06:54:39.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-22T06:55:14.000Z (over 8 years ago)
- Last Synced: 2025-03-25T17:51:15.758Z (over 1 year ago)
- Language: TypeScript
- Size: 73.2 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# orm-ts
[](https://travis-ci.org/zhang740/orm-ts)
[](https://coveralls.io/github/zhang740/orm-ts)
[](https://www.npmjs.com/package/orm-ts)
[](https://github.com/zhang740/orm-ts/blob/master/LICENSE)
## Install
```shell
npm i orm-ts --save
```
## Example
### a simple example
service:
```ts
@register()
export class ActivityService {
@lazyInject()
private acitvityDomain: ActivityDomain;
async getById(id: number) {
return await this.acitvityDomain.getById(id);
}
}
```
domain:
```ts
@register()
export class ActivityDomain extends BaseDomain {
@lazyInject()
repository: ActivityRepository;
async getById(id: number) {
return await this.repository.getById(id);
}
}
```
repository:
```ts
@repository(ActivityModel)
export class ActivityRepository extends BaseRepository {
}
@repository(UserModel)
export class UserRepository extends BaseRepository {
@cachePut((username: string) => `user_${username}`, { expiredTime: 30 * 60 })
@bindSql(`select * from #table# where username = #username#`, { page: false })
async getByUsername(username: string) {
// SQL
return new UserModel(this.queryOne(`
xxxxxxxx
`, arguments));
}
async add(model: UserModel) { }
@cacheEvict((username: string) => `user_${username}`)
async updateByUsername(username: string, model: UserModel) { }
@cacheEvict((username: string) => `user_${username}`)
async deleteByUsername(username: string) { }
}
```
domain:
```ts
export class ActivityModel extends BaseModel {
id: number;
@manyToOne('owner', UserModel, 'getByUsername')
user: Promise;
@manyToOne('teamId', TeamModel)
team: Promise;
}
export class UserModel extends BaseModel {
id: number;
@valid([
{ minLength: 10 },
{ custom: (str: string) => true, errorMsg: 'xxx' }
])
username: string;
}
```
#### [See the full example.](https://github.com/zhang740/orm-ts/tree/master/example)