-
Notifications
You must be signed in to change notification settings - Fork 1
/
document.js
107 lines (96 loc) · 2.67 KB
/
document.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
/*jshint laxcomma: true, smarttabs: true, esnext: true, node: true*/
'use strict';
/**
* Handles nested documents
* @module tastypie-rethink/lib/fields/docuemnt
* @author Eric Satterwhite
* @since 3.0.0
* @requires tastypie/lib/class
* @requires tastypie/lib/fields/api
* @requires tastypie/lib/utility
*/
let tastypie = require( 'tastypie' )
, ApiField = require( 'tastypie/lib/fields/api' )
, toModule = require( 'tastypie/lib/utility' ).toModule
, Class = tastypie.Class
, Document
;
/**
* @constructor
* @alias module:tastypie-rethink/lib/fields/document
* @param {Object} options
* @param {module:tastypie/lib/resource} options.to A resource class to represent a related object
* @param {String|Number|Function} [options.default=Object.create(null)] Default value for the field value
*/
Document = new Class({
inherits: ApiField
,is_nested: true
,options:{
to: null
,default: function(){
return Object.create(null)
}
}
,constructor: function( options ){
this.parent('constructor', options );
this.instance = new this.cls();
}
/**
* Converts a serialized value into a full resource instance value
* @method module:tastypie-rethink/lib/fields/document#hydrate
* @param {module:tastypie/lib/resource~Bundle} bundle
**/
,hydrate: function( bundle, cb ){
this.parent('hydrate', bundle,(err, value) => {
if( err ){
return cb(err);
}
this.instance.full_hydrate( { req:bundle.req, res:bundle.res, data:value}, ( err, result ) => {
cb( err, result.object );
});
});
}
/**
* Used to distil an object in to something suitable for serialization
* @method module:tastypie-rethink/lib/field/document#to_minimal
* @param {Mixed} obj A value to dehydrate
* @param {Function} callback A node style callback to be called when execution is complete
**/
,dehydrate: function( obj, cb ){
if( !this.instance.options.apiname ){
this.instance.setOptions({
apiname: this.resource.options.apiname
});
}
obj = obj.toJSON ? obj.toJSON() : obj;
var attribute = this.options.name;
this.parent('dehydrate',obj, ( err, value ) => {
if( err || !value ){
return cb && cb( err, value );
}
this.instance.full_dehydrate( obj[attribute] ? obj[attribute] : value , null, cb );
});
}
,type: function(){
return "object"
}
});
Object.defineProperties(Document.prototype,{
cls: {
enumerable: false
,writeable: false
,get: function(){
if( typeof this.options.to === 'string'){
return toModule( this.options.to );
}
return this.options.to;
}
}
});
Object.defineProperty(tastypie.fields, 'document', {
configurable:false
,get: function(){
return Document;
}
});
module.exports = Document;