@mbc-cqrs-serverless/tenant 1.0.14 → 1.0.16
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 +198 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
# MBC CQRS
|
|
3
|
+
# MBC CQRS Serverless Framework - Tenant Package
|
|
4
4
|
|
|
5
5
|
## Description
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
The Tenant package provides multi-tenancy support for CQRS applications. It manages:
|
|
8
|
+
|
|
9
|
+
- **Tenant Management**: Create, update, and manage tenant entities
|
|
10
|
+
- **Tenant Groups**: Organize tenants into logical groups
|
|
11
|
+
- **Tenant Isolation**: Automatic data isolation between tenants
|
|
12
|
+
- **Tenant Settings**: Tenant-specific configuration management
|
|
8
13
|
|
|
9
14
|
## Installation
|
|
10
15
|
|
|
@@ -12,11 +17,201 @@ This package provides CRUD functionality and tenant-level isolation in a multi-t
|
|
|
12
17
|
npm install @mbc-cqrs-serverless/tenant
|
|
13
18
|
```
|
|
14
19
|
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Basic Setup
|
|
23
|
+
|
|
24
|
+
Import and configure the tenant module:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { TenantModule } from '@mbc-cqrs-serverless/tenant';
|
|
28
|
+
import { Module } from '@nestjs/common';
|
|
29
|
+
|
|
30
|
+
@Module({
|
|
31
|
+
imports: [
|
|
32
|
+
TenantModule.register({
|
|
33
|
+
tableName: 'your-tenant-table',
|
|
34
|
+
}),
|
|
35
|
+
],
|
|
36
|
+
})
|
|
37
|
+
export class AppModule {}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Tenant Operations
|
|
41
|
+
|
|
42
|
+
#### Create Tenant
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { TenantService } from '@mbc-cqrs-serverless/tenant';
|
|
46
|
+
|
|
47
|
+
@Injectable()
|
|
48
|
+
export class MyTenantService {
|
|
49
|
+
constructor(private readonly tenantService: TenantService) {}
|
|
50
|
+
|
|
51
|
+
async createTenant(data: CreateTenantDto) {
|
|
52
|
+
return this.tenantService.create({
|
|
53
|
+
code: 'TENANT001',
|
|
54
|
+
name: 'Acme Corporation',
|
|
55
|
+
description: 'Enterprise customer',
|
|
56
|
+
attributes: {
|
|
57
|
+
plan: 'enterprise',
|
|
58
|
+
maxUsers: 100,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### Get Tenant
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
async getTenant(tenantCode: string) {
|
|
69
|
+
return this.tenantService.findByCode(tenantCode);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async getAllTenants() {
|
|
73
|
+
return this.tenantService.findAll();
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
#### Update Tenant
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
async updateTenant(tenantCode: string, data: UpdateTenantDto) {
|
|
81
|
+
return this.tenantService.update(tenantCode, {
|
|
82
|
+
name: data.name,
|
|
83
|
+
attributes: data.attributes,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Delete Tenant
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
async deleteTenant(tenantCode: string) {
|
|
92
|
+
return this.tenantService.delete(tenantCode);
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Tenant Groups
|
|
97
|
+
|
|
98
|
+
Organize tenants into groups for easier management:
|
|
99
|
+
|
|
100
|
+
#### Create Tenant Group
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
async createTenantGroup(data: CreateGroupDto) {
|
|
104
|
+
return this.tenantService.createGroup({
|
|
105
|
+
code: 'ENTERPRISE',
|
|
106
|
+
name: 'Enterprise Customers',
|
|
107
|
+
tenantCodes: ['TENANT001', 'TENANT002'],
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### Add Tenant to Group
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
async addTenantToGroup(groupCode: string, tenantCode: string) {
|
|
116
|
+
return this.tenantService.addToGroup({
|
|
117
|
+
groupCode,
|
|
118
|
+
tenantCode,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Update Tenant Group
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
async updateTenantGroup(groupCode: string, data: UpdateGroupDto) {
|
|
127
|
+
return this.tenantService.updateGroup(groupCode, {
|
|
128
|
+
name: data.name,
|
|
129
|
+
tenantCodes: data.tenantCodes,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Tenant Context
|
|
135
|
+
|
|
136
|
+
Access current tenant in your services:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { TenantContext, InjectTenantContext } from '@mbc-cqrs-serverless/tenant';
|
|
140
|
+
|
|
141
|
+
@Injectable()
|
|
142
|
+
export class OrderService {
|
|
143
|
+
constructor(
|
|
144
|
+
@InjectTenantContext() private readonly tenantContext: TenantContext
|
|
145
|
+
) {}
|
|
146
|
+
|
|
147
|
+
async createOrder(data: CreateOrderDto) {
|
|
148
|
+
const tenantCode = this.tenantContext.getTenantCode();
|
|
149
|
+
// Order is automatically associated with current tenant
|
|
150
|
+
return this.orderRepository.create({
|
|
151
|
+
tenantCode,
|
|
152
|
+
...data,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Common Tenant
|
|
159
|
+
|
|
160
|
+
Create a common tenant for shared data:
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
async createCommonTenant() {
|
|
164
|
+
return this.tenantService.createCommonTenant({
|
|
165
|
+
name: 'Common',
|
|
166
|
+
description: 'Shared data across all tenants',
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## API Reference
|
|
172
|
+
|
|
173
|
+
### TenantService
|
|
174
|
+
|
|
175
|
+
| Method | Description |
|
|
176
|
+
|--------|-------------|
|
|
177
|
+
| `create(dto)` | Create new tenant |
|
|
178
|
+
| `update(code, dto)` | Update tenant |
|
|
179
|
+
| `delete(code)` | Delete tenant |
|
|
180
|
+
| `findByCode(code)` | Find tenant by code |
|
|
181
|
+
| `findAll()` | Get all tenants |
|
|
182
|
+
| `createGroup(dto)` | Create tenant group |
|
|
183
|
+
| `updateGroup(code, dto)` | Update tenant group |
|
|
184
|
+
| `addToGroup(dto)` | Add tenant to group |
|
|
185
|
+
| `removeFromGroup(dto)` | Remove tenant from group |
|
|
186
|
+
|
|
187
|
+
### TenantContext
|
|
188
|
+
|
|
189
|
+
| Method | Description |
|
|
190
|
+
|--------|-------------|
|
|
191
|
+
| `getTenantCode()` | Get current tenant code |
|
|
192
|
+
| `getTenant()` | Get current tenant entity |
|
|
193
|
+
| `isCommonTenant()` | Check if common tenant |
|
|
194
|
+
|
|
195
|
+
## Tenant Data Structure
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
interface Tenant {
|
|
199
|
+
pk: string; // Partition key
|
|
200
|
+
sk: string; // Sort key
|
|
201
|
+
code: string; // Unique tenant code
|
|
202
|
+
name: string; // Display name
|
|
203
|
+
description?: string; // Optional description
|
|
204
|
+
attributes?: Record<string, any>; // Custom attributes
|
|
205
|
+
createdAt: string; // Creation timestamp
|
|
206
|
+
updatedAt: string; // Last update timestamp
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
15
210
|
## Documentation
|
|
16
211
|
|
|
17
212
|
Visit https://mbc-cqrs-serverless.mbc-net.com/ to view the full documentation.
|
|
18
213
|
|
|
19
214
|
## License
|
|
20
215
|
|
|
21
|
-
Copyright © 2024, Murakami Business Consulting, Inc. https://www.mbc-net.com/
|
|
216
|
+
Copyright © 2024, Murakami Business Consulting, Inc. https://www.mbc-net.com/
|
|
22
217
|
This project and sub projects are under the MIT License.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mbc-cqrs-serverless/tenant",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "Multiple tenant management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mbc",
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@mbc-cqrs-serverless/core": "^0.1.48-beta.0"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "4ab967474160873735b8cb816272a7012b8940d5"
|
|
48
48
|
}
|