velocious 1.0.93 → 1.0.95
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/application.js +19 -0
- package/src/cli/base-command.js +1 -0
- package/src/cli/commands/db/migrate.js +2 -0
- package/src/cli/commands/generate/base-models.js +7 -0
- package/src/cli/commands/generate/model.js +1 -1
- package/src/configuration-resolver.js +6 -3
- package/src/configuration.js +27 -4
- package/src/database/drivers/base-column.js +69 -0
- package/src/database/drivers/base-columns-index.js +19 -0
- package/src/database/drivers/base-foreign-key.js +12 -0
- package/src/database/drivers/base-table.js +32 -0
- package/src/database/drivers/base.js +125 -3
- package/src/database/migrator.js +17 -2
- package/src/database/query/drop-table-base.js +8 -0
- package/src/database/query/index.js +42 -0
- package/src/database/query-parser/from-parser.js +17 -6
- package/src/database/query-parser/group-parser.js +21 -8
- package/src/database/record/index.js +12 -11
- package/src/database/record/relationships/base.js +29 -6
- package/src/database/table-data/index.js +198 -6
- package/src/environment-handlers/base.js +58 -0
- package/src/environment-handlers/browser.js +0 -24
- package/src/environment-handlers/node/cli/commands/generate/base-models.js +123 -0
- package/src/environment-handlers/node.js +6 -1
- package/src/initializer.js +4 -0
- package/src/logger.js +54 -2
- package/src/routes/base-route.js +25 -4
- package/src/routes/get-route.js +3 -1
- package/src/routes/namespace-route.js +3 -1
- package/src/routes/post-route.js +3 -1
- package/src/routes/resource-route.js +3 -1
- package/src/utils/backtrace-cleaner.js +10 -0
|
@@ -25,20 +25,37 @@ export default class TableData {
|
|
|
25
25
|
*/
|
|
26
26
|
addColumn(name, args = {}) {
|
|
27
27
|
if (name instanceof TableColumn) {
|
|
28
|
-
this.
|
|
28
|
+
this.getColumns().push(name)
|
|
29
29
|
} else {
|
|
30
30
|
const column = new TableColumn(name, args)
|
|
31
31
|
|
|
32
|
-
this.
|
|
32
|
+
this.getColumns().push(column)
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @returns {TableColumn[]}
|
|
38
|
+
*/
|
|
36
39
|
getColumns() { return this._columns }
|
|
37
40
|
|
|
41
|
+
/**
|
|
42
|
+
* @param {import("./table-foreign-key.js").default} foreignKey
|
|
43
|
+
*/
|
|
38
44
|
addForeignKey(foreignKey) { this._foreignKeys.push(foreignKey) }
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @returns {import("./table-foreign-key.js").default[]}
|
|
48
|
+
*/
|
|
39
49
|
getForeignKeys() { return this._foreignKeys }
|
|
40
50
|
|
|
51
|
+
/**
|
|
52
|
+
* @param {TableIndex} index
|
|
53
|
+
*/
|
|
41
54
|
addIndex(index) { this._indexes.push(index) }
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @returns {TableIndex[]}
|
|
58
|
+
*/
|
|
42
59
|
getIndexes() { return this._indexes }
|
|
43
60
|
|
|
44
61
|
/**
|
|
@@ -56,34 +73,209 @@ export default class TableData {
|
|
|
56
73
|
* @returns {boolean}
|
|
57
74
|
*/
|
|
58
75
|
getIfNotExists() { return this.args.ifNotExists }
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @returns {TableReference[]}
|
|
79
|
+
*/
|
|
59
80
|
getReferences() { return this._references }
|
|
60
81
|
|
|
82
|
+
/**
|
|
83
|
+
* @param {string} name
|
|
84
|
+
* @param {object} args
|
|
85
|
+
* @param {boolean} args.autoIncrement
|
|
86
|
+
* @param {any} args.default
|
|
87
|
+
* @param {boolean} args.dropColumn
|
|
88
|
+
* @param {boolean|object} args.foreignKey
|
|
89
|
+
* @param {boolean|object} args.index
|
|
90
|
+
* @param {number} args.maxLength
|
|
91
|
+
* @param {boolean} args.null
|
|
92
|
+
* @param {boolean} args.primaryKey
|
|
93
|
+
* @returns {void}
|
|
94
|
+
*/
|
|
61
95
|
bigint(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "bigint"}, args)) }
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {string} name
|
|
99
|
+
* @param {object} args
|
|
100
|
+
* @param {boolean} args.autoIncrement
|
|
101
|
+
* @param {any} args.default
|
|
102
|
+
* @param {boolean} args.dropColumn
|
|
103
|
+
* @param {boolean|object} args.foreignKey
|
|
104
|
+
* @param {boolean|object} args.index
|
|
105
|
+
* @param {number} args.maxLength
|
|
106
|
+
* @param {boolean} args.null
|
|
107
|
+
* @param {boolean} args.primaryKey
|
|
108
|
+
* @returns {void}
|
|
109
|
+
*/
|
|
62
110
|
blob(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "blob"}, args)) }
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {string} name
|
|
114
|
+
* @param {object} args
|
|
115
|
+
* @param {boolean} args.autoIncrement
|
|
116
|
+
* @param {any} args.default
|
|
117
|
+
* @param {boolean} args.dropColumn
|
|
118
|
+
* @param {boolean|object} args.foreignKey
|
|
119
|
+
* @param {boolean|object} args.index
|
|
120
|
+
* @param {number} args.maxLength
|
|
121
|
+
* @param {boolean} args.null
|
|
122
|
+
* @param {boolean} args.primaryKey
|
|
123
|
+
* @returns {void}
|
|
124
|
+
*/
|
|
63
125
|
boolean(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "boolean"}, args)) }
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {string} name
|
|
129
|
+
* @param {object} args
|
|
130
|
+
* @param {boolean} args.autoIncrement
|
|
131
|
+
* @param {any} args.default
|
|
132
|
+
* @param {boolean} args.dropColumn
|
|
133
|
+
* @param {boolean|object} args.foreignKey
|
|
134
|
+
* @param {boolean|object} args.index
|
|
135
|
+
* @param {number} args.maxLength
|
|
136
|
+
* @param {boolean} args.null
|
|
137
|
+
* @param {boolean} args.primaryKey
|
|
138
|
+
* @returns {void}
|
|
139
|
+
*/
|
|
64
140
|
datetime(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "datetime"}, args)) }
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {string} name
|
|
144
|
+
* @param {object} args
|
|
145
|
+
* @param {boolean} args.autoIncrement
|
|
146
|
+
* @param {any} args.default
|
|
147
|
+
* @param {boolean} args.dropColumn
|
|
148
|
+
* @param {boolean|object} args.foreignKey
|
|
149
|
+
* @param {boolean|object} args.index
|
|
150
|
+
* @param {number} args.maxLength
|
|
151
|
+
* @param {boolean} args.null
|
|
152
|
+
* @param {boolean} args.primaryKey
|
|
153
|
+
* @returns {void}
|
|
154
|
+
*/
|
|
65
155
|
integer(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "integer"}, args)) }
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @param {string} name
|
|
159
|
+
* @param {object} args
|
|
160
|
+
* @param {boolean} args.autoIncrement
|
|
161
|
+
* @param {any} args.default
|
|
162
|
+
* @param {boolean} args.dropColumn
|
|
163
|
+
* @param {boolean|object} args.foreignKey
|
|
164
|
+
* @param {boolean|object} args.index
|
|
165
|
+
* @param {number} args.maxLength
|
|
166
|
+
* @param {boolean} args.null
|
|
167
|
+
* @param {boolean} args.primaryKey
|
|
168
|
+
* @returns {void}
|
|
169
|
+
*/
|
|
170
|
+
json(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "json"}, args)) }
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @param {string} name
|
|
174
|
+
* @param {object} args
|
|
175
|
+
* @param {boolean} args.autoIncrement
|
|
176
|
+
* @param {any} args.default
|
|
177
|
+
* @param {boolean} args.dropColumn
|
|
178
|
+
* @param {boolean|object} args.foreignKey
|
|
179
|
+
* @param {boolean|object} args.index
|
|
180
|
+
* @param {number} args.maxLength
|
|
181
|
+
* @param {boolean} args.null
|
|
182
|
+
* @param {boolean} args.primaryKey
|
|
183
|
+
* @returns {void}
|
|
184
|
+
*/
|
|
66
185
|
tinyint(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "tinyint"}, args)) }
|
|
67
186
|
|
|
187
|
+
/**
|
|
188
|
+
* @param {string} name
|
|
189
|
+
* @param {object} args
|
|
190
|
+
* @param {boolean} args.autoIncrement
|
|
191
|
+
* @param {any} args.default
|
|
192
|
+
* @param {boolean} args.dropColumn
|
|
193
|
+
* @param {boolean|object} args.foreignKey
|
|
194
|
+
* @param {boolean|object} args.index
|
|
195
|
+
* @param {number} args.maxLength
|
|
196
|
+
* @param {boolean} args.null
|
|
197
|
+
* @param {boolean} args.primaryKey
|
|
198
|
+
* @returns {void}
|
|
199
|
+
*/
|
|
68
200
|
references(name, args = {}) {
|
|
69
201
|
const columnName = `${name}_id`
|
|
70
202
|
const reference = new TableReference(name, args)
|
|
71
|
-
const
|
|
203
|
+
const {polymorphic, ...restArgs} = args
|
|
204
|
+
const columnArgs = Object.assign({isNewColumn: true, type: "bigint"}, restArgs)
|
|
72
205
|
const column = new TableColumn(columnName, columnArgs)
|
|
73
206
|
const index = new TableIndex([column])
|
|
74
207
|
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
208
|
+
this.getColumns().push(column)
|
|
209
|
+
this.getIndexes().push(index)
|
|
210
|
+
this.getReferences().push(reference)
|
|
211
|
+
|
|
212
|
+
if (polymorphic) {
|
|
213
|
+
const typeColumnName = `${name}_type`
|
|
214
|
+
const typeColumn = new TableColumn(typeColumnName, {isNewColumn: true, type: "string"})
|
|
215
|
+
|
|
216
|
+
this.getColumns().push(typeColumn)
|
|
217
|
+
}
|
|
78
218
|
}
|
|
79
219
|
|
|
220
|
+
/**
|
|
221
|
+
* @param {string} name
|
|
222
|
+
* @param {object} args
|
|
223
|
+
* @param {boolean} args.autoIncrement
|
|
224
|
+
* @param {any} args.default
|
|
225
|
+
* @param {boolean} args.dropColumn
|
|
226
|
+
* @param {boolean|object} args.foreignKey
|
|
227
|
+
* @param {boolean|object} args.index
|
|
228
|
+
* @param {number} args.maxLength
|
|
229
|
+
* @param {boolean} args.null
|
|
230
|
+
* @param {boolean} args.primaryKey
|
|
231
|
+
* @returns {void}
|
|
232
|
+
*/
|
|
80
233
|
string(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "string"}, args)) }
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @param {string} name
|
|
237
|
+
* @param {object} args
|
|
238
|
+
* @param {boolean} args.autoIncrement
|
|
239
|
+
* @param {any} args.default
|
|
240
|
+
* @param {boolean} args.dropColumn
|
|
241
|
+
* @param {boolean|object} args.foreignKey
|
|
242
|
+
* @param {boolean|object} args.index
|
|
243
|
+
* @param {number} args.maxLength
|
|
244
|
+
* @param {boolean} args.null
|
|
245
|
+
* @param {boolean} args.primaryKey
|
|
246
|
+
* @returns {void}
|
|
247
|
+
*/
|
|
81
248
|
text(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "text"}, args)) }
|
|
82
249
|
|
|
250
|
+
/**
|
|
251
|
+
* @param {object} args
|
|
252
|
+
* @param {boolean} args.autoIncrement
|
|
253
|
+
* @param {any} args.default
|
|
254
|
+
* @param {boolean} args.dropColumn
|
|
255
|
+
* @param {boolean|object} args.foreignKey
|
|
256
|
+
* @param {boolean|object} args.index
|
|
257
|
+
* @param {number} args.maxLength
|
|
258
|
+
* @param {boolean} args.null
|
|
259
|
+
* @param {boolean} args.primaryKey
|
|
260
|
+
* @returns {void}
|
|
261
|
+
*/
|
|
83
262
|
timestamps(args = {}) {
|
|
84
263
|
this.datetime("created_at", args)
|
|
85
264
|
this.datetime("updated_at", args)
|
|
86
265
|
}
|
|
87
266
|
|
|
267
|
+
/**
|
|
268
|
+
* @param {string} name
|
|
269
|
+
* @param {object} args
|
|
270
|
+
* @param {boolean} args.autoIncrement
|
|
271
|
+
* @param {any} args.default
|
|
272
|
+
* @param {boolean} args.dropColumn
|
|
273
|
+
* @param {boolean|object} args.foreignKey
|
|
274
|
+
* @param {boolean|object} args.index
|
|
275
|
+
* @param {number} args.maxLength
|
|
276
|
+
* @param {boolean} args.null
|
|
277
|
+
* @param {boolean} args.primaryKey
|
|
278
|
+
* @returns {void}
|
|
279
|
+
*/
|
|
88
280
|
uuid(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "uuid"}, args)) }
|
|
89
281
|
}
|
|
@@ -1,4 +1,60 @@
|
|
|
1
1
|
export default class VelociousEnvironmentHandlerBase {
|
|
2
|
+
/**
|
|
3
|
+
* @param {import("../cli/base-command.js").default} _command
|
|
4
|
+
* @returns {Promise<void>}
|
|
5
|
+
*/
|
|
6
|
+
async cliCommandsGenerateBaseModels(_command) { // eslint-disable-line no-unused-vars
|
|
7
|
+
throw new Error("cliCommandsGenerateBaseModels not implemented")
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {import("../cli/base-command.js").default} _command
|
|
12
|
+
* @returns {Promise<void>}
|
|
13
|
+
*/
|
|
14
|
+
async cliCommandsInit(_command) { // eslint-disable-line no-unused-vars
|
|
15
|
+
throw new Error("cliCommandsInit not implemented")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {import("../cli/base-command.js").default} _command
|
|
20
|
+
* @returns {Promise<void>}
|
|
21
|
+
*/
|
|
22
|
+
async cliCommandsMigrationGenerate(_command) { // eslint-disable-line no-unused-vars
|
|
23
|
+
throw new Error("cliCommandsMigrationGenerate not implemented")
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {import("../cli/base-command.js").default} _command
|
|
28
|
+
* @returns {Promise<void>}
|
|
29
|
+
*/
|
|
30
|
+
async cliCommandsMigrationDestroy(_command) { // eslint-disable-line no-unused-vars
|
|
31
|
+
throw new Error("cliCommandsMigrationDestroy not implemented")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {import("../cli/base-command.js").default} _command
|
|
36
|
+
* @returns {Promise<void>}
|
|
37
|
+
*/
|
|
38
|
+
async cliCommandsGenerateModel(_command) { // eslint-disable-line no-unused-vars
|
|
39
|
+
throw new Error("cliCommandsGenerateModel not implemented")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {import("../cli/base-command.js").default} _command
|
|
44
|
+
* @returns {Promise<void>}
|
|
45
|
+
*/
|
|
46
|
+
async cliCommandsServer(_command) { // eslint-disable-line no-unused-vars
|
|
47
|
+
throw new Error("cliCommandsServer not implemented")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {import("../cli/base-command.js").default} _command
|
|
52
|
+
* @returns {Promise<void>}
|
|
53
|
+
*/
|
|
54
|
+
async cliCommandsTest(_command) { // eslint-disable-line no-unused-vars
|
|
55
|
+
throw new Error("cliCommandsTest not implemented")
|
|
56
|
+
}
|
|
57
|
+
|
|
2
58
|
/**
|
|
3
59
|
* @interface
|
|
4
60
|
*/
|
|
@@ -34,6 +90,8 @@ export default class VelociousEnvironmentHandlerBase {
|
|
|
34
90
|
async importApplicationRoutes() { throw new Error("importApplicationRoutes not implemented") }
|
|
35
91
|
|
|
36
92
|
/**
|
|
93
|
+
* @param {object} args
|
|
94
|
+
* @param {string[]} args.commandParts
|
|
37
95
|
* @interface
|
|
38
96
|
*/
|
|
39
97
|
async requireCommand({commandParts}) { throw new Error("requireCommand not implemented") } // eslint-disable-line no-unused-vars
|
|
@@ -15,30 +15,6 @@ export default class VelociousEnvironmentsHandlerBrowser extends Base {
|
|
|
15
15
|
this.migrationsRequireContextCallback = migrationsRequireContextCallback
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
async cliCommandsInit(_command) { // eslint-disable-line no-unused-vars
|
|
19
|
-
throw new Error("Unsupported on browser")
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async cliCommandsMigrationGenerate(_command) { // eslint-disable-line no-unused-vars
|
|
23
|
-
throw new Error("Unsupported on browser")
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async cliCommandsMigrationDestroy(_command) { // eslint-disable-line no-unused-vars
|
|
27
|
-
throw new Error("Unsupported on browser")
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async cliCommandsModelGenerate(_command) { // eslint-disable-line no-unused-vars
|
|
31
|
-
throw new Error("Unsupported on browser")
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async cliCommandsServer(_command) { // eslint-disable-line no-unused-vars
|
|
35
|
-
throw new Error("Unsupported on browser")
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async cliCommandsTest(_command) { // eslint-disable-line no-unused-vars
|
|
39
|
-
throw new Error("Unsupported on browser")
|
|
40
|
-
}
|
|
41
|
-
|
|
42
18
|
/**
|
|
43
19
|
* @returns {object}
|
|
44
20
|
*/
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import BaseCommand from "../../../../../cli/base-command.js"
|
|
2
|
+
import fileExists from "../../../../../utils/file-exists.js"
|
|
3
|
+
import fs from "fs/promises"
|
|
4
|
+
import * as inflection from "inflection"
|
|
5
|
+
|
|
6
|
+
export default class DbGenerateModel extends BaseCommand {
|
|
7
|
+
async execute() {
|
|
8
|
+
await this.getConfiguration().initializeModels()
|
|
9
|
+
|
|
10
|
+
const modelsDir = `${process.cwd()}/src/model-bases`
|
|
11
|
+
const modelClasses = this.getConfiguration().getModelClasses()
|
|
12
|
+
|
|
13
|
+
if (!await fileExists(modelsDir)) {
|
|
14
|
+
await fs.mkdir(modelsDir, {recursive: true})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
for (const modelClassName in modelClasses) {
|
|
18
|
+
const modelClass = modelClasses[modelClassName]
|
|
19
|
+
const modelName = inflection.dasherize(modelClassName)
|
|
20
|
+
const modelNameCamelized = inflection.camelize(modelName.replaceAll("-", "_"))
|
|
21
|
+
const modelBaseFileName = `${inflection.dasherize(inflection.underscore(modelName))}.js`
|
|
22
|
+
const modelPath = `${modelsDir}/${modelBaseFileName}`
|
|
23
|
+
|
|
24
|
+
console.log(`create src/model-bases/${modelBaseFileName}`)
|
|
25
|
+
|
|
26
|
+
let fileContent = `import Record from "velocious/src/database/record/index.js"\n\n`
|
|
27
|
+
|
|
28
|
+
fileContent += `export default class ${modelNameCamelized}Base extends Record {\n`
|
|
29
|
+
|
|
30
|
+
const columns = await modelClass._getTable().getColumns()
|
|
31
|
+
let methodsCount = 0
|
|
32
|
+
|
|
33
|
+
for (const column of columns) {
|
|
34
|
+
const camelizedColumnName = inflection.camelize(column.getName(), true)
|
|
35
|
+
const camelizedColumnNameBigFirst = inflection.camelize(column.getName())
|
|
36
|
+
let jsdocType
|
|
37
|
+
|
|
38
|
+
if (column.getType() == "varchar") {
|
|
39
|
+
jsdocType = "string"
|
|
40
|
+
} else if (["bigint", "int", "integer", "smallint"].includes(column.getType())) {
|
|
41
|
+
jsdocType = "number"
|
|
42
|
+
} else if (["date", "datetime"].includes(column.getType())) {
|
|
43
|
+
jsdocType = "Date"
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (methodsCount > 0) {
|
|
47
|
+
fileContent += "\n"
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (jsdocType) {
|
|
51
|
+
fileContent += " /**\n"
|
|
52
|
+
fileContent += ` * @returns {${jsdocType}}\n`
|
|
53
|
+
fileContent += " */\n"
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fileContent += ` ${camelizedColumnName}() { return this.readAttribute("${camelizedColumnName}") }\n\n`
|
|
57
|
+
|
|
58
|
+
if (jsdocType) {
|
|
59
|
+
fileContent += " /**\n"
|
|
60
|
+
fileContent += ` * @param {${jsdocType}} newValue\n`
|
|
61
|
+
fileContent += " * @returns {void}\n"
|
|
62
|
+
fileContent += " */\n"
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fileContent += ` set${camelizedColumnNameBigFirst}(newValue) { return this._setColumnAttribute("${camelizedColumnName}", newValue) }\n\n`
|
|
66
|
+
|
|
67
|
+
fileContent += " /**\n"
|
|
68
|
+
fileContent += " * @returns {boolean}\n"
|
|
69
|
+
fileContent += " */\n"
|
|
70
|
+
fileContent += ` has${camelizedColumnNameBigFirst}() { return this._hasAttribute(this.${camelizedColumnName}()) }\n`
|
|
71
|
+
|
|
72
|
+
methodsCount++
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (const relationship of modelClass.getRelationships()) {
|
|
76
|
+
let fileName, fullFilePath
|
|
77
|
+
|
|
78
|
+
if (relationship.getPolymorphic()) {
|
|
79
|
+
fileName = "velocious/src/database/record/index.js"
|
|
80
|
+
} else {
|
|
81
|
+
fileName = inflection.dasherize(inflection.underscore(relationship.getTargetModelClass().name))
|
|
82
|
+
fullFilePath = `src/models/${fileName}.js`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (methodsCount > 0) {
|
|
86
|
+
fileContent += "\n"
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (relationship.getType() == "belongsTo" || relationship.getType() == "hasOne") {
|
|
90
|
+
fileContent += " /**\n"
|
|
91
|
+
|
|
92
|
+
if (fullFilePath && await fileExists(fullFilePath)) {
|
|
93
|
+
fileContent += ` * @returns {import("../models/${fileName}.js").default}\n`
|
|
94
|
+
} else {
|
|
95
|
+
fileContent += ` * @returns {import("velocious/src/database/record/index.js").default}\n`
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
fileContent += " */\n"
|
|
99
|
+
fileContent += ` ${relationship.getRelationshipName()}() { return this.getRelationshipByName("${relationship.getRelationshipName()}").loaded() }\n`
|
|
100
|
+
} else if (relationship.getType() == "hasMany") {
|
|
101
|
+
fileContent += " /**\n"
|
|
102
|
+
|
|
103
|
+
if (fullFilePath && await fileExists(fullFilePath)) {
|
|
104
|
+
fileContent += ` * @returns {Array<import("../models/${fileName}.js").default>}\n`
|
|
105
|
+
} else {
|
|
106
|
+
fileContent += ` * @returns {Array<import("velocious/src/database/record/index.js").default>}\n`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
fileContent += " */\n"
|
|
110
|
+
fileContent += ` ${relationship.getRelationshipName()}() { return this.getRelationshipByName("${relationship.getRelationshipName()}").loaded() }\n`
|
|
111
|
+
} else {
|
|
112
|
+
throw new Error(`Unknown relationship type: ${relationship.getType()}`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
methodsCount++
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
fileContent += "}\n"
|
|
119
|
+
|
|
120
|
+
await fs.writeFile(modelPath, fileContent)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Base from "./base.js"
|
|
2
2
|
import CliCommandsDestroyMigration from "./node/cli/commands/destroy/migration.js"
|
|
3
3
|
import CliCommandsInit from "./node/cli/commands/init.js"
|
|
4
|
+
import CliCommandsGenerateBaseModels from "./node/cli/commands/generate/base-models.js"
|
|
4
5
|
import CliCommandsGenerateMigration from "./node/cli/commands/generate/migration.js"
|
|
5
6
|
import CliCommandsGenerateModel from "./node/cli/commands/generate/model.js"
|
|
6
7
|
import CliCommandsServer from "./node/cli/commands/server.js"
|
|
@@ -60,7 +61,11 @@ export default class VelociousEnvironmentHandlerNode extends Base{
|
|
|
60
61
|
return await this.forwardCommand(command, CliCommandsDestroyMigration)
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
async
|
|
64
|
+
async cliCommandsGenerateBaseModels(command) {
|
|
65
|
+
return await this.forwardCommand(command, CliCommandsGenerateBaseModels)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async cliCommandsGenerateModel(command) {
|
|
64
69
|
return await this.forwardCommand(command, CliCommandsGenerateModel)
|
|
65
70
|
}
|
|
66
71
|
|
package/src/initializer.js
CHANGED
package/src/logger.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import Configuration from "./configuration.js"
|
|
2
|
+
import restArgsError from "./utils/rest-args-error.js"
|
|
2
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} message
|
|
6
|
+
* @returns {Promise<void>}
|
|
7
|
+
*/
|
|
3
8
|
function consoleLog(message) {
|
|
4
9
|
return new Promise((resolve) => {
|
|
5
10
|
if (process.stdout) {
|
|
@@ -10,6 +15,10 @@ function consoleLog(message) {
|
|
|
10
15
|
})
|
|
11
16
|
}
|
|
12
17
|
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} message
|
|
20
|
+
* @returns {Promise<void>}
|
|
21
|
+
*/
|
|
13
22
|
function consoleError(message) {
|
|
14
23
|
return new Promise((resolve) => {
|
|
15
24
|
if (process.stderr) {
|
|
@@ -20,6 +29,10 @@ function consoleError(message) {
|
|
|
20
29
|
})
|
|
21
30
|
}
|
|
22
31
|
|
|
32
|
+
/**
|
|
33
|
+
* @param {string} message
|
|
34
|
+
* @returns {Promise<void>}
|
|
35
|
+
*/
|
|
23
36
|
function consoleWarn(message) {
|
|
24
37
|
return new Promise((resolve) => {
|
|
25
38
|
if (process.stderr) {
|
|
@@ -30,6 +43,9 @@ function consoleWarn(message) {
|
|
|
30
43
|
})
|
|
31
44
|
}
|
|
32
45
|
|
|
46
|
+
/**
|
|
47
|
+
* @param {Array} messages
|
|
48
|
+
*/
|
|
33
49
|
function functionOrMessages(messages) {
|
|
34
50
|
if (messages.length === 1 && typeof messages[0] == "function") {
|
|
35
51
|
messages = messages[0]()
|
|
@@ -38,6 +54,11 @@ function functionOrMessages(messages) {
|
|
|
38
54
|
return messages
|
|
39
55
|
}
|
|
40
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Converts multiple message parts into a single string.
|
|
59
|
+
* @param {...any} messages - Parts to combine into a message
|
|
60
|
+
* @returns {string}
|
|
61
|
+
*/
|
|
41
62
|
function messagesToMessage(...messages) {
|
|
42
63
|
let message = ""
|
|
43
64
|
|
|
@@ -59,8 +80,15 @@ function messagesToMessage(...messages) {
|
|
|
59
80
|
}
|
|
60
81
|
|
|
61
82
|
class Logger {
|
|
62
|
-
|
|
63
|
-
|
|
83
|
+
/**
|
|
84
|
+
* @param {any} object
|
|
85
|
+
* @param {object} args
|
|
86
|
+
* @param {boolean} args.debug
|
|
87
|
+
*/
|
|
88
|
+
constructor(object, {debug, ...restArgs} = {}) {
|
|
89
|
+
restArgsError(restArgs)
|
|
90
|
+
|
|
91
|
+
this._debug = debug
|
|
64
92
|
|
|
65
93
|
if (typeof object == "string") {
|
|
66
94
|
this._subject = object
|
|
@@ -85,20 +113,40 @@ class Logger {
|
|
|
85
113
|
return this._configuration
|
|
86
114
|
}
|
|
87
115
|
|
|
116
|
+
/**
|
|
117
|
+
* @param {...Parameters<typeof consoleLog>} messages - forwarded args
|
|
118
|
+
*/
|
|
88
119
|
async debug(...messages) {
|
|
89
120
|
if (this._debug || this.getConfiguration()?.debug) {
|
|
90
121
|
await this.log(...messages)
|
|
91
122
|
}
|
|
92
123
|
}
|
|
93
124
|
|
|
125
|
+
/**
|
|
126
|
+
* @param {...Parameters<typeof functionOrMessages>} messages - forwarded args
|
|
127
|
+
*/
|
|
94
128
|
async log(...messages) {
|
|
95
129
|
await consoleLog(messagesToMessage(this._subject, ...functionOrMessages(messages)))
|
|
96
130
|
}
|
|
97
131
|
|
|
132
|
+
/**
|
|
133
|
+
* @param {...Parameters<typeof functionOrMessages>} messages - forwarded args
|
|
134
|
+
*/
|
|
98
135
|
async error(...messages) {
|
|
99
136
|
await consoleError(messagesToMessage(this._subject, ...functionOrMessages(messages)))
|
|
100
137
|
}
|
|
101
138
|
|
|
139
|
+
/**
|
|
140
|
+
* @param {boolean} newValue
|
|
141
|
+
* @returns {void}
|
|
142
|
+
*/
|
|
143
|
+
setDebug(newValue) {
|
|
144
|
+
this._debug = newValue
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {...Parameters<typeof functionOrMessages>} messages - forwarded args
|
|
149
|
+
*/
|
|
102
150
|
async warn(...messages) {
|
|
103
151
|
await consoleWarn(messagesToMessage(this._subject, ...functionOrMessages(messages)))
|
|
104
152
|
}
|
|
@@ -106,6 +154,10 @@ class Logger {
|
|
|
106
154
|
|
|
107
155
|
export {Logger}
|
|
108
156
|
|
|
157
|
+
/**
|
|
158
|
+
* @param {any} object
|
|
159
|
+
* @param {...Parameters<typeof functionOrMessages>} messages - forwarded args
|
|
160
|
+
*/
|
|
109
161
|
export default async function logger(object, ...messages) {
|
|
110
162
|
const className = object.constructor.name
|
|
111
163
|
const configuration = object.configuration || Configuration.current()
|
package/src/routes/base-route.js
CHANGED
|
@@ -11,16 +11,28 @@ export function initBaseRoute() {
|
|
|
11
11
|
VelociousBaseRoute = class VelociousBaseRoute {
|
|
12
12
|
routes = []
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} name
|
|
16
|
+
*/
|
|
17
|
+
get(name) {
|
|
18
|
+
const route = new GetRoute({name})
|
|
16
19
|
|
|
17
20
|
this.routes.push(route)
|
|
18
21
|
}
|
|
19
22
|
|
|
23
|
+
/**
|
|
24
|
+
* @interface
|
|
25
|
+
* @param {string} _path
|
|
26
|
+
*/
|
|
20
27
|
matchWithPath(_path) { // eslint-disable-line no-unused-vars
|
|
21
28
|
throw new Error(`No 'matchWithPath' implemented on ${this.constructor.name}`)
|
|
22
29
|
}
|
|
23
30
|
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} name
|
|
33
|
+
* @param {function(NamespaceRoute) : void} callback
|
|
34
|
+
* @returns {void}
|
|
35
|
+
*/
|
|
24
36
|
namespace(name, callback) {
|
|
25
37
|
const route = new NamespaceRoute({name})
|
|
26
38
|
|
|
@@ -31,12 +43,21 @@ export function initBaseRoute() {
|
|
|
31
43
|
}
|
|
32
44
|
}
|
|
33
45
|
|
|
34
|
-
|
|
35
|
-
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} name
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*/
|
|
50
|
+
post(name) {
|
|
51
|
+
const route = new PostRoute({name})
|
|
36
52
|
|
|
37
53
|
this.routes.push(route)
|
|
38
54
|
}
|
|
39
55
|
|
|
56
|
+
/**
|
|
57
|
+
* @param {string} name
|
|
58
|
+
* @param {function(ResourceRoute) : void} callback
|
|
59
|
+
* @returns {void}
|
|
60
|
+
*/
|
|
40
61
|
resources(name, callback) {
|
|
41
62
|
const route = new ResourceRoute({name})
|
|
42
63
|
|