/
Decorators.js
131 lines (130 loc) · 5.79 KB
/
Decorators.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
"use strict";
var MongoDB = require('mongodb');
var _ = require('lodash');
var Skmatc = require('skmatc');
var Transforms_1 = require('./Transforms');
/**
* Specifies the name of the collection to which this instance's documents should be sent.
* @param name The name of the MongoDB collection to store the documents in.
*
* This decorator replaces the use of the static collection property on instance implementation
* classes. If your transpiler does not support decorators then you are free to make use of the
* property instead.
*/
function Collection(name) {
return function (target) {
target.collection = name;
};
}
exports.Collection = Collection;
/**
* Specifies a MongoDB collection level index to be managed by Iridium for this instance type.
* More than one instance of this decorator may be used if you wish to specify multiple indexes.
* @param spec The formal index specification which defines the properties and ordering used in the index.
* @param options The options dictating the way in which the index behaves.
*
* This decorator replaces the use of the static indexes property on instance implementation
* classes. If your transpiler does not support decorators then you are free to make use of the
* property instead.
*/
function Index(spec, options) {
return function (target) {
target.indexes = (target.indexes || []).concat({ spec: spec, options: options || {} });
};
}
exports.Index = Index;
/**
* Specifies a custom validator to be made available for this collection's schema.
* More than one instance of this decorator may be used if you wish to specify multiple validators.
* @param forType The value in the schema which will be delegated to this function for validation.
* @param validate A function which calls this.assert(condition) to determine whether a schema node is valid or not.
*
* This decorator replaces the use of the static validators property on instance implementation
* classes. If your transpiler does not support decorators then you are free to make use of the
* property instead.
*/
function Validate(forType, validate) {
return function (target) {
target.validators = (target.validators || []).concat(Skmatc.create(function (schema) { return schema === forType; }, validate));
};
}
exports.Validate = Validate;
function Property() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
var name = null, asType = false, required = true;
if (args.length > 1 && typeof args[args.length - 1] === 'boolean')
required = args.pop();
return function (target, property) {
var staticTarget = target;
if (!property)
name = args.shift();
else {
name = property;
staticTarget = target.constructor;
}
asType = args.pop() || false;
staticTarget.schema = _.clone(staticTarget.schema || { _id: false });
if (!required && typeof asType !== 'boolean')
staticTarget.schema[name] = { $required: required, $type: asType };
else
staticTarget.schema[name] = asType;
};
}
exports.Property = Property;
/**
* Specifies a custom transform to be applied to the property this decorator is applied to.
*
* @param fromDB The function used to convert values from the database for the application.
* @param toDB The function used to convert values from the application to the form used in the database.
*
* This decorator can either compliment or replace the static transforms property on your instance
* class, however only one transform can be applied to any property at a time.
* If your transpiler does not support decorators then you are free to make use of the
* property instead.
*
* If this decorator is applied to the instance class itself, as opposed to a property, then
* it will be treated as a $document transformer - and will receive the full document as opposed
* to individual property values. Similarly, it is expected to return a full document when either
* fromDB or toDB is called.
*/
function Transform(fromDB, toDB) {
return function (target, property) {
if (property === void 0) { property = '$document'; }
var staticTarget = (target.constructor || target);
staticTarget.transforms = _.clone(staticTarget.transforms || {});
staticTarget.transforms[property] = {
fromDB: fromDB,
toDB: toDB
};
};
}
exports.Transform = Transform;
/**
* Specifies that this property should be treated as an ObjectID, with the requisite validator and transforms.
*
* This decorator applies an ObjectID validator to the property, which ensures that values sent to the database
* are instances of the MongoDB ObjectID type, as well as applying a transform operation which converts ObjectIDs
* to strings for your application, and then converts strings back to ObjectIDs for the database.
*/
function ObjectID(target, name) {
Property(MongoDB.ObjectID)(target, name);
Transform(Transforms_1.DefaultTransforms.ObjectID.fromDB, Transforms_1.DefaultTransforms.ObjectID.toDB)(target, name);
}
exports.ObjectID = ObjectID;
/**
* Specifies that this property should be stored using the MongoDB binary type and represented as a Buffer.
*
* This decorator applies a Buffer validator to the property, which ensures that values you send to the database
* are well formatted Buffer objects represented using the BSON Binary datatype. In addition to this, it will
* apply a transform which ensures you only work with Buffer objects and that data is always stored in Binary
* format.
*/
function Binary(target, name) {
Property(Buffer)(target, name);
Transform(Transforms_1.DefaultTransforms.Binary.fromDB, Transforms_1.DefaultTransforms.Binary.toDB)(target, name);
}
exports.Binary = Binary;
//# sourceMappingURL=Decorators.js.map