-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
235 lines (216 loc) · 6.23 KB
/
index.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
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
232
233
234
235
const { cwd, log } = henri;
const path = require('path');
const fs = require('fs');
const { mergeTypes, mergeResolvers } = require('merge-graphql-schemas');
const { makeExecutableSchema } = require('graphql-tools');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { runQuery } = require('apollo-server-core');
function load(location) {
const includeAll = require('include-all');
return new Promise((resolve, reject) => {
includeAll.optional(
{
dirname: path.resolve(location),
filter: /(.+)\.js$/,
excludeDirs: /^\.(git|svn)$/,
flatten: true,
force: true,
},
(err, modules) => {
if (err) {
return reject(err);
}
log.info('models loaded from disk');
return resolve(modules);
}
);
});
}
async function configure(models) {
const { config } = henri;
const user = config.has('user') ? config.get('user').toLowerCase() : 'user';
delete henri.stores;
delete henri._user;
henri.stores = {};
const configuration = {
adapters: {},
models: {},
};
for (const id of Object.keys(models)) {
const model = models[id];
checkStoreOrDie(model);
const storeName = model.store || 'default';
const store = await getStore(storeName);
global[model.globalId] = store.addModel(model, user);
henri._models.push(model.globalId);
configuration.adapters[storeName] = store;
extractGraph(model);
}
mergeGraph();
return configuration;
}
function checkStoreOrDie(model) {
const { config } = henri;
if (!model.store && !config.has('stores.default')) {
return log.fatalError(
`There is no default store and ${model.identity} is missing one`
);
}
if (model.store && !config.has(`stores.${model.store}`)) {
return log.fatalError(
`It seems like ${model.store} is not configured. ${
model.identity
} is using it.`
);
}
}
async function getStore(name) {
const { config } = henri;
if (henri.stores[name]) {
return henri.stores[name];
}
const store = config.get(`stores.${name}`);
const valid = {
mongoose: 'mongoose',
disk: 'disk',
mysql: 'mysql',
mariadb: 'mysql',
postgresql: 'postgresql',
mssql: 'mssql',
};
if (typeof valid[store.adapter] === 'undefined') {
return log.fatalError(
`Adapter '${store.adapter}' is not valid. Check your configuration file.`
);
}
const conn = valid[store.adapter];
const Pkg = requirePackage(store, conn);
henri.stores[name] = new Pkg(name, store);
return henri.stores[name];
}
function requirePackage(store, conn) {
try {
const Pkg = henri.isTest
? require(`@usehenri/${conn}`)
: require(path.resolve(cwd, 'node_modules', `@usehenri/${conn}`));
return Pkg;
} catch (e) {
return log.fatalError(`
Unable to load database adapter '${store.adapter}'. Seems like you
should install it using: npm install @usehenri/${store.adapter}`);
}
}
function extractGraph(model) {
if (typeof model.graphql === 'undefined') {
return;
}
const { types = null, resolvers = null } = model.graphql;
if (typeof types === 'string') {
henri._graphql.typesList.push(types);
}
if (typeof resolvers === 'object') {
henri._graphql.resolversList.push(resolvers);
}
}
function mergeGraph() {
let should = false;
if (henri._graphql.typesList.length > 0) {
should = true;
henri._graphql.types = mergeTypes(henri._graphql.typesList);
}
if (henri._graphql.resolversList.length > 0) {
should = true;
henri._graphql.resolvers = mergeResolvers(henri._graphql.resolversList);
}
if (should) {
henri._graphql.schema = makeExecutableSchema({
typeDefs: henri._graphql.types,
resolvers: henri._graphql.resolvers,
});
}
}
async function start(configuration) {
return new Promise(async (resolve, reject) => {
for (const store of Object.keys(henri.stores)) {
await henri.stores[store].start();
}
if (henri._models.length > 0) {
// TODO: Add some way of backing up data and restoring if fails
const eslintFile = path.resolve(cwd, '.eslintrc');
try {
const eslintRc = JSON.parse(fs.readFileSync(eslintFile, 'utf8'));
henri._models.map(modelName => (eslintRc.globals[modelName] = true));
fs.writeFileSync(eslintFile, JSON.stringify(eslintRc, null, 2));
} catch (e) {} // Do nothing
}
return resolve();
});
}
async function stop() {
return new Promise(async (resolve, reject) => {
if (henri.stores.length < 1) {
log.warn('no models/stores needed to be stopped.');
return resolve();
}
for (const store of Object.keys(henri.stores)) {
await henri.stores[store].stop();
}
henri._models.forEach(name => delete global[name]);
log.warn('models (orm) gracefully stopped');
return resolve();
});
}
async function init() {
const { config } = henri;
henri._graphql = {
active: false,
endpoint: (config.has('graphql') && config.get('graphql')) || '/_henri/gql',
resolvers: null,
resolversList: [],
schema: null,
typesList: [],
types: null,
register: () => {
henri._graphql.active = true;
henri.router.use(
henri._graphql.endpoint,
graphqlExpress({ schema: henri._graphql.schema })
);
if (!henri.isProduction) {
log.info('starting graphiql interface');
henri.router.use(
'/_henri/graphiql',
graphiqlExpress({ endpointURL: henri._graphql.endpoint })
);
}
},
};
henri.graphql = async (query = `{ No query }`, context = {}) => {
if (henri._graphql && !henri._graphql.schema) {
return 'No graphql schema found.';
}
const { schema } = henri._graphql;
return runQuery({ schema, query, context });
};
await start(
await configure(
await load(
config.has('location.models')
? path.resolve(config.get('location.models'))
: './app/models'
)
)
);
}
async function reload() {
await stop();
delete henri._graphql;
henri._models.forEach(name => delete global[name]);
henri._models = [];
await init();
log.warn('models are reloaded');
}
henri.addLoader(reload);
henri.addUnloader(stop);
module.exports = init();
log.info('model module loaded.');