From f17242b8245c4edc52262aad26a489962144de29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Mon, 18 Feb 2019 12:56:38 +0100 Subject: [PATCH] Transfer relevant changes from branch `neode` --- .travis.yml | 2 ++ README.md | 22 +++++++++++++--------- docker-compose.override.yml | 7 +++++++ docker-compose.prod.yml | 9 +++++++++ docker-compose.travis.yml | 6 ++++++ docker-compose.yml | 7 ------- neo4j/Dockerfile | 1 + neo4j/migrate.sh | 11 +++++++++++ src/bootstrap/neo4j.js | 2 +- src/middleware/slugify/uniqueSlug.js | 15 +++++++++++++++ src/middleware/slugify/uniqueSlug.spec.js | 18 ++++++++++++++++++ 11 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 docker-compose.prod.yml create mode 100755 neo4j/migrate.sh create mode 100644 src/middleware/slugify/uniqueSlug.js create mode 100644 src/middleware/slugify/uniqueSlug.spec.js diff --git a/.travis.yml b/.travis.yml index f4a01b147..e699197cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,10 +16,12 @@ before_install: - curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose - chmod +x docker-compose - sudo mv docker-compose /usr/local/bin + - yarn global add wait-on install: - docker build --build-arg BUILD_COMMIT=$TRAVIS_COMMIT --target production -t humanconnection/nitro-backend:latest . - docker-compose -f docker-compose.yml -f docker-compose.travis.yml up -d + - wait-on http://localhost:7474 && docker-compose exec neo4j migrate script: - docker-compose exec backend yarn run lint diff --git a/README.md b/README.md index 1b12562d2..7f4676a6c 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,15 @@ > This Prototype tries to resolve the biggest hurdle of connecting > our services together. This is not possible in a sane way using -> our current approach. -> -> With this Prototype we can explore using the combination of +> our current approach. +> +> With this Prototype we can explore using the combination of > GraphQL and the Neo4j Graph Database for achieving the connected > nature of a social graph with better development experience as we > do not need to connect data by our own any more through weird table > structures etc. -> +> > #### Advantages: > - easer data structure > - better connected data @@ -19,10 +19,10 @@ > - more performant and better to understand API > - better API client that uses caching > -> We still need to evaluate the drawbacks and estimate the development +> We still need to evaluate the drawbacks and estimate the development > cost of such an approach -## How to get in touch +## How to get in touch Connect with other developers over [Discord](https://discord.gg/6ub73U3) ## Quick Start @@ -35,6 +35,10 @@ Before you start, fork the repository using the fork button above, then clone it Run: ```sh docker-compose up + +# create indices etc. +docker-compose exec neo4j migrate + # if you want seed data # open another terminal and run docker-compose exec backend yarn run db:seed @@ -94,7 +98,7 @@ _.env_ ```yaml NEO4J_URI=bolt://localhost:7687 -NEO4J_USER=neo4j +NEO4J_USERNAME=neo4j NEO4J_PASSWORD=letmein ``` @@ -116,7 +120,7 @@ Just set `MOCK=true` inside `.env` or pass it on application start. ## Seed and Reset the Database -Optionally you can seed the GraphQL service by executing mutations that +Optionally you can seed the GraphQL service by executing mutations that will write sample data to the database: ```bash @@ -152,5 +156,5 @@ npm run test - [x] check if sorting is working - [x] check if pagination is working - [ ] check if upload is working (using graphql-yoga?) -- [x] evaluate middleware +- [x] evaluate middleware - [ ] ignore Posts and Comments by blacklisted Users diff --git a/docker-compose.override.yml b/docker-compose.override.yml index ef7d52c7e..b972c31f6 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -11,6 +11,13 @@ services: - /nitro-backend/node_modules command: yarn run dev neo4j: + environment: + - NEO4J_AUTH=none ports: - 7687:7687 - 7474:7474 + volumes: + - neo4j-data:/data + +volumes: + neo4j-data: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 000000000..c4f5dc4f5 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,9 @@ +version: "3.7" + +services: + neo4j: + environment: + - NEO4J_PASSWORD=letmein + backend: + environment: + - NEO4J_PASSWORD=letmein diff --git a/docker-compose.travis.yml b/docker-compose.travis.yml index 761a2aa64..e1998f6dd 100644 --- a/docker-compose.travis.yml +++ b/docker-compose.travis.yml @@ -1,6 +1,12 @@ version: "3.7" services: + neo4j: + environment: + - NEO4J_AUTH=none + ports: + - 7687:7687 + - 7474:7474 backend: image: humanconnection/nitro-backend:builder build: diff --git a/docker-compose.yml b/docker-compose.yml index 6905bb893..1e8c9158c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,14 +27,7 @@ services: context: neo4j networks: - hc-network - volumes: - - neo4j-data:/data - environment: - - NEO4J_AUTH=none networks: hc-network: name: hc-network - -volumes: - neo4j-data: diff --git a/neo4j/Dockerfile b/neo4j/Dockerfile index cb7fd228f..f6e71811b 100644 --- a/neo4j/Dockerfile +++ b/neo4j/Dockerfile @@ -1,2 +1,3 @@ FROM neo4j:3.5.0 RUN wget https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/3.5.0.1/apoc-3.5.0.1-all.jar -P plugins/ +COPY migrate.sh /usr/local/bin/migrate diff --git a/neo4j/migrate.sh b/neo4j/migrate.sh new file mode 100755 index 000000000..c659909fa --- /dev/null +++ b/neo4j/migrate.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -e +if [[ -z "${NEO4J_PASSWORD}" ]]; then + echo 'CALL db.index.fulltext.createNodeIndex("full_text_search",["Post"],["title", "content"]);' | cypher-shell +else + echo "CALL dbms.security.changePassword('${NEO4J_PASSWORD}');" | cypher-shell --username neo4j --password neo4j + echo "CREATE CONSTRAINT ON (p:Post) ASSERT p.slug IS UNIQUE;" | cypher-shell + echo "CREATE CONSTRAINT ON (c:Category) ASSERT c.slug IS UNIQUE;" | cypher-shell + echo "CREATE CONSTRAINT ON (u:User) ASSERT u.slug IS UNIQUE;" | cypher-shell + echo "CREATE CONSTRAINT ON (o:Organization) ASSERT o.slug IS UNIQUE;" | cypher-shell +fi diff --git a/src/bootstrap/neo4j.js b/src/bootstrap/neo4j.js index 766c12065..929e62f23 100644 --- a/src/bootstrap/neo4j.js +++ b/src/bootstrap/neo4j.js @@ -9,7 +9,7 @@ export default function () { driver = neo4j.driver( process.env.NEO4J_URI || 'bolt://localhost:7687', neo4j.auth.basic( - process.env.NEO4J_USER || 'neo4j', + process.env.NEO4J_USERNAME || 'neo4j', process.env.NEO4J_PASSWORD || 'neo4j' ) ) diff --git a/src/middleware/slugify/uniqueSlug.js b/src/middleware/slugify/uniqueSlug.js new file mode 100644 index 000000000..0f5b264a7 --- /dev/null +++ b/src/middleware/slugify/uniqueSlug.js @@ -0,0 +1,15 @@ +import slugify from 'slug' +export default function uniqueSlug(string, isUnique) { + let slug = slugify(string, { + lower: true + }) + if (isUnique(slug)) return slug; + + let count = 0 + let uniqueSlug + do { + count += 1 + uniqueSlug = `${slug}-${count}` + } while(!isUnique(uniqueSlug)); + return uniqueSlug; +} diff --git a/src/middleware/slugify/uniqueSlug.spec.js b/src/middleware/slugify/uniqueSlug.spec.js new file mode 100644 index 000000000..3d379bafb --- /dev/null +++ b/src/middleware/slugify/uniqueSlug.spec.js @@ -0,0 +1,18 @@ +import uniqueSlug from './uniqueSlug' + +describe('uniqueSlug', () => { + it('slugifies given string', () => { + const string = 'Hello World' + const isUnique = () => true + expect(uniqueSlug(string, isUnique)).toEqual('hello-world') + }) + + it('increments slugified string until unique', () => { + const string = 'Hello World' + const isUnique = jest.fn() + isUnique + .mockReturnValueOnce(false) + .mockReturnValueOnce(true) + expect(uniqueSlug(string, isUnique)).toEqual('hello-world-1') + }) +})