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

Virtual attribute breakage with Objection 1.6.4+ #45

Closed
Aerlin opened this issue Mar 30, 2019 · 6 comments
Closed

Virtual attribute breakage with Objection 1.6.4+ #45

Aerlin opened this issue Mar 30, 2019 · 6 comments

Comments

@Aerlin
Copy link

Aerlin commented Mar 30, 2019

Virtual attributes aren't being added to results after updating Objection to 1.6.4+. I think this issue is related.

For example, trying to stringify Objection's results won't include virtual attributes.

Comment.query().then(function(results) {
	console.log("comments " + JSON.stringify( results , null, "\t"));
})

I think Feathers-Objection is affected by this. Unsure if it's something F-O can fix or if it needs to be fixed on Objection's end. But reverting to objection@1.6.3 works for now.

@dekelev
Copy link
Member

dekelev commented Apr 1, 2019

@Aerlin Did you try with Objection.js latest version 1.6.6?

@Aerlin
Copy link
Author

Aerlin commented Apr 2, 2019

Yeah. I double checked now and 1.6.6 is affected.

Example of my setup

// models/Comment.js
const { Model, snakeCaseMappers } = require('objection');
const moment = require('moment');
const md = require('markdown-it')({
  html: true,
  linkify: true,
  typographer: true
});
...
module.exports = function(app) {
	class Comment extends Model {
		static get tableName() {
			return 'comments';
		}
		static get virtualAttributes() {
		  return ['created_ago', 'updated_ago', 'html'];
		}
		created_ago() {
		  return this.created_at &&  moment( this.created_at.getTime() ).fromNow(true);
		}

		updated_ago() {
		  return this.updated_at && moment( this.updated_at.getTime() ).fromNow(true);
		}

		html() {
		 // console.log("Comment html()");
		  return this.body && md.render(this.body);
		}
		...
	}
	return Comment;
}
// Elsewhere.
const Comment = require('../models/Comment.js')(app);
var builder = Comment.query().then(function(results) {
	console.log("comments " + JSON.stringify( results , null, "\t"));
}).catch(function(err) {
	console.trace(err);
});

In Objection@1.6.3, the query results contain the created_ago, updated_ago and html fields. In 1.6.4, 1.6.5 & 1.6.6 the fields are missing. I know this isn't Feathers-Objection's fault. Just wanted to let you know the issue was affecting F-O. Although I'm starting to wonder if I broke something as nobody else seems to be reporting the issue on the Objection issues list. 😕

edit: Version info
Node: 8.11.4
NPM: 6.8.0
Objection: 1.6.6
Feathers-Objection: 3.2.0
Feathers: 3.3.1

edit2: I think this guy's experiencing a similar problem affecting Koa.

@Aerlin
Copy link
Author

Aerlin commented Apr 2, 2019

Objection's Model virtuals parameter used to default to true, now defaults to false.

Vincit/objection.js@5530377#diff-008bf8359e2b2c171c571598395ecfb7L22

Vincit/objection.js@8115f0b#diff-008bf8359e2b2c171c571598395ecfb7R52

If Objection/lib/model/modelToJson.js getVirtuals() is set to default to true, the missing virtual attributes return.

function getVirtuals(opt) {
  if (!opt) {
    return true;
  } else if (Array.isArray(opt.virtuals)) {
    return opt.virtuals;
  } else if (opt.virtuals) {
    return true;
  } else {
    return true; // <--------------
  }
}

I 'm not sure if the change was intended and F-O needs to implicitly requests virtuals somewhere now, or if it's a bug that they can fix.

@koskimas
Copy link

koskimas commented Apr 2, 2019

This is a bug in objection. Sorry about this. I'm releasing 1.6.7 right now with a fix.

@koskimas
Copy link

koskimas commented Apr 2, 2019

@koskimas
Copy link

koskimas commented Apr 2, 2019

objection 1.6.7 is now released.

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

3 participants