|
| 1 | +# This Makefile is intended to be called by higher level/project Makefiles |
| 2 | +# It bakes in a number of standard practices include the use of the src |
| 3 | +# directory (choose your build dir as appropriate). |
| 4 | +# |
| 5 | +# It also includes database scripts, which technically aren't the concern of this |
| 6 | +# module, but very often services do have a database component but don't need |
| 7 | +# any sort of service-specific database modules (just plain pg). So this Makefile |
| 8 | +# has targets for setting up a database for the service, and generating types |
| 9 | +# using kysely, and using db-migrate for schema managment. |
| 10 | +# |
| 11 | +# With all that, you can have a Makefile that does this: |
| 12 | +# |
| 13 | +# .PHONY: all service dbi ts |
| 14 | +# |
| 15 | +# export DB_NAME ?= cool_db |
| 16 | +# export SERVICE_NAME ?= my-cool-serv |
| 17 | +# |
| 18 | +# include node_modules/@openapi-typescript-infra/service/Makefile |
| 19 | +# |
| 20 | +# all: service dbi ts |
| 21 | +# |
| 22 | + |
| 23 | +build_dir := $(node -e "console.log(require('./package.json').main.replace(/^.\//, '').split('/')[0])") |
| 24 | +src_files := $(shell find src -name '*.ts') |
| 25 | +build_files := $(patsubst src/%.ts,$(build_dir)/%.js,$(src_files)) |
| 26 | + |
| 27 | +# General utilities |
| 28 | +clean: |
| 29 | + yarn dlx rimraf ./$(build_dir) src/generated |
| 30 | + |
| 31 | +# Typescript items |
| 32 | +ts: $(word 1, $(build_files)) |
| 33 | + |
| 34 | +$(word 1, $(build_files)): $(src_files) |
| 35 | + ./node_modules/.bin/tsc -p tsconfig.build.json |
| 36 | + |
| 37 | +service: src/generated/service/index.ts |
| 38 | + |
| 39 | +src/generated/service/index.ts: api/${SERVICE_NAME}.yaml |
| 40 | + echo "Building service interface" |
| 41 | + yarn dlx openapi-typescript-express ./api/${SERVICE_NAME}.yaml \ |
| 42 | + --output ./src/generated/service/index.ts |
| 43 | + ./node_modules/.bin/prettier ./src/generated/service/index.ts --write |
| 44 | + |
| 45 | +# Postgres database things |
| 46 | +export PGUSER ?= postgres |
| 47 | +export PGPASSWORD ?= postgres |
| 48 | +export PGHOST ?= localhost |
| 49 | + |
| 50 | +db-ci: |
| 51 | + yarn run-pg-sql -q postgres ./migrations/setup/ci_setup.sql |
| 52 | + yarn run-pg-sql -q postgres ./migrations/setup/db_setup.sql |
| 53 | + yarn migration:apply |
| 54 | + yarn run-pg-sql $(DB_NAME) ./migrations/setup/dev_setup.sql |
| 55 | + |
| 56 | +db-drop: |
| 57 | + yarn run-pg-sql -q postgres -e "DROP DATABASE IF EXISTS $(DB_NAME);" |
| 58 | + |
| 59 | +db+: |
| 60 | + yarn migration:apply |
| 61 | + |
| 62 | +db-: |
| 63 | + yarn migration:undo |
| 64 | + |
| 65 | +db-clean: db-drop db-ci |
| 66 | + |
| 67 | +dbi: |
| 68 | + echo "Generating database types" |
| 69 | + DATABASE_URL=postgres://$(PGUSER):$(PGPASSWORD)@$(PGHOST)/$(DB_NAME) yarn kysely-codegen \ |
| 70 | + --dialect postgres --schema public \ |
| 71 | + --out-file src/generated/database.ts |
0 commit comments