Go back to 8base Academy
May 26, 2022

Seeding Databases and Environments using Serverless Task Functions

Sebastian Scholl
@SebScholl

(00:03)
Hey there,
this is Sebastian from 8base.

(00:05)
Today we're going to be looking at how
you can seed your database

(00:08)
or a new workspace environment
with dummy data

(00:11)
using a serverless function,
specifically task functions.

(00:15)
Now I'm going to be showing
this in the context of CICD

(00:18)
for seeding a new
workspace environment.

(00:21)
However, this could work for whether

(00:23)
you only have one environment
in your workspace or multiple,

(00:26)
so it really applies
in multiple ways.

(00:28)
Really at the end of the day,
what it is,

(00:30)
it's a more creative use
of the task function.

(00:34)
Let's get started by
just first looking a little bit

(00:36)
at the workspace
that we have.

(00:38)
Here I have my workspace
SuperTasks Backend,

(00:41)
and inside of here I have
multiple workspace environments:

(00:44)
Master, Staging, Development,

(00:45)
and then this feature
branch called Multitenant.

(00:49)
Now what I'm going to be doing is I'm
going to be going to my command line

(00:52)
to create a new environment
which I will want to develop in.

(00:57)
I can do that by shooting
over to my VS Code.

(01:03)
I'm in my SuperTasks Backend
right here,

(01:06)
and I'm going to run "8base configure"

(01:08)
just to make sure that I'm
connected to the right workspace

(01:11)
as I do jump around sometimes.

(01:13)
SuperTasks Backend, perfect.

(01:16)
Then I'm going to run
"8base environment list".

(01:25)
Cool, and now I'm just going to set
Dev as my current environment.

(01:30)
Awesome.

(01:31)
Now what I'm going to do is I'm going
to go ahead and create my new environment,

(01:35)
so "8base environment branch"
and I'm going to run the help command

(01:42)
just so we can look at some of
the options really quickly.

(01:45)
If we wanted to, any of the data
that's currently in our

(01:51)
Dev environment when branching this
environment, we could copy over.

(01:55)
Now there's reasons why
you might not want to do that,

(01:58)
especially if you're going from a
production environment

(02:00)
to a feature branch,

(02:01)
maybe you don't want to bring
your users production data down.

(02:05)
That's where this way of seeing
your database would become more useful.

(02:11)
What I'm going to say is
I'm going to say "branch",

(02:14)
I'm going to say the name of
the new environment is "newFeature",

(02:26)
and the mode
is going to be "SYSTEM".

(02:31)
By saying "SYSTEM" that means
that we're not copying over any data.

(02:35)
I'm going to run that,
and I will check back in in a minute

(02:38)
once this new environment has created.

(02:41)
Okay, so now with that
new environment created,

(02:44)
what we're going to go ahead and do
is create a new function,

(02:48)
which is going to be a task function.

(02:49)
"8base g", for generate, "task",

(02:53)
and we're going to call
this "seedDummyData"

(02:58)
and syntax is going to be Javascript.

(03:01)
Now just because you author a function

(03:03)
doesn't mean you have to
deploy a function, right?

(03:05)
You can create as
many functions as you want,

(03:08)
which you only ever use for managing
your workspace locally,

(03:11)
or interacting with your API.

(03:13)
It's a great way of essentially
generating a function quickly

(03:17)
and then having an environment in which

(03:18)
you can quickly execute
the scripts you write,

(03:21)
have a pre-authenticated API,
really just a nice working environment.

(03:26)
That's what we're going to leverage
here to be able to essentially

(03:30)
write a script that interacts with our
API to generate some dummy data.

(03:35)
Now we have that task right here.

(03:39)
Once again if I was deploying,

(03:40)
I didn't want it out, I could just
comment it out and I'd be good.

(03:43)
Now I'm going to go into the source
and I'm going to look at my function here.

(03:49)
I'm just going to say
that once it's done,

(03:52)
task is completed,
"Task completed." Period.

(03:57)
Doesn't need to be a string literal,
can just be a normal string, cool.

(04:03)
Now a couple things
that we want to do:

(04:06)
the first thing is that
I have some dummy data

(04:09)
which I'm going to pull
from off screen,

(04:13)
and this is going to be the dummy data
essentially that we send to our API.

(04:18)
Right now, if we wanted to,

(04:20)
we could pull in some
type of cool faker module

(04:23)
that fake generates a bunch of data.

(04:26)
We could write something
that numerically calculates it.

(04:30)
However you want to come up with your
dummy data, you can 100% do that.

(04:33)
However, instead of creating a separate
file that we have to import that file

(04:38)
and read the data,
we can just actually use a mock.

(04:41)
Instead of it being called the request
mock, I'm going to call it "seed.json".

(04:49)
Go in there, delete the headers
and the body, that's not relevant.

(04:53)
We have a data object,
we can call it "data"

(04:57)
and I'm going to drop in this json.

(04:59)
Essentially it's four lists,
each one with five tasks,

(05:02)
and just some text on each one.

(05:05)
We'll save that, cool.

(05:07)
Now inside this function,
if I wanted to, which I do,

(05:11)
I'm going to "console.log"
out the "event.data"

(05:14)
and that should be
the data that we just created.

(05:17)
I'm just going to
update this to say "seed"

(05:20)
so that when I copy it, it works.

(05:23)
"8base invoke-local",
the name of the function,

(05:25)
"seedDummyData"
and then the mock is "seed".

(05:29)
I'm going to run that, and boom,

(05:30)
that's exactly now what's
console logging now.

(05:33)
Now we know we have
our data inside our function.

(05:36)
What I said earlier about the
pre-authenticated API module,

(05:39)
that's the context argument.

(05:41)
Here context, API, GraphQL request,
I'm all set up to interact with my API.

(05:47)
What I'm going to do now is
I'm going to do a for loop.

(05:51)
I'm going to do for each object inside,
so for each list.

(05:59)
It's going to be the constant
inside of our "event.data".

(06:08)
Once again,
I always just like logging things out.

(06:12)
Probably pretty junior of me,
but it is what it is.

(06:17)
Making sure that
we're getting integers right now.

(06:24)
I think if we change that to the "of"
we will be good, cool.

(06:30)
Here we're getting each list
with the dummy data

(06:32)
and the tasks and all that stuff.

(06:34)
Awesome, so now we know that,

(06:36)
okay, well we are getting
this argument here.

(06:38)
Name, description, dummy tasks,
and this is what we're getting,

(06:42)
and this is the dummy data
that we want to create for our list.

(06:45)
Let's jump back now to our console.

(06:50)
Here I already have the mutation
written that we're going to want to use.

(06:54)
Essentially we're writing a mutation

(06:56)
where we pass the name, the description,
and the dummy tasks,

(06:58)
and then we're going to
create a list using that.

(07:01)
We're going to use
the listCreate operation

(07:03)
and as the data pass in the name,
the description,

(07:06)
and then to the task relationship,
create new task through that relationship,

(07:11)
which is the dummy tasks array,
and we just get an ID back.

(07:13)
We don't really care
about the response in this case.

(07:17)
I copied that, and we can see we have
no data in our database, cool.

(07:22)
Let me go back to VS Code, awesome.

(07:28)
Here do a new constant called "MUTATION"

(07:33)
which is going to equal
that mutation that we created.

(07:39)
Awesome.

(07:41)
Now inside here, pretty simple:
"await ctx.api.gqlRequest"

(07:48)
and into that pass in the mutation
as well as the list.

(07:54)
We're just going to iterate over those,
very straightforward.

(07:58)
Let's go ahead and run that.

(08:04)
Done.

(08:05)
Now if we go back to our console
and reload the page,

(08:11)
we can see that we interact with our API

(08:13)
and we're able to generate
the dummy data in this new environment

(08:17)
as well as all the different tasks
that we wanted in there as well.

(08:22)
I know this was a short video,

(08:24)
however I always like to,
when investigating new tools,

(08:27)
see the ways that the people
that are the power users of those tools

(08:31)
use them in creative or different ways.

(08:33)
Essentially, if you were
to look at our documentation,

(08:35)
you'd see tasks as these things
which you run on a schedule,

(08:40)
or you invoke from another function.

(08:42)
However, they are actually
an amazing utility to use

(08:46)
when wanting to interface with your API

(08:48)
in essentially a
pre-authenticated environment,

(08:51)
and even use them
as more on an ad hoc basis.

(08:54)
Maybe you have some way
of cleaning data that you want to use

(08:58)
and you have that function stored locally
to whenever you want to run it.

(09:02)
I hope this gave you
some cool new ideas

(09:04)
on ways that you can have
more efficient 8base projects

(09:07)
or develop in a more
maintainable and systematic way.

(09:10)
If you like these videos,

(09:12)
and you want to be subscribed
to get updates when new ones come out,

(09:16)
definitely subscribe to the channel,

(09:19)
definitely check out some of the other
videos that we've been producing.

(09:22)
They'll give you some other great tips
on how to develop with 8base,

(09:26)
and I look forward
to seeing you in future videos.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.