herteby:denormalize

v0.6.6Published 7 months ago

Denormalize

Simple denormalization for Meteor

Introduction

meteor add herteby:denormalize

In this readme, parent always refers to the documents in which the cache is stored, while child refers to the documents that will be cached.

Example: You have two collections - Users and Roles. The Users store the _id of any Roles they have been assigned. If you want each User to cache information from any Roles that are assigned to it, the Users would be the parents and the Roles would be the children, and it would be either a one or many relationship, depending on if a User can have multiple Roles. If you wanted each Role to store a list of all Users which have that role, the Roles would be the parents and the Users would be the children, and it would be an inverse or many-inverse relationship.

Collection.cache(options)

1Posts.cache({
2  type:'one',
3  collection:Meteor.users,
4  fields:['username', 'profile.firstName', 'profile.lastName'],
5  referenceField:'author_id',
6  cacheField:'author'
7})

Notes and clarification:

  • "one" and "inverse" are many-to-one relationships (with "one", a parent can only have one child, but many parents could have the same child). "many" and "many-inverse" are many-to-many relationships
  • When cacheField is an array (all types except "one"), the order of the children is not guaranteed.
  • When referenceField is an array, if it contains duplicate _ids, they will be ignored. The cacheField will always contain unique children.

Collection.cacheCount(options)

1TodoLists.cacheCount({
2  collection:Todos,
3  referenceField:'list_id',
4  cacheField:'counts.important',
5  selector:{done:null, priority:{$lt:3}}
6})

cacheCount() can be used on "inverse" and "many-inverse" relationships

Collection.cacheField(options)

1Meteor.users.cacheField({
2  fields:['profile.firstName', 'profile.lastName'],
3  cacheField:'fullname',
4  transform(doc){
5    return doc.profile.firstName + ' ' + doc.profile.lastName
6  }
7})
8

Note: The transform function could also fetch data from other collections or through HTTP if you wanted, as long as it's done synchronously.

Migration

If you decide to add a new cache or change the cache options on a collection that already contains documents, those documents need to be updated. There are two options for this:

migrate(collectionName, cacheField, [selector])

1import {migrate} from 'meteor/herteby:denormalize'
2migrate('users', 'fullName')
3migrate('users', 'fullAddress', {fullAddress:{$exists:false}})

This updates the specified cacheField for all documents in the collection, or all documents matching the selector. Selector can also be an _id.

autoMigrate()

1import {autoMigrate} from 'meteor/herteby:denormalize'
2autoMigrate() //should be called last in your server code, after all caches have been declared

When autoMigrate() is called, it checks all the caches you have declared against a collection (called _cacheMigrations in the DB) to see wether they need to be migrated. If any do, it will run a migration on them, and then save the options to _cacheMigrations, so that it won't run again unless you change any of the options. If you later for example decide to add another field to a cache, it will rerun automatically.

One thing it does not do is remove the old cacheField, if you were to change the name or remove the cache. That part you have to do yourself.

Note: it does not check the documents, it just checks each cache declaration, so it won't thrash your DB on server start going through millions of records (unless something needs to be updated).

Nested referenceFields

For "one" and "inverse", nested referenceFields are simply declared like referenceField:'nested.reference.field'

For "many" and "many-inverse", if the referenceField is an Array containing objects, a colon is used to show where the Array starts.

Example:

If the parent doc looks like this:

1{
2  //...
3  references:{
4    users:[{_id:'user1'}, {_id:'user2'}]
5  }
6}

The referenceField string should be 'references.users:_id'

Recursive caching

You can use the output (the cacheField) of one cache function as one of the fields to be cached by another cache function, or even as the referenceField. They will all be updated correctly. This way you can create "chains" connecting three or more collections.

In the examples below, all cache fields start with _, which may be a good convention to follow for all your caches.

Use cacheField() to cache the sum of all cached items from a purchase

1Bills.cacheField({
2  fields:['_items'],
3  cacheField:'_sum',
4  transform(doc){
5    return _.sum(_.map(doc._items, 'price'))
6  }
7})

Caching the cacheFields of another cache

1Bills.cache({
2  cacheField:'_items',
3  collection:Items,
4  type:'many',
5  referenceField:'item_ids',
6  fields:['name', 'price']
7})
8Customers.cache({
9  cacheField:'_bills',
10  collection:Bills,
11  type:'inverse',
12  referenceField:'customer_id',
13  fields:['_sum', '_items']
14})

Using the cacheField of another cache as referenceField

1Customers.cache({
2  cacheField:'_bills2',
3  collection:Bills,
4  type:'inverse',
5  referenceField:'customer_id',
6  fields:['item_ids', '_sum']
7})
8Customers.cache({
9  cacheField:'_items',
10  collection:Items,
11  type:'many',
12  referenceField:'_bills2:item_ids',
13  fields:['name', 'price']
14})

Incestuous relationships

With this fun title I'm simply referring to caches where the parent and child collections are the same.

1Meteor.users.cache({
2  cacheField:'_friends',
3  collection:Meteor.users,
4  type:'many',
5  referenceField:'friend_ids',
6  fields:['name', 'profile.avatar']
7})

This works fine, but there is one thing you can not do - cache the cacheField of a document in the same collection - in this example it would be caching the friends of a users friends. This would lead to an infinite loop and infinitely growing caches.

When are the caches updated?

The caches for cache() and cacheCount() are updated immediately and synchronously.

1Posts.cache({
2  cacheField:'_author',
3  //...
4})
5Posts.insert({_id:'post1', author_id:'user1'})
6Posts.findOne('post1')._author //will contain the cached user

cache() uses 5 hooks: parent.after.insert, parent.after.update, child.after.insert, child.after.update and child.after.remove. There are then checks done to make sure it doesn't do unnecessary updates.

Basically you should always be able to rely on the caches being updated. If they're not, that should be considered a bug.

However, to avoid a complicated issue with "recursive caching", the update of cacheField() is always deferred.

1Meteor.users.cacheField({
2  fields:['address', 'postalCode', 'city'],
3  cacheField:'_fullAddress',
4})
5Meteor.users.insert({_id:'user1', ...})
6Meteor.users.findOne('user1')._fullAddress //will not contain the cached address yet
7Meteor.setTimeout(()=>{
8  Meteor.users.findOne('user1')._fullAddress //now it should be there
9}, 50)

Note: Since this package relies on collection-hooks, it won't detect any updates you do to the DB outside of Meteor. To solve that, you can call the migrate() function afterwards.

Testing the package

meteor test-packages packages/denormalize --driver-package=practicalmeteor:mocha

(Then open localhost:3000 in your browser) The package currently has over 120 tests Note: The "slowness warnings" in the results are just due to the asynchronous tests