1 | 'use strict';
|
2 |
|
3 | const Promise = require('bluebird');
|
4 | const types = require('punchcard-content-types');
|
5 | const config = require('config');
|
6 |
|
7 | const tables = require('./database/tables');
|
8 | const schemas = require('./database/schemas');
|
9 | const utils = require('./utils');
|
10 |
|
11 | const knex = require('knex');
|
12 |
|
13 | const database = knex(config.knex);
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const init = () => {
|
21 | return types(null, config).then(cts => {
|
22 |
|
23 | const contentSchema = utils.singleItem('name', 'content', schemas).fields;
|
24 |
|
25 |
|
26 | const schemata = schemas.filter(schema => {
|
27 | if (schema.name === 'content') {
|
28 | return false;
|
29 | }
|
30 |
|
31 | return true;
|
32 | }).concat(cts.map(type => {
|
33 |
|
34 | return {
|
35 | name: `content-type--${type.id}`,
|
36 | fields: contentSchema,
|
37 | };
|
38 | }));
|
39 |
|
40 |
|
41 | return Promise.map(schemata, schema => {
|
42 | return tables.create(database, schema);
|
43 | });
|
44 | }).then(() => {
|
45 |
|
46 | return database;
|
47 | }).catch(e => {
|
48 | throw new Error(e);
|
49 | });
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | module.exports = database;
|
57 | module.exports.init = init;
|