Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Deploy Lambda

on:
push:
branches:
- feat/lamda

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup Node 20
uses: actions/setup-node@v3
with:
node-version: 20

- name: Install dependencies
run: npm install --production

- name: Create ZIP
run: |
zip -r function.zip . \
-x "*.git*" \
-x "node_modules/.cache/*"

- name: Deploy to Lambda
uses: appleboy/lambda-action@v0.1.5
with:
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_region: us-east-1
function_name: digifymenu-apis
zip_file: function.zip
49 changes: 37 additions & 12 deletions config/db.connect.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
const mongoose = require('mongoose');
const mongoose = require("mongoose");

let isConnected = false;
let connectionPromise = null;

const isProduction = process.env.NODE_ENV === "production";

const connectDB = async () => {
try {
await mongoose.connect(process.env.DATABASE_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
});
} catch (err) {
console.error(err);
}
}

module.exports = connectDB
if (isConnected) {
return;
}

if (connectionPromise) {
await connectionPromise;
return;
}

connectionPromise = mongoose.connect(process.env.DATABASE_URI, {
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
// Production optimizations
autoIndex: !isProduction, // Don't rebuild indexes on Lambda cold starts
maxPoolSize: 10, // Allow more concurrent queries
minPoolSize: 2, // Keep warm connections
}).then((db) => {
isConnected = db.connections[0].readyState === 1;
return db;
}).catch((error) => {
isConnected = false;
connectionPromise = null;
console.error("MongoDB connection error:", error);
throw error;
});

await connectionPromise;
};

module.exports = connectDB;

17 changes: 10 additions & 7 deletions controller/adsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ const getAds = async (req, res) => {

const filter = { branch_id, is_admin: false, is_expired: false };

const count = await Ads.countDocuments(filter);
const rows = await Ads.find(filter)
.sort({ _id: -1 })
.skip(skip)
.limit(limit);
const [count, rows] = await Promise.all([
Ads.countDocuments(filter),
Ads.find(filter)
.sort({ _id: -1 })
.skip(skip)
.limit(limit)
.lean(),
]);

return res.status(200).json({
success: true,
Expand Down Expand Up @@ -93,7 +96,7 @@ const getActiveAds = async (req, res) => {
branch_id,
valid_from: { $lte: now },
valid_to: { $gte: now },
}).sort({ _id: -1 });
}).sort({ _id: -1 }).lean();

return res.status(200).json({ success: true, data: ads });
} catch (error) {
Expand Down Expand Up @@ -175,7 +178,7 @@ const checkAdsImageExistsDB = async (fileName, id = null) => {
filter._id = { $ne: id };
}

const exists = await Ads.findOne(filter);
const exists = await Ads.findOne(filter).select('_id').lean();

return {
success: true,
Expand Down
20 changes: 12 additions & 8 deletions controller/categoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ const getCategories = async (req, res) => {

const filter = { branch_id, is_deleted: false };

const totalCount = await Category.countDocuments(filter);
const rows = await Category.find(filter)
.sort({ display_order: 1 }) // ASC
.skip(skip)
.limit(limit);
const [totalCount, rows] = await Promise.all([
Category.countDocuments(filter),
Category.find(filter)
.sort({ display_order: 1 })
.skip(skip)
.limit(limit)
.lean(),
]);

return sendResponse(res, 200, "Categories fetched successfully", rows, {
totalCount: totalCount,
Expand All @@ -87,7 +90,7 @@ const getCategoryById = async (req, res) => {
try {
const { id } = req.params;

const category = await Category.findById(id);
const category = await Category.findById(id).lean();
if (!category) {
return res.status(404).json({
success: false,
Expand Down Expand Up @@ -202,7 +205,8 @@ const searchCategory = async (req, res) => {

const categories = await Category.find(filter)
.sort({ name: 1 })
.limit(10);
.limit(10)
.lean();

return sendResponse(res, 200, "Categories fetched successfully", categories);

Expand All @@ -228,7 +232,7 @@ const checkCategoryImageExistsDB = async (fileName, id = null) => {
filter._id = { $ne: id };
}

const exists = await Category.findOne(filter);
const exists = await Category.findOne(filter).select('_id').lean();

return {
success: true,
Expand Down
Loading
Loading