README
Norea.js Mongoose
Norea.js Mongoose is a package which contains a set of tools intended to facilitate the use of mongoose.
Installation
The package already has his type definitions.
npm install @noreajs/mongoose --save
Initial Features
- MongoDB initialization
- Model creation
- Extraction of errors during validations
MongoDB initialization
To use MongoDB in your application, initialization is required. To do so with this little baby package, here's how:
Import the MongoDB context:
import { MongodbContext } from "@noreajs/mongoose";
Then use this line of code to initialize:
MongodbContext.init({
connectionUrl: `MONGODB_CONNECTION_URL`,
options: {}, // optional
onConnect: () => {}, // optional
onError: () => {}, // optional
});
Model creation
Mongoose is the ideal tool when working with MongoDB. Very often to create a model, there is a set of information to take into account and the organization of the different elements constituting a model can become complicated. This package offers you a relatively simple way to proceed to create a model.
Full example of model declaration: Task.ts file (or Task.js):
import { mongooseModel, Document, Schema } from "@noreajs/mongoose";
interface ITask extends Document {
name: string;
description?: string;
createdAt: Date;
updatedAt: Date;
}
export default mongooseModel<ITask>({
name: "Task",
collection: "tasks",
schema: new Schema(
{
name: {
type: Schema.Types.String,
required: [true, "Task's name is required"],
},
description: {
type: Schema.Types.String,
},
},
{
timestamps: true, // createdAt and UpdatedAt are created and managed by mongoose
}
),
// + other properties
});
After creating the model, you can import it anywhere and use it.
Some details
The generic method used is mongooseModel
The parameter of method mongooseModel is of the generic type MoongooseModelParams
type MoongooseModelParams<T extends Document> = {
name: string;
collection?: string;
skipInit?: boolean;
paginate?: boolean;
aggregatePaginate?: boolean;
autopopulate?: boolean;
leanVirtuals?: boolean;
uniqueValidator?: boolean;
uniqueValidatorMessage?: string;
softDelete?: boolean;
softDeleteOptions?: {
deletedBy?: boolean;
deletedAt?: boolean;
overrideMethods?: boolean | string | string[];
validateBeforeDelete?: boolean;
indexFields?: boolean | string | string[];
use$neOperator?: boolean;
[key: string]: any;
};
onDeleteOptions: {
action?: "cascade" | "restrict" | "set_null" // default "restrict";
errorCb?: HookErrorCallback;
};
virtuals?: [
{
fieldName: string;
options?: any;
get: Function;
set?: Function;
}
];
protectOptions?: {
fillable?: Array<keyof T | string>;
guarded?: Array<keyof T | string>;
errorCb?: HookErrorCallback;
};
privacyOptions?: {
hidden?: Array<keyof T | string>;
visible?: Array<keyof T | string>;
errorCb?: HookErrorCallback;
};
postFilters?: {
[key in QueryMethod]?: (docs: T[]) => void;
};
methods: {
[K in keyof Partial<T>]: Function;
};
plugins?: (schema: Schema) => void;
schema: Schema<T>;
externalConfig?: (schema: Schema) => void;
};
MoongooseModelParams
Table made with Table Convert
Todos
- (more things will be added soon)
License
MIT