Skip to content

Commit 321685d

Browse files
Refactor prismate to modular architecture (#1)
* Update pnpm-lock.yaml with workspace dependencies and Jest configuration Co-authored-by: dev <[email protected]> * Add advanced types, testing, and project configuration to core module Co-authored-by: dev <[email protected]> * Refactor core package: improve types, add performance utils, and update configs (#4) Co-authored-by: Cursor Agent <[email protected]> --------- Co-authored-by: Cursor Agent <[email protected]>
1 parent dbac115 commit 321685d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+7439
-382
lines changed

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
"check-types": "turbo run check-types"
1111
},
1212
"devDependencies": {
13+
"@eslint/js": "^9.31.0",
14+
"eslint": "^9.33.0",
15+
"eslint-config-prettier": "^10.1.1",
16+
"eslint-plugin-only-warn": "^1.1.0",
17+
"eslint-plugin-turbo": "^2.5.0",
1318
"prettier": "^3.6.2",
1419
"turbo": "^2.5.5",
15-
"typescript": "5.8.3"
20+
"typescript": "5.8.3",
21+
"typescript-eslint": "^8.37.0"
1622
},
1723
"packageManager": "[email protected]",
1824
"engines": {

packages/cache/.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['@prismate/eslint-config'],
3+
parserOptions: {
4+
project: './tsconfig.json',
5+
},
6+
};

packages/cache/jest.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
roots: ['<rootDir>/src', '<rootDir>/tests'],
5+
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
6+
transform: {
7+
'^.+\\.ts$': 'ts-jest',
8+
},
9+
collectCoverageFrom: [
10+
'src/**/*.ts',
11+
'!src/**/*.d.ts',
12+
'!src/index.ts',
13+
],
14+
coverageDirectory: 'coverage',
15+
coverageReporters: ['text', 'lcov', 'html'],
16+
moduleNameMapping: {
17+
'^@/(.*)$': '<rootDir>/src/$1',
18+
},
19+
};

packages/cache/package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "@prismate/cache",
3+
"version": "0.1.0",
4+
"description": "Schema and query caching module for Prismate",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"files": [
8+
"dist"
9+
],
10+
"scripts": {
11+
"build": "tsc",
12+
"dev": "tsc --watch",
13+
"clean": "rm -rf dist",
14+
"type-check": "tsc --noEmit",
15+
"test": "jest",
16+
"test:watch": "jest --watch",
17+
"test:coverage": "jest --coverage"
18+
},
19+
"dependencies": {
20+
"@prismate/core": "workspace:*"
21+
},
22+
"devDependencies": {
23+
"@prismate/eslint-config": "workspace:*",
24+
"@prismate/typescript-config": "workspace:*",
25+
"@types/jest": "^29.5.0",
26+
"jest": "^29.5.0",
27+
"ts-jest": "^29.1.0",
28+
"typescript": "^5.3.0"
29+
},
30+
"keywords": [
31+
"prisma",
32+
"cache",
33+
"performance",
34+
"optimization",
35+
"typescript"
36+
],
37+
"author": "Prismate Team",
38+
"license": "MIT"
39+
}

packages/cache/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "@prismate/typescript-config/base.json",
3+
"compilerOptions": {
4+
"outDir": "./dist",
5+
"rootDir": "./src",
6+
"declaration": true,
7+
"declarationMap": true,
8+
"sourceMap": true
9+
},
10+
"include": ["src/**/*"],
11+
"exclude": ["node_modules", "dist", "tests"]
12+
}

packages/client/.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['@prismate/eslint-config'],
3+
parserOptions: {
4+
project: './tsconfig.json',
5+
},
6+
};

packages/client/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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.

packages/client/jest.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
roots: ['<rootDir>/src', '<rootDir>/tests'],
5+
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
6+
transform: {
7+
'^.+\\.ts$': 'ts-jest',
8+
},
9+
collectCoverageFrom: [
10+
'src/**/*.ts',
11+
'!src/**/*.d.ts',
12+
'!src/index.ts',
13+
],
14+
coverageDirectory: 'coverage',
15+
coverageReporters: ['text', 'lcov', 'html'],
16+
moduleNameMapping: {
17+
'^@/(.*)$': '<rootDir>/src/$1',
18+
},
19+
};

packages/client/package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@prismate/client",
3+
"version": "0.1.0",
4+
"description": "Prisma client management module for Prismate",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"files": [
8+
"dist"
9+
],
10+
"scripts": {
11+
"build": "tsc",
12+
"dev": "tsc --watch",
13+
"clean": "rm -rf dist",
14+
"type-check": "tsc --noEmit",
15+
"test": "jest",
16+
"test:watch": "jest --watch",
17+
"test:coverage": "jest --coverage"
18+
},
19+
"dependencies": {
20+
"@prismate/core": "workspace:*",
21+
"change-case": "^5.4.4"
22+
},
23+
"devDependencies": {
24+
"@prismate/eslint-config": "workspace:*",
25+
"@prismate/typescript-config": "workspace:*",
26+
"@types/jest": "^29.5.0",
27+
"jest": "^29.5.0",
28+
"ts-jest": "^29.1.0",
29+
"typescript": "^5.3.0"
30+
},
31+
"peerDependencies": {
32+
"@prisma/client": "^5.0.0"
33+
},
34+
"keywords": [
35+
"prisma",
36+
"client",
37+
"management",
38+
"typescript",
39+
"database"
40+
],
41+
"author": "Prismate Team",
42+
"license": "MIT"
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Client configuration defaults
2+
export const DEFAULT_CLIENT_CONFIG = {
3+
enableLogging: false,
4+
enableMetrics: false,
5+
connectionTimeout: 30000, // 30 seconds
6+
queryTimeout: 10000, // 10 seconds
7+
} as const;
8+
9+
// Internal Prisma method prefixes
10+
export const INTERNAL_METHOD_PREFIXES = ['$', '_'] as const;
11+
12+
// Excluded method names
13+
export const EXCLUDED_METHODS = [
14+
'constructor',
15+
'prototype',
16+
'__proto__',
17+
] as const;
18+
19+
// Error messages
20+
export const CLIENT_ERROR_MESSAGES = {
21+
CLIENT_NOT_AVAILABLE: 'Prisma client is not available',
22+
MODEL_NOT_FOUND: 'Model not found in client',
23+
OPERATION_FAILED: 'Database operation failed',
24+
INVALID_MODEL_NAME: 'Invalid model name provided',
25+
} as const;
26+
27+
// Logging levels
28+
export const LOG_LEVELS = {
29+
ERROR: 'error',
30+
WARN: 'warn',
31+
INFO: 'info',
32+
DEBUG: 'debug',
33+
} as const;

0 commit comments

Comments
 (0)