|
| 1 | +# @prismate/client |
| 2 | + |
| 3 | +Prisma client management module for Prismate, providing type-safe client wrappers and model management. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Type-Safe Client Management**: Wrapper around Prisma clients with full TypeScript support |
| 8 | +- **Model Extraction**: Automatic extraction of model names and schemas from Prisma clients |
| 9 | +- **Memory Safety**: Handles nullable clients gracefully with proper error handling |
| 10 | +- **Schema Building**: Constructs comprehensive schema information from DMMF data |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +pnpm add @prismate/client |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### Basic Client Management |
| 21 | + |
| 22 | +```typescript |
| 23 | +import { PrismaClientManager } from '@prismate/client'; |
| 24 | +import { PrismaClient } from '@prisma/client'; |
| 25 | + |
| 26 | +const prisma = new PrismaClient(); |
| 27 | +const manager = new PrismaClientManager(prisma); |
| 28 | + |
| 29 | +// Access available models |
| 30 | +console.log(manager.models); // ['user', 'post', 'comment'] |
| 31 | + |
| 32 | +// Access schemas |
| 33 | +console.log(manager.schemas.user); // User model schema |
| 34 | +console.log(manager.schemas.post); // Post model schema |
| 35 | +``` |
| 36 | + |
| 37 | +### With DMMF Data |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { PrismaClientManager } from '@prismate/client'; |
| 41 | +import { PrismaClient } from '@prisma/client'; |
| 42 | + |
| 43 | +const prisma = new PrismaClient(); |
| 44 | +const dmmf = prisma._dmmf; |
| 45 | + |
| 46 | +const manager = new PrismaClientManager(prisma, dmmf); |
| 47 | + |
| 48 | +// Full schema information available |
| 49 | +console.log(manager.schemas.user.name); // Field name |
| 50 | +console.log(manager.schemas.user.email.type); // Field type |
| 51 | +console.log(manager.schemas.user.email.isRequired); // Required status |
| 52 | +``` |
| 53 | + |
| 54 | +### Nullable Client Handling |
| 55 | + |
| 56 | +```typescript |
| 57 | +import { PrismaClientManager } from '@prismate/client'; |
| 58 | + |
| 59 | +// Safe handling of nullable clients |
| 60 | +const manager = new PrismaClientManager(null); |
| 61 | + |
| 62 | +console.log(manager.hasClient); // false |
| 63 | +console.log(manager.models); // [] |
| 64 | +console.log(manager.schemas); // {} |
| 65 | +``` |
| 66 | + |
| 67 | +## API Reference |
| 68 | + |
| 69 | +### PrismaClientManager |
| 70 | + |
| 71 | +#### Constructor |
| 72 | + |
| 73 | +```typescript |
| 74 | +new PrismaClientManager<TClient, TDMMF>( |
| 75 | + client: TClient, |
| 76 | + dmmf?: DMMF |
| 77 | +) |
| 78 | +``` |
| 79 | + |
| 80 | +#### Properties |
| 81 | + |
| 82 | +- `models`: Array of available model names |
| 83 | +- `schemas`: Object containing model schemas |
| 84 | +- `hasClient`: Boolean indicating if client is available |
| 85 | +- `modelCount`: Number of available models |
| 86 | +- `schemaCount`: Number of available schemas |
| 87 | + |
| 88 | +#### Methods |
| 89 | + |
| 90 | +- `create()`: Create new records |
| 91 | +- `findUnique()`: Find unique records |
| 92 | +- `findMany()`: Find multiple records |
| 93 | +- `update()`: Update existing records |
| 94 | +- `delete()`: Delete records |
| 95 | + |
| 96 | +## Type Safety |
| 97 | + |
| 98 | +The module provides comprehensive TypeScript types: |
| 99 | + |
| 100 | +- `ModelName<TClient>`: Extracted model names |
| 101 | +- `Schemas<TDMMF>`: Schema structure types |
| 102 | +- `SchemaField`: Individual field definitions |
| 103 | +- `DMMFModel`: Model structure types |
| 104 | + |
| 105 | +## Error Handling |
| 106 | + |
| 107 | +All operations include proper error handling: |
| 108 | + |
| 109 | +```typescript |
| 110 | +try { |
| 111 | + const result = await manager.create('user', { data: userData }); |
| 112 | +} catch (error) { |
| 113 | + console.error('Operation failed:', error); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Performance |
| 118 | + |
| 119 | +- **Lazy Loading**: Schemas are built only when needed |
| 120 | +- **Immutable Data**: All schema data is frozen for performance |
| 121 | +- **Memory Efficient**: Minimal memory footprint with automatic cleanup |
| 122 | + |
| 123 | +## Contributing |
| 124 | + |
| 125 | +See the main [Prismate repository](https://github.com/prismate/prismate) for contribution guidelines. |
| 126 | + |
| 127 | +## License |
| 128 | + |
| 129 | +MIT License - see LICENSE file for details. |
0 commit comments