-
-
Save elliotf/4750628 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose') | |
, Schema = mongoose.Schema | |
mongoose.connect('mongodb://localhost/identities'); | |
var IdentitySchema = new Schema({ | |
type : { type: String, enum: ['twitter', 'facebook', 'google'] } | |
, uid : { type: String } | |
}) | |
var UserSchema = new Schema({ | |
identities : [ IdentitySchema ] | |
}) | |
UserSchema.index({ "identities.type": 1, "identities.uid" : 1 }, { unique: true }) | |
var User = mongoose.model('User', UserSchema) | |
// clean up db before tests | |
User.remove({}, function(){ | |
// creates an user from identities array | |
function testUserInsert(){ | |
var user = new User() | |
, identities = [].slice.call(arguments) | |
identities.forEach(function(id){ | |
user.identities.push(id) | |
}) | |
user.save(function(err){ | |
if (err){ | |
console.log("error creating user\n", identities, "\n", err.message) | |
} else { | |
console.log("user created\n", identities) | |
} | |
}) | |
} | |
// multiple ids | |
testUserInsert( | |
{ uid: 'Jim', type: 'twitter' } | |
, { uid: 'Jim', type: 'facebook' } | |
) | |
// different service, ok | |
testUserInsert( | |
{ uid: 'Jim', type: 'google' } | |
) | |
// same service, should raise an error | |
testUserInsert( | |
{ uid: 'Jim', type: 'twitter' } | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment