-
Notifications
You must be signed in to change notification settings - Fork 123
/
FieldSelectorSpec.js
92 lines (84 loc) · 3.63 KB
/
FieldSelectorSpec.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
import MapSelectorSpec from './MapSelectorSpec';
import ModelSelectorSpec from './ModelSelectorSpec';
import ModelBasedSelectorSpec from './ModelBasedSelectorSpec';
import idArgSelector from './idArgSelector';
import QuerySet from '../QuerySet';
import Model from '../Model';
import ForeignKey from '../fields/ForeignKey';
import ManyToMany from '../fields/ManyToMany';
export default class FieldSelectorSpec extends ModelBasedSelectorSpec {
constructor({
field, fieldModel, accessorName, isVirtual, ...other
}) {
super(other);
this._field = field;
this._fieldModel = fieldModel;
this._accessorName = accessorName;
this._isVirtual = isVirtual;
}
get key() {
return this._accessorName;
}
get dependencies() {
return [this._orm, idArgSelector];
}
valueForInstance(instance, session) {
if (!instance) return null;
let value;
if (this._parent instanceof ModelSelectorSpec) {
value = instance[this._accessorName];
} else if (this._parent instanceof FieldSelectorSpec) {
const {
[this._parent.toModelName]: ParentToModel,
} = session;
const parentRef = this._parent.valueForInstance(instance, session);
const parentInstance = parentRef ? new ParentToModel(parentRef) : null;
value = parentInstance ? parentInstance[this._accessorName] : null;
}
if (value instanceof Model) {
return value.ref;
}
if (value instanceof QuerySet) {
return value.toRefArray();
}
return value;
}
map(selector) { /* eslint-disable no-underscore-dangle */
if (selector instanceof ModelSelectorSpec) {
if (this.toModelName === selector._model.modelName) {
throw new Error(`Cannot select models in a \`map()\` call. If you just want the \`${this._accessorName}\` as a ref array then you can simply drop the \`map()\`. Otherwise make sure you're passing a field selector of the form \`${this.toModelName}.<field>\` or a custom selector instead.`);
} else {
throw new Error(`Cannot select \`${selector._model.modelName}\` models in this \`map()\` call. Make sure you're passing a field selector of the form \`${this.toModelName}.<field>\` or a custom selector instead.`);
}
} else if (selector instanceof FieldSelectorSpec) {
if (this.toModelName !== selector._model.modelName) {
throw new Error(`Cannot select fields of the \`${selector._model.modelName}\` model in this \`map()\` call. Make sure you're passing a field selector of the form \`${this.toModelName}.<field>\` or a custom selector instead.`);
}
} else if (
!selector ||
typeof selector !== 'function' ||
!selector.recomputations
) {
throw new Error(`\`map()\` requires a selector as an input. Received: ${JSON.stringify(selector)} of type ${typeof selector}`);
}
if (
!(this._field instanceof ForeignKey) &&
!(this._field instanceof ManyToMany)
) {
throw new Error('Cannot map selectors for non-collection fields');
}
return new MapSelectorSpec({
parent: this,
model: this._model,
orm: this._orm,
field: this._field,
accessorName: this._accessorName,
selector,
});
}
get toModelName() {
return (this._field.toModelName === 'this')
? this._fieldModel.modelName
: this._field.toModelName;
}
}