database-connector
v2.5.19
Published
MongoDB models package with Mongoose schemas for e-commerce applications. Includes User, Product, Store, Order and more with built-in validation and virtual properties.
Readme
Database Connector Package
MongoDB models package with Mongoose schemas for e-commerce application.
Installation
npm install database-connectorUsage
Basic Setup
const { connectToDatabase, configure, User, Product, Store } = require('database-connector');
// Configure the package (IMPORTANT: Do this before using models)
configure({
baseURL: process.env.APP_URL || 'http://localhost:3000',
defaultImagePath: '/images/default.png',
defaultStoreImagePath: '/images/stores/default.jpg'
});
// Connect to database
await connectToDatabase(process.env.MONGODB_URI);
// Use models
const user = await User.findById(userId);
const products = await Product.find({ storeId });Configuration
Important: You must configure the package before using any models that have virtual properties for image URLs (Product and Store models).
const { configure } = require('database-connector');
configure({
baseURL: 'https://api.yourdomain.com', // Your API base URL
defaultImagePath: '/images/default.png', // Default product image
defaultStoreImagePath: '/images/stores/default.jpg' // Default store image
});Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| baseURL | string | process.env.APP_URL or 'http://localhost:3000' | Base URL for constructing image URLs |
| defaultImagePath | string | '/images/default.png' | Default image path for products without images |
| defaultStoreImagePath | string | '/images/stores/default.jpg' | Default image path for stores without images |
Environment Variables
The package will use process.env.APP_URL as the default baseURL if not configured explicitly. You can set this in your .env file:
APP_URL=https://api.yourdomain.com
MONGODB_URI=mongodb://localhost:27017/your-databaseAvailable Models
- User - User accounts and profiles
- Store - Store/shop information
- Product - Product catalog
- Order - Customer orders
- Cart - Shopping cart
- Bill - Billing information
- Payment - Payment transactions
- Category - Product categories
- StoreCategory - Store categories
- Offer - Special offers and promotions
- FlashDeal - Flash sale deals
- Subscription - Store subscriptions
- Plan - Subscription plans
- SubscriptionOffer - Subscription offer details
- ReductionOffer - Reduction/discount offers
- Notification - User notifications
- View - Product/store view tracking
- Sale - Sales records
- StoreRate - Store ratings
- ResetPassword - Password reset tokens
- PaymentType - Payment method types
- UserAction - User activity tracking
- Policy - Store/product policies
Virtual Properties
Some models include virtual properties that generate full URLs for images:
Product Model
imagesss- Returns array of product images with full URLsvariantImages- Returns array of variant images with full URLsdiscounted- Returns discounted price if offer exists
const product = await Product.findById(productId).populate('offer');
console.log(product.imagesss);
// ['https://api.yourdomain.com/uploads/product1.jpg', ...]Store Model
imageUrl- Returns store image with full URLrating- Calculates average rating from ratingSum/ratingCount
const store = await Store.findById(storeId);
console.log(store.imageUrl);
// 'https://api.yourdomain.com/uploads/store-logo.jpg'Example: Complete Setup
const express = require('express');
const {
connectToDatabase,
configure,
User,
Product,
Store,
Order
} = require('database-connector');
const app = express();
// Configure the package FIRST
configure({
baseURL: process.env.APP_URL || 'http://localhost:3000'
});
// Connect to database
connectToDatabase(process.env.MONGODB_URI)
.then(() => console.log('Database connected'))
.catch(err => console.error('Database connection failed:', err));
// Use models in your routes
app.get('/products/:id', async (req, res) => {
try {
const product = await Product.findById(req.params.id);
res.json({
...product.toJSON(),
// Virtual properties are automatically included with toJSON
images: product.imagesss
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000);Swagger Documentation
All models include comprehensive Swagger/OpenAPI documentation. You can use these to generate API documentation.
Policy Schema
The Policy model can be used both as a standalone model and as an embedded schema:
const { Policy, policySchema } = require('database-connector');
// As standalone model
const policy = new Policy({ /* ... */ });
await policy.save();
// As embedded schema (already used in Product, Store, User models)
const product = new Product({
name: 'Product Name',
policy: {
workingTime: { openTime: '09:00', closeTime: '18:00' },
// ... other policy fields
}
});TypeScript Support
Type definitions are not included in this package yet. They may be added in future versions.
License
ISC
Changelog
See CHANGELOG.md for version history and changes.
