crud-api-express 1.1.7 → 1.1.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/readme.md +90 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crud-api-express",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
package/readme.md CHANGED
@@ -41,7 +41,7 @@ The `CrudController` class simplifies the creation of RESTful APIs in Node.js ap
41
41
 
42
42
  ## 🔧 Usage
43
43
 
44
- Here's a basic example of how to use `CrudController`:
44
+ Here's a basic example of how to use in Es module `CrudController`:
45
45
 
46
46
  ```javascript
47
47
  import express from 'express';
@@ -109,6 +109,95 @@ const exampleController = new CrudController(ExampleModel, 'examples', options);
109
109
 
110
110
  const mongoURI = 'mongodb://localhost:27017/mydatabase';
111
111
 
112
+ mongoose
113
+ .connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
114
+ .then(() => {
115
+ console.log('Connected to MongoDB');
116
+
117
+ const app = express();
118
+ app.use(express.json());
119
+ app.use('/api', exampleController.getRouter());
120
+
121
+ console.log(exampleController.getRoutes());
122
+
123
+ const PORT = process.env.PORT || 3000;
124
+ app.listen(PORT, () => {
125
+ console.log(`Server is running on port ${PORT}`);
126
+ });
127
+ })
128
+ .catch((err) => {
129
+ console.error('Error connecting to MongoDB:', err.message);
130
+ process.exit(1);
131
+ });
132
+ ```
133
+ Here's a basic example of how to use in cjs module `CrudController`:
134
+ ```javascript
135
+
136
+ const express = require('express');
137
+ const mongoose = require('mongoose');
138
+ const CrudController = require('crud-api-express');
139
+
140
+ const Schema = mongoose.Schema;
141
+ const ExampleSchema = new Schema(
142
+ {
143
+ type: { type: String, default: 'Percentage', enum: ['Percentage', 'Flat'] },
144
+ status: { type: String, default: 'Active', trim: true },
145
+ expiry_date: { type: Date, index: true, trim: true },
146
+ },
147
+ { timestamps: true, versionKey: false }
148
+ );
149
+
150
+ const ExampleModel = mongoose.model('Example', ExampleSchema);
151
+
152
+ const options = {
153
+ middleware: [
154
+ (req, res, next) => {
155
+ const authToken = req.headers.authorization;
156
+ if (!authToken) {
157
+ return res.status(401).json({ message: 'Unauthorized' });
158
+ }
159
+ next();
160
+ },
161
+ (req, res, next) => {
162
+ console.log(`Request received at ${new Date()}`);
163
+ next();
164
+ },
165
+ ],
166
+ onSuccess: (res, method, result) => {
167
+ console.log(`Successful ${method} operation:`, result);
168
+ res.status(200).json({ success: true, data: result });
169
+ },
170
+ onError: (res, method, error) => {
171
+ console.error(`Error in ${method} operation:`, error);
172
+ res.status(500).json({ error: error.message });
173
+ },
174
+ methods: ['GET', 'POST', 'PUT', 'DELETE'],
175
+ aggregatePipeline: [
176
+ { $match: { status: 'Active' } },
177
+ { $sort: { createdAt: -1 } },
178
+ ],
179
+ customRoutes: [
180
+ {
181
+ method: 'get',
182
+ path: '/custom-route',
183
+ handler: (req, res) => {
184
+ res.json({ message: 'Custom route handler executed' });
185
+ },
186
+ },
187
+ {
188
+ method: 'post',
189
+ path: '/custom-action',
190
+ handler: (req, res) => {
191
+ res.json({ message: 'Custom action executed' });
192
+ },
193
+ },
194
+ ],
195
+ };
196
+
197
+ const exampleController = new CrudController(ExampleModel, 'examples', options);
198
+
199
+ const mongoURI = 'mongodb://localhost:27017/mydatabase';
200
+
112
201
  mongoose
113
202
  .connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
114
203
  .then(() => {