@hiliosai/sdk 0.1.11 → 0.1.12
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/package.json +2 -6
- package/src/configs/moleculer/index.ts +15 -2
- package/src/datasources/extensions/soft-delete.extension.ts +1 -0
- package/src/datasources/extensions/tenant.extension.ts +20 -19
- package/src/index.ts +1 -0
- package/src/service/define-service.ts +2 -5
- package/src/utils/index.ts +8 -0
- package/tsup.config.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hiliosai/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
@@ -11,19 +11,15 @@
|
|
|
11
11
|
"typecheck": "tsc --noEmit"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@keyv/redis": "5.1.5",
|
|
15
14
|
"@ltv/env": "4.0.3",
|
|
16
15
|
"@moleculer/channels": "0.2.0",
|
|
17
|
-
"keyv": "5.5.5",
|
|
18
|
-
"lodash": "4.17.21",
|
|
19
16
|
"moleculer": "0.14.35"
|
|
20
17
|
},
|
|
21
18
|
"devDependencies": {
|
|
22
19
|
"@hiliosai/prettier": "workspace:*",
|
|
23
20
|
"@hiliosai/typescript": "workspace:*",
|
|
24
21
|
"@pkg/dev-utils": "workspace:*",
|
|
25
|
-
"@types/lodash": "4.17.21",
|
|
26
22
|
"bun-types": "latest"
|
|
27
23
|
},
|
|
28
24
|
"prettier": "@hiliosai/prettier"
|
|
29
|
-
}
|
|
25
|
+
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import env from '@ltv/env';
|
|
2
|
-
import type {BrokerOptions} from 'moleculer';
|
|
2
|
+
import type {BrokerOptions, Middleware} from 'moleculer';
|
|
3
3
|
import os from 'os';
|
|
4
4
|
|
|
5
|
+
import {
|
|
6
|
+
ContextHelpersMiddleware,
|
|
7
|
+
PermissionsMiddleware,
|
|
8
|
+
} from '../../middlewares';
|
|
5
9
|
import {bulkheadConfig} from './bulkhead';
|
|
6
10
|
import {ChannelsMiddleware} from './channels';
|
|
7
11
|
import {circuitBreakerConfig} from './circuit-breaker';
|
|
@@ -79,7 +83,16 @@ const configs: BrokerOptions = {
|
|
|
79
83
|
// Enable built-in tracing function. More info: https://moleculer.services/docs/0.14/tracing.html
|
|
80
84
|
tracing: tracingConfig,
|
|
81
85
|
|
|
82
|
-
middlewares: [
|
|
86
|
+
middlewares: [
|
|
87
|
+
ChannelsMiddleware,
|
|
88
|
+
PermissionsMiddleware as Middleware,
|
|
89
|
+
ContextHelpersMiddleware as Middleware,
|
|
90
|
+
],
|
|
83
91
|
};
|
|
84
92
|
|
|
93
|
+
export const createConfig = (customConfig: BrokerOptions) => ({
|
|
94
|
+
...configs,
|
|
95
|
+
...customConfig,
|
|
96
|
+
});
|
|
97
|
+
|
|
85
98
|
export default configs;
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1
2
|
/**
|
|
2
3
|
* Multi-Tenant Extension for Prisma
|
|
3
4
|
* Automatically filters queries by tenant context for multi-tenant applications
|
|
4
|
-
*
|
|
5
|
+
*
|
|
5
6
|
* Usage:
|
|
6
7
|
* ```typescript
|
|
7
8
|
* import { createTenantExtension } from './extensions/tenant.extension';
|
|
8
|
-
*
|
|
9
|
+
*
|
|
9
10
|
* class MyPrismaDS extends PrismaDatasource<PrismaClient> {
|
|
10
11
|
* protected applyExtensions(client: PrismaClient) {
|
|
11
12
|
* return client.$extends(createTenantExtension());
|
|
12
13
|
* }
|
|
13
14
|
* }
|
|
14
15
|
* ```
|
|
15
|
-
*
|
|
16
|
+
*
|
|
16
17
|
* Requires models to have `tenantId String` field
|
|
17
18
|
*/
|
|
18
19
|
|
|
@@ -27,71 +28,71 @@ export function createTenantExtension() {
|
|
|
27
28
|
|
|
28
29
|
return {
|
|
29
30
|
name: 'Tenant',
|
|
30
|
-
|
|
31
|
+
|
|
31
32
|
client: {
|
|
32
33
|
// Set tenant context
|
|
33
34
|
$setTenant(tenantId: string) {
|
|
34
35
|
tenantContext.currentTenantId = tenantId;
|
|
35
36
|
},
|
|
36
|
-
|
|
37
|
+
|
|
37
38
|
// Get current tenant
|
|
38
39
|
$getCurrentTenant() {
|
|
39
40
|
return tenantContext.currentTenantId;
|
|
40
41
|
},
|
|
41
|
-
|
|
42
|
+
|
|
42
43
|
// Clear tenant context
|
|
43
44
|
$clearTenant() {
|
|
44
45
|
tenantContext.currentTenantId = null;
|
|
45
46
|
},
|
|
46
47
|
},
|
|
47
|
-
|
|
48
|
+
|
|
48
49
|
query: {
|
|
49
50
|
$allModels: {
|
|
50
51
|
// Automatically add tenantId filter to all read operations
|
|
51
|
-
async findMany({
|
|
52
|
+
async findMany({args, query}: any) {
|
|
52
53
|
if (tenantContext.currentTenantId) {
|
|
53
54
|
args.where ??= {};
|
|
54
55
|
args.where.tenantId ??= tenantContext.currentTenantId;
|
|
55
56
|
}
|
|
56
57
|
return query(args);
|
|
57
58
|
},
|
|
58
|
-
|
|
59
|
-
async findUnique({
|
|
59
|
+
|
|
60
|
+
async findUnique({args, query}: any) {
|
|
60
61
|
if (tenantContext.currentTenantId) {
|
|
61
62
|
args.where ??= {};
|
|
62
63
|
args.where.tenantId ??= tenantContext.currentTenantId;
|
|
63
64
|
}
|
|
64
65
|
return query(args);
|
|
65
66
|
},
|
|
66
|
-
|
|
67
|
-
async findFirst({
|
|
67
|
+
|
|
68
|
+
async findFirst({args, query}: any) {
|
|
68
69
|
if (tenantContext.currentTenantId) {
|
|
69
70
|
args.where ??= {};
|
|
70
71
|
args.where.tenantId ??= tenantContext.currentTenantId;
|
|
71
72
|
}
|
|
72
73
|
return query(args);
|
|
73
74
|
},
|
|
74
|
-
|
|
75
|
+
|
|
75
76
|
// Automatically add tenantId to create operations
|
|
76
|
-
async create({
|
|
77
|
+
async create({args, query}: any) {
|
|
77
78
|
if (tenantContext.currentTenantId) {
|
|
78
79
|
args.data ??= {};
|
|
79
80
|
args.data.tenantId ??= tenantContext.currentTenantId;
|
|
80
81
|
}
|
|
81
82
|
return query(args);
|
|
82
83
|
},
|
|
83
|
-
|
|
84
|
+
|
|
84
85
|
// Add tenantId filter to update operations
|
|
85
|
-
async update({
|
|
86
|
+
async update({args, query}: any) {
|
|
86
87
|
if (tenantContext.currentTenantId) {
|
|
87
88
|
args.where ??= {};
|
|
88
89
|
args.where.tenantId ??= tenantContext.currentTenantId;
|
|
89
90
|
}
|
|
90
91
|
return query(args);
|
|
91
92
|
},
|
|
92
|
-
|
|
93
|
+
|
|
93
94
|
// Add tenantId filter to delete operations
|
|
94
|
-
async delete({
|
|
95
|
+
async delete({args, query}: any) {
|
|
95
96
|
if (tenantContext.currentTenantId) {
|
|
96
97
|
args.where ??= {};
|
|
97
98
|
args.where.tenantId ??= tenantContext.currentTenantId;
|
|
@@ -101,4 +102,4 @@ export function createTenantExtension() {
|
|
|
101
102
|
},
|
|
102
103
|
},
|
|
103
104
|
};
|
|
104
|
-
}
|
|
105
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import omit from 'lodash/omit';
|
|
2
1
|
import type {ServiceSchema as MoleculerServiceSchema} from 'moleculer';
|
|
3
2
|
|
|
4
3
|
import {MemoizeMixin} from '../middlewares';
|
|
5
4
|
import type {DatasourceConstructorRegistry} from '../middlewares/datasource.middleware';
|
|
6
5
|
import {DatasourceMixin} from '../mixins';
|
|
7
6
|
import type {ServiceConfig} from '../types/service';
|
|
7
|
+
import {omit} from '../utils';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Define a service
|
|
@@ -47,13 +47,10 @@ export function defineService<
|
|
|
47
47
|
propsToOmit
|
|
48
48
|
) as unknown as MoleculerServiceSchema<TSettings>;
|
|
49
49
|
|
|
50
|
-
const datasources = config.datasources ?? {};
|
|
51
|
-
|
|
52
|
-
// TODO: Add mixins config support
|
|
53
50
|
return {
|
|
54
51
|
...serviceSchema,
|
|
55
52
|
mixins: [
|
|
56
|
-
DatasourceMixin(datasources),
|
|
53
|
+
DatasourceMixin(config.datasources),
|
|
57
54
|
MemoizeMixin(),
|
|
58
55
|
...(serviceSchema.mixins ?? []),
|
|
59
56
|
],
|
package/tsup.config.ts
CHANGED