{"_id":"@bumjs/nestjs-slonik","_rev":"5-fe118d15485e0988ca376387d5505325","name":"@bumjs/nestjs-slonik","dist-tags":{"latest":"9.4.0"},"versions":{"9.0.0":{"name":"@bumjs/nestjs-slonik","version":"9.0.0","description":"Slonik module for NestJS","keywords":["nest","nestjs","slonik"],"author":{"name":"Mikhail Korobkov"},"license":"MIT","url":"https://github.com/mkorobkov/nestjs-slonik#readme","scripts":{"build":"rm -rf dist && tsc -p tsconfig.build.json","format":"prettier --write \"**/*.ts\"","lint":"eslint 'lib/**/*.ts' --fix","test:e2e":"jest --config ./tests/jest-e2e.json --runInBand","test:e2e:dev":"jest --config ./tests/jest-e2e.json --runInBand --watch","prerelease":"npm run build","release":"release-it"},"devDependencies":{"@commitlint/cli":"12.1.4","@commitlint/config-angular":"12.1.4","@nestjs/common":"8.3.1","@nestjs/core":"8.3.1","@nestjs/platform-express":"8.3.1","@nestjs/testing":"8.3.1","@types/jest":"27.4.0","@types/node":"17.0.8","@types/supertest":"2.0.11","@types/uuid":"8.3.4","@typescript-eslint/eslint-plugin":"5.12.0","@typescript-eslint/parser":"5.12.0","eslint":"8.9.0","eslint-config-prettier":"8.4.0","eslint-plugin-import":"2.25.4","husky":"6.0.0","jest":"27.5.1","lint-staged":"12.3.4","pg":"8.7.3","prettier":"2.5.1","reflect-metadata":"0.1.13","release-it":"14.12.4","rxjs":"7.5.4","slonik":"27.1.0","supertest":"6.2.2","ts-jest":"27.1.3","typescript":"4.5.5"},"dependencies":{"uuid":"8.3.2"},"peerDependencies":{"@nestjs/common":"^8.0.0 || ^9.0.0 || ^10.0.0","@nestjs/core":"^8.0.0 || ^9.0.0 || ^10.0.0","reflect-metadata":"^0.1.13","rxjs":"^7.2.0","slonik":">=27.0.0"},"lint-staged":{"*.ts":["prettier --write"]},"repository":{"type":"git","url":"git+https://github.com/mkorobkov/nestjs-slonik.git"},"types":"./index.d.ts","gitHead":"8f0ce57772995341dd3bf43eaeebc7eb3114a276","bugs":{"url":"https://github.com/mkorobkov/nestjs-slonik/issues"},"homepage":"https://github.com/mkorobkov/nestjs-slonik#readme","_id":"@bumjs/nestjs-slonik@9.0.0","_nodeVersion":"18.12.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-KcOrjkWBoHh3j56TPi0UCparc1CJrFB8prjh29tGsGBGNCJUlIIC3u9gdj+gBBInWkTtJ1yPI/AJ47I2avQiXg==","shasum":"d4617a7d3c34f79e9166a209b86969ecba948c8b","tarball":"https://registry.npmjs.org/@bumjs/nestjs-slonik/-/nestjs-slonik-9.0.0.tgz","fileCount":23,"unpackedSize":28728,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIB/qNO4rJPUJXVbsw2MQnaCGyew5Oq9eTJpEonAhheyeAiBf+ztSUNt0IxbADLkioUiAGYk6/9ISmEHEAO75HccjDg=="}]},"_npmUser":{"name":"padorang684","email":"padorang684@gmail.com"},"directories":{},"maintainers":[{"name":"padorang684","email":"padorang684@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-slonik_9.0.0_1696302515894_0.4297551937013697"},"_hasShrinkwrap":false},"9.1.0":{"name":"@bumjs/nestjs-slonik","version":"9.1.0","description":"Slonik module for NestJS","keywords":["nest","nestjs","slonik"],"type":"module","author":{"name":"Mikhail Korobkov"},"license":"MIT","url":"https://github.com/mkorobkov/nestjs-slonik#readme","scripts":{"build":"rm -rf dist && tsc -p tsconfig.build.json","format":"prettier --write \"**/*.ts\"","lint":"eslint 'lib/**/*.ts' --fix","test:e2e":"jest --config ./tests/jest-e2e.json --runInBand","test:e2e:dev":"jest --config ./tests/jest-e2e.json --runInBand --watch","prerelease":"npm run build","release":"release-it"},"devDependencies":{"@commitlint/cli":"12.1.4","@commitlint/config-angular":"12.1.4","@nestjs/platform-express":"8.3.1","@nestjs/testing":"8.3.1","@types/jest":"27.4.0","@types/node":"17.0.8","@types/supertest":"2.0.11","@types/uuid":"8.3.4","husky":"6.0.0","lint-staged":"12.3.4","pg":"^8.11.3","prettier":"2.5.1","reflect-metadata":"0.1.13","rxjs":"7.5.4","slonik":"^34.1.0","supertest":"6.2.2","typescript":"5.2.2"},"dependencies":{"uuid":"8.3.2"},"peerDependencies":{"@nestjs/common":"^8.0.0 || ^9.0.0 || ^10.0.0","@nestjs/core":"^8.0.0 || ^9.0.0 || ^10.0.0","reflect-metadata":"^0.1.13","rxjs":"^7.2.0","slonik":">=27.0.0"},"lint-staged":{"*.ts":["prettier --write"]},"repository":{"type":"git","url":"git+https://github.com/mkorobkov/nestjs-slonik.git"},"types":"./index.d.ts","gitHead":"a3d759b0ba10ac29ae02fce6905e585d7b720604","bugs":{"url":"https://github.com/mkorobkov/nestjs-slonik/issues"},"homepage":"https://github.com/mkorobkov/nestjs-slonik#readme","_id":"@bumjs/nestjs-slonik@9.1.0","_nodeVersion":"16.14.0","_npmVersion":"8.3.1","dist":{"integrity":"sha512-A9c4s3EkmG2Kkbg6e8qSwkQTj0b/51ax90hZOGfYop/SgTBuObS9WWPvo3L+iYu4Qv+zileSF6+2mSagMsgyHQ==","shasum":"0e30d971837df54a8361fc159850ae6722e4715c","tarball":"https://registry.npmjs.org/@bumjs/nestjs-slonik/-/nestjs-slonik-9.1.0.tgz","fileCount":24,"unpackedSize":165700,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIC/E8CFwfhSRxg8tdrWtvJDzug/gk2xE2kP1yb+mUR7iAiEA8DOOrGRDU52fNqOTGaFxlNCSJNJvKYr3EAnzA29y56w="}]},"_npmUser":{"name":"padorang684","email":"padorang684@gmail.com"},"directories":{},"maintainers":[{"name":"padorang684","email":"padorang684@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-slonik_9.1.0_1698056670759_0.7777494299961989"},"_hasShrinkwrap":false},"9.2.0":{"name":"@bumjs/nestjs-slonik","version":"9.2.0","description":"Slonik module for NestJS","keywords":["nest","nestjs","slonik"],"main":"dist/index.js","type":"module","author":{"name":"Mikhail Korobkov"},"license":"MIT","url":"https://github.com/mkorobkov/nestjs-slonik#readme","scripts":{"build":"rm -rf dist && tsc -p tsconfig.build.json","format":"prettier --write \"**/*.ts\"","lint":"eslint 'lib/**/*.ts' --fix","test:e2e":"jest --config ./tests/jest-e2e.json --runInBand","test:e2e:dev":"jest --config ./tests/jest-e2e.json --runInBand --watch","prerelease":"npm run build","release":"release-it"},"devDependencies":{"@commitlint/cli":"12.1.4","@commitlint/config-angular":"12.1.4","@nestjs/platform-express":"8.3.1","@nestjs/testing":"8.3.1","@types/jest":"27.4.0","@types/node":"17.0.8","@types/supertest":"2.0.11","@types/uuid":"8.3.4","husky":"6.0.0","lint-staged":"12.3.4","pg":"^8.11.3","prettier":"2.5.1","reflect-metadata":"0.1.13","rxjs":"7.5.4","slonik":"^34.1.0","supertest":"6.2.2","typescript":"5.2.2"},"dependencies":{"uuid":"8.3.2"},"peerDependencies":{"@nestjs/common":"^8.0.0 || ^9.0.0 || ^10.0.0","@nestjs/core":"^8.0.0 || ^9.0.0 || ^10.0.0","reflect-metadata":"^0.1.13","rxjs":"^7.2.0","slonik":">=27.0.0"},"lint-staged":{"*.ts":["prettier --write"]},"repository":{"type":"git","url":"git+https://github.com/mkorobkov/nestjs-slonik.git"},"types":"./dist/index.d.ts","gitHead":"a3d759b0ba10ac29ae02fce6905e585d7b720604","bugs":{"url":"https://github.com/mkorobkov/nestjs-slonik/issues"},"homepage":"https://github.com/mkorobkov/nestjs-slonik#readme","_id":"@bumjs/nestjs-slonik@9.2.0","_nodeVersion":"16.14.0","_npmVersion":"8.3.1","dist":{"integrity":"sha512-XGixSY/Cqp3YjmDWN9AphV9W7Y4RR8tGIXx+idIZBXj7SNL1i7NHthrsSvD2fytsIBVb7gIFBP9Bwou9MdWiTw==","shasum":"739e40f1a764beb4bae8813b9064f35a33421c94","tarball":"https://registry.npmjs.org/@bumjs/nestjs-slonik/-/nestjs-slonik-9.2.0.tgz","fileCount":22,"unpackedSize":165502,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCAGHRC9zhTYsZbk6wL4aRgQBks9rkgXgZX7lV9XXhxJAIhAOkWcd/ELJtEzJ7ILf1O3JSvhDjzR6/WHzgxwH5MIndL"}]},"_npmUser":{"name":"padorang684","email":"padorang684@gmail.com"},"directories":{},"maintainers":[{"name":"padorang684","email":"padorang684@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-slonik_9.2.0_1698056757755_0.6118695270204852"},"_hasShrinkwrap":false},"9.3.0":{"name":"@bumjs/nestjs-slonik","version":"9.3.0","description":"Slonik module for NestJS","keywords":["nest","nestjs","slonik"],"type":"module","author":{"name":"Mikhail Korobkov"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","url":"https://github.com/mkorobkov/nestjs-slonik#readme","scripts":{"build":"rm -rf dist && tsc -p tsconfig.build.json","format":"prettier --write \"**/*.ts\"","lint":"eslint 'lib/**/*.ts' --fix","test:e2e":"jest --config ./tests/jest-e2e.json --runInBand","test:e2e:dev":"jest --config ./tests/jest-e2e.json --runInBand --watch","prerelease":"npm run build","release":"release-it"},"devDependencies":{},"dependencies":{},"peerDependencies":{"@nestjs/common":"^8.0.0 || ^9.0.0 || ^10.0.0","@nestjs/core":"^8.0.0 || ^9.0.0 || ^10.0.0","reflect-metadata":"^0.1.13","rxjs":"^7.2.0","slonik":">=27.0.0"},"repository":{"type":"git","url":"git+https://github.com/mkorobkov/nestjs-slonik.git"},"gitHead":"a3d759b0ba10ac29ae02fce6905e585d7b720604","bugs":{"url":"https://github.com/mkorobkov/nestjs-slonik/issues"},"homepage":"https://github.com/mkorobkov/nestjs-slonik#readme","_id":"@bumjs/nestjs-slonik@9.3.0","_nodeVersion":"16.14.0","_npmVersion":"8.3.1","dist":{"integrity":"sha512-IVuCbkm9tdT1thsI1xdo3BFBdCsKfhLHJ8yiEyAPjDzUqOEAke7xEZmYlqFmQDsczH4Qif/QGHNCpenuSOa6aA==","shasum":"6118a8738d118bb209dacb0e8c187d0473f47f5c","tarball":"https://registry.npmjs.org/@bumjs/nestjs-slonik/-/nestjs-slonik-9.3.0.tgz","fileCount":22,"unpackedSize":176718,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF+c/3c7Hp9jmy8EjBlxF5jYAXDk05NaHwjtwyLbgJsOAiA9lb8BtWWS5Q9ATKnuXTkbUSm/cAmG82VL/Pvlla5uBg=="}]},"_npmUser":{"name":"padorang684","email":"padorang684@gmail.com"},"directories":{},"maintainers":[{"name":"padorang684","email":"padorang684@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-slonik_9.3.0_1698064151715_0.7868845597386718"},"_hasShrinkwrap":false},"9.4.0":{"name":"@bumjs/nestjs-slonik","version":"9.4.0","description":"Slonik module for NestJS","keywords":["nest","nestjs","slonik"],"type":"module","author":{"name":"Mikhail Korobkov"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","url":"https://github.com/mkorobkov/nestjs-slonik#readme","scripts":{"build":"rm -rf dist && tsc -p tsconfig.build.json","format":"prettier --write \"**/*.ts\"","lint":"eslint 'lib/**/*.ts' --fix","test:e2e":"jest --config ./tests/jest-e2e.json --runInBand","test:e2e:dev":"jest --config ./tests/jest-e2e.json --runInBand --watch","prerelease":"npm run build","release":"release-it"},"devDependencies":{},"dependencies":{},"peerDependencies":{"@nestjs/common":"^8.0.0 || ^9.0.0 || ^10.0.0","@nestjs/core":"^8.0.0 || ^9.0.0 || ^10.0.0","reflect-metadata":"^0.1.13","rxjs":"^7.2.0","slonik":">=27.0.0"},"repository":{"type":"git","url":"git+https://github.com/mkorobkov/nestjs-slonik.git"},"gitHead":"4ca1582d39e39297f7680793e2a8d78a3124eb3f","bugs":{"url":"https://github.com/mkorobkov/nestjs-slonik/issues"},"homepage":"https://github.com/mkorobkov/nestjs-slonik#readme","_id":"@bumjs/nestjs-slonik@9.4.0","_nodeVersion":"16.14.0","_npmVersion":"8.3.1","dist":{"integrity":"sha512-DjlafrOP2z1ylca9jjTKc0g4hdlmjbLCgjx/yPwoCKY2I6kcXC7Q/V3Jf0SYxWaYr/uE1+PnGcOqXPSPfba5Ig==","shasum":"a09d99754f641a904b47e013377c5729f0af31f6","tarball":"https://registry.npmjs.org/@bumjs/nestjs-slonik/-/nestjs-slonik-9.4.0.tgz","fileCount":22,"unpackedSize":170304,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBmw0J4ueUCNawA1sLry2HmAIBQ9hu9zzBL7Oh/3blRpAiEAj7HU+BqRABa8ATEblGa6xh0hJEDHo1CKJrmxb+JvzXY="}]},"_npmUser":{"name":"padorang684","email":"padorang684@gmail.com"},"directories":{},"maintainers":[{"name":"padorang684","email":"padorang684@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-slonik_9.4.0_1698066382939_0.5922366833282797"},"_hasShrinkwrap":false}},"time":{"created":"2023-10-03T03:08:35.893Z","9.0.0":"2023-10-03T03:08:36.081Z","modified":"2023-10-23T13:06:23.366Z","9.1.0":"2023-10-23T10:24:31.074Z","9.2.0":"2023-10-23T10:25:57.988Z","9.3.0":"2023-10-23T12:29:11.958Z","9.4.0":"2023-10-23T13:06:23.170Z"},"maintainers":[{"name":"padorang684","email":"padorang684@gmail.com"}],"description":"Slonik module for NestJS","homepage":"https://github.com/mkorobkov/nestjs-slonik#readme","keywords":["nest","nestjs","slonik"],"repository":{"type":"git","url":"git+https://github.com/mkorobkov/nestjs-slonik.git"},"author":{"name":"Mikhail Korobkov"},"bugs":{"url":"https://github.com/mkorobkov/nestjs-slonik/issues"},"license":"MIT","readme":"<a name=\"nestjs-slonik\"></a>\n## Description\n\n[Slonik](https://github.com/gajus/slonik) module for [Nest 8/9](https://github.com/nestjs/nest)\n\nThis module is based on [@nestjs/typeorm](https://github.com/nestjs/typeorm) and [@nestjs/sequelize](https://github.com/nestjs/sequelize) modules.\n\nThis README is based on nest.js [documentation](https://github.com/nestjs/docs.nestjs.com/blob/ed5ebf0a4c19edfa74b48ff108ed48088c7f6a2c/content/techniques/sql.md).\n\n## Contents\n* [nestjs-slonik](#nestjs-slonik)\n    * [Installation](#installation)\n    * [Basic import](#basic-import)\n    * [Multiple databases](#multiple-databases)\n    * [Testing](#testing)\n    * [Async configuration](#async-configuration)\n    * [Routing queries to different connections](#routing-queries-to-different-connections)\n    * [License](#license)\n\n<a name=\"installation\"></a>\n### Installation\n\n###### npm\n\n```bash\nnpm i --save nestjs-slonik slonik\n```\n\n###### yarn\n\n```bash\nyarn add nestjs-slonik slonik\n```\n\n<a name=\"basic-import\"></a>\n### Basic import\n\nOnce the installation process is complete, we can import the `SlonikModule` into the root `AppModule`.\n\n\n> app.module.ts\n```typescript\nimport { Module } from '@nestjs/common';\nimport { SlonikModule } from 'nestjs-slonik';\n\n@Module({\n  imports: [\n    SlonikModule.forRoot({\n      connectionUri: 'postgres://root:root@localhost/test',\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\nThe `forRoot()` method supports configuration properties described below.\n\n<table>\n  <tr>\n    <td colspan='2'><b>Slonik options</b></td>\n  </tr>\n  <tr>\n    <td><code>connectionUri</code></td>\n    <td><a href='https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING'>Connection URI</a></td>\n  </tr>\n  <tr>\n    <td><code>clientConfiguration</code></td>\n    <td><a href='https://github.com/gajus/slonik#api'>Slonik configuration object</a></td>\n  </tr>\n  <tr>\n    <td colspan='2'><b>SlonikModule options</b></td>\n  </tr>\n  <tr>\n    <td><code>name</code></td>\n    <td>Connection pool name. Used to inject different db connections (default: <code>default</code>)</td>\n  </tr>\n  <tr>\n    <td><code>toRetry</code></td>\n    <td>Function that determines whether the module should attempt to connect upon failure\n<br><code>(err: any) => boolean</code>\n<br>err parameter is error that was thrown</td>\n  </tr>\n  <tr>\n    <td><code>verboseRetryLog</code></td>\n    <td>If <code>true</code>, will show verbose error messages on each connection retry (default: <code>false</code>)</td>\n  </tr>\n  <tr>\n    <td><code>retryAttempts</code></td>\n    <td>Number of attempts to connect to the database (default: <code>10</code>)</td>\n  </tr>\n  <tr>\n    <td><code>retryDelay</code></td>\n    <td>Delay between connection retry attempts (ms) (default: <code>3000</code>)</td>\n  </tr>\n</table>\n\nOnce this is done, the Slonik pool will be available to inject across the entire project (without needing to\nimport any modules), for example:\n\n> app.service.ts\n```typescript\nimport { Injectable } from '@nestjs/common';\nimport { InjectPool } from 'nestjs-slonik';\nimport { DatabasePool, sql } from 'slonik';\n\n@Injectable()\nexport class AppService {\n  constructor(@InjectPool() private pool: DatabasePool) {}\n\n  getHello(): Promise<string> {\n    return this.pool.oneFirst<string>(sql`SELECT 'Hello World!';`);\n  }\n}\n```\n\n<a name=\"multiple-databases\"></a>\n### Multiple databases\n\nSome projects require multiple database connections. This can also be achieved with this module.\nTo work with multiple pools, first create the pools. In this case, pool naming becomes **mandatory**.\n\n```typescript\n@Module({\n  imports: [\n    SlonikModule.forRoot({\n      connectionUri: 'postgres://user:pwd@user_db_host:5432/users',\n    }),\n    SlonikModule.forRoot({\n      name: 'albumsConnection',\n      connectionUri: 'postgres://user:pwd@album_db_host:5432/albums',\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n> **Notice** If you don't set the `name` for a pool, its name is set to `default`. Please note that you shouldn't\n> have multiple pools without a name, or with the same name, otherwise they will get overridden.\n\nNow you can inject the Slonik pool for a given pool name:\n\n```typescript\n@Injectable()\nexport class AlbumsService {\n  constructor(\n    @InjectPool()\n    private usersPool: DatabasePool,\n    @InjectPool('albumsConnection')\n    private albumsPool: DatabasePool,\n  ) {}\n}\n```\n\nIt's also possible to inject any Slonik pool instance to the providers:\n\n```typescript\nimport { DatabasePool } from 'slonik';\nimport { getPoolToken } from 'nestjs-slonik';\n\n@Module({\n  providers: [\n    {\n      provide: AlbumsService,\n      useFactory: (albumsConnection: DatabasePool) => {\n        return new AlbumsService(albumsConnection);\n      },\n      inject: [getPoolToken('albumsConnection')],\n    },\n  ],\n})\nexport class AlbumsModule {}\n```\n\n<a name=\"testing\"></a>\n### Testing\n\nWhen it comes to unit testing an application, we usually want to avoid making a database connection,\nkeeping our test suites independent and their execution process as fast as possible. But our classes might\ndepend on Slonik pool instance. How do we handle that? The solution is to create mock pool. In order to achieve\nthat, we set up [custom providers](https://docs.nestjs.com/fundamentals/custom-providers).\nEach registered pool is automatically represented by an `<poolName='default'>SlonikPool` token.\n\nThe `nestjs-slonik` package exposes the `getPoolToken()` function which returns a prepared token based on a given\npool name.\n\n```typescript\n@Module({\n  providers: [\n    UsersService,\n    {\n      provide: getPoolToken(),\n      useValue: mockPool,\n    },\n  ],\n})\nexport class UsersModule {}\n```\n\nNow a substitute `mockPool` will be used as the Slonik pool. Whenever any class asks for Slonik pool using\nan `@InjectPool()` decorator, Nest will use the registered `mockPool` object.\n\n#### How to create mockPool\n\n- https://github.com/gajus/slonik#mocking-slonik\n- https://github.com/oguimbal/pg-mem/wiki/Libraries-adapters#-slonik\n\n> example of pg-mem usage\n```typescript\nimport { newDb } from 'pg-mem';\n\nconst mockDb = newDb();\nmockDb.public.none(`create table users(id text);\n                    insert into users values ('john doe');`);\nconst mockPool = mockDb.adapters.createSlonik();\n```\n\nYou can read more about pg-mem [here](https://github.com/oguimbal/pg-mem#-usage)\n\n<a name=\"async-configuration\"></a>\n### Async configuration\n\nYou may want to pass your `SlonikModule` options asynchronously instead of statically.\nIn this case, use the `forRootAsync()` method, which provides several ways to deal with async configuration.\n\nOne approach is to use a factory function:\n\n```typescript\nSlonikModule.forRootAsync({\n  useFactory: () => ({\n    connectionUri: 'postgres://root:root@0.0.0.0:5432/test',\n  }),\n});\n```\n\nOur factory behaves like any other [asynchronous provider](https://docs.nestjs.com/fundamentals/async-providers)\n(e.g., it can be `async` and it's able to inject dependencies through `inject`).\n\n```typescript\nSlonikModule.forRootAsync({\n  imports: [ConfigModule],\n  useFactory: (configService: ConfigService) => ({\n    connectionUri: configService.get('DATABASE_URL'),\n    clientConfigurationInput: {\n      interceptors: [\n        createFieldNameTransformationInterceptor({ format: 'CAMEL_CASE' }),\n      ],\n    },\n  }),\n  inject: [ConfigService],\n});\n```\n\nAlternatively, you can use the `useClass` syntax:\n\n```typescript\nSlonikModule.forRootAsync({\n  useClass: SlonikConfigService,\n});\n```\n\nThe construction above will instantiate `SlonikConfigService` inside `SlonikModule` and use it to provide\nan options object by calling `createSlonikOptions()`. Note that this means that the `SlonikConfigService`\nhas to implement the `SlonikOptionsFactory` interface, as shown below:\n\n```typescript\n@Injectable()\nclass SlonikConfigService implements SlonikOptionsFactory {\n  createSlonikOptions(): SlonikModuleOptions {\n    return {\n      connectionUri: 'postgres://root:root@localhost/test',\n    };\n  }\n}\n```\n\nIn order to prevent the creation of `SlonikConfigService` inside `SlonikModule` and use a provider imported\nfrom a different module, you can use the `useExisting` syntax.\n\n```typescript\nSlonikModule.forRootAsync({\n  imports: [ConfigModule],\n  useExisting: ConfigService,\n});\n```\n\nThis construction works the same as `useClass` with one critical difference - `SlonikModule` will lookup\nimported modules to reuse an existing `ConfigService` instead of instantiating a new one.\n\n> Make sure that the `name` property is defined at the same level as the `useFactory`, `useClass`, or\n> `useValue` property. This will allow Nest to properly register the pool under the appropriate injection token.\n\n<a name=\"routing-queries-to-different-connections\"></a>\n### Routing queries to different connections\n\nRead more about this pattern here: [Slonik docs](https://github.com/gajus/slonik#routing-queries-to-different-connections)\n\n```typescript\n@Module({\n  imports: [\n    SlonikModule.forRoot({\n      name: 'slave',\n      connectionUri: 'postgres://slave',\n    }),\n    SlonikModule.forRootAsync({\n      inject: [getPoolToken('slave')],\n      useFactory: (slavePool: DatabasePool) => ({\n        connectionUri: 'postgres://master',\n        clientConfigurationInput: {\n          interceptors: [\n            {\n              beforePoolConnection: async (connectionContext) => {\n                if (connectionContext?.query?.sql?.includes('SELECT')) {\n                  return slavePool;\n                }\n              },\n            },\n          ],\n        },\n      }),\n    }),\n  ],\n  controllers: [AppController],\n})\nexport class AppModule {}\n```\n\nWe register `master` pool as the default pool, but use Slonik interceptors to return\n`slave` pool on SELECT queries.\n\n> app.controller.ts\n```typescript\nimport { Controller, Get } from '@nestjs/common';\nimport { InjectPool } from 'nestjs-slonik';\nimport { DatabasePool, sql } from 'slonik';\n\n@Controller()\nexport class AppController {\n  constructor(@InjectPool() private readonly pool: DatabasePool) {}\n\n  @Get()\n  async getHello() {\n    await this.pool.query(sql`UPDATE 1`);\n    return this.pool.oneFirst(sql`SELECT 1`);\n  }\n}\n```\n\nThe first query will use `postgres://master` connection.\n\nThe second query will use `postgres://slave` connection.\n\n<a name=\"license\"></a>\n## License\n\n[MIT license](LICENSE)\n","readmeFilename":"README.md"}