Nightly Patch Machine

    loopback4-extension-typeorm
    TypeScript icon, indicating that this package has built-in type declarations

    0.1.1 • Public • Published

    loopback-typeorm

    A component to provide TypeORM in LoopBack 4

    ALPHA: This is an experimental mixin for LoopBack 4, and is not currently supported for any production purpose. Instructions and APIs may change without warning.

    Usage

    1. Install this plugin
    npm install --save loopback-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

    Install

    npm i loopback4-extension-typeorm

    DownloadsWeekly Downloads

    2

    Version

    0.1.1

    License

    MIT

    Last publish

    Collaborators

    • kjdelisle