An automatic field transformation plugin for Mongoose 5. Any transformations are registered as save
, update
and findOneAndUpdate
middleware.
import * as bcrypt from 'bcrypt';
import * as mongoose from 'mongoose';
import {AsyncTransform, MongooseTransformFieldPlugin} from 'mongoose-transform-field-plugin';
const schema = new mongoose.Schema({
password: String
});
const transformer: AsyncTransform<string> = (pwd: string): Promise<string> => {
return bcrypt.hash(pwd, 12);
};
// Run as non-parallel Mongoose middleware by default
MongooseTransformFieldPlugin.transformAsync(schema, 'password', transformer);
// Run as non-parallel Mongoose middleware explicitly
MongooseTransformFieldPlugin.transformAsync(schema, 'password', false, transformer);
// Run as a parallel Mongoose middleware
MongooseTransformFieldPlugin.transformAsync(schema, 'password', true, transformer);
import * as bcrypt from 'bcrypt';
import * as mongoose from 'mongoose';
import {AsyncTransform, MongooseTransformFieldPlugin, TransformAsyncOptions} from 'mongoose-transform-field-plugin';
const schema = new mongoose.Schema({
password: String
});
const transform: AsyncTransform<string> = (pwd: string): Promise<string> => {
return bcrypt.hash(pwd, 12);
};
// Run as non-parallel Mongoose middleware by default
let config: TransformAsyncOptions<string> = {
field: 'password',
transformer: transform
};
schema.plugin(MongooseTransformFieldPlugin.transformAsync.plugin, config);
// Run as non-parallel Mongoose middleware explicitly
config = {
field: 'password',
parallel: false,
transformer: transform
};
schema.plugin(MongooseTransformFieldPlugin.transformAsync.plugin, config);
// Run as a parallel Mongoose middleware
config = {
field: 'password',
parallel: true,
transformer: transform
};
schema.plugin(MongooseTransformFieldPlugin.transformAsync.plugin, config);
This transforms accented characters from a string, trims it and makes it lowercase before storing it in another field. Useful if you want some basic search functionality.
import * as mongoose from 'mongoose';
import {MongooseTransformFieldPlugin} from 'mongoose-transform-field-plugin';
const schema = new mongoose.Schema({
firstname: String,
firstname_normalised: {
select: false,
type: String
},
lastname: String,
lastname_normalised: {
select: false,
type: String
}
});
const fields = {
firstname: 'firstname_normalised', // firstname will be normalised into the firstname_normalised field
lastname: 'lastname_normalised' // lastname will be normalised into the lastname_normalised field
};
// Direct usage
MongooseTransformFieldPlugin.normalise(schema, fields);
// Plugin usage
schema.plugin(MongooseTransformFieldPlugin.normalise.plugin, fields);
You should really use schema setters instead, but this is included for completeness' sake.
import * as bcrypt from 'bcrypt';
import * as mongoose from 'mongoose';
import {MongooseTransformFieldPlugin, SyncTransform, TransformSyncOptions} from 'mongoose-transform-field-plugin';
const schema = new mongoose.Schema({
password: String
});
const transform: SyncTransform<string> = (pwd: string): string => {
return bcrypt.hashSync(pwd, 12);
};
// Direct usage
MongooseTransformFieldPlugin.transformSync(schema, 'password', transform);
// Plugin usage
const conf: TransformSyncOptions<string> = {
field: 'password',
transformer: transform
};
schema.plugin(MongooseTransformFieldPlugin.transformSync.plugin, conf);