Coder Social home page Coder Social logo

abelosorio / sequelize-views-support Goto Github PK

View Code? Open in Web Editor NEW
28.0 4.0 10.0 157 KB

Adds VIEWS support to Sequelize

Home Page: https://www.npmjs.com/package/sequelize-views-support

License: MIT License

JavaScript 100.00%
sequelize views postgresql nodejs

sequelize-views-support's Introduction

Add Views support to Sequelize

NPM version

This package adds support to Views in Sequelize.

NOTE: Currently it's only supported in PostgreSQL.

Motivation

I've been using PostgreSQL and other RDBMS for a long time, and I can't stress enough about how useful is the View mechanism. For that reason (and others that you'd get bored reading them) an ORM without Views support is incomplete.

This package needs a lot of further work, tests, discussion... but for the moment it works as expected and fulfills its purpose. Feel free of discuss changes, ideas, or whatever you think. Thanks.

Read

Install

npm install --save sequelize-views-support

How to use

First, when creating the sequelize instance, you have to do it using this package and not the Sequelize's:

sequelize.js:

const Sequelize = require('sequelize-views-support');

const sequelize = new Sequelize(
  // Use the same construction call you've been using so far
);

module.exports = sequelize;

Then, when creating your view models you have to set two more options (let's call this view Foo):

models/foo.js:

module.exports = (sequelize, DataTypes) => {
  const Foo = sequelize.define('foo', {
    field1: DataTypes.DATE,
    field2: DataTypes.STRING,
    // etc...
  }, {
    treatAsView: true,
    viewDefinition: `
      CREATE VIEW "foo" AS
        -- Put here your view's definition
        -- DO NOT USE "CREATE OR REPLACE", JUST "CREATE"
        -- You can create complex Views using any of the PostgreSQL DSL's supported features
    `
  });

  return Foo;
};

That's it. Now you can sync your models including views. Take into account that views will be created after syncing all your models. This is because your views may depend on models.

Migrations

The next step is to maintain your views. You're going to need to write migrations to change your views' definition, as well as changing it in the viewDefinition option.

You can ignore the viewDefinition option, but you won't be able to sync that view.

Mantainers

sequelize-views-support's People

Contributors

abelosorio avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sequelize-views-support's Issues

upgrade to pg v8 and sequelize v6

"dependencies": {
"pg": "^8.5.1",
"sequelize": "^6.5.0"
},

If I upgrade to the latest version of pg and sequelize, queryInterface becomes camelCase.

 static drop(options) {
    const method = this.options.treatAsView ? 'dropView' : 'dropTable';

    return this.queryInterface[method](
      this.getTableName(options), options
    );
  }
static syncView(options) {
    return this.queryInterface.createView(
      this.getTableName(options),
      this.getViewDefinition(),
      options
    );
  }

View couldn't be created

I couldn't create a view. Below is my code.

Model.js


module.exports = (sequelize, DataTypes) => {
    const VwBackscattering = sequelize.define('vw_backscattering', {
            capture_datetime:  DataTypes.DATE,
            bb_470nm:DataTypes.DOUBLE,
            bb_532nm:DataTypes.DOUBLE,
            bb_650nm: DataTypes.DOUBLE,
            boat_datetime:  DataTypes.DATE,
            rainfall: DataTypes.DOUBLE,
            temperature: DataTypes.DOUBLE,
            latitude: DataTypes.DOUBLE,
            longitude: DataTypes.DOUBLE
        },
        {
            treatAsView: true,
            viewDefinition: `
              CREATE VIEW "vw_backscattering" AS
            'SELECT t1.*, t2.*,
            'ST_SetSRID(ST_MakePoint(t2.longitude, t2.latitude), 4326) as point_geom
            'FROM backscatterings t1 INNER JOIN 
            '    ancillary_data t2 ON t1.capture_datetime = t2.boat_datetime;`
        });
    return VwBackscattering;
};


index.js


const Sequelize = require('sequelize-views-support');
const view = require('./models/VwBackscattering');
const VwBackscattering = view(db, Sequelize.DataTypes)
VwBackscattering.sync().then(() => {
    console.log('VwBackscattering table created');
});
module.exports = VwBackscattering;

DROP VIEW failing

Inside query-interface-views-support.js, on line 4, you have

const sql = DROP VIEW IF EXISTS "${viewName}"${options.cascade && ' CASCADE'};

That creates "DROP VIEW IF EXISTS someViewundefined"

It should be
const sql = DROP VIEW IF EXISTS "${viewName}" ${options.cascade ? 'CASCADE': ''};

"Table 'myView' already exists" on soft sync

The situation is that i have a already defined schema so I cannot do force sync. On normal table the soft sync does work. You even can add a view on a already existent schema. But when trying to do a soft sync when you have already, the error is thrown: Table 'myView' already exists.

SequelizeDatabaseError: relation "my_view" does not exist

View creation works as expected but when I try to issue a model.findAll on the view I get a UnhandledPromiseRejectionWarning: SequelizeDatabaseError: relation "my_view" does not exist.

Am I using the package in a way that was not intended?

Thanks!

Use latest version of Sequelize

When I installed this package, I got this dependency tree:

However in the package.json of this package sequelize@^6.0.0 is in the dependencies. And Sequelize 6 is not even out yet. Can someone explain this discrepancy?

I am currently using Sequelize 5 in my project. Is this package only compatible with Sequelize 4?

[email protected] support

Is there any reason why you are using the pg@7 package. That package has known issues with Sequelize. Would you be willing to set your package.json file to ^6.1.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.