@rezamirzapour/pod-sdk 1.0.1 → 1.0.2
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 +89 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -389,10 +389,12 @@ export async function createBlogPost(meta: BlogPostMeta) {
|
|
|
389
389
|
|
|
390
390
|
### 2. High-Level Generic CRUD Repository (`createCrud<DataType>`)
|
|
391
391
|
|
|
392
|
-
Create an ORM-like typed repository for any entity type
|
|
392
|
+
Create an ORM-like typed repository for any entity type and expose it through a dedicated service layer:
|
|
393
|
+
|
|
394
|
+
#### Step 1: Create the Products Repository & API (`services/productsApi.ts`)
|
|
393
395
|
|
|
394
396
|
```typescript
|
|
395
|
-
//
|
|
397
|
+
// services/productsApi.ts
|
|
396
398
|
import { podSdk } from '@/lib/pod';
|
|
397
399
|
|
|
398
400
|
export interface ProductData {
|
|
@@ -404,62 +406,107 @@ export interface ProductData {
|
|
|
404
406
|
entityId?: number;
|
|
405
407
|
}
|
|
406
408
|
|
|
407
|
-
// Instantiate typed CRUD service for "
|
|
408
|
-
|
|
409
|
+
// 1. Instantiate typed CRUD service for "product_entity"
|
|
410
|
+
const productCrud = podSdk.createCrud<ProductData>({
|
|
409
411
|
name: 'product_entity',
|
|
410
412
|
type: 'commerce',
|
|
411
413
|
detailedType: 'gadgets',
|
|
412
414
|
});
|
|
413
|
-
```
|
|
414
415
|
|
|
415
|
-
|
|
416
|
+
// 2. Encapsulate business queries and domain operations
|
|
417
|
+
export class ProductsApi {
|
|
418
|
+
/**
|
|
419
|
+
* Retrieves all products formatted as clean domain objects
|
|
420
|
+
*/
|
|
421
|
+
public static async getProducts(): Promise<ProductData[]> {
|
|
422
|
+
const result = await productCrud.getAll();
|
|
423
|
+
return result.result.map((r) => r.item.metadata.data);
|
|
424
|
+
}
|
|
416
425
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
426
|
+
/**
|
|
427
|
+
* Retrieves a single product by numerical entity ID
|
|
428
|
+
*/
|
|
429
|
+
public static async getProductById(id: number): Promise<ProductData | null> {
|
|
430
|
+
const res = await productCrud.getById(id);
|
|
431
|
+
return res.result?.[0]?.metadata.data || null;
|
|
432
|
+
}
|
|
420
433
|
|
|
421
|
-
|
|
422
|
-
|
|
434
|
+
/**
|
|
435
|
+
* Creates a product and automatically binds the generated entityId
|
|
436
|
+
*/
|
|
437
|
+
public static async createProduct(data: Omit<ProductData, 'entityId'>) {
|
|
438
|
+
return productCrud.createAndBindEntityId({
|
|
439
|
+
...data,
|
|
440
|
+
inStock: true,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
423
443
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
444
|
+
/**
|
|
445
|
+
* Updates an existing product
|
|
446
|
+
*/
|
|
447
|
+
public static async updateProduct(entityId: number, data: ProductData) {
|
|
448
|
+
return productCrud.update(entityId, data);
|
|
449
|
+
}
|
|
429
450
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
451
|
+
/**
|
|
452
|
+
* Soft-archives a product without deleting history
|
|
453
|
+
*/
|
|
454
|
+
public static async archiveProduct(entityId: number, data: ProductData) {
|
|
455
|
+
return productCrud.archive(entityId, data);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Deletes a product permanently
|
|
460
|
+
*/
|
|
461
|
+
public static async deleteProduct(entityId: number) {
|
|
462
|
+
return productCrud.delete(entityId);
|
|
463
|
+
}
|
|
434
464
|
}
|
|
465
|
+
```
|
|
435
466
|
|
|
436
|
-
|
|
437
|
-
export async function saveNewProduct(data: { title: string; sku: string; price: number }) {
|
|
438
|
-
const res = await productCrud.createAndBindEntityId({
|
|
439
|
-
title: data.title,
|
|
440
|
-
sku: data.sku,
|
|
441
|
-
price: data.price,
|
|
442
|
-
inStock: true,
|
|
443
|
-
category: 'electronics',
|
|
444
|
-
});
|
|
467
|
+
---
|
|
445
468
|
|
|
446
|
-
|
|
447
|
-
return res;
|
|
448
|
-
}
|
|
469
|
+
#### Step 2: Consume `ProductsApi` in Server Components (`app/products/page.tsx`)
|
|
449
470
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
471
|
+
```tsx
|
|
472
|
+
// app/products/page.tsx (Server Component)
|
|
473
|
+
import { ProductsApi } from '@/services/productsApi';
|
|
474
|
+
|
|
475
|
+
export default async function ProductsPage() {
|
|
476
|
+
const products = await ProductsApi.getProducts();
|
|
454
477
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
478
|
+
return (
|
|
479
|
+
<main className="container mx-auto py-8 px-4">
|
|
480
|
+
<h1 className="text-2xl font-bold mb-6">Products Catalog</h1>
|
|
481
|
+
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
|
|
482
|
+
{products.map((p) => (
|
|
483
|
+
<div key={p.sku} className="border p-4 rounded-lg shadow-sm">
|
|
484
|
+
<h2 className="font-semibold text-lg">{p.title}</h2>
|
|
485
|
+
<p className="text-gray-500 text-sm">SKU: {p.sku}</p>
|
|
486
|
+
<p className="text-blue-600 font-bold mt-2">${p.price.toLocaleString()}</p>
|
|
487
|
+
</div>
|
|
488
|
+
))}
|
|
489
|
+
</div>
|
|
490
|
+
</main>
|
|
491
|
+
);
|
|
458
492
|
}
|
|
493
|
+
```
|
|
459
494
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
495
|
+
---
|
|
496
|
+
|
|
497
|
+
#### Step 3: Mutate via Server Actions (`app/actions/products.ts`)
|
|
498
|
+
|
|
499
|
+
```typescript
|
|
500
|
+
// app/actions/products.ts
|
|
501
|
+
'use server';
|
|
502
|
+
|
|
503
|
+
import { ProductsApi } from '@/services/productsApi';
|
|
504
|
+
import { revalidatePath } from 'next/cache';
|
|
505
|
+
|
|
506
|
+
export async function addProduct(data: { title: string; sku: string; price: number; category: string }) {
|
|
507
|
+
const result = await ProductsApi.createProduct(data);
|
|
508
|
+
revalidatePath('/products');
|
|
509
|
+
return result;
|
|
463
510
|
}
|
|
464
511
|
```
|
|
465
512
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rezamirzapour/pod-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Unified, type-safe enterprise SDK for POD Platform microservices (SSO, CustomPost, Podspace, Podform, Notification, Social, CMS, IUMS) powered by @rezamirzapour/http.",
|
|
5
5
|
"author": "Reza",
|
|
6
6
|
"license": "MIT",
|