Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Postgres data source - set up scripts #29

Merged
merged 5 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,37 @@ npm install

### Start and Initialize the Database

Use docker compose to start the database.
Use docker compose to start the database(s).

```
docker-compose up
docker-compose -p aerogeardatasyncserver up
```

There are 2 Postgres instances defined in docker-compose configuration:
1. For storing the configuration of the sync server itself
2. For storing the [Memeolist](#whats-memeolist) data.

Since docker-compose is only used with development, starting up the Postgres instance for [Memeolist](#whats-memeolist)
will not cause any harm.

Initialize the database in another terminal.

```
# no sample schema/resolvers
npm run db:init

# sample schema/resolvers for memeolist - in-memory data source
npm run db:init:memeo:inmem

# sample schema/resolvers for memeolist - Postgres data source
npm run db:init:memeo:postgres
```

`npm run db:init` sets up the necessary tables and seeds the database with data useful for local development. **It is a destructive action.** It drops and recreates the tables every time.
`npm run db:init*` commands set up the necessary tables. **Those are destructive actions.**
They drop and recreate the tables every time.

`npm run db:init:memeo:*` commands are useful for local development which and seed the database with config and tables
for [Memeolist](#whats-memeolist) sample application.

### Start the Server

Expand All @@ -50,7 +67,7 @@ npm run db:shell
The Postgres container started by `docker-compose` can be stopped with `Ctrl + C`. To remove it fully:

```
docker-compose rm
docker-compose -p aerogeardatasyncserver rm

Going to remove aerogeardatasyncserver_postgres_1
Are you sure? [yN] y
Expand All @@ -66,7 +83,7 @@ npm run test:unit

Start the database first:
```
docker-compose up
docker-compose -p aerogeardatasyncserver up
```

Then, in a separate session, init the database (blank) and start the application:
Expand Down Expand Up @@ -165,8 +182,28 @@ The baseline architecture is shown below:

## Memeolist

To start the application with MemeoList schema and queries run
### What's Memeolist?

Memeolist is an application where AeroGear team targets testing AeroGear mobile services and SDKs on it.

You can see the specification for it here: https://github.com/aerogear/proposals/blob/master/dogfood.md

There is some tooling adjusted to create Memeolist app's backend within the project.

### Memeolist - In memory

To start the application with MemeoList schema and queries with an in-memory data source, run these commands:
```
docker-compose -p aerogeardatasyncserver up
npm run db:init:memeo:inmem
npm run dev:memeo
```

### Memeolist - Postgres

To start the application with MemeoList schema and queries with an Postgres source, run these commands:
```
npm run db:init:memeo
docker-compose -p aerogeardatasyncserver up
npm run db:init:memeo:postgres
npm run dev:memeo
```
```
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ services:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgresql
POSTGRES_DB: aerogear_data_sync_db

postgres_memeo:
image: postgres:9.6
ports:
- "15432:5432"
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgresql
POSTGRES_DB: memeolist_db
volumes:
- ./examples:/tmp/examples
6 changes: 6 additions & 0 deletions examples/memeolist.tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS "Meme";

CREATE TABLE "Meme" (
"id" SERIAL NOT NULL,
"photoUrl" CHARACTER VARYING(500) NOT NULL
);
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
"docker:push:release": "./scripts/docker_push_release.sh",
"release:validate": "./scripts/validateRelease.sh",
"db:init": "FORCE_DROP=true node ./scripts/sync_models",
"db:init:memeo": "FORCE_DROP=true node ./scripts/sync_models && sequelize db:seed --seed 20180711143600-memeolist-example.js",
"db:shell": "docker exec -it aerogeardatasyncserver_postgres_1 psql -U postgresql -d aerogear_data_sync_db"
"db:init:memeo:inmem": "FORCE_DROP=true node ./scripts/sync_models && sequelize db:seed --seed memeolist-example-inmem.js",
"db:init:memeo:postgres": "FORCE_DROP=true node ./scripts/sync_models && sequelize db:seed --seed memeolist-example-postgres.js && docker exec aerogeardatasyncserver_postgres_memeo_1 psql -U postgres -d memeolist_db -f /tmp/examples/memeolist.tables.sql",
"db:shell": "docker exec -it aerogeardatasyncserver_postgres_1 psql -U postgresql -d aerogear_data_sync_db",
"db:shell:memeo": "docker exec -it aerogeardatasyncserver_postgres_memeo_1 psql -U postgresql -d memeolist_db"
},
"devDependencies": {
"apollo-fetch": "^0.7.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'

const {schema} = require('./memeolist-example-shared')

const time = new Date()

const datasources = [
{
id: 2,
id: 1,
name: 'nedb_memeolist',
type: 'InMemory',
config: '{"options":{"timestampData":true}}',
Expand All @@ -13,39 +15,12 @@ const datasources = [
}
]

const notesSchema = {
id: 2,
name: 'default',
schema: `

type Meme {
id: ID! @isUnique
photoUrl: String!
}

type Query {
allMemes:[Meme!]!
}

type Mutation {
createMeme(photoUrl: String!):Meme!
}

type Subscription {
_: Boolean
}

`,
createdAt: time,
updatedAt: time
}

const resolvers = [
{
type: 'Query',
field: 'allMemes',
DataSourceId: 2,
GraphQLSchemaId: 2,
DataSourceId: 1,
GraphQLSchemaId: 1,
requestMapping: '{"operation": "find", "query": {"_type":"meme"}}',
responseMapping: '{{ toJSON (convertNeDBIds context.result) }}',
createdAt: time,
Expand All @@ -54,8 +29,8 @@ const resolvers = [
{
type: 'Mutation',
field: 'createMeme',
DataSourceId: 2,
GraphQLSchemaId: 2,
DataSourceId: 1,
GraphQLSchemaId: 1,
requestMapping: `{
"operation": "insert",
"doc": {
Expand All @@ -72,7 +47,7 @@ const resolvers = [
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('DataSources', datasources, {})
await queryInterface.bulkInsert('GraphQLSchemas', [notesSchema], {})
await queryInterface.bulkInsert('GraphQLSchemas', [schema], {})
return queryInterface.bulkInsert('Resolvers', resolvers, {})
}
}
54 changes: 54 additions & 0 deletions sequelize/seeders/memeolist-example-postgres.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict'

const {schema} = require('./memeolist-example-shared')

const time = new Date()

const datasources = [
{
id: 1,
name: 'nedb_postgres',
type: 'Postgres',
config: `{"options":{
"user": "postgresql",
"password": "postgres",
"database": "memeolist_db",
"host": "127.0.0.1",
"port": "15432",
"dialect": "postgres"
}}`,
createdAt: time,
updatedAt: time
}
]

const resolvers = [
{
type: 'Query',
field: 'allMemes',
DataSourceId: 1,
GraphQLSchemaId: 1,
requestMapping: 'SELECT "id", "photoUrl" FROM "Meme"',
responseMapping: '{{ toJSON context.result }}',
createdAt: time,
updatedAt: time
},
{
type: 'Mutation',
field: 'createMeme',
DataSourceId: 1,
GraphQLSchemaId: 1,
requestMapping: `INSERT INTO "Meme" ("photoUrl") VALUES ('{{context.arguments.photoUrl}}') RETURNING *;`,
responseMapping: '{{ toJSON context.result.[0] }}',
createdAt: time,
updatedAt: time
}
]

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('DataSources', datasources, {})
await queryInterface.bulkInsert('GraphQLSchemas', [schema], {})
return queryInterface.bulkInsert('Resolvers', resolvers, {})
}
}
34 changes: 34 additions & 0 deletions sequelize/seeders/memeolist-example-shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const time = new Date()

const memeoListSchema = {
id: 1,
name: 'default',
schema: `

type Meme {
id: ID! @isUnique
photoUrl: String!
}

type Query {
allMemes:[Meme!]!
}

type Mutation {
createMeme(photoUrl: String!):Meme!
}

type Subscription {
_: Boolean
}

`,
createdAt: time,
updatedAt: time
}

module.exports = {
schema: memeoListSchema
}
2 changes: 0 additions & 2 deletions server/lib/resolvers/builders/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function buildPostgresResolver (dataSourceClient, compiledRequestMapping, compil

dataSourceClient.query(queryString, [], (err, res) => {
if (err) return reject(err)
// TODO: should we end the connection with each request?
// dataSourceClient.end()

const responseString = compiledResponseMapping({
context: {
Expand Down