• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knex / knex / 3980053368

pending completion
3980053368

Pull #5459

github

GitHub
Merge 6d89bfd2f into 5caf526c2
Pull Request #5459: Additional lint checks before publishing

4186 of 4984 branches covered (83.99%)

Branch coverage included in aggregate %.

11 of 11 new or added lines in 7 files covered. (100.0%)

19749 of 20922 relevant lines covered (94.39%)

1379.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.16
/lib/schema/columnbuilder.js
1
const extend = require('lodash/extend');
63✔
2
const assign = require('lodash/assign');
63✔
3
const toArray = require('lodash/toArray');
63✔
4
const { addQueryContext } = require('../util/helpers');
63✔
5

6
// The chainable interface off the original "column" method.
7
class ColumnBuilder {
8
  constructor(client, tableBuilder, type, args) {
9
    this.client = client;
6,698✔
10
    this._method = 'add';
6,698✔
11
    this._single = {};
6,698✔
12
    this._modifiers = {};
6,698✔
13
    this._statements = [];
6,698✔
14
    this._type = columnAlias[type] || type;
6,698✔
15
    this._args = args;
6,698✔
16
    this._tableBuilder = tableBuilder;
6,698✔
17

18
    // If we're altering the table, extend the object
19
    // with the available "alter" methods.
20
    if (tableBuilder._method === 'alter') {
6,698✔
21
      extend(this, AlterMethods);
730✔
22
    }
23
  }
24

25
  // Specify that the current column "references" a column,
26
  // which may be tableName.column or just "column"
27
  references(value) {
28
    return this._tableBuilder.foreign
121✔
29
      .call(this._tableBuilder, this._args[0], undefined, this)
30
      ._columnBuilder(this)
31
      .references(value);
32
  }
33
}
34

35
// All of the modifier methods that can be used to modify the current query.
36
const modifiers = [
63✔
37
  'default',
38
  'defaultsTo',
39
  'defaultTo',
40
  'unsigned',
41
  'nullable',
42
  'first',
43
  'after',
44
  'comment',
45
  'collate',
46
  'check',
47
  'checkPositive',
48
  'checkNegative',
49
  'checkIn',
50
  'checkNotIn',
51
  'checkBetween',
52
  'checkLength',
53
  'checkRegex',
54
];
55

56
// Aliases for convenience.
57
const aliasMethod = {
63✔
58
  default: 'defaultTo',
59
  defaultsTo: 'defaultTo',
60
};
61

62
// If we call any of the modifiers (index or otherwise) on the chainable, we pretend
63
// as though we're calling `table.method(column)` directly.
64
modifiers.forEach(function (method) {
63✔
65
  const key = aliasMethod[method] || method;
1,071✔
66
  ColumnBuilder.prototype[method] = function () {
1,071✔
67
    this._modifiers[key] = toArray(arguments);
2,016✔
68
    return this;
2,016✔
69
  };
70
});
71

72
addQueryContext(ColumnBuilder);
63✔
73

74
ColumnBuilder.prototype.notNull = ColumnBuilder.prototype.notNullable =
63✔
75
  function notNullable() {
76
    return this.nullable(false);
555✔
77
  };
78

79
['index', 'primary', 'unique'].forEach(function (method) {
63✔
80
  ColumnBuilder.prototype[method] = function () {
189✔
81
    if (this._type.toLowerCase().indexOf('increments') === -1) {
626✔
82
      this._tableBuilder[method].apply(
480✔
83
        this._tableBuilder,
84
        [this._args[0]].concat(toArray(arguments))
85
      );
86
    }
87
    return this;
626✔
88
  };
89
});
90

91
ColumnBuilder.extend = (methodName, fn) => {
63✔
92
  if (
1!
93
    Object.prototype.hasOwnProperty.call(ColumnBuilder.prototype, methodName)
94
  ) {
95
    throw new Error(
×
96
      `Can't extend ColumnBuilder with existing method ('${methodName}').`
97
    );
98
  }
99

100
  assign(ColumnBuilder.prototype, { [methodName]: fn });
1✔
101
};
102

103
const AlterMethods = {};
63✔
104

105
// Specify that the column is to be dropped. This takes precedence
106
// over all other rules for the column.
107
AlterMethods.drop = function () {
63✔
108
  this._single.drop = true;
×
109

110
  return this;
×
111
};
112

113
// Specify the "type" that we're looking to set the
114
// Knex takes no responsibility for any data-loss that may
115
// occur when changing data types.
116
AlterMethods.alterType = function (type) {
63✔
117
  this._statements.push({
×
118
    grouping: 'alterType',
119
    value: type,
120
  });
121

122
  return this;
×
123
};
124

125
// Set column method to alter (default is add).
126
AlterMethods.alter = function ({
63✔
127
  alterNullable = true,
65✔
128
  alterType = true,
66✔
129
} = {}) {
130
  this._method = 'alter';
69✔
131
  this.alterNullable = alterNullable;
69✔
132
  this.alterType = alterType;
69✔
133

134
  return this;
69✔
135
};
136

137
// Alias a few methods for clarity when processing.
138
const columnAlias = {
63✔
139
  float: 'floating',
140
  enum: 'enu',
141
  boolean: 'bool',
142
  string: 'varchar',
143
  bigint: 'bigInteger',
144
};
145

146
module.exports = ColumnBuilder;
63✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc