Created
December 15, 2011 19:25
-
-
Save timoxley/1482444 to your computer and use it in GitHub Desktop.
account.js
This file contains hidden or 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
'use strict' | |
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema | |
function forceFormat(subdomain) { | |
return subdomain.toLowerCase().remove(/[^a-z0-9]/g) | |
} | |
var AccountSchema = new Schema({ | |
name: {type: String, length: 255, required: true }, | |
subdomain: {type: String, length: 255, required: true, set: forceFormat, match: /^[a-z0-9]{3,100}$/}, | |
}) | |
AccountSchema.index({ name: 1, subdomain: 1 }, { unique: true, dropDups : true }) | |
var Account = mongoose.model('Account', AccountSchema) | |
module.exports = Account |
This file contains hidden or 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
'name must be unique': function(test) { | |
account.save(function(err) { | |
if (err) throw err | |
blueprints.generate('Account', function(err, anotherAccount) { | |
if (err) throw err | |
anotherAccount.name = account.name | |
anotherAccount.save(function(err) { | |
test.ok(/duplicate/.test(err) || /unique/.test(err) ) | |
test.done() | |
}) | |
}) | |
}) | |
}, | |
'subdomain must be unique': function(test) { | |
account.save(function(err) { | |
console.log(arguments) | |
if (err) throw err | |
blueprints.generate('Account', function(err, anotherAccount) { | |
if (err) throw err | |
anotherAccount.subdomain = account.subdomain | |
anotherAccount.save(function(err) { | |
test.ok(/duplicate/.test(err) || /unique/.test(err) ) | |
test.done() | |
}) | |
}) | |
}) |
This file contains hidden or 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
'use strict' | |
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema | |
function forceFormat(subdomain) { | |
return subdomain.toLowerCase().remove(/[^a-z0-9]/g) | |
} | |
var AccountSchema = new Schema({ | |
name: {type: String, length: 255, unique: true, required: true }, | |
subdomain: {type: String, length: 255, unique: true, required: true, set: forceFormat, match: /^[a-z0-9]{3,100}$/}, | |
}) | |
AccountSchema.index({ name: 1, subdomain: 1 }, { unique: true, dropDups : true }) // this line seems to be completely ignored, removing it does nothing | |
var Account = mongoose.model('Account', AccountSchema) | |
module.exports = Account |
This file contains hidden or 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
✖ user - name must be unique | |
AssertionError: null == true | |
at Object.ok (/usr/local/lib/node_modules/nodeunit/lib/types.js:83:39) | |
at Promise.<anonymous> (/Users/secoif/Projects/groupdock/groupdock.js/test/models/account_test.js:62:18) | |
at Promise.<anonymous> (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:120:8) | |
at Promise.<anonymous> (events.js:67:17) | |
at Promise.emit (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:59:38) | |
at Promise.complete (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:70:20) | |
at handleSave (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/model.js:267:13) | |
at Array.0 (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/utils.js:394:16) | |
at EventEmitter._tickCallback (node.js:192:40) | |
✖ user - subdomain must be unique | |
AssertionError: null == true | |
at Object.ok (/usr/local/lib/node_modules/nodeunit/lib/types.js:83:39) | |
at Promise.<anonymous> (/Users/secoif/Projects/groupdock/groupdock.js/test/models/account_test.js:77:18) | |
at Promise.<anonymous> (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:120:8) | |
at Promise.<anonymous> (events.js:67:17) | |
at Promise.emit (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:59:38) | |
at Promise.complete (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:70:20) | |
at handleSave (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/model.js:267:13) | |
at Array.0 (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/utils.js:394:16) | |
at EventEmitter._tickCallback (node.js:192:40) |
This file contains hidden or 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
✔ user - name must be unique | |
✖ user - subdomain must be unique | |
AssertionError: false == true | |
at Object.ok (/usr/local/lib/node_modules/nodeunit/lib/types.js:83:39) | |
at Promise.<anonymous> (/Users/secoif/Projects/groupdock/groupdock.js/test/models/account_test.js:76:18) | |
at Promise.<anonymous> (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:120:8) | |
at Promise.<anonymous> (events.js:67:17) | |
at Promise.emit (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:59:38) | |
at Promise.complete (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/promise.js:70:20) | |
at handleSave (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/model.js:267:13) | |
at Array.0 (/Users/secoif/Projects/groupdock/groupdock.js/node_modules/mongoose/lib/utils.js:394:16) | |
at EventEmitter._tickCallback (node.js:192:40) |
This file contains hidden or 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
'use strict' | |
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema | |
function forceFormat(subdomain) { | |
return subdomain.toLowerCase().remove(/[^a-z0-9]/g) | |
} | |
var AccountSchema = new Schema({ | |
name: {type: String, length: 255, unique: true, required: true }, | |
subdomain: {type: String, length: 255, index: true, required: true, set: forceFormat, match: /^[a-z0-9]{3,100}$/}, | |
}) | |
AccountSchema.path('subdomain').validate(function (subdomain, callback) { | |
Account.findOne({subdomain: subdomain}, function(err, found) { | |
callback(!found) | |
}) | |
}, 'duplicate subdomains are not allowed'); | |
AccountSchema.index({ name: 1, subdomain: 1 }, { unique: true, dropDups : true }) | |
AccountSchema.set('safe', true); | |
var Account = mongoose.model('Account', AccountSchema) | |
module.exports = Account |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment