File size: 7,691 Bytes
e4bf523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use strict'
/**

 * Copyright (c) 2017~2021, OBCon Inc.

 * All rights reserved.

 */

/**

 * @file

 * @copyright 2017~2021, OBCon Inc.

 * @author gye hyun james kim [pnuskgh@gmail.com]

 */

 //--- https://sequelize.org/docs/v6/, 6.12.2
 //--- http://docs.sequelizejs.com/
 //--- https://zetawiki.com/wiki/MySQL_컬럼_추가
 //---     ALTER TABLE `employee` ADD `comments` VARCHAR(200) NOT NULL;
let Sequelize = require('sequelize');

//--- MySQL의 필드 타입
//--- 전체 레코드의 최대 크기 : 64KB (65,535 bytes)
//--- https://goodgid.github.io/JS-char-vs-varchar/
//---     varchar   : 64KB. 동일한 공간에 저장. 인덱스 생성 가능
//---     text      : 64KB. 별도 공간에 저장. Full text 검색 가능
//---         mediumtext (16MB), longtext (4GB)
//---     blob      : 바이너리 데이터를 저장. text와 유사
class Model {
    constructor() {
        this._use = true;                                   //--- true. 사용하는 모듈

        this._title = '';
        this._database = null;
        this._tableName = null;
        this._table = null;
        this._define = config.databases.scada.define;
        this._timestamps = true;
    }

    get title() {
        return this._title;
    }

    set title(value) {
        this._title = value;
    }

    get database() {
        return this._database;
    }

    set database(value) {
        this._database = value;
    }

    get tableName() {
        return this._tableName;
    }

    set tableName(value) {
        this._tableName = value;
    }

    get table() {
        if (this._table == null) {
            this._table = this.define;
        }
        return this._table;
    }

    set table(value) {
        this._table = value;
    }

    get fieldNames() {
        return Object.getOwnPropertyNames(this.fields);
    }

    //--- 필드 정의
    get fieldDesc() {
        return {
            name: {
                type: 'string',         len: 72,
                allowNull: false,
                defaultValue: '',
                validate: {},
                title: '이름',
                strType: 'string'
            },
            description: {
                type: 'text',
                allowNull: true,
                defaultValue: '',
                validate: {},
                title: '상세 설명',
                strType: 'text'
            },
            assignedUserId: {
                type: 'integer',        len: 11,
                allowNull: false,
                defaultValue: 1,
                validate: {
                    min: 1
                },
                title: '담당자 ID',
                strType: 'integer'
            },
            assignedUserName: {
                type: 'string',         len: 72,
                allowNull: true,
                defaultValue: '',
                validate: {},
                title: '담당자',
                strType: 'string'
            },
            createdBy: {
                type: 'integer',        len: 11,
                allowNull: false,
                defaultValue: 1,
                validate: {
                    min: 1
                },
                title: '생성자',
                strType: 'integer'
            },
            updatedBy: {
                type: 'integer',        len: 11,
                allowNull: false,
                defaultValue: 1,
                validate: {
                    min: 1
                },
                title: '수정자',
                strType: 'integer'
            },
            deleted: {
                type: 'boolean',
                allowNull: false,
                defaultValue: false,
                validate: {},
                title: '삭제',
                strType: 'boolean'
            }
        };
    }

    //--- Sequelize.DataTypes : https://sequelize.org/docs/v6/moved/data-types/
    //---    문자열 : STRING, STRING(${len}), STRING.BINARY, TEXT, TEXT('tiny')
    //---    숫자   : INTEGER, BIGINT, BIGINT(${len})
    //---             FLOAT, FLOAT(~), FLOAT(~, ~) / DOUBLE, DOUBLE(~), DOUBLE(~, ~) / DECIMAL, DECIMAL(~, ~)
    //---    날자   : DATE, DATEONLY
    //---        DATE     : moment(), moment.utc().format('YYYY-MM-DD HH:mm:ss'), 'YYYY-MM-DDTHH:MM:SSZ', "2016-01-01 00:00:00+00:00"
    //---        DATEONLY : 'YYYY-MM-DD'
    //---    논리   : BOOLEAN
    desc2field(desc) {
        let field = {};

        switch (desc.type) {
        case 'string':
            field.type = Sequelize.STRING(desc.len);
            break;
        case 'text':
            field.type = Sequelize.TEXT;
            break;

        case 'integer':
            field.type = Sequelize.INTEGER(desc.len);
            break;
        case 'bigint':
            field.type = Sequelize.BIGINT(desc.len);
            break;
        case 'float':
            field.type = Sequelize.FLOAT;
            break;
        case 'decimal':
            field.type = Sequelize.DECIMAL(desc.len, desc.lenSub);
            break;

        case 'date':
            field.type = Sequelize.DATE;
            break;

        case 'dateonly':
            field.type = Sequelize.DATEONLY;
            break;

        case 'boolean':
            field.type = Sequelize.BOOLEAN;
            break;

        default:
            field.type = Sequelize.STRING(desc.len);
            break;
        }

        for (let name of ['allowNull', 'defaultValue', 'validate', 'title', 'strType', 'enum']) {
            if (typeof(desc[name]) != 'undefined') {
                field[name] = desc[name];
            }
        }
        return field;
    }

    get fields() {
        const rtFields = {};
        for (let key in this.fieldDesc) {
            rtFields[key] = this.desc2field(this.fieldDesc[key]);
        }
        return rtFields;
    }

    getTypeEnum() {
        const typeEnum = [ 
            ['1', '정류기'], ['2', '원격 TB'], ['3', '배관링크'], ['4', '수위센서'], ['5', '관말압력'],
            ['6', '특정정압기'], ['7', '수신감도']
        ];
        if ((config.service.name == 'service_scada_caprotec') || (config.service.name == 'service_scada_ursc')) {
            typeEnum.push(['8', '범용장치1']);
            typeEnum.push(['A', '두께측정장치']);
        } else {
            typeEnum.push(['8', '범용장비1']);
            typeEnum.push(['A', '두께측정장비']);
        }
        typeEnum.push(['B', '정압기 압력 자동 조절 장치']);
        return typeEnum;
    }

    //--- Database 정의
    //---     id    : auto_increment
    //---     createdAt, updatedAt
    //--- http://docs.sequelizejs.com/manual/tutorial/models-definition.html#validations
    get define() {
        if (this._timestamps) {
            this._define.timestamps = true;
            return this.database.define(this.tableName, this.fields, this._define);
        } else {
            this._define.timestamps = false;
            return this.database.define(this.tableName, this.fields, this._define);
        }
    }

    //--- 데이터 초기화
    init() {
        if (this.define != null) {
            this.table.sync();                              //--- Table이 없는 경우 생성

            // new Sequelize().define().sync().then();
            // new Sequelize().define().findAll()
            // new Sequelize().define().update
        } 
    }
}

module.exports = Model;