A template for Node-Express-PostgreSQL projects written in TypeScript, based on the Alchemy Code Lab Express/SQL template.
There are several npm scripts:
npm run start- runs
tscto transpile your TS to JS, putting it in a/distdirectory, then runsnode dist/server.js
- runs
npm run start:watch- runs two other scripts alongside each other using
concurrently, runningtscon any changes while updating withnodemon
- runs two other scripts alongside each other using
npm run testandnpm run test:watch- running
jest(configured for TS), either once or watching for changes
- running
npm run setup-db- runs through the database-related pieces of the template, dropping and creating tables and setting up a Postgres client
- runs
tscto make sure it's using the latest code, then runs thesetup-db.jsfile from/dist
npm run format- formats the project code with
prettier
- formats the project code with
-
Click the handy green
Use this templatebutton at the top of the code for this repository. This will take you to the repo creation page - go ahead and make the repo as you normally would; you don't need to check the 'include all branches' box. -
When your new repo has been created from the template, clone it down from that new repo's page.
-
In your local environment,
cdinto the newly cloned project and runnpm ito install the needed packages (take a peek in thepackage.jsonfor a full list). -
Set up your environment variables in a
.envfile. The included.env-examplehas a generic DATABASE_URL included and assigns PORT to 7890. Change these as needed in your.env. Thedotenvmodule configuration is handled in the scripts, so you can simply refer toprocess.env.SOME_VARIABLEwherever needed without importing the module. -
To check that everything has been configured correctly, I recommend running the tests (see scripts above). There is a set of 'health-check' tests that should pass out of the box, but if you can't get it to pass on a fresh install then there is likely something off in how I've configured this. Please let me know! Feel free to open issues or PRs for this or anything else that seems fishy.
-
If the tests pass, run one of the start scripts and make sure it starts up correctly - once the server is successfully running you'll see a console log indicating what port it's listening on. If it doesn't work on the first try, try again - there are some quirks with how the TypeScript transpilation happens and I'm not confident I've worked them all out, but once the
/distfolder has been created the start scripts should work just fine. -
The
/distfolder gets created the first timetscruns, which may happen as a result of several scripts. It is included in the.gitignorelist, so if you need to include your transpiled code in the repo where this will be uploaded (e.g. for deployment) you will want to remove it from the.gitignore. -
Don't change files in the
/distfolder. They're going to look awful, and that's okay. This is the result of your TypeScript code being transpiled to JavaScript, and it will update to reflect your most recent changes automatically. -
In general, leave the top-level folder/file hierarchy as it is. The
/sqlfolder, for example, needs to be where it is to make it available to both the TS files in the/srcdirectory and the JS files in/distvia the same path.
Jest testing in GitHub's test environment is configured in .github/workflows/nodejs.yml. I have removed the hardcoded credentials so that this can be used with whatever test database environment you happen to be using. You will need to add the relevant credentials to your GitHub repository - instructions for this process are currently outside the scope of this template, but I'll add a good link if I find one. Here are the credentials you will need to add if you wish to use this feature - these fields can also be replaced in the mentioned .yml file with the relevant information:
POSTGRES_TEST_USER- the username for your postgres test environemnt
POSTGRES_TEST_PASSWORD- the password for your postgres test environment
POSTGRES_TEST_DB- the connection string for your postgres test database, which would look something like
postgres://username:password@host:port/databasename
- the connection string for your postgres test database, which would look something like