Skip to content

Commit 37cf800

Browse files
Robin BuschmannRobin Buschmann
authored andcommitted
Merge branch 'master' into accessors
2 parents 9ae8076 + 5cb73f1 commit 37cf800

File tree

11 files changed

+106
-14
lines changed

11 files changed

+106
-14
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ npm-debug.log
44
.gitignore
55
tsconfig.json
66
test
7+
example
78
.DS_Store
89
.idea

README.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Decorators and some other extras for sequelize (v3 + v4).
99
- [`@Table` API](#table-api)
1010
- [`@Column` API](#column-api)
1111
- [Usage](#usage)
12+
- [Configuration](#Ccnfiguration)
13+
- [Model-path resolving](#model-path-resolving)
1214
- [Model association](#model-association)
1315
- [One-to-many](#one-to-many)
1416
- [Many-to-many](#many-to-many)
@@ -173,15 +175,26 @@ const sequelize = new Sequelize({
173175
username: 'root',
174176
password: '',
175177
storage: ':memory:',
176-
modelPaths: [__dirname + '/models'] // here
178+
modelPaths: [__dirname + '/models']
177179
});
178-
179-
sequelize.addModels([Person]); // and/or here
180180
```
181181
Before you can use your models, you have to tell sequelize where they can be found. So either set `modelPaths` in the
182182
sequlize config or add the required models later on by calling `sequelize.addModels([Person])` or
183-
`sequelize.addModels([__dirname + '/models'])`.
183+
`sequelize.addModels([__dirname + '/models'])`:
184+
185+
```typescript
186+
sequelize.addModels([Person]);
187+
sequelize.addModels(['path/to/models']);
188+
```
189+
#### Model-path resolving
190+
When using a path to resolve the required models, either the class has to be exported as default or if not exported
191+
as default, the file should have the same name as the corresponding class:
192+
```typescript
193+
export default class User extends Model<User> {}
184194

195+
// User.ts
196+
export class User extends Model<User> {}
197+
```
185198
### Build and create
186199
Instantiation and inserts can be achieved in the good old sequelize way
187200
```typescript
@@ -361,10 +374,10 @@ explicitly:
361374
proofedBooks: Book[];
362375
```
363376

364-
### Type safe usage of generated getter and setter
365-
With the creation of a relation, sequelize generates getter and setter functions on the corresponding
377+
### Type safe usage of auto generated functions
378+
With the creation of a relation, sequelize generates some method on the corresponding
366379
models. So when you create a 1:n relation between `ModelA` and `ModelB`, an instance of `ModelA` will
367-
have the functions `getModelBs`, `setModelBs`, `addModelB`. These functions still exist with *sequelize-typescript*.
380+
have the functions `getModelBs`, `setModelBs`, `addModelB`, `removeModelB`, `hasModelB`. These functions still exist with *sequelize-typescript*.
368381
But TypeScript will not know of them and in turn will complain, when you try to access `getModelB`, `setModelB` or
369382
`addModelB`. To make TypeScript happy, the `Model.prototype` of *sequelize-typescript* has `$set`, `$get`, `$add`
370383
functions.
@@ -387,9 +400,13 @@ To use them pass the property key of the respective relation as the first parame
387400
```typescript
388401
const modelA = new ModelA();
389402

390-
modelA.$set('bs', [ /* models */]).then( /* ... */);
391-
modelA.$add('b', /* model */).then( /* ... */);
403+
modelA.$set('bs', [ /* instance */]).then( /* ... */);
404+
modelA.$add('b', /* instance */).then( /* ... */);
392405
modelA.$get('bs').then( /* ... */);
406+
modelA.$count('bs').then( /* ... */);
407+
modelA.$has('bs').then( /* ... */);
408+
modelA.$remove('bs', /* instance */ ).then( /* ... */);
409+
modelA.$create('bs', /* value */ ).then( /* ... */);
393410
```
394411

395412
## Model validation

example/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {Comment} from './models/Comment';
55
import {Sequelize} from "../index";
66
import * as prettyjson from 'prettyjson';
77
import {Person} from "./models/validation-only/Person";
8-
import {Book} from "./models/Book";
8+
import Book from "./models/Book";
99

1010
/* tslint:disable:no-console */
1111
/* tslint:disable:no-unused-new */

example/models/Author.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Table, Model, PrimaryKey, Column, AutoIncrement, BelongsToMany,
22
DefaultScope, Scopes} from "../../index";
33
import {AuthorFriend} from "./AuthorFriend";
4-
import {Book} from "./Book";
4+
import Book from "./Book";
55

66
@DefaultScope({
77
attributes: ['id', 'name']

example/models/Book.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {Author} from "./Author";
88
}]}
99
})
1010
@Table
11-
export class Book extends Model<Book> {
11+
export default class Book extends Model<Book> {
1212

1313
@Column
1414
title: string;

lib/services/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export function getModels(arg: Array<typeof Model|string>): Array<typeof Model>
195195
if (!module[modelName] && !module.default) {
196196
throw new Error(`No default export defined for file "${file}" or export does not satisfy filename.`);
197197
}
198-
return module[modelName];
198+
return module[modelName] || module.default;
199199
});
200200

201201
models.push(..._models);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sequelize-typescript",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Decorators and some other extras for sequelize (v3 + v4)",
55
"scripts": {
66
"build": "tsc && tsc --project lib/models/v4",

test/models/exports/Game.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {Table, Column, Model} from '../../../index';
2+
3+
@Table
4+
export class Game extends Model<Game> {
5+
6+
@Column
7+
title: string;
8+
}

test/models/exports/gamer.model.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {Table, Column, Model} from '../../../index';
2+
3+
@Table
4+
export default class Gamer extends Model<Gamer> {
5+
6+
@Column
7+
nickname: string;
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {Table, Model} from '../../../../index';
2+
3+
@Table
4+
export class Cheater extends Model<Cheater> {}

0 commit comments

Comments
 (0)