Rock U - Data - Persisted Datasets
Transcribed Video Content
A persistent data set is, well, a set of data that's persisted. But what does that mean? It means that what you'll do is you'll write a script, that can pull the data that you're after. And that script and the data that it generates is saved in the system and is only refreshed on a certain interval. So, if you've worked with persistent data views, it's a similar concept.
But, what this really enables you to do is to have that script or query written and saved. You only have to write it once. The data only exists once in one place in the database, and then it can be pulled from anywhere else in the system, from numerous different pages or blocks. Instead of having to recreate the query or copy and paste things from one place to another, you can just go and get it directly. So, how does it work?
Let's go into the configuration and check it out. Just to get you familiar with the process, we're going to use a very simple, very basic example just to demonstrate the concepts in a relatable way. And we're going to start by going to admin tools and CMS configuration. Under CMS configuration, you'll see a block here for persisted data sets. And, of course, there aren't any right now, but that's okay.
We're going to add one. The first thing that we'll want is a name. And I said, we're going to make this a very simple data set. So, I'm gonna put campus. And we'll need an access key.
This is particularly important because the access key is what will be used to reference the persisted data set in other areas of Rock. And so this is going to be the key that lets you get at this data from elsewhere. So for this access key, I'm just going to put campus again. You might want something a little bit more robust if you're going to have a lot of these because you might have more than one that references campus, but for our purposes, that'll do. And of course, you'll want a description.
And, of course, the build script. Now, the build script is how we create the actual dataset. And in this example, I said, I'm just gonna do a super simple script, maybe even too simple for a persistent data set, but you can still see how it works. And here we've got a simple script. It is Lava, and there's a couple points that you'll want to note.
First, that the entire script is enclosed in brackets, and also that we're using the, to JSON filter here to write this out to JSON because that is how they're stored. And so, this is the the type of format that you'll want to use. Again, this is a very simple script, and there's not a lot of data behind it. So even if you needed to pull campus information, the campus ID and the campus name, Persistent data sets might not be required, but for large, large data sets, they're almost essential for performance. As we scroll down, you can see the enabled Lava commands.
Depending on your build script, you might need to enable certain Lava commands. I'm just gonna click all just for demonstration purposes, make sure everything works. We also have some settings here for the refresh interval, the memory cache duration, and the allow manual refresh that we want to highlight. At a minimum, you must provide a refresh interval to indicate how frequently the data gathered by your build script is going to be refreshed. And so campuses don't change very often, so I could probably make this way longer, but I'm going to say twenty four hours.
You can store the dataset in cache, which makes it even faster to access the data for the specified amount of time. So in here, you'll just put how long it should stay in cache. And, you can optionally choose to allow the dataset to be refreshed manually by checking this box. And, that'll let a person refresh the data instead of having to wait for the next refresh interval. Now, the entity type is optional.
And when you create a persistent dataset, the data is stored as JSON. And setting an entity type indicates what type of entity the JSON object should be associated with. This entity type is used by the persistent dataset lava filter when entity related options append following, are specified. In other words, the entity type is only needed if you're going to do something with the data that requires knowing the type of entity being worked with. If you're just displaying the data on a page, which is what we're going to do, then this isn't needed.
So I'm just going to leave it blank. At a minimum, that's all we need. I said, we just have our basic script here that's gonna pull the ID and name, so I'm gonna go ahead and save that. And when I do, I can see it on the list here. And because we enabled a manual refresh, we can refresh this data, and it'll tell you when it was last refreshed and how long it took.
And with the data refreshed, I can preview. And here's our script output. Now, again, this is in JSON, so it looks this way. But we've got our number one main campus, which is our main campus's ID. Again, this isn't a super complex query, but just for demonstration purposes.
So, now we've got our data out there. So, what do we do with it? In this example, we're going to pull it into an HTML block. So, let's take a look at what that looks . So, here I've got an HTML block, just on a random page.
And we've got a little bit of markup here, but the main point that we want to draw attention to is this first line here, that we're going to assign data equal to campus. Now, campus is that access key that we set up on the persisted data set. And I said, that uses that gets used by the persisted data set filter. So which persisted dataset do we want? The one with the access key of campus.
And then from there, we can do a, , four item in data and a little bit of minor HTML to pull in the merge fields. Again, ID and name was all that we got. And just that, you can display the data on the page, and it doesn't have to re query it every time the page loads to go get it. It can just go out to the persistent data set and pull it directly. So, of course, we've only scratched the surface of what you can do with persistent data sets.
But for more information, check out our community guide, the Designing and Building Websites Using Rock guide, posted online. And thanks for watching.