undeexcepturi
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
149 lines (97 loc) • 4.13 kB
Markdown
---
title: Defining entities
---
Entities are simple javascript objects (so called POJO), decorated with `` decorator. No real restrictions are made, you do not have to extend any base class, you are more than welcome to [use entity constructors](./entity-constructors.md), just do not forget to specify primary key with `` decorator.
```typescript title="./entities/Book.ts"
export class Book {
_id: ObjectID;
createdAt = new Date();
updatedAt = new Date();
title: string;
// when you provide correct type hint, ORM will read it for you
author: Author;
// or you can specify the entity as class reference or string name
publisher: Publisher;
tags = new Collection<BookTag>(this);
constructor(title: string, author: Author) {
this.title = title;
this.author = author;
}
}
export interface Book extends IEntity<string> { }
```
You will need to extend Book's interface with `IEntity`. The interface represents internal methods added to your entity's prototype via `` decorator.
> `IEntity` is generic interface, its type parameter depends on data type of normalized primary key produced by used driver. SQL drivers usually use `number` and Mongo driver uses `string`. This type default to union type `number | string`. Keep in mind that you have to worry about this only when you define your primary key as `_id` instead of `id`.
As you can see, entity properties are decorated either with `` decorator, or with one of reference decorators: ``, ``, `` and ``.
Here is another example of `Author` entity, that was referenced from the `Book` one:
```typescript title="./entities/Author.ts"
export class Author {
_id: ObjectID;
createdAt = new Date();
updatedAt = new Date();
name: string;
email: string;
age: number;
termsAccepted = false;
identities: string[];
born: Date;
books = new Collection<Book>(this);
favouriteBook: Book;
version: number;
versionAsString: string;
constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
}
export interface Author extends IEntity { }
```
More information about how collections work can be found on [collections page](./collections.md).
If you want to define your entity in Vanilla JavaScript, take a look [here](./usage-with-js.md).
## Entity file names
You are free to choose one of those formats for entity filename (for a `BookTag` entity):
- `BookTag.ts`
- `BookTag.model.ts`
- `book-tag.ts`
- `book-tag.model.ts`
- `book-tag.entity.ts`
Entity name is inferred from the first part of file name before first dot occurs, so you can add any suffix behind the dot, not just `.model.ts` or `.entity.ts`.
## Using BaseEntity
You can define your own base entity with properties that you require on all entities, like primary key and created/updated time.
> If you are initializing the ORM via `entities` option, you need to specify all your base entities as well.
```typescript title="./entities/BaseEntity.ts"
export abstract class BaseEntity {
_id: ObjectID;
createdAt = new Date();
updatedAt = new Date();
}
```
## Note about SQL drivers and
All entities described above were defined with `_id: ObjectID` primary key - those were Mongo entities.
For SQL drivers, you will want to define your primary key as `id: number` instead:
```typescript
id: number;
```
With your entities set up, you can start [using entity manager](./entity-manager.md) and [repositories](./repositories.md) as described in following sections.