├── .gitignore ├── docker-compose.yml └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | cockroach-data -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | services: 3 | db: 4 | image: "cockroachdb/cockroach" 5 | ports: 6 | - "26257:26257" 7 | # - "8080:8080" 8 | volumes: 9 | - ./cockroach-data/roach1:/cockroach/cockroach-data 10 | command: start --insecure 11 | postgraphile: 12 | image: "graphile/postgraphile" 13 | ports: 14 | - "5000:5000" 15 | command: postgraphile -n 0.0.0.0 --connection postgresql://root@db:26257/defaultdb?sslmode=disable --schema defaultdb --watch 16 | depends_on: 17 | - db 18 | hasura: 19 | image: "hasura/graphql-engine" 20 | ports: 21 | - "8080:8080" 22 | environment: 23 | - HASURA_GRAPHQL_DATABASE_URL=postgresql://root@db:26257/defaultdb?sslmode=disable 24 | - HASURA_GRAPHQL_ENABLE_CONSOLE=true 25 | depends_on: 26 | - db -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Experiment with DBs and auto API generation 2 | 3 | ## CockroachDB 4 | 5 | We will use Docker to run everything. Because we need to run more than one docker image and this is a toy project we will use [docker compose](https://docs.docker.com/compose/gettingstarted/). 6 | 7 | The first service is the database. We will use [cockroach database](https://www.cockroachlabs.com/docs/stable/start-a-local-cluster-in-docker.html). 8 | 9 | I created the first version of `docker-compose.yml` based on two tutorials linked above. To test run `docker-compose up` and connect http://localhost:8080/. 10 | 11 | ## External client 12 | 13 | (optional) 14 | 15 | [Let's connect to the database with an external client](https://www.cockroachlabs.com/docs/managed/stable/managed-connect-to-your-cluster.html). 16 | 17 | Start compose 18 | 19 | ``` 20 | docker-compose up 21 | ``` 22 | 23 | [Use DB client of your preference](https://github.com/dhamaniasad/awesome-postgres#gui). I would use [pgcli](https://www.pgcli.com/). 24 | 25 | ``` 26 | pgcli "postgresql://root@localhost:26257/defaultdb?sslmode=disable" 27 | Server: PostgreSQL CCL 28 | Version: 2.0.2 29 | Chat: https://gitter.im/dbcli/pgcli 30 | Mail: https://groups.google.com/forum/#!forum/pgcli 31 | Home: http://pgcli.com 32 | root@localhost:defaultdb> Exception in thread completion_refresh: 33 | Traceback (most recent call last): 34 | File "python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner 35 | self.run() 36 | File "python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run 37 | self._target(*self._args, **self._kwargs) 38 | File "pgcli/2.0.2/libexec/lib/python3.7/site-packages/pgcli/completion_refresher.py", line 68, in _bg_refresh 39 | refresher(completer, executor) 40 | File "pgcli/2.0.2/libexec/lib/python3.7/site-packages/pgcli/completion_refresher.py", line 111, in refresh_tables 41 | completer.extend_foreignkeys(executor.foreignkeys()) 42 | File "pgcli/2.0.2/libexec/lib/python3.7/site-packages/pgcli/pgcompleter.py", line 259, in extend_foreignkeys 43 | for fk in fk_data: 44 | File "pgcli/2.0.2/libexec/lib/python3.7/site-packages/pgcli/pgexecute.py", line 614, in foreignkeys 45 | cur.execute(query) 46 | psycopg2.NotSupportedError: unimplemented at or near ")" 47 | DETAIL: source SQL: 48 | 49 | SELECT s_p.nspname AS parentschema, 50 | t_p.relname AS parenttable, 51 | unnest(( 52 | select 53 | array_agg(attname ORDER BY i)cs-mode Refreshing completions... 54 | ^ 55 | HINT: See: https://github.com/cockroachdb/cockroach/issues/23620 56 | 57 | > 58 | ``` 59 | 60 | ## Sample database 61 | 62 | Let's set up [sample database](https://stackoverflow.com/questions/5363613/sample-database-for-postgresql). 63 | 64 | There are a lot of them (but not all work out of the box): 65 | 66 | - https://github.com/devrimgunduz/pagila 67 | - https://github.com/catherinedevlin/opensourceshakespeare 68 | - http://pgfoundry.org/projects/dbsamples/ 69 | - https://musicbrainz.org/doc/MusicBrainz_Database/Download 70 | - http://wiki.postgresql.org/wiki/Sample_Databases 71 | 72 | I picked up [opensourceshakespeare](https://github.com/catherinedevlin/opensourceshakespeare). 73 | 74 | ``` 75 | docker-compose up -d 76 | Starting db-experiment_db_1 ... done 77 | docker exec -it db-experiment_db_1 ./cockroach sql --insecure 78 | root@:26257/defaultdb> IMPORT PGDUMP 'https://raw.githubusercontent.com/stereobooster/opensourceshakespeare/master/shakespeare.sql'; 79 | ``` 80 | 81 | ## Autogenerated API 82 | 83 | ### Postgraphile 84 | 85 | Let's add Postgraphile. To do this we can use [official docker image](https://hub.docker.com/r/graphile/postgraphile/). But first, let's try it locally 86 | 87 | ``` 88 | npx postgraphile -n 0.0.0.0 --connection "postgresql://root@localhost:26257/defaultdb?sslmode=disable" --schema defaultdb --watch 89 | 90 | A fatal error occurred when building the initial schema, so the process will now exit. Error details: 91 | 92 | error: syntax error at or near "operator" 93 | at Connection.parseE (/postgraphile/node_modules/pg/lib/connection.js:601:11) 94 | at Connection.parseMessage (/postgraphile/node_modules/pg/lib/connection.js:398:19) 95 | at Socket. (/postgraphile/node_modules/pg/lib/connection.js:120:22) 96 | at Socket.emit (events.js:182:13) 97 | at addChunk (_stream_readable.js:283:12) 98 | at readableAddChunk (_stream_readable.js:264:11) 99 | at Socket.Readable.push (_stream_readable.js:219:10) 100 | at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) 101 | ``` 102 | 103 | Doesn't work :(. Need to try with PostgreSQL. 104 | 105 | ### Hasura 106 | 107 | Let's add [Hasura section to the docker compose](https://docs.hasura.io/1.0/graphql/manual/deployment/docker/index.html#step-1-get-the-docker-run-sh-bash-script). 108 | 109 | ``` 110 | docker-compose up 111 | ... 112 | hasura_1 | {"internal":{"statement":"SET LOCAL \"hasura.user\" = '{\"x-hasura-role\":\"admin\"}'","prepared":false,"error":{"exec_status":"FatalError","hint":"See: https://github.com/cockroachdb/cockroach/issues/32562","message":"unimplemented at or near \"hasura.user\"","status_code":"0A000","description":"source SQL:\nSET LOCAL \"hasura.user\" = '{\"x-hasura-role\":\"admin\"}'\n ^"},"arguments":[]},"path":"$","error":"postgres query error","code":"postgres-error"} 113 | ``` 114 | 115 | Doesn't work :(. Need to try with PostgreSQL. 116 | 117 | ## Conclusion 118 | 119 | At this moment it is not possible to use CockroachDB with some tools which assume PostgreSQL backend. 120 | --------------------------------------------------------------------------------