@decentparadox/typeconnect
v1.0.2
Published
**TypeConnect** is an npm package that simplifies database operations by providing a universal Entity Manager for interacting with both SQLite and PostgreSQL databases. With this package, you can easily define entities using decorators, and perform common
Downloads
201
Maintainers
Readme
TypeConnect
TypeConnect is an npm package that simplifies database operations by providing a universal Entity Manager for interacting with both SQLite and PostgreSQL databases. With this package, you can easily define entities using decorators, and perform common database operations (CRUD) without worrying about the underlying database specifics.
Features
- Multi-Database Support: Seamlessly supports both SQLite and PostgreSQL databases.
- Entity and Column Decorators: Use decorators to define tables and columns in your entities.
- CRUD Operations: Perform create, read, update, and delete operations easily across both SQLite and PostgreSQL.
- Database Connection Management: Automatically connects and disconnects from the database.
- Dynamic SQL Queries: Adapts SQL queries to work with both SQLite and PostgreSQL.
Installation
To install TypeConnect, use npm:
npm install @decentparadox/typeconnect
Usage
1. Define an Entity
Use the @Entity
and @Column
decorators to define your database tables and columns.
import { Entity, Column } from "typeconnect";
@Entity("users")
class User {
@Column("id")
id: string;
@Column("name")
name: string;
@Column("email")
email: string;
}
2. Initialize EntityManager
Create an instance of the EntityManager
and connect to your database.
import { EntityManager } from "typeconnect";
// Connect to SQLite (default) or PostgreSQL
const entityManager = new EntityManager(":memory:", "sqlite");
// Connect to PostgreSQL
// const entityManager = new EntityManager("postgres://user:password@localhost:5432/mydb", "postgres");
// Connect to database
await entityManager.connect();
3. Perform CRUD Operations
Use the EntityManager
to perform CRUD operations (Create, Read, Update, Delete).
Create Table
await entityManager.createTable(User);
Save an Entity
const user = new User();
user.id = "1";
user.name = "John Doe";
user.email = "[email protected]";
await entityManager.save(user);
Find All Entities
const users = await entityManager.findAll(User);
console.log(users);
Find an Entity by ID
const user = await entityManager.findById(User, "1");
console.log(user);
Update an Entity
const updates = { name: "Jane Doe" };
await entityManager.update(User, "1", updates);
Delete an Entity
await entityManager.delete(User, "1");
4. Disconnect from Database
Once you're done with the database, disconnect it:
await entityManager.disconnect();
Configuration
You can configure TypeConnect to use different database types (SQLite or PostgreSQL) by passing the appropriate values in the constructor.
- SQLite: Default database type (
sqlite
) - PostgreSQL: Set
dbType
to"postgres"
and provide the connection string.
const entityManager = new EntityManager(":memory:", "sqlite"); // SQLite
const entityManager = new EntityManager("postgres://user:password@localhost:5432/mydb", "postgres"); // PostgreSQL
Advanced Usage
Multiple Entities
You can create multiple entities and manage them using the same EntityManager
instance.
Custom SQL Queries
The EntityManager
supports dynamically generated SQL queries. You can also add your custom SQL queries if needed.
Future Scope
- Enhanced Query Builder: Support more complex queries such as
JOIN
,GROUP BY
, andORDER BY
. - Support for Other Databases: Expand to support MySQL, MongoDB, and others.
- ORM Features: Add model validation, migrations, and relationships between entities.
- Better Error Handling: Improve error logging and handling for complex database interactions.
Contributing
We welcome contributions! If you'd like to contribute to the development of TypeConnect, please fork the repository and create a pull request.
License
MIT License. See LICENSE for more information.