@hiliosai/sdk 0.1.19 → 0.1.20

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
@@ -226,7 +226,8 @@ declare class PrismaDatasource<TPrismaClient extends PrismaClientLike = PrismaCl
226
226
  protected createClient(): TPrismaClient;
227
227
  /**
228
228
  * Apply extensions to the Prisma client
229
- * Override this method to add production extensions like soft delete, audit trails, etc.
229
+ * By default, applies tenant extension for multi-tenant support
230
+ * Override this method to add additional extensions like soft delete, audit trails, etc.
230
231
  */
231
232
  protected applyExtensions(client: TPrismaClient): TPrismaClient;
232
233
  /**
@@ -340,6 +341,13 @@ declare function createTenantExtension(): {
340
341
  $setTenant(tenantId: string): void;
341
342
  $getCurrentTenant(): string | null;
342
343
  $clearTenant(): void;
344
+ $enableBypassMode(): void;
345
+ $disableBypassMode(): void;
346
+ $enableStrictMode(): void;
347
+ $disableStrictMode(): void;
348
+ $withBypassMode<T>(fn: () => Promise<T>): Promise<T>;
349
+ $withTenant<T>(tenantId: string, fn: () => Promise<T>): Promise<T>;
350
+ $withSystemMode<T>(fn: () => Promise<T>): Promise<T>;
343
351
  };
344
352
  query: {
345
353
  $allModels: {
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,14 @@ 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
+ const client = this.client;
1679
+ if (client.$withSystemMode) {
1680
+ await client.$withSystemMode(async () => {
1681
+ await this.client.$queryRaw`SELECT 1`;
1682
+ });
1683
+ } else {
1684
+ await this.client.$queryRaw`SELECT 1`;
1685
+ }
1526
1686
  this.broker.logger.info("Prisma health check passed");
1527
1687
  return true;
1528
1688
  } catch (error) {
@@ -1703,86 +1863,6 @@ var softDeleteExtension = {
1703
1863
  }
1704
1864
  };
1705
1865
 
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
1866
  // src/datasources/extensions/retry.extension.ts
1787
1867
  var retryExtension = {
1788
1868
  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.20",
4
4
  "type": "module",
5
5
  "license": "UNLICENSED",
6
6
  "main": "./dist/index.js",