<commit_before>import mongoose from 'mongoose';
import bcrypt from 'bcrypt';

let Schema = mongoose.Schema;
let UserSchema = new Schema({
	username: {
		type: String,
		unique: true
	},
	password: String,
	email: String,
	admin: Boolean
});

/**
 *
 */
UserSchema.pre('save', function (next) {
	if (!this.isModified('password')) return next();

	bcrypt.genSalt(10, (err, salt) => {
		if (err) return next(err);

		bcrypt.hash(this.password, salt, (err, hash) => {
			if (err) return next(err);

			this.password = hash;
			return next();
		});
	});
});

/**
 *
 */
UserSchema.methods.toClient = function () {
	let user = {
		username: this.username,
		email: this.email
	};

	if (this.admin) {
		user.admin = this.admin;
	}

	return user;
};

/**
 *
 */
UserSchema.statics.get = function (username, done) {
	this.findOne({username}, done);
};

export default UserSchema;<commit_msg>Adding findOrCreate() functionality to the user schema.  Might see about adding this to all schemas?<commit_after>import mongoose from 'mongoose';
import bcrypt from 'bcrypt';

let Schema = mongoose.Schema;
let UserSchema = new Schema({
	username: {
		type: String,
		unique: true
	},
	password: String,
	email: String,
	admin: Boolean
});

/**
 *
 */
UserSchema.pre('save', function (next) {
	if (!this.isModified('password')) return next();

	bcrypt.genSalt(10, (err, salt) => {
		if (err) return next(err);

		bcrypt.hash(this.password, salt, (err, hash) => {
			if (err) return next(err);

			this.password = hash;
			return next();
		});
	});
});

/**
 *
 */
UserSchema.methods.toClient = function () {
	let user = {
		username: this.username,
		email: this.email
	};

	if (this.admin) {
		user.admin = this.admin;
	}

	return user;
};

/**
 *
 */
UserSchema.statics.get = function (username, done) {
	this.findOne({username}, done);
};

/**
 *
 */
UserSchema.statics.findOrCreate = function (conditions, doc, options, callback) {
	if (arguments.length < 4) {
		if (typeof options === 'function') {
			// Scenario: findOrCreate(conditions, doc, callback)
			callback = options;
			options = {};
		}
		else if (typeof doc === 'function') {
			// Scenario: findOrCreate(conditions, callback);
			callback = doc;
			doc = {};
			options = {};
		}
	}

	this.findOne(conditions, (err, result) => {
		if (err || result) {
			if (options && options.upsert && !err) {
				this.update(conditions, doc, (err, count) => {
					this.findOne(conditions, (err, result) => {
						callback(err, result, false);
					});
				});
			}
			else {
				callback(err, result, false)
			}
		}
		else {
			for (var key in doc) {
				conditions[key] = doc[key]; 
			}

			var obj = new this(conditions)

			obj.save(err => {
				callback(err, obj, true);
			});
		}
	});
};

export default UserSchema;
{'fraction_non_alphanumeric': 0.17336228736880202, 'fraction_numerical': 0.0018096272167933405, 'mean_word_length': 3.305295950155763, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4579348', 'n_tokens_mistral': 1067, 'n_tokens_neox': 957, 'n_words': 235}