npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@noreajs/mongoose

v1.0.2

Published

Mongoose helpers

Downloads

44

Readme

Norea.js Mongoose

Norea.js Mongoose is a package which contains a set of tools intended to facilitate the use of mongoose.

Version

Downloads/week

License

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, Where T is the interface which describes the model, and which must extend the mongoose Document interface.

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 descriptions: | Attribute | Type | Optional | Default | Description | |------------------------|------------------|----------|----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | name | string | false | | Model name | | collection | string | true | | Collection name (optional, induced from model name) | | skipInit | boolean | true | false | Whether to skip initialization or not | | paginate | boolean | true | true | The plugin automatically added when true is Mongoose Paginate | | aggregatePaginate | boolean | true | true | The plugin automatically added when true is Mongoose Aggregate Paginate V2 | | autopopulate | boolean | true | true | Always populate() certain fields in your Mongoose schemas. Only apply this plugin to top-level schemas. Don't apply this plugin to child schemas. | | leanVirtuals | boolean | true | true | Attach virtuals to the results of Mongoose queries when using .lean(). | | uniqueValidator | boolean | true | true | Catch unique validation error like normal validation error | | uniqueValidatorMessage | string | true | Expected {PATH} to be unique. | Unique validator message You can pass through a custom error message as part of the optional options argument: You have access to all of the standard Mongoose error message templating: :{PATH}, {VALUE}, {TYPE} | | softDelete | boolean | true | false | Active soft delete on model | | softDeleteOptions | any | true | | Soft delete options | | onDeleteOptions | any | true | | On record delete options | | plugins | function | true | | Add globally a plugin to a mongoose schema | | schema | mongoose.Schema | true | | Mongoose schema defining the model | | virtuals | array | true | | Define the virtual attributes of the model | | methods | object | true | | Define the methods associated with the models | | externalConfig | function | true | | Configure the schema created by adding methods, middlewares, virtuals and many other things provided by Mongoose - Methods - https://mongoosejs.com/docs/guide.html#methods - Middlewares - https://mongoosejs.com/docs/middleware.html - Virtuals - https://mongoosejs.com/docs/guide.html#virtuals | | protectOptions | object | true | | Prevent mass assignment on some attributes | | privacyOptions | object | true | | hide or display some attributes while fetching data | | postFilters | object | true | | Filters to apply on post middleware |

Table made with Table Convert

Todos

  • (more things will be added soon)

License

MIT