crud-api-express 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +43 -27
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# CRUD API Controller using Express and Mongoose
|
|
2
2
|
|
|
3
|
-
npm install crud-api
|
|
3
|
+
npm install crud-api-express
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
This project provides a flexible and reusable CRUD (Create, Read, Update, Delete) API controller for MongoDB using Express.js and Mongoose.
|
|
@@ -33,39 +33,55 @@ To use `CrudController` in your Node.js project, follow these steps:
|
|
|
33
33
|
Here's a basic example of how to use CrudController:
|
|
34
34
|
|
|
35
35
|
|
|
36
|
-
import express
|
|
37
|
-
import mongoose
|
|
38
|
-
import CrudController
|
|
36
|
+
import express from 'express';
|
|
37
|
+
import mongoose from 'mongoose';
|
|
38
|
+
import CrudController from 'crud-api-express'; // Path to your CrudController file
|
|
39
39
|
|
|
40
|
-
// Define
|
|
41
|
-
|
|
42
|
-
name: string;
|
|
43
|
-
age: number;
|
|
44
|
-
}
|
|
40
|
+
// Define Mongoose schema and model
|
|
41
|
+
const Schema = mongoose.Schema;
|
|
45
42
|
|
|
46
|
-
const ExampleSchema = new
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
43
|
+
const ExampleSchema = new Schema({
|
|
44
|
+
title: { type: String, required: true, trim: true },
|
|
45
|
+
type: { type: String, default: 'Percentage', enum: ['Percentage', 'Flat'] },
|
|
46
|
+
value: { type: Number, required: true },
|
|
47
|
+
status: { type: String, default: 'Active', trim: true },
|
|
48
|
+
expiry_date: { type: Date, index: true, trim: true },
|
|
49
|
+
}, { timestamps: true, versionKey: false });
|
|
50
50
|
|
|
51
|
-
const ExampleModel = mongoose.model
|
|
51
|
+
const ExampleModel = mongoose.model('Example', ExampleSchema);
|
|
52
52
|
|
|
53
53
|
// Create an instance of CrudController
|
|
54
|
-
const exampleController = new CrudController
|
|
55
|
-
// Optional configuration
|
|
54
|
+
const exampleController = new CrudController(ExampleModel, 'examples', {
|
|
55
|
+
// Optional configuration options here
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
-
//
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
app
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
// MongoDB Atlas connection URI
|
|
59
|
+
const mongoURI = 'mongodb+srv';
|
|
60
|
+
|
|
61
|
+
// Connect to MongoDB Atlas
|
|
62
|
+
mongoose.connect(mongoURI, { useNewUrlParser: false, useUnifiedTopology: false })
|
|
63
|
+
.then(() => {
|
|
64
|
+
console.log('Connected to MongoDB Atlas');
|
|
65
|
+
// Start the Express server after successful connection
|
|
66
|
+
const app = express();
|
|
67
|
+
|
|
68
|
+
// Middleware to parse JSON bodies
|
|
69
|
+
app.use(express.json());
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
// Mount the CrudController router
|
|
73
|
+
app.use('/api', exampleController.getRouter());
|
|
74
|
+
|
|
75
|
+
// Start the server
|
|
76
|
+
const PORT = process.env.PORT || 3000;
|
|
77
|
+
app.listen(PORT, () => {
|
|
78
|
+
console.log(`Server is running on port ${PORT}`);
|
|
79
|
+
});
|
|
80
|
+
})
|
|
81
|
+
.catch(err => {
|
|
82
|
+
console.error('Error connecting to MongoDB Atlas:', err.message);
|
|
83
|
+
process.exit(1); // Exit process with failure
|
|
84
|
+
});
|
|
69
85
|
|
|
70
86
|
|
|
71
87
|
# API
|