forked from sequelize/sequelize
/
query-interface.ts
231 lines (189 loc) · 6.54 KB
/
query-interface.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { DataTypes, Model, fn, literal, col } from 'sequelize';
// tslint:disable-next-line:no-submodule-imports
import { QueryInterface } from 'sequelize/lib/query-interface';
declare let queryInterface: QueryInterface;
async function test() {
await queryInterface.createTable(
'nameOfTheNewTable',
{
attr1: DataTypes.STRING,
attr2: DataTypes.INTEGER,
attr3: {
allowNull: false,
defaultValue: false,
type: DataTypes.BOOLEAN,
},
// foreign key usage
attr4: {
onDelete: 'cascade',
onUpdate: 'cascade',
references: {
key: 'id',
model: 'another_table_name',
},
type: DataTypes.INTEGER,
},
attr5: {
onDelete: 'cascade',
onUpdate: 'cascade',
references: {
key: 'id',
model: { schema: '<schema>', tableName: 'another_table_name' },
},
type: DataTypes.INTEGER,
},
createdAt: {
type: DataTypes.DATE,
},
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
updatedAt: {
type: DataTypes.DATE,
},
},
{
charset: 'latin1', // default: null
collate: 'latin1_general_ci',
engine: 'MYISAM', // default: 'InnoDB'
uniqueKeys: {
test: {
customIndex: true,
fields: ['attr2', 'attr3'],
}
}
}
);
await queryInterface.createTable({ tableName: '<table-name>' }, {});
await queryInterface.dropTable('nameOfTheExistingTable');
await queryInterface.dropTable({ schema: '<schema>', tableName: 'nameOfTheExistingTable' });
await queryInterface.bulkDelete({ tableName: 'foo', schema: 'bar' }, {}, {});
const bulkInsertRes: Promise<number | object> = queryInterface.bulkInsert({ tableName: 'foo', as: 'bar', name: 'as' }, [{}], {});
const bulkInsertResWithAttrs: Promise<number | object> = queryInterface.bulkInsert('foo', [{}], {}, { bar: { type: DataTypes.JSON } });
await queryInterface.bulkUpdate({ tableName: 'foo', delimiter: 'bar', as: 'baz', name: 'quz' }, {}, {});
await queryInterface.dropTrigger({ tableName: 'foo', as: 'bar', name: 'baz' }, 'foo', {});
await queryInterface.quoteTable({ tableName: 'foo', delimiter: 'bar' });
queryInterface.quoteIdentifier("foo");
queryInterface.quoteIdentifier("foo", true);
queryInterface.quoteIdentifiers("table.foo");
await queryInterface.dropAllTables();
await queryInterface.renameTable('Person', 'User');
await queryInterface.renameTable(
{ schema: '<schema>', tableName: 'Person' },
{ schema: '<schema>', tableName: 'User' },
);
const tableNames: string[] = await queryInterface.showAllTables();
/*
attributes will be something like:
{
name: {
type: 'VARCHAR(255)', // this will be 'CHARACTER VARYING' for pg!
allowNull: true,
defaultValue: null
},
isBetaMember: {
type: 'TINYINT(1)', // this will be 'BOOLEAN' for pg!
allowNull: false,
defaultValue: false
}
}
*/
const attributes: object = await queryInterface.describeTable('Person');
await queryInterface.addColumn('nameOfAnExistingTable', 'nameOfTheNewAttribute', DataTypes.STRING);
// or
await queryInterface.addColumn(
{ tableName: 'nameOfAnExistingTable', schema: 'nameOfSchema' },
'nameOfTheNewAttribute',
DataTypes.STRING
);
// or
await queryInterface.addColumn('nameOfAnExistingTable', 'nameOfTheNewAttribute', {
allowNull: false,
type: DataTypes.STRING,
});
await queryInterface.removeColumn('Person', 'signature');
// or
await queryInterface.removeColumn({ tableName: 'Person', schema: 'nameOfSchema' }, 'signature');
await queryInterface.changeColumn('nameOfAnExistingTable', 'nameOfAnExistingAttribute', {
allowNull: false,
defaultValue: 0.0,
type: DataTypes.FLOAT,
});
// or
await queryInterface.changeColumn(
{ tableName: 'nameOfAnExistingTable', schema: 'nameOfSchema' },
'nameOfAnExistingAttribute',
{
allowNull: false,
defaultValue: 0.0,
type: DataTypes.FLOAT,
}
);
await queryInterface.renameColumn('Person', 'signature', 'sig');
await queryInterface.renameColumn({ schema: '<schema>', tableName: 'Person' }, 'signature', 'sig');
// This example will create the index person_firstname_lastname
await queryInterface.addIndex('Person', ['firstname', 'lastname']);
await queryInterface.addIndex({ schema: '<schema>', tableName: 'Person' }, ['firstname', 'lastname']);
// This example will create a unique index with the name SuperDuperIndex using the optional 'options' field.
// Possible options:
// - indexName: The name of the index. Default is __
// - parser: For FULLTEXT columns set your parser
// - indexType: Set a type for the index, e.g. BTREE. See the documentation of the used dialect
// - logging: A function that receives the sql query, e.g. console.log
await queryInterface.addIndex('Person', ['firstname', 'lastname'], {
name: 'SuperDuperIndex',
type: 'UNIQUE',
});
await queryInterface.addIndex('Foo', {
name: 'foo_a',
fields: [
{ name: 'foo_b', order: 'DESC' },
'foo_c',
{ name: 'foo_d', order: 'ASC', collate: 'foobar', length: 42 }
],
});
await queryInterface.addIndex('Foo', {
name: 'foo_b_lower',
fields: [
fn('lower', col('foo_b'))
],
});
await queryInterface.addIndex('Foo', {
name: 'foo_c_lower',
fields: [
literal('LOWER(foo_c)')
]
})
await queryInterface.removeIndex('Person', 'SuperDuperIndex');
await queryInterface.removeIndex({ schema: '<schema>', tableName: 'Person' }, 'SuperDuperIndex');
// or
await queryInterface.removeIndex('Person', ['firstname', 'lastname']);
await queryInterface.sequelize.transaction(trx => queryInterface.addConstraint('Person', {
name: 'firstnamexlastname',
fields: ['firstname', 'lastname'],
type: 'unique',
transaction: trx,
}))
await queryInterface.removeConstraint('Person', 'firstnamexlastname');
await queryInterface.removeConstraint({ schema: '<schema>', tableName: 'Person' }, 'firstnamexlastname');
await queryInterface.select(null, 'Person', {
where: {
a: 1,
},
});
await queryInterface.select(null, { schema: '<schema>', tableName: 'Person' }, {
where: {
a: 1,
},
});
await queryInterface.delete(null, 'Person', {
where: {
a: 1,
},
});
class TestModel extends Model {}
await queryInterface.upsert("test", {"a": 1}, {"b": 2}, {"c": 3}, TestModel, {});
await queryInterface.insert(null, 'test', {});
}