Seeding Data
Once the application is deployed, it can be populated with initial data by loading the data from seed files.
Seed files
Seeds are defined CUE files that contain data and are located in the seed
folder of each template.
<span><span style="color: var(--shiki-token-keyword)">package</span><span style="color: var(--shiki-color-text)"> master</span></span>
<span></span>
<span><span style="color: var(--shiki-token-keyword)">import</span><span style="color: var(--shiki-color-text)"> (</span></span>
<span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">tailor.build/template/seed/utils</span><span style="color: var(--shiki-color-text)">"</span></span>
<span><span style="color: var(--shiki-color-text)">)</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">#Roles: {</span></span>
<span><span style="color: var(--shiki-color-text)"> Admin: {</span></span>
<span><span style="color: var(--shiki-color-text)"> id: utils.uuid </span><span style="color: var(--shiki-token-keyword)">&</span><span style="color: var(--shiki-color-text)"> {</span><span style="color: var(--shiki-token-constant)">_</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> #type: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">roles</span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> #value: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">Admin</span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-color-text)">}</span></span>
<span><span style="color: var(--shiki-color-text)"> name: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">Admin</span><span style="color: var(--shiki-color-text)">"</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span></span>
<span><span style="color: var(--shiki-color-text)"> Editor: {</span></span>
<span><span style="color: var(--shiki-color-text)"> id: utils.uuid </span><span style="color: var(--shiki-token-keyword)">&</span><span style="color: var(--shiki-color-text)"> {</span><span style="color: var(--shiki-token-constant)">_</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> #type: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">roles</span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> #value: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">Editor</span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-color-text)">}</span></span>
<span><span style="color: var(--shiki-color-text)"> name: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">Editor</span><span style="color: var(--shiki-color-text)">"</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span></span>
<span><span style="color: var(--shiki-color-text)"> Viewer: {</span></span>
<span><span style="color: var(--shiki-color-text)"> id: utils.uuid </span><span style="color: var(--shiki-token-keyword)">&</span><span style="color: var(--shiki-color-text)"> {</span><span style="color: var(--shiki-token-constant)">_</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> #type: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">roles</span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> #value: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">Viewer</span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-color-text)">}</span></span>
<span><span style="color: var(--shiki-color-text)"> name: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">Viewer</span><span style="color: var(--shiki-color-text)">"</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">Role: {</span></span>
<span><span style="color: var(--shiki-color-text)"> items: [</span></span>
<span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">for</span><span style="color: var(--shiki-color-text)"> k</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> v </span><span style="color: var(--shiki-token-keyword)">in</span><span style="color: var(--shiki-color-text)"> #Roles {</span></span>
<span><span style="color: var(--shiki-color-text)"> v</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)"> ]</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
Models that feature associations benefit from having reproducible UUIDs as their identifier. Calling utils.uuid function with the same string arguments (#type, #value) will produce the same UUID, allowing us to associate subsequently created models to that particular role.
Seed data into the Tailor DB
Once your seed data is ready, run the following commands to load the data into an application.
First, install all the dependencies required to run seed.mjs
.
<span><span style="color: var(--shiki-token-function)">pnpm</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">i</span></span>
<span></span>
Next, run the seed script to load the data into the Tailor DB.
<span><span style="color: var(--shiki-token-function)">node</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">../common/scripts/seed.mjs</span></span>
<span></span>
Upon running the above command, you will be asked to choose a workspace and an application to seed the data.
Seed script
The seed script performs the following tasks:
- Configures the workspace and application
- Generates Machine user token to run the queries
- Executes GraphQL Mutations to load the data into Tailor DB