services

Description
Service functions are used to make calls to your MongoDB database using your mongoose model.
Details

Members


<static> exports.getAllService

Description
Database query to fetch all documents of a collection.
Parameters
Name Type Description
model mongoose.Model Mongoose model for the collection being queried.
query object MongoDB or Mongoose query to filter out documents.
Returns
The matching documents as an array of objects if successful.
A mongoose error object if unsuccessful.
Examples
const { getAllService } = require("emfrest/services")

Usage

const arrayOfDocuments = await getAllService(model, query)
Details

<static> exports.createResourceService

Description
Database query to create a document.
Parameters
Name Type Description
model mongoose.Model Mongoose model for the collection being queried.
data object Object with fields to be created in the specified document.
Returns
The created document as an object if successful.
A mongoose error object if unsuccessful.
Examples
const { createResourceService } = require("emfrest/services")

Usage

const document = await createResourceService(model, newDocumentData)
Details

<static> exports.getOneByIdService

Description
Database query to fetch a document by it's ObjectId.
Parameters
Name Type Description
model mongoose.Model Mongoose model for the collection being queried.
id mongoose.ObjectId MongoDB ObjectId of the document to be fetched.
Returns
The document as an object if successful.
A mongoose error object if unsuccessful.
Examples
const { getOneByIdService } = require("emfrest/services")

Usage

const document = await getOneByIdService(model, documentObjectId)
Details

<static> exports.updateOneByIdService

Description
Database query to update a document by it's ObjectId.
Parameters
Name Type Description
model mongoose.Model Mongoose model for the collection being queried.
id mongoose.ObjectId MongoDB ObjectId of the document to be updated.
newData object Object with fields to be updated in the specified document.
Returns
The updated document as an object if successful.
A mongoose error object if unsuccessful.
Examples
const { updateOneByIdService } = require("emfrest/services")

Usage

const document = await updateOneByIdService(model, documentObjectId, newDocumentData)
Details

<static> exports.deleteByIdService

Description
Database query to delete a document by it's ObjectId.
Parameters
Name Type Description
model mongoose.Model Mongoose model for the collection being queried.
id mongoose.ObjectId MongoDB ObjectId of the document to be deleted.
Returns
An empty object if successful.
A mongoose error object if unsuccessful.
Examples
const { deleteByIdService } = require("emfrest/services")

Usage

const document = await deleteByIdService(model, documentObjectId)
Details