简体   繁体   中英

Document Polymorphism in a MongoDb collection using Node.js / MongooseJs -

I need to maintain a collection 'users' of [User] documents for implementing an authentication mechanism for my site.

[User] ~ {color: string, username:string, password-hash:string, password-salt: string}

Additionally, each [User] can be one of 3 colors - [RedUser] , [BlueUser] and [GreenUser] and will have differing schemas depending on color. All extending from the [User] schema:

[RedUser] ~ Union( [User],  {redfield:string} )
[GreenUser] ~ Union( [User],  {greenfield:boolean} )
[BlueUser] ~ Union( [User],  {bluefield:number} )

eg a green user - {color:'green',username:'bob',password-hash:'1313a...',password-salt:'...'}

For authentication I want to query the 'users' collection using the general [User] schema, as all I need to know is the password-hash.

However, I would like a function as follows (pseudocode):

Authenticate( username, password )
    userDoc <- users.findOne({username:username})
    if badPassword(usersDoc.password-hash,password) throw error
    else
        if userDoc.color=='red'
            return recast(userDoc, RedUser)
        if userDoc.color=='green'
            return recast(userDoc, BlueUser)
        if userDoc.color=='blue'
            return recast(userDoc, GreenUser)

So how could I do the up-casting from a [User] document to a [RedUser] for example?

I am using Node.js/MongooseJs/Coffeescript

thanks!

Pretty simple really, we just make use of the tiny but useful mongoose-schema-extend package from https://github.com/briankircho/mongoose-schema-extend

###
    Dependencies.
###

mongoose = require('mongoose')
extend = require('mongoose-schema-extend')

###
    Main
### 

class Demo
    constructor: (dbUrl)->
        @db = mongoose.createConnection( dbUrl )

        userSchema = new mongoose.Schema
            username                    : { type: String, default: "" }                 
            password_hash               : { type: String, default: "" }     
            ,
            {collection : 'users',  discriminatorKey : 'color'}

        userSchema.methods.verifyPassword = (pass)->
            console.log("is it a good password? " + (this.password_hash==pass))

        redSchema = userSchema.extend
            redfield                : { type: String, default: "red" }          
        redSchema.methods.printColor = ->
            console.log("RED!") 

        blueSchema = userSchema.extend
            bluefield           : { type: String, default: "blue" }             
        blueSchema.methods.printColor = ->
            console.log("BLUE!")        

        greenSchema = userSchema.extend
            greenfield          : { type: String, default: "green" }                
        greenSchema.methods.printColor = ->
            console.log("GREEN!")               

        # create the mongoose db models
        @UserModel = @db.model('User', userSchema)
        @RedModel = @db.model('Red', redSchema)
        @BlueModel = @db.model('Blue', blueSchema)      
        @GreenModel = @db.model('Green', greenSchema)

    populate: ->
        user1 = new @RedModel({username:'user1',password_hash:'pass1',redfield:'blabla'})
        user1.save()
        user2 = new @GreenModel({username:'user2',password_hash:'pass2',greenfield:'blabla2'})
        user2.save()    
        user3 = new @RedModel({username:'user3',password_hash:'pass3',redfield:'blabla3'})
        user3.save()        
        user4 = new @GreenModel({username:'user4',password_hash:'pass1',greenfield:'dflabla'})
        user4.save()
        user5 = new @BlueModel({username:'user5',password_hash:'pass2',bluefield:'blabla2'})
        user5.save()    
        user6 = new @BlueModel({username:'user6',password_hash:'pass3',bluefield:'blabla3'})
        user6.save()                

    auth:(username,callback)->
        @UserModel.findOne({ username: username }, callback)

###
    Start the demo
###

demo = new Demo('mongodb://localhost/testpolymorph')
demo.populate()
demo.auth 'user5', (err,userDoc)->  
    userDoc.verifyPassword("pass")
    userDoc.printColor()
demo.auth 'user3', (err,userDoc)->
    userDoc.verifyPassword("pass3") 
    userDoc.printColor()
demo.auth 'user4', (err,userDoc)->
    userDoc.verifyPassword("pass3") 
    userDoc.printColor()    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM