velocious 1.0.94 → 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
CHANGED
|
@@ -44,8 +44,7 @@ class ValidationError extends Error {
|
|
|
44
44
|
|
|
45
45
|
class VelociousDatabaseRecord {
|
|
46
46
|
/**
|
|
47
|
-
* @
|
|
48
|
-
* @returns {Record<string, T>}
|
|
47
|
+
* @returns {Record<string, import("./validators/base.js").default>}
|
|
49
48
|
*/
|
|
50
49
|
static validatorTypes() {
|
|
51
50
|
if (!this._validatorTypes) this._validatorTypes = {}
|
|
@@ -55,16 +54,14 @@ class VelociousDatabaseRecord {
|
|
|
55
54
|
|
|
56
55
|
/**
|
|
57
56
|
* @param {string} name
|
|
58
|
-
* @
|
|
59
|
-
* @param {T} validatorClass
|
|
57
|
+
* @param {import("./validators/base.js").default} validatorClass
|
|
60
58
|
*/
|
|
61
59
|
static registerValidatorType(name, validatorClass) {
|
|
62
60
|
this.validatorTypes()[name] = validatorClass
|
|
63
61
|
}
|
|
64
62
|
|
|
65
63
|
/**
|
|
66
|
-
* @
|
|
67
|
-
* @returns {T}
|
|
64
|
+
* @returns {import("./validators/base.js").default}
|
|
68
65
|
*/
|
|
69
66
|
static getValidatorType(validatorName) {
|
|
70
67
|
if (!(validatorName in this.validatorTypes())) throw new Error(`Validator type ${validatorName} not found`)
|
|
@@ -189,8 +186,7 @@ class VelociousDatabaseRecord {
|
|
|
189
186
|
}
|
|
190
187
|
|
|
191
188
|
/**
|
|
192
|
-
* @
|
|
193
|
-
* @returns {T}
|
|
189
|
+
* @returns {import("./relationships/base.js").default}
|
|
194
190
|
*/
|
|
195
191
|
static getRelationshipByName(relationshipName) {
|
|
196
192
|
if (!this._relationships) this._relationships = {}
|
|
@@ -203,7 +199,7 @@ class VelociousDatabaseRecord {
|
|
|
203
199
|
}
|
|
204
200
|
|
|
205
201
|
/**
|
|
206
|
-
* @returns {Array}
|
|
202
|
+
* @returns {Array<import("./relationships/base.js").default>}
|
|
207
203
|
*/
|
|
208
204
|
static getRelationships() {
|
|
209
205
|
if (this._relationships) return Object.values(this._relationships)
|
|
@@ -269,7 +265,7 @@ class VelociousDatabaseRecord {
|
|
|
269
265
|
}
|
|
270
266
|
|
|
271
267
|
/**
|
|
272
|
-
* @param {
|
|
268
|
+
* @param {Record<string, any>} attributes
|
|
273
269
|
* @returns {Promise<InstanceType<typeof this>>}
|
|
274
270
|
*/
|
|
275
271
|
static async create(attributes) {
|
|
@@ -1050,7 +1046,7 @@ class VelociousDatabaseRecord {
|
|
|
1050
1046
|
|
|
1051
1047
|
/**
|
|
1052
1048
|
* Assigns the given attributes to the record.
|
|
1053
|
-
* @param {
|
|
1049
|
+
* @param {Record<string, any>} attributesToAssign
|
|
1054
1050
|
* @returns {void}
|
|
1055
1051
|
*/
|
|
1056
1052
|
assign(attributesToAssign) {
|
|
@@ -67,6 +67,13 @@ export default class VelociousDatabaseRecordBaseRelationship {
|
|
|
67
67
|
*/
|
|
68
68
|
getRelationshipName() { return this.relationshipName }
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* @returns {boolean}
|
|
72
|
+
*/
|
|
73
|
+
getPolymorphic() {
|
|
74
|
+
return this._polymorphic
|
|
75
|
+
}
|
|
76
|
+
|
|
70
77
|
/**
|
|
71
78
|
* @returns {string} The name of the foreign key, e.g. "id" etc.
|
|
72
79
|
*/
|
|
@@ -78,11 +85,12 @@ export default class VelociousDatabaseRecordBaseRelationship {
|
|
|
78
85
|
getType() { return this.type }
|
|
79
86
|
|
|
80
87
|
/**
|
|
81
|
-
* @
|
|
82
|
-
* @returns {typeof T} The target model class for this relationship, e.g. if the relationship is "posts" then the target model class is the Post class.
|
|
88
|
+
* @returns {typeof import("../index.js").default} The target model class for this relationship, e.g. if the relationship is "posts" then the target model class is the Post class.
|
|
83
89
|
*/
|
|
84
90
|
getTargetModelClass() {
|
|
85
|
-
if (this.
|
|
91
|
+
if (this.getPolymorphic()) {
|
|
92
|
+
return null
|
|
93
|
+
} else if (this.className) {
|
|
86
94
|
return this.modelClass._getConfiguration().getModelClass(this.className)
|
|
87
95
|
} else if (this.klass) {
|
|
88
96
|
return this.klass
|
|
@@ -25,7 +25,7 @@ export default class DbGenerateModel extends BaseCommand {
|
|
|
25
25
|
|
|
26
26
|
let fileContent = `import Record from "velocious/src/database/record/index.js"\n\n`
|
|
27
27
|
|
|
28
|
-
fileContent += `export default class ${modelNameCamelized} extends Record {\n`
|
|
28
|
+
fileContent += `export default class ${modelNameCamelized}Base extends Record {\n`
|
|
29
29
|
|
|
30
30
|
const columns = await modelClass._getTable().getColumns()
|
|
31
31
|
let methodsCount = 0
|
|
@@ -72,6 +72,49 @@ export default class DbGenerateModel extends BaseCommand {
|
|
|
72
72
|
methodsCount++
|
|
73
73
|
}
|
|
74
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
|
+
|
|
75
118
|
fileContent += "}\n"
|
|
76
119
|
|
|
77
120
|
await fs.writeFile(modelPath, fileContent)
|