Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. 'use strict'
  2.  
  3. const _ = require( 'lodash' )
  4. const Async = require( 'async' )
  5. const Util = require( 'util' )
  6. const Path = require( 'path' )
  7.  
  8. module.exports = Mongoose => {
  9. const { Schema, Types, Model, Promise } = Mongoose
  10.  
  11. class UserClass extends Model {
  12. constructor ( data ) {
  13. super(data)
  14. }
  15.  
  16. toString () {
  17. //onsole.log( `[${_method}] Arguments:`, arguments )
  18. return `tostr`
  19. }
  20.  
  21. static schema(){
  22. return {
  23. name: [{
  24. first: String,
  25. value: String
  26. }],
  27. username: {
  28. type: String,
  29. required: true,
  30. unique: true
  31. },
  32. password: {
  33. type: String,
  34. required:
  35. true
  36. },
  37. admin: Boolean,
  38. location: String,
  39. meta: {
  40. age: Number,
  41. website: String
  42. },
  43. created_at: Date,
  44. updated_at: Date
  45. }
  46. }
  47.  
  48. // `fullName` becomes a virtual
  49. get fullName() {
  50. return `${this.firstName} ${this.lastName}`;
  51. }
  52.  
  53. getBar() {
  54. return 'baz';
  55. }
  56.  
  57. set fullName(v) {
  58. const firstSpace = v.indexOf(' ');
  59. this.firstName = v.split(' ')[0];
  60. this.lastName = firstSpace === -1 ? '' : v.substr(firstSpace + 1);
  61. }
  62.  
  63. // `getFullName()` becomes a document method
  64. getFullName() {
  65. return `${this.firstName} ${this.lastName}`;
  66. }
  67.  
  68. static createUser( data ) {
  69. const _method = "(static) UserClass.createUser"
  70.  
  71. const newDoc = {}
  72. // `[${_method}] `
  73. // console.log( `[${_method}] ` )
  74.  
  75. const _args = arguments
  76.  
  77. const _this = this
  78. return new Promise( ( res, rej ) => {
  79. /*
  80. if ( ! _.isObject( data ) || ! _.has( data, 'username') || ! _.has( data, 'password') ){
  81. console.log( `[${_method}] Invalid data provided:`, _args )
  82. return rej( `[${_method}] Invalid data` )
  83. }
  84.  
  85. newDoc.username = data.username
  86. newDoc.password = data.password
  87. */
  88.  
  89. if ( ! _.isObject( data ) ) {
  90. console.log( `[${_method}] Data is invalid or absent:`, _args )
  91. return rej( `[${_method}] Data is invalid or absent` )
  92. }
  93.  
  94. if ( _.has( data, 'name' ) )
  95. newDoc.name = {}
  96.  
  97. // Name given in OBJECT
  98. if ( _.isObject( data.name ) ){
  99. console.log( `[${_method}] Debug - data.name is an OBJECT` )
  100.  
  101. if ( _.isString( data.name.first ) )
  102. newDoc.name.first = data.name.first
  103.  
  104. if ( _.isString( data.name.last ) )
  105. newDoc.name.last = data.name.last
  106. }
  107.  
  108. // Name given in ARRAY
  109. else if ( _.isArray( data.name ) ){
  110. console.log( `[${_method}] Debug - data.name is an ARRAY` )
  111.  
  112. if ( data.name.length >= 2 ){
  113. newDoc.name.first = data.name[0]
  114. newDoc.name.last = data.name[1]
  115. }
  116. else if ( data.name.length === 1 ) {
  117. newDoc.name.last = data.name[0]
  118. }
  119. }
  120.  
  121. // Name given in STRING
  122. else if ( _.isString( data.name ) ){
  123. console.log( `[${_method}] Debug - data.name is a STRING` )
  124.  
  125. let nameSegs = data.name.split(' ')
  126.  
  127. if ( nameSegs.name.length >= 2 ){
  128. newDoc.name.first = nameSegs.name[0]
  129. newDoc.name.last = nameSegs.name[1]
  130. }
  131. else if ( nameSegs.name.length === 1 ) {
  132. newDoc.name.last = nameSegs.name[0]
  133. }
  134. }
  135.  
  136. // Name NOT given
  137. else {
  138. console.log( `[${_method}] Debug - data.name is UNDEFINED or INVALID data type` )
  139. }
  140.  
  141. if ( _.isString( data.username ) ) {
  142. console.log( `[${_method}] Debug - data.username is a STRING` )
  143.  
  144. newDoc.username = data.username
  145. }
  146. else if ( _.isObject( data.name ) && Object.keys( data.name ).length !== 0 ) {
  147. let un = []
  148.  
  149. if ( _.isString( data.name.first ) )
  150. un.push( data.name.first )
  151.  
  152. if ( _.isString( data.name.last ) )
  153. un.push( data.name.last )
  154.  
  155. if ( un.length === 0 ){
  156. console.log( `[${_method}] ABORT - Unable to define username - No username provided & no first/last name to construct one from` )
  157. return rej( `[${_method}] ABORT - Unable to define username - No username provided & no first/last name to construct one from` )
  158. }
  159.  
  160. newDoc.username = un.join('.')
  161.  
  162. console.log( `[${_method}] Debug - data.username is UNDEFINED but a username was constructed from the users name:`, ewDoc.username )
  163. }
  164. else {
  165. console.log( `[${_method}] Aborting - No username or first/last name provided - unable to define a username` )
  166. return rej( `[${_method}] Aborting - No username or first/last name provided - unable to define a username` )
  167. }
  168.  
  169. new _this( data )
  170. .save()
  171. .then( newUserDoc => {
  172. console.log( `[${_method}] User Created - newUserDoc:`, newUserDoc )
  173.  
  174. return res( newUserDoc )
  175. } )
  176. .catch( err => rej( err ) )
  177.  
  178. })
  179. }
  180.  
  181. static findUserByUsername( username ) {
  182. const _this = this
  183. const _method = "(static) UserClass.findUserByUsername"
  184.  
  185. return new Promise( ( res, rej ) => {
  186. if ( ! username || ! _.isString( username ) )
  187. return rej( `[${_method}] Username is empty or non-string value` )
  188.  
  189. let docQuery
  190.  
  191. docQuery = this
  192. .find({
  193. username: username
  194. })
  195.  
  196. docQuery
  197. .then( userDoc => {
  198. // If no results were found, then
  199. if ( _.isEmpty( userDoc ) )
  200. return rej( `[${_method}] No user with the name ${username} found` )
  201.  
  202. // Otherwise, return a single object
  203. return res( userDoc )
  204. })
  205. .catch( err => {
  206. console.error( `[${_method}] ERROR:`, err )
  207. return rej( err )
  208. })
  209.  
  210. })
  211. }
  212. }
  213.  
  214. return Mongoose.model( UserClass, UserClass.schema())
  215. }
  216.  
  217.  
  218. /**
  219. EXAMPLE USAGE:
  220.  
  221. 'use strict'
  222.  
  223. const _ = require( 'lodash' )
  224. const Async = require( 'async' )
  225. const Util = require( 'util' )
  226. const AppRoot = require( 'app-root-path' )
  227. const Path = require( 'path' )
  228. const Columnify = require( 'columnify' )
  229. const Mongoose = require( 'mongoose' )
  230.  
  231. Mongoose.Promise = require( 'bluebird' )
  232.  
  233. Mongoose.connect( 'mongodb://127.0.0.1:27017/sasset_test', { config: { autoIndex: false } })
  234.  
  235. const UserModel = require('./sasset-core-beta/models/Es6odm')( Mongoose )
  236.  
  237. UserModel
  238. .createUser({
  239. username: 'foouser1',
  240. password: 'foouser1',
  241. name: {
  242. first: 'John',
  243. last: 'Doe'
  244. }
  245. })
  246. .then( doc => {
  247. console.log("Created:",doc)
  248. console.log("doc.getFoo():",doc.getFoo())
  249. console.log("doc.getBar():",doc.getBar())
  250. })
  251. .catch( err => {
  252. console.error( 'ERROR:',err )
  253. } )
  254. .finally( () => {
  255. console.log('Finally.. ending')
  256. Mongoose.connection.close()
  257. } )
  258. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement