-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
141 lines (115 loc) · 3.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
//dependencies
const _ = require('lodash');
const { include } = require('@lykmapipo/include');
require('@lykmapipo/mongoose-common');
//common plugins
const fake = require('@lykmapipo/mongoose-faker');
const search = require('mongoose-regex-search');
const autopopulate = require('mongoose-autopopulate');
const hide = require('mongoose-hidden');
const exist = require('mongoose-exists');
const taggable = require('@lykmapipo/mongoose-taggable');
const aggregatable = require('@lykmapipo/mongoose-aggregatable');
//constants
const defaultHidden = ({
defaultHidden: {
password: true,
__v: true,
__t: true
},
virtuals: {
id: 'hideJSON',
runInBackgroundQueue: 'hide',
runInBackgroundOptions: 'hide'
}
});
//rest actions plugin
const del = include(__dirname, 'lib', 'delete');
const get = include(__dirname, 'lib', 'get');
const patch = include(__dirname, 'lib', 'patch');
const post = include(__dirname, 'lib', 'post');
const put = include(__dirname, 'lib', 'put');
/**
* @module mongoose-rest-actions
* @name restActions
* @function restActions
* @description mongoose schema plugins to support http verb(s)
* @param {Schema} schema valid mongoose schema
* @param {Object} [schemaOptns] valid delete plugin options
* @param {String} [schemaOptns.root] a field name to use to hold results.
* default to `data`
*
* @author lally elias<[email protected]>
* @version 0.18.0
* @since 0.1.0
* @example
*
* const { Schema } = require('mongoose');
* const actions = require('mongoose-rest-actions');
*
* const User = new Schema({
* name: { type: String }
* });
* User.plugin(actions);
*
*/
module.exports = exports = function restActions(schema, schemaOptns) {
/* @todo refactor and simplify */
//ignore if already plugged-in
if (schema.statics.get) {
return;
}
//normalize options
const schemaOptions = _.merge({}, {
root: 'data'
}, schemaOptns);
//ensure indexed timestamps fields
//currently mongoose does not index them
//see https://github.com/Automattic/mongoose/blob/master/lib/schema.js#L1002
const hasTimeStamps = _.get(schema, 'options.timestamps', false);
if (hasTimeStamps) {
//obtain timestamps paths
const createdAtField =
(_.isBoolean(hasTimeStamps) ? 'createdAt' : hasTimeStamps.createdAt);
schema.statics.CREATED_AT_FIELD = createdAtField;
const updatedAtField =
(_.isBoolean(hasTimeStamps) ? 'updatedAt' : hasTimeStamps.updatedAt);
schema.statics.UPDATED_AT_FIELD = updatedAtField;
//ensure index on create timestamp path if not exists
if (schema.paths[createdAtField]) {
schema.paths[createdAtField].options.index = true;
schema.index({
[createdAtField]: 1
});
}
//ensure index on update timestamp path if not exists
if (schema.paths[updatedAtField]) {
schema.paths[updatedAtField].options.index = true;
schema.index({
[updatedAtField]: 1
});
}
}
//extend schema with deletedAt timestamp
schema.add({
deletedAt: {
type: Date,
index: true
}
});
//rest actions plugin
get(schema, schemaOptions);
post(schema, schemaOptions);
put(schema, schemaOptions);
patch(schema, schemaOptions);
del(schema, schemaOptions);
//lastly common plugins
fake(schema, schemaOptions);
exist(schema, schemaOptions);
taggable(schema, schemaOptions);
search(schema, schemaOptions);
autopopulate(schema, schemaOptions);
aggregatable(schema, schemaOptions);
hide(defaultHidden)(schema, schemaOptions);
};