crud-api-express 1.0.1 → 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 +44 -28
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -33,40 +33,56 @@ 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
|
|
39
|
-
|
|
40
|
-
// Define your Mongoose schema and model
|
|
41
|
-
interface ExampleModel extends Document {
|
|
42
|
-
name: string;
|
|
43
|
-
age: number;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const ExampleSchema = new mongoose.Schema<ExampleModel>({
|
|
47
|
-
name: { type: String, required: true },
|
|
48
|
-
age: { type: Number, required: true },
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const ExampleModel = mongoose.model<ExampleModel>('Example', ExampleSchema);
|
|
36
|
+
import express from 'express';
|
|
37
|
+
import mongoose from 'mongoose';
|
|
38
|
+
import CrudController from 'crud-api-express'; // Path to your CrudController file
|
|
52
39
|
|
|
53
|
-
//
|
|
54
|
-
const
|
|
55
|
-
// Optional configuration
|
|
56
|
-
});
|
|
40
|
+
// Define Mongoose schema and model
|
|
41
|
+
const Schema = mongoose.Schema;
|
|
57
42
|
|
|
58
|
-
|
|
59
|
-
|
|
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 });
|
|
60
50
|
|
|
61
|
-
|
|
62
|
-
app.use('/api/examples', exampleController.getRouter());
|
|
51
|
+
const ExampleModel = mongoose.model('Example', ExampleSchema);
|
|
63
52
|
|
|
64
|
-
//
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
console.log(`Server is running on port ${PORT}`);
|
|
53
|
+
// Create an instance of CrudController
|
|
54
|
+
const exampleController = new CrudController(ExampleModel, 'examples', {
|
|
55
|
+
// Optional configuration options here
|
|
68
56
|
});
|
|
69
57
|
|
|
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
|
+
});
|
|
85
|
+
|
|
70
86
|
|
|
71
87
|
# API
|
|
72
88
|
|