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

virtuals not working #102

Closed
ghost opened this issue Apr 14, 2018 · 3 comments
Closed

virtuals not working #102

ghost opened this issue Apr 14, 2018 · 3 comments

Comments

@ghost
Copy link

ghost commented Apr 14, 2018

Simplest working example

"use strict";

const yourProjectID = '????';

const Datastore = require('@google-cloud/datastore');
const gstore = require('gstore-node')();

const datastore = new Datastore({
    projectId: yourProjectID
});


gstore.connect(datastore);

const thingSchema = new gstore.Schema({});

thingSchema.virtual('numbers').get(function fullName() {
    return 12345;
});

const thingModel = gstore.model('Thing', thingSchema);

const aThing = new thingModel({}, 1);

console.log(aThing.numbers); // expect: 12345

// try saving and retrieving it

(async function go() {

    await aThing.save();

    const retrievedThing = await thingModel.get(1);

    console.log(retrievedThing.numbers); // expect: 12345

})();

I get undefined for both logs.

@sebelga
Copy link
Owner

sebelga commented Apr 15, 2018

Hello,
For now, the implementation to access the virtual properties is through the "entity.plain()" method (https://sebelga.gitbooks.io/gstore-node/content/entity/methods/plain.html).
This is when the virtual properties are attached to the entityData.

So it would be

const retrievedThing = await thingModel.get(1);
console.log(retrievedThing.plain({ virtuals: true }).numbers); 

I am going to look into the possibility to directy access them from then entity instance.

@ghost
Copy link
Author

ghost commented Apr 16, 2018

I see. I was not expecting that as docs state: ''Virtuals are properties that are added to the entities at runtime that are not persisted in the Datastore."

But thanks for replying.

sebelga added a commit that referenced this issue May 7, 2018
@sebelga
Copy link
Owner

sebelga commented May 8, 2018

Hello,
I just made release v4.2.0 that has support to directly access virtuals from the entity instance.
Let me know if it works for you 👍

@sebelga sebelga closed this as completed May 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant