|
1 |
| -var _ = require('lodash'), |
2 |
| - when = require('when'), |
3 |
| - knex = require('../../models/base').knex, |
4 |
| - schema = require('../schema').tables, |
5 |
| - client = require('../../models/base').client; |
| 1 | +var _ = require('lodash'), |
| 2 | + when = require('when'), |
| 3 | + knex = require('../../models/base').knex, |
| 4 | + schema = require('../schema').tables, |
| 5 | + client = require('../../models/base').client, |
| 6 | + sqlite3 = require('./sqlite3'), |
| 7 | + mysql = require('./mysql'), |
| 8 | + pgsql = require('./pgsql'); |
6 | 9 |
|
7 |
| -function createTable(table) { |
8 |
| - return knex.schema.createTable(table, function (t) { |
9 |
| - var column, |
10 |
| - columnKeys = _.keys(schema[table]); |
11 |
| - _.each(columnKeys, function (key) { |
12 |
| - // creation distinguishes between text with fieldtype, string with maxlength and all others |
13 |
| - if (schema[table][key].type === 'text' && schema[table][key].hasOwnProperty('fieldtype')) { |
14 |
| - column = t[schema[table][key].type](key, schema[table][key].fieldtype); |
15 |
| - } else if (schema[table][key].type === 'string' && schema[table][key].hasOwnProperty('maxlength')) { |
16 |
| - column = t[schema[table][key].type](key, schema[table][key].maxlength); |
17 |
| - } else { |
18 |
| - column = t[schema[table][key].type](key); |
19 |
| - } |
| 10 | +function addTableColumn(tablename, table, columnname) { |
| 11 | + var column; |
| 12 | + // creation distinguishes between text with fieldtype, string with maxlength and all others |
| 13 | + if (schema[tablename][columnname].type === 'text' && schema[tablename][columnname].hasOwnProperty('fieldtype')) { |
| 14 | + column = table[schema[tablename][columnname].type](columnname, schema[tablename][columnname].fieldtype); |
| 15 | + } else if (schema[tablename][columnname].type === 'string' && schema[tablename][columnname].hasOwnProperty('maxlength')) { |
| 16 | + column = table[schema[tablename][columnname].type](columnname, schema[tablename][columnname].maxlength); |
| 17 | + } else { |
| 18 | + column = table[schema[tablename][columnname].type](columnname); |
| 19 | + } |
20 | 20 |
|
21 |
| - if (schema[table][key].hasOwnProperty('nullable') && schema[table][key].nullable === true) { |
22 |
| - column.nullable(); |
23 |
| - } else { |
24 |
| - column.notNullable(); |
25 |
| - } |
26 |
| - if (schema[table][key].hasOwnProperty('primary') && schema[table][key].primary === true) { |
27 |
| - column.primary(); |
28 |
| - } |
29 |
| - if (schema[table][key].hasOwnProperty('unique') && schema[table][key].unique) { |
30 |
| - column.unique(); |
31 |
| - } |
32 |
| - if (schema[table][key].hasOwnProperty('unsigned') && schema[table][key].unsigned) { |
33 |
| - column.unsigned(); |
34 |
| - } |
35 |
| - if (schema[table][key].hasOwnProperty('references')) { |
36 |
| - //check if table exists? |
37 |
| - column.references(schema[table][key].references); |
38 |
| - } |
39 |
| - if (schema[table][key].hasOwnProperty('defaultTo')) { |
40 |
| - column.defaultTo(schema[table][key].defaultTo); |
41 |
| - } |
42 |
| - }); |
43 |
| - }); |
| 21 | + if (schema[tablename][columnname].hasOwnProperty('nullable') && schema[tablename][columnname].nullable === true) { |
| 22 | + column.nullable(); |
| 23 | + } else { |
| 24 | + column.notNullable(); |
| 25 | + } |
| 26 | + if (schema[tablename][columnname].hasOwnProperty('primary') && schema[tablename][columnname].primary === true) { |
| 27 | + column.primary(); |
| 28 | + } |
| 29 | + if (schema[tablename][columnname].hasOwnProperty('unique') && schema[tablename][columnname].unique) { |
| 30 | + column.unique(); |
| 31 | + } |
| 32 | + if (schema[tablename][columnname].hasOwnProperty('unsigned') && schema[tablename][columnname].unsigned) { |
| 33 | + column.unsigned(); |
| 34 | + } |
| 35 | + if (schema[tablename][columnname].hasOwnProperty('references')) { |
| 36 | + //check if table exists? |
| 37 | + column.references(schema[tablename][columnname].references); |
| 38 | + } |
| 39 | + if (schema[tablename][columnname].hasOwnProperty('defaultTo')) { |
| 40 | + column.defaultTo(schema[tablename][columnname].defaultTo); |
| 41 | + } |
44 | 42 | }
|
45 | 43 |
|
46 |
| -function deleteTable(table) { |
47 |
| - return knex.schema.dropTableIfExists(table); |
| 44 | +function addColumn(table, column) { |
| 45 | + return knex.schema.table(table, function (t) { |
| 46 | + addTableColumn(table, t, column); |
| 47 | + }); |
48 | 48 | }
|
49 | 49 |
|
50 |
| -function getTablesFromSqlite3() { |
51 |
| - return knex.raw("select * from sqlite_master where type = 'table'").then(function (response) { |
52 |
| - return _.reject(_.pluck(response, 'tbl_name'), function (name) { |
53 |
| - return name === 'sqlite_sequence'; |
54 |
| - }); |
| 50 | +function addUnique(table, column) { |
| 51 | + return knex.schema.table(table, function (table) { |
| 52 | + table.unique(column); |
55 | 53 | });
|
56 | 54 | }
|
57 | 55 |
|
58 |
| -function getTablesFromPgSQL() { |
59 |
| - return knex.raw("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'").then(function (response) { |
60 |
| - return _.flatten(_.pluck(response.rows, 'table_name')); |
| 56 | +function dropUnique(table, column) { |
| 57 | + return knex.schema.table(table, function (table) { |
| 58 | + table.dropUnique(column); |
61 | 59 | });
|
62 | 60 | }
|
63 | 61 |
|
64 |
| -function getTablesFromMySQL() { |
65 |
| - return knex.raw('show tables').then(function (response) { |
66 |
| - return _.flatten(_.map(response[0], function (entry) { |
67 |
| - return _.values(entry); |
68 |
| - })); |
| 62 | +function createTable(table) { |
| 63 | + return knex.schema.createTable(table, function (t) { |
| 64 | + var columnKeys = _.keys(schema[table]); |
| 65 | + _.each(columnKeys, function (column) { |
| 66 | + return addTableColumn(table, t, column); |
| 67 | + }); |
69 | 68 | });
|
70 | 69 | }
|
71 | 70 |
|
| 71 | +function deleteTable(table) { |
| 72 | + return knex.schema.dropTableIfExists(table); |
| 73 | +} |
| 74 | + |
72 | 75 | function getTables() {
|
73 | 76 | if (client === 'sqlite3') {
|
74 |
| - return getTablesFromSqlite3(); |
| 77 | + return sqlite3.getTables(); |
| 78 | + } |
| 79 | + if (client === 'mysql') { |
| 80 | + return mysql.getTables(); |
| 81 | + } |
| 82 | + if (client === 'pg') { |
| 83 | + return pgsql.getTables(); |
| 84 | + } |
| 85 | + return when.reject("No support for database client " + client); |
| 86 | +} |
| 87 | + |
| 88 | +function getIndexes(table) { |
| 89 | + if (client === 'sqlite3') { |
| 90 | + return sqlite3.getIndexes(table); |
| 91 | + } |
| 92 | + if (client === 'mysql') { |
| 93 | + return mysql.getIndexes(table); |
| 94 | + } |
| 95 | + if (client === 'pg') { |
| 96 | + return pgsql.getIndexes(table); |
| 97 | + } |
| 98 | + return when.reject("No support for database client " + client); |
| 99 | +} |
| 100 | + |
| 101 | +function getColumns(table) { |
| 102 | + if (client === 'sqlite3') { |
| 103 | + return sqlite3.getColumns(table); |
75 | 104 | }
|
76 | 105 | if (client === 'mysql') {
|
77 |
| - return getTablesFromMySQL(); |
| 106 | + return mysql.getColumns(table); |
78 | 107 | }
|
79 | 108 | if (client === 'pg') {
|
80 |
| - return getTablesFromPgSQL(); |
| 109 | + return pgsql.getColumns(table); |
81 | 110 | }
|
82 | 111 | return when.reject("No support for database client " + client);
|
83 | 112 | }
|
84 | 113 |
|
85 | 114 | module.exports = {
|
86 | 115 | createTable: createTable,
|
87 | 116 | deleteTable: deleteTable,
|
88 |
| - getTables: getTables |
| 117 | + getTables: getTables, |
| 118 | + getIndexes: getIndexes, |
| 119 | + addUnique: addUnique, |
| 120 | + dropUnique: dropUnique, |
| 121 | + addColumn: addColumn, |
| 122 | + getColumns: getColumns |
89 | 123 | };
|
0 commit comments