mongosuper
Version:
mongosuper is a superset of mongoose. It is a wrapper around mongoose. It manage your mongoose connection and keep it alive always. It also provide you some extra features like CRUD operations, etc.
60 lines • 3.04 kB
JavaScript
/* This File is used to Create Data in Database. */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Create = void 0;
/**
* This is an asynchronous function that creates a new document in a database using the provided data
* and model, and returns a success or failure message along with the new data.
* @param data - The data parameter is an object that contains the information to be saved to the
* database. It is used to create a new document in the database.
* @param model - The "model" parameter is likely a Mongoose model that represents a specific
* collection in a MongoDB database. It is used to create a new document in the collection based on the
* "data" parameter passed to the function.
* @returns an object with properties `status`, `message`, `NewCount`, and `NewData`. The values of
* these properties depend on the outcome of the function. If the document creation is successful,
* `status` is `true`, `message` is `"Successfully Created Data"`, `NewCount` is the length of the
* array containing the newly created document (which is 1),
*/
function Create(data, model) {
return __awaiter(this, void 0, void 0, function* () {
try {
const result = yield model.create(data); // Create the document and save it to the database.
return {
status: true,
message: "Successfully Created Data",
NewCount: 1,
NewData: [result]
};
}
catch (err) {
console.error(err); // Log the error for debugging purposes
return {
status: false,
message: "Failed to Create Data",
NewCount: 0,
NewData: []
};
}
});
}
exports.Create = Create;
});
//# sourceMappingURL=Create.js.map