schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var __1 = require("../../");
var ColumnTypeUndefinedError_1 = require("../../error/ColumnTypeUndefinedError");
/**
 * Column decorator is used to mark a specific class property as a table column.
 * Only properties decorated with this decorator will be persisted to the database when entity be saved.
 */
function Column(typeOrOptions, options) {
    return function (object, propertyName) {
        // normalize parameters
        var type;
        if (typeof typeOrOptions === "string" || typeOrOptions instanceof Function) {
            type = typeOrOptions;
        }
        else if (typeOrOptions) {
            options = typeOrOptions;
            type = typeOrOptions.type;
        }
        if (!options)
            options = {};
        // if type is not given explicitly then try to guess it
        var reflectMetadataType = Reflect && Reflect.getMetadata ? Reflect.getMetadata("design:type", object, propertyName) : undefined;
        if (!type && reflectMetadataType) // if type is not given explicitly then try to guess it
            type = reflectMetadataType;
        // check if there is no type in column options then set type from first function argument, or guessed one
        if (!options.type && type)
            options.type = type;
        // specify HSTORE type if column is HSTORE
        if (options.type === "hstore" && !options.hstoreType)
            options.hstoreType = reflectMetadataType === Object ? "object" : "string";
        if (typeOrOptions instanceof Function) { // register an embedded
            __1.getMetadataArgsStorage().embeddeds.push({
                target: object.constructor,
                propertyName: propertyName,
                isArray: reflectMetadataType === Array || options.array === true,
                prefix: options.prefix !== undefined ? options.prefix : undefined,
                type: typeOrOptions
            });
        }
        else { // register a regular column
            // if we still don't have a type then we need to give error to user that type is required
            if (!options.type)
                throw new ColumnTypeUndefinedError_1.ColumnTypeUndefinedError(object, propertyName);
            // create unique
            if (options.unique === true)
                __1.getMetadataArgsStorage().uniques.push({ target: object.constructor, columns: [propertyName] });
            __1.getMetadataArgsStorage().columns.push({
                target: object.constructor,
                propertyName: propertyName,
                mode: "regular",
                options: options
            });
            if (options.generated) {
                __1.getMetadataArgsStorage().generations.push({
                    target: object.constructor,
                    propertyName: propertyName,
                    strategy: typeof options.generated === "string" ? options.generated : "increment"
                });
            }
        }
    };
}
exports.Column = Column;
 
//# sourceMappingURL=Column.js.map