Skip to content

Commit e3cd0bc

Browse files
committed
chore: initial commit
0 parents  commit e3cd0bc

File tree

10 files changed

+734
-0
lines changed

10 files changed

+734
-0
lines changed

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
POSTGRES_USER=postgres_user
3+
POSTGRES_PASSWORD=pg_pass
4+
POSTGRES_DB=myapp
5+
6+
# actual connection used by Postgraphile
7+
DATABASE_URL=postgres://postgres_user:pg_pass@db/myapp
8+
# used for development purposes
9+
SHADOW_DATABASE_URL=postgres://postgres_user:pg_pass@db/myapp_shadow
10+
# super user account
11+
ROOT_DATABASE_URL=postgres://postgres_user:pg_pass@db/postgres

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.env
2+
*.log
3+
4+
node_modules

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Postgres GraphQL
2+
3+
This is an example project using the following technologies to provide a GraphQL server.
4+
5+
- Postgres
6+
- Postgraphql
7+
- Graphile Migrate
8+
9+
The main benefit here is our GraphQL API types come directly from the Database, furthermore in our frontend applications
10+
we can automatically generate Typescript Types and Typed requests to interact with our API greatly improving development
11+
agility and our application's integrity.
12+
13+
## Getting Started
14+
15+
The only hard requirement is that you have `docker` and `docker-compose` installed which, although naturally you could
16+
install everything locally.
17+
18+
### .env
19+
20+
First lets copy the `.env.example` to `.env`, this file will be used by `docker-compose` which will pass these environment
21+
variables into designated containers.
22+
23+
```bash
24+
cp .env.example .env
25+
```
26+
27+
### Local Docker Development
28+
29+
```bash
30+
docker-compose build
31+
```
32+
33+
Lets initialize and insure the database is ready to go.
34+
35+
```bash
36+
docker-compose up database
37+
```
38+
39+
Install the dependencies
40+
41+
```bash
42+
docker-compose run migrations yarn
43+
```
44+
45+
Initialize graphile-migrate
46+
47+
```bash
48+
docker-compose run migrations yarn graphile-migrate init
49+
```
50+
51+
Write some migrations in `current.sql`, once your ready to commit.
52+
53+
```bash
54+
docker-compose exec migrations yarn graphile-migrate commit
55+
```

apps/database/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

apps/database/.gmrc

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Graphile Migrate configuration.
3+
*
4+
* If you decide to commit this file (recommended) please ensure that it does
5+
* not contain any secrets (passwords, etc) - we recommend you manage these
6+
* with environmental variables instead.
7+
*
8+
* This file is in JSON5 format, in VSCode you can use "JSON with comments" as
9+
* the file format.
10+
*/
11+
12+
{
13+
/*
14+
* connectionString: this tells Graphile Migrate where to find the database
15+
* to run the migrations against.
16+
*
17+
* RECOMMENDATION: use `DATABASE_URL` envvar instead.
18+
*/
19+
// "connectionString": "postgres://appuser:apppassword@host:5432/appdb",
20+
21+
/*
22+
* shadowConnectionString: like connectionString, but this is used for the
23+
* shadow database (which will be reset frequently).
24+
*
25+
* RECOMMENDATION: use `SHADOW_DATABASE_URL` envvar instead.
26+
*/
27+
// "shadowConnectionString": "postgres://appuser:apppassword@host:5432/appdb_shadow",
28+
29+
/*
30+
* rootConnectionString: like connectionString, but this is used for
31+
* dropping/creating the database in `graphile-migrate reset`. This isn't
32+
* necessary, shouldn't be used in production, but helps during development.
33+
*
34+
* RECOMMENDATION: use `ROOT_DATABASE_URL` envvar instead.
35+
*/
36+
// "rootConnectionString": "postgres://adminuser:adminpassword@host:5432/postgres",
37+
38+
/*
39+
* pgSettings: key-value settings to be automatically loaded into PostgreSQL
40+
* before running migrations, using an equivalent of `SET LOCAL <key> TO
41+
* <value>`
42+
*/
43+
"pgSettings": {
44+
// "search_path": "app_public,app_private,app_hidden,public",
45+
},
46+
47+
/*
48+
* placeholders: substituted in SQL files when compiled/executed. Placeholder
49+
* keys should be prefixed with a colon and in all caps, like
50+
* `:COLON_PREFIXED_ALL_CAPS`. Placeholder values should be strings. They
51+
* will be replaced verbatim with NO ESCAPING AT ALL (this differs from how
52+
* psql handles placeholders) so should only be used with "safe" values. This
53+
* is useful for committing migrations where certain parameters can change
54+
* between environments (development, staging, production) but you wish to
55+
* use the same signed migration files for all.
56+
*
57+
* The special value "!ENV" can be used to indicate an environmental variable
58+
* of the same name should be used.
59+
*
60+
* Graphile Migrate automatically sets the `:DATABASE_NAME` and
61+
* `:DATABASE_OWNER` placeholders, and you should not attempt to override
62+
* these.
63+
*/
64+
"placeholders": {
65+
// ":DATABASE_VISITOR": "!ENV", // Uses process.env.DATABASE_VISITOR
66+
},
67+
68+
/*
69+
* Actions allow you to run scripts or commands at certain points in the
70+
* migration lifecycle. SQL files are ran against the database directly.
71+
* "command" actions are ran with the following environmental variables set:
72+
*
73+
* - GM_DBURL: the PostgreSQL URL of the database being migrated
74+
* - GM_DBNAME: the name of the database from GM_DBURL
75+
* - GM_DBUSER: the user from GM_DBURL
76+
* - GM_SHADOW: set to 1 if the shadow database is being migrated, left unset
77+
* otherwise
78+
*
79+
* If "shadow" is unspecified, the actions will run on events to both shadow
80+
* and normal databases. If "shadow" is true the action will only run on
81+
* actions to the shadow DB, and if false only on actions to the main DB.
82+
*/
83+
84+
/*
85+
* afterReset: actions executed after a `graphile-migrate reset` command.
86+
*/
87+
"afterReset": [
88+
// "afterReset.sql",
89+
// { "_": "command", "command": "graphile-worker --schema-only" },
90+
],
91+
92+
/*
93+
* afterAllMigrations: actions executed once all migrations are complete.
94+
*/
95+
"afterAllMigrations": [
96+
// {
97+
// "_": "command",
98+
// "shadow": true,
99+
// "command": "if [ \"$IN_TESTS\" != \"1\" ]; then ./scripts/dump-db; fi",
100+
// },
101+
],
102+
103+
/*
104+
* afterCurrent: actions executed once the current migration has been
105+
* evaluated (i.e. in watch mode).
106+
*/
107+
"afterCurrent": [
108+
// {
109+
// "_": "command",
110+
// "shadow": true,
111+
// "command": "if [ \"$IN_TESTS\" = \"1\" ]; then ./scripts/test-seed; fi",
112+
// },
113+
],
114+
115+
/*
116+
* blankMigrationContent: content to be written to the current migration
117+
* after commit. NOTE: this should only contain comments.
118+
*/
119+
// "blankMigrationContent": "-- Write your migration here\n",
120+
121+
/****************************************************************************\
122+
*** ***
123+
*** You probably don't want to edit anything below here. ***
124+
*** ***
125+
\****************************************************************************/
126+
127+
/*
128+
* manageGraphileMigrateSchema: if you set this false, you must be sure to
129+
* keep the graphile_migrate schema up to date yourself. We recommend you
130+
* leave it at its default.
131+
*/
132+
// "manageGraphileMigrateSchema": true,
133+
134+
/*
135+
* migrationsFolder: path to the folder in which to store your migrations.
136+
*/
137+
// migrationsFolder: "./migrations",
138+
139+
"//generatedWith": "1.0.2"
140+
}

apps/database/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ARG BASE_IMAGE=node:12.18.2-alpine3.11
2+
3+
FROM $BASE_IMAGE as base
4+
5+
WORKDIR /app
6+
7+
CMD yarn start

apps/database/migrations/current.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Enter migration here

apps/database/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"start": "graphile-migrate watch",
5+
"mig-init": "graphile-migrate init"
6+
},
7+
"dependencies": {
8+
"graphile-migrate": "^1.0.2"
9+
}
10+
}

0 commit comments

Comments
 (0)