1.0.5 • Published 2 years ago

@m92/mongo-odm v1.0.5

Weekly downloads
6
License
MIT
Repository
github
Last release
2 years ago

@m92/mongo-odm

npm version  License: MIT  Vulnerabilities: Snyk  Downloads

This is an ODM (Object Document Mapping) which provides CRUD functionalities to interact with MongoDB Collections. This package uses mongoose package using its v6.x.x version.

This package provides the following functionalities:

  • MongoDB Connection Helper
  • Mongoose Schema Wrapper
  • Collection Modeling Class

Table of Content

Installation

$ npm install --save @m92/mongo-odm

Environment Variables

The following environment variables need to be set to work with this package:

##### Mongo Config
export MONGO_HOSTS=
export MONGO_DBNAME=
export MONGO_USER_AUTH=false
export MONGO_USERNAME=
export MONGO_PASSWORD=
export MONGO_REPLICASET=
export MONGO_REPLICASET_COUNT=0
export MONGO_READ_PREFERENCE=
export MONGO_SSL_ENABLED=false
export MONGO_SSL_VALIDATE=false
export MONGO_PEM_PATH=
export MONGO_POOL_SIZE=5

Note: Do not export variable 'MONGO_READ_PREFERENCE' if no value is to be set.

Connecting to MongoDB

MongoDB needs to be connected before the 'MongoModel' methods can executed. The connection can be established as shown below:

import mongoConnect from '@m92/mongo-odm/mongoConnect'
await mongoConnect()

Creating a Collection Schema

import mongoSchemaWrapper from '@m92/mongo-odm/mongoSchemaWrapper'

const CollectionSchemaObject = {
    // Schema Properties as defined by mongoose Schema Class
}
const options = {}

const CollectionSchema = mongoSchemaWrapper(CollectionSchemaObject, options)
export default CollectionSchema

mongoSchemaWrapper() returns an instance of mongoose Schema Class.

Note: The 'options' object properties to be used is as defined by mongoose Schema Class. By default, mongoSchemaWrapper adds 'timestamps: true' option which can be overriden if needed. You may avoid passing the 'options' object if no extra options are required for a given Schema.

Using mongoose Schema Class

import { Schema } from '@m92/mongo-odm/mongoose'

const SubDocumentSchema = new Schema({
  // Schema Properties as defined by mongoose Schema Class
})

export default SubDocumentSchema

Creating a Collection Model

import MongoModel from '@m92/mongo-odm/MongoModel'
import CollectionSchema from './CollectionSchema.mjs'

const CollectionODM = new MongoModel('Collection', CollectionSchema)
export default CollectionODM

Properties of MongoModel Instance

PropertiesDescription
CollectionODM.ModelNameName of the Model
CollectionODM.Schemamongoose Collection Schema
CollectionODM.MongooseModelmongoose Model instance

Methods of MongoModel Instance

MethodDescription
CollectionODM.getCountReturns the count of Documents
CollectionODM.createOneCreates a new Document
CollectionODM.createManyCreates multiple new Documents
CollectionODM.replaceAllReplaces all Documents with new Documents
CollectionODM.findOneFinds and returns a single Document
CollectionODM.findManyFinds and returns mulitple Documents
CollectionODM.findByIdFinds using MongoDB ObjectId and returns a single Document
CollectionODM.findOneByFinds using key-value pair and returns a single Document
CollectionODM.findManyByFinds using key-value pair and returns mulitple Documents
CollectionODM.updateOneUpdates a single Document and returns the Updated Document
CollectionODM.updateManyUpdates multiple Documents and returns the Updated Documents
CollectionODM.updateByIdUpdates a single Document with spcified MongoDB ObjectId and returns the Updated Document
CollectionODM.updateOneByUpdates a single Document using key-value pair and returns the Updated Document
CollectionODM.updateManyByUpdates multiple Documents using key-value pair and returns the Updated Documents
CollectionODM.removeRemoves multiple documents and returns the delete response
CollectionODM.removeByIdDeletes a single Document with spicifed MongoDB ObjectId and returns the Deleted Document
CollectionODM.listReturns all the Documents from a given Collection
CollectionODM.searchSearches and returns Documents from a given Collection

CollectionODM.getCount(query)

Arguments
  • query (Object): mongoose query object
Returns
  • count (Number): Number of documents found
Example
const count = await CollectionODM.getCount({ ...queryProps })

CollectionODM.createOne(attrs)

Arguments
  • attrs (Object): Object as per CollectionSchema
Returns
  • document (Object): Created Lean Document
Example
const doc = await CollectionODM.createOne({ ...SchemaProps })

CollectionODM.createMany(attrs)

Arguments
  • attrs (Array): Array of Object as per CollectionSchema
Returns
  • documents (Array): Created Lean Documents Array
Example
const docs = await CollectionODM.createMany([{ ...SchemaProps }])

CollectionODM.replaceAll(attrs)

Arguments
  • attrs (Array): Array of Object as per CollectionSchema
Returns
  • documents (Array): Created Lean Documents Array
Example
const docs = await CollectionODM.replaceAll([{ ...SchemaProps }])

CollectionODM.findOne(query, projection, options)

Arguments
  • query (Object): mongoose query object
  • projection (Object): mongoose projection object
  • options (Object): mongoose options object as per 'findOne' method
Returns
  • document (Object): Lean Document
Example
const doc = await CollectionODM.findOne(query, projection, options)

CollectionODM.findMany(query, projection, options)

Arguments
  • query (Object): mongoose query object
  • projection (Object): mongoose projection object
  • options (Object): mongoose options object as per 'find' method
Returns
  • documents (Array): Lean Documents Array
Example
const docs = await CollectionODM.findMany(query, projection, options)

CollectionODM.findById(id, projection, options)

Arguments
  • id (String): mongoose Document '_id' Value
  • projection (Object): mongoose projection object
  • options (Object): mongoose options object as per 'findById' method
Returns
  • document (Object): Found Lean Document
Example
const doc = await CollectionODM.findById(id, projection, options)

CollectionODM.findOneBy(key, value, projection, options)

Arguments
  • key (String): mongoose Document Property Name
  • value (any): mongoose Document Property Query Value
  • projection (Object): mongoose projection object
  • options (Object): mongoose options object as per 'findOne' method
Returns
  • document (Object): Found Lean Document
Example
const doc = await CollectionODM.findOneBy(key, value, projection, options)

CollectionODM.findManyBy(key, value, projection, options)

Arguments
  • key (String): mongoose Document Property Name
  • value (any): mongoose Document Property Query Value
  • projection (Object): mongoose projection object
  • options (Object): mongoose options object as per 'findOne' method
Returns
  • documents (Array): Found Lean Documents Array
Example
const doc = await CollectionODM.findManyBy(key, value, projection, options)

CollectionODM.updateOne(query, updateObj, options)

Arguments
  • query (Object): mongoose query object
  • updateObj (Object): mongoose update object
  • options (Object): mongoose options object as per 'findOneAndUpdate' method
Returns
  • document (Object): Updated Lean Document
Example
const updatedDoc = await CollectionODM.updateOne(query, updateObj, options)

CollectionODM.updateMany(query, updateObj, options)

Arguments
  • query (Object): mongoose query object
  • updateObj (Object): mongoose update object
  • options (Object): mongoose options object as per 'updateMany' method
Returns
  • documents (Array): Updated Lean Documents Array
Example
const updatedDocs = await CollectionODM.updateMany(query, updateObj, options)

CollectionODM.updateById(id, updateObj, options)

Arguments
  • id (String): mongoose Document '_id' Value
  • updateObj (Object): mongoose update object
  • options (Object): mongoose options object as per 'findByIdAndUpdate' method
Returns
  • document (Object): Updated Lean Document
Example
const updatedDoc = await CollectionODM.updateById(id, updateObj, options)

CollectionODM.updateOneBy(key, value, updateObj, options)

Arguments
  • key (String): mongoose Document Property Name
  • value (any): mongoose Document Property Query Value
  • updateObj (Object): mongoose update object
  • options (Object): mongoose options object as per 'findOneAndUpdate' method
Returns
  • document (Object): Updated Lean Document
Example
const updatedDoc = await CollectionODM.updateOneBy(key, value, updateObj, options)

CollectionODM.updateManyBy(key, value, updateObj, options)

Arguments
  • key (String): mongoose Document Property Name
  • value (any): mongoose Document Property Query Value
  • updateObj (Object): mongoose update object
  • options (Object): mongoose options object as per 'updateMany' method
Returns
  • documents (Array): Updated Lean Documents Array
Example
const updatedDocs = await CollectionODM.updateManyBy(key, value, updateObj, options)

CollectionODM.remove(query, options)

Arguments
  • query (Object): mongoose query object
  • options (Object): mongoose options object as per 'deleteMany' method
Returns
  • removeResponse (Object): // TODO
Example
const removeResponse = await CollectionODM.remove(query, options)

CollectionODM.removeById(id, options)

Arguments
  • id (String): mongoose Document '_id' Value
  • options (Object): mongoose options object as per 'findByIdAndRemove' method
Returns
  • document (Object): Deleted Lean Document
Example
const doc = await CollectionODM.removeById(id, options)

CollectionODM.list(projection, options)

Arguments
  • projection (Object): mongoose projection object
  • options (Object): mongoose options object as per 'find' method
Returns
  • documents (Array): Found Lean Documents Array
Example
const docs = await CollectionODM.list(projection, options)

CollectionODM.search(query, projection, options)

Arguments
  • query (Object): mongoose query object
  • projection (Object): mongoose projection object
  • options (Object): mongoose options object as per 'find' method
Returns
  • responseData (Object): Found Lean Documents Array with Counts
    {
      "_meta": {
        "totalDocuments": 0,   // Total number of documents found against the 'query'
        "documentsCount": 0    // Number of documents found for given pagination options
      },
      "documents": []          // Lean Documents Array
    }
Example
const responseData = await CollectionODM.search(query, projection, options)
const queryDocsCount = responseData._meta.totalDocuments
const pageDocsCount = responseData._meta.documentsCount
const docs = responseData.documents

Resources

License

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.4.17

2 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.10

3 years ago

0.0.53

3 years ago

0.0.54

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.13

3 years ago

0.4.14

3 years ago

0.4.11

3 years ago

0.4.12

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.1.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.0.52

3 years ago

0.0.51

3 years ago

0.0.41

5 years ago

0.0.50

5 years ago

0.0.40

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago