Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/svtslv/nestjs-knex
Knex module for Nest
https://github.com/svtslv/nestjs-knex
database knex nest nestjs orm
Last synced: 3 days ago
JSON representation
Knex module for Nest
- Host: GitHub
- URL: https://github.com/svtslv/nestjs-knex
- Owner: svtslv
- Created: 2020-02-28T20:05:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-16T10:47:07.000Z (5 months ago)
- Last Synced: 2024-09-19T10:08:37.469Z (about 2 months ago)
- Topics: database, knex, nest, nestjs, orm
- Language: TypeScript
- Size: 698 KB
- Stars: 65
- Watchers: 4
- Forks: 5
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NestJS Knex
## Table of Contents
- [Description](#description)
- [Installation](#installation)
- [Examples](#examples)
- [License](#license)## Description
Integrates Knex with Nest## Installation
```bash
npm install nestjs-knex knex
```You can also use the interactive CLI
```sh
npx nestjs-modules
```## Examples
```bash
npm install nestjs-knex knex sqlite3
```### KnexModule.forRoot(options, connection?)
```ts
import { Module } from '@nestjs/common';
import { KnexModule } from 'nestjs-knex';
import { AppController } from './app.controller';@Module({
imports: [
KnexModule.forRoot({
config: {
client: "sqlite3",
useNullAsDefault: true,
connection: ':memory:',
},
}),
],
controllers: [AppController],
})
export class AppModule {}
```### KnexModule.forRootAsync(options, connection?)
```ts
import { Module } from '@nestjs/common';
import { KnexModule } from 'nestjs-knex';
import { AppController } from './app.controller';@Module({
imports: [
KnexModule.forRootAsync({
useFactory: () => ({
config: {
client: "sqlite3",
useNullAsDefault: true,
connection: ':memory:',
},
}),
}),
],
controllers: [AppController],
})
export class AppModule {}
```### InjectKnex(connection?)
```ts
import { Controller, Get, } from '@nestjs/common';
import { InjectKnex, Knex } from 'nestjs-knex';@Controller()
export class AppController {
constructor(
@InjectKnex() private readonly knex: Knex,
) {}@Get()
async getHello() {
if (!await this.knex.schema.hasTable('users')) {
await this.knex.schema.createTable('users', table => {
table.increments('id').primary();
table.string('name');
});
}
await this.knex.table('users').insert({ name: 'Name' });
const users = await this.knex.table('users');
return { users };
}
}
```## License
MIT