drizzle-multitenant 1.0.9 → 1.0.10
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 +129 -38
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,20 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./assets/banner.svg" alt="drizzle-multitenant" width="500" />
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<strong>Multi-tenancy toolkit for Drizzle ORM</strong>
|
|
7
|
+
</p>
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
<p align="center">
|
|
10
|
+
Schema isolation, tenant context propagation, and parallel migrations for PostgreSQL
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
<a href="https://www.npmjs.com/package/drizzle-multitenant"><img src="https://img.shields.io/npm/v/drizzle-multitenant.svg?style=flat-square&color=4A9A98" alt="npm version"></a>
|
|
15
|
+
<a href="https://www.npmjs.com/package/drizzle-multitenant"><img src="https://img.shields.io/npm/dm/drizzle-multitenant.svg?style=flat-square&color=3D5A80" alt="npm downloads"></a>
|
|
16
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-6AADAB.svg?style=flat-square" alt="License"></a>
|
|
17
|
+
<a href="https://mateusflorez.github.io/drizzle-multitenant/"><img src="https://img.shields.io/badge/docs-online-2B3E5C.svg?style=flat-square" alt="Documentation"></a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
<br />
|
|
8
21
|
|
|
9
22
|
## Features
|
|
10
23
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
| Feature | Description |
|
|
25
|
+
|---------|-------------|
|
|
26
|
+
| **Schema Isolation** | PostgreSQL schema-per-tenant with automatic LRU pool management |
|
|
27
|
+
| **Context Propagation** | AsyncLocalStorage-based tenant context across your entire stack |
|
|
28
|
+
| **Parallel Migrations** | Apply migrations to all tenants concurrently with progress tracking |
|
|
29
|
+
| **Cross-Schema Queries** | Type-safe joins between tenant and shared tables |
|
|
30
|
+
| **Connection Retry** | Automatic retry with exponential backoff for transient failures |
|
|
31
|
+
| **Framework Support** | First-class Express, Fastify, and NestJS integrations |
|
|
32
|
+
|
|
33
|
+
<br />
|
|
18
34
|
|
|
19
35
|
## Installation
|
|
20
36
|
|
|
@@ -22,8 +38,12 @@ Multi-tenancy toolkit for Drizzle ORM with schema isolation, tenant context, and
|
|
|
22
38
|
npm install drizzle-multitenant drizzle-orm pg
|
|
23
39
|
```
|
|
24
40
|
|
|
41
|
+
<br />
|
|
42
|
+
|
|
25
43
|
## Quick Start
|
|
26
44
|
|
|
45
|
+
### 1. Define your configuration
|
|
46
|
+
|
|
27
47
|
```typescript
|
|
28
48
|
// tenant.config.ts
|
|
29
49
|
import { defineConfig } from 'drizzle-multitenant';
|
|
@@ -39,6 +59,8 @@ export default defineConfig({
|
|
|
39
59
|
});
|
|
40
60
|
```
|
|
41
61
|
|
|
62
|
+
### 2. Create the tenant manager
|
|
63
|
+
|
|
42
64
|
```typescript
|
|
43
65
|
// app.ts
|
|
44
66
|
import { createTenantManager } from 'drizzle-multitenant';
|
|
@@ -46,51 +68,118 @@ import config from './tenant.config';
|
|
|
46
68
|
|
|
47
69
|
const tenants = createTenantManager(config);
|
|
48
70
|
|
|
49
|
-
//
|
|
50
|
-
const db = tenants.getDb('
|
|
71
|
+
// Type-safe database for each tenant
|
|
72
|
+
const db = tenants.getDb('acme');
|
|
51
73
|
const users = await db.select().from(schema.users);
|
|
52
|
-
|
|
53
|
-
// With retry and validation
|
|
54
|
-
const db = await tenants.getDbAsync('tenant-123');
|
|
55
74
|
```
|
|
56
75
|
|
|
57
|
-
|
|
76
|
+
<br />
|
|
77
|
+
|
|
78
|
+
## CLI Commands
|
|
58
79
|
|
|
59
80
|
```bash
|
|
60
|
-
npx drizzle-multitenant init
|
|
61
|
-
npx drizzle-multitenant generate --name=users
|
|
62
|
-
npx drizzle-multitenant migrate --all
|
|
63
|
-
npx drizzle-multitenant status
|
|
81
|
+
npx drizzle-multitenant init # Interactive setup wizard
|
|
82
|
+
npx drizzle-multitenant generate --name=users # Generate migration
|
|
83
|
+
npx drizzle-multitenant migrate --all # Apply to all tenants
|
|
84
|
+
npx drizzle-multitenant status # Check migration status
|
|
85
|
+
npx drizzle-multitenant tenant:create --id=acme # Create new tenant
|
|
64
86
|
```
|
|
65
87
|
|
|
88
|
+
<br />
|
|
89
|
+
|
|
66
90
|
## Framework Integrations
|
|
67
91
|
|
|
92
|
+
<details>
|
|
93
|
+
<summary><strong>Express</strong></summary>
|
|
94
|
+
|
|
68
95
|
```typescript
|
|
69
|
-
// Express
|
|
70
96
|
import { createExpressMiddleware } from 'drizzle-multitenant/express';
|
|
71
|
-
app.use(createExpressMiddleware({ manager: tenants, extractTenantId: (req) => req.headers['x-tenant-id'] }));
|
|
72
97
|
|
|
73
|
-
|
|
98
|
+
app.use(createExpressMiddleware({
|
|
99
|
+
manager: tenants,
|
|
100
|
+
extractTenantId: (req) => req.headers['x-tenant-id'] as string,
|
|
101
|
+
}));
|
|
102
|
+
|
|
103
|
+
app.get('/users', async (req, res) => {
|
|
104
|
+
const db = req.tenantContext.db;
|
|
105
|
+
const users = await db.select().from(schema.users);
|
|
106
|
+
res.json(users);
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
</details>
|
|
111
|
+
|
|
112
|
+
<details>
|
|
113
|
+
<summary><strong>Fastify</strong></summary>
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
74
116
|
import { fastifyTenantPlugin } from 'drizzle-multitenant/fastify';
|
|
75
|
-
fastify.register(fastifyTenantPlugin, { manager: tenants, extractTenantId: (req) => req.headers['x-tenant-id'] });
|
|
76
117
|
|
|
77
|
-
|
|
118
|
+
fastify.register(fastifyTenantPlugin, {
|
|
119
|
+
manager: tenants,
|
|
120
|
+
extractTenantId: (req) => req.headers['x-tenant-id'] as string,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
fastify.get('/users', async (req, reply) => {
|
|
124
|
+
const db = req.tenantContext.db;
|
|
125
|
+
const users = await db.select().from(schema.users);
|
|
126
|
+
return users;
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
</details>
|
|
131
|
+
|
|
132
|
+
<details>
|
|
133
|
+
<summary><strong>NestJS</strong></summary>
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
78
136
|
import { TenantModule, InjectTenantDb } from 'drizzle-multitenant/nestjs';
|
|
79
|
-
|
|
137
|
+
|
|
138
|
+
@Module({
|
|
139
|
+
imports: [
|
|
140
|
+
TenantModule.forRoot({
|
|
141
|
+
config,
|
|
142
|
+
extractTenantId: (req) => req.headers['x-tenant-id'],
|
|
143
|
+
}),
|
|
144
|
+
],
|
|
145
|
+
})
|
|
146
|
+
export class AppModule {}
|
|
147
|
+
|
|
148
|
+
@Injectable({ scope: Scope.REQUEST })
|
|
149
|
+
export class UserService {
|
|
150
|
+
constructor(@InjectTenantDb() private db: TenantDb) {}
|
|
151
|
+
|
|
152
|
+
findAll() {
|
|
153
|
+
return this.db.select().from(users);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
80
156
|
```
|
|
81
157
|
|
|
82
|
-
|
|
158
|
+
</details>
|
|
83
159
|
|
|
84
|
-
|
|
160
|
+
<br />
|
|
85
161
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
162
|
+
## Documentation
|
|
163
|
+
|
|
164
|
+
<table>
|
|
165
|
+
<tr>
|
|
166
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/getting-started">Getting Started</a></td>
|
|
167
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/configuration">Configuration</a></td>
|
|
168
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/cli">CLI Commands</a></td>
|
|
169
|
+
</tr>
|
|
170
|
+
<tr>
|
|
171
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/frameworks/express">Express</a></td>
|
|
172
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/frameworks/fastify">Fastify</a></td>
|
|
173
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/frameworks/nestjs">NestJS</a></td>
|
|
174
|
+
</tr>
|
|
175
|
+
<tr>
|
|
176
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/cross-schema">Cross-Schema Queries</a></td>
|
|
177
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/guide/advanced">Advanced Features</a></td>
|
|
178
|
+
<td><a href="https://mateusflorez.github.io/drizzle-multitenant/api/reference">API Reference</a></td>
|
|
179
|
+
</tr>
|
|
180
|
+
</table>
|
|
181
|
+
|
|
182
|
+
<br />
|
|
94
183
|
|
|
95
184
|
## Requirements
|
|
96
185
|
|
|
@@ -98,6 +187,8 @@ import { TenantModule, InjectTenantDb } from 'drizzle-multitenant/nestjs';
|
|
|
98
187
|
- PostgreSQL 12+
|
|
99
188
|
- Drizzle ORM 0.29+
|
|
100
189
|
|
|
190
|
+
<br />
|
|
191
|
+
|
|
101
192
|
## License
|
|
102
193
|
|
|
103
194
|
MIT
|
package/package.json
CHANGED