MCPcopy
hub / github.com/sequelize/sequelize-auto / addField

Method addField

src/auto-generator.ts:236–428  ·  view source on GitHub ↗
(table: string, field: string)

Source from the content-addressed store, hash-verified

234
235 // Create a string containing field attributes (type, defaultValue, etc.)
236 private addField(table: string, field: string): string {
237
238 // ignore Sequelize standard fields
239 const additional = this.options.additional;
240 if (additional && (additional.timestamps !== false) && (this.isTimestampField(field) || this.isParanoidField(field))) {
241 return '';
242 }
243
244 if (this.isIgnoredField(field)) {
245 return '';
246 }
247
248 // Find foreign key
249 const foreignKey = this.foreignKeys[table] && this.foreignKeys[table][field] ? this.foreignKeys[table][field] : null;
250 const fieldObj = this.tables[table][field] as Field;
251
252 if (_.isObject(foreignKey)) {
253 fieldObj.foreignKey = foreignKey;
254 }
255
256 const fieldName = recase(this.options.caseProp, field);
257 let str = this.quoteName(fieldName) + ": {\n";
258
259 const quoteWrapper = '"';
260
261 const unique = fieldObj.unique || fieldObj.foreignKey && fieldObj.foreignKey.isUnique;
262
263 const isSerialKey = (fieldObj.foreignKey && fieldObj.foreignKey.isSerialKey) ||
264 this.dialect.isSerialKey && this.dialect.isSerialKey(fieldObj);
265
266 let wroteAutoIncrement = false;
267 const space = this.space;
268
269 // column's attributes
270 const fieldAttrs = _.keys(fieldObj);
271 fieldAttrs.forEach(attr => {
272
273 // We don't need the special attribute from postgresql; "unique" is handled separately
274 if (attr === "special" || attr === "elementType" || attr === "unique") {
275 return true;
276 }
277
278 if (isSerialKey && !wroteAutoIncrement) {
279 str += space[3] + "autoIncrement: true,\n";
280 // Resort to Postgres' GENERATED BY DEFAULT AS IDENTITY instead of SERIAL
281 if (this.dialect.name === "postgres" && fieldObj.foreignKey && fieldObj.foreignKey.isPrimaryKey === true &&
282 (fieldObj.foreignKey.generation === "ALWAYS" || fieldObj.foreignKey.generation === "BY DEFAULT")) {
283 str += space[3] + "autoIncrementIdentity: true,\n";
284 }
285 wroteAutoIncrement = true;
286 }
287
288 if (attr === "foreignKey") {
289 if (foreignKey && foreignKey.isForeignKey) {
290 str += space[3] + "references: {\n";
291 str += space[4] + "model: \'" + fieldObj[attr].foreignSources.target_table + "\',\n";
292 str += space[4] + "key: \'" + fieldObj[attr].foreignSources.target_column + "\'\n";
293 str += space[3] + "}";

Callers 1

addTableMethod · 0.95

Calls 10

isTimestampFieldMethod · 0.95
isParanoidFieldMethod · 0.95
isIgnoredFieldMethod · 0.95
quoteNameMethod · 0.95
isStringMethod · 0.95
escapeSpecialMethod · 0.95
isArrayMethod · 0.95
isNumberMethod · 0.95
getSqTypeMethod · 0.95
recaseFunction · 0.90

Tested by

no test coverage detected