auto-pool-data
v1.0.0
Published
Database model package for auto-pool-data with Sequelize and PostgreSQL support.
Maintainers
Readme
auto-pool-data
A reusable Sequelize package that provides inventory, warehouse, purchase order, and sales order management models for PostgreSQL.
The package uses your application's existing Sequelize instance and creates only the tables managed by this package without affecting your application's existing models or data.
Features
- Uses your existing Sequelize connection
- No additional database connections are created
- Creates only package-specific tables
- Existing tables and data remain unchanged
- Supports PostgreSQL and Sequelize v7
- Ready-to-use inventory and order management models
- Models can be imported and used directly
Included Models
The package provides the following Sequelize models:
- Warehouse
- Inventory
- PurchaseOrder
- PurchaseOrderItem
- SalesOrder
- SalesOrderItem
- SalesOrderItemAllocation
Installation
Install the package:
npm install auto-pool-dataInstall required peer dependencies:
npm install @sequelize/core @sequelize/postgres pgProject Setup
Step 1: Configure Sequelize
Create your Sequelize instance.
config/database.ts
import { Sequelize } from "@sequelize/core";
import { PostgresDialect } from "@sequelize/postgres";
export const sequelize = new Sequelize({
dialect: PostgresDialect,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASS,
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT ?? 5432),
logging: false,
pool: {
max: 20,
min: 5,
idle: 10000,
acquire: 30000,
},
});Step 2: Initialize Package Models
Before using any package model, initialize the package with your Sequelize instance.
import { initializeAutoPoolData } from "auto-pool-data";
import { sequelize } from "./config/database";
await initializeAutoPoolData(sequelize);This registers all package models with your application's Sequelize instance.
Important
You must initialize the package before using any exported model.
await initializeAutoPoolData(sequelize);If initialization is skipped, Sequelize will throw an error similar to:
Model Warehouse has not been initialized yet.Creating Package Tables
To create package tables:
import { syncAutoPoolData } from "auto-pool-data";
await syncAutoPoolData(true);This creates only the tables managed by this package.
Existing tables are preserved.
Existing data is preserved.
No application-specific models are synced.
Sync Options
await syncAutoPoolData(sync);| Value | Description | |---------|-------------| | true | Create package tables if they do not exist | | false | Skip table creation |
Default Behavior
await syncAutoPoolData();or
await syncAutoPoolData(false);Result:
No tables are created.Recommended Usage
During first-time setup:
await initializeAutoPoolData(sequelize);
await syncAutoPoolData(true);After tables have been created:
await initializeAutoPoolData(sequelize);
await syncAutoPoolData(false);or simply:
await initializeAutoPoolData(sequelize);Using Models
All models can be imported directly from the package.
import {
Warehouse,
Inventory,
PurchaseOrder,
PurchaseOrderItem,
SalesOrder,
SalesOrderItem,
SalesOrderItemAllocation,
} from "auto-pool-data";Example: Create Warehouse
import { Warehouse } from "auto-pool-data";
await Warehouse.create({
name: "Main Warehouse",
});Example: Get All Warehouses
const warehouses = await Warehouse.findAll();Complete Example
import { app } from "./app";
import {
initializeAutoPoolData,
syncAutoPoolData,
Warehouse,
} from "auto-pool-data";
import { sequelize } from "./config/database";
const PORT = 5000;
const start = async () => {
// Initialize package models
await initializeAutoPoolData(sequelize);
// Create package tables if missing
await syncAutoPoolData(true);
// Use package models
await Warehouse.create({
name: "Warehouse 1",
});
app.listen(PORT, () => {
console.log(`Server running on ${PORT}`);
});
};
start();SalesOrderItemAllocation
SalesOrderItemAllocation.create({
sales_order_item_id: 1,
warehouse_id: 1,
quantity: 5,
});Exported APIs
initializeAutoPoolData
Initializes package models using your Sequelize instance.
await initializeAutoPoolData(sequelize);Returns:
{
Warehouse,
Inventory,
PurchaseOrder,
PurchaseOrderItem,
SalesOrder,
SalesOrderItem,
SalesOrderItemAllocation
}syncAutoPoolData
Creates package tables.
await syncAutoPoolData(true);Notes
- Uses your application's Sequelize instance.
- Does not create a new database connection.
- Does not call
sequelize.sync()globally. - Only package models are synced.
- Existing application models are not affected.
- Existing database tables remain unchanged.
- Existing data is never deleted.
Requirements
- Node.js 18+
- PostgreSQL
- Sequelize v7
License
ISC License
