Skip to content
This repository was archived by the owner on Jul 11, 2019. It is now read-only.

Commit cb44a5d

Browse files
committed
prep release
1 parent c08ad56 commit cb44a5d

File tree

10 files changed

+232
-38
lines changed

10 files changed

+232
-38
lines changed

.npmignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/.idea/
2+
/node_modules/
3+
/coverage/
4+
/.nyc_output/
5+
/package-lock.json
6+
/.env
7+
/src/
8+
/*.tgz
9+
/types/_Update.d.ts
10+
/dist/_transformSyncInternal.d.ts
11+
/test/
12+
/.travis.yml
13+
/.nycrc
14+
/mocha.opts
15+
/tsconfig.json
16+
/tsconfig.test.json
17+
/tslint.json
18+
/update-version.js

.travis.yml

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@ env:
3131
- TS_NODE_CACHE_DIRECTORY=./node_modules/.cache/ts-node
3232
- TS_NODE_PROJECT=./tsconfig.test.json
3333

34-
#stages:
35-
# - Test
36-
# - Stub
37-
# - Stub2
38-
39-
#jobs:
40-
# include:
41-
# - stage: Stub
42-
# services: false
43-
# node_js: *latestTested
44-
# before_install: false
45-
# after_success: echo bar
46-
# script: echo foo
47-
# - stage: Stub2
48-
# services: false
49-
# node_js: *latestTested
50-
# before_install: false
51-
# after_success: echo qux
52-
# script: echo baz
34+
stages:
35+
- Test
36+
- name: &releaseStage Release
37+
if: tag IS present
38+
39+
jobs:
40+
include:
41+
- stage: *releaseStage
42+
services: false
43+
node_js: *latestTested
44+
script: npm run build
45+
after_success: false
46+
before_deploy: node ./update-version.js
47+
deploy:
48+
- provider: npm
49+
on:
50+
tags: true
51+
skip_cleanup: true
52+
email: a.molcanovas@gmail.com
53+
api_key:
54+
secure: "D8D0XyBCa2HzM/tq9PrU8bYIp8sHaZCxYJhZOnyELJ+vPf39nKW1vyVa34jcOab1JVbXS/Y29vPtd03FGPXPRZABJcjkXxBNW9o7u4khDKqZ2Z/498gvSZAIpSNrQzBQNPzwM1WZ4Fit1FIG24lzLIHCrfFV6pPc+urC8C6IO10MUqkRhE0TfyouHCXQkd59oZL7u3pSY/LB70bId5Noq8Cmr2bloDIZFwy5bZq3Udm4WX/+tMiO9qTDcwfZGX0uFPwumFJw/xURGHmmrbn+qarbRDH6RNa2MHMgkime/f1vR1K9J+22m6MqpH0aZ30EaaUGsXuBNwDPSSTc2Y+9RQE6D5sTZzBdtpHKAXa0ACiCFte+x+SCypHBubquSU0gkO+CDBoohVFG4h7pocawsMMfR5+IOKnBxBmiwJC8QjUYislzkUL9TS/ZmW9lrab8PQSKlpiXXdEWkI4pDxQbF4fDG33aveCKw+geB0vqh8iTVQphqhBbp3elZMuKl/fsbkCPloIgjIb+d8M3r7UVYC0wM9N2/FMr5NVKFL1e4r9X1GcusS0X4itFvqwuKlBYM9d1LLn1vPUnETfFKrA+sKvoJVVCuvZUyzZ7MnBLZ17djwuCrp0jb3AF+DKZUH35SvRjiMldflaHvzv5J94s7251uhTeSPx4MLmB6iogMXM="

README.md

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,155 @@
11
# mongoose-transform-field-plugin
22

3-
[![Greenkeeper badge](https://badges.greenkeeper.io/Alorel/mongoose-transform-field-plugin.svg)](https://greenkeeper.io/)
3+
An automatic field transformation plugin for Mongoose 5. Any transformations are registered as `save`, `update` and `findOneAndUpdate` middleware.
4+
5+
[![NPM link](https://nodei.co/npm/mongoose-transform-field-plugin.svg?compact=true)](https://www.npmjs.com/package/mongoose-transform-field-plugin)
6+
[![Build Status](https://travis-ci.org/Alorel/mongoose-transform-field-plugin.svg?branch=master)](https://travis-ci.org/Alorel/mongoose-transform-field-plugin)
7+
[![Coverage Status](https://coveralls.io/repos/github/Alorel/mongoose-transform-field-plugin/badge.svg?branch=master)](https://coveralls.io/github/Alorel/mongoose-transform-field-plugin?branch=master)
8+
[![Greenkeeper badge](https://badges.greenkeeper.io/Alorel/mongoose-transform-field-plugin.svg)](https://greenkeeper.io/)
9+
![Supports Node >= 6](https://img.shields.io/badge/Node-%3E=6.0.0-brightgreen.svg)
10+
![Supports Mongoose >= 5.0](https://img.shields.io/badge/Mongoose-%3E=5.0.0-brightgreen.svg)
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+
```

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "mongoose-transform-field-plugin",
33
"version": "1.0.0",
4-
"description": "Easily apply field transformation middleware",
5-
"main": "dist/plugin.js",
4+
"description": "An automatic field transformation plugin for Mongoose 5",
5+
"main": "dist/index.js",
66
"engines": {
77
"node": ">=6.0"
88
},
99
"scripts": {
1010
"clean": "rimraf dist",
1111
"build": "npm run clean && tsc",
12+
"doctoc": "doctoc --github README.md",
1213
"typecheck": "tsc --noEmit",
1314
"typecheck:watch": "npm run typecheck -- --watch",
1415
"lint": "tslint -p ./tsconfig.test.json -s node_modules/custom-tslint-formatters/formatters -t grouped",
@@ -57,6 +58,7 @@
5758
"chai": "^4.1.2",
5859
"coveralls": "^3.0.0",
5960
"custom-tslint-formatters": "^2.1.1",
61+
"doctoc": "^1.3.0",
6062
"env-cmd": "^7.0.0",
6163
"lodash": "^4.17.5",
6264
"mocha": "^5.0.0",

src/normalise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace normalise {
5757
* @param fields Fields to normalise. The keys should be the source, non-normalised fields while the values will hold
5858
* the normalised output.
5959
*/
60-
export function plugin(schema: Schema, fields: { [sourceField: string]: string }): void {
60+
export function plugin(schema: Schema, fields?: any): void {
6161
normalise(schema, fields);
6262
}
6363
}

src/transformAsync.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ import {AsyncTransform} from '../types/Transform';
99
* @param field Field to apply the transformation to
1010
* @param transformer The transformation function. This should return a Promise.
1111
*/
12-
function transformAsync(schema: Schema, field: string, transformer: AsyncTransform<any>): void;
12+
function transformAsync<T = any>(schema: Schema, field: string, transformer: AsyncTransform<T>): void;
1313
/**
1414
* Perform an asynchronous transformation on the given field
1515
* @param schema Schema to apply the transformation to
1616
* @param field Field to apply the transformation to
1717
* @param parallel Whether or not this should run as parallel middleware
1818
* @param transformer The transformation function. This should return a Promise.
1919
*/
20-
function transformAsync(schema: Schema, field: string, parallel: boolean, transformer: AsyncTransform<any>): void;
21-
function transformAsync(schema: Schema,
22-
field: string,
23-
parallelOrTransformer: boolean | AsyncTransform<any>,
24-
possibleTransformer?: AsyncTransform<any>): void {
20+
function transformAsync<T = any>(schema: Schema,
21+
field: string,
22+
parallel: boolean,
23+
transformer: AsyncTransform<T>): void;
24+
function transformAsync<T>(schema: Schema,
25+
field: string,
26+
parallelOrTransformer: boolean | AsyncTransform<T>,
27+
possibleTransformer?: AsyncTransform<T>): void {
2528
if (!schema) {
2629
throw new TypeError('Schema is required');
2730
} else if (typeof field !== 'string' || !field) {
@@ -115,7 +118,7 @@ function transformAsync(schema: Schema,
115118

116119
namespace transformAsync {
117120
/** Options for applying the plugin as a schema plugin */
118-
export interface TransformAsyncOptions {
121+
export interface TransformAsyncOptions<T = any> {
119122
/** Field to apply the transformation to */
120123
field: string;
121124
/**
@@ -124,20 +127,25 @@ namespace transformAsync {
124127
*/
125128
parallel?: boolean;
126129
/** The transformation function. This should return a Promise. */
127-
transformer: AsyncTransform<any>;
130+
transformer: AsyncTransform<T>;
128131
}
129132

130133
/**
131134
* Perform an asynchronous transformation on the given field
132135
* @param schema Schema to apply the transformation to
133136
* @param options Configuration
134137
*/
135-
export function plugin(schema: Schema, options: TransformAsyncOptions): void {
138+
export function plugin(schema: Schema, options?: any): void {
136139
if (!options) {
137140
throw new TypeError('Options are required');
138141
}
139142

140-
transformAsync(schema, options.field, !!options.parallel, options.transformer);
143+
transformAsync(
144+
schema,
145+
(<TransformAsyncOptions>options).field,
146+
!!<any>(<TransformAsyncOptions>options).parallel,
147+
(<TransformAsyncOptions>options).transformer
148+
);
141149
}
142150
}
143151

src/transformSync.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {transformSyncInternal} from './_transformSyncInternal';
88
* @param field Field to apply the transformation to
99
* @param transformer The transformation function.
1010
*/
11-
function transformSync(schema: Schema, field: string, transformer: SyncTransform<any>): void {
11+
function transformSync<T = any>(schema: Schema, field: string, transformer: SyncTransform<T>): void {
1212
if (typeof field !== 'string' || !field) {
1313
throw new TypeError('The field must be a non-empty string');
1414
} else if (typeof transformer !== 'function') {
@@ -25,24 +25,24 @@ function transformSync(schema: Schema, field: string, transformer: SyncTransform
2525

2626
namespace transformSync {
2727
/** Synchronous transformer options */
28-
export interface TransformSyncOptions {
28+
export interface TransformSyncOptions<T = any> {
2929
/** Field to apply the transformation to */
3030
field: string;
3131
/** The transformation function. */
32-
transformer: SyncTransform<any>;
32+
transformer: SyncTransform<T>;
3333
}
3434

3535
/**
3636
* Perform a synchronous transformation on the given field
3737
* @param schema Schema to apply the transformation to
3838
* @param options Configuration
3939
*/
40-
export function plugin(schema: Schema, options: TransformSyncOptions): void {
40+
export function plugin(schema: Schema, options?: any): void {
4141
if (!options) {
4242
throw new Error('Options are required');
4343
}
4444

45-
transformSync(schema, options.field, options.transformer);
45+
transformSync(schema, (<TransformSyncOptions>options).field, (<TransformSyncOptions>options).transformer);
4646
}
4747
}
4848

types/Transform.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {Thenable} from './Thenable';
22

3+
/** A synchronous transformation function */
34
export type SyncTransform<T = any> = (val: T) => T;
5+
/** An asynchronous transformation function */
46
export type AsyncTransform<T = any> = (val: T) => Thenable<T>;

types/_Update.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @internal */
12
export interface Update {
23
$set: any;
34
$setOnInsert: any;

update-version.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs');
2+
const {join} = require('path');
3+
4+
const path = join(__dirname, 'README.md');
5+
let contents = fs.readFileSync(path, 'utf8');
6+
const version = require('./package').version;
7+
8+
contents = contents.replace(/\?branch=master/g, `?branch=${version}`);
9+
fs.writeFileSync(path, contents);

0 commit comments

Comments
 (0)