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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/readme.md +43 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crud-api-express",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
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, { Request, Response } from 'express';
37
- import mongoose, { Document, Model } from 'mongoose';
38
- import CrudController, { CrudOptions } from 'crud-api';
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 your Mongoose schema and model
41
- interface ExampleModel extends Document {
42
- name: string;
43
- age: number;
44
- }
40
+ // Define Mongoose schema and model
41
+ const Schema = mongoose.Schema;
45
42
 
46
- const ExampleSchema = new mongoose.Schema<ExampleModel>({
47
- name: { type: String, required: true },
48
- age: { type: Number, required: true },
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<ExampleModel>('Example', ExampleSchema);
51
+ const ExampleModel = mongoose.model('Example', ExampleSchema);
52
52
 
53
53
  // Create an instance of CrudController
54
- const exampleController = new CrudController<ExampleModel>(ExampleModel, 'examples', {
55
- // Optional configuration
54
+ const exampleController = new CrudController(ExampleModel, 'examples', {
55
+ // Optional configuration options here
56
56
  });
57
57
 
58
- // Create Express application
59
- const app = express();
60
-
61
- // Mount the CrudController router
62
- app.use('/api/examples', exampleController.getRouter());
63
-
64
- // Start the server
65
- const PORT = process.env.PORT || 3000;
66
- app.listen(PORT, () => {
67
- console.log(`Server is running on port ${PORT}`);
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