@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 CHANGED
@@ -219,6 +219,119 @@ export class UsersService {
219
219
  }
220
220
  ```
221
221
 
222
+ ### Using Base Repositories
223
+
224
+ The SDK provides base repository classes for common CRUD operations with automatic tenant scoping:
225
+
226
+ #### Primary Database Repositories
227
+
228
+ For entities in the primary/platform database (tenants, users, sessions, etc.):
229
+
230
+ ```typescript
231
+ import { Injectable } from '@nestjs/common';
232
+ import { PrimaryBaseRepository, PrimaryDatabaseService } from '@vritti/api-sdk';
233
+ import { User, CreateUserDto, UpdateUserDto } from './types';
234
+
235
+ @Injectable()
236
+ export class UserRepository extends PrimaryBaseRepository<
237
+ User,
238
+ CreateUserDto,
239
+ UpdateUserDto
240
+ > {
241
+ constructor(database: PrimaryDatabaseService) {
242
+ // Use model delegate pattern - type-safe with IDE autocomplete!
243
+ super(database, (prisma) => prisma.user);
244
+ }
245
+
246
+ // Add custom methods as needed
247
+ async findByEmail(email: string): Promise<User | null> {
248
+ return this.model.findUnique({ where: { email } });
249
+ }
250
+
251
+ async findActiveUsers(): Promise<User[]> {
252
+ return this.model.findMany({
253
+ where: { status: 'ACTIVE' },
254
+ orderBy: { createdAt: 'desc' },
255
+ });
256
+ }
257
+ }
258
+ ```
259
+
260
+ #### Tenant Database Repositories
261
+
262
+ For tenant-scoped entities (products, orders, customers, etc.):
263
+
264
+ ```typescript
265
+ import { Injectable } from '@nestjs/common';
266
+ import { TenantBaseRepository, TenantDatabaseService } from '@vritti/api-sdk';
267
+ import { Product, CreateProductDto, UpdateProductDto } from './types';
268
+
269
+ @Injectable()
270
+ export class ProductRepository extends TenantBaseRepository<
271
+ Product,
272
+ CreateProductDto,
273
+ UpdateProductDto
274
+ > {
275
+ constructor(database: TenantDatabaseService) {
276
+ // Short syntax is also supported
277
+ super(database, (p) => p.product);
278
+ }
279
+
280
+ // Custom methods for product-specific queries
281
+ async findBySku(sku: string): Promise<Product | null> {
282
+ return this.model.findUnique({ where: { sku } });
283
+ }
284
+
285
+ async findInStock(): Promise<Product[]> {
286
+ return this.model.findMany({
287
+ where: { quantity: { gt: 0 } },
288
+ });
289
+ }
290
+ }
291
+ ```
292
+
293
+ #### Available Base Repository Methods
294
+
295
+ Both `PrimaryBaseRepository` and `TenantBaseRepository` provide these methods:
296
+
297
+ ```typescript
298
+ // Create
299
+ await repository.create(data);
300
+
301
+ // Read
302
+ await repository.findById(id);
303
+ await repository.findOne({ where: { email } });
304
+ await repository.findMany({ where: { status: 'ACTIVE' } });
305
+
306
+ // Update
307
+ await repository.update(id, data);
308
+ await repository.updateMany({ status: 'PENDING' }, { status: 'ACTIVE' });
309
+
310
+ // Delete
311
+ await repository.delete(id);
312
+ await repository.deleteMany({ status: 'INACTIVE' });
313
+
314
+ // Count & Exists
315
+ await repository.count({ status: 'ACTIVE' });
316
+ await repository.exists({ email: 'user@example.com' });
317
+ ```
318
+
319
+ #### Benefits of the Model Delegate Pattern
320
+
321
+ ```typescript
322
+ // ✅ Type-safe with IDE autocomplete
323
+ super(database, (prisma) => prisma.user);
324
+
325
+ // ✅ Refactor-friendly - TypeScript errors if model name changes
326
+ super(database, (p) => p.emailVerification);
327
+
328
+ // ✅ Works with complex model names
329
+ super(database, (p) => p.inventoryItem);
330
+
331
+ // ✅ No hardcoded strings
332
+ // ❌ Old way: super(database, 'user') // Error-prone!
333
+ ```
334
+
222
335
  ## Architecture
223
336
 
224
337
  ### Gateway Mode (`forServer()`)