Go back to 8base Academy
May 26, 2022

Seeding Databases and Environments using Serverless Task Functions

Sebastian Scholl

Hey there,
this is Sebastian from 8base.

Today we're going to be looking at how
you can seed your database

or a new workspace environment
with dummy data

using a serverless function,
specifically task functions.

Now I'm going to be showing
this in the context of CICD

for seeding a new
workspace environment.

However, this could work for whether

you only have one environment
in your workspace or multiple,

so it really applies
in multiple ways.

Really at the end of the day,
what it is,

it's a more creative use
of the task function.

Let's get started by
just first looking a little bit

at the workspace
that we have.

Here I have my workspace
SuperTasks Backend,

and inside of here I have
multiple workspace environments:

Master, Staging, Development,

and then this feature
branch called Multitenant.

Now what I'm going to be doing is I'm
going to be going to my command line

to create a new environment
which I will want to develop in.

I can do that by shooting
over to my VS Code.

I'm in my SuperTasks Backend
right here,

and I'm going to run "8base configure"

just to make sure that I'm
connected to the right workspace

as I do jump around sometimes.

SuperTasks Backend, perfect.

Then I'm going to run
"8base environment list".

Cool, and now I'm just going to set
Dev as my current environment.


Now what I'm going to do is I'm going
to go ahead and create my new environment,

so "8base environment branch"
and I'm going to run the help command

just so we can look at some of
the options really quickly.

If we wanted to, any of the data
that's currently in our

Dev environment when branching this
environment, we could copy over.

Now there's reasons why
you might not want to do that,

especially if you're going from a
production environment

to a feature branch,

maybe you don't want to bring
your users production data down.

That's where this way of seeing
your database would become more useful.

What I'm going to say is
I'm going to say "branch",

I'm going to say the name of
the new environment is "newFeature",

and the mode
is going to be "SYSTEM".

By saying "SYSTEM" that means
that we're not copying over any data.

I'm going to run that,
and I will check back in in a minute

once this new environment has created.

Okay, so now with that
new environment created,

what we're going to go ahead and do
is create a new function,

which is going to be a task function.

"8base g", for generate, "task",

and we're going to call
this "seedDummyData"

and syntax is going to be Javascript.

Now just because you author a function

doesn't mean you have to
deploy a function, right?

You can create as
many functions as you want,

which you only ever use for managing
your workspace locally,

or interacting with your API.

It's a great way of essentially
generating a function quickly

and then having an environment in which

you can quickly execute
the scripts you write,

have a pre-authenticated API,
really just a nice working environment.

That's what we're going to leverage
here to be able to essentially

write a script that interacts with our
API to generate some dummy data.

Now we have that task right here.

Once again if I was deploying,

I didn't want it out, I could just
comment it out and I'd be good.

Now I'm going to go into the source
and I'm going to look at my function here.

I'm just going to say
that once it's done,

task is completed,
"Task completed." Period.

Doesn't need to be a string literal,
can just be a normal string, cool.

Now a couple things
that we want to do:

the first thing is that
I have some dummy data

which I'm going to pull
from off screen,

and this is going to be the dummy data
essentially that we send to our API.

Right now, if we wanted to,

we could pull in some
type of cool faker module

that fake generates a bunch of data.

We could write something
that numerically calculates it.

However you want to come up with your
dummy data, you can 100% do that.

However, instead of creating a separate
file that we have to import that file

and read the data,
we can just actually use a mock.

Instead of it being called the request
mock, I'm going to call it "seed.json".

Go in there, delete the headers
and the body, that's not relevant.

We have a data object,
we can call it "data"

and I'm going to drop in this json.

Essentially it's four lists,
each one with five tasks,

and just some text on each one.

We'll save that, cool.

Now inside this function,
if I wanted to, which I do,

I'm going to "console.log"
out the "event.data"

and that should be
the data that we just created.

I'm just going to
update this to say "seed"

so that when I copy it, it works.

"8base invoke-local",
the name of the function,

and then the mock is "seed".

I'm going to run that, and boom,

that's exactly now what's
console logging now.

Now we know we have
our data inside our function.

What I said earlier about the
pre-authenticated API module,

that's the context argument.

Here context, API, GraphQL request,
I'm all set up to interact with my API.

What I'm going to do now is
I'm going to do a for loop.

I'm going to do for each object inside,
so for each list.

It's going to be the constant
inside of our "event.data".

Once again,
I always just like logging things out.

Probably pretty junior of me,
but it is what it is.

Making sure that
we're getting integers right now.

I think if we change that to the "of"
we will be good, cool.

Here we're getting each list
with the dummy data

and the tasks and all that stuff.

Awesome, so now we know that,

okay, well we are getting
this argument here.

Name, description, dummy tasks,
and this is what we're getting,

and this is the dummy data
that we want to create for our list.

Let's jump back now to our console.

Here I already have the mutation
written that we're going to want to use.

Essentially we're writing a mutation

where we pass the name, the description,
and the dummy tasks,

and then we're going to
create a list using that.

We're going to use
the listCreate operation

and as the data pass in the name,
the description,

and then to the task relationship,
create new task through that relationship,

which is the dummy tasks array,
and we just get an ID back.

We don't really care
about the response in this case.

I copied that, and we can see we have
no data in our database, cool.

Let me go back to VS Code, awesome.

Here do a new constant called "MUTATION"

which is going to equal
that mutation that we created.


Now inside here, pretty simple:
"await ctx.api.gqlRequest"

and into that pass in the mutation
as well as the list.

We're just going to iterate over those,
very straightforward.

Let's go ahead and run that.


Now if we go back to our console
and reload the page,

we can see that we interact with our API

and we're able to generate
the dummy data in this new environment

as well as all the different tasks
that we wanted in there as well.

I know this was a short video,

however I always like to,
when investigating new tools,

see the ways that the people
that are the power users of those tools

use them in creative or different ways.

Essentially, if you were
to look at our documentation,

you'd see tasks as these things
which you run on a schedule,

or you invoke from another function.

However, they are actually
an amazing utility to use

when wanting to interface with your API

in essentially a
pre-authenticated environment,

and even use them
as more on an ad hoc basis.

Maybe you have some way
of cleaning data that you want to use

and you have that function stored locally
to whenever you want to run it.

I hope this gave you
some cool new ideas

on ways that you can have
more efficient 8base projects

or develop in a more
maintainable and systematic way.

If you like these videos,

and you want to be subscribed
to get updates when new ones come out,

definitely subscribe to the channel,

definitely check out some of the other
videos that we've been producing.

They'll give you some other great tips
on how to develop with 8base,

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.