@hiliosai/sdk 0.1.20 → 0.1.22
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/dist/index.d.ts +23 -30
- package/dist/index.js +24 -18
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -156,14 +156,6 @@ declare abstract class AbstractDatasource<TContext = AppContext> implements Base
|
|
|
156
156
|
}): Promise<R>;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
interface PrismaClientLike {
|
|
160
|
-
$connect(): Promise<void>;
|
|
161
|
-
$disconnect(): Promise<void>;
|
|
162
|
-
$queryRaw: any;
|
|
163
|
-
$transaction: any;
|
|
164
|
-
$extends: any;
|
|
165
|
-
[key: string]: any;
|
|
166
|
-
}
|
|
167
159
|
interface SoftDeleteExtension {
|
|
168
160
|
softDelete: {
|
|
169
161
|
[model: string]: {
|
|
@@ -180,10 +172,25 @@ interface AuditTrailExtension {
|
|
|
180
172
|
};
|
|
181
173
|
}
|
|
182
174
|
interface TenantExtension {
|
|
183
|
-
$
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
175
|
+
$setTenant: (tenantId: string) => void;
|
|
176
|
+
$getCurrentTenant: () => string | null;
|
|
177
|
+
$clearTenant: () => void;
|
|
178
|
+
$enableBypassMode: () => void;
|
|
179
|
+
$disableBypassMode: () => void;
|
|
180
|
+
$enableStrictMode: () => void;
|
|
181
|
+
$disableStrictMode: () => void;
|
|
182
|
+
$withBypassMode: <T>(fn: () => Promise<T>) => Promise<T>;
|
|
183
|
+
$withTenant: <T>(tenantId: string, fn: () => Promise<T>) => Promise<T>;
|
|
184
|
+
$withSystemMode: <T>(fn: () => Promise<T>) => Promise<T>;
|
|
185
|
+
}
|
|
186
|
+
type PrismaClientWithTenant<T = any> = T & TenantExtension;
|
|
187
|
+
interface PrismaClientLike extends Partial<TenantExtension> {
|
|
188
|
+
$connect(): Promise<void>;
|
|
189
|
+
$disconnect(): Promise<void>;
|
|
190
|
+
$queryRaw: any;
|
|
191
|
+
$transaction: any;
|
|
192
|
+
$extends: any;
|
|
193
|
+
[key: string]: any;
|
|
187
194
|
}
|
|
188
195
|
declare global {
|
|
189
196
|
var __prisma: PrismaClientLike | undefined;
|
|
@@ -232,6 +239,8 @@ declare class PrismaDatasource<TPrismaClient extends PrismaClientLike = PrismaCl
|
|
|
232
239
|
protected applyExtensions(client: TPrismaClient): TPrismaClient;
|
|
233
240
|
/**
|
|
234
241
|
* Get extended client with all applied extensions
|
|
242
|
+
* Note: Since extensions are already applied in initializePrismaClient,
|
|
243
|
+
* this just returns the client directly to avoid double-applying extensions
|
|
235
244
|
*/
|
|
236
245
|
get extendedClient(): TPrismaClient;
|
|
237
246
|
/**
|
|
@@ -241,6 +250,7 @@ declare class PrismaDatasource<TPrismaClient extends PrismaClientLike = PrismaCl
|
|
|
241
250
|
/**
|
|
242
251
|
* Called automatically when context is injected
|
|
243
252
|
* Sets tenant context from service context if available
|
|
253
|
+
* Skips update if currently in bypass mode to avoid overriding admin operations
|
|
244
254
|
*/
|
|
245
255
|
private updateTenantFromContext;
|
|
246
256
|
/**
|
|
@@ -318,23 +328,6 @@ declare const softDeleteExtension: {
|
|
|
318
328
|
};
|
|
319
329
|
};
|
|
320
330
|
|
|
321
|
-
/**
|
|
322
|
-
* Multi-Tenant Extension for Prisma
|
|
323
|
-
* Automatically filters queries by tenant context for multi-tenant applications
|
|
324
|
-
*
|
|
325
|
-
* Usage:
|
|
326
|
-
* ```typescript
|
|
327
|
-
* import { createTenantExtension } from './extensions/tenant.extension';
|
|
328
|
-
*
|
|
329
|
-
* class MyPrismaDS extends PrismaDatasource<PrismaClient> {
|
|
330
|
-
* protected applyExtensions(client: PrismaClient) {
|
|
331
|
-
* return client.$extends(createTenantExtension());
|
|
332
|
-
* }
|
|
333
|
-
* }
|
|
334
|
-
* ```
|
|
335
|
-
*
|
|
336
|
-
* Requires models to have `tenantId String` field
|
|
337
|
-
*/
|
|
338
331
|
declare function createTenantExtension(): {
|
|
339
332
|
name: string;
|
|
340
333
|
client: {
|
|
@@ -909,4 +902,4 @@ declare function DatasourceMixin(datasourceConstructors?: DatasourceConstructorR
|
|
|
909
902
|
|
|
910
903
|
declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
|
|
911
904
|
|
|
912
|
-
export { AbstractDatasource, type ActionHandler, type ActionSchemaWithContext, type ActionWithPermissions, type AppContext, type AppMeta, type AuditTrailExtension, type BaseDatasource, type BaseSpec, CHANNELS, type CarouselItem, type ChannelSendOptions, ContextHelpersMiddleware, CreateHealthCheckMiddleware, DEFAULT_DATASOURCE_CACHE_TTL, type DatasourceConstructorRegistry, type DatasourceContext, type DatasourceInstanceRegistry, type DatasourceInstanceTypes, DatasourceMixin, type Env, HEALTH_CHECK_DEFAULTS, INTEGRATION_CHANNELS, IntegrationCapability, type IntegrationChannelName, type IntegrationConfig, type IntegrationMessageFailedPayload, type IntegrationMessageReceivedPayload, type IntegrationMessageSentPayload, IntegrationPlatform, type IntegrationRegisteredPayload, type IntegrationServiceConfig, type IntegrationServiceSchema, IntegrationStatus, type IntegrationUnregisteredPayload, MemoizeMixin, type MemoizeMixinOptions, type Message, type MessageAttachment, type MessageButton, type MessageContent, MessageContentType, type MessageDirection, type MessageParticipant, type MessageStatus, type MessageType, NAMESPACE, PERMISSIONS, type Permission, type PermissionHelpers, PermissionsMiddleware, type PlatformMessage, PrismaDatasource, REDIS_URL, ROLE_PERMISSIONS, type SendResult, type SendToChannelMethod, type ServiceActionsSchema, type ServiceConfig, type ServiceSchema, type ServiceWithDatasources, type SoftDeleteExtension, type Tenant, type TenantExtension, type User, UserRole, type WebhookEvent, createDatasourceMiddleware, createTenantExtension, defineIntegration, defineService, isDev, isProd, isTest, configs as moleculer, nodeEnv, omit, retryExtension, softDeleteExtension };
|
|
905
|
+
export { AbstractDatasource, type ActionHandler, type ActionSchemaWithContext, type ActionWithPermissions, type AppContext, type AppMeta, type AuditTrailExtension, type BaseDatasource, type BaseSpec, CHANNELS, type CarouselItem, type ChannelSendOptions, ContextHelpersMiddleware, CreateHealthCheckMiddleware, DEFAULT_DATASOURCE_CACHE_TTL, type DatasourceConstructorRegistry, type DatasourceContext, type DatasourceInstanceRegistry, type DatasourceInstanceTypes, DatasourceMixin, type Env, HEALTH_CHECK_DEFAULTS, INTEGRATION_CHANNELS, IntegrationCapability, type IntegrationChannelName, type IntegrationConfig, type IntegrationMessageFailedPayload, type IntegrationMessageReceivedPayload, type IntegrationMessageSentPayload, IntegrationPlatform, type IntegrationRegisteredPayload, type IntegrationServiceConfig, type IntegrationServiceSchema, IntegrationStatus, type IntegrationUnregisteredPayload, MemoizeMixin, type MemoizeMixinOptions, type Message, type MessageAttachment, type MessageButton, type MessageContent, MessageContentType, type MessageDirection, type MessageParticipant, type MessageStatus, type MessageType, NAMESPACE, PERMISSIONS, type Permission, type PermissionHelpers, PermissionsMiddleware, type PlatformMessage, type PrismaClientWithTenant, PrismaDatasource, REDIS_URL, ROLE_PERMISSIONS, type SendResult, type SendToChannelMethod, type ServiceActionsSchema, type ServiceConfig, type ServiceSchema, type ServiceWithDatasources, type SoftDeleteExtension, type Tenant, type TenantExtension, type User, UserRole, type WebhookEvent, createDatasourceMiddleware, createTenantExtension, defineIntegration, defineService, isDev, isProd, isTest, configs as moleculer, nodeEnv, omit, retryExtension, softDeleteExtension };
|
package/dist/index.js
CHANGED
|
@@ -1417,12 +1417,16 @@ var AbstractDatasource = class {
|
|
|
1417
1417
|
};
|
|
1418
1418
|
|
|
1419
1419
|
// src/datasources/extensions/tenant.extension.ts
|
|
1420
|
+
var globalTenantContext = {
|
|
1421
|
+
currentTenantId: null,
|
|
1422
|
+
bypassMode: false,
|
|
1423
|
+
strictMode: false
|
|
1424
|
+
};
|
|
1425
|
+
function isInBypassMode() {
|
|
1426
|
+
return globalTenantContext.bypassMode;
|
|
1427
|
+
}
|
|
1420
1428
|
function createTenantExtension() {
|
|
1421
|
-
const tenantContext =
|
|
1422
|
-
currentTenantId: null,
|
|
1423
|
-
bypassMode: false,
|
|
1424
|
-
strictMode: false
|
|
1425
|
-
};
|
|
1429
|
+
const tenantContext = globalTenantContext;
|
|
1426
1430
|
return {
|
|
1427
1431
|
name: "Tenant",
|
|
1428
1432
|
client: {
|
|
@@ -1621,9 +1625,11 @@ var PrismaDatasource = class extends AbstractDatasource {
|
|
|
1621
1625
|
}
|
|
1622
1626
|
/**
|
|
1623
1627
|
* Get extended client with all applied extensions
|
|
1628
|
+
* Note: Since extensions are already applied in initializePrismaClient,
|
|
1629
|
+
* this just returns the client directly to avoid double-applying extensions
|
|
1624
1630
|
*/
|
|
1625
1631
|
get extendedClient() {
|
|
1626
|
-
return this.
|
|
1632
|
+
return this.client;
|
|
1627
1633
|
}
|
|
1628
1634
|
/**
|
|
1629
1635
|
* Initialize datasource - called after broker injection
|
|
@@ -1634,8 +1640,12 @@ var PrismaDatasource = class extends AbstractDatasource {
|
|
|
1634
1640
|
/**
|
|
1635
1641
|
* Called automatically when context is injected
|
|
1636
1642
|
* Sets tenant context from service context if available
|
|
1643
|
+
* Skips update if currently in bypass mode to avoid overriding admin operations
|
|
1637
1644
|
*/
|
|
1638
1645
|
updateTenantFromContext() {
|
|
1646
|
+
if (isInBypassMode()) {
|
|
1647
|
+
return;
|
|
1648
|
+
}
|
|
1639
1649
|
const tenantId = this.context?.meta?.tenantId;
|
|
1640
1650
|
if (tenantId && typeof tenantId === "string") {
|
|
1641
1651
|
this.setTenantContext(tenantId);
|
|
@@ -1675,9 +1685,8 @@ var PrismaDatasource = class extends AbstractDatasource {
|
|
|
1675
1685
|
async healthCheck() {
|
|
1676
1686
|
try {
|
|
1677
1687
|
this.broker.logger.info("Running Prisma health check");
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
await client.$withSystemMode(async () => {
|
|
1688
|
+
if (this.client.$withSystemMode) {
|
|
1689
|
+
await this.client.$withSystemMode(async () => {
|
|
1681
1690
|
await this.client.$queryRaw`SELECT 1`;
|
|
1682
1691
|
});
|
|
1683
1692
|
} else {
|
|
@@ -1737,9 +1746,8 @@ var PrismaDatasource = class extends AbstractDatasource {
|
|
|
1737
1746
|
*/
|
|
1738
1747
|
setTenantContext(tenantId) {
|
|
1739
1748
|
this._client ?? (this._client = this.initializePrismaClient());
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
tenantClient.$setTenant(tenantId);
|
|
1749
|
+
if (this._client.$setTenant) {
|
|
1750
|
+
this._client.$setTenant(tenantId);
|
|
1743
1751
|
this.broker.logger.debug("Tenant context set:", { tenantId });
|
|
1744
1752
|
} else {
|
|
1745
1753
|
this.broker.logger.warn(
|
|
@@ -1754,9 +1762,8 @@ var PrismaDatasource = class extends AbstractDatasource {
|
|
|
1754
1762
|
if (!this._client) {
|
|
1755
1763
|
return null;
|
|
1756
1764
|
}
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
return tenantClient.$getCurrentTenant();
|
|
1765
|
+
if (this._client.$getCurrentTenant) {
|
|
1766
|
+
return this._client.$getCurrentTenant();
|
|
1760
1767
|
}
|
|
1761
1768
|
return null;
|
|
1762
1769
|
}
|
|
@@ -1772,9 +1779,8 @@ var PrismaDatasource = class extends AbstractDatasource {
|
|
|
1772
1779
|
if (previousTenant) {
|
|
1773
1780
|
this.setTenantContext(previousTenant);
|
|
1774
1781
|
} else {
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
tenantClient.$clearTenant();
|
|
1782
|
+
if (this.client.$clearTenant) {
|
|
1783
|
+
this.client.$clearTenant();
|
|
1778
1784
|
}
|
|
1779
1785
|
}
|
|
1780
1786
|
}
|