A schema plugin that allows documents to have expiration dates, and defaults queries to respect these.
This plugin is meant to be used on a class of documents that is only valid for a certain period of time. That mainly means that the document might have a validFrom date (which means it is only valid AFTER the specific time) and an validUntil date (which means it is only valid until that date). Either of these values can be null, which means the document is valid indefinitely on that end.
When querying for documents that have this plugin, by default, documents will only be returned if they are currently valid.
const ExpiringSchemaPlugin = ;const ticketSchema = concertId: String ;ticketSchema;const TicketModel = mongoose;// Results will only contain tickets that are currently validconst results = await Model;// Results will only contain tickets that were valid on `someDate`const results = await Model;
- Note that you can use
validUntilto represent that there is no bound on when it starts being valid or when it stops being valid, respectively.
defaultValidFilter: You can specify a function that provides a different default query to determine if documents are valid or not. By default, all currently valid documents come back. The function takes a parameter that is a
Date() representing when "now" is for the query.
ExpiringSchemaPlugin contains a constant that has two options in it:
- VALID_FILTERS.CURRENT (default): Queries for docs current valid.
- VALID_FILTERS.CURRENT_AND_FUTURE (default): Queries for docs current or future valid.
Access these via
npm install mongoose-expiring-schema
Running the tests
Tests written using
ava framework. Run them using:
Designed for use in node environments, currently no build process. Supports node 8 and above.
Please feel free to fork and submit pull requests
- Makinde Adeagbo - makinde (github)
This project is licensed under the ISC License - see the LICENSE.md file for details