sequelize-typescript-generator 12.0.0 → 13.0.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/.github/workflows/ci.yml +175 -150
- package/.vscode/launch.json +212 -0
- package/.vscode/tasks.json +95 -0
- package/CHANGELOG.md +17 -0
- package/CLAUDE.md +13 -0
- package/CONTEXT.md +67 -0
- package/README.md +827 -199
- package/build/bin/cli.js +65 -47
- package/build/bin/utils.d.ts +5 -2
- package/build/bin/utils.js +87 -104
- package/build/builders/Builder.d.ts +2 -2
- package/build/builders/Builder.js +3 -5
- package/build/builders/ModelBuilder.d.ts +43 -8
- package/build/builders/ModelBuilder.js +120 -122
- package/build/builders/NativeModelRenderer.d.ts +98 -0
- package/build/builders/NativeModelRenderer.js +353 -0
- package/build/builders/associationNaming.d.ts +9 -0
- package/build/builders/associationNaming.js +15 -0
- package/build/builders/decoratorsDependency.d.ts +27 -0
- package/build/builders/decoratorsDependency.js +45 -0
- package/build/builders/defaultValues.d.ts +40 -0
- package/build/builders/defaultValues.js +56 -0
- package/build/builders/generatedFile.d.ts +23 -0
- package/build/builders/generatedFile.js +31 -0
- package/build/builders/index.d.ts +1 -1
- package/build/builders/index.js +1 -5
- package/build/builders/nativeAssociations.d.ts +109 -0
- package/build/builders/nativeAssociations.js +308 -0
- package/build/builders/nativeAttributes.d.ts +96 -0
- package/build/builders/nativeAttributes.js +174 -0
- package/build/builders/nativeWiring.d.ts +51 -0
- package/build/builders/nativeWiring.js +108 -0
- package/build/builders/utils.d.ts +75 -0
- package/build/builders/utils.js +142 -48
- package/build/config/IConfig.d.ts +11 -2
- package/build/config/IConfig.js +4 -6
- package/build/config/format.d.ts +27 -0
- package/build/config/format.js +26 -0
- package/build/config/index.d.ts +1 -1
- package/build/config/index.js +1 -2
- package/build/connection/createConnection.d.ts +8 -1
- package/build/connection/createConnection.js +30 -7
- package/build/connection/index.d.ts +1 -1
- package/build/connection/index.js +1 -17
- package/build/dialects/AssociationsParser.d.ts +10 -2
- package/build/dialects/AssociationsParser.js +16 -19
- package/build/dialects/Dialect.d.ts +52 -9
- package/build/dialects/Dialect.js +93 -57
- package/build/dialects/DialectMSSQL.d.ts +22 -3
- package/build/dialects/DialectMSSQL.js +169 -75
- package/build/dialects/DialectMariaDB.d.ts +13 -3
- package/build/dialects/DialectMariaDB.js +98 -79
- package/build/dialects/DialectMySQL.d.ts +13 -3
- package/build/dialects/DialectMySQL.js +101 -83
- package/build/dialects/DialectPostgres.d.ts +14 -3
- package/build/dialects/DialectPostgres.js +129 -75
- package/build/dialects/DialectSQLite.d.ts +41 -3
- package/build/dialects/DialectSQLite.js +166 -24
- package/build/dialects/associationDiscovery.d.ts +64 -0
- package/build/dialects/associationDiscovery.js +275 -0
- package/build/dialects/associationsFileMerge.d.ts +21 -0
- package/build/dialects/associationsFileMerge.js +107 -0
- package/build/dialects/createDialect.d.ts +8 -0
- package/build/dialects/createDialect.js +24 -0
- package/build/dialects/dataTypes.d.ts +58 -0
- package/build/dialects/dataTypes.js +108 -0
- package/build/dialects/foreignKeys.d.ts +115 -0
- package/build/dialects/foreignKeys.js +198 -0
- package/build/dialects/index.d.ts +5 -5
- package/build/dialects/index.js +5 -13
- package/build/dialects/paranoid.d.ts +24 -0
- package/build/dialects/paranoid.js +29 -0
- package/build/dialects/utils.d.ts +2 -8
- package/build/dialects/utils.js +61 -58
- package/build/index.d.ts +11 -3
- package/build/index.js +5 -19
- package/build/lint/Linter.d.ts +14 -3
- package/build/lint/Linter.js +114 -14
- package/build/lint/eslintDefaultConfig.d.ts +2 -2
- package/build/lint/eslintDefaultConfig.js +28 -23
- package/build/lint/index.d.ts +1 -1
- package/build/lint/index.js +1 -5
- package/build/utils/errors.d.ts +6 -0
- package/build/utils/errors.js +6 -0
- package/docker-compose.yml +65 -0
- package/docs/adr/0001-esm-only-package.md +36 -0
- package/docs/adr/0002-native-default-output-format.md +41 -0
- package/docs/agents/domain.md +51 -0
- package/docs/agents/issue-tracker.md +45 -0
- package/docs/agents/triage-labels.md +15 -0
- package/docs/migration/12-to-13.md +248 -0
- package/docs/plans/v13-phase5-association-discovery.md +168 -0
- package/docs/plans/v13-phase6-native-emitter.md +537 -0
- package/docs/plans/v13-upgrade-plan.md +137 -0
- package/jest.config.cjs +31 -0
- package/package.json +44 -27
- package/scripts/check-no-sequelize-typescript.cjs +68 -0
- package/tsconfig.json +4 -3
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { BelongsToManyOptions, BelongsToOptions, HasManyOptions } from 'sequelize';
|
|
2
|
+
import type { ITableMetadata } from '../dialects/Dialect.js';
|
|
3
|
+
import type { IAssociationMetadata } from '../dialects/AssociationsParser.js';
|
|
4
|
+
/**
|
|
5
|
+
* Sequelize association method for each association kind.
|
|
6
|
+
*/
|
|
7
|
+
export declare const ASSOCIATION_METHODS: {
|
|
8
|
+
readonly HasOne: "hasOne";
|
|
9
|
+
readonly HasMany: "hasMany";
|
|
10
|
+
readonly BelongsTo: "belongsTo";
|
|
11
|
+
readonly BelongsToMany: "belongsToMany";
|
|
12
|
+
};
|
|
13
|
+
export type AssociationMethod = typeof ASSOCIATION_METHODS[keyof typeof ASSOCIATION_METHODS];
|
|
14
|
+
/**
|
|
15
|
+
* Options emitted on a native association call, in output order:
|
|
16
|
+
* as, through, foreignKey, otherKey, targetKey, sourceKey, onDelete, onUpdate.
|
|
17
|
+
*/
|
|
18
|
+
export type AssociationWiringOptions = Partial<Pick<BelongsToOptions, 'as' | 'foreignKey' | 'targetKey' | 'onDelete' | 'onUpdate'>> & Partial<Pick<HasManyOptions, 'sourceKey'>> & Partial<Pick<BelongsToManyOptions, 'otherKey'>> & {
|
|
19
|
+
throughModel?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* A resolved association call for the wiring file.
|
|
23
|
+
*/
|
|
24
|
+
export interface INativeAssociationWiring {
|
|
25
|
+
sourceModel: string;
|
|
26
|
+
targetModel: string;
|
|
27
|
+
method: AssociationMethod;
|
|
28
|
+
options: AssociationWiringOptions;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Report whether an association exposes a collection on the source model.
|
|
32
|
+
* @param {IAssociationMetadata} association
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
export declare const isToManyAssociation: (association: IAssociationMetadata) => boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Resolve the foreign key column of an association. A discovered association
|
|
38
|
+
* carries it directly; a file-defined one is resolved from the constraint that
|
|
39
|
+
* connects the two models, on the side that holds the foreign key.
|
|
40
|
+
* @param {ITableMetadata} source
|
|
41
|
+
* @param {IAssociationMetadata} association
|
|
42
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
43
|
+
* @returns {string | undefined}
|
|
44
|
+
*/
|
|
45
|
+
export declare const resolveAssociationForeignKey: (source: ITableMetadata, association: IAssociationMetadata, tablesByModel: ReadonlyMap<string, ITableMetadata>) => string | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Resolve the complete wiring for an association call: source and target model,
|
|
48
|
+
* Sequelize method, and options in output order.
|
|
49
|
+
* @param {ITableMetadata} source
|
|
50
|
+
* @param {IAssociationMetadata} association
|
|
51
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
52
|
+
* @returns {INativeAssociationWiring}
|
|
53
|
+
*/
|
|
54
|
+
export declare const resolveAssociationWiring: (source: ITableMetadata, association: IAssociationMetadata, tablesByModel: ReadonlyMap<string, ITableMetadata>) => INativeAssociationWiring;
|
|
55
|
+
/**
|
|
56
|
+
* A type argument of an association mixin. `model` is a bare model reference,
|
|
57
|
+
* `attribute` an indexed access (`model["attr"]`), `literal` a string literal
|
|
58
|
+
* type and `number` the fallback primary key type.
|
|
59
|
+
*/
|
|
60
|
+
export type MixinTypeArgument = {
|
|
61
|
+
kind: 'model';
|
|
62
|
+
name: string;
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'attribute';
|
|
65
|
+
model: string;
|
|
66
|
+
attribute: string;
|
|
67
|
+
} | {
|
|
68
|
+
kind: 'literal';
|
|
69
|
+
value: string;
|
|
70
|
+
} | {
|
|
71
|
+
kind: 'number';
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* A mixin member declaration on a model class.
|
|
75
|
+
*/
|
|
76
|
+
export interface IMixinDeclaration {
|
|
77
|
+
propertyName: string;
|
|
78
|
+
mixinTypeName: string;
|
|
79
|
+
typeArguments: MixinTypeArgument[];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Build the mixin member declarations an association contributes to its source
|
|
83
|
+
* model. To-one associations expose get/set/create; to-many associations expose
|
|
84
|
+
* the full get/set/add/remove/has/count/create set.
|
|
85
|
+
* @param {IAssociationMetadata} association
|
|
86
|
+
* @param {string} alias
|
|
87
|
+
* @param {string | undefined} targetPrimaryKeyAttribute
|
|
88
|
+
* @param {string | undefined} foreignKeyAttribute
|
|
89
|
+
* @returns {IMixinDeclaration[]}
|
|
90
|
+
*/
|
|
91
|
+
export declare const buildAssociationMixinDeclarations: (association: IAssociationMetadata, alias: string, targetPrimaryKeyAttribute: string | undefined, foreignKeyAttribute: string | undefined) => IMixinDeclaration[];
|
|
92
|
+
/**
|
|
93
|
+
* Collect the named imports a native model file needs from `sequelize`: the base
|
|
94
|
+
* set, the brands its attributes use, and every association mixin type. The
|
|
95
|
+
* result is sorted for deterministic output.
|
|
96
|
+
* @param {ITableMetadata} table
|
|
97
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
98
|
+
* @returns {string[]}
|
|
99
|
+
*/
|
|
100
|
+
export declare const collectSequelizeImports: (table: ITableMetadata, tablesByModel: ReadonlyMap<string, ITableMetadata>) => string[];
|
|
101
|
+
/**
|
|
102
|
+
* Collect the model names a native model file imports as types: foreign key
|
|
103
|
+
* targets and association targets, excluding the model itself and any
|
|
104
|
+
* non-generated model. The result is sorted for deterministic output.
|
|
105
|
+
* @param {ITableMetadata} table
|
|
106
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
107
|
+
* @returns {string[]}
|
|
108
|
+
*/
|
|
109
|
+
export declare const collectModelTypeImports: (table: ITableMetadata, tablesByModel: ReadonlyMap<string, ITableMetadata>) => string[];
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import pluralize from 'pluralize';
|
|
2
|
+
import { resolveAssociationPropertyName } from './associationNaming.js';
|
|
3
|
+
import { classifyAttribute, resolvePrimaryKeyAttribute } from './nativeAttributes.js';
|
|
4
|
+
/**
|
|
5
|
+
* Sequelize association method for each association kind.
|
|
6
|
+
*/
|
|
7
|
+
export const ASSOCIATION_METHODS = {
|
|
8
|
+
HasOne: 'hasOne',
|
|
9
|
+
HasMany: 'hasMany',
|
|
10
|
+
BelongsTo: 'belongsTo',
|
|
11
|
+
BelongsToMany: 'belongsToMany',
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Report whether an association exposes a collection on the source model.
|
|
15
|
+
* @param {IAssociationMetadata} association
|
|
16
|
+
* @returns {boolean}
|
|
17
|
+
*/
|
|
18
|
+
export const isToManyAssociation = (association) => association.associationName.includes('Many');
|
|
19
|
+
/**
|
|
20
|
+
* Find the model field name of the single-column foreign key on a table whose
|
|
21
|
+
* constraint points at the given target model.
|
|
22
|
+
* @param {ITableMetadata} table
|
|
23
|
+
* @param {string} targetModel
|
|
24
|
+
* @returns {string | undefined}
|
|
25
|
+
*/
|
|
26
|
+
const findForeignKeyColumnTargeting = (table, targetModel) => {
|
|
27
|
+
for (const column of Object.values(table.columns)) {
|
|
28
|
+
if (column.foreignKey && column.foreignKey.targetModel === targetModel) {
|
|
29
|
+
return column.name;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Resolve the foreign key column of an association. A discovered association
|
|
36
|
+
* carries it directly; a file-defined one is resolved from the constraint that
|
|
37
|
+
* connects the two models, on the side that holds the foreign key.
|
|
38
|
+
* @param {ITableMetadata} source
|
|
39
|
+
* @param {IAssociationMetadata} association
|
|
40
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
41
|
+
* @returns {string | undefined}
|
|
42
|
+
*/
|
|
43
|
+
export const resolveAssociationForeignKey = (source, association, tablesByModel) => {
|
|
44
|
+
if (association.foreignKey) {
|
|
45
|
+
return association.foreignKey;
|
|
46
|
+
}
|
|
47
|
+
const { associationName, targetModel, joinModel } = association;
|
|
48
|
+
if (associationName === 'BelongsTo') {
|
|
49
|
+
return findForeignKeyColumnTargeting(source, targetModel);
|
|
50
|
+
}
|
|
51
|
+
if (associationName === 'HasOne' || associationName === 'HasMany') {
|
|
52
|
+
const target = tablesByModel.get(targetModel);
|
|
53
|
+
return target && findForeignKeyColumnTargeting(target, source.name);
|
|
54
|
+
}
|
|
55
|
+
const junction = joinModel ? tablesByModel.get(joinModel) : undefined;
|
|
56
|
+
return junction && findForeignKeyColumnTargeting(junction, source.name);
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Resolve the other key of a many-to-many association: the junction foreign key
|
|
60
|
+
* pointing at the target model.
|
|
61
|
+
* @param {IAssociationMetadata} association
|
|
62
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
63
|
+
* @returns {string | undefined}
|
|
64
|
+
*/
|
|
65
|
+
const resolveAssociationOtherKey = (association, tablesByModel) => {
|
|
66
|
+
if (association.associationName !== 'BelongsToMany') {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const junction = association.joinModel ? tablesByModel.get(association.joinModel) : undefined;
|
|
70
|
+
return junction && findForeignKeyColumnTargeting(junction, association.targetModel);
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Resolve the target key of an association: the explicit target key, otherwise,
|
|
74
|
+
* for a BelongsTo, the target key of the source foreign key column.
|
|
75
|
+
* @param {ITableMetadata} source
|
|
76
|
+
* @param {IAssociationMetadata} association
|
|
77
|
+
* @returns {string | undefined}
|
|
78
|
+
*/
|
|
79
|
+
const resolveAssociationTargetKey = (source, association) => {
|
|
80
|
+
if (association.targetKey) {
|
|
81
|
+
return association.targetKey;
|
|
82
|
+
}
|
|
83
|
+
if (association.associationName !== 'BelongsTo') {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
for (const column of Object.values(source.columns)) {
|
|
87
|
+
if (column.foreignKey && column.foreignKey.targetModel === association.targetModel) {
|
|
88
|
+
return column.foreignKey.targetKey;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Resolve the complete wiring for an association call: source and target model,
|
|
95
|
+
* Sequelize method, and options in output order.
|
|
96
|
+
* @param {ITableMetadata} source
|
|
97
|
+
* @param {IAssociationMetadata} association
|
|
98
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
99
|
+
* @returns {INativeAssociationWiring}
|
|
100
|
+
*/
|
|
101
|
+
export const resolveAssociationWiring = (source, association, tablesByModel) => {
|
|
102
|
+
const alias = resolveAssociationPropertyName(association);
|
|
103
|
+
const foreignKey = resolveAssociationForeignKey(source, association, tablesByModel);
|
|
104
|
+
const otherKey = resolveAssociationOtherKey(association, tablesByModel);
|
|
105
|
+
const targetKey = resolveAssociationTargetKey(source, association);
|
|
106
|
+
const options = {
|
|
107
|
+
as: alias,
|
|
108
|
+
...association.joinModel && { throughModel: association.joinModel },
|
|
109
|
+
...foreignKey && { foreignKey },
|
|
110
|
+
...otherKey && { otherKey },
|
|
111
|
+
...targetKey && { targetKey },
|
|
112
|
+
...association.sourceKey && { sourceKey: association.sourceKey },
|
|
113
|
+
...association.onDelete && { onDelete: association.onDelete },
|
|
114
|
+
...association.onUpdate && { onUpdate: association.onUpdate },
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
sourceModel: source.name,
|
|
118
|
+
targetModel: association.targetModel,
|
|
119
|
+
method: ASSOCIATION_METHODS[association.associationName],
|
|
120
|
+
options,
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Uppercase the first character of a name.
|
|
125
|
+
* @param {string} value
|
|
126
|
+
* @returns {string}
|
|
127
|
+
*/
|
|
128
|
+
const upperFirst = (value) => value.length ? value[0].toUpperCase() + value.slice(1) : value;
|
|
129
|
+
/**
|
|
130
|
+
* Build the primary key type argument of a mixin. When the target has a single
|
|
131
|
+
* primary key it is an indexed access; otherwise it falls back to `number`.
|
|
132
|
+
* @param {string} targetModel
|
|
133
|
+
* @param {string | undefined} targetPrimaryKeyAttribute
|
|
134
|
+
* @returns {MixinTypeArgument}
|
|
135
|
+
*/
|
|
136
|
+
const buildPrimaryKeyTypeArgument = (targetModel, targetPrimaryKeyAttribute) => targetPrimaryKeyAttribute
|
|
137
|
+
? { kind: 'attribute', model: targetModel, attribute: targetPrimaryKeyAttribute }
|
|
138
|
+
: { kind: 'number' };
|
|
139
|
+
/**
|
|
140
|
+
* Build the mixin member declarations an association contributes to its source
|
|
141
|
+
* model. To-one associations expose get/set/create; to-many associations expose
|
|
142
|
+
* the full get/set/add/remove/has/count/create set.
|
|
143
|
+
* @param {IAssociationMetadata} association
|
|
144
|
+
* @param {string} alias
|
|
145
|
+
* @param {string | undefined} targetPrimaryKeyAttribute
|
|
146
|
+
* @param {string | undefined} foreignKeyAttribute
|
|
147
|
+
* @returns {IMixinDeclaration[]}
|
|
148
|
+
*/
|
|
149
|
+
export const buildAssociationMixinDeclarations = (association, alias, targetPrimaryKeyAttribute, foreignKeyAttribute) => {
|
|
150
|
+
const { associationName, targetModel } = association;
|
|
151
|
+
const modelArgument = { kind: 'model', name: targetModel };
|
|
152
|
+
const primaryKeyArgument = buildPrimaryKeyTypeArgument(targetModel, targetPrimaryKeyAttribute);
|
|
153
|
+
const pluralName = upperFirst(alias);
|
|
154
|
+
const singularName = upperFirst(pluralize.singular(alias));
|
|
155
|
+
if (associationName === 'BelongsTo' || associationName === 'HasOne') {
|
|
156
|
+
const prefix = associationName === 'BelongsTo' ? 'BelongsTo' : 'HasOne';
|
|
157
|
+
return [
|
|
158
|
+
{
|
|
159
|
+
propertyName: `get${pluralName}`,
|
|
160
|
+
mixinTypeName: `${prefix}GetAssociationMixin`,
|
|
161
|
+
typeArguments: [modelArgument],
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
propertyName: `set${pluralName}`,
|
|
165
|
+
mixinTypeName: `${prefix}SetAssociationMixin`,
|
|
166
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
propertyName: `create${pluralName}`,
|
|
170
|
+
mixinTypeName: `${prefix}CreateAssociationMixin`,
|
|
171
|
+
typeArguments: [modelArgument],
|
|
172
|
+
},
|
|
173
|
+
];
|
|
174
|
+
}
|
|
175
|
+
const prefix = associationName === 'HasMany' ? 'HasMany' : 'BelongsToMany';
|
|
176
|
+
const createArguments = associationName === 'HasMany'
|
|
177
|
+
? foreignKeyAttribute
|
|
178
|
+
? [modelArgument, { kind: 'literal', value: foreignKeyAttribute }]
|
|
179
|
+
: [modelArgument]
|
|
180
|
+
: [modelArgument];
|
|
181
|
+
return [
|
|
182
|
+
{
|
|
183
|
+
propertyName: `get${pluralName}`,
|
|
184
|
+
mixinTypeName: `${prefix}GetAssociationsMixin`,
|
|
185
|
+
typeArguments: [modelArgument],
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
propertyName: `set${pluralName}`,
|
|
189
|
+
mixinTypeName: `${prefix}SetAssociationsMixin`,
|
|
190
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
propertyName: `add${singularName}`,
|
|
194
|
+
mixinTypeName: `${prefix}AddAssociationMixin`,
|
|
195
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
propertyName: `add${pluralName}`,
|
|
199
|
+
mixinTypeName: `${prefix}AddAssociationsMixin`,
|
|
200
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
propertyName: `remove${singularName}`,
|
|
204
|
+
mixinTypeName: `${prefix}RemoveAssociationMixin`,
|
|
205
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
propertyName: `remove${pluralName}`,
|
|
209
|
+
mixinTypeName: `${prefix}RemoveAssociationsMixin`,
|
|
210
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
propertyName: `has${singularName}`,
|
|
214
|
+
mixinTypeName: `${prefix}HasAssociationMixin`,
|
|
215
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
propertyName: `has${pluralName}`,
|
|
219
|
+
mixinTypeName: `${prefix}HasAssociationsMixin`,
|
|
220
|
+
typeArguments: [modelArgument, primaryKeyArgument],
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
propertyName: `count${pluralName}`,
|
|
224
|
+
mixinTypeName: `${prefix}CountAssociationsMixin`,
|
|
225
|
+
typeArguments: [],
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
propertyName: `create${singularName}`,
|
|
229
|
+
mixinTypeName: `${prefix}CreateAssociationMixin`,
|
|
230
|
+
typeArguments: createArguments,
|
|
231
|
+
},
|
|
232
|
+
];
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Base named imports from `sequelize` every native model file needs.
|
|
236
|
+
*/
|
|
237
|
+
const BASE_SEQUELIZE_IMPORTS = [
|
|
238
|
+
'DataTypes',
|
|
239
|
+
'InferAttributes',
|
|
240
|
+
'InferCreationAttributes',
|
|
241
|
+
'Model',
|
|
242
|
+
'Sequelize',
|
|
243
|
+
];
|
|
244
|
+
/**
|
|
245
|
+
* Collect the named imports a native model file needs from `sequelize`: the base
|
|
246
|
+
* set, the brands its attributes use, and every association mixin type. The
|
|
247
|
+
* result is sorted for deterministic output.
|
|
248
|
+
* @param {ITableMetadata} table
|
|
249
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
250
|
+
* @returns {string[]}
|
|
251
|
+
*/
|
|
252
|
+
export const collectSequelizeImports = (table, tablesByModel) => {
|
|
253
|
+
const imports = new Set(BASE_SEQUELIZE_IMPORTS);
|
|
254
|
+
for (const column of Object.values(table.columns)) {
|
|
255
|
+
const targetModel = column.foreignKey?.targetModel;
|
|
256
|
+
const isForeignKeyTargetGenerated = targetModel !== undefined && tablesByModel.has(targetModel);
|
|
257
|
+
const kind = classifyAttribute(column, table, isForeignKeyTargetGenerated);
|
|
258
|
+
if (kind === 'foreignKey' || kind === 'foreignKeyNullable') {
|
|
259
|
+
imports.add('ForeignKey');
|
|
260
|
+
}
|
|
261
|
+
else if (kind === 'creationOptional' || kind === 'creationOptionalNullable') {
|
|
262
|
+
imports.add('CreationOptional');
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (table.timestamps) {
|
|
266
|
+
imports.add('CreationOptional');
|
|
267
|
+
}
|
|
268
|
+
const associations = table.associations ?? [];
|
|
269
|
+
if (associations.length) {
|
|
270
|
+
imports.add('Association');
|
|
271
|
+
imports.add('NonAttribute');
|
|
272
|
+
for (const association of associations) {
|
|
273
|
+
const target = tablesByModel.get(association.targetModel);
|
|
274
|
+
const alias = resolveAssociationPropertyName(association);
|
|
275
|
+
const targetPrimaryKeyAttribute = target && resolvePrimaryKeyAttribute(target);
|
|
276
|
+
const foreignKeyAttribute = resolveAssociationForeignKey(table, association, tablesByModel);
|
|
277
|
+
for (const mixin of buildAssociationMixinDeclarations(association, alias, targetPrimaryKeyAttribute, foreignKeyAttribute)) {
|
|
278
|
+
imports.add(mixin.mixinTypeName);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return [...imports].sort();
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Collect the model names a native model file imports as types: foreign key
|
|
286
|
+
* targets and association targets, excluding the model itself and any
|
|
287
|
+
* non-generated model. The result is sorted for deterministic output.
|
|
288
|
+
* @param {ITableMetadata} table
|
|
289
|
+
* @param {ReadonlyMap<string, ITableMetadata>} tablesByModel
|
|
290
|
+
* @returns {string[]}
|
|
291
|
+
*/
|
|
292
|
+
export const collectModelTypeImports = (table, tablesByModel) => {
|
|
293
|
+
const imports = new Set();
|
|
294
|
+
const selfName = table.name;
|
|
295
|
+
for (const column of Object.values(table.columns)) {
|
|
296
|
+
const targetModel = column.foreignKey?.targetModel;
|
|
297
|
+
if (targetModel !== undefined && targetModel !== selfName && tablesByModel.has(targetModel)) {
|
|
298
|
+
imports.add(targetModel);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
for (const association of table.associations ?? []) {
|
|
302
|
+
const { targetModel } = association;
|
|
303
|
+
if (targetModel !== selfName && tablesByModel.has(targetModel)) {
|
|
304
|
+
imports.add(targetModel);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return [...imports].sort();
|
|
308
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { IndexMethod } from 'sequelize';
|
|
2
|
+
import type { IColumnMetadata, IColumnForeignKeyMetadata, ITableMetadata, ITablesMetadata } from '../dialects/Dialect.js';
|
|
3
|
+
/**
|
|
4
|
+
* Attribute kinds a native column declaration can take. They drive both the
|
|
5
|
+
* TypeScript type node (plain type, `T | null`, `CreationOptional<...>`,
|
|
6
|
+
* `ForeignKey<...>`) and the imports the model file needs.
|
|
7
|
+
*/
|
|
8
|
+
export declare const ATTRIBUTE_KINDS: readonly ["plain", "nullable", "creationOptional", "creationOptionalNullable", "foreignKey", "foreignKeyNullable"];
|
|
9
|
+
export type AttributeKind = typeof ATTRIBUTE_KINDS[number];
|
|
10
|
+
/**
|
|
11
|
+
* Column option keys emitted for a native attribute, in output order.
|
|
12
|
+
*/
|
|
13
|
+
export declare const COLUMN_OPTION_KEYS: readonly ["type", "primaryKey", "autoIncrement", "allowNull", "defaultValue", "comment", "field"];
|
|
14
|
+
/**
|
|
15
|
+
* Model init option keys emitted for a native model, in output order.
|
|
16
|
+
*/
|
|
17
|
+
export declare const INIT_OPTION_KEYS: readonly ["sequelize", "tableName", "freezeTableName", "schema", "timestamps", "paranoid", "deletedAt", "hasTrigger", "comment", "indexes"];
|
|
18
|
+
/**
|
|
19
|
+
* Sequelize-managed timestamp attributes declared on a model when timestamps
|
|
20
|
+
* are enabled.
|
|
21
|
+
*/
|
|
22
|
+
export declare const TIMESTAMP_ATTRIBUTES: readonly ["createdAt", "updatedAt"];
|
|
23
|
+
/**
|
|
24
|
+
* Report whether a column is the soft-delete marker of a paranoid table.
|
|
25
|
+
* @param {IColumnMetadata} column
|
|
26
|
+
* @param {ITableMetadata} table
|
|
27
|
+
* @returns {boolean}
|
|
28
|
+
*/
|
|
29
|
+
export declare const isParanoidColumn: (column: IColumnMetadata, table: ITableMetadata) => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Report whether a column is optional at creation time. Auto-increment columns,
|
|
32
|
+
* columns with a default value and the soft-delete column are populated by the
|
|
33
|
+
* database or Sequelize, so they are typed with `CreationOptional`.
|
|
34
|
+
* @param {IColumnMetadata} column
|
|
35
|
+
* @param {ITableMetadata} table
|
|
36
|
+
* @returns {boolean}
|
|
37
|
+
*/
|
|
38
|
+
export declare const isCreationOptionalColumn: (column: IColumnMetadata, table: ITableMetadata) => boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Classify a column into a native attribute kind. A column that carries a
|
|
41
|
+
* foreign key to a generated model is branded with `ForeignKey`; otherwise the
|
|
42
|
+
* creation-optional and nullable flags decide the kind.
|
|
43
|
+
* @param {IColumnMetadata} column
|
|
44
|
+
* @param {ITableMetadata} table
|
|
45
|
+
* @param {boolean} isForeignKeyTargetGenerated
|
|
46
|
+
* @returns {AttributeKind}
|
|
47
|
+
*/
|
|
48
|
+
export declare const classifyAttribute: (column: IColumnMetadata, table: ITableMetadata, isForeignKeyTargetGenerated: boolean) => AttributeKind;
|
|
49
|
+
/**
|
|
50
|
+
* Resolve the single primary key attribute of a table. Composite and missing
|
|
51
|
+
* primary keys resolve to undefined.
|
|
52
|
+
* @param {ITableMetadata} table
|
|
53
|
+
* @returns {string | undefined}
|
|
54
|
+
*/
|
|
55
|
+
export declare const resolvePrimaryKeyAttribute: (table: ITableMetadata) => string | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Index tables by model name for lookups keyed by association and foreign key
|
|
58
|
+
* target model names.
|
|
59
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
60
|
+
* @returns {ReadonlyMap<string, ITableMetadata>}
|
|
61
|
+
*/
|
|
62
|
+
export declare const indexTablesByModelName: (tablesMetadata: ITablesMetadata) => ReadonlyMap<string, ITableMetadata>;
|
|
63
|
+
/**
|
|
64
|
+
* Resolve the target attribute a foreign key references: the explicit target
|
|
65
|
+
* key, otherwise the single primary key of the target model.
|
|
66
|
+
* @param {IColumnForeignKeyMetadata} foreignKey
|
|
67
|
+
* @param {ITableMetadata} target
|
|
68
|
+
* @returns {string | undefined}
|
|
69
|
+
*/
|
|
70
|
+
export declare const resolveForeignKeyTargetAttribute: (foreignKey: IColumnForeignKeyMetadata, target: ITableMetadata) => string | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* A native index descriptor. Fields are database column names in index order.
|
|
73
|
+
*/
|
|
74
|
+
export interface INativeIndex {
|
|
75
|
+
name: string;
|
|
76
|
+
isUnique: boolean;
|
|
77
|
+
fields: string[];
|
|
78
|
+
using?: IndexMethod;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Report whether an index covers exactly the primary key column set. Such an
|
|
82
|
+
* index is the implicit primary key index and is not re-emitted.
|
|
83
|
+
* @param {readonly string[]} fields
|
|
84
|
+
* @param {readonly string[]} primaryKeyColumns
|
|
85
|
+
* @returns {boolean}
|
|
86
|
+
*/
|
|
87
|
+
export declare const isPrimaryKeyIndex: (fields: readonly string[], primaryKeyColumns: readonly string[]) => boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Collect the indexes to emit for a table. Column indices are grouped by index
|
|
90
|
+
* name in first-seen order while scanning columns; each index's fields are the
|
|
91
|
+
* database column names in `seq` order. The implicit primary key index is
|
|
92
|
+
* skipped.
|
|
93
|
+
* @param {ITableMetadata} table
|
|
94
|
+
* @returns {INativeIndex[]}
|
|
95
|
+
*/
|
|
96
|
+
export declare const collectTableIndexes: (table: ITableMetadata) => INativeIndex[];
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attribute kinds a native column declaration can take. They drive both the
|
|
3
|
+
* TypeScript type node (plain type, `T | null`, `CreationOptional<...>`,
|
|
4
|
+
* `ForeignKey<...>`) and the imports the model file needs.
|
|
5
|
+
*/
|
|
6
|
+
export const ATTRIBUTE_KINDS = [
|
|
7
|
+
'plain',
|
|
8
|
+
'nullable',
|
|
9
|
+
'creationOptional',
|
|
10
|
+
'creationOptionalNullable',
|
|
11
|
+
'foreignKey',
|
|
12
|
+
'foreignKeyNullable',
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Column option keys emitted for a native attribute, in output order.
|
|
16
|
+
*/
|
|
17
|
+
export const COLUMN_OPTION_KEYS = [
|
|
18
|
+
'type',
|
|
19
|
+
'primaryKey',
|
|
20
|
+
'autoIncrement',
|
|
21
|
+
'allowNull',
|
|
22
|
+
'defaultValue',
|
|
23
|
+
'comment',
|
|
24
|
+
'field',
|
|
25
|
+
];
|
|
26
|
+
/**
|
|
27
|
+
* Model init option keys emitted for a native model, in output order.
|
|
28
|
+
*/
|
|
29
|
+
export const INIT_OPTION_KEYS = [
|
|
30
|
+
'sequelize',
|
|
31
|
+
'tableName',
|
|
32
|
+
'freezeTableName',
|
|
33
|
+
'schema',
|
|
34
|
+
'timestamps',
|
|
35
|
+
'paranoid',
|
|
36
|
+
'deletedAt',
|
|
37
|
+
'hasTrigger',
|
|
38
|
+
'comment',
|
|
39
|
+
'indexes',
|
|
40
|
+
];
|
|
41
|
+
/**
|
|
42
|
+
* Sequelize-managed timestamp attributes declared on a model when timestamps
|
|
43
|
+
* are enabled.
|
|
44
|
+
*/
|
|
45
|
+
export const TIMESTAMP_ATTRIBUTES = ['createdAt', 'updatedAt'];
|
|
46
|
+
/**
|
|
47
|
+
* Report whether a column is the soft-delete marker of a paranoid table.
|
|
48
|
+
* @param {IColumnMetadata} column
|
|
49
|
+
* @param {ITableMetadata} table
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
export const isParanoidColumn = (column, table) => table.paranoid === true && table.deletedAt === column.name;
|
|
53
|
+
/**
|
|
54
|
+
* Report whether a column is optional at creation time. Auto-increment columns,
|
|
55
|
+
* columns with a default value and the soft-delete column are populated by the
|
|
56
|
+
* database or Sequelize, so they are typed with `CreationOptional`.
|
|
57
|
+
* @param {IColumnMetadata} column
|
|
58
|
+
* @param {ITableMetadata} table
|
|
59
|
+
* @returns {boolean}
|
|
60
|
+
*/
|
|
61
|
+
export const isCreationOptionalColumn = (column, table) => column.autoIncrement || column.defaultValue !== undefined || isParanoidColumn(column, table);
|
|
62
|
+
/**
|
|
63
|
+
* Classify a column into a native attribute kind. A column that carries a
|
|
64
|
+
* foreign key to a generated model is branded with `ForeignKey`; otherwise the
|
|
65
|
+
* creation-optional and nullable flags decide the kind.
|
|
66
|
+
* @param {IColumnMetadata} column
|
|
67
|
+
* @param {ITableMetadata} table
|
|
68
|
+
* @param {boolean} isForeignKeyTargetGenerated
|
|
69
|
+
* @returns {AttributeKind}
|
|
70
|
+
*/
|
|
71
|
+
export const classifyAttribute = (column, table, isForeignKeyTargetGenerated) => {
|
|
72
|
+
if (isForeignKeyTargetGenerated) {
|
|
73
|
+
return column.allowNull ? 'foreignKeyNullable' : 'foreignKey';
|
|
74
|
+
}
|
|
75
|
+
const creationOptional = isCreationOptionalColumn(column, table);
|
|
76
|
+
if (creationOptional && column.allowNull) {
|
|
77
|
+
return 'creationOptionalNullable';
|
|
78
|
+
}
|
|
79
|
+
if (creationOptional) {
|
|
80
|
+
return 'creationOptional';
|
|
81
|
+
}
|
|
82
|
+
return column.allowNull ? 'nullable' : 'plain';
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Resolve the single primary key attribute of a table. Composite and missing
|
|
86
|
+
* primary keys resolve to undefined.
|
|
87
|
+
* @param {ITableMetadata} table
|
|
88
|
+
* @returns {string | undefined}
|
|
89
|
+
*/
|
|
90
|
+
export const resolvePrimaryKeyAttribute = (table) => {
|
|
91
|
+
const primaryKeyColumns = Object.values(table.columns).filter(column => column.primaryKey);
|
|
92
|
+
return primaryKeyColumns.length === 1 ? primaryKeyColumns[0].name : undefined;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Index tables by model name for lookups keyed by association and foreign key
|
|
96
|
+
* target model names.
|
|
97
|
+
* @param {ITablesMetadata} tablesMetadata
|
|
98
|
+
* @returns {ReadonlyMap<string, ITableMetadata>}
|
|
99
|
+
*/
|
|
100
|
+
export const indexTablesByModelName = (tablesMetadata) => {
|
|
101
|
+
const tablesByModel = new Map();
|
|
102
|
+
for (const table of Object.values(tablesMetadata)) {
|
|
103
|
+
tablesByModel.set(table.name, table);
|
|
104
|
+
}
|
|
105
|
+
return tablesByModel;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Resolve the target attribute a foreign key references: the explicit target
|
|
109
|
+
* key, otherwise the single primary key of the target model.
|
|
110
|
+
* @param {IColumnForeignKeyMetadata} foreignKey
|
|
111
|
+
* @param {ITableMetadata} target
|
|
112
|
+
* @returns {string | undefined}
|
|
113
|
+
*/
|
|
114
|
+
export const resolveForeignKeyTargetAttribute = (foreignKey, target) => foreignKey.targetKey ?? resolvePrimaryKeyAttribute(target);
|
|
115
|
+
/**
|
|
116
|
+
* Report whether an index covers exactly the primary key column set. Such an
|
|
117
|
+
* index is the implicit primary key index and is not re-emitted.
|
|
118
|
+
* @param {readonly string[]} fields
|
|
119
|
+
* @param {readonly string[]} primaryKeyColumns
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
export const isPrimaryKeyIndex = (fields, primaryKeyColumns) => {
|
|
123
|
+
if (primaryKeyColumns.length === 0 || fields.length !== primaryKeyColumns.length) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
const primaryKeySet = new Set(primaryKeyColumns);
|
|
127
|
+
return fields.every(field => primaryKeySet.has(field));
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Collect the indexes to emit for a table. Column indices are grouped by index
|
|
131
|
+
* name in first-seen order while scanning columns; each index's fields are the
|
|
132
|
+
* database column names in `seq` order. The implicit primary key index is
|
|
133
|
+
* skipped.
|
|
134
|
+
* @param {ITableMetadata} table
|
|
135
|
+
* @returns {INativeIndex[]}
|
|
136
|
+
*/
|
|
137
|
+
export const collectTableIndexes = (table) => {
|
|
138
|
+
const columns = Object.values(table.columns);
|
|
139
|
+
const primaryKeyColumns = columns
|
|
140
|
+
.filter(column => column.primaryKey)
|
|
141
|
+
.map(column => column.originName);
|
|
142
|
+
const indexesByName = new Map();
|
|
143
|
+
for (const column of columns) {
|
|
144
|
+
for (const index of column.indices ?? []) {
|
|
145
|
+
let accumulator = indexesByName.get(index.name);
|
|
146
|
+
if (!accumulator) {
|
|
147
|
+
accumulator = {
|
|
148
|
+
name: index.name,
|
|
149
|
+
isUnique: index.unique ?? false,
|
|
150
|
+
...index.using && { using: index.using },
|
|
151
|
+
fields: [],
|
|
152
|
+
};
|
|
153
|
+
indexesByName.set(index.name, accumulator);
|
|
154
|
+
}
|
|
155
|
+
accumulator.fields.push({ field: column.originName, seq: index.seq ?? 0 });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const indexes = [];
|
|
159
|
+
for (const accumulator of indexesByName.values()) {
|
|
160
|
+
const fields = [...accumulator.fields]
|
|
161
|
+
.sort((left, right) => left.seq - right.seq)
|
|
162
|
+
.map(entry => entry.field);
|
|
163
|
+
if (isPrimaryKeyIndex(fields, primaryKeyColumns)) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
indexes.push({
|
|
167
|
+
name: accumulator.name,
|
|
168
|
+
isUnique: accumulator.isUnique,
|
|
169
|
+
fields,
|
|
170
|
+
...accumulator.using && { using: accumulator.using },
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return indexes;
|
|
174
|
+
};
|