Seed a database from a Node.js script
Load a schema and rows from a SQL file into a SQL Database database with a Node.js script and the @aziontech/sql package of Azion Lib.
You can load a schema and its first rows into a SQL Database database from a Node.js script. The script uses the @aziontech/sql package of Azion Lib. It reads the statements from a SQL file, so the same file seeds a database in every environment.
Prerequisites
- Node.js and npm. The script is an ES module that uses top-level
await, and@aziontech/sqlneeds the globalfetchof Node.js. - A personal token. To create one, refer to Manage personal tokens.
- A database. To create one, refer to Create and manage databases. The examples use a database named
storefront-db.
Install the package
The script imports @aziontech/sql from the node_modules directory of its project.
In an empty directory, create the project and install the package:
npm prints the new package.json, and the install ends with:
The project has @aziontech/sql in its package.json dependencies.
Write the seed file
The seed file holds the SQL statements, in the order the database runs them. End each statement with a ; at the end of its line, because the script splits the file there. A ; inside a value stays in its statement, as in 'Pen; blue'.
The database stops at the first statement that fails, and the statements before it stay applied. Because a second run starts again from the first statement, write every statement so that it can run twice. CREATE TABLE IF NOT EXISTS skips a table that exists, and INSERT OR REPLACE overwrites a row with the same key. For more information, refer to Send related statements in one call.
Create a seed.sql file:
The seed.sql file holds one CREATE TABLE statement and three INSERT OR REPLACE statements.
Write the seed script
The functions of @aziontech/sql find a database by a search on its name, and the search also matches part of a name: storefront finds storefront-db. The script first reads the database with getDatabase and stops unless its name matches exactly.
The useExecute function then sends every statement of the file to the database in one call. The useQuery function runs SELECT COUNT(*) on the products table, so the script confirms the seed. Each function reads your personal token from the AZION_TOKEN environment variable.
The script takes the database name as its first argument. Create a seed.mjs file:
When the name check, the seed, or the count fails, the script prints the error and exits with code 1. The count query names the products table: when your seed file creates other tables, name one of them there.
Run the seed
To run the seed:
-
In the terminal where you run the script, store your personal token in the
AZION_TOKENvariable. Replace[TOKEN VALUE]with your token:Every script you start from this terminal reads the token, until the terminal closes.
-
Run the script with the full name of your database:
Text -
(Optional) Run the script again. The statements skip the existing table and overwrite the rows, so the count stays the same:
Text
The database holds the products table and its three rows.
When no database has the exact name you passed, such as storefront, the script prints Seed failed: no database is named storefront and writes nothing. When a statement fails, it prints the database error, such as Seed failed: no such table: nope.