1 | "use strict"
|
2 |
|
3 | const DAO = require("@motif/dao")
|
4 | const _ = require("lodash")
|
5 |
|
6 | class Model {
|
7 |
|
8 | get design() {
|
9 | return {
|
10 | type: "unset"
|
11 | }
|
12 | }
|
13 |
|
14 | get connection() {
|
15 | if (this._collection) {
|
16 | return this._collection.connection
|
17 | }
|
18 |
|
19 | return null
|
20 | }
|
21 |
|
22 | get db() {
|
23 | if (this._dao) {
|
24 | return this._dao.db
|
25 | }
|
26 | if (this._collection) {
|
27 | return this._collection.db
|
28 | }
|
29 | return null
|
30 | }
|
31 |
|
32 | get config() {
|
33 | return this._config
|
34 | }
|
35 |
|
36 | constructor({ collection, design, config }) {
|
37 | this._collection = collection || null
|
38 | this._dao = design || null
|
39 | this._config = config
|
40 | }
|
41 |
|
42 | createDesign() {
|
43 | let designConfig = this.design
|
44 |
|
45 | if (designConfig && designConfig.type !== "unset") {
|
46 | this._dao = new DAO.Design(designConfig)
|
47 | }
|
48 |
|
49 | return this._dao
|
50 | }
|
51 |
|
52 | setCollection(collection) {
|
53 | this._collection = collection
|
54 | }
|
55 |
|
56 | setConnection(connection) {
|
57 | if (this._dao) {
|
58 | let collection = this._dao.createCollection(connection)
|
59 | this.setCollection(collection)
|
60 | }
|
61 | }
|
62 |
|
63 | initialize() {
|
64 | if (this._dao) {
|
65 | return this._dao.initialize({
|
66 | autoCreate: process.motif.context.initMode ? true : false
|
67 | })
|
68 | }
|
69 | return false
|
70 | }
|
71 |
|
72 | constants(key) {
|
73 | let constants = process.motif.constants || {}
|
74 | return constants[key.toUpperCase()] || {}
|
75 | }
|
76 |
|
77 | model(key, connection = null) {
|
78 | let models = process.motif.models
|
79 | let modelData = models[key]
|
80 | if (!modelData) {
|
81 | return null
|
82 | }
|
83 |
|
84 | let config = this.config
|
85 | let design = modelData.design
|
86 | let model = new modelData.constructor({ config, design })
|
87 | if (connection) {
|
88 | model.setCollection(design.createCollection(connection))
|
89 | }
|
90 |
|
91 | return model
|
92 | }
|
93 |
|
94 | defaultTable() {
|
95 | let db = this.db
|
96 | if (this._dao && this._dao.defaultSchema && this._dao.defaultSchema.name) {
|
97 | return db.table(this._dao.defaultSchema.name)
|
98 | }
|
99 | return db.table()
|
100 | }
|
101 |
|
102 | filterData( origin, options = {} ) {
|
103 | let res = options.excludesAll === true ? {} : origin
|
104 | let mapKeys = _.isObject(options.mapKeys) ? options.mapKeys : {}
|
105 | let includes = _.isObject(options.includes) ? options.includes : {}
|
106 | let excludes = _.isArray(options.excludes) ? options.excludes : []
|
107 |
|
108 | if (options.includes) {
|
109 | _.forEach(includes, (value, key) => {
|
110 | if (_.isFunction(value)) {
|
111 | res[key] = value(origin)
|
112 | }
|
113 | else {
|
114 | res[key] = value
|
115 | }
|
116 | })
|
117 | }
|
118 |
|
119 | if (_.isObject(res)) {
|
120 | if (options.mapKeys) {
|
121 | res = _.mapKeys(res, (value, key ) => {
|
122 | let newKey = key
|
123 | if (mapKeys[key]) {
|
124 | newKey = mapKeys[key]
|
125 | }
|
126 | return newKey
|
127 | })
|
128 | }
|
129 |
|
130 | if (options.excludes) {
|
131 | _.forEach(res, (value, key) => {
|
132 | if (value === null || _.includes(excludes, key)) {
|
133 | delete res[key]
|
134 | }
|
135 | })
|
136 | }
|
137 | }
|
138 |
|
139 | if (options.sortKeys) {
|
140 | const data = {};
|
141 | Object.keys(res).sort().forEach(function(key) {
|
142 | data[key] = res[key];
|
143 | })
|
144 | return data
|
145 | }
|
146 |
|
147 | return Object.assign({}, res)
|
148 | }
|
149 |
|
150 | findOne( res, defaultValue = null ) {
|
151 | return (res && res.length > 0) ? res[0] : defaultValue
|
152 | }
|
153 |
|
154 | getTotal( res, field = "total", defaultValue = 0 ) {
|
155 | return (res && res.length > 0) ? res[0][field] : defaultValue
|
156 | }
|
157 |
|
158 | async dbTask(_arg1, _arg2) {
|
159 | let models = arguments.length === 2 && _.isArray(_arg1) ? _arg1 : arguments.length === 2 && [_arg1] || []
|
160 | let callback = arguments.length === 2 && _.isFunction(_arg2) ? _arg2 : _.isFunction(_arg1) && _arg1
|
161 |
|
162 | return await this.db.task(async connection => {
|
163 | this.setConnection(connection)
|
164 | _.forEach(models, model => {
|
165 | model.setConnection(connection)
|
166 | })
|
167 | return await callback(connection)
|
168 | })
|
169 | }
|
170 |
|
171 | async dbTransaction(_arg1, _arg2) {
|
172 | let models = arguments.length === 2 && _.isArray(_arg1) ? _arg1 : arguments.length === 2 && [_arg1] || []
|
173 | let callback = arguments.length === 2 && _.isFunction(_arg2) ? _arg2 : _.isFunction(_arg1) && _arg1
|
174 |
|
175 | return await this.db.transaction(async connection => {
|
176 | this.setConnection(connection)
|
177 | _.forEach(models, model => {
|
178 | model.setConnection(connection)
|
179 | })
|
180 | return await callback(connection)
|
181 | })
|
182 | }
|
183 |
|
184 | static dbConnection(connectionName) {
|
185 | return DAO.connection(connectionName)
|
186 | }
|
187 |
|
188 | static get prefix() {
|
189 | return process.motif.context.prefix
|
190 | }
|
191 | }
|
192 |
|
193 | module.exports = Model
|