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: add TypeORM support #5801

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@
# - Standby owner(s): @deepakrkris
/extensions/apiconnect @raymondfeng @deepakrkris

#
# TypeORM
#
# - Issue label: TypeORM
# - Primary owner(s): @hacksparrow
# - Standby owner(s): n/a
/extensions/typeorm @hacksparrow

#
# Build & internal tooling
#
Expand Down
3 changes: 2 additions & 1 deletion docs/site/MONOREPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ one in the monorepo: `npm run update-monorepo-file`
| [extensions/health](https://github.com/strongloop/loopback-next/tree/master/extensions/health) | @loopback/extension-health | An extension exposes health check related endpoints with LoopBack 4 |
| [extensions/logging](https://github.com/strongloop/loopback-next/tree/master/extensions/logging) | @loopback/extension-logging | An extension exposes logging for Winston and Fluentd with LoopBack 4 |
| [extensions/metrics](https://github.com/strongloop/loopback-next/tree/master/extensions/metrics) | @loopback/extension-metrics | An extension exposes metrics for Prometheus with LoopBack 4 |
| [fixtures/mock-oauth2-provider](https://github.com/strongloop/loopback-next/tree/master/fixtures/mock-oauth2-provider) | _(private)_ | An internal application to mock the OAuth2 authorization flow login with a social app like facebook, google etc |
| [extensions/typeorm](https://github.com/strongloop/loopback-next/tree/master/extensions/typeorm) | @loopback/typeorm | Adds support for TypeORM in LoopBack |
| [fixtures/mock-oauth2-provider](https://github.com/strongloop/loopback-next/tree/master/fixtures/mock-oauth2-provider) | @loopback/mock-oauth2-provider | An internal application to mock the OAuth2 authorization flow login with a social app like facebook, google etc |
| [fixtures/tsdocs-monorepo](https://github.com/strongloop/loopback-next/tree/master/fixtures/tsdocs-monorepo) | _(private)_ | A monorepo for tsdocs testing |
| [packages/authentication](https://github.com/strongloop/loopback-next/tree/master/packages/authentication) | @loopback/authentication | A LoopBack component for authentication support. |
| [packages/authorization](https://github.com/strongloop/loopback-next/tree/master/packages/authorization) | @loopback/authorization | A LoopBack component for authorization support. |
Expand Down
10 changes: 10 additions & 0 deletions docs/site/Using-TypeORM-with-LoopBack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
lang: en
title: 'How to use TypeORM with LoopBack'
keywords: LoopBack 4.0, LoopBack 4, TypeORM
sidebar: lb4_sidebar
layout: readme
source: loopback-next
file: extensions/typeorm/README.md
permalink: /doc/en/lb4/Using-typeorm-with-loopback.html
---
7 changes: 7 additions & 0 deletions docs/site/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ children:
url: Database-migrations.html
output: 'web, pdf'

- title: 'Using TypeORM'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason we need a parent item instead of having "How to use TypeORM with LoopBack" as the top level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be more articles under it later. Eg: "How to create custom repositories", etc.

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

- title: 'Running and debugging apps'
url: Running-and-debugging-apps.html
output: 'web, pdf'
Expand Down
5 changes: 5 additions & 0 deletions extensions/typeorm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

25 changes: 25 additions & 0 deletions extensions/typeorm/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2020.
Node module: @loopback/typeorm
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
163 changes: 163 additions & 0 deletions extensions/typeorm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# @loopback/typeorm

This module enables TypeORM support in LoopBack. For pending features, refer to
the [Limitations](#limitations) section below.

## Overview

[TypeORM](https://typeorm.io/) is a TypeScript ORM for Node.js. It supports many
databases and can be used an as alternative to LoopBack's Juggler ORM.

## Stability: ⚠️Experimental⚠️

> Experimental packages provide early access to advanced or experimental
> functionality to get community feedback. Such modules are published to npm
> using `0.x.y` versions. Their APIs and functionality may be subject to
> breaking changes in future releases.

## Installation

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

## Basic Use

### Enabling TypeORM support

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';
export class MyApplication extends BootMixin(TypeOrmMixin(RestApplication)) {
...
}
```

### 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 `typeorm-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 Entities

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

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

@Column()
title: string;

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

### 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 '../typeorm-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](https://github.com/strongloop/loopback-next/issues/5893)
(currently only `number`, `string`, and `boolean` are supported)
2. [Full JSON/OpenAPI schema for entities](https://github.com/strongloop/loopback-next/issues/5894),
including variants like with/without id, with/without relations, partial,
etc.
3. [Support for LoopBack-style filters](https://github.com/strongloop/loopback-next/issues/5895)
4. [JSON/OpenAPI schema to describe the supported filter format](https://github.com/strongloop/loopback-next/issues/5896)
5. [Custom repository classes](https://github.com/strongloop/loopback-next/issues/5897)
(e.g. to implement bookRepo.findByTitle(title)).
6. [Database migration](https://github.com/strongloop/loopback-next/issues/5898)

Community contribution is welcome.

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
Loading