Josuto / monguito

Lightweight MongoDB Abstract Repository implementation for Node.js apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom collection name support

Josuto opened this issue Β· comments

Good day,

First and foremost, thank you for this excellent project. Has the possibility to let the implementation of MongooseRepository decide on the name of the underlying collection it will use, been explored before? I'd be happy to assist should you approve the idea.

Thanks again for monguito!

Best wishes,
Rijk

Originally posted by @rijkdw in #230

Hi Rijk!

It turns out that there is an easy way to provide a custom name for collections without having to perform any updates in Monguito's code base πŸ˜™. As you may know, Monguito wraps Mongoose to benefit from some resources such as Schema. The constructor of this type specifies collection as an optional parameter that you can use to define a name for the collection modelled by a schema. Here's an example of how you would do it:

export const BookSchema = extendSchema(
  BaseSchema,
  {
    title: { type: String, required: true },
    description: { type: String, required: false },
    edition: { type: Number, required: true },
  },
  { collection: 'AwesomeBooks' },
);

Please let me know if that answers your question 😊

Ahh, it does. I was aware of setting the collection name upon creation of the Model, but not of the Schema. Thank you @Josuto πŸ™‡β€β™‚οΈ

My pleasure, @rijkdw! Please keep on sharing any doubts or ideas you may have πŸ˜‰