This module enables TypeORM support in LoopBack. For pending features, refer to the Limitations section below.
TypeORM is a TypeScript ORM for Node.js. It supports many databases and can be used an as alternative to LoopBack's Juggler ORM.
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.
npm install --save @loopback/typeorm
To enable TypeORM support, import TypeOrmMixin
from @loopback/typeorm
and
apply it to your application class as shown below.
import {BootMixin} from '@loopback/boot';
import {RestApplication} from '@loopback/rest';
import {TypeOrmMixin} from '@loopback/typeorm';
export class MyApplication extends BootMixin(TypeOrmMixin(RestApplication)) {
...
}
Connections 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.
// 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
.
npm install sqlite3
Then, register SqliteConnection
connection in application.ts
file as shown
below:
import {BootMixin} from '@loopback/boot';
import {RestApplication} from '@loopback/rest';
import {TypeOrmMixin} from '@loopback/typeorm';
export class MyApplication extends BootMixin(TypeOrmMixin(RestApplication)) {
super(options);
this.connection(SqliteConnection);
...
}
Refer to the TypeORM documentation for the supported databases and the underlying drivers.
Entities are equivalent to LoopBack's Models.
Define the entities as usual and keep them in a directory named
typeorm-entities
.
// 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;
}
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.
// 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});
}
}
Please note, the current implementation does not support the following:
-
Complete TypeORM to OpenAPI data type conversion
(currently only
number
,string
, andboolean
are supported) - Full JSON/OpenAPI schema for entities, including variants like with/without id, with/without relations, partial, etc.
- Support for LoopBack-style filters
- JSON/OpenAPI schema to describe the supported filter format
- Custom repository classes (e.g. to implement bookRepo.findByTitle(title)).
- Database migration
Community contribution is welcome.
Run npm test
from the root folder.
See all contributors.
MIT