Numerous Packages Multiply

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

    2.0.1 • Public • Published

    loopback4-sequelize

    LoopBack

    This is a loopback4 extension that provides Sequelize's query builder at repository level in any loopback 4 application. It has zero learning curve as it follows similar interface as DefaultCrudRepository. For relational databases, Sequelize is a popular ORM of choice.

    For pending features, refer to the Limitations section below.

    Installation

    To install this extension in your Loopback 4 project, run the following command:

    npm install loopback4-sequelize

    You'll also need to install the driver for your preferred database:

    # One of the following:
    npm install --save pg pg-hstore # Postgres
    npm install --save mysql2
    npm install --save mariadb
    npm install --save sqlite3
    npm install --save tedious # Microsoft SQL Server
    npm install --save oracledb # Oracle Database

    Usage

    You can watch a video overview of this extension by clicking here.

    Both newly developed and existing projects can benefit from the extension by simply changing the parent classes in the target Data Source and Repositories.

    Step 1: Configure DataSource

    Change the parent class from juggler.DataSource to SequelizeDataSource like below.

    // ...
    import {SequelizeDataSource} from 'loopback4-sequelize';
    
    // ...
    export class PgDataSource
      extends SequelizeDataSource
      implements LifeCycleObserver {
      // ...
    }

    Step 2: Configure Repository

    Change the parent class from DefaultCrudRepository to SequelizeCrudRepository like below.

    // ...
    import {SequelizeCrudRepository} from 'loopback4-sequelize';
    
    export class YourRepository extends SequelizeCrudRepository<
      YourModel,
      typeof YourModel.prototype.id,
      YourModelRelations
    > {
      // ...
    }

    Relations

    Supported Loopback Relations

    With SequelizeCrudRepository, you can utilize following relations without any additional configuration:

    1. HasMany Relation
    2. BelongsTo Relation
    3. HasOne Relation
    4. HasManyThrough Relation
    5. ReferencesMany Relation

    The default relation configuration, generated using the lb4 relation command (i.e. inclusion resolvers in the repository and property decorators in the model), remain unchanged.

    INNER JOIN

    Check the demo video of using inner joins here: https://youtu.be/ZrUxIk63oRc?t=76

    When using SequelizeCrudRepository, the find(), findOne(), and findById() methods accept a new option called required in the include filter. Setting this option to true will result in an inner join query that explicitly requires the specified condition for the child model. If the row does not meet this condition, it will not be fetched and returned.

    An example of the filter object might look like this to fetch the books who contains "Art" in their title, which belongs to category "Programming":

    {
      "where": {"title": {"like": "%Art%"}},
      "include": [
        {
          "relation": "category",
          "scope": {
            "where": {
              "name": "Programming"
            }
          },
          "required": true // 👈
        }
      ]
    }

    Debug strings reference

    There are three built-in debug strings available in this extension to aid in debugging. To learn more about how to use them, see this page.

    String Description
    Datasource
    loopback:sequelize:datasource Database Connections logs
    loopback:sequelize:queries Logs Executed SQL Queries and Parameters
    Repository
    loopback:sequelize:modelbuilder Logs Translation of Loopback Models Into Sequelize Supported Definitions. Helpful When Debugging Datatype Issues

    Limitations

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

    1. SQL Transactions.
    2. Loopback Migrations (via default migrate.ts). Though you're good if using external packages like db-migrate.
    3. Connection Pooling is not implemented yet.

    Community contribution is welcome.

    Feedback

    If you've noticed a bug or have a question or have a feature request, search the issue tracker to see if someone else in the community has already created a ticket. If not, go ahead and make one! All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can. If you think this extension is useful, please star it. Appreciation really helps in keeping this project alive.

    Contributing

    Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.

    Code of conduct

    Code of conduct guidelines here.

    License

    MIT

    Install

    npm i loopback4-sequelize

    DownloadsWeekly Downloads

    25

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    263 kB

    Total Files

    87

    Last publish

    Collaborators

    • samarpan_sf