Skip to content

const8ine/js-sqlite-starter

Repository files navigation

SQLite + JavaScript Project Starter

Running

This project uses Node v20.

The database itself is missing in this project, run the following command to create a database using migrations and seeds

npm run start

or

node index.js

that is the same.

The database will appear in src/database folder (src/database/db.sqlite)

Use DB Browser for SQLite see the contents of the generated database https://sqlitebrowser.org/dl/

Step-by-Step Guide to Create a Migration with Knex

This guide outlines how to create database migrations using Knex, a SQL query builder.

Create a Migration File

npx knex migrate:make create_some_table

This command will create a new file in the migrations directory src/database/migrations with a timestamp prefixed filename, e.g.: 20200916135224_create_some_table.js

Run the Migration

npx knex migrate:latest

Rollback the Migration

npx knex migrate:rollback

Create a Seed File

Seeds are used to populate your database with initial or sample data. Basically you use knex CLI to create a seed file:

node create_seed.js name_of_yor_new_seed_file

The script will create a new file in the seeds directory src/database/seeds with a timestamp prefixed filename, e.g.: 20200916135224_name_you_provided.js and example content will be added to the file.

We recommend using the following script because Knex itself doesn't add timestamps to seed names. This additional functionality ensures that the seeds will be executed in the right order.

About

SQLite + JavaScript Project Starter

Topics

Resources

Stars

Watchers

Forks