prisma-libs
v1.0.3
Published
Prisma schema and migrations for database management
Readme
Prisma Libs
A modern, type-safe database library built with Prisma ORM. This package provides a clean and intuitive API for database operations with full TypeScript support.
Features
- 🚀 Latest Prisma Version: Built with Prisma 5.10.0
- 🔒 Type Safety: Full TypeScript support with generated types
- 🛠️ Utility Functions: Pre-built CRUD operations for common use cases
- 🔄 Database Migrations: Easy database schema management
- 🌱 Seeding: Built-in database seeding for development
- 📊 Health Checks: Database connection monitoring
- 🛑 Graceful Shutdown: Proper cleanup on application termination
Installation
npm install prisma-libsQuick Start
1. Set up your database
Create a .env file in your project root:
DATABASE_URL="postgresql://username:password@localhost:5432/your_database_name"2. Generate Prisma client
npx prisma generate3. Push your schema to the database
npx prisma db push4. Use in your code
import { prisma, userUtils, postUtils, connectDatabase } from 'prisma-libs';
async function main() {
// Connect to database
await connectDatabase();
// Create a user
const user = await userUtils.createUser('[email protected]', 'John Doe');
console.log('Created user:', user);
// Create a post
const post = await postUtils.createPost(
'My First Post',
'This is the content of my first post.',
user.id,
true
);
console.log('Created post:', post);
// Find user with posts
const userWithPosts = await userUtils.findUserById(user.id);
console.log('User with posts:', userWithPosts);
}
main().catch(console.error);API Reference
Core Exports
prisma: Singleton Prisma client instancePrismaClient: Prisma client classUser,Post: Generated TypeScript types
Database Management
import { connectDatabase, disconnectDatabase, healthCheck } from 'prisma-libs';
// Connect to database
await connectDatabase();
// Check database health
const isHealthy = await healthCheck();
// Disconnect from database
await disconnectDatabase();User Operations
import { userUtils } from 'prisma-libs';
// Create a new user
const user = await userUtils.createUser('[email protected]', 'User Name');
// Find user by email
const user = await userUtils.findUserByEmail('[email protected]');
// Find user by ID (includes posts)
const user = await userUtils.findUserById('user-id');
// Update user
const updatedUser = await userUtils.updateUser('user-id', {
name: 'New Name',
email: '[email protected]'
});
// Delete user
await userUtils.deleteUser('user-id');
// Get all users
const users = await userUtils.getAllUsers();Post Operations
import { postUtils } from 'prisma-libs';
// Create a new post
const post = await postUtils.createPost(
'Post Title',
'Post content',
'author-user-id',
true // published
);
// Find post by ID (includes author)
const post = await postUtils.findPostById('post-id');
// Find posts by author
const posts = await postUtils.findPostsByAuthor('author-user-id');
// Update post
const updatedPost = await postUtils.updatePost('post-id', {
title: 'New Title',
published: true
});
// Delete post
await postUtils.deletePost('post-id');
// Get all posts
const posts = await postUtils.getAllPosts();
// Get only published posts
const publishedPosts = await postUtils.getPublishedPosts();Graceful Shutdown
import { setupGracefulShutdown } from 'prisma-libs';
// Set up graceful shutdown handlers
setupGracefulShutdown();Database Schema
The package includes a sample schema with User and Post models:
model User {
id String @id @default(cuid())
email String @unique
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
posts Post[]
@@map("users")
}
model Post {
id String @id @default(cuid())
title String
content String?
published Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId String
@@map("posts")
}Development
Prerequisites
- Node.js 18 or higher
- PostgreSQL database
- npm or yarn
Setup
- Clone the repository
- Install dependencies:
npm install - Copy
env.exampleto.envand configure your database URL - Generate Prisma client:
npm run generate - Push schema to database:
npm run db:push - Seed database:
npm run db:seed
Available Scripts
npm run build- Build the TypeScript codenpm run dev- Watch mode for developmentnpm run generate- Generate Prisma clientnpm run db:push- Push schema changes to databasenpm run db:migrate- Create and apply migrationsnpm run db:studio- Open Prisma Studionpm run db:seed- Seed the database with sample data
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
MIT License - see LICENSE file for details.
Support
For issues and questions, please open an issue on GitHub. # prisma-libs
