Skip to content

Instantly share code, notes, and snippets.

@lrezende
Created April 1, 2016 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrezende/6b9b00e213fc2cd2efb44bf8f13abf39 to your computer and use it in GitHub Desktop.
Save lrezende/6b9b00e213fc2cd2efb44bf8f13abf39 to your computer and use it in GitHub Desktop.
clientSchema.pre('validate', function (next) {
// set code automatically
// generate all possible codes
// this is a fixed list from A/001 to Z/100
let _all_codes = new Set()
for (let _letter of 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
for (let i = 1; i <= 100; i++) {
_all_codes.add(_letter + '/' + sprintf('%03d', i));
}
}
// get all codes from db
var _db_codes = new Set();
mongoose.models['Client']
.find()
.select('-_id code')
.sort('code')
.lean()
.exec(function(err, codes) {
if (err) {
console.log(err)
}
//
console.log(_all_codes)
})
// get next code
// *1 return the first gap and set the code
// this.code = 'D/078'
next();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment