1.1.51 β€’ Published 3 years ago

mongo-quick v1.1.51

Weekly downloads
108
License
ISC
Repository
github
Last release
3 years ago

mongo-quick

Quick start with mongodb, right now.

Table of Contents

The motive of this library is to just jump start with mongoose without much crucial logging for saving data, deleting collections, making models, etc.

To request feature for this library or any questions please create respective issue here https://github.com/sahilrajput03/mongo-quick/issues/new .

Quick tip 1: Install this library via, npm i mongo-quick

Quick tip 2: Run below command to add db's address to .env file

echo MONGO_DB_URI=mongodb://localhost/testdb >> .env You can specify any your own atlas or mongo database server (mongoDB) running on somewhere else.

New release with no breaking changes plus typescript support now πŸ˜‰

Quick tip 3: Add "type": "module" in root object to your package.json file

Quick tip 4: Add start script node.exe --no-warnings index.js in package.json file.

Quick tip 5: Add dev script nodemon -q -x node.exe --no-warnings index.js in package.json file.

Now you don't need to make it difficult to start using this πŸ‘‰awesome library. You just install and start using mongo-quick in any existing project.

Alert

  • Define a .env file and put MONGO_DB_URI=mongodb://localhost/testdb there(otherwise it won't connect to mongodb). I have set it up with local db, but you can use any online/offline mongodb

  • You don't need to install mongoose or dotenv, they are already inscribed this mongo-quick library.

Usage

There are examples below using respective features from mongo-quick library .

Quick tip 3: Remember methods having LazyπŸ˜‚ in their names tells that you need to call them by prefixing them with await keyword. :D

Quick tip 4: Remember methods having InLogπŸ˜‚ in their names tells that you'll receive nice logs about the execution/results of those statements in console. :D

For non-typescript users

Start quick with a new npm project from scratch, like this

mkdir quick-learnings && cd quick-learnings && npm init -y
npm i mongo-quick
npm i -g nodemon
echo MONGO_DB_URI=mongodb://localhost/testdb >> .env
echo console.log('Paste any example from `mongo-quick` docs in this file.') >> server.js
nodemon --quiet server.js
echo Thanks for trying mongo-quick.

Paste any of the below Examples in server.js file, and see mongo-quick in action.

For typescript users

Start quick with a new typescript project from scratch, like this

mkdir quick-learnings && cd quick-learnings && npm init -y
npm i -D typescript
npm i -g ts-node-dev
npm i mongo-quick
echo MONGO_DB_URI=mongodb://localhost/testdb >> .env
tsc --init
echo console.log('Paste any example from `mongo-quick` docs in this file.') >> server.ts
ts-node-dev --respawn --clear server.ts
echo Thanks for trying mongo-quick.

Note: As I quote from ts-node-dev docs:

"The good thing is that ts-node-dev watches used tsconfig.json file, and will reinitialize compilation on its change, but you have to restart the process manually when you update used version of typescript or make any other changes that may effect compilation results."

So, after editing your tsconfig.json file, you must manually restart the ts-node-dev server.

Configure tsconfig.json file like that -

  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "outDir": "./lib"
    ...other default config from typescript.
  }

Put any of the below Examples in server.ts file, and use to see mongo-quick in action. Or you may simply use,tsnd server.js to run though.

Tip (tsnd is just shorthand for ts-node-dev).

Example 0

import { connectMongoDb_Lazy_InLog, saveToCollection_Lazy, useCollection, deleteCollection_Lazy_InLog, closeConnection, saveToCollection_Lazy_Piped } from "mongo-quick";
// You can use destructuring styled named imports too, but I recommend serving all of them just from a single default export just like I have done in below examples via `mq`.

Example 1

const mq = require("mongo-quick");
// import mq from "mongo-quick"; //For typescript users.

(async function () {
  const itemCollection2020 = mq.useCollection("itemCollection2020", {
    itemModel: String,
    itemAuthor: String,
    // So, ^^^^^^ here goes your mongoose schema for a model.
  });
  // itemCollection2020 is nothing but the model returned by mongoose.model() method.

  console.log(await mq.connectMongoDb_Lazy_InLog());
  // Tip: This automatically picks the database uri of MONGO_DB_URI from .env file.

  await mq.saveToCollection_Lazy(itemCollection2020, {
    itemModel: "Cadbury - Dairy Milk",
    itemAuthor: "Nestle",
    // So, ^^^^^^ here goes the data you want to save to the collection in mongodb.
  });

  console.log(await mq.deleteCollection_Lazy_InLog(itemCollection2020));
  // Deletes all documents in the collection and logs the deletion process info after that.

  mq.closeConnection();
  // This will simply close the mongodb connection.
})();

Example 2

const mq = require("mongo-quick");
// import mq from "mongo-quick"; //For typescript users.

(async function () {
  const itemCollection2020 = mq.useCollection("itemCollection2020", {
    Dimensions: Array,
    Author: String,
    Specialization: String,
    Angle: Object,
    Sides: Object,
    Edges: Number,
    Roundness: String,
    // Note order is IMPORTANT, coz piped method will save the data with same order of the keys of the schema. :)
    // Try keeping Arrays at top, coz mongodb does that too,  you can obviously ignore this tip though.
  });

  console.log(await mq.connectMongoDb_Lazy_InLog());

  await mq.saveToCollection_Lazy_Piped(itemCollection2020, [22, 33, 444], "Ramanujan", "Mathematician", { x: 20, y: 30, z: 50 }, { x: 10, y: 8 }, 4, "Completely Round");

  mq.closeConnection();
})();

Example 3

const mq = require("mongo-quick");
// import mq from "mongo-quick"; //For typescript users.

(async function () {
  const itemCollection2020 = mq.useCollection("itemCollection2020");

  console.log(await mq.connectMongoDb_Lazy_InLog());

  await mq.saveToCollection_Lazy(itemCollection2020, {
    quality: "top notch",
    age: "ancient-era",
    style: [1, 2, 3, 4],
    behaviour: { ca: 20, ma: 40 },
    sharpness: 44,
    coolness: true,
  });
  // console.log(await mq.deleteCollection_Lazy_InLog(itemCollection2020))

  mq.closeConnection();
})();

Example 4

const mq = require("mongo-quick");
// import mq from "mongo-quick"; //For typescript users.

(async function () {
  console.log(await mq.connectMongoDb_Lazy_InLog());

  await mq.saveToCollection_Lazy("itemCollection2020", {
    // Adding data to collection by ^^^ providing collection name as parameter.
    quality: "top notch",
    age: "ancient-era",
    style: [1, 2, 3, 4],
    behaviour: { ca: 20, ma: 40 },
    sharpness: 44,
    coolness: true,
  });

  mq.closeConnection();
})();

Example 5

const mq = require("mongo-quick");
// import mq from "mongo-quick"; //For typescript users.

(async function () {
  console.log(await mq.connectMongoDb_Lazy_InLog());

  console.log(await mq.deleteCollection_Lazy_InLog("itemCollection2020"));
  // Deleting collection by passing collection name as ^^^ parameter.

  mq.closeConnection();
})();

Happy hackings!

1.1.51

3 years ago

1.1.50

3 years ago

1.1.49

4 years ago

1.1.48

4 years ago

1.1.44

4 years ago

1.1.47

4 years ago

1.1.46

4 years ago

1.1.43

4 years ago

1.1.42

4 years ago

1.1.41

4 years ago

1.1.40

4 years ago

1.1.38

4 years ago

1.1.37

4 years ago

1.1.36

4 years ago

1.1.39

4 years ago

1.1.33

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.33

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.26

4 years ago

1.0.27

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.19

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.9

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago