mongoose-backpack

0.2.7 • Public • Published
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

// ...

const { appendParams } = require("mongoose-backpack");

// ...
// ...

const userSchema = new Schema(
  {
    first_name: {
      type: String,
      required: true,
    },
    last_name: {
      type: String,
      required: true,
    },
    email: {
      type: String,
      required: true,
    },
    // ...
    // ...
  },
  { timestamps: true }
);

// ...
// ...

appendData = {
  id: function () {
    return this._id;
  },
  full_name: function () {
    return this.first_name + " " + this.last_name;
  },
};
appendParams(userSchema, appendData);

// ...
// ...

module.exports = mongoose.model("User", userSchema);
const express = require("express");
const mongoose = require("mongoose");
const { mongooseRouteName } = require("mongoose-backpack");

// Create a new Express app
const app = express();

// Connect to the MongoDB Atlas cluster
mongoose
  .connect(
    "mongodb+srv://jaykumargohil:#####@jkgmongo.9bdgpey.mongodb.net/demo",
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
  )
  .then(() => {
    console.log("Connected to MongoDB Atlas");
  })
  .catch((err) => {
    console.error("Error connecting to MongoDB Atlas", err);
  });

// Define a schema for the "message" collection
const messageSchema = new mongoose.Schema({
  name: { type: String, required: true },
  message: { type: String, required: true },
  image: { type: String, required: false },
  file: { type: String, required: false },
});

// Create a model for the "message" collection
const Message = mongoose.model("Message", messageSchema);

app.use("/api", mongooseRouteName("people", Message));

// Start the Express app
const port = process.env.PORT || 3002;
app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

Package Sidebar

Install

npm i mongoose-backpack

Weekly Downloads

69

Version

0.2.7

License

MIT

Unpacked Size

8.61 kB

Total Files

4

Last publish

Collaborators

  • jksk21