masumsoft / express-cassandra

Cassandra ORM/ODM/OGM for NodeJS with support for Apache Cassandra, ScyllaDB, Datastax Enterprise, Elassandra & JanusGraph.
http://express-cassandra.readthedocs.io
GNU Lesser General Public License v3.0
227 stars 67 forks source link

Require'ing a file in a Model definition file causes whole thing to be unable to connect #215

Closed keyhole425 closed 4 years ago

keyhole425 commented 4 years ago

I am trying to import a trigger to use as a hook function. Calling it in with a regular Node require statement makes THE ENTIRE MODULE FAIL TO CONNECT.

Seems like a HUGE problem and seriously the two should not affect each other at all from my understanding.

Here's sample code:

const trigger = require('../../triggers/customer_trig_au');

module.exports = {
  fields:{ // code omitted}
}

Commenting out the trigger call makes it all work, keeping it makes it unable to connect. Doesn't matter what file I call (eg files that are successfully imported and used elsewhere produce the same error here).

keyhole425 commented 4 years ago

Actually from testing it looks like I cannot import any file at all (node core modules included) which kind of makes the hooks pointless unless you can achieve what you need with vanilla JS.

Am I missing something core that's screwing everything up or is this actually a limitation of this?

masumsoft commented 4 years ago

Importing modules from model schema definition files should work fine unless they create circular reference in some way. Importing validator modules and other libraries from model definition files is a common use case with express-cassandra.

keyhole425 commented 4 years ago

@masumsoft When you say "Importing modules from model schema definition files" do you mean like I am doing in the code snippet above?

I can bind the Model files correctly and everything else works perfectly as expected (all credit to you for such a fine module), but whenever I attempt to import a file into the Model definition file, it doesn't error out (passes linting etc) but it refuses to connect to the database (remote connection). Commenting out that import and it all works straight away.

masumsoft commented 4 years ago

Let's see a basic example so we both get a clear picture about the situation:

File: models/PersonModel.js

const validator = require('validator');

module.exports = {
    fields:{
        email   : "text",
        name    : "text",
        surname : "text",
        age     : "int",
        created : "timestamp"
    },
    key:["name"],
    before_save: function (instance, options) {
        if (validator.isEmail(instance.email)) {
            return true;
        }
        return false;
    },
}

File: index.js

var models = require('express-cassandra');

models.setDirectory( __dirname + '/models').bind(
    {
        clientOptions: {
            contactPoints: ['127.0.0.1'],
            protocolOptions: { port: 9042 },
            keyspace: 'mykeyspace',
            queryOptions: {consistency: models.consistencies.one}
        },
        ormOptions: {
            defaultReplicationStrategy : {
                class: 'SimpleStrategy',
                replication_factor: 1
            },
            migration: 'alter'
        }
    },
    function(err) {
        if(err) throw err;

        var john = new models.instance.Person({
            email: "john.doe@example.com",
            name: "John",
            surname: "Doe",
            age: 32,
            created: Date.now()
        });
        john.save(function(err){
            if(err) {
                console.log(err);
                models.close();
                return;
            }
            console.log('Yuppiie!');
            models.close();
        });
    }
);

The above setup works just fine for me. So I thought maybe there is some sort of circular reference in your codebase. It's difficult to guess without more information or code example where you could re-produce the issue you are facing.

keyhole425 commented 4 years ago

Hi @masumsoft,

Apologies for the tone above, I think I was overly tired or frustrated at the time. You are correct, it was a circular reference problem, thanks for pointing it out in a clear manner.

Thoroughly enjoy the library, thanks!