Skip to content

Commit a52e8e1

Browse files
Robin BuschmannRobin Buschmann
authored andcommitted
merged from intercharge-backend branch
2 parents 865e187 + bc08847 commit a52e8e1

29 files changed

+1692
-1
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.md]
13+
insert_final_newline = false
14+
trim_trailing_whitespace = false

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# mac
2+
*.DS_Store
3+
4+
# ide
5+
.idea
6+
7+
# libraries
8+
node_modules
9+
typings
10+
11+
# cache
12+
.temp
13+
.sass-cache
14+
15+
# testing
16+
coverage
17+
test
18+
19+
# certificates
20+
certificates/*
21+
22+
# documentation
23+
documentation
24+
25+
# js files
26+
**/*.js
27+
**/*.js.map
28+
# expect
29+
!Gruntfile.js
30+
31+
# ..
32+
evseDataMock.json
33+
evseStatusMock.json
34+
playground.sql
35+
req.xml
36+
37+
# Elastic Beanstalk Files
38+
.elasticbeanstalk/*
39+
!.elasticbeanstalk/*.cfg.yml
40+
!.elasticbeanstalk/*.global.yml

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
# sequelize-typescript
1+
# sequelize-typescript
2+
3+
For simplicity and to prevent an interface chaos for the interaction of _TypeScript_ and _Sequelize_, a wrapper on top
4+
of both is implemented. The implementation is currently found in `orm/`.
5+
6+
## Definition of database models
7+
To define a database model you have to annotate the class(which represents you specific entity) with the `Table` and
8+
`Column` annotations. `Table` for defining the entity and `Column` for defining the column/property of the entity.
9+
For example:
10+
11+
````
12+
13+
@Table
14+
class Person {
15+
16+
@Column
17+
@PrimaryKey
18+
id: number;
19+
20+
@Column
21+
name: string;
22+
23+
}
24+
25+
26+
````
27+
#### Associations
28+
For Relations between entities there are some annotations like `BelongsTo`, `HasOne` or `BelongsToMany` that can be used. To define
29+
foreign keys, use the `ForeignKey` annotation.
30+
31+
##### Many-To-Many
32+
````
33+
34+
@Table
35+
class Person {
36+
37+
...
38+
39+
@BelongsToMany(() => Group, () => PersonGroup)
40+
groups: Group[];
41+
42+
}
43+
44+
@Table
45+
class Group {
46+
47+
...
48+
49+
@BelongsToMany(() => Person, () => PersonGroup)
50+
persons: Person[];
51+
52+
}
53+
54+
@Table
55+
class PersonGroup {
56+
57+
@ForeignKey(() => Person)
58+
personId: number;
59+
60+
@ForeignKey(() => Group)
61+
groupId: number;
62+
63+
}
64+
65+
66+
````

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "sequelize-typescript",
3+
"version": "1.0.1",
4+
"description": "Wrapper for sequelize to annotate classes",
5+
"scripts": {},
6+
"main": "dist",
7+
"dependencies": {
8+
"@types/node": "^6.0.41",
9+
"@types/reflect-metadata": "0.0.4",
10+
"@types/sequelize": "^4.0.36",
11+
"sequelize": "git+https://github.com/RobinBuschmann/sequelize.git"
12+
},
13+
"devDependencies": {
14+
"typescript": "^2.0.3"
15+
},
16+
"engines": {
17+
"node": ">=0.8.15"
18+
}
19+
}

src/annotations/AutoIncrement.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'reflect-metadata';
2+
import Sequelize = require("sequelize");
3+
import {SequelizeModelService} from "../services/SequelizeModelService";
4+
5+
/**
6+
* Sets auto increment true for annotated field
7+
*/
8+
export function AutoIncrement(target: any, propertyName: string) {
9+
10+
let options = SequelizeModelService.getAttributeOptions(target.constructor, propertyName);
11+
12+
options.autoIncrement = true;
13+
}

src/annotations/BelongsTo.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Model} from "../models/Model";
2+
import {SequelizeAssociationService} from "../services/SequelizeAssociationService";
3+
4+
export function BelongsTo(relatedClassGetter: () => typeof Model, foreignKey?: string) {
5+
6+
return function (target: any, propertyName: string) {
7+
8+
SequelizeAssociationService.addAssociation(
9+
target.constructor,
10+
SequelizeAssociationService.BELONGS_TO,
11+
relatedClassGetter,
12+
propertyName,
13+
foreignKey
14+
)
15+
}
16+
}

src/annotations/BelongsToMany.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Model} from "../models/Model";
2+
import {SequelizeAssociationService} from "../services/SequelizeAssociationService";
3+
4+
export function BelongsToMany(relatedClassGetter: () => typeof Model, throughClass: () => typeof Model);
5+
export function BelongsToMany(relatedClassGetter: () => typeof Model, through: string, foreignKey?: string);
6+
export function BelongsToMany(relatedClassGetter: () => typeof Model, through: (() => typeof Model)|string, foreignKey?: string) {
7+
8+
return function (target: any, propertyName: string) {
9+
10+
SequelizeAssociationService.addAssociation(
11+
target.constructor,
12+
SequelizeAssociationService.BELONGS_TO_MANY,
13+
relatedClassGetter,
14+
propertyName,
15+
through,
16+
foreignKey
17+
)
18+
}
19+
}

src/annotations/Boolean.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'reflect-metadata';
2+
import Sequelize = require("sequelize");
3+
import {Type} from "./Type";
4+
import {DataType} from "../models/DataType";
5+
6+
/**
7+
* Sets type option for annotated property to specified value.
8+
* This annotation will not work together with Column(options) annotation,
9+
* but has to be used with Column (Please notice the difference with and
10+
* without options)
11+
*/
12+
export const Boolean = Type(DataType.BOOLEAN);

src/annotations/Column.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'reflect-metadata';
2+
import Sequelize = require("sequelize");
3+
import {DataTypeAbstract} from "sequelize";
4+
import {DefineAttributeColumnOptions} from "sequelize";
5+
import {SequelizeModelService} from "../services/SequelizeModelService";
6+
7+
export function Column(options: DefineAttributeColumnOptions);
8+
export function Column(target: any, propertyName: string);
9+
export function Column(arg) {
10+
11+
// In case of no specified options, we retrieve the
12+
// sequelize data type by the type of the property
13+
if(arguments.length === 2) {
14+
15+
let target = arguments[0];
16+
let propertyName = arguments[1];
17+
18+
const sequelizeType = SequelizeModelService.getSequelizeTypeByDesignType(target, propertyName);
19+
const options = SequelizeModelService.getAttributeOptions(target.constructor, propertyName);
20+
options.type = sequelizeType;
21+
22+
return;
23+
}
24+
25+
return function (target: any, propertyName: string) {
26+
27+
SequelizeModelService.addAttribute(target.constructor, propertyName, arg)
28+
}
29+
30+
}

src/annotations/Default.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'reflect-metadata';
2+
import Sequelize = require("sequelize");
3+
import {SequelizeModelService} from "../services/SequelizeModelService";
4+
5+
/**
6+
* Sets the specified default value for the annotated field
7+
*/
8+
export function Default(value: any) {
9+
10+
return function (target: any, propertyName: string) {
11+
12+
let options = SequelizeModelService.getAttributeOptions(target.constructor, propertyName);
13+
14+
options.defaultValue = value;
15+
}
16+
}

0 commit comments

Comments
 (0)