Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const _ = require( 'lodash' )
- const Async = require( 'async' )
- const Util = require( 'util' )
- const Path = require( 'path' )
- module.exports = Mongoose => {
- const { Schema, Types, Model, Promise } = Mongoose
- class UserClass extends Model {
- constructor ( data ) {
- super(data)
- }
- toString () {
- //onsole.log( `[${_method}] Arguments:`, arguments )
- return `tostr`
- }
- static schema(){
- return {
- name: [{
- first: String,
- value: String
- }],
- username: {
- type: String,
- required: true,
- unique: true
- },
- password: {
- type: String,
- required:
- true
- },
- admin: Boolean,
- location: String,
- meta: {
- age: Number,
- website: String
- },
- created_at: Date,
- updated_at: Date
- }
- }
- // `fullName` becomes a virtual
- get fullName() {
- return `${this.firstName} ${this.lastName}`;
- }
- getBar() {
- return 'baz';
- }
- set fullName(v) {
- const firstSpace = v.indexOf(' ');
- this.firstName = v.split(' ')[0];
- this.lastName = firstSpace === -1 ? '' : v.substr(firstSpace + 1);
- }
- // `getFullName()` becomes a document method
- getFullName() {
- return `${this.firstName} ${this.lastName}`;
- }
- static createUser( data ) {
- const _method = "(static) UserClass.createUser"
- const newDoc = {}
- // `[${_method}] `
- // console.log( `[${_method}] ` )
- const _args = arguments
- const _this = this
- return new Promise( ( res, rej ) => {
- /*
- if ( ! _.isObject( data ) || ! _.has( data, 'username') || ! _.has( data, 'password') ){
- console.log( `[${_method}] Invalid data provided:`, _args )
- return rej( `[${_method}] Invalid data` )
- }
- newDoc.username = data.username
- newDoc.password = data.password
- */
- if ( ! _.isObject( data ) ) {
- console.log( `[${_method}] Data is invalid or absent:`, _args )
- return rej( `[${_method}] Data is invalid or absent` )
- }
- if ( _.has( data, 'name' ) )
- newDoc.name = {}
- // Name given in OBJECT
- if ( _.isObject( data.name ) ){
- console.log( `[${_method}] Debug - data.name is an OBJECT` )
- if ( _.isString( data.name.first ) )
- newDoc.name.first = data.name.first
- if ( _.isString( data.name.last ) )
- newDoc.name.last = data.name.last
- }
- // Name given in ARRAY
- else if ( _.isArray( data.name ) ){
- console.log( `[${_method}] Debug - data.name is an ARRAY` )
- if ( data.name.length >= 2 ){
- newDoc.name.first = data.name[0]
- newDoc.name.last = data.name[1]
- }
- else if ( data.name.length === 1 ) {
- newDoc.name.last = data.name[0]
- }
- }
- // Name given in STRING
- else if ( _.isString( data.name ) ){
- console.log( `[${_method}] Debug - data.name is a STRING` )
- let nameSegs = data.name.split(' ')
- if ( nameSegs.name.length >= 2 ){
- newDoc.name.first = nameSegs.name[0]
- newDoc.name.last = nameSegs.name[1]
- }
- else if ( nameSegs.name.length === 1 ) {
- newDoc.name.last = nameSegs.name[0]
- }
- }
- // Name NOT given
- else {
- console.log( `[${_method}] Debug - data.name is UNDEFINED or INVALID data type` )
- }
- if ( _.isString( data.username ) ) {
- console.log( `[${_method}] Debug - data.username is a STRING` )
- newDoc.username = data.username
- }
- else if ( _.isObject( data.name ) && Object.keys( data.name ).length !== 0 ) {
- let un = []
- if ( _.isString( data.name.first ) )
- un.push( data.name.first )
- if ( _.isString( data.name.last ) )
- un.push( data.name.last )
- if ( un.length === 0 ){
- console.log( `[${_method}] ABORT - Unable to define username - No username provided & no first/last name to construct one from` )
- return rej( `[${_method}] ABORT - Unable to define username - No username provided & no first/last name to construct one from` )
- }
- newDoc.username = un.join('.')
- console.log( `[${_method}] Debug - data.username is UNDEFINED but a username was constructed from the users name:`, ewDoc.username )
- }
- else {
- console.log( `[${_method}] Aborting - No username or first/last name provided - unable to define a username` )
- return rej( `[${_method}] Aborting - No username or first/last name provided - unable to define a username` )
- }
- new _this( data )
- .save()
- .then( newUserDoc => {
- console.log( `[${_method}] User Created - newUserDoc:`, newUserDoc )
- return res( newUserDoc )
- } )
- .catch( err => rej( err ) )
- })
- }
- static findUserByUsername( username ) {
- const _this = this
- const _method = "(static) UserClass.findUserByUsername"
- return new Promise( ( res, rej ) => {
- if ( ! username || ! _.isString( username ) )
- return rej( `[${_method}] Username is empty or non-string value` )
- let docQuery
- docQuery = this
- .find({
- username: username
- })
- docQuery
- .then( userDoc => {
- // If no results were found, then
- if ( _.isEmpty( userDoc ) )
- return rej( `[${_method}] No user with the name ${username} found` )
- // Otherwise, return a single object
- return res( userDoc )
- })
- .catch( err => {
- console.error( `[${_method}] ERROR:`, err )
- return rej( err )
- })
- })
- }
- }
- return Mongoose.model( UserClass, UserClass.schema())
- }
- /**
- EXAMPLE USAGE:
- 'use strict'
- const _ = require( 'lodash' )
- const Async = require( 'async' )
- const Util = require( 'util' )
- const AppRoot = require( 'app-root-path' )
- const Path = require( 'path' )
- const Columnify = require( 'columnify' )
- const Mongoose = require( 'mongoose' )
- Mongoose.Promise = require( 'bluebird' )
- Mongoose.connect( 'mongodb://127.0.0.1:27017/sasset_test', { config: { autoIndex: false } })
- const UserModel = require('./sasset-core-beta/models/Es6odm')( Mongoose )
- UserModel
- .createUser({
- username: 'foouser1',
- password: 'foouser1',
- name: {
- first: 'John',
- last: 'Doe'
- }
- })
- .then( doc => {
- console.log("Created:",doc)
- console.log("doc.getFoo():",doc.getFoo())
- console.log("doc.getBar():",doc.getBar())
- })
- .catch( err => {
- console.error( 'ERROR:',err )
- } )
- .finally( () => {
- console.log('Finally.. ending')
- Mongoose.connection.close()
- } )
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement