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

How do we specify denormalization? #12

Open
ujwal-setlur opened this issue Aug 16, 2018 · 6 comments
Open

How do we specify denormalization? #12

ujwal-setlur opened this issue Aug 16, 2018 · 6 comments

Comments

@ujwal-setlur
Copy link

Hi,

How do we specify denormalization structure with this package?

Thanks

@ujwal-setlur
Copy link
Author

I tried the following, but it didn't work:

type ColorMessage @mongo(name: "colorMessages") 
{
  _id: ID!
  color: Color @link(field: "colorId")
  sender: User @link(field: "senderId")
  date: Date!
  recipients: [User] @link(field: "recipientIds")
}

type Color @mongo(name: "colors")
{
  _id: ID!
  name: String
  description: String
  notes: String
  date: Date @map(to: "createdAt")
  rgb: [Int]
  spectrum: [SpectrumPoint]
  owner: User @link(field: "createdBy")
  messages: [ColorMessage] @link(to: "color", autoremove: true, 
    denormalize: {field: "messagesCache", body: {senderId: 1, date: 1, recipientIds: 1}})
}

@ujwal-setlur
Copy link
Author

The autoremove is honored, but denormalize is not. Looking at the code in LinkDirective.js, I believe it should be:

    if (args.to) {
      config = Object.assign({}, args);
      config.inversedBy = args.to;
      delete config.to;
    }
thisCollection.addLinks({
      [field.name]: {
        collection: referencedCollection,
        ...config,
      },
    });

If I use direct addLinks API, it works:

Colors.addLinks({
  "messages": {
    collection: ColorMessages,
    inversedBy: "color",
    autoremove: true,
    denormalize: {
      field: "messagesCache",
      body: {
        senderId: 1,
        date: 1,
        recipientIds: 1
      }
    }
  }
});

@ujwal-setlur
Copy link
Author

Well, this explains it :-)

directiveDefinitions.js:

  directive @link(
    field: String
    to: String
    metadata: Boolean
    unique: Boolean
    autoremove: Boolean
  ) on FIELD_DEFINITION

Any chances of adding support for denormalize?

@ujwal-setlur
Copy link
Author

Aargh! I just saw this at the bottom of the README:

This is a very quick way to setup your schema, however if you need to use denormalisation abilities, and you don't want to give up the sugary directives above:

import { db } from 'meteor/cultofcoders:grapher';

Meteor.startup(() => {
  const userCommentLinker = db.users.getLinker('comments');
  Object.assign(userCommentLinker.linkConfig, {
    denormalize: {},
  });
  userCommentLinker._initDenormalization();
});

Duh, I will try this...

@ujwal-setlur
Copy link
Author

Hmm, when I try this:

Meteor.startup(() => {
  // initialize Apollo server
  initialize({}, {
    gui: Meteor.isDevelopment
  });
  // set up denormalization stuff
  const colorMessagesLinker = db.colors.getLinker("messages");
  Object.assign(colorMessagesLinker.linkConfig, {
    denormalize: {
      field: "messagesCache",
      body: {
        senderId: 1,
        date: 1,
        recipientIds: 1
      }
    }
  });
  colorMessagesLinker._initDenormalization();

});

I get this startup error:

W20180823-13:00:22.405(5.5)? (STDERR) /Users/ujwal/.meteor/packages/meteor-tool/.1.7.0_5.rjf80g.ow62r++os.osx.x86_64+web.browser+web.browser.legacy+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
W20180823-13:00:22.405(5.5)? (STDERR) 						throw(ex);
W20180823-13:00:22.406(5.5)? (STDERR) 						^
W20180823-13:00:22.406(5.5)? (STDERR)
W20180823-13:00:22.406(5.5)? (STDERR) TypeError: Cannot read property 'isMeta' of undefined
W20180823-13:00:22.406(5.5)? (STDERR)     at Linker.isMeta (packages/cultofcoders:grapher/lib/links/linker.js:92:50)
W20180823-13:00:22.406(5.5)? (STDERR)     at Linker._initDenormalization (packages/cultofcoders:grapher/lib/links/linker.js:370:18)
W20180823-13:00:22.406(5.5)? (STDERR)     at Meteor.startup (imports/startup/server/index.js:41:23)
W20180823-13:00:22.406(5.5)? (STDERR)     at Function.time (/Users/ujwal/Work/seamlz/seamlz-server/.meteor/local/build/programs/server/profile.js:309:28)
W20180823-13:00:22.406(5.5)? (STDERR)     at /Users/ujwal/Work/seamlz/seamlz-server/.meteor/local/build/programs/server/boot.js:427:13
W20180823-13:00:22.407(5.5)? (STDERR)     at /Users/ujwal/Work/seamlz/seamlz-server/.meteor/local/build/programs/server/boot.js:472:5
W20180823-13:00:22.407(5.5)? (STDERR)     at Function.run (/Users/ujwal/Work/seamlz/seamlz-server/.meteor/local/build/programs/server/profile.js:510:12)
W20180823-13:00:22.407(5.5)? (STDERR)     at /Users/ujwal/Work/seamlz/seamlz-server/.meteor/local/build/programs/server/boot.js:470:11

@ujwal-setlur
Copy link
Author

OK, this worked:

Meteor.startup(() => {
  // initialize Apollo server
  initialize({}, {
    gui: Meteor.isDevelopment
  });
  // set up denormalization stuff
  const colorMessagesLinker = db.colorMessages.getLinker("color");
  const colorsInverseLinker = db.colors.getLinker("messages");
  colorsInverseLinker._setupVirtualConfig(colorMessagesLinker);
  Object.assign(colorsInverseLinker.linkConfig, {
    denormalize: {
      field: "messagesCache",
      body: {
        senderId: 1,
        date: 1,
        recipientIds: 1
      }
    }
  });
  colorsInverseLinker._initDenormalization();
});

Had to read through linker.js code to figure it out. For denormalization on an inverse link, the direct link has to be explicitly specified first using:

colorsInverseLinker._setupVirtualConfig(colorMessagesLinker);

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

1 participant