mythix-orm-sql-base 1.10.1 → 1.11.0
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/lib/sql-query-generator-base.js +194 -1
- package/package.json +2 -2
|
@@ -15,6 +15,12 @@ const DefaultHelpers = Types.DefaultHelpers;
|
|
|
15
15
|
const LiteralBase = Literals.LiteralBase;
|
|
16
16
|
|
|
17
17
|
/// The "base" SQL generator for all SQL-type databases.
|
|
18
|
+
///
|
|
19
|
+
/// This class is used to generate SQL statements for the
|
|
20
|
+
/// underlying SQL database. Database drivers can and often
|
|
21
|
+
/// will create their own class that extends from this class.
|
|
22
|
+
///
|
|
23
|
+
/// Extends: [QueryGeneratorBase](https://github.com/th317erd/mythix-orm/wiki/QueryGeneratorBase)
|
|
18
24
|
class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
19
25
|
/// Escape a field name, usually for a projection alias.
|
|
20
26
|
/// This method is primarily used for generating aliases
|
|
@@ -3045,6 +3051,20 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3045
3051
|
return `TRUNCATE TABLE ${escapedTableName}`;
|
|
3046
3052
|
}
|
|
3047
3053
|
|
|
3054
|
+
/// Generate an `ALTER TABLE` statement
|
|
3055
|
+
/// to rename the table.
|
|
3056
|
+
///
|
|
3057
|
+
/// Arguments:
|
|
3058
|
+
/// Model: class [Model](https://github.com/th317erd/mythix-orm/wiki/Model)
|
|
3059
|
+
/// The model that defines the table being altered.
|
|
3060
|
+
/// newModelAttributes: object
|
|
3061
|
+
/// An object that contains a `tableName` key for the new table name
|
|
3062
|
+
/// options?: object
|
|
3063
|
+
/// Options for the operation.
|
|
3064
|
+
///
|
|
3065
|
+
/// Return: Array<string>
|
|
3066
|
+
/// An array of `ALTER TABLE` statements to change the table's name. For
|
|
3067
|
+
/// most databases this will probably only be a single statement.
|
|
3048
3068
|
generateAlterTableStatement(Model, newModelAttributes, options) {
|
|
3049
3069
|
if (Nife.isEmpty(newModelAttributes))
|
|
3050
3070
|
return [];
|
|
@@ -3060,6 +3080,23 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3060
3080
|
return statements;
|
|
3061
3081
|
}
|
|
3062
3082
|
|
|
3083
|
+
/// Generate an `ALTER TABLE ... DROP COLUMN` statement.
|
|
3084
|
+
///
|
|
3085
|
+
/// Arguments:
|
|
3086
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3087
|
+
/// The field to drop from the database.
|
|
3088
|
+
/// options?:
|
|
3089
|
+
/// Options for the operation. Though these might contain
|
|
3090
|
+
/// database specific options, generic options that should
|
|
3091
|
+
/// work for most databases are:
|
|
3092
|
+
/// | Option | Type | Default Value | Description |
|
|
3093
|
+
/// | ------ | ---- | ------------- | ----------- |
|
|
3094
|
+
/// | `ifExists` | `boolean` | `false` | If `true`, then add an `IF EXISTS` clause to the `ALTER TABLE` statement. |
|
|
3095
|
+
/// | `cascade` | `boolean` | `true` | If `true`, then add a `CASCADE` clause to the `ALTER TABLE` statement. |
|
|
3096
|
+
///
|
|
3097
|
+
/// Return: string
|
|
3098
|
+
/// An `ALTER TABLE` statement to drop the column specified
|
|
3099
|
+
/// by the provided `field`.
|
|
3063
3100
|
generateDropColumnStatement(field, _options) {
|
|
3064
3101
|
let Model = field.Model;
|
|
3065
3102
|
let options = _options || {};
|
|
@@ -3067,15 +3104,46 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3067
3104
|
return `ALTER TABLE ${this.getEscapedTableName(Model, options)} DROP COLUMN${(options.ifExists) ? ' IF EXISTS' : ''} ${this.getEscapedColumnName(Model, field, { ...options, columnNameOnly: true })} ${(options.cascade !== false) ? 'CASCADE' : 'RESTRICT'}`;
|
|
3068
3105
|
}
|
|
3069
3106
|
|
|
3107
|
+
/// Generate an `ALTER TABLE ... RENAME COLUMN` statement.
|
|
3108
|
+
///
|
|
3109
|
+
/// Arguments:
|
|
3110
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3111
|
+
/// The field to rename in the database.
|
|
3112
|
+
/// newField: object | [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3113
|
+
/// A raw object containing a `columnName` or `fieldName` properties, or a `Field` instance
|
|
3114
|
+
/// containing a `columnName` or `fieldName`. This will be used as the new name
|
|
3115
|
+
/// of the column.
|
|
3116
|
+
/// options?:
|
|
3117
|
+
/// Options for the operation.
|
|
3118
|
+
///
|
|
3119
|
+
/// Return: string
|
|
3120
|
+
/// An `ALTER TABLE` statement to rename the column specified
|
|
3121
|
+
/// by the provided `field`.
|
|
3070
3122
|
generateAlterColumnRenameStatement(field, newField, _options) {
|
|
3071
3123
|
let Model = field.Model;
|
|
3072
3124
|
let options = _options || {};
|
|
3073
3125
|
let prefix = `ALTER TABLE ${this.getEscapedTableName(Model, options)}`;
|
|
3074
3126
|
let escapedColumnName = this.getEscapedColumnName(Model, field, { ...options, columnNameOnly: true });
|
|
3075
3127
|
|
|
3076
|
-
return `${prefix} RENAME COLUMN ${escapedColumnName} TO ${this.escapeID(newField.columnName)}`;
|
|
3128
|
+
return `${prefix} RENAME COLUMN ${escapedColumnName} TO ${this.escapeID(newField.columnName || newField.fieldName)}`;
|
|
3077
3129
|
}
|
|
3078
3130
|
|
|
3131
|
+
/// Generate an `ALTER TABLE` statement to add or remove
|
|
3132
|
+
/// a `NOT NULL` constraint on the specified `field`.
|
|
3133
|
+
///
|
|
3134
|
+
/// Arguments:
|
|
3135
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3136
|
+
/// The field to add or remove the constraint from in the database.
|
|
3137
|
+
/// newField: object | [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3138
|
+
/// A raw object containing an `allowNull` property, or a `Field` instance
|
|
3139
|
+
/// containing a `allowNull` property. If `true`, then a `NOT NULL` constraint
|
|
3140
|
+
/// will be added. If `false`, then any `NOT NULL` constraint will be dropped.
|
|
3141
|
+
/// options?:
|
|
3142
|
+
/// Options for the operation.
|
|
3143
|
+
///
|
|
3144
|
+
/// Return: string
|
|
3145
|
+
/// An `ALTER TABLE` statement to add or remove the `NOT NULL` constraint
|
|
3146
|
+
/// of the specified `field`.
|
|
3079
3147
|
generateAlterColumnSetOrDropNullConstraintStatement(field, newField, _options) {
|
|
3080
3148
|
let Model = field.Model;
|
|
3081
3149
|
let options = _options || {};
|
|
@@ -3085,6 +3153,26 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3085
3153
|
return `${prefix} ALTER COLUMN ${escapedColumnName} ${(newField.allowNull) ? 'DROP' : 'SET'} NOT NULL`;
|
|
3086
3154
|
}
|
|
3087
3155
|
|
|
3156
|
+
/// Generate an `ALTER TABLE` statement to add or remove
|
|
3157
|
+
/// a `DEFAULT` value for the specified `field`.
|
|
3158
|
+
///
|
|
3159
|
+
/// Arguments:
|
|
3160
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3161
|
+
/// The field to add or remove the `DEFAULT` value from in the database.
|
|
3162
|
+
/// newField: object | [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3163
|
+
/// A raw object or a `Field` instance defining how the field is being altered.
|
|
3164
|
+
/// This argument is not used by this method, but is passed through when calling
|
|
3165
|
+
/// other alter table generators.
|
|
3166
|
+
/// newDefaultValue: any
|
|
3167
|
+
/// The new `DEFAULT` value to apply to the column. If this argument is `undefined`,
|
|
3168
|
+
/// then any `DEFAULT` value applied to the column will be dropped. This value must
|
|
3169
|
+
/// already be escaped and ready for the underlying database to consume.
|
|
3170
|
+
/// options?:
|
|
3171
|
+
/// Options for the operation.
|
|
3172
|
+
///
|
|
3173
|
+
/// Return: string
|
|
3174
|
+
/// An `ALTER TABLE ... ALTER COLUMN ... DROP | SET DEFAULT` statement to add or set
|
|
3175
|
+
/// a `DEFAULT` value for this column.
|
|
3088
3176
|
generateAlterColumnSetDefaultStatement(field, newField, newDefaultValue, _options) {
|
|
3089
3177
|
let Model = field.Model;
|
|
3090
3178
|
let options = _options || {};
|
|
@@ -3097,6 +3185,25 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3097
3185
|
return `${prefix} ALTER COLUMN ${escapedColumnName} SET DEFAULT ${newDefaultValue}`;
|
|
3098
3186
|
}
|
|
3099
3187
|
|
|
3188
|
+
/// Generate an `ALTER TABLE` statement to change the
|
|
3189
|
+
/// type of the `field` specified.
|
|
3190
|
+
///
|
|
3191
|
+
/// Arguments:
|
|
3192
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3193
|
+
/// The field to change the type on in the database.
|
|
3194
|
+
/// newField: object | [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3195
|
+
/// A raw object or a `Field` instance defining how the field is being altered.
|
|
3196
|
+
/// This argument is not used by this method, but is passed through when calling
|
|
3197
|
+
/// other alter table generators.
|
|
3198
|
+
/// newFieldType: string
|
|
3199
|
+
/// The new type to change the field/column to. This must be a raw type that
|
|
3200
|
+
/// the underlying database supports, in database format.
|
|
3201
|
+
/// options?:
|
|
3202
|
+
/// Options for the operation.
|
|
3203
|
+
///
|
|
3204
|
+
/// Return: string
|
|
3205
|
+
/// An `ALTER TABLE ... ALTER COLUMN ... SET DATA TYPE` statement to alter
|
|
3206
|
+
/// the column's data type.
|
|
3100
3207
|
generateAlterColumnChangeTypeStatement(field, newField, newFieldType, _options) {
|
|
3101
3208
|
let Model = field.Model;
|
|
3102
3209
|
let options = _options || {};
|
|
@@ -3106,6 +3213,22 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3106
3213
|
return `${prefix} ALTER COLUMN ${escapedColumnName} SET DATA TYPE ${newFieldType}`;
|
|
3107
3214
|
}
|
|
3108
3215
|
|
|
3216
|
+
/// Generate an `ALTER TABLE` statement to add or remove
|
|
3217
|
+
/// a `PRIMARY KEY` constraint on the specified `field`.
|
|
3218
|
+
///
|
|
3219
|
+
/// Arguments:
|
|
3220
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3221
|
+
/// The field to add or remove the constraint from in the database.
|
|
3222
|
+
/// newField: object | [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3223
|
+
/// A raw object containing an `primaryKey` property, or a `Field` instance
|
|
3224
|
+
/// containing a `primaryKey` property. If `true`, then a `PRIMARY KEY` constraint
|
|
3225
|
+
/// will be added. If `false`, then any `PRIMARY KEY` constraint will be dropped.
|
|
3226
|
+
/// options?:
|
|
3227
|
+
/// Options for the operation.
|
|
3228
|
+
///
|
|
3229
|
+
/// Return: string
|
|
3230
|
+
/// An `ALTER TABLE` statement to add or remove the `PRIMARY KEY` constraint
|
|
3231
|
+
/// of the specified `field`.
|
|
3109
3232
|
generateAlterColumnChangePrimaryKeyConstraintStatement(field, newField, _options) {
|
|
3110
3233
|
let Model = field.Model;
|
|
3111
3234
|
let options = _options || {};
|
|
@@ -3118,6 +3241,22 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3118
3241
|
return `${prefix} ALTER COLUMN ${escapedColumnName} DROP CONSTRAINT PRIMARY KEY`;
|
|
3119
3242
|
}
|
|
3120
3243
|
|
|
3244
|
+
/// Generate an `ALTER TABLE` statement to add or remove
|
|
3245
|
+
/// a `UNIQUE` constraint on the specified `field`.
|
|
3246
|
+
///
|
|
3247
|
+
/// Arguments:
|
|
3248
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3249
|
+
/// The field to add or remove the constraint from in the database.
|
|
3250
|
+
/// newField: object | [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3251
|
+
/// A raw object containing an `unique` property, or a `Field` instance
|
|
3252
|
+
/// containing a `unique` property. If `true`, then a `UNIQUE` constraint
|
|
3253
|
+
/// will be added. If `false`, then any `UNIQUE` constraint will be dropped.
|
|
3254
|
+
/// options?:
|
|
3255
|
+
/// Options for the operation.
|
|
3256
|
+
///
|
|
3257
|
+
/// Return: string
|
|
3258
|
+
/// An `ALTER TABLE` statement to add or remove a `UNIQUE` constraint
|
|
3259
|
+
/// of the specified `field`.
|
|
3121
3260
|
generateAlterColumnChangeUniqueConstraintStatement(field, newField, _options) {
|
|
3122
3261
|
let Model = field.Model;
|
|
3123
3262
|
let options = _options || {};
|
|
@@ -3130,6 +3269,28 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3130
3269
|
return `${prefix} ALTER COLUMN ${escapedColumnName} DROP CONSTRAINT UNIQUE`;
|
|
3131
3270
|
}
|
|
3132
3271
|
|
|
3272
|
+
/// Generate multiple `ALTER TABLE` statements to change the
|
|
3273
|
+
/// `field` provided to match the `newFieldAttributes` provided.
|
|
3274
|
+
///
|
|
3275
|
+
/// Arguments:
|
|
3276
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3277
|
+
/// The field to alter in the database.
|
|
3278
|
+
/// newFieldAttributes: object | [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3279
|
+
/// A raw object containing field properties, or a `Field` instance. Any differing
|
|
3280
|
+
/// properties between these two "fields" will generate an `ALTER TABLE` statement
|
|
3281
|
+
/// for that difference. For example, if the `columnName` between both "fields" is
|
|
3282
|
+
/// different, then an `ALTER TABLE` statement will be generated to change the column's
|
|
3283
|
+
/// name. Properties that are checked for differences are: `primaryKey`, `unique`, `index`,
|
|
3284
|
+
/// `columnName` & `fieldName`, `allowNull`, `type`, and `defaultValue`. If any of these
|
|
3285
|
+
/// differ between the two provided "fields", then an `ALTER TABLE` statement will be generated
|
|
3286
|
+
/// to update the column to match the new properties of `newFieldAttributes`.
|
|
3287
|
+
/// options?:
|
|
3288
|
+
/// Options for the operation.
|
|
3289
|
+
///
|
|
3290
|
+
/// Return: Array<string>
|
|
3291
|
+
/// Multiple `ALTER TABLE` statements to alter the specified `field`
|
|
3292
|
+
/// to match the new properties defined by `newFieldAttributes`. If
|
|
3293
|
+
/// no changes are detected, then an empty array will be returned instead.
|
|
3133
3294
|
generateAlterColumnStatements(field, _newFieldAttributes, _options) {
|
|
3134
3295
|
if (Nife.isEmpty(_newFieldAttributes))
|
|
3135
3296
|
return [];
|
|
@@ -3225,6 +3386,22 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3225
3386
|
return statements.filter(Boolean);
|
|
3226
3387
|
}
|
|
3227
3388
|
|
|
3389
|
+
/// Generate an `ALTER TABLE ... ADD COLUMN` statement.
|
|
3390
|
+
///
|
|
3391
|
+
/// Arguments:
|
|
3392
|
+
/// field: [Field](https://github.com/th317erd/mythix-orm/wiki/Field)
|
|
3393
|
+
/// The field to add to the database.
|
|
3394
|
+
/// options?:
|
|
3395
|
+
/// Options for the operation. Though these might contain
|
|
3396
|
+
/// database specific options, generic options that should
|
|
3397
|
+
/// work for most databases are:
|
|
3398
|
+
/// | Option | Type | Default Value | Description |
|
|
3399
|
+
/// | ------ | ---- | ------------- | ----------- |
|
|
3400
|
+
/// | `ifNotExists` | `boolean` | `false` | If `true`, then add an `IF NOT EXISTS` clause to the `ALTER TABLE` statement. |
|
|
3401
|
+
///
|
|
3402
|
+
/// Return: string
|
|
3403
|
+
/// An `ALTER TABLE ... ADD COLUMN` statement to add the column specified
|
|
3404
|
+
/// by the provided `field`.
|
|
3228
3405
|
generateAddColumnStatement(field, _options) {
|
|
3229
3406
|
let Model = field.Model;
|
|
3230
3407
|
let options = _options || {};
|
|
@@ -3233,6 +3410,22 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
3233
3410
|
return `${prefix} ADD COLUMN${(options.ifNotExists) ? ' IF NOT EXISTS' : ''} ${this.generateColumnDeclarationStatement(Model, field, options)}`;
|
|
3234
3411
|
}
|
|
3235
3412
|
|
|
3413
|
+
/// Convert the provided `queryEngine` into
|
|
3414
|
+
/// a `SELECT` statement.
|
|
3415
|
+
///
|
|
3416
|
+
/// This is similar to a `toSQL` method in other ORMs.
|
|
3417
|
+
/// It is usually called directly from the `queryEngine` itself,
|
|
3418
|
+
/// i.e. `queryEngine.toString()` will call this method.
|
|
3419
|
+
///
|
|
3420
|
+
/// Arguments:
|
|
3421
|
+
/// queryEngine: [QueryEngine](https://github.com/th317erd/mythix-orm/wiki/QueryEngine)
|
|
3422
|
+
/// The query engine to use to generate a `SELECT` statement.
|
|
3423
|
+
/// options?: object
|
|
3424
|
+
/// Options for the operation. These are simply passed off to <see>SQLQueryGeneratorBase.generateSelectStatement</see>.
|
|
3425
|
+
///
|
|
3426
|
+
/// Return: string
|
|
3427
|
+
/// A fully formatted `SELECT` statement, that was generated from
|
|
3428
|
+
/// the provided `queryEngine`.
|
|
3236
3429
|
toConnectionString(queryEngine, options) {
|
|
3237
3430
|
return this.generateSelectStatement(queryEngine, options);
|
|
3238
3431
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mythix-orm-sql-base",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "SQL base support for Mythix ORM",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"homepage": "https://github.com/th317erd/mythix-orm-sql-base#readme",
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"mythix-orm": "^1.
|
|
37
|
+
"mythix-orm": "^1.13.1"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"luxon": "^3.1.0",
|