UNPKG

2.42 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __spreadArrays = (this && this.__spreadArrays) || function () {
14 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
15 for (var r = Array(s), k = 0, i = 0; i < il; i++)
16 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
17 r[k] = a[j];
18 return r;
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21exports.ManyToOne = void 0;
22require("reflect-metadata");
23var type_graphql_1 = require("type-graphql");
24var typeorm_1 = require("typeorm");
25var decorators_1 = require("../decorators");
26var utils_1 = require("../utils");
27function ManyToOne(parentType, joinFunc, options) {
28 if (options === void 0) { options = {}; }
29 // Need to grab the class name from within a decorator
30 var klass;
31 var extractClassName = function (target) {
32 klass = target.constructor.name;
33 };
34 // This Decorator creates the foreign key field for the association so that the consumer
35 // Doesn't need to hand roll this each time by doing somethign like:
36 // @StringField()
37 // userId?: ID;
38 var createForeignKeyField = function (target, propertyKey, descriptor) {
39 klass = target.constructor.name;
40 Reflect.defineProperty(target, klass + "Id", {});
41 decorators_1.IdField(options)(target, propertyKey + "Id", descriptor);
42 };
43 // NOTE: this is unnecessary, but I'm keeping it around because otherwise it will generate the schema properties in a different order
44 // It could otherwise safely be deleted
45 var graphQLdecorator = [
46 type_graphql_1.Field(parentType, __assign({ nullable: true }, options))
47 ];
48 // END NOTE
49 var factories = __spreadArrays([
50 extractClassName
51 ], graphQLdecorator, [
52 typeorm_1.ManyToOne(parentType, joinFunc, options),
53 typeorm_1.JoinColumn(),
54 createForeignKeyField
55 ]);
56 return utils_1.composeMethodDecorators.apply(void 0, factories);
57}
58exports.ManyToOne = ManyToOne;
59//# sourceMappingURL=ManyToOne.js.map
\No newline at end of file