Skip to content

Adrianmjim/nestjs-libsql-client

Repository files navigation

Nest Logo Turso

Test codecov NPM Version NPM Download Stats

Description

The Turso Lib SQL client module for NestJS.

Installation

Install the dependencies via npm, pnpm or yarn:

npm:

$ npm i -s @nestjs/common @libsql/client nestjs-libsql-client

pnpm:

$ pnpm add @nestjs/common @libsql/client nestjs-libsql-client

yarn:

$ yarn add @nestjs/common @libsql/client nestjs-libsql-client

Configuration

First, import the module into your NestJS application and configure it using the configuration url provided by Turso or desired local configuration

import { Module } from '@nestjs/common';
import { LibSqlModule } from 'nestjs-libsql-client';

@Module({
  imports: [
    LibSqlModule.forRoot({
      url: 'file:local.db',
    }),
  ],
})
export class AppModule {}

Or, You can configure it asynchronously as follows:

import { Module } from '@nestjs/common';
import { LibSqlModule } from 'nestjs-libsql-client';

@Module({
  imports: [
    LibSqlModule.forRootAsync({
      imports: [],
      inject: [],
      useFactory: () => ({
         url: 'file:local.db',
      }),
    }),
  ],
})
export class AppModule {}

Multiple connections

In certain situations, we will need to connect to different Turso databases, with this module this is possible:

import { Module } from '@nestjs/common';
import { LibSqlModule } from 'nestjs-libsql-client';

@Module({
  imports: [
    LibSqlModule.forRoot([
      {
        name: 'connection1',
        libSqlConfig: {
           url: 'file:local.db',
        },
      },
      {
        name: 'connection2',
        libSqlConfig: {
           url: 'file:local.db2',
        },
      },
    ]),
  ],
})
export class AppModule {}

Or asynchronously:

import { Module } from '@nestjs/common';
import { LibSqlModule } from 'nestjs-libsql-client';

@Module({
  imports: [
    LibSqlModule.forRootAsync({
      imports: [],
      inject: [],
      useFactory: () => ([
        {
          name: 'connection1',
          libSqlConfig: {
            url: 'file:local.db',
          },
        },
        {
          name: 'connection2',
          libSqlConfig: {
            url: 'file:local.db2',
          },
        },
      ]),
    }),
  ],
})
export class AppModule {}

Usage

First, inject the client into the module where you want to use it:

import { Module } from '@nestjs/common';
import { LibSqlModule } from 'nestjs-libsql-client';

@Module({
  imports: [
    LibSqlModule.injectClient(),
  ],
})
export class CatModule {}

Or, for a specific connection:

import { Module } from '@nestjs/common';
import { LibSqlModule } from 'nestjs-libsql-client';

@Module({
  imports: [
    LibSqlModule.injectClient('connection1', 'connection2'),
  ],
})
export class CatModule {}

Now you can use the Turso libsql client in any provider of your module, just use InjectLibSqlClient decorator:

import { Client } from '@libsql/client';
import { InjectLibSqlClient } from 'nestjs-libsql-client';

export class CatService {
  constructor(@InjectLibSqlClient() private readonly libSqlClient: Client) {}

  public doSomething(): void {

  }
}

Or, for a specific connection:

import { Client } from '@libsql/client';
import { InjectLibSqlClient } from 'nestjs-libsql-client';

export class CatService {
  constructor(
    @InjectLibSqlClient('connection1') private readonly libSqlClient: Client,
    @InjectLibSqlClient('connection2') private readonly libSqlClient2: Client,
  ) {}

  public doSomething(): void {

  }
}

It's also possible to use the InjectLibSqlClient decorator to inject the libsql client when you don't want to explicitly type it with the client:

import { InjectLibSqlClient } from 'nestjs-libsql-client';

export class CatService {
  constructor(
    @InjectLibSqlClient() private readonly libSqlClient: unknown,
  ) {}

  public doSomething(): void {

  }
}

🤝 Contributing contributions welcome

Contributions, issues and feature requests are welcome.

Authors

👤 Adrián Martínez Jiménez

See also the list of contributors who participated in this project.

Show Your Support

Please ⭐️ this repository if this project helped you!

📝 License

Copyright © 2024 Adrián Martínez Jiménez.

This project is licensed under the MIT License - see the LICENSE file for details.