Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models won't sync! #3

Open
craftsman-expert opened this issue Feb 2, 2022 · 0 comments
Open

Models won't sync! #3

craftsman-expert opened this issue Feb 2, 2022 · 0 comments

Comments

@craftsman-expert
Copy link

"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(); // << Models won't 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant