npm package: @noukha-technologies/mdm-client
Description: Client library for consuming Noukha MDM services
Author: Noukha
Latest version: 1.0.4

# @noukha/mdm-client

Client library for consuming Noukha MDM (Master Data Management) services via HTTP API.

## Installation

```bash
npm install @noukha/mdm-client
```

## Features

- **HTTP Client**: Axios-based HTTP client with retry logic
- **Group Management**: Full CRUD operations for groups
- **Schema Management**: Dynamic schema creation and management
- **Record Management**: CRUD operations for dynamic records
- **Bulk Operations**: Bulk create, update, and delete operations
- **File Upload**: Excel file upload support
- **Error Handling**: Comprehensive error handling with retry logic
- **TypeScript Support**: Full TypeScript support with type definitions

## Quick Start

### 1. Initialize the Client

```typescript
import { MdmClient } from '@noukha/mdm-client';

const client = new MdmClient({
  baseUrl: 'http://localhost:3000/api',
  apiKey: 'your-api-key', // optional
  timeout: 30000,
  retries: 3
});
```

### 2. Use the Client

```typescript
// Create a group
const group = await client.createGroup({
  groupName: 'products',
  displayName: 'Products'
});

// Create a schema
const schema = await client.createSchema({
  name: 'product',
  displayName: 'Product',
  fields: [
    {
      name: 'name',
      type: FieldType.STRING,
      required: true,
      unique: true
    },
    {
      name: 'price',
      type: FieldType.NUMBER,
      required: true
    }
  ]
});

// Create a record
const record = await client.createRecord('product', {
  data: {
    name: 'Sample Product',
    price: 99.99
  }
});

// Get all records with pagination
const records = await client.getAllRecords('product', {
  page: 1,
  limit: 10,
  search: 'sample'
});
```

## API Reference

### Group Management

```typescript
// Create a group
await client.createGroup(createGroupDto: CreateGroupDto): Promise<Group>

// Get all groups
await client.getAllGroups(): Promise<Group[]>

// Get a specific group
await client.getGroup(groupId: string): Promise<Group>

// Update a group
await client.updateGroup(groupId: string, updateGroupDto: UpdateGroupDto): Promise<Group>

// Delete a group
await client.deleteGroup(groupId: string): Promise<void>
```

### Schema Management

```typescript
// Create a schema
await client.createSchema(createSchemaDto: CreateSchemaDto): Promise<any>

// Get all schemas with pagination
await client.getAllSchemas(query?: FindSchemasQueryDto): Promise<PaginatedResponse<CreateSchemaDto>>

// Get a specific schema
await client.getSchema(schemaName: string): Promise<CreateSchemaDto>

// Update a schema
await client.updateSchema(schemaName: string, updateSchemaDto: CreateSchemaDto): Promise<any>

// Delete a schema
await client.deleteSchema(schemaName: string, force?: boolean): Promise<any>

// Get schemas by group ID
await client.getSchemasByGroupId(groupId: string): Promise<CreateSchemaDto[]>
```

### Record Management

```typescript
// Create a record
await client.createRecord(schemaName: string, createRecordDto: CreateRecordDto): Promise<any>

// Get all records with pagination
await client.getAllRecords(schemaName: string, query?: FindRecordsQueryDto): Promise<PaginatedResponse<any>>

// Get a specific record
await client.getRecord(schemaName: string, recordId: string): Promise<any>

// Update a record
await client.updateRecord(schemaName: string, recordId: string, updateRecordDto: UpdateRecordDto): Promise<any>

// Delete a record
await client.deleteRecord(schemaName: string, recordId: string): Promise<any>
```

### Bulk Operations

```typescript
// Bulk create records
await client.bulkCreateRecords(schemaName: string, records: CreateRecordDto[]): Promise<any[]>

// Bulk update records
await client.bulkUpdateRecords(schemaName: string, updates: Array<{ id: string; data: any }>): Promise<any>

// Bulk delete records
await client.bulkDeleteRecords(schemaName: string, ids: string[]): Promise<any>
```

### File Upload

```typescript
// Upload Excel file
await client.uploadExcelFile(file: File | Buffer, filename?: string): Promise<any>
```

### Health Check

```typescript
// Check service health
await client.healthCheck(): Promise<any>
```

## Configuration

```typescript
interface MdmClientConfig {
  baseUrl: string;                    // Base URL of the MDM service
  apiKey?: string;                    // API key for authentication
  timeout?: number;                   // Request timeout in milliseconds (default: 30000)
  retries?: number;                   // Number of retries for failed requests (default: 3)
  headers?: Record<string, string>;   // Additional headers
}
```

## Error Handling

The client includes comprehensive error handling:

- **Automatic Retries**: Failed requests are automatically retried with exponential backoff
- **Error Messages**: Detailed error messages from the API
- **Type Safety**: TypeScript ensures type safety for all operations

```typescript
try {
  const group = await client.createGroup({
    groupName: 'products',
    displayName: 'Products'
  });
} catch (error) {
  console.error('Failed to create group:', error.message);
}
```

## Pagination

All list operations support pagination:

```typescript
const records = await client.getAllRecords('product', {
  page: 1,
  limit: 10,
  search: 'sample',
  sort: 'createdAt',
  order: 'desc',
  filters: {
    isActive: true
  }
});

console.log(records.data);        // Array of records
console.log(records.meta.total);  // Total count
console.log(records.meta.page);   // Current page
console.log(records.meta.limit);  // Items per page
console.log(records.meta.totalPages); // Total pages
```

## Search and Filtering

Most operations support search and filtering:

```typescript
// Search records
const records = await client.getAllRecords('product', {
  search: 'laptop',
  filters: {
    price: { $gte: 100 },
    category: 'electronics'
  }
});

// Search schemas
const schemas = await client.getAllSchemas({
  search: 'product',
  groupId: 'group123'
});
```

## License

MIT