@vritti/api-sdk 0.0.3 → 0.0.5
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 +113 -0
- package/dist/index.cjs +786 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +579 -7
- package/dist/index.d.ts +579 -7
- package/dist/index.js +775 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -334,16 +334,15 @@ var PrimaryDatabaseService = class _PrimaryDatabaseService {
|
|
|
334
334
|
this.logger.log(`Cleared ${size} cached tenant configs`);
|
|
335
335
|
}
|
|
336
336
|
/**
|
|
337
|
-
* Get
|
|
338
|
-
*
|
|
339
|
-
* This is useful for platform admin operations (creating tenants, billing, etc.)
|
|
337
|
+
* Get the Prisma client for the primary database.
|
|
338
|
+
* This is a synchronous property that returns the initialized Prisma client.
|
|
340
339
|
*
|
|
341
340
|
* @returns Primary database client instance
|
|
342
341
|
* @throws Error if primary database client is not initialized
|
|
343
342
|
*/
|
|
344
|
-
|
|
343
|
+
get prismaClient() {
|
|
345
344
|
if (!this.primaryDbClient) {
|
|
346
|
-
throw new Error("Primary database client not initialized
|
|
345
|
+
throw new Error("Primary database client not initialized");
|
|
347
346
|
}
|
|
348
347
|
return this.primaryDbClient;
|
|
349
348
|
}
|
|
@@ -929,6 +928,17 @@ var TenantDatabaseService = class _TenantDatabaseService {
|
|
|
929
928
|
this.startConnectionCleaner();
|
|
930
929
|
}
|
|
931
930
|
/**
|
|
931
|
+
* Get the Prisma client for the current tenant's database.
|
|
932
|
+
* This returns the tenant-scoped database client.
|
|
933
|
+
*
|
|
934
|
+
* @returns Tenant-scoped database client instance
|
|
935
|
+
* @throws UnauthorizedException if tenant context not set
|
|
936
|
+
* @throws InternalServerErrorException if connection fails
|
|
937
|
+
*/
|
|
938
|
+
get prismaClient() {
|
|
939
|
+
return this.getDbClient();
|
|
940
|
+
}
|
|
941
|
+
/**
|
|
932
942
|
* Get tenant-scoped database client for the current request/message
|
|
933
943
|
*
|
|
934
944
|
* This method:
|
|
@@ -1122,7 +1132,7 @@ var DatabaseModule = class _DatabaseModule {
|
|
|
1122
1132
|
* })
|
|
1123
1133
|
*/
|
|
1124
1134
|
static forServer(options) {
|
|
1125
|
-
return this.createDynamicModule(options, "
|
|
1135
|
+
return this.createDynamicModule(options, "server");
|
|
1126
1136
|
}
|
|
1127
1137
|
/**
|
|
1128
1138
|
* Configure DatabaseModule for Microservice/Messaging mode (RabbitMQ workers)
|
|
@@ -1165,7 +1175,7 @@ var DatabaseModule = class _DatabaseModule {
|
|
|
1165
1175
|
PrimaryDatabaseService,
|
|
1166
1176
|
TenantDatabaseService
|
|
1167
1177
|
];
|
|
1168
|
-
if (mode === "
|
|
1178
|
+
if (mode === "server") {
|
|
1169
1179
|
providers.push({
|
|
1170
1180
|
provide: APP_INTERCEPTOR,
|
|
1171
1181
|
useClass: TenantContextInterceptor
|
|
@@ -1196,6 +1206,545 @@ DatabaseModule = _ts_decorate10([
|
|
|
1196
1206
|
Module3({})
|
|
1197
1207
|
], DatabaseModule);
|
|
1198
1208
|
|
|
1209
|
+
// src/database/repositories/primary-base.repository.ts
|
|
1210
|
+
import { Logger as Logger6 } from "@nestjs/common";
|
|
1211
|
+
var PrimaryBaseRepository = class {
|
|
1212
|
+
static {
|
|
1213
|
+
__name(this, "PrimaryBaseRepository");
|
|
1214
|
+
}
|
|
1215
|
+
database;
|
|
1216
|
+
logger;
|
|
1217
|
+
modelGetter;
|
|
1218
|
+
/**
|
|
1219
|
+
* Lazy getter for Prisma client.
|
|
1220
|
+
* Accesses the client from the database service only when needed,
|
|
1221
|
+
* avoiding initialization timing issues with NestJS lifecycle.
|
|
1222
|
+
*/
|
|
1223
|
+
get prisma() {
|
|
1224
|
+
return this.database.prismaClient;
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* Lazy getter for the Prisma model delegate.
|
|
1228
|
+
* Returns the specific model (e.g., prisma.user, prisma.tenant) for this repository.
|
|
1229
|
+
*/
|
|
1230
|
+
get model() {
|
|
1231
|
+
return this.modelGetter(this.prisma);
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Create a new repository instance
|
|
1235
|
+
*
|
|
1236
|
+
* @param database - The primary database service
|
|
1237
|
+
* @param getModel - Function that returns the Prisma model delegate from the client
|
|
1238
|
+
*
|
|
1239
|
+
* @example
|
|
1240
|
+
* ```typescript
|
|
1241
|
+
* // Standard usage with full parameter name
|
|
1242
|
+
* constructor(database: PrimaryDatabaseService) {
|
|
1243
|
+
* super(database, (prisma) => prisma.user);
|
|
1244
|
+
* }
|
|
1245
|
+
*
|
|
1246
|
+
* // Short syntax
|
|
1247
|
+
* constructor(database: PrimaryDatabaseService) {
|
|
1248
|
+
* super(database, (p) => p.user);
|
|
1249
|
+
* }
|
|
1250
|
+
*
|
|
1251
|
+
* // Complex model names
|
|
1252
|
+
* constructor(database: PrimaryDatabaseService) {
|
|
1253
|
+
* super(database, (p) => p.emailVerification);
|
|
1254
|
+
* }
|
|
1255
|
+
* ```
|
|
1256
|
+
*/
|
|
1257
|
+
constructor(database, getModel) {
|
|
1258
|
+
this.database = database;
|
|
1259
|
+
this.logger = new Logger6(this.constructor.name);
|
|
1260
|
+
this.modelGetter = getModel;
|
|
1261
|
+
this.logger.debug(`Initialized ${this.constructor.name}`);
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Create a new record
|
|
1265
|
+
*
|
|
1266
|
+
* @param data - The data to create the record with
|
|
1267
|
+
* @returns Promise resolving to the created record
|
|
1268
|
+
*
|
|
1269
|
+
* @example
|
|
1270
|
+
* ```typescript
|
|
1271
|
+
* const user = await userRepository.create({
|
|
1272
|
+
* email: 'user@example.com',
|
|
1273
|
+
* name: 'John Doe'
|
|
1274
|
+
* });
|
|
1275
|
+
* ```
|
|
1276
|
+
*/
|
|
1277
|
+
async create(data) {
|
|
1278
|
+
this.logger.log("Creating record");
|
|
1279
|
+
return await this.model.create({
|
|
1280
|
+
data
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Find a single record by ID
|
|
1285
|
+
*
|
|
1286
|
+
* @param id - The record ID
|
|
1287
|
+
* @returns Promise resolving to the record or null if not found
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```typescript
|
|
1291
|
+
* const user = await userRepository.findById('user-id-123');
|
|
1292
|
+
* ```
|
|
1293
|
+
*/
|
|
1294
|
+
async findById(id) {
|
|
1295
|
+
this.logger.debug(`Finding record by ID: ${id}`);
|
|
1296
|
+
return await this.model.findUnique({
|
|
1297
|
+
where: {
|
|
1298
|
+
id
|
|
1299
|
+
}
|
|
1300
|
+
});
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Find a single record with custom where clause
|
|
1304
|
+
*
|
|
1305
|
+
* @param where - The where clause or findUnique args
|
|
1306
|
+
* @returns Promise resolving to the record or null if not found
|
|
1307
|
+
*
|
|
1308
|
+
* @example
|
|
1309
|
+
* ```typescript
|
|
1310
|
+
* // Simple where clause
|
|
1311
|
+
* const user = await userRepository.findOne({ email: 'user@example.com' });
|
|
1312
|
+
*
|
|
1313
|
+
* // With include
|
|
1314
|
+
* const user = await userRepository.findOne({
|
|
1315
|
+
* where: { email: 'user@example.com' },
|
|
1316
|
+
* include: { posts: true }
|
|
1317
|
+
* });
|
|
1318
|
+
* ```
|
|
1319
|
+
*/
|
|
1320
|
+
async findOne(where) {
|
|
1321
|
+
this.logger.debug("Finding record with custom query");
|
|
1322
|
+
return await this.model.findUnique(typeof where === "object" && "where" in where ? where : {
|
|
1323
|
+
where
|
|
1324
|
+
});
|
|
1325
|
+
}
|
|
1326
|
+
/**
|
|
1327
|
+
* Find multiple records
|
|
1328
|
+
*
|
|
1329
|
+
* @param args - Prisma findMany arguments (where, orderBy, take, skip, etc.)
|
|
1330
|
+
* @returns Promise resolving to an array of records
|
|
1331
|
+
*
|
|
1332
|
+
* @example
|
|
1333
|
+
* ```typescript
|
|
1334
|
+
* // Find all users
|
|
1335
|
+
* const users = await userRepository.findMany();
|
|
1336
|
+
*
|
|
1337
|
+
* // Find with filtering and pagination
|
|
1338
|
+
* const users = await userRepository.findMany({
|
|
1339
|
+
* where: { status: 'ACTIVE' },
|
|
1340
|
+
* orderBy: { createdAt: 'desc' },
|
|
1341
|
+
* take: 10,
|
|
1342
|
+
* skip: 0
|
|
1343
|
+
* });
|
|
1344
|
+
* ```
|
|
1345
|
+
*/
|
|
1346
|
+
async findMany(args) {
|
|
1347
|
+
this.logger.debug("Finding multiple records");
|
|
1348
|
+
return await this.model.findMany(args);
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Update a record by ID
|
|
1352
|
+
*
|
|
1353
|
+
* @param id - The record ID
|
|
1354
|
+
* @param data - The data to update
|
|
1355
|
+
* @returns Promise resolving to the updated record
|
|
1356
|
+
*
|
|
1357
|
+
* @example
|
|
1358
|
+
* ```typescript
|
|
1359
|
+
* const user = await userRepository.update('user-id-123', {
|
|
1360
|
+
* name: 'Jane Doe'
|
|
1361
|
+
* });
|
|
1362
|
+
* ```
|
|
1363
|
+
*/
|
|
1364
|
+
async update(id, data) {
|
|
1365
|
+
this.logger.log(`Updating record with ID: ${id}`);
|
|
1366
|
+
return await this.model.update({
|
|
1367
|
+
where: {
|
|
1368
|
+
id
|
|
1369
|
+
},
|
|
1370
|
+
data
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
/**
|
|
1374
|
+
* Update multiple records
|
|
1375
|
+
*
|
|
1376
|
+
* @param where - The where clause to match records
|
|
1377
|
+
* @param data - The data to update
|
|
1378
|
+
* @returns Promise resolving to the count of updated records
|
|
1379
|
+
*
|
|
1380
|
+
* @example
|
|
1381
|
+
* ```typescript
|
|
1382
|
+
* const result = await userRepository.updateMany(
|
|
1383
|
+
* { status: 'PENDING' },
|
|
1384
|
+
* { status: 'ACTIVE' }
|
|
1385
|
+
* );
|
|
1386
|
+
* console.log(`Updated ${result.count} users`);
|
|
1387
|
+
* ```
|
|
1388
|
+
*/
|
|
1389
|
+
async updateMany(where, data) {
|
|
1390
|
+
this.logger.log("Updating multiple records");
|
|
1391
|
+
return await this.model.updateMany({
|
|
1392
|
+
where,
|
|
1393
|
+
data
|
|
1394
|
+
});
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* Delete a record by ID
|
|
1398
|
+
*
|
|
1399
|
+
* @param id - The record ID
|
|
1400
|
+
* @returns Promise resolving to the deleted record
|
|
1401
|
+
*
|
|
1402
|
+
* @example
|
|
1403
|
+
* ```typescript
|
|
1404
|
+
* const user = await userRepository.delete('user-id-123');
|
|
1405
|
+
* ```
|
|
1406
|
+
*/
|
|
1407
|
+
async delete(id) {
|
|
1408
|
+
this.logger.log(`Deleting record with ID: ${id}`);
|
|
1409
|
+
return await this.model.delete({
|
|
1410
|
+
where: {
|
|
1411
|
+
id
|
|
1412
|
+
}
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
/**
|
|
1416
|
+
* Delete multiple records
|
|
1417
|
+
*
|
|
1418
|
+
* @param where - The where clause to match records
|
|
1419
|
+
* @returns Promise resolving to the count of deleted records
|
|
1420
|
+
*
|
|
1421
|
+
* @example
|
|
1422
|
+
* ```typescript
|
|
1423
|
+
* const result = await userRepository.deleteMany({
|
|
1424
|
+
* status: 'INACTIVE',
|
|
1425
|
+
* createdAt: { lt: new Date('2020-01-01') }
|
|
1426
|
+
* });
|
|
1427
|
+
* console.log(`Deleted ${result.count} users`);
|
|
1428
|
+
* ```
|
|
1429
|
+
*/
|
|
1430
|
+
async deleteMany(where) {
|
|
1431
|
+
this.logger.log("Deleting multiple records");
|
|
1432
|
+
return await this.model.deleteMany({
|
|
1433
|
+
where
|
|
1434
|
+
});
|
|
1435
|
+
}
|
|
1436
|
+
/**
|
|
1437
|
+
* Count records
|
|
1438
|
+
*
|
|
1439
|
+
* @param where - Optional where clause to filter records
|
|
1440
|
+
* @returns Promise resolving to the count of records
|
|
1441
|
+
*
|
|
1442
|
+
* @example
|
|
1443
|
+
* ```typescript
|
|
1444
|
+
* // Count all users
|
|
1445
|
+
* const total = await userRepository.count();
|
|
1446
|
+
*
|
|
1447
|
+
* // Count active users
|
|
1448
|
+
* const activeCount = await userRepository.count({ status: 'ACTIVE' });
|
|
1449
|
+
* ```
|
|
1450
|
+
*/
|
|
1451
|
+
async count(where) {
|
|
1452
|
+
this.logger.debug("Counting records");
|
|
1453
|
+
return await this.model.count({
|
|
1454
|
+
where
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
/**
|
|
1458
|
+
* Check if a record exists
|
|
1459
|
+
*
|
|
1460
|
+
* @param where - The where clause to match records
|
|
1461
|
+
* @returns Promise resolving to true if at least one record exists, false otherwise
|
|
1462
|
+
*
|
|
1463
|
+
* @example
|
|
1464
|
+
* ```typescript
|
|
1465
|
+
* const emailExists = await userRepository.exists({
|
|
1466
|
+
* email: 'user@example.com'
|
|
1467
|
+
* });
|
|
1468
|
+
* ```
|
|
1469
|
+
*/
|
|
1470
|
+
async exists(where) {
|
|
1471
|
+
const count = await this.model.count({
|
|
1472
|
+
where
|
|
1473
|
+
});
|
|
1474
|
+
return count > 0;
|
|
1475
|
+
}
|
|
1476
|
+
};
|
|
1477
|
+
|
|
1478
|
+
// src/database/repositories/tenant-base.repository.ts
|
|
1479
|
+
import { Logger as Logger7 } from "@nestjs/common";
|
|
1480
|
+
var TenantBaseRepository = class {
|
|
1481
|
+
static {
|
|
1482
|
+
__name(this, "TenantBaseRepository");
|
|
1483
|
+
}
|
|
1484
|
+
database;
|
|
1485
|
+
logger;
|
|
1486
|
+
modelGetter;
|
|
1487
|
+
/**
|
|
1488
|
+
* Lazy getter for Prisma client.
|
|
1489
|
+
* Accesses the client from the database service only when needed,
|
|
1490
|
+
* avoiding initialization timing issues with NestJS lifecycle.
|
|
1491
|
+
*/
|
|
1492
|
+
get prisma() {
|
|
1493
|
+
return this.database.prismaClient;
|
|
1494
|
+
}
|
|
1495
|
+
/**
|
|
1496
|
+
* Lazy getter for the Prisma model delegate.
|
|
1497
|
+
* Returns the specific model (e.g., prisma.product, prisma.order) for this repository.
|
|
1498
|
+
*/
|
|
1499
|
+
get model() {
|
|
1500
|
+
return this.modelGetter(this.prisma);
|
|
1501
|
+
}
|
|
1502
|
+
/**
|
|
1503
|
+
* Create a new repository instance
|
|
1504
|
+
*
|
|
1505
|
+
* @param database - The tenant database service
|
|
1506
|
+
* @param getModel - Function that returns the Prisma model delegate from the client
|
|
1507
|
+
*
|
|
1508
|
+
* @example
|
|
1509
|
+
* ```typescript
|
|
1510
|
+
* // Standard usage with full parameter name
|
|
1511
|
+
* constructor(database: TenantDatabaseService) {
|
|
1512
|
+
* super(database, (prisma) => prisma.product);
|
|
1513
|
+
* }
|
|
1514
|
+
*
|
|
1515
|
+
* // Short syntax
|
|
1516
|
+
* constructor(database: TenantDatabaseService) {
|
|
1517
|
+
* super(database, (p) => p.product);
|
|
1518
|
+
* }
|
|
1519
|
+
*
|
|
1520
|
+
* // Complex model names
|
|
1521
|
+
* constructor(database: TenantDatabaseService) {
|
|
1522
|
+
* super(database, (p) => p.inventoryItem);
|
|
1523
|
+
* }
|
|
1524
|
+
* ```
|
|
1525
|
+
*/
|
|
1526
|
+
constructor(database, getModel) {
|
|
1527
|
+
this.database = database;
|
|
1528
|
+
this.logger = new Logger7(this.constructor.name);
|
|
1529
|
+
this.modelGetter = getModel;
|
|
1530
|
+
this.logger.debug(`Initialized ${this.constructor.name}`);
|
|
1531
|
+
}
|
|
1532
|
+
/**
|
|
1533
|
+
* Create a new record
|
|
1534
|
+
*
|
|
1535
|
+
* @param data - The data to create the record with
|
|
1536
|
+
* @returns Promise resolving to the created record
|
|
1537
|
+
*
|
|
1538
|
+
* @example
|
|
1539
|
+
* ```typescript
|
|
1540
|
+
* const product = await productRepository.create({
|
|
1541
|
+
* name: 'Widget',
|
|
1542
|
+
* sku: 'WDG-001',
|
|
1543
|
+
* price: 9.99
|
|
1544
|
+
* });
|
|
1545
|
+
* ```
|
|
1546
|
+
*/
|
|
1547
|
+
async create(data) {
|
|
1548
|
+
this.logger.log("Creating record");
|
|
1549
|
+
return await this.model.create({
|
|
1550
|
+
data
|
|
1551
|
+
});
|
|
1552
|
+
}
|
|
1553
|
+
/**
|
|
1554
|
+
* Find a single record by ID
|
|
1555
|
+
*
|
|
1556
|
+
* @param id - The record ID
|
|
1557
|
+
* @returns Promise resolving to the record or null if not found
|
|
1558
|
+
*
|
|
1559
|
+
* @example
|
|
1560
|
+
* ```typescript
|
|
1561
|
+
* const product = await productRepository.findById('product-id-123');
|
|
1562
|
+
* ```
|
|
1563
|
+
*/
|
|
1564
|
+
async findById(id) {
|
|
1565
|
+
this.logger.debug(`Finding record by ID: ${id}`);
|
|
1566
|
+
return await this.model.findUnique({
|
|
1567
|
+
where: {
|
|
1568
|
+
id
|
|
1569
|
+
}
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
/**
|
|
1573
|
+
* Find a single record with custom where clause
|
|
1574
|
+
*
|
|
1575
|
+
* @param where - The where clause or findUnique args
|
|
1576
|
+
* @returns Promise resolving to the record or null if not found
|
|
1577
|
+
*
|
|
1578
|
+
* @example
|
|
1579
|
+
* ```typescript
|
|
1580
|
+
* // Simple where clause
|
|
1581
|
+
* const product = await productRepository.findOne({ sku: 'WDG-001' });
|
|
1582
|
+
*
|
|
1583
|
+
* // With include
|
|
1584
|
+
* const product = await productRepository.findOne({
|
|
1585
|
+
* where: { sku: 'WDG-001' },
|
|
1586
|
+
* include: { category: true }
|
|
1587
|
+
* });
|
|
1588
|
+
* ```
|
|
1589
|
+
*/
|
|
1590
|
+
async findOne(where) {
|
|
1591
|
+
this.logger.debug("Finding record with custom query");
|
|
1592
|
+
return await this.model.findUnique(typeof where === "object" && "where" in where ? where : {
|
|
1593
|
+
where
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Find multiple records
|
|
1598
|
+
*
|
|
1599
|
+
* @param args - Prisma findMany arguments (where, orderBy, take, skip, etc.)
|
|
1600
|
+
* @returns Promise resolving to an array of records
|
|
1601
|
+
*
|
|
1602
|
+
* @example
|
|
1603
|
+
* ```typescript
|
|
1604
|
+
* // Find all products
|
|
1605
|
+
* const products = await productRepository.findMany();
|
|
1606
|
+
*
|
|
1607
|
+
* // Find with filtering and pagination
|
|
1608
|
+
* const products = await productRepository.findMany({
|
|
1609
|
+
* where: { status: 'ACTIVE' },
|
|
1610
|
+
* orderBy: { createdAt: 'desc' },
|
|
1611
|
+
* take: 10,
|
|
1612
|
+
* skip: 0
|
|
1613
|
+
* });
|
|
1614
|
+
* ```
|
|
1615
|
+
*/
|
|
1616
|
+
async findMany(args) {
|
|
1617
|
+
this.logger.debug("Finding multiple records");
|
|
1618
|
+
return await this.model.findMany(args);
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* Update a record by ID
|
|
1622
|
+
*
|
|
1623
|
+
* @param id - The record ID
|
|
1624
|
+
* @param data - The data to update
|
|
1625
|
+
* @returns Promise resolving to the updated record
|
|
1626
|
+
*
|
|
1627
|
+
* @example
|
|
1628
|
+
* ```typescript
|
|
1629
|
+
* const product = await productRepository.update('product-id-123', {
|
|
1630
|
+
* price: 12.99
|
|
1631
|
+
* });
|
|
1632
|
+
* ```
|
|
1633
|
+
*/
|
|
1634
|
+
async update(id, data) {
|
|
1635
|
+
this.logger.log(`Updating record with ID: ${id}`);
|
|
1636
|
+
return await this.model.update({
|
|
1637
|
+
where: {
|
|
1638
|
+
id
|
|
1639
|
+
},
|
|
1640
|
+
data
|
|
1641
|
+
});
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Update multiple records
|
|
1645
|
+
*
|
|
1646
|
+
* @param where - The where clause to match records
|
|
1647
|
+
* @param data - The data to update
|
|
1648
|
+
* @returns Promise resolving to the count of updated records
|
|
1649
|
+
*
|
|
1650
|
+
* @example
|
|
1651
|
+
* ```typescript
|
|
1652
|
+
* const result = await productRepository.updateMany(
|
|
1653
|
+
* { status: 'PENDING' },
|
|
1654
|
+
* { status: 'ACTIVE' }
|
|
1655
|
+
* );
|
|
1656
|
+
* console.log(`Updated ${result.count} products`);
|
|
1657
|
+
* ```
|
|
1658
|
+
*/
|
|
1659
|
+
async updateMany(where, data) {
|
|
1660
|
+
this.logger.log("Updating multiple records");
|
|
1661
|
+
return await this.model.updateMany({
|
|
1662
|
+
where,
|
|
1663
|
+
data
|
|
1664
|
+
});
|
|
1665
|
+
}
|
|
1666
|
+
/**
|
|
1667
|
+
* Delete a record by ID
|
|
1668
|
+
*
|
|
1669
|
+
* @param id - The record ID
|
|
1670
|
+
* @returns Promise resolving to the deleted record
|
|
1671
|
+
*
|
|
1672
|
+
* @example
|
|
1673
|
+
* ```typescript
|
|
1674
|
+
* const product = await productRepository.delete('product-id-123');
|
|
1675
|
+
* ```
|
|
1676
|
+
*/
|
|
1677
|
+
async delete(id) {
|
|
1678
|
+
this.logger.log(`Deleting record with ID: ${id}`);
|
|
1679
|
+
return await this.model.delete({
|
|
1680
|
+
where: {
|
|
1681
|
+
id
|
|
1682
|
+
}
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* Delete multiple records
|
|
1687
|
+
*
|
|
1688
|
+
* @param where - The where clause to match records
|
|
1689
|
+
* @returns Promise resolving to the count of deleted records
|
|
1690
|
+
*
|
|
1691
|
+
* @example
|
|
1692
|
+
* ```typescript
|
|
1693
|
+
* const result = await productRepository.deleteMany({
|
|
1694
|
+
* status: 'INACTIVE',
|
|
1695
|
+
* createdAt: { lt: new Date('2020-01-01') }
|
|
1696
|
+
* });
|
|
1697
|
+
* console.log(`Deleted ${result.count} products`);
|
|
1698
|
+
* ```
|
|
1699
|
+
*/
|
|
1700
|
+
async deleteMany(where) {
|
|
1701
|
+
this.logger.log("Deleting multiple records");
|
|
1702
|
+
return await this.model.deleteMany({
|
|
1703
|
+
where
|
|
1704
|
+
});
|
|
1705
|
+
}
|
|
1706
|
+
/**
|
|
1707
|
+
* Count records
|
|
1708
|
+
*
|
|
1709
|
+
* @param where - Optional where clause to filter records
|
|
1710
|
+
* @returns Promise resolving to the count of records
|
|
1711
|
+
*
|
|
1712
|
+
* @example
|
|
1713
|
+
* ```typescript
|
|
1714
|
+
* // Count all products
|
|
1715
|
+
* const total = await productRepository.count();
|
|
1716
|
+
*
|
|
1717
|
+
* // Count active products
|
|
1718
|
+
* const activeCount = await productRepository.count({ status: 'ACTIVE' });
|
|
1719
|
+
* ```
|
|
1720
|
+
*/
|
|
1721
|
+
async count(where) {
|
|
1722
|
+
this.logger.debug("Counting records");
|
|
1723
|
+
return await this.model.count({
|
|
1724
|
+
where
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
/**
|
|
1728
|
+
* Check if a record exists
|
|
1729
|
+
*
|
|
1730
|
+
* @param where - The where clause to match records
|
|
1731
|
+
* @returns Promise resolving to true if at least one record exists, false otherwise
|
|
1732
|
+
*
|
|
1733
|
+
* @example
|
|
1734
|
+
* ```typescript
|
|
1735
|
+
* const skuExists = await productRepository.exists({
|
|
1736
|
+
* sku: 'WDG-001'
|
|
1737
|
+
* });
|
|
1738
|
+
* ```
|
|
1739
|
+
*/
|
|
1740
|
+
async exists(where) {
|
|
1741
|
+
const count = await this.model.count({
|
|
1742
|
+
where
|
|
1743
|
+
});
|
|
1744
|
+
return count > 0;
|
|
1745
|
+
}
|
|
1746
|
+
};
|
|
1747
|
+
|
|
1199
1748
|
// src/database/decorators/tenant.decorator.ts
|
|
1200
1749
|
import { createParamDecorator } from "@nestjs/common";
|
|
1201
1750
|
var Tenant = createParamDecorator((data, ctx) => {
|
|
@@ -1214,13 +1763,232 @@ var Onboarding = /* @__PURE__ */ __name(() => SetMetadata("isOnboarding", true),
|
|
|
1214
1763
|
// src/auth/decorators/public.decorator.ts
|
|
1215
1764
|
import { SetMetadata as SetMetadata2 } from "@nestjs/common";
|
|
1216
1765
|
var Public = /* @__PURE__ */ __name(() => SetMetadata2("isPublic", true), "Public");
|
|
1766
|
+
|
|
1767
|
+
// src/http/http.module.ts
|
|
1768
|
+
import { Module as Module4 } from "@nestjs/common";
|
|
1769
|
+
|
|
1770
|
+
// src/http/guards/csrf.guard.ts
|
|
1771
|
+
import { ForbiddenException, Injectable as Injectable8, Logger as Logger8 } from "@nestjs/common";
|
|
1772
|
+
import { Reflector as Reflector2 } from "@nestjs/core";
|
|
1773
|
+
function _ts_decorate11(decorators, target, key, desc) {
|
|
1774
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1775
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1776
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1777
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1778
|
+
}
|
|
1779
|
+
__name(_ts_decorate11, "_ts_decorate");
|
|
1780
|
+
function _ts_metadata7(k, v) {
|
|
1781
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
1782
|
+
}
|
|
1783
|
+
__name(_ts_metadata7, "_ts_metadata");
|
|
1784
|
+
var CsrfGuard = class _CsrfGuard {
|
|
1785
|
+
static {
|
|
1786
|
+
__name(this, "CsrfGuard");
|
|
1787
|
+
}
|
|
1788
|
+
reflector;
|
|
1789
|
+
logger = new Logger8(_CsrfGuard.name);
|
|
1790
|
+
constructor(reflector) {
|
|
1791
|
+
this.reflector = reflector;
|
|
1792
|
+
}
|
|
1793
|
+
async canActivate(context) {
|
|
1794
|
+
const request = context.switchToHttp().getRequest();
|
|
1795
|
+
const reply = context.switchToHttp().getResponse();
|
|
1796
|
+
const safeMethods = [
|
|
1797
|
+
"GET",
|
|
1798
|
+
"HEAD",
|
|
1799
|
+
"OPTIONS"
|
|
1800
|
+
];
|
|
1801
|
+
if (safeMethods.includes(request.method)) {
|
|
1802
|
+
return true;
|
|
1803
|
+
}
|
|
1804
|
+
try {
|
|
1805
|
+
const fastifyInstance = request.server;
|
|
1806
|
+
if (!fastifyInstance.csrfProtection) {
|
|
1807
|
+
this.logger.error("CSRF protection plugin not found. Ensure @fastify/csrf-protection is registered.");
|
|
1808
|
+
throw new ForbiddenException("CSRF protection not configured");
|
|
1809
|
+
}
|
|
1810
|
+
await new Promise((resolve, reject) => {
|
|
1811
|
+
fastifyInstance.csrfProtection(request, reply, (err) => {
|
|
1812
|
+
if (err) {
|
|
1813
|
+
reject(err);
|
|
1814
|
+
} else {
|
|
1815
|
+
resolve();
|
|
1816
|
+
}
|
|
1817
|
+
});
|
|
1818
|
+
});
|
|
1819
|
+
this.logger.debug(`CSRF validation successful for ${request.method} ${request.url}`);
|
|
1820
|
+
return true;
|
|
1821
|
+
} catch (error) {
|
|
1822
|
+
this.logger.warn(`CSRF validation failed for ${request.method} ${request.url}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
1823
|
+
throw new ForbiddenException({
|
|
1824
|
+
errors: [
|
|
1825
|
+
{
|
|
1826
|
+
field: "csrf",
|
|
1827
|
+
message: "Invalid or missing CSRF token"
|
|
1828
|
+
}
|
|
1829
|
+
],
|
|
1830
|
+
message: "CSRF validation failed"
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
};
|
|
1835
|
+
CsrfGuard = _ts_decorate11([
|
|
1836
|
+
Injectable8(),
|
|
1837
|
+
_ts_metadata7("design:type", Function),
|
|
1838
|
+
_ts_metadata7("design:paramtypes", [
|
|
1839
|
+
typeof Reflector2 === "undefined" ? Object : Reflector2
|
|
1840
|
+
])
|
|
1841
|
+
], CsrfGuard);
|
|
1842
|
+
|
|
1843
|
+
// src/http/http.module.ts
|
|
1844
|
+
function _ts_decorate12(decorators, target, key, desc) {
|
|
1845
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1846
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1847
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1848
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1849
|
+
}
|
|
1850
|
+
__name(_ts_decorate12, "_ts_decorate");
|
|
1851
|
+
var HttpModule = class {
|
|
1852
|
+
static {
|
|
1853
|
+
__name(this, "HttpModule");
|
|
1854
|
+
}
|
|
1855
|
+
};
|
|
1856
|
+
HttpModule = _ts_decorate12([
|
|
1857
|
+
Module4({
|
|
1858
|
+
providers: [
|
|
1859
|
+
CsrfGuard
|
|
1860
|
+
],
|
|
1861
|
+
exports: [
|
|
1862
|
+
CsrfGuard
|
|
1863
|
+
]
|
|
1864
|
+
})
|
|
1865
|
+
], HttpModule);
|
|
1866
|
+
|
|
1867
|
+
// src/http/filters/http-exception.filter.ts
|
|
1868
|
+
import { Catch, HttpException, HttpStatus, Logger as Logger9 } from "@nestjs/common";
|
|
1869
|
+
function _ts_decorate13(decorators, target, key, desc) {
|
|
1870
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1871
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1872
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1873
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1874
|
+
}
|
|
1875
|
+
__name(_ts_decorate13, "_ts_decorate");
|
|
1876
|
+
var HttpExceptionFilter = class _HttpExceptionFilter {
|
|
1877
|
+
static {
|
|
1878
|
+
__name(this, "HttpExceptionFilter");
|
|
1879
|
+
}
|
|
1880
|
+
logger = new Logger9(_HttpExceptionFilter.name);
|
|
1881
|
+
catch(exception, host) {
|
|
1882
|
+
const ctx = host.switchToHttp();
|
|
1883
|
+
const reply = ctx.getResponse();
|
|
1884
|
+
const request = ctx.getRequest();
|
|
1885
|
+
let status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
1886
|
+
let errors = [];
|
|
1887
|
+
let message;
|
|
1888
|
+
if (exception instanceof HttpException) {
|
|
1889
|
+
status = exception.getStatus();
|
|
1890
|
+
const exceptionResponse = exception.getResponse();
|
|
1891
|
+
if (typeof exceptionResponse === "object" && exceptionResponse !== null) {
|
|
1892
|
+
const responseObj = exceptionResponse;
|
|
1893
|
+
if (Array.isArray(responseObj.message)) {
|
|
1894
|
+
errors = this.parseValidationErrors(responseObj.message);
|
|
1895
|
+
message = "Validation failed";
|
|
1896
|
+
} else if (responseObj.message) {
|
|
1897
|
+
errors = [
|
|
1898
|
+
{
|
|
1899
|
+
field: "general",
|
|
1900
|
+
message: responseObj.message
|
|
1901
|
+
}
|
|
1902
|
+
];
|
|
1903
|
+
message = responseObj.message;
|
|
1904
|
+
}
|
|
1905
|
+
} else if (typeof exceptionResponse === "string") {
|
|
1906
|
+
errors = [
|
|
1907
|
+
{
|
|
1908
|
+
field: "general",
|
|
1909
|
+
message: exceptionResponse
|
|
1910
|
+
}
|
|
1911
|
+
];
|
|
1912
|
+
message = exceptionResponse;
|
|
1913
|
+
}
|
|
1914
|
+
} else if (exception instanceof Error) {
|
|
1915
|
+
this.logger.error(`Unhandled error: ${exception.message}`, exception.stack);
|
|
1916
|
+
errors = [
|
|
1917
|
+
{
|
|
1918
|
+
field: "general",
|
|
1919
|
+
message: "Internal server error"
|
|
1920
|
+
}
|
|
1921
|
+
];
|
|
1922
|
+
message = "An unexpected error occurred";
|
|
1923
|
+
} else {
|
|
1924
|
+
this.logger.error("Unknown exception type", exception);
|
|
1925
|
+
errors = [
|
|
1926
|
+
{
|
|
1927
|
+
field: "general",
|
|
1928
|
+
message: "Internal server error"
|
|
1929
|
+
}
|
|
1930
|
+
];
|
|
1931
|
+
message = "An unexpected error occurred";
|
|
1932
|
+
}
|
|
1933
|
+
const errorResponse = {
|
|
1934
|
+
errors,
|
|
1935
|
+
message,
|
|
1936
|
+
statusCode: status,
|
|
1937
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1938
|
+
path: request.url
|
|
1939
|
+
};
|
|
1940
|
+
if (status >= 500) {
|
|
1941
|
+
this.logger.error(`HTTP ${status} Error: ${JSON.stringify(errorResponse)}`, exception instanceof Error ? exception.stack : void 0);
|
|
1942
|
+
} else {
|
|
1943
|
+
this.logger.warn(`HTTP ${status} Error: ${JSON.stringify(errorResponse)}`);
|
|
1944
|
+
}
|
|
1945
|
+
reply.status(status).send(errorResponse);
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* Parse class-validator error messages into field-specific errors
|
|
1949
|
+
*/
|
|
1950
|
+
parseValidationErrors(messages) {
|
|
1951
|
+
const errors = [];
|
|
1952
|
+
for (const msg of messages) {
|
|
1953
|
+
if (typeof msg === "string") {
|
|
1954
|
+
errors.push({
|
|
1955
|
+
field: "general",
|
|
1956
|
+
message: msg
|
|
1957
|
+
});
|
|
1958
|
+
} else if (typeof msg === "object" && msg.property && msg.constraints) {
|
|
1959
|
+
const field = msg.property;
|
|
1960
|
+
const constraintMessages = Object.values(msg.constraints);
|
|
1961
|
+
for (const constraintMsg of constraintMessages) {
|
|
1962
|
+
errors.push({
|
|
1963
|
+
field,
|
|
1964
|
+
message: constraintMsg
|
|
1965
|
+
});
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
return errors.length > 0 ? errors : [
|
|
1970
|
+
{
|
|
1971
|
+
field: "general",
|
|
1972
|
+
message: "Validation failed"
|
|
1973
|
+
}
|
|
1974
|
+
];
|
|
1975
|
+
}
|
|
1976
|
+
};
|
|
1977
|
+
HttpExceptionFilter = _ts_decorate13([
|
|
1978
|
+
Catch()
|
|
1979
|
+
], HttpExceptionFilter);
|
|
1217
1980
|
export {
|
|
1218
1981
|
AuthConfigModule,
|
|
1982
|
+
CsrfGuard,
|
|
1219
1983
|
DatabaseModule,
|
|
1984
|
+
HttpExceptionFilter,
|
|
1985
|
+
HttpModule,
|
|
1220
1986
|
Onboarding,
|
|
1987
|
+
PrimaryBaseRepository,
|
|
1221
1988
|
PrimaryDatabaseService,
|
|
1222
1989
|
Public,
|
|
1223
1990
|
Tenant,
|
|
1991
|
+
TenantBaseRepository,
|
|
1224
1992
|
TenantContextService,
|
|
1225
1993
|
TenantDatabaseService,
|
|
1226
1994
|
VrittiAuthGuard
|