@quazex/nestjs-postgres 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +22 -0
- package/lib/postgres.decorators.d.ts +2 -0
- package/lib/postgres.decorators.js +7 -0
- package/lib/postgres.interfaces.d.ts +10 -0
- package/lib/postgres.interfaces.js +2 -0
- package/lib/postgres.module.d.ts +11 -0
- package/lib/postgres.module.js +64 -0
- package/lib/postgres.providers.d.ts +9 -0
- package/lib/postgres.providers.js +46 -0
- package/lib/postgres.tokens.d.ts +4 -0
- package/lib/postgres.tokens.js +8 -0
- package/lib/postgres.types.d.ts +2 -0
- package/lib/postgres.types.js +2 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 quazex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# NestJS Postgres Module
|
|
2
|
+
|
|
3
|
+
Core features:
|
|
4
|
+
|
|
5
|
+
- Based on [postgres library for NodeJS](https://github.com/porsager/postgres);
|
|
6
|
+
- Covered with unit and e2e tests;
|
|
7
|
+
- Basic module without unnecessary boilerplate.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @quazex/nestjs-postgres postgres
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Importing the Module
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { Module } from '@nestjs/common';
|
|
21
|
+
import { PostgresModule } from '@quazex/nestjs-postgres';
|
|
22
|
+
|
|
23
|
+
@Module({
|
|
24
|
+
imports: [
|
|
25
|
+
PostgresModule.forRoot({
|
|
26
|
+
host: 'localhost',
|
|
27
|
+
port: 5432,
|
|
28
|
+
user: 'test',
|
|
29
|
+
password: 'test',
|
|
30
|
+
database: 'test',
|
|
31
|
+
}),
|
|
32
|
+
],
|
|
33
|
+
})
|
|
34
|
+
export class AppModule {}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Injecting the Client
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { Injectable } from '@nestjs/common';
|
|
41
|
+
import { InjectPostgres } from '@quazex/nestjs-postgres';
|
|
42
|
+
import type { Sql } from 'postgres';
|
|
43
|
+
|
|
44
|
+
@Injectable()
|
|
45
|
+
export class DatabaseService {
|
|
46
|
+
constructor(@InjectPostgres() private readonly sql: Sql) {}
|
|
47
|
+
|
|
48
|
+
async insert(document: object) {
|
|
49
|
+
await this.sql`INSERT INTO table ${this.sql(document)}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async select(id: string) {
|
|
53
|
+
const [row] = await this.sql`SELECT * FROM table WHERE id = ${id}`;
|
|
54
|
+
return row;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Async Configuration
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { Module } from '@nestjs/common';
|
|
63
|
+
import { PostgresModule } from '@quazex/nestjs-postgres';
|
|
64
|
+
|
|
65
|
+
@Module({
|
|
66
|
+
imports: [
|
|
67
|
+
PostgresModule.forRootAsync({
|
|
68
|
+
useFactory: (config: SomeConfigProvider) => ({
|
|
69
|
+
host: config.PG_HOST,
|
|
70
|
+
port: config.PG_PORT,
|
|
71
|
+
user: config.PG_USER,
|
|
72
|
+
password: config.PG_PASSWORD,
|
|
73
|
+
database: config.PG_DATABASE,
|
|
74
|
+
}),
|
|
75
|
+
inject: [
|
|
76
|
+
SomeConfigProvider,
|
|
77
|
+
],
|
|
78
|
+
}),
|
|
79
|
+
],
|
|
80
|
+
})
|
|
81
|
+
export class AppModule {}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Connection and graceful shutdown
|
|
85
|
+
|
|
86
|
+
By default, this module doesn't manage the client connection on application shutdown. You can read more about lifecycle hooks on the NestJS [documentation page](https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown).
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// main.ts
|
|
90
|
+
const app = await NestFactory.create(AppModule);
|
|
91
|
+
|
|
92
|
+
app.enableShutdownHooks();
|
|
93
|
+
|
|
94
|
+
await app.listen(process.env.PORT ?? 3000);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// app.bootstrap.ts
|
|
99
|
+
import { Injectable, OnApplicationShutdown } from '@nestjs/common';
|
|
100
|
+
import { InjectPostgres } from '@quazex/nestjs-postgres';
|
|
101
|
+
import type { Sql } from 'postgres';
|
|
102
|
+
|
|
103
|
+
@Injectable()
|
|
104
|
+
export class AppBootstrap implements OnApplicationShutdown {
|
|
105
|
+
constructor(@InjectPostgres() private readonly sql: Sql) {}
|
|
106
|
+
|
|
107
|
+
public async onApplicationShutdown(): Promise<void> {
|
|
108
|
+
await this.sql.end();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./postgres.decorators"), exports);
|
|
18
|
+
__exportStar(require("./postgres.interfaces"), exports);
|
|
19
|
+
__exportStar(require("./postgres.module"), exports);
|
|
20
|
+
__exportStar(require("./postgres.providers"), exports);
|
|
21
|
+
__exportStar(require("./postgres.tokens"), exports);
|
|
22
|
+
__exportStar(require("./postgres.types"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectPostgres = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const postgres_tokens_1 = require("./postgres.tokens");
|
|
6
|
+
const InjectPostgres = () => ((0, common_1.Inject)(postgres_tokens_1.PostgresTokens.CLIENT));
|
|
7
|
+
exports.InjectPostgres = InjectPostgres;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { InjectionToken, ModuleMetadata, OptionalFactoryDependency, Type } from '@nestjs/common';
|
|
2
|
+
import { TPostgresOptions } from './postgres.types';
|
|
3
|
+
export interface TPostgresOptionsFactory {
|
|
4
|
+
createPostgresOptions(): Promise<TPostgresOptions> | TPostgresOptions;
|
|
5
|
+
}
|
|
6
|
+
export interface TPostgresAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
7
|
+
inject?: Array<InjectionToken | OptionalFactoryDependency>;
|
|
8
|
+
useExisting?: Type<TPostgresOptionsFactory>;
|
|
9
|
+
useFactory?: (...args: any[]) => Promise<TPostgresOptions> | TPostgresOptions;
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DynamicModule, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { Sql } from 'postgres';
|
|
3
|
+
import { TPostgresAsyncOptions } from './postgres.interfaces';
|
|
4
|
+
import { TPostgresOptions } from './postgres.types';
|
|
5
|
+
export declare class PostgresModule implements OnApplicationShutdown {
|
|
6
|
+
private readonly client;
|
|
7
|
+
constructor(client: Sql);
|
|
8
|
+
onApplicationShutdown(): Promise<void>;
|
|
9
|
+
static forRoot(options: TPostgresOptions): DynamicModule;
|
|
10
|
+
static forRootAsync(asyncOptions: TPostgresAsyncOptions): DynamicModule;
|
|
11
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var PostgresModule_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.PostgresModule = void 0;
|
|
17
|
+
const postgres_decorators_1 = require("./postgres.decorators");
|
|
18
|
+
const postgres_providers_1 = require("./postgres.providers");
|
|
19
|
+
let PostgresModule = PostgresModule_1 = class PostgresModule {
|
|
20
|
+
constructor(client) {
|
|
21
|
+
this.client = client;
|
|
22
|
+
}
|
|
23
|
+
async onApplicationShutdown() {
|
|
24
|
+
await this.client.end();
|
|
25
|
+
}
|
|
26
|
+
static forRoot(options) {
|
|
27
|
+
const optionsProvider = postgres_providers_1.PostgresProviders.getOptions(options);
|
|
28
|
+
const clientProvider = postgres_providers_1.PostgresProviders.getClient();
|
|
29
|
+
const dynamicModule = {
|
|
30
|
+
module: PostgresModule_1,
|
|
31
|
+
global: true,
|
|
32
|
+
providers: [
|
|
33
|
+
optionsProvider,
|
|
34
|
+
clientProvider,
|
|
35
|
+
],
|
|
36
|
+
exports: [
|
|
37
|
+
clientProvider,
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
return dynamicModule;
|
|
41
|
+
}
|
|
42
|
+
static forRootAsync(asyncOptions) {
|
|
43
|
+
const optionsProvider = postgres_providers_1.PostgresProviders.getAsyncOptions(asyncOptions);
|
|
44
|
+
const clientProvider = postgres_providers_1.PostgresProviders.getClient();
|
|
45
|
+
const dynamicModule = {
|
|
46
|
+
module: PostgresModule_1,
|
|
47
|
+
global: true,
|
|
48
|
+
imports: asyncOptions.imports,
|
|
49
|
+
providers: [
|
|
50
|
+
optionsProvider,
|
|
51
|
+
clientProvider,
|
|
52
|
+
],
|
|
53
|
+
exports: [
|
|
54
|
+
clientProvider,
|
|
55
|
+
],
|
|
56
|
+
};
|
|
57
|
+
return dynamicModule;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
exports.PostgresModule = PostgresModule;
|
|
61
|
+
exports.PostgresModule = PostgresModule = PostgresModule_1 = __decorate([
|
|
62
|
+
__param(0, (0, postgres_decorators_1.InjectPostgres)()),
|
|
63
|
+
__metadata("design:paramtypes", [Function])
|
|
64
|
+
], PostgresModule);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FactoryProvider, Provider, ValueProvider } from '@nestjs/common';
|
|
2
|
+
import { Sql } from 'postgres';
|
|
3
|
+
import { TPostgresAsyncOptions } from './postgres.interfaces';
|
|
4
|
+
import { TPostgresOptions } from './postgres.types';
|
|
5
|
+
export declare class PostgresProviders {
|
|
6
|
+
static getOptions(options: TPostgresOptions): ValueProvider<TPostgresOptions>;
|
|
7
|
+
static getAsyncOptions(options: TPostgresAsyncOptions): Provider<TPostgresOptions>;
|
|
8
|
+
static getClient(): FactoryProvider<Sql>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PostgresProviders = void 0;
|
|
7
|
+
const postgres_1 = __importDefault(require("postgres"));
|
|
8
|
+
const postgres_tokens_1 = require("./postgres.tokens");
|
|
9
|
+
class PostgresProviders {
|
|
10
|
+
static getOptions(options) {
|
|
11
|
+
return {
|
|
12
|
+
provide: postgres_tokens_1.PostgresTokens.OPTIONS,
|
|
13
|
+
useValue: options,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
static getAsyncOptions(options) {
|
|
17
|
+
if (options.useFactory) {
|
|
18
|
+
return {
|
|
19
|
+
provide: postgres_tokens_1.PostgresTokens.OPTIONS,
|
|
20
|
+
useFactory: options.useFactory,
|
|
21
|
+
inject: options.inject,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (options.useExisting) {
|
|
25
|
+
return {
|
|
26
|
+
provide: postgres_tokens_1.PostgresTokens.OPTIONS,
|
|
27
|
+
useFactory: async (factory) => {
|
|
28
|
+
const client = await factory.createPostgresOptions();
|
|
29
|
+
return client;
|
|
30
|
+
},
|
|
31
|
+
inject: [options.useExisting],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
throw new Error('Must provide useFactory or useClass');
|
|
35
|
+
}
|
|
36
|
+
static getClient() {
|
|
37
|
+
return {
|
|
38
|
+
provide: postgres_tokens_1.PostgresTokens.CLIENT,
|
|
39
|
+
useFactory: (config) => (0, postgres_1.default)(config),
|
|
40
|
+
inject: [
|
|
41
|
+
postgres_tokens_1.PostgresTokens.OPTIONS,
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.PostgresProviders = PostgresProviders;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PostgresTokens = void 0;
|
|
4
|
+
var PostgresTokens;
|
|
5
|
+
(function (PostgresTokens) {
|
|
6
|
+
PostgresTokens["OPTIONS"] = "postgres_module_options";
|
|
7
|
+
PostgresTokens["CLIENT"] = "postgres_module_client";
|
|
8
|
+
})(PostgresTokens || (exports.PostgresTokens = PostgresTokens = {}));
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quazex/nestjs-postgres",
|
|
3
|
+
"repository": {
|
|
4
|
+
"type": "git",
|
|
5
|
+
"url": "git://github.com/quazex/nestjs-postgres.git"
|
|
6
|
+
},
|
|
7
|
+
"description": "NestJS module for Postgres library",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"author": "Alexander Smirnov",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">= 24.x"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"main": "lib/index.js",
|
|
18
|
+
"types": "lib/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"lib/**"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"postgres",
|
|
24
|
+
"postgresql",
|
|
25
|
+
"nestjs",
|
|
26
|
+
"nest",
|
|
27
|
+
"typescript",
|
|
28
|
+
"nodejs",
|
|
29
|
+
"node"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build:clear": "rimraf lib",
|
|
33
|
+
"build:compile": "npm run build:clear && tsc --project tsconfig.build.json",
|
|
34
|
+
"dev:watch": "tsc --watch",
|
|
35
|
+
"dev:check": "tsc --noEmit",
|
|
36
|
+
"dev:lint": "eslint",
|
|
37
|
+
"dev:format": "npm run dev:lint -- --fix",
|
|
38
|
+
"dev:test": "jest",
|
|
39
|
+
"prepare": "husky"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@nestjs/common": ">=9.0.0 <12.0.0",
|
|
43
|
+
"postgres": ">=3.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@faker-js/faker": "9.8.0",
|
|
47
|
+
"@jest/globals": "29.7.0",
|
|
48
|
+
"@nestjs/common": "11.1.17",
|
|
49
|
+
"@nestjs/testing": "11.1.17",
|
|
50
|
+
"@quazex/eslint-config": "1.1.2",
|
|
51
|
+
"@testcontainers/postgresql": "11.13.0",
|
|
52
|
+
"husky": "9.1.7",
|
|
53
|
+
"jest": "29.7.0",
|
|
54
|
+
"postgres": "3.4.8",
|
|
55
|
+
"rimraf": "6.0.1",
|
|
56
|
+
"testcontainers": "11.13.0",
|
|
57
|
+
"ts-jest": "29.4.0",
|
|
58
|
+
"ts-node": "10.9.2",
|
|
59
|
+
"typescript": "5.9.3"
|
|
60
|
+
}
|
|
61
|
+
}
|