TypeScriptTemplate
This is a starting template for a TypeScript web app that has the following tech stack:
- Node.js
- MySQL/MariaDB
- Fastify
- Prisma
- Kysely
- EJS
- Tailwind
I created this to save myself the time of having to spend hours initializing and configuring a project every time I have an idea since this is already a pretty good starting point.
Setup
Setting up the application will consist of the following:
- Install MySQL/MariaDB server
- Create a new database for the application
- Create a new user with full access to the database
- Configure environment file with database connection information and other optional changes
- Installing node packages
- Running application
Database Setup
Docker
You have two options for setting up your database: configuring the Docker compose file or installing and
configuring a server manually. If you choose to use Docker, all you have to do is configure the following
values in the compose.yaml file:
MYSQL_ROOT_PASSWORDMYSQL_DATABASEMYSQL_USERMYSQL_PASSWORD
You may also configure the port if desired. Whatever values you enter for the above compose.yaml file
must also be copied to your .env file. See Configuring Environment File for more info.
Once configured, you can run docker compose up -d to start your database server. Otherwise, you may
ignore this section and continue to install your own server.
Configuring Database Server
This project was created using MariaDB and uses @prisma/adapter-mariadb. While MariaDB is compliant with
MySQL and will probably have no issues working with MySQL, it is highly advised you use MariaDB for your
database.
Once your database server is installed, you can run the following commands:
-- Create the database
CREATE DATABASE `foobar`
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- Create the user (pls do not use the default password)
CREATE USER 'foo'@'%' IDENTIFIED BY '<PASSWORD>';
-- if you prefer a more locked down account that can only connect locally:
--CREATE USER 'foo'@'192.168.%' IDENTIFIED BY '<PASSWORD>';
-- Grant all privileges on the recipes database
GRANT ALL PRIVILEGES ON `foobar`.* TO 'foo'@'%';
-- Apply the privilege changes
FLUSH PRIVILEGES;
The names of the database and account are not important, nor is the host value for the account. Feel free to configure the database name, user name, and host. If you decide to do so, please remember to use the updated values later in the config.
Configuring Environment File
Once you've confirmed your database is fully configured and you can connect using
the newly created account, you will need to populate the .env config file. You
may rename the .env.example file to .env to have all required values pre-populated
in the config.
The only required values to be updated are the keys prefixed with DB_, as these
values are used to build the database connection string, and all the values under
the # Security settings comment. For the SESSION_SECRET value, it is advised
you generate a cryptographically random string of at least 32 characters.
# Application settings
APP_PORT=8080
# Environments - dev, prod
APP_ENVIRONMENT=prod
# Database settings
DB_HOST=<server IP>
DB_PORT=3306
DB_NAME=foobar
DB_USER=foo
DB_PASS=<PASSWORD>
# Security settings
SESSION_SECRET=a2VwdG9mZmljaWFsYXJyYW5nZW1lbnRpbmRlcGVuZGVudGxha2VjbGVhcmx5b2xkZXJibG9vZHRpcHNwZWVjaA==
Running Application
Once the .env file has been configured, you may now run the application. This application
was developed with Node.js v25.9.0 and npm v11.12.1. Please see Node.js's download
page for installation.
If/when you have Node.js installed, open a shell in the root of this project and run the following commands:
# Install Node.js packages
npm i
# Deploy database using provided connection information
npm run db:deploy
# Seed database with required data
npm run db:seed
# Run application
npm start
If successful, you will see the IP of the server in the console output.