The Turso Lib SQL client module for NestJS.
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
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 {}
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 {}
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 {
}
}
Contributions, issues and feature requests are welcome.
👤 Adrián Martínez Jiménez
- Github: @adrianmjim
See also the list of contributors who participated in this project.
Please ⭐️ this repository if this project helped you!
Copyright © 2024 Adrián Martínez Jiménez.
This project is licensed under the MIT License - see the LICENSE file for details.