JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q41564F
  • License MIT

Package Exports

  • mongoose-backpack
  • mongoose-backpack/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongoose-backpack) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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}`);
});