Giter Club home page Giter Club logo

loopback4-extension-typeorm's Introduction

loopback4-extension-typeorm

A component to provide TypeORM in LoopBack 4

DEPRECATED ALPHA: This is an experimental proof of concept showing how to implement a TypeORM mixin for LoopBack 4. The implementation is not currently supported for any production purpose and we are not maintaining this repository any more.

Usage

  1. Install this plugin and some dependencies
npm install --save loopback4-extension-typeorm typeorm
  1. In your application, make your own Application class, but instead of extending Application, you'll want to call the provided mixin as your base class.
import {Application} from '@loopback/core';
import {TypeORMRepositoryMixin} from 'loopback-typeorm';

export class MyApplication extends TypeORMRepositoryMixin(Application) {
  constructor() {
    super(...);
  }
}
  1. Create a connection (or multiple!) in your new subclass, and define whatever repositories you'd like to create.

A helpful way to ensure that your configuration has all of the required values is to import the ConnectionOptions type from TypeORM directly.

Note: There are connection options that become required within different use cases and contexts. For info on how to configure your database connection, see the TypeORM docs.

import {Application} from '@loopback/core';
import {TypeORMRepositoryMixin} from 'loopback-typeorm';
import {ConnectionOptions} from 'typeorm';
import {Order, Customer} from './models';

export class MyApplication extends TypeORMRepositoryMixin(Application) {
  mySqlConnection: Connection;
  constructor() {
    super();
    const connectionOptions: ConnectionOptions = {
      name: 'connectionName',
      host: 'somehost.com',
      database: 'mydb',
      port: 3306,
      type: 'mysql',
      username: 'admin',
      password: 'secretpassword',
      // etc...
    };
    this.mySqlConnection = this.createTypeOrmConnection(connectionOptions);

    // Automatically uses the connection to bind repositories to
    // your application context.
    this.typeOrmRepository(this.mySqlConnection, Order);
    this.typeOrmRepository(this.mySqlConnection, Customer);
   }
}
  1. Finally, consume your repositories in your controllers!
import {Customer, CustomerSchema} from '../models';
import {Repository} from 'typeorm';

export class CustomerController {
  constructor(@inject('repositories.Customer') customerRepo: Repository) {
    // ...
  }

  @get('/customer/{id}')
  @param.path.number('id');
  async getCustomerById(id: number) {
    // Using TypeORM's repository!
    return await this.customerRepo.findOneById(id);
  }

  @post('/customer')
  @param.body('customer', CustomerSchema);
  async createCustomer(customer: Customer) {
    return await this.customerRepo.save(customer);
  }
}

Testing

To run tests, you'll need an installation of Docker.

npm it

LoopBack

loopback4-extension-typeorm's People

Contributors

bajtos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.