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

Fixed the fieldObject.type reference with fieldtype in validate_field(). #183

Closed AlexisWilke closed 5 years ago

AlexisWilke commented 5 years ago

I decided to try to create a Pull request, So I'll close #182

I'm starting with express-cassandra and I got the following error

(node:13187) UnhandledPromiseRejectionWarning: apollo.model.validator.invalidschema: Invalid field type "undefined" for field: content_id

Looking at the code I see that the type can either be a field or a string;

get_field_type(modelSchema, fieldName) {
  var fieldObject = modelSchema.fields[fieldName];

  if (typeof fieldObject === 'string') {
    return fieldObject;
  }
  if (_.isPlainObject(fieldObject)) {
    return fieldObject.type;
  }
  throw new Error('Field type not defined properly');
},

However, in the code generating the error:

var fieldtype = this.get_field_type(modelSchema, fieldName);
if (!_.has(datatypes, fieldtype)) {
  throw new Error(util.format('Invalid field type "%s" for field: %s', fieldObject.type, fieldName));
}

I suggest changing the fieldObject.type with fieldtype as follow:

  throw new Error(util.format('Invalid field type "%s" for field: %s', fieldtype, fieldName));

Then I can see that it's not happen with 'long' as a field type:

(node:13187) UnhandledPromiseRejectionWarning: apollo.model.validator.invalidschema: Invalid field type "long" for field: content_id

Which is strange, but at least makes a lot more sense.

masumsoft commented 5 years ago

@AlexisWilke Could you please share your schema or atleast the type definition of content_id? I guess that would help me sort out the issue properly.

AlexisWilke commented 5 years ago

The following ought to do it;

 module.exports = {
     fields: {
        content_id: 'long'
     },
     key: ['content_id']
}

The problem is when you use an invalid type. Reading various documentations, I though express-cassandra would also make use of non CQL types where a bigint would be represented by long. My mistake, but the fact is that as a side effect I found this bug.

There is also the word "default" which is misspelled in the same file.

'Invalid defult value for field: %s(%s)'

Currently on line 48. (the 'a' is missing)

masumsoft commented 5 years ago

Okay, nice catch @AlexisWilke, I'm going to merge it then.

AlexisWilke commented 5 years ago

Note that the word default is not fixed in the patch...

masumsoft commented 5 years ago

Yah I've added another commit for that.