|
1 | 1 | # mongoose-transform-field-plugin
|
2 | 2 |
|
3 |
| -[](https://greenkeeper.io/) |
| 3 | +An automatic field transformation plugin for Mongoose 5. Any transformations are registered as `save`, `update` and `findOneAndUpdate` middleware. |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/mongoose-transform-field-plugin) |
| 6 | +[](https://travis-ci.org/Alorel/mongoose-transform-field-plugin) |
| 7 | +[](https://coveralls.io/github/Alorel/mongoose-transform-field-plugin?branch=master) |
| 8 | +[](https://greenkeeper.io/) |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +----- |
| 13 | + |
| 14 | +# Table of Contents |
| 15 | + |
| 16 | +<!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 17 | +<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 18 | + |
| 19 | + |
| 20 | +- [Asynchronous transformation](#asynchronous-transformation) |
| 21 | + - [Direct usage](#direct-usage) |
| 22 | + - [Schema plugin usage](#schema-plugin-usage) |
| 23 | +- [Normalisation](#normalisation) |
| 24 | +- [Synchronous Transformations](#synchronous-transformations) |
| 25 | + |
| 26 | +<!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 27 | + |
| 28 | +# Asynchronous transformation |
| 29 | + |
| 30 | +## Direct usage |
| 31 | + |
| 32 | +```typescript |
| 33 | +import * as bcrypt from 'bcrypt'; |
| 34 | +import * as mongoose from 'mongoose'; |
| 35 | +import {AsyncTransform, MongooseTransformFieldPlugin} from 'mongoose-transform-field-plugin'; |
| 36 | + |
| 37 | +const schema = new mongoose.Schema({ |
| 38 | + password: String |
| 39 | +}); |
| 40 | + |
| 41 | +const transformer: AsyncTransform<string> = (pwd: string): Promise<string> => { |
| 42 | + return bcrypt.hash(pwd, 12); |
| 43 | +}; |
| 44 | + |
| 45 | +// Run as non-parallel Mongoose middleware by default |
| 46 | +MongooseTransformFieldPlugin.transformAsync(schema, 'password', transformer); |
| 47 | + |
| 48 | +// Run as non-parallel Mongoose middleware explicitly |
| 49 | +MongooseTransformFieldPlugin.transformAsync(schema, 'password', false, transformer); |
| 50 | + |
| 51 | +// Run as a parallel Mongoose middleware |
| 52 | +MongooseTransformFieldPlugin.transformAsync(schema, 'password', true, transformer); |
| 53 | +``` |
| 54 | + |
| 55 | +## Schema plugin usage |
| 56 | + |
| 57 | +```typescript |
| 58 | +import * as bcrypt from 'bcrypt'; |
| 59 | +import * as mongoose from 'mongoose'; |
| 60 | +import {AsyncTransform, MongooseTransformFieldPlugin, TransformAsyncOptions} from 'mongoose-transform-field-plugin'; |
| 61 | + |
| 62 | +const schema = new mongoose.Schema({ |
| 63 | + password: String |
| 64 | +}); |
| 65 | + |
| 66 | +const transform: AsyncTransform<string> = (pwd: string): Promise<string> => { |
| 67 | + return bcrypt.hash(pwd, 12); |
| 68 | +}; |
| 69 | + |
| 70 | +// Run as non-parallel Mongoose middleware by default |
| 71 | +let config: TransformAsyncOptions<string> = { |
| 72 | + field: 'password', |
| 73 | + transformer: transform |
| 74 | +}; |
| 75 | +schema.plugin(MongooseTransformFieldPlugin.transformAsync.plugin, config); |
| 76 | + |
| 77 | +// Run as non-parallel Mongoose middleware explicitly |
| 78 | +config = { |
| 79 | + field: 'password', |
| 80 | + parallel: false, |
| 81 | + transformer: transform |
| 82 | +}; |
| 83 | +schema.plugin(MongooseTransformFieldPlugin.transformAsync.plugin, config); |
| 84 | + |
| 85 | +// Run as a parallel Mongoose middleware |
| 86 | +config = { |
| 87 | + field: 'password', |
| 88 | + parallel: true, |
| 89 | + transformer: transform |
| 90 | +}; |
| 91 | +schema.plugin(MongooseTransformFieldPlugin.transformAsync.plugin, config); |
| 92 | +``` |
| 93 | + |
| 94 | +# Normalisation |
| 95 | + |
| 96 | +This transforms accented characters from a string, trims it and makes it lowercase before storing it in another field. |
| 97 | +Useful if you want some basic search functionality. |
| 98 | + |
| 99 | +```typescript |
| 100 | +import * as mongoose from 'mongoose'; |
| 101 | +import {MongooseTransformFieldPlugin} from 'mongoose-transform-field-plugin'; |
| 102 | + |
| 103 | +const schema = new mongoose.Schema({ |
| 104 | + firstname: String, |
| 105 | + firstname_normalised: { |
| 106 | + select: false, |
| 107 | + type: String |
| 108 | + }, |
| 109 | + lastname: String, |
| 110 | + lastname_normalised: { |
| 111 | + select: false, |
| 112 | + type: String |
| 113 | + } |
| 114 | +}); |
| 115 | + |
| 116 | +const fields = { |
| 117 | + firstname: 'firstname_normalised', // firstname will be normalised into the firstname_normalised field |
| 118 | + lastname: 'lastname_normalised' // lastname will be normalised into the lastname_normalised field |
| 119 | +}; |
| 120 | + |
| 121 | +// Direct usage |
| 122 | +MongooseTransformFieldPlugin.normalise(schema, fields); |
| 123 | + |
| 124 | +// Plugin usage |
| 125 | +schema.plugin(MongooseTransformFieldPlugin.normalise.plugin, fields); |
| 126 | +``` |
| 127 | + |
| 128 | +# Synchronous Transformations |
| 129 | + |
| 130 | +You should really use schema setters instead, but this is included for completeness' sake. |
| 131 | + |
| 132 | +```typescript |
| 133 | +import * as bcrypt from 'bcrypt'; |
| 134 | +import * as mongoose from 'mongoose'; |
| 135 | +import {MongooseTransformFieldPlugin, SyncTransform, TransformSyncOptions} from 'mongoose-transform-field-plugin'; |
| 136 | + |
| 137 | +const schema = new mongoose.Schema({ |
| 138 | + password: String |
| 139 | +}); |
| 140 | + |
| 141 | +const transform: SyncTransform<string> = (pwd: string): string => { |
| 142 | + return bcrypt.hashSync(pwd, 12); |
| 143 | +}; |
| 144 | + |
| 145 | +// Direct usage |
| 146 | +MongooseTransformFieldPlugin.transformSync(schema, 'password', transform); |
| 147 | + |
| 148 | +// Plugin usage |
| 149 | +const conf: TransformSyncOptions<string> = { |
| 150 | + field: 'password', |
| 151 | + transformer: transform |
| 152 | +}; |
| 153 | + |
| 154 | +schema.plugin(MongooseTransformFieldPlugin.transformSync.plugin, conf); |
| 155 | +``` |
0 commit comments