Guest User

Untitled

a guest
Jan 28th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import bcrypt from 'bcryptjs'
  2. import util from 'util'
  3. import {
  4. ottoman,
  5. bucket,
  6. } from '../db'
  7. const _throw = m => {throw m}
  8. const User = ottoman.model('User', {
  9. username: {type: 'string', readonly: true},
  10. email: 'string',
  11. name: 'string',
  12. password: 'string',
  13. createdAt: {type: 'Date', default: Date.now},
  14. }, {id: 'username'})
  15.  
  16. User.pre('save', function (user, next) {
  17. this.password = bcrypt.hashSync(this.password, 10)
  18. next()
  19. })
  20. const create = util.promisify(User.create.bind(User))
  21. const count = util.promisify(User.count.bind(User))
  22. const getById = util.promisify(User.getById.bind(User))
  23. const find = util.promisify(User.find.bind(User))
  24. User.check_email = async email => await count({ email }) && _throw('Duplicated: ' + email)
  25. User.createAndSave = async args => await create({...args})
  26. User.byId = async id => await getById(id)
  27. User.find_filtered = async filter => await find(filter)
  28.  
  29. ottoman.ensureIndices(function (err) {
  30. if (err) {
  31. return console.error('Error ensure indices Places', err)
  32. }
  33. console.log('Ensure indices Places')
  34. })
  35.  
  36. export default User
Add Comment
Please, Sign In to add comment