Closed stevenk99 closed 3 years ago
Did you find a solution for your question? I am facing the same challenge
NVolz In the end I spent a lot of time preparing all the data from mongoDB to create a single master data store, before feeding it into 11ty. This store consisted of, say, a dozen main object arrays (Seasons, Divisions, Matches, Players etc) with helper/pointer arrays into these, for example, 'DivisionMatches' (with better DB practise, some of these could have been in mongoDB, but this can make the admin side more detailed because of the mongoDB (nosql) requirement to update an inter-table relationship in more than one table. If one knows the end result is only for SSG, it can make sense to keep it simpler in mongoDB). I had to keep on top of js object array principles and functions to avoid assignments that replicated data (increasing memory and duplication of data) and stick to reference, preferable, because the templates are then pretty much ready to go for handling pagination and filtering. Then within the templates, if a helper wasn't available, I always had access to all data, 'alldata.Seasons' for example, for filtering or sort. I suppose a good principle is that your templates read very simply and cleanly, indicating clearly your entity structure. These are familiar in your mind (and documented or on your wall) with the right mapping ready to go. You do sometimes need filtering, so I wrote some generic 11ty filters in /lib/filters, 'filterarray' for example). I've not bothered with subdir data files or any of that, just a few filters used occasionally. Its working well, I use a low cost node provider for the admin side, photos on s3, then netlify runs through the 11ty app data. One league has seasons back to 2008, about 3 divisions/season and 6 teams/division playing home and away, with variable and complicated scoring systems. Netlify crunches out pages for each season, team, player stats tables, in 90s or so. During the covid years, I moved away from monolith (sql server, .net, 'live' dedicated servers) to node, js, mongoDB and I'm so glad I did, as it has given a fool-proof, rock solid, very low cost, secure system that works fantastically for clients who require data/informational websites. It took a long time though.
Can I ask anyone's feedback of referencing eleventy global data in one JavaScript data file from another. By way of a much simplified made-up example, I've used two global data files, to represent football divisions and teams:
_data/divisions.js - nosql array of all divisions
_data/teams.js - nosql array of all teams
Now I understand it is possible to 'cross-reference' global data in a layout, for example, using a filter, say, to pick out the relevant team's division:
filters/findinarray.js
.elevently.js
teams.liquid - eg "Liverpool play in the Premiership"
but something similar in the data file does not work, throwing a reference error "divisions is not defined":
_data/teams.js
This is likely obvious to you why it fails, but I'm not sure. Maybe it is possible somehow, with a require or something. The filter itself is not important, its the access to the other global data I want to understand.
So to get round this, we can throw the data 'sets' into a node module to inter-relate the data:
mymodules/football.js
_data/divisions.js -
_data/teams.js
Instead of modules, could we place all data into one global data file that every layout refers to, which might mean fewer calls in a pagination build perhaps?:
_data/footballdata.js
What method(s) would you recommend to inter-link global data, principally for more complex models than this example? For example, is the api called unnecessarily and repeatedly when building multiple html files using pagination? I wonder if there is some caching solution. Any thoughts, alternatives, recommendations or feedback would be appreciated.