crud-api-express 1.0.7 → 1.0.9

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/dist/index.js CHANGED
@@ -23,8 +23,6 @@ class CrudController {
23
23
  this.router.post(path, applyMiddleware(async (req, res) => {
24
24
  const method = 'POST';
25
25
  try {
26
- console.log("Request:", req);
27
- console.log("Request body:", req.body);
28
26
  const result = await this.model.create(req.body);
29
27
  if (options.relatedModel && options.relatedMethods?.includes('POST')) {
30
28
  await options.relatedModel.create({ [options.relatedField]: result._id, ...req.body });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crud-api-express",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
package/readme.md CHANGED
@@ -32,40 +32,83 @@ To use `CrudController` in your Node.js project, follow these steps:
32
32
  # Usage
33
33
  Here's a basic example of how to use CrudController:
34
34
 
35
-
36
35
  import express from 'express';
37
36
  import mongoose from 'mongoose';
38
- import CrudController from 'crud-api-express';
39
-
37
+ import CrudController from 'crud-api-express';
40
38
 
41
39
  const Schema = mongoose.Schema;
42
-
43
40
  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 },
41
+ type: { type: String, default: 'Percentage', enum: ['Percentage', 'Flat'] },
42
+ status: { type: String, default: 'Active', trim: true },
43
+ expiry_date: { type: Date, index: true, trim: true },
47
44
  }, { timestamps: true, versionKey: false });
48
45
 
49
46
  const ExampleModel = mongoose.model('Example', ExampleSchema);
50
47
 
51
-
52
- const exampleController = new CrudController(ExampleModel, 'examples', {
53
- // Optional configuration options here
54
- });
55
-
56
-
57
- const mongoURI = 'mdhd';
58
-
59
-
60
- mongoose.connect(mongoURI, { useNewUrlParser: false, useUnifiedTopology: false })
48
+ const options = {
49
+ middleware: [
50
+ (req, res, next) => {
51
+ // Example: Authentication middleware
52
+ const authToken = req.headers.authorization;
53
+ if (!authToken) {
54
+ return res.status(401).json({ message: 'Unauthorized' });
55
+ }
56
+ // Verify token logic here
57
+ next();
58
+ },
59
+ (req, res, next) => {
60
+ // Example: Logging middleware
61
+ console.log(`Request received at ${new Date()}`);
62
+ next();
63
+ }
64
+ ],
65
+ onSuccess: (res, method, result) => {
66
+ console.log(`Successful ${method} operation:`, result);
67
+ res.status(200).json({ success: true, data: result });
68
+ },
69
+ onError: (res, method, error) => {
70
+ console.error(`Error in ${method} operation:`, error);
71
+ res.status(500).json({ error: error.message });
72
+ },
73
+ methods: ['GET', 'POST', 'PUT', 'DELETE'],
74
+ relatedModel: RelatedModel,
75
+ relatedField: 'relatedId',
76
+ aggregatePipeline: [
77
+ { $match: { status: 'Active' } },
78
+ { $sort: { createdAt: -1 } }
79
+ ],
80
+ customRoutes: [
81
+ {
82
+ method: 'get',
83
+ path: '/custom-route',
84
+ handler: (req, res) => {
85
+ res.json({ message: 'Custom route handler executed' });
86
+ }
87
+ },
88
+ {
89
+ method: 'post',
90
+ path: '/custom-action',
91
+ handler: (req, res) => {
92
+ // Custom logic for handling POST request
93
+ res.json({ message: 'Custom action executed' });
94
+ }
95
+ }
96
+ ]
97
+ };
98
+
99
+ const exampleController = new CrudController(ExampleModel, 'examples', options);
100
+
101
+ const mongoURI = 'mongodb://localhost:27017/mydatabase';
102
+
103
+ mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
61
104
  .then(() => {
62
- console.log('Connected to MongoDB Atlas');
105
+ console.log('Connected to MongoDB');
63
106
 
64
107
  const app = express();
65
-
66
108
  app.use(express.json());
67
109
 
68
110
  app.use('/api', exampleController.getRouter());
111
+
69
112
  console.log(exampleController.getRoutes());
70
113
 
71
114
  const PORT = process.env.PORT || 3000;
@@ -74,7 +117,7 @@ mongoose.connect(mongoURI, { useNewUrlParser: false, useUnifiedTopology: false }
74
117
  });
75
118
  })
76
119
  .catch(err => {
77
- console.error('Error connecting to MongoDB Atlas:', err.message);
120
+ console.error('Error connecting to MongoDB:', err.message);
78
121
  process.exit(1);
79
122
  });
80
123
 
package/src/index.ts CHANGED
@@ -50,8 +50,6 @@ class CrudController<T extends Document> {
50
50
  this.router.post(path, applyMiddleware(async (req, res) => {
51
51
  const method = 'POST';
52
52
  try {
53
- console.log("Request:", req);
54
- console.log("Request body:", req.body);
55
53
  const result: any = await this.model.create(req.body);
56
54
  if (options.relatedModel && options.relatedMethods?.includes('POST')) {
57
55
  await options.relatedModel.create({ [options.relatedField!]: result._id, ...req.body });