@hiliosai/sdk 0.1.19 → 0.1.21

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 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
- $tenant: {
184
- setContext: (tenantId: string) => void;
185
- getCurrentTenant: () => string | null;
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;
@@ -226,7 +233,8 @@ declare class PrismaDatasource<TPrismaClient extends PrismaClientLike = PrismaCl
226
233
  protected createClient(): TPrismaClient;
227
234
  /**
228
235
  * Apply extensions to the Prisma client
229
- * Override this method to add production extensions like soft delete, audit trails, etc.
236
+ * By default, applies tenant extension for multi-tenant support
237
+ * Override this method to add additional extensions like soft delete, audit trails, etc.
230
238
  */
231
239
  protected applyExtensions(client: TPrismaClient): TPrismaClient;
232
240
  /**
@@ -340,6 +348,13 @@ declare function createTenantExtension(): {
340
348
  $setTenant(tenantId: string): void;
341
349
  $getCurrentTenant(): string | null;
342
350
  $clearTenant(): void;
351
+ $enableBypassMode(): void;
352
+ $disableBypassMode(): void;
353
+ $enableStrictMode(): void;
354
+ $disableStrictMode(): void;
355
+ $withBypassMode<T>(fn: () => Promise<T>): Promise<T>;
356
+ $withTenant<T>(tenantId: string, fn: () => Promise<T>): Promise<T>;
357
+ $withSystemMode<T>(fn: () => Promise<T>): Promise<T>;
343
358
  };
344
359
  query: {
345
360
  $allModels: {
@@ -901,4 +916,4 @@ declare function DatasourceMixin(datasourceConstructors?: DatasourceConstructorR
901
916
 
902
917
  declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
903
918
 
904
- 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 };
919
+ 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
@@ -1416,6 +1416,154 @@ var AbstractDatasource = class {
1416
1416
  }
1417
1417
  };
1418
1418
 
1419
+ // src/datasources/extensions/tenant.extension.ts
1420
+ function createTenantExtension() {
1421
+ const tenantContext = {
1422
+ currentTenantId: null,
1423
+ bypassMode: false,
1424
+ strictMode: false
1425
+ };
1426
+ return {
1427
+ name: "Tenant",
1428
+ client: {
1429
+ // Set tenant context
1430
+ $setTenant(tenantId) {
1431
+ tenantContext.currentTenantId = tenantId;
1432
+ tenantContext.bypassMode = false;
1433
+ },
1434
+ // Get current tenant
1435
+ $getCurrentTenant() {
1436
+ return tenantContext.currentTenantId;
1437
+ },
1438
+ // Clear tenant context
1439
+ $clearTenant() {
1440
+ tenantContext.currentTenantId = null;
1441
+ tenantContext.bypassMode = false;
1442
+ },
1443
+ // Enable bypass mode (admin operations)
1444
+ $enableBypassMode() {
1445
+ tenantContext.bypassMode = true;
1446
+ },
1447
+ // Disable bypass mode
1448
+ $disableBypassMode() {
1449
+ tenantContext.bypassMode = false;
1450
+ },
1451
+ // Enable strict mode (requires tenantId for operations)
1452
+ $enableStrictMode() {
1453
+ tenantContext.strictMode = true;
1454
+ },
1455
+ // Disable strict mode
1456
+ $disableStrictMode() {
1457
+ tenantContext.strictMode = false;
1458
+ },
1459
+ // Execute with bypass mode temporarily
1460
+ async $withBypassMode(fn) {
1461
+ const previousMode = tenantContext.bypassMode;
1462
+ tenantContext.bypassMode = true;
1463
+ try {
1464
+ return await fn();
1465
+ } finally {
1466
+ tenantContext.bypassMode = previousMode;
1467
+ }
1468
+ },
1469
+ // Execute with specific tenant temporarily
1470
+ async $withTenant(tenantId, fn) {
1471
+ const previousTenant = tenantContext.currentTenantId;
1472
+ const previousBypass = tenantContext.bypassMode;
1473
+ tenantContext.currentTenantId = tenantId;
1474
+ tenantContext.bypassMode = false;
1475
+ try {
1476
+ return await fn();
1477
+ } finally {
1478
+ tenantContext.currentTenantId = previousTenant;
1479
+ tenantContext.bypassMode = previousBypass;
1480
+ }
1481
+ },
1482
+ // Execute without tenant filtering (system operations)
1483
+ async $withSystemMode(fn) {
1484
+ const previousTenant = tenantContext.currentTenantId;
1485
+ const previousBypass = tenantContext.bypassMode;
1486
+ tenantContext.currentTenantId = null;
1487
+ tenantContext.bypassMode = true;
1488
+ try {
1489
+ return await fn();
1490
+ } finally {
1491
+ tenantContext.currentTenantId = previousTenant;
1492
+ tenantContext.bypassMode = previousBypass;
1493
+ }
1494
+ }
1495
+ },
1496
+ query: {
1497
+ $allModels: {
1498
+ // Automatically add tenantId filter to all read operations
1499
+ async findMany({ args, query }) {
1500
+ var _a;
1501
+ if (!tenantContext.bypassMode && tenantContext.currentTenantId) {
1502
+ args.where ?? (args.where = {});
1503
+ (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1504
+ } else if (tenantContext.strictMode && !tenantContext.bypassMode && !tenantContext.currentTenantId) {
1505
+ throw new Error("Tenant context required for this operation. Use $withSystemMode() for system-level operations.");
1506
+ }
1507
+ return query(args);
1508
+ },
1509
+ async findUnique({ args, query }) {
1510
+ var _a;
1511
+ if (!tenantContext.bypassMode && tenantContext.currentTenantId) {
1512
+ args.where ?? (args.where = {});
1513
+ (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1514
+ } else if (tenantContext.strictMode && !tenantContext.bypassMode && !tenantContext.currentTenantId) {
1515
+ throw new Error("Tenant context required for this operation. Use $withSystemMode() for system-level operations.");
1516
+ }
1517
+ return query(args);
1518
+ },
1519
+ async findFirst({ args, query }) {
1520
+ var _a;
1521
+ if (!tenantContext.bypassMode && tenantContext.currentTenantId) {
1522
+ args.where ?? (args.where = {});
1523
+ (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1524
+ } else if (tenantContext.strictMode && !tenantContext.bypassMode && !tenantContext.currentTenantId) {
1525
+ throw new Error("Tenant context required for this operation. Use $withSystemMode() for system-level operations.");
1526
+ }
1527
+ return query(args);
1528
+ },
1529
+ // Automatically add tenantId to create operations
1530
+ async create({ args, query }) {
1531
+ var _a;
1532
+ if (!tenantContext.bypassMode && tenantContext.currentTenantId) {
1533
+ args.data ?? (args.data = {});
1534
+ (_a = args.data).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1535
+ } else if (tenantContext.strictMode && !tenantContext.bypassMode && !tenantContext.currentTenantId) {
1536
+ throw new Error("Tenant context required for this operation. Use $withSystemMode() for system-level operations.");
1537
+ }
1538
+ return query(args);
1539
+ },
1540
+ // Add tenantId filter to update operations
1541
+ async update({ args, query }) {
1542
+ var _a;
1543
+ if (!tenantContext.bypassMode && tenantContext.currentTenantId) {
1544
+ args.where ?? (args.where = {});
1545
+ (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1546
+ } else if (tenantContext.strictMode && !tenantContext.bypassMode && !tenantContext.currentTenantId) {
1547
+ throw new Error("Tenant context required for this operation. Use $withSystemMode() for system-level operations.");
1548
+ }
1549
+ return query(args);
1550
+ },
1551
+ // Add tenantId filter to delete operations
1552
+ async delete({ args, query }) {
1553
+ var _a;
1554
+ if (!tenantContext.bypassMode && tenantContext.currentTenantId) {
1555
+ args.where ?? (args.where = {});
1556
+ (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1557
+ } else if (tenantContext.strictMode && !tenantContext.bypassMode && !tenantContext.currentTenantId) {
1558
+ throw new Error("Tenant context required for this operation. Use $withSystemMode() for system-level operations.");
1559
+ }
1560
+ return query(args);
1561
+ }
1562
+ }
1563
+ }
1564
+ };
1565
+ }
1566
+
1419
1567
  // src/datasources/prisma.datasource.ts
1420
1568
  var PrismaDatasource = class extends AbstractDatasource {
1421
1569
  constructor(prismaClient) {
@@ -1461,10 +1609,15 @@ var PrismaDatasource = class extends AbstractDatasource {
1461
1609
  }
1462
1610
  /**
1463
1611
  * Apply extensions to the Prisma client
1464
- * Override this method to add production extensions like soft delete, audit trails, etc.
1612
+ * By default, applies tenant extension for multi-tenant support
1613
+ * Override this method to add additional extensions like soft delete, audit trails, etc.
1465
1614
  */
1466
1615
  applyExtensions(client) {
1467
- return client;
1616
+ let extended = client;
1617
+ if (typeof client.$extends === "function") {
1618
+ extended = extended.$extends(createTenantExtension());
1619
+ }
1620
+ return extended;
1468
1621
  }
1469
1622
  /**
1470
1623
  * Get extended client with all applied extensions
@@ -1522,7 +1675,13 @@ var PrismaDatasource = class extends AbstractDatasource {
1522
1675
  async healthCheck() {
1523
1676
  try {
1524
1677
  this.broker.logger.info("Running Prisma health check");
1525
- await this.client.$queryRaw`SELECT 1`;
1678
+ if (this.client.$withSystemMode) {
1679
+ await this.client.$withSystemMode(async () => {
1680
+ await this.client.$queryRaw`SELECT 1`;
1681
+ });
1682
+ } else {
1683
+ await this.client.$queryRaw`SELECT 1`;
1684
+ }
1526
1685
  this.broker.logger.info("Prisma health check passed");
1527
1686
  return true;
1528
1687
  } catch (error) {
@@ -1577,9 +1736,8 @@ var PrismaDatasource = class extends AbstractDatasource {
1577
1736
  */
1578
1737
  setTenantContext(tenantId) {
1579
1738
  this._client ?? (this._client = this.initializePrismaClient());
1580
- const tenantClient = this._client;
1581
- if (tenantClient.$setTenant) {
1582
- tenantClient.$setTenant(tenantId);
1739
+ if (this._client.$setTenant) {
1740
+ this._client.$setTenant(tenantId);
1583
1741
  this.broker.logger.debug("Tenant context set:", { tenantId });
1584
1742
  } else {
1585
1743
  this.broker.logger.warn(
@@ -1594,9 +1752,8 @@ var PrismaDatasource = class extends AbstractDatasource {
1594
1752
  if (!this._client) {
1595
1753
  return null;
1596
1754
  }
1597
- const tenantClient = this._client;
1598
- if (tenantClient.$getCurrentTenant) {
1599
- return tenantClient.$getCurrentTenant();
1755
+ if (this._client.$getCurrentTenant) {
1756
+ return this._client.$getCurrentTenant();
1600
1757
  }
1601
1758
  return null;
1602
1759
  }
@@ -1612,9 +1769,8 @@ var PrismaDatasource = class extends AbstractDatasource {
1612
1769
  if (previousTenant) {
1613
1770
  this.setTenantContext(previousTenant);
1614
1771
  } else {
1615
- const tenantClient = this.client;
1616
- if (tenantClient.$clearTenant) {
1617
- tenantClient.$clearTenant();
1772
+ if (this.client.$clearTenant) {
1773
+ this.client.$clearTenant();
1618
1774
  }
1619
1775
  }
1620
1776
  }
@@ -1703,86 +1859,6 @@ var softDeleteExtension = {
1703
1859
  }
1704
1860
  };
1705
1861
 
1706
- // src/datasources/extensions/tenant.extension.ts
1707
- function createTenantExtension() {
1708
- const tenantContext = {
1709
- currentTenantId: null
1710
- };
1711
- return {
1712
- name: "Tenant",
1713
- client: {
1714
- // Set tenant context
1715
- $setTenant(tenantId) {
1716
- tenantContext.currentTenantId = tenantId;
1717
- },
1718
- // Get current tenant
1719
- $getCurrentTenant() {
1720
- return tenantContext.currentTenantId;
1721
- },
1722
- // Clear tenant context
1723
- $clearTenant() {
1724
- tenantContext.currentTenantId = null;
1725
- }
1726
- },
1727
- query: {
1728
- $allModels: {
1729
- // Automatically add tenantId filter to all read operations
1730
- async findMany({ args, query }) {
1731
- var _a;
1732
- if (tenantContext.currentTenantId) {
1733
- args.where ?? (args.where = {});
1734
- (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1735
- }
1736
- return query(args);
1737
- },
1738
- async findUnique({ args, query }) {
1739
- var _a;
1740
- if (tenantContext.currentTenantId) {
1741
- args.where ?? (args.where = {});
1742
- (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1743
- }
1744
- return query(args);
1745
- },
1746
- async findFirst({ args, query }) {
1747
- var _a;
1748
- if (tenantContext.currentTenantId) {
1749
- args.where ?? (args.where = {});
1750
- (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1751
- }
1752
- return query(args);
1753
- },
1754
- // Automatically add tenantId to create operations
1755
- async create({ args, query }) {
1756
- var _a;
1757
- if (tenantContext.currentTenantId) {
1758
- args.data ?? (args.data = {});
1759
- (_a = args.data).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1760
- }
1761
- return query(args);
1762
- },
1763
- // Add tenantId filter to update operations
1764
- async update({ args, query }) {
1765
- var _a;
1766
- if (tenantContext.currentTenantId) {
1767
- args.where ?? (args.where = {});
1768
- (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1769
- }
1770
- return query(args);
1771
- },
1772
- // Add tenantId filter to delete operations
1773
- async delete({ args, query }) {
1774
- var _a;
1775
- if (tenantContext.currentTenantId) {
1776
- args.where ?? (args.where = {});
1777
- (_a = args.where).tenantId ?? (_a.tenantId = tenantContext.currentTenantId);
1778
- }
1779
- return query(args);
1780
- }
1781
- }
1782
- }
1783
- };
1784
- }
1785
-
1786
1862
  // src/datasources/extensions/retry.extension.ts
1787
1863
  var retryExtension = {
1788
1864
  name: "Retry",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hiliosai/sdk",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "license": "UNLICENSED",
6
6
  "main": "./dist/index.js",