Skip to content

Instantly share code, notes, and snippets.

@davidsierradz
Last active July 22, 2019 16:50
Show Gist options
  • Save davidsierradz/b82bcab1d4bb1e8ccefbdfc8eb02b9dd to your computer and use it in GitHub Desktop.
Save davidsierradz/b82bcab1d4bb1e8ccefbdfc8eb02b9dd to your computer and use it in GitHub Desktop.
`mongoose-paginate-v2` and `mongoose-delete`

mongoose-paginate-v2 and mongoose-delete

Requeriments

  • MongoDB: 4
  • node.js: v11.15.0

Instructions

  1. Start a mongodb container:
docker run --name some-mongo -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=secret --publish "27017:27017" mongo:4
  1. Install the example:
yarn install # or npm install
  1. Run the example:
node index.js
const mongoose = require("mongoose");
const mongoose_delete = require("mongoose-delete");
const mongoosePaginate = require("mongoose-paginate-v2");
mongoose.connect("mongodb://localhost/test?authsource=admin", {
auth: {
user: "root",
password: "secret"
},
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", async function() {
const kittySchema = new mongoose.Schema({
name: String
});
kittySchema.plugin(mongoose_delete, {
overrideMethods: ["find", "findOne"]
});
kittySchema.plugin(mongoosePaginate);
const Kitten = mongoose.model("Kitten", kittySchema);
const fluffy = new Kitten({ name: "Fluffy" });
// await fluffy.save()
// await fluffy.delete()
console.log(await Kitten.findWithDeleted())
console.log(await Kitten.paginate(Kitten.findWithDeleted()));
});
{
"name": "paginate-test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"mongoose": "^5.6.5",
"mongoose-delete": "^0.5.0",
"mongoose-paginate-v2": "^1.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment