1.0.0 • Published 5 months ago
@akib6074/simple-orm v1.0.0
Simple ORM for PostgreSQL
A lightweight, type-safe ORM for PostgreSQL written in TypeScript. Features connection pooling, CRUD operations, and a simple API.
Installation
npm install @akib6074/simple-orm
Features
- 🔄 Connection pooling for better performance
- 🔒 Type-safe database operations
- 📝 Simple and intuitive API
- 🛠️ Full CRUD operations support
- 🔍 Flexible query building
- 🔐 SSL support for secure connections
Quick Start
import { Model } from '@akib6074/simple-orm';
// Define your model
class User extends Model<{
id?: number;
name: string;
email: string;
}> {
static tableName = 'users';
static columns = ['id', 'name', 'email'];
}
// Initialize the database connection
await User.initialize({
host: 'localhost',
port: 5432,
database: 'your_database',
user: 'your_username',
password: 'your_password'
});
// Create a new user
const newUser = await User.create({
name: 'John Doe',
email: 'john@example.com'
});
// Find all users
const allUsers = await User.findAll();
// Find users with specific conditions
const specificUsers = await User.findAllWhere({ name: 'John' });
// Find user by ID
const user = await User.findById(1);
// Update user
const updatedUser = await User.update(1, { name: 'Jane Doe' });
// Delete user
const deleted = await User.delete(1);
// Don't forget to close the connection pool when done
await User.close();
API Reference
Model Class
The base class that provides all database operations.
Static Methods
initialize(config: DatabaseConfig): Promise<void>
- Initializes the database connection pool
- Required before any database operations
findAll(): Promise<ModelAttributes[]>
- Returns all records from the table
findAllWhere(conditions: Partial<ModelAttributes>): Promise<ModelAttributes[]>
- Returns records matching the given conditions
findById(id: number): Promise<ModelAttributes | null>
- Returns a single record by ID
create(data: Partial<ModelAttributes>): Promise<ModelAttributes>
- Creates a new record and returns it
update(id: number, data: Partial<ModelAttributes>): Promise<ModelAttributes | null>
- Updates a record and returns the updated version
delete(id: number): Promise<boolean>
- Deletes a record and returns true if successful
close(): Promise<void>
- Closes the database connection pool
Types
interface DatabaseConfig {
host: string;
port: number;
database: string;
user: string;
password: string;
ssl?: boolean;
}
interface ModelAttributes {
[key: string]: any;
}
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
1.0.0
5 months ago