Skip to content

Commit

Permalink
docs: site page
Browse files Browse the repository at this point in the history
Site page
  • Loading branch information
Yaapa Hage committed Jul 7, 2020
1 parent ff2b918 commit e178db3
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 10 deletions.
132 changes: 132 additions & 0 deletions docs/site/Using-TypeORM-with-LoopBack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
lang: en
title: 'How to use TypeORM with LoopBack'
keywords: LoopBack 4.0, LoopBack 4, TypeORM
sidebar: lb4_sidebar
permalink: /doc/en/lb4/Using-TypeORM-with-LoopBack.html
---

## Overview

If you would like to use TypeORM with LoopBack, it is possible with the use of
`@loopback/typeorm`. Install it as a dependency in your project.

```sh
npm install --save @loopback/typeorm
```

Then, import `TypeOrmMixin` from `@loopback/typeorm` and apply it to your
application class as shown below.

```ts
import {BootMixin} from '@loopback/boot';
import {RestApplication} from '@loopback/rest';
import {TypeOrmMixin} from '@loopback/typeorm';
export class MyApplication extends BootMixin(TypeOrmMixin(RestApplication)) {
...
}
```

### Creating Entities

[Entities](https://typeorm.io/#/entities) are equivalent to LoopBack's Models.
Define the entities as usual and keep them in a directory named `entities`.

```ts
// src/entities/book.entity.ts
import {Entity, Column, PrimaryColumn} from 'typeorm';
@Entity()
export class Photo {
@PrimaryColumn()
id: number;

@Column()
title: string;

@Column()
isPublished: boolean;
}
```

### Creating Connections

[Connections](https://typeorm.io/#/connection) are equivalent to LoopBack's
datasources. They contain the connectivity and other details about the
databases. Define the connection in files with a `.connection.ts` extension and
keep them in the `connections` directory of your project.

```ts
// src/connections/sqlite.connection.ts
import path from 'path';
import {ConnectionOptions} from '@loopback/typeorm';
import {Book} from '../entities/';

export const SqliteConnection: ConnectionOptions = {
name: 'SQLite',
type: 'sqlite',
database: './mydb.sql',
entities: [Book],
synchronize: true,
};
```

Make sure to install the underlying database driver. For example, if you are
using SQlite, you'll need to install `sqlite3`.

```sh
npm install sqlite3
```

Refer to the
[TypeORM documentation](https://github.com/typeorm/typeorm#installation) for the
supported databases and the underlying drivers.

### Creating controllers

Controllers continue to work as usual. And you don't have to create repositories
since TypeORM creates them for you; just inject them in the controllers. The
repository API is 100% TypeORM
[repository API](https://typeorm.io/#/repository-api).

```ts
// src/controllers/book.controller.ts
import {get, post, Request, requestBody} from '@loopback/rest';
import {getModelSchema, Repository, typeorm} from '@loopback/typeorm';
import {Book} from '../entities';

export class BookController {
@typeorm.repository(Book) private bookRepo: Repository<Book>;

constructor() {}

// Create a new book
@post('/book')
async create(@requestBody() data: Book) {
const book = new Book();
book.title = data.title;
book.published = false;
return await this.bookRepo.save(book);
}

// Find book by title
@get('/note/{title}')
async findByTitle(@param.path.string('title') title: string) {
return await this.bookRepo.find({title});
}
}
```

## Limitations

Please note, the current implementation does not support the following:

1. Complete TypeORM to OpenAPI data type conversion (currently only `number`,
`string`, and `boolean` are supported)
2. Full JSON/OpenAPI schema for models, including variants like with/without id,
with/without relations, partial, etc.
3. Json/OpenAPI schema to describe the supported filter format
4. Support for LoopBack-style filters
5. Custom repository classes (e.g. to implement bookRepo.findByTitle(title)).
6. Database migration

We will be implementing them progressively.
7 changes: 7 additions & 0 deletions docs/site/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ children:
output: 'web, pdf'
children:

- title: 'Using TypeORM'
output: 'web, pdf'
children:
- title: 'How to use TypeORM with LoopBack'
url: Using-TypeORM-with-LoopBack.html
output: 'web, pdf'

- title: 'Building REST APIs'
output: 'web, pdf'
children:
Expand Down
20 changes: 10 additions & 10 deletions packages/typeorm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ To enable TypeORM support, import `TypeOrmMixin` from `@loopback/typeorm` and
apply it to your application class as shown below.

```ts
import {BootMixin} from '@loopback/boot';
import {RestApplication} from '@loopback/rest';
import {TypeOrmMixin} from '@loopback/typeorm';
...
TypeOrmMixin(RestApplication);
export class MyApplication extends BootMixin(TypeOrmMixin(RestApplication)) {
...
}
```

### Creating Entities

[Entities](https://typeorm.io/#/entities) are equivalent to Juggler's Models.
Define the entities as usual and keep them in a directory named `entities`, or
even `models` or any name, just make sure to load them correctly in the
connections.
[Entities](https://typeorm.io/#/entities) are equivalent to LoopBack's Models.
Define the entities as usual and keep them in a directory named `entities`.

```ts
// src/entities/book.entity.ts
Expand All @@ -52,10 +52,10 @@ export class Photo {

### Creating Connections

[Connections](https://typeorm.io/#/connection) are equivalent to Juggler
[Connections](https://typeorm.io/#/connection) are equivalent to LoopBack's
datasources. They contain the connectivity and other details about the
databases. Define the connection in files with a `.connection.ts` extension and
keep them in the `connections` directory of the project.
keep them in the `connections` directory of your project.

```ts
// src/connections/sqlite.connection.ts
Expand Down Expand Up @@ -120,7 +120,7 @@ export class BookController {

## Limitations

The current implementation does not support the following:
Please note, the current implementation does not support the following:

1. Complete TypeORM to OpenAPI data type conversion (currently only `number`,
`string`, and `boolean` are supported)
Expand All @@ -131,7 +131,7 @@ The current implementation does not support the following:
5. Custom repository classes (e.g. to implement bookRepo.findByTitle(title)).
6. Database migration

We will be implementing them progressively in the follow up stories.
We will be implementing them progressively.

## Contributions

Expand Down

0 comments on commit e178db3

Please sign in to comment.