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

Transaction ExecPostHooks missing entityData #115

Closed
aroraenterprise opened this issue Jul 26, 2018 · 7 comments
Closed

Transaction ExecPostHooks missing entityData #115

aroraenterprise opened this issue Jul 26, 2018 · 7 comments

Comments

@aroraenterprise
Copy link

When doing a transaction and after calling execPostHooks on the transaction my post save functions do not have 'this' set to the current entity. Which means I am unable to work with the entity in my post save hooks for example when I am using transactions.

@sebelga
Copy link
Owner

sebelga commented Jul 27, 2018

Hello,
Could you share your Schema with the post hooks + a small example of your transaction so I can reproduce it?
thanks!

@aroraenterprise
Copy link
Author

const eventSchema = new Schema({
  organizerId: { type: String, required: true },
  created: { type: Number, write: false },
  modified: { type: Number, write: false },
  version: { type: String, default: process.env.AppVersion, write: false },
  creatorId: { type: String, required: true, write: false },
  // additional props
 ...
}, { explicitOnly: true, keyType: 'id' });

async function setCreatorPermissions() {
  console.log('here i do some stuff with permissions...');
  return new Promise.resolve();
}

function indexEvent() {
  const entity = datastoreUtility.fromDatastore(this.entityData);
  return new Promise((resolve) => {
    algoliaClient.update(
      `${eventKind}-${entity.id}`,
      entity,
      () => {
        resolve();
      },
    );
  });
}

eventSchema.post('save', [
  setCreatorPermissions,
  indexEvent,
]);

module.exports = gstore.model(eventKind, eventSchema);

Transaction Example

const transaction = gstore.transaction();
  await transaction.run();

  try {
  const event = await Event.get(eventId);
    const itemData = Event.sanitize(cartData);
    const item = new Event(
      itemData
    );
    await item.save(transaction);
    await transaction.commit();
    await transaction.execPostHooks(); // this doesn't seem to execute the post save hooks...
    if (event[gstore.ERR_HOOKS]) {
      return {
        success: false,
        ...event[gstore.ERR_HOOKS][0],
      };
    }
    return {
      success: true,
      output: event.plain()
    };
  } catch (error) {
    return {
      success: false,
      error,
    };
  }

@sebelga
Copy link
Owner

sebelga commented Jul 30, 2018

Hi,
Thanks for reporting, the scope on the entity was missing indeed in the execPostHooks() call.
I just made a release with a fix (v4.2.4), let me know if it solves the issue.

A few comments on your code example:

const eventSchema = new Schema({
  organizerId: { type: String, required: true },
  created: { type: Number, write: false },
  modified: { type: Number, write: false },
  version: { type: String, default: process.env.AppVersion, write: false },
  creatorId: { type: String, required: true, write: false },
  // additional props
 ...
});

// You don't need to set { explicitOnly: true, keyType: 'id' }, it is the default

async function setCreatorPermissions() {
  console.log('here i do some stuff with permissions...');
  return new Promise.resolve(); // remove the "new"
}

...

const transaction = gstore.transaction();
await transaction.run();

try {
  const event = await Event.get(eventId); // why this line?
    const itemData = Event.sanitize(cartData);
    const item = new Event(
      itemData
    );
    const event = await item.save(transaction); // Set the "event" here not on the "get"
    await transaction.commit();
    await transaction.execPostHooks();
    
    if (event[gstore.ERR_HOOKS]) {
      return {
        success: false,
        ...event[gstore.ERR_HOOKS][0],
      };
    }
    ....
  }

@aroraenterprise
Copy link
Author

Okay will try in a day or so and report back! Thank you for taking the time to read and comment over the code!

@sebelga
Copy link
Owner

sebelga commented Aug 1, 2018

Great, happy to help. Let me know how it goes!

@aroraenterprise
Copy link
Author

It worked! Thank you so much.

@sebelga
Copy link
Owner

sebelga commented Aug 17, 2018

Great, thanks for the feedback! 👍

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

2 participants