TypeScript Entity Framework for Prisma ORM with Active Record pattern, fluent query builder, relation graphs, batch operations, advanced CRUD, search filters, and pagination utilities.
npm install prisma-entity-frameworkuser.create(), user.update() |
bash
npm install prisma-entity-framework
or
yarn add prisma-entity-framework
or
pnpm add prisma-entity-framework
`
Requirements:
- Node.js >= 16
- Prisma Client >= 4.0.0
---
π Quick Start
1. Configure Prisma Client (one-time setup)
`typescript
import { PrismaClient } from '@prisma/client';
import { configurePrisma } from 'prisma-entity-framework';
const prisma = new PrismaClient();
configurePrisma(prisma);
`
2. Define an Entity
`typescript
import { BaseEntity, Property } from 'prisma-entity-framework';
import { User as PrismaUser } from '@prisma/client';
import { prisma } from './prisma-client';
export class User extends BaseEntity {
static readonly model = prisma.user;
@Property() declare id: number;
@Property() declare name: string;
@Property() declare email: string;
}
`
3. Use It!
`typescript
// Create a new user with the Active Record pattern
const user = new User({ name: "John Doe", email: "john.doe@example.com" });
await user.create();
// Find users with the declarative query builder
const results = await User.findByFilter({
isActive: true
}, {
onlyOne: true, //get only first match or all records, false by default
search: {
stringSearch: [{ keys: ['name', 'email'], value: 'john', mode: 'LIKE' }]
},
pagination: { page: 1, pageSize: 10, take: 10, skip: 0 }
});
console.log(results.data); // Paginated array of User instances
`
---
β¨ Core Features
- ποΈ Active Record Pattern: Manage your data with intuitive instance methods like user.create().
`typescript
const user = new User({ name: "John" });
await user.create();
`
- π Advanced Query Builder: Build complex, declarative queries with support for LIKE, ranges, and lists.
`typescript
const users = await User.findByFilter({name: "John"}, {
search: {
rangeSearch: [{ keys: ['age'], min: 18 }]
}
});
`
- β‘ Optimized Batch Operations: High-performance, database-aware batching for createMany, updateMany, and upsertMany.
`typescript
await User.createMany([{ name: "User1" }, { name: "User2" }]);
`
- π Parallel Execution: Run batch operations concurrently for a 2-6x speed boost with zero configuration required.
`typescript
// This feature is automatic, no code change needed!
const manyUsers = [{ email: 'user1@example.com' }, { email: 'user2@example.com' }];
await User.upsertMany(manyUsers); // Runs in parallel
`
- πΈοΈ Graph Traversal: Analyze and navigate your data model with utilities for dependency sorting and pathfinding.
`typescript
import { ModelUtils } from 'prisma-entity-framework';
const path = ModelUtils.findPathToParentModel('Comment', 'User'); // -> "post.author"
`
- π Automatic Pagination: Get formatted, paginated responses from your queries out of the box.
`typescript
const paginated = await User.findByFilter({}, { pagination: { page: 1, pageSize: 10 } });
`
---
π Documentation
Dive deeper into the framework's capabilities:
- Complete API Reference: A detailed breakdown of all classes, methods, and types.
- Advanced Examples: See complex queries in action.
- Advanced configuration guide: Learn about advanced configuration.
- Property Behavior Guide: Understand how the @Property decorator works.
- Testing Guide: Best practices for testing your entities.
---
π§ͺ Testing
`bash
Run all tests (SQLite)
npm test
Test a specific database
npm run test:mysql
Run tests on all databases
npm run test:all-databases
``