From 83eadf0929a2506541e34b4a6f73d46bfa2404d3 Mon Sep 17 00:00:00 2001 From: Joey Baker Date: Wed, 26 Oct 2016 10:57:54 -0700 Subject: [PATCH] Fix: don't build CRUD routes until ready Previously, we were building the crud routes before we had run through the association logic. This meant that routes could get created without a complete list of associations available to it. This is slightly less performant b/c we need to run through two loops, but ensures that the full association data is available to all routes. --- src/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 48ddb4c..709a747 100644 --- a/src/index.js +++ b/src/index.js @@ -36,7 +36,6 @@ const register = (server, options = {}, next) => { // Join tables if (model.options.name.singular !== model.name) continue; - crud(server, model, options); for (const key of Object.keys(model.associations)) { const association = model.associations[key]; @@ -92,6 +91,13 @@ const register = (server, options = {}, next) => { } } + // build the methods for each model now that we've defined all the + // associations + Object.keys(models).forEach((modelName) => { + const model = models[modelName]; + crud(server, model, options); + }); + next(); };