-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.11 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
"use strict";
const fastifyPlugin = require("fastify-plugin");
const Sequelize = require("sequelize-cockroachdb");
async function cockroachConnector(fastify, options) {
// CockroachDB is a Postgre SQL Database so override custom dialect
options.settings ? (options.settings.dialect = "postgres") : undefined;
const sequelize = new Sequelize(
options.database,
options.user,
options.password ? options.password : "",
options.settings
? options.settings
: {
dialect: "postgres",
port: options.port ? options.port : 26257,
logging: false
}
);
// Ensure details are correct
await sequelize.sync();
const decorator = {
instance: sequelize
};
if (options.models.length !== 0) {
decorator.models = {};
options.models.forEach(model => {
decorator.models[
model.alias
? model.alias
: `${model.name[0].toUpperCase()}${model.name.slice(1)}`
] = sequelize.define(model.name, model.schema);
});
}
fastify.decorate("cockroachdb", decorator);
}
module.exports = fastifyPlugin(cockroachConnector);