Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added declare to docs #1649

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 62 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,20 @@ import { Table, Column, Model, HasMany } from 'sequelize-typescript';
@Table
class Person extends Model {
@Column
name: string;
declare name: string;

@Column
birthday: Date;
declare birthday: Date;

@HasMany(() => Hobby)
hobbies: Hobby[];
declare hobbies: Hobby[];
}
```

Sequelize adds getter & setter for each attribute defined through `Model.init`. Adding class fields will override them, causing issues with access. To prevent that `declare` should be put before any attribute.

More could be found in [official docs](https://sequelize.org/docs/v6/core-concepts/model-basics/#caveat-with-public-class-fields).

### Less strict

```typescript
Expand Down Expand Up @@ -157,13 +161,13 @@ Annotations to define custom and type safe `createdAt`, `updatedAt` and `deleted

```typescript
@CreatedAt
creationDate: Date;
declare creationDate: Date;

@UpdatedAt
updatedOn: Date;
declare updatedOn: Date;

@DeletedAt
deletionDate: Date;
declare deletionDate: Date;
```

| Decorator | Description |
Expand All @@ -179,7 +183,7 @@ the js type can be inferred automatically (see [Type inference](#type-inference)

```typescript
@Column
name: string;
declare name: string;
```

If the type cannot or should not be inferred, use:
Expand All @@ -188,7 +192,7 @@ If the type cannot or should not be inferred, use:
import {DataType} from 'sequelize-typescript';

@Column(DataType.TEXT)
name: string;
declare name: string;
```

Or for a more detailed column description, use an object literal
Expand All @@ -201,7 +205,7 @@ from sequelize are valid):
comment: 'Some value',
...
})
value: number;
declare value: number;
```

#### Column API
Expand Down Expand Up @@ -343,7 +347,7 @@ export const NUser = 'Not a model';
export class User extends Model {

@Column
nickname: string;
declare nickname: string;
}
```

Expand Down Expand Up @@ -413,26 +417,26 @@ and `@ForeignKey` annotations.
@Table
class Player extends Model {
@Column
name: string;
declare name: string;

@Column
num: number;
declare num: number;

@ForeignKey(() => Team)
@Column
teamId: number;
declare teamId: number;

@BelongsTo(() => Team)
team: Team;
declare team: Team;
}

@Table
class Team extends Model {
@Column
name: string;
declare name: string;

@HasMany(() => Player)
players: Player[];
declare players: Player[];
}
```

Expand All @@ -458,18 +462,18 @@ class Book extends Model {
@Table
class Author extends Model {
@BelongsToMany(() => Book, () => BookAuthor)
books: Book[];
declare books: Book[];
}

@Table
class BookAuthor extends Model {
@ForeignKey(() => Book)
@Column
bookId: number;
declare bookId: number;

@ForeignKey(() => Author)
@Column
authorId: number;
declare authorId: number;
}
```

Expand All @@ -480,7 +484,7 @@ need to be set up manually. For `Author` model it can be achieved like so:

```ts
@BelongsToMany(() => Book, () => BookAuthor)
books: Array<Book & {BookAuthor: BookAuthor}>;
declare books: Array<Book & {BookAuthor: BookAuthor}>;
```

### One-to-one
Expand Down Expand Up @@ -520,26 +524,26 @@ So if you define a model with multiple relations like
class Book extends Model {
@ForeignKey(() => Person)
@Column
authorId: number;
declare authorId: number;

@BelongsTo(() => Person)
author: Person;
declare author: Person;

@ForeignKey(() => Person)
@Column
proofreaderId: number;
declare proofreaderId: number;

@BelongsTo(() => Person)
proofreader: Person;
declare proofreader: Person;
}

@Table
class Person extends Model {
@HasMany(() => Book)
writtenBooks: Book[];
declare writtenBooks: Book[];

@HasMany(() => Book)
proofedBooks: Book[];
declare proofedBooks: Book[];
}
```

Expand All @@ -550,17 +554,17 @@ explicitly:

// in class "Books":
@BelongsTo(() => Person, 'authorId')
author: Person;
declare author: Person;

@BelongsTo(() => Person, 'proofreaderId')
proofreader: Person;
declare proofreader: Person;

// in class "Person":
@HasMany(() => Book, 'authorId')
writtenBooks: Book[];
declare writtenBooks: Book[];

@HasMany(() => Book, 'proofreaderId')
proofedBooks: Book[];
declare proofedBooks: Book[];
```

### Type safe usage of auto generated functions
Expand All @@ -576,13 +580,13 @@ functions.
@Table
class ModelA extends Model {
@HasMany(() => ModelB)
bs: ModelB[];
declare bs: ModelB[];
}

@Table
class ModelB extends Model {
@BelongsTo(() => ModelA)
a: ModelA;
declare a: ModelA;
}
```

Expand Down Expand Up @@ -615,11 +619,11 @@ The `@Index` annotation can be used without passing any parameters.
class Person extends Model {
@Index // Define an index with default name
@Column
name: string;
declare name: string;

@Index // Define another index
@Column
birthday: Date;
declare birthday: Date;
}
```

Expand All @@ -631,11 +635,11 @@ an object literal (see [indexes define option](https://sequelize.org/v5/manual/m
class Person extends Model {
@Index('my-index') // Define a multi-field index on name and birthday
@Column
name: string;
declare name: string;

@Index('my-index') // Add birthday as the second field to my-index
@Column
birthday: Date;
declare birthday: Date;

@Index({
// index options
Expand All @@ -654,10 +658,10 @@ class Person extends Model {
collate: 'NOCASE',
})
@Column
jobTitle: string;
declare jobTitle: string;

@Column
isEmployee: boolean;
declare isEmployee: boolean;
}
```

Expand Down Expand Up @@ -692,11 +696,11 @@ const JobIndex = createIndexDecorator({
class Person extends Model {
@SomeIndex // Add name to SomeIndex
@Column
name: string;
declare name: string;

@SomeIndex // Add birthday to SomeIndex
@Column
birthday: Date;
declare birthday: Date;

@JobIndex({
// index field options
Expand All @@ -705,10 +709,10 @@ class Person extends Model {
collate: 'NOCASE',
})
@Column
jobTitle: string;
declare jobTitle: string;

@Column
isEmployee: boolean;
declare isEmployee: boolean;
}
```

Expand Down Expand Up @@ -805,48 +809,48 @@ export class Shoe extends Model {
@IsUUID(4)
@PrimaryKey
@Column
id: string;
declare id: string;

@Equals('lala')
@Column
readonly key: string;
declare readonly key: string;

@Contains('Special')
@Column
special: string;
declare special: string;

@Length({ min: 3, max: 15 })
@Column
brand: string;
declare brand: string;

@IsUrl
@Column
brandUrl: string;
declare brandUrl: string;

@Is('HexColor', (value) => {
if (!HEX_REGEX.test(value)) {
throw new Error(`"${value}" is not a hex color value.`);
}
})
@Column
primaryColor: string;
declare primaryColor: string;

@Is(function hexColor(value: string): void {
if (!HEX_REGEX.test(value)) {
throw new Error(`"${value}" is not a hex color value.`);
}
})
@Column
secondaryColor: string;
declare secondaryColor: string;

@Is(HEX_REGEX)
@Column
tertiaryColor: string;
declare tertiaryColor: string;

@IsDate
@IsBefore('2017-02-27')
@Column
producedAt: Date;
declare producedAt: Date;
}
```

Expand All @@ -872,23 +876,23 @@ sequelize (See sequelize [docs](https://sequelize.org/master/manual/scopes.html)
@Table
export class ShoeWithScopes extends Model {
@Column
readonly secretKey: string;
declare readonly secretKey: string;

@Column
primaryColor: string;
declare primaryColor: string;

@Column
secondaryColor: string;
declare secondaryColor: string;

@Column
producedAt: Date;
declare producedAt: Date;

@ForeignKey(() => Manufacturer)
@Column
manufacturerId: number;
declare manufacturerId: number;

@BelongsTo(() => Manufacturer)
manufacturer: Manufacturer;
declare manufacturer: Manufacturer;
}
```

Expand All @@ -904,7 +908,7 @@ The name of the method cannot be the same as the name of the hook (for example,
@Table
export class Person extends Model {
@Column
name: string;
declare name: string;

@BeforeUpdate
@BeforeCreate
Expand Down