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

mongoose-email-address-manager

v2.0.0

Published

Manage multiple email addresses per user (or other model/doc) with this mongoose schema plugin.

Downloads

1

Readme

Mongoose Email Address Manager

Manage multiple email addresses per user (or other model/doc) with this mongoose schema plugin.

Something like...

{
    email_addresses: [
        {
            email_address: {type: String, unique: unique},
            primary: Date,
            verification: {
                date: Date,
                code: String,
                code_expiration: Date
            }
        }
    ]
}

Install it...

npm install mongoose-email-address-manager

Plug it in... (defaults are shown)

var db = require('mongoose'),
    emailAddressManagerPlugin = require('mongoose-email-address-manager'),
    UserSchema = db.Schema(),
    options = {
        _id: true, // removes _id fields from email_address sub docs when set to false
        index: true, // sets indexes on emails and verification code
        unique: true, // throws a mongo error if emails are not unique
        sparse: true, // sparse or not
        required: false, // verifies that at least one email sub doc exists
        verificationCodePrefix: 'emvc-', // useful for web pages that deal with more than one type of access code / validation code (like mobile numbers)
        verificationCodeExpiration: 0, // the amount of hours to add to the expiration date of validation code, defaults to never expire (0)
        emailValidationRegex: /^.+@.+$/, // very simple regex validator for email addresses, overwrite if you want something more powerful :)
        style: function (key) { // keeping you inline with your style guide, if you require camelCase or Snake_Case or so be it... override your keys with this function, default being under_score
          return key
        }
    };

    UserSchema.plugin(emailAddressManagerPlugin, options);

Make the model...

var User = db.model('user', UserSchema);

Make the instance or find the instance with the statics below ...

var user = new User({
    email_addresses: [{
        email_address: '[email protected]'
    }]
});

Use the statics...

User.findOneByEmail - like mongooses findOne except the first arg is an email_address instead of a condition
User.findByEmail - same here... this is useful if unique is set to false and more than one doc has the same email
User.findByEmailVerificationCode - find the doc by one of it's email verification codes
User.emailExists - use this to check if email exists within entire collection so you can quickly throw that "email already exists" error on your signup forms or something with that cool new API

Empower your self with easy methods on the instance...

user.addEmail // add a new email
user.addEmailAndSave
user.getEmail // find the email object within the doc
user.getPrimaryEmail // returns the email object
user.setPrimaryEmail // changes or sets the primary email address (will not save)
user.setPrimaryEmailAndSave // sets the primary email and saves the doc
user.isPrimaryEmail // is it?
user.removeEmail // removes an email address (will not save)
user.removeEmailAndSave
user.emailExists // do we have the email in the doc? It's always a yes or no answer
user.startEmailVerificationProcess // this configures and saves some stuff like a verification code and expiration date for the email (will not save)
user.startEmailVerificationProcessAndSave
user.getEmailByVerificationCode // if you don't have the doc in memory find it first with the static: findByEmailVerificationCode
user.setVerifiedEmail // set an email as verified (will not save)
user.setVerifiedEmailAndSave
user.isVerifiedEmail // returns boolean on rather or not that email address has been verified

Use the one and only extremely powerful virtual field that hands down that primary email address at all times unless it's undefined

user.email; // the primary email or equivalent of user.getPrimaryEmail().email_address

Misc Notes

On creation/save the first email will become primary pre save if there is not a current primary email address unless there are no email addresses. On removal of an email address a new primary will be elected Methods that interact with the DB are Async/Await/Promise compatible (callbacks not currently supported)

Please refer to the tests for further use.

Found a bug? Let me know or submit a PR...

MIT