Skip to content

Instantly share code, notes, and snippets.

@awilson28
Last active March 1, 2016 22:16
Show Gist options
  • Save awilson28/9b2210eedf9114b636e4 to your computer and use it in GitHub Desktop.
Save awilson28/9b2210eedf9114b636e4 to your computer and use it in GitHub Desktop.
'use strict';
var crypto = require('crypto');
var mongoose = require('mongoose'),
extend = require('mongoose-schema-extend');
var Schema = mongoose.Schema;
/*
Example of how to use mongoose-schema extend
User documents and AuthUser documents will be in the same collection/table
but if we query for users --- User.find({type: 'Users'}) --- this will return only users whose
discriminatorKey (type) is User
so user documents will have a `type` field that points to the string "User"
because we name the model "User"
u.type === 'User'
same for authUsers --- they will have a `type` field that points to "AuthUser"
to get only authUsers, we must do AuthUser.find({type: 'AuthUsers'})
because remember, one of the KEY features of mongoose extend is that we can create
two different types of a model (users and authusers in this case) and yet allow
them to share the SAME collection! Cool! Querying ease!
in this case, `User` is the base class
*/
var User = new Schema({
firstName: {type: String, required: true},
lastName: {type: String, required: true},
email: {type: String, required: true, unique: true},
address: {
street: {type: String},
city: {type: String},
state: {type: String},
zip: {type: String}
}
}, {collection: 'users', discriminatorKey: 'type'});
var authUser = User.extend({
password: String,
salt: String,
roles: [String],
twitter: {id: String, username: String, token: String, tokenSecret: String},
facebook: {id: String},
google: {id: String},
needPwReset: {type: Boolean, defeault: false},
inactive: {type: Boolean, default: false}
});
mongoose.model('User', User);
mongoose.model('AuthUser', authUser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment