crud-api-express 1.0.1 → 1.0.3
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 +38 -27
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -33,41 +33,52 @@ 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
|
-
});
|
|
36
|
+
import express from 'express';
|
|
37
|
+
import mongoose from 'mongoose';
|
|
38
|
+
import CrudController from 'crud-api-express';
|
|
50
39
|
|
|
51
|
-
const ExampleModel = mongoose.model<ExampleModel>('Example', ExampleSchema);
|
|
52
40
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
41
|
+
const Schema = mongoose.Schema;
|
|
42
|
+
|
|
43
|
+
const ExampleSchema = new Schema({
|
|
44
|
+
type: { type: String, default: 'Percentage', enum: ['Percentage', 'Flat'] },
|
|
45
|
+
status: { type: String, default: 'Active', trim: true },
|
|
46
|
+
expiry_date: { type: Date, index: true, trim: true },
|
|
47
|
+
}, { timestamps: true, versionKey: false });
|
|
57
48
|
|
|
58
|
-
|
|
59
|
-
const app = express();
|
|
49
|
+
const ExampleModel = mongoose.model('Example', ExampleSchema);
|
|
60
50
|
|
|
61
|
-
// Mount the CrudController router
|
|
62
|
-
app.use('/api/examples', exampleController.getRouter());
|
|
63
51
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
app.listen(PORT, () => {
|
|
67
|
-
console.log(`Server is running on port ${PORT}`);
|
|
52
|
+
const exampleController = new CrudController(ExampleModel, 'examples', {
|
|
53
|
+
// Optional configuration options here
|
|
68
54
|
});
|
|
69
55
|
|
|
70
56
|
|
|
57
|
+
const mongoURI = 'mongodb+srv://mukesh:Umgtr1NgoQXcRccg@cluster0.zgxjwge.mongodb.net/test';
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
mongoose.connect(mongoURI, { useNewUrlParser: false, useUnifiedTopology: false })
|
|
61
|
+
.then(() => {
|
|
62
|
+
console.log('Connected to MongoDB Atlas');
|
|
63
|
+
|
|
64
|
+
const app = express();
|
|
65
|
+
|
|
66
|
+
app.use(express.json());
|
|
67
|
+
|
|
68
|
+
app.use('/api', exampleController.getRouter());
|
|
69
|
+
|
|
70
|
+
const PORT = process.env.PORT || 3000;
|
|
71
|
+
app.listen(PORT, () => {
|
|
72
|
+
console.log(`Server is running on port ${PORT}`);
|
|
73
|
+
});
|
|
74
|
+
})
|
|
75
|
+
.catch(err => {
|
|
76
|
+
console.error('Error connecting to MongoDB Atlas:', err.message);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
71
82
|
# API
|
|
72
83
|
|
|
73
84
|
model: Mongoose model for CRUD operations.
|