/
createTable.js
40 lines (38 loc) · 1 KB
/
createTable.js
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
var knex = require('knex')({
client: 'postgresql',
connection: {
host : 'localhost',
port : 5432,
database : 'ubuntu',
charset : 'utf8'
}
});
var Promise = require('bluebird');
var createIndexes = process.argv.length > 1 && process.argv[2] === "index";
function createIndex(column) {
return knex.schema.table('messages', function (table) {
table.index(column);
});
}
if (createIndexes) {
Promise.all([['sender', 'recipient'], 'recipient', 'timestamp', 'id'].map(function (column) {
return createIndex(column);
})).then(function () {
knex.destroy();
});
} else {
knex.schema.createTable('messages', function (table) {
table.increments('id')
table.datetime('timestamp');
table.string('sender');
table.string('recipient');
table.text('message');
table.integer('dialog_id');
}).then(function () {
return knex.schema.createTable('dialogs', function (table) {
table.increments('id')
});
}).then(function () {
knex.destroy();
});
}