@dismissible/nestjs-postgres-storage 0.0.2-canary.738340d.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/README.md ADDED
@@ -0,0 +1,211 @@
1
+ # @dismissible/nestjs-postgres-storage
2
+
3
+ PostgreSQL storage adapter for the Dismissible system using Prisma.
4
+
5
+ > **Part of the Dismissible API** - This library is part of the [Dismissible API](https://dismissible.io) ecosystem. Visit [dismissible.io](https://dismissible.io) for more information and documentation.
6
+
7
+ ## Overview
8
+
9
+ This library provides a production-ready PostgreSQL storage adapter for the Dismissible system. It uses Prisma for database access and includes:
10
+
11
+ - Persistent storage of dismissible items
12
+ - Automatic database migrations via Prisma
13
+ - Connection pooling and lifecycle management
14
+ - Full TypeScript support
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @dismissible/nestjs-postgres-storage
20
+ ```
21
+
22
+ You'll also need to install the peer dependencies:
23
+
24
+ ```bash
25
+ npm install @dismissible/nestjs-storage @dismissible/nestjs-dismissible-item @dismissible/nestjs-logger prisma
26
+ ```
27
+
28
+ ## Prerequisites
29
+
30
+ - PostgreSQL database (version 12 or higher)
31
+ - Node.js 18 or higher
32
+
33
+ ## Getting Started
34
+
35
+ ### 1. Database Setup
36
+
37
+ First, set up your PostgreSQL database connection string:
38
+
39
+ ```env
40
+ DISMISSIBLE_POSTGRES_STORAGE_CONNECTION_STRING=postgresql://user:password@localhost:5432/dismissible
41
+ ```
42
+
43
+ ### 2. Database Migration
44
+
45
+ Run Prisma migrations to set up the database schema:
46
+
47
+ ```bash
48
+ # Generate Prisma client
49
+ npx prisma generate --schema=node_modules/@dismissible/nestjs-postgres-storage/prisma/schema.prisma
50
+
51
+ # Push schema to database (for development)
52
+ npx prisma db push --schema=node_modules/@dismissible/nestjs-postgres-storage/prisma/schema.prisma
53
+
54
+ # Or run migrations (for production)
55
+ npx prisma migrate dev --schema=node_modules/@dismissible/nestjs-postgres-storage/prisma/schema.prisma
56
+ ```
57
+
58
+ Alternatively, you can use the provided CLI tool:
59
+
60
+ ```bash
61
+ npx dismissible-prisma generate
62
+ npx dismissible-prisma migrate dev
63
+ ```
64
+
65
+ ### 3. Module Configuration
66
+
67
+ Import and configure the module in your NestJS application:
68
+
69
+ ```typescript
70
+ import { Module } from '@nestjs/common';
71
+ import { DismissibleModule } from '@dismissible/nestjs-dismissible';
72
+ import { PostgresStorageModule } from '@dismissible/nestjs-postgres-storage';
73
+ import { LoggerModule } from '@dismissible/nestjs-logger';
74
+
75
+ @Module({
76
+ imports: [
77
+ LoggerModule.forRoot({}),
78
+ PostgresStorageModule.forRoot({
79
+ connectionString: process.env.DISMISSIBLE_POSTGRES_STORAGE_CONNECTION_STRING!,
80
+ }),
81
+ DismissibleModule.forRoot({
82
+ storage: PostgresStorageModule,
83
+ }),
84
+ ],
85
+ })
86
+ export class AppModule {}
87
+ ```
88
+
89
+ ### 4. Async Configuration
90
+
91
+ You can also configure the module asynchronously:
92
+
93
+ ```typescript
94
+ import { Module } from '@nestjs/common';
95
+ import { ConfigModule, ConfigService } from '@nestjs/config';
96
+ import { PostgresStorageModule } from '@dismissible/nestjs-postgres-storage';
97
+
98
+ @Module({
99
+ imports: [
100
+ ConfigModule.forRoot(),
101
+ PostgresStorageModule.forRootAsync({
102
+ imports: [ConfigModule],
103
+ useFactory: (config: ConfigService) => ({
104
+ connectionString: config.get<string>('DISMISSIBLE_POSTGRES_STORAGE_CONNECTION_STRING')!,
105
+ }),
106
+ inject: [ConfigService],
107
+ }),
108
+ ],
109
+ })
110
+ export class AppModule {}
111
+ ```
112
+
113
+ ## Database Schema
114
+
115
+ The library uses Prisma to manage the database schema. The schema includes:
116
+
117
+ - `DismissibleItem` table with fields:
118
+ - `id` (String, primary key)
119
+ - `userId` (String, indexed)
120
+ - `createdAt` (DateTime)
121
+ - `dismissedAt` (DateTime, nullable)
122
+ - `metadata` (Json, nullable)
123
+
124
+ ## API Reference
125
+
126
+ ### PostgresStorageModule
127
+
128
+ #### `PostgresStorageModule.forRoot(options)`
129
+
130
+ Configures the PostgreSQL storage module synchronously.
131
+
132
+ **Options:**
133
+
134
+ - `connectionString: string` - PostgreSQL connection string
135
+
136
+ **Returns:** `DynamicModule`
137
+
138
+ #### `PostgresStorageModule.forRootAsync(options)`
139
+
140
+ Configures the PostgreSQL storage module asynchronously.
141
+
142
+ **Options:**
143
+
144
+ - `imports?: any[]` - Modules to import
145
+ - `useFactory: (deps) => PostgresStorageModuleOptions` - Factory function
146
+ - `inject?: any[]` - Dependencies to inject into the factory
147
+
148
+ **Returns:** `DynamicModule`
149
+
150
+ ### PostgresStorageAdapter
151
+
152
+ The adapter implements `IDismissibleStorage` and provides:
153
+
154
+ - `get(userId, itemId)` - Retrieve an item
155
+ - `create(item)` - Create a new item
156
+ - `update(item)` - Update an existing item
157
+
158
+ ### PrismaService
159
+
160
+ The service manages the Prisma client lifecycle:
161
+
162
+ - Automatically connects on module initialization
163
+ - Automatically disconnects on module destruction
164
+ - Uses connection pooling via `pg` adapter
165
+
166
+ ## CLI Tool
167
+
168
+ The package includes a CLI tool for Prisma operations:
169
+
170
+ ```bash
171
+ # Generate Prisma client
172
+ npx dismissible-prisma generate
173
+
174
+ # Run migrations
175
+ npx dismissible-prisma migrate dev
176
+
177
+ # Push schema (development)
178
+ npx dismissible-prisma db push
179
+
180
+ # View database
181
+ npx dismissible-prisma studio
182
+ ```
183
+
184
+ ## Environment Variables
185
+
186
+ - `DISMISSIBLE_POSTGRES_STORAGE_CONNECTION_STRING` - PostgreSQL connection string (required)
187
+
188
+ ## Production Considerations
189
+
190
+ 1. **Connection Pooling**: The adapter uses `pg` connection pooling. Configure your pool size based on your application's needs.
191
+
192
+ 2. **Migrations**: Always use Prisma migrations in production:
193
+
194
+ ```bash
195
+ npx prisma migrate deploy --schema=node_modules/@dismissible/nestjs-postgres-storage/prisma/schema.prisma
196
+ ```
197
+
198
+ 3. **Monitoring**: Monitor database connections and query performance.
199
+
200
+ 4. **Backups**: Ensure regular database backups are in place.
201
+
202
+ ## Related Packages
203
+
204
+ - `@dismissible/nestjs-dismissible` - Main dismissible service
205
+ - `@dismissible/nestjs-storage` - Storage interface
206
+ - `@dismissible/nestjs-dismissible-item` - Data models
207
+ - `@dismissible/nestjs-logger` - Logging
208
+
209
+ ## License
210
+
211
+ MIT
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * CLI helper for @dismissible/nestjs-postgres-storage
5
+ * Runs Prisma commands with the correct schema path.
6
+ *
7
+ * Usage:
8
+ * npx dismissible-prisma generate
9
+ * npx dismissible-prisma migrate dev
10
+ * npx dismissible-prisma migrate deploy
11
+ * npx dismissible-prisma db push
12
+ * npx dismissible-prisma studio
13
+ */
14
+
15
+ const { execSync } = require('child_process');
16
+ const path = require('path');
17
+
18
+ const schemaPath = path.join(__dirname, '..', 'prisma', 'schema.prisma');
19
+ const args = process.argv.slice(2);
20
+
21
+ if (args.length === 0) {
22
+ console.log(`
23
+ @dismissible/nestjs-postgres-storage - Prisma CLI Helper
24
+
25
+ Usage:
26
+ npx dismissible-prisma <command> [options]
27
+
28
+ Commands:
29
+ generate Generate Prisma Client
30
+ migrate dev Create and apply migrations (development)
31
+ migrate deploy Apply pending migrations (production)
32
+ migrate reset Reset database and apply all migrations
33
+ db push Push schema to database without migrations
34
+ db pull Pull schema from database
35
+ studio Open Prisma Studio
36
+
37
+ Examples:
38
+ npx dismissible-prisma generate
39
+ npx dismissible-prisma migrate dev --name init
40
+ npx dismissible-prisma db push
41
+
42
+ Schema location: ${schemaPath}
43
+ `);
44
+ process.exit(0);
45
+ }
46
+
47
+ const command = `npx prisma ${args.join(' ')} --schema="${schemaPath}"`;
48
+
49
+ console.log(`Running: ${command}\n`);
50
+
51
+ try {
52
+ execSync(command, { stdio: 'inherit' });
53
+ } catch (error) {
54
+ process.exit(error.status || 1);
55
+ }
package/jest.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ export default {
2
+ displayName: 'postgres-storage',
3
+ preset: '../../jest.preset.js',
4
+ testEnvironment: 'node',
5
+ transform: {
6
+ '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.json' }],
7
+ },
8
+ moduleFileExtensions: ['ts', 'js', 'html'],
9
+ coverageDirectory: '../../coverage/libs/postgres-storage',
10
+ };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@dismissible/nestjs-postgres-storage",
3
+ "version": "0.0.2-canary.738340d.0",
4
+ "description": "PostgreSQL storage adapter for Dismissible using Prisma",
5
+ "main": "./src/index.js",
6
+ "types": "./src/index.d.ts",
7
+ "bin": {
8
+ "dismissible-prisma": "./bin/dismissible-prisma.js"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "import": "./src/index.mjs",
13
+ "require": "./src/index.js",
14
+ "types": "./src/index.d.ts"
15
+ },
16
+ "./prisma/schema.prisma": "./prisma/schema.prisma"
17
+ },
18
+ "prisma": {
19
+ "schema": "prisma/schema.prisma"
20
+ },
21
+ "peerDependencies": {
22
+ "@nestjs/common": "^11.0.0",
23
+ "@dismissible/nestjs-storage": "^0.0.2-canary.738340d.0",
24
+ "@dismissible/nestjs-dismissible-item": "^0.0.2-canary.738340d.0",
25
+ "@dismissible/nestjs-logger": "^0.0.2-canary.738340d.0",
26
+ "prisma": "^7.0.0"
27
+ },
28
+ "peerDependenciesMeta": {
29
+ "@nestjs/common": {
30
+ "optional": false
31
+ },
32
+ "@dismissible/nestjs-storage": {
33
+ "optional": false
34
+ },
35
+ "@dismissible/nestjs-dismissible-item": {
36
+ "optional": false
37
+ },
38
+ "@dismissible/nestjs-logger": {
39
+ "optional": false
40
+ },
41
+ "prisma": {
42
+ "optional": true
43
+ }
44
+ },
45
+ "dependencies": {
46
+ "@prisma/client": "^7.0.0",
47
+ "@prisma/adapter-pg": "^7.0.0",
48
+ "pg": "^8.13.0"
49
+ },
50
+ "devDependencies": {
51
+ "@types/pg": "^8.11.10"
52
+ },
53
+ "keywords": [
54
+ "nestjs",
55
+ "dismissible",
56
+ "storage",
57
+ "postgres",
58
+ "prisma",
59
+ "adapter"
60
+ ],
61
+ "author": "",
62
+ "license": "MIT",
63
+ "repository": {
64
+ "type": "git",
65
+ "url": ""
66
+ }
67
+ }
@@ -0,0 +1,24 @@
1
+
2
+ /* !!! This is code generated by Prisma. Do not edit directly. !!! */
3
+ /* eslint-disable */
4
+ // biome-ignore-all lint: generated file
5
+ // @ts-nocheck
6
+ /*
7
+ * This file should be your main import to use Prisma-related types and utilities in a browser.
8
+ * Use it to get access to models, enums, and input types.
9
+ *
10
+ * This file does not contain a `PrismaClient` class, nor several other helpers that are intended as server-side only.
11
+ * See `client.ts` for the standard, server-side entry point.
12
+ *
13
+ * 🟢 You can import this file directly.
14
+ */
15
+
16
+ import * as Prisma from './internal/prismaNamespaceBrowser'
17
+ export { Prisma }
18
+ export * as $Enums from './enums'
19
+ export * from './enums';
20
+ /**
21
+ * Model DismissibleItem
22
+ *
23
+ */
24
+ export type DismissibleItem = Prisma.DismissibleItemModel
@@ -0,0 +1,44 @@
1
+
2
+ /* !!! This is code generated by Prisma. Do not edit directly. !!! */
3
+ /* eslint-disable */
4
+ // biome-ignore-all lint: generated file
5
+ // @ts-nocheck
6
+ /*
7
+ * This file should be your main import to use Prisma. Through it you get access to all the models, enums, and input types.
8
+ * If you're looking for something you can import in the client-side of your application, please refer to the `browser.ts` file instead.
9
+ *
10
+ * 🟢 You can import this file directly.
11
+ */
12
+
13
+ import * as process from 'node:process'
14
+ import * as path from 'node:path'
15
+
16
+ import * as runtime from "@prisma/client/runtime/client"
17
+ import * as $Enums from "./enums"
18
+ import * as $Class from "./internal/class"
19
+ import * as Prisma from "./internal/prismaNamespace"
20
+
21
+ export * as $Enums from './enums'
22
+ export * from "./enums"
23
+ /**
24
+ * ## Prisma Client
25
+ *
26
+ * Type-safe database client for TypeScript
27
+ * @example
28
+ * ```
29
+ * const prisma = new PrismaClient()
30
+ * // Fetch zero or more DismissibleItems
31
+ * const dismissibleItems = await prisma.dismissibleItem.findMany()
32
+ * ```
33
+ *
34
+ * Read more in our [docs](https://pris.ly/d/client).
35
+ */
36
+ export const PrismaClient = $Class.getPrismaClientClass()
37
+ export type PrismaClient<LogOpts extends Prisma.LogLevel = never, OmitOpts extends Prisma.PrismaClientOptions["omit"] = Prisma.PrismaClientOptions["omit"], ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = $Class.PrismaClient<LogOpts, OmitOpts, ExtArgs>
38
+ export { Prisma }
39
+
40
+ /**
41
+ * Model DismissibleItem
42
+ *
43
+ */
44
+ export type DismissibleItem = Prisma.DismissibleItemModel