Skip to content

Commit

Permalink
Update Neo4j README
Browse files Browse the repository at this point in the history
  • Loading branch information
ltwlf authored Aug 11, 2020
1 parent a84a23a commit 2064540
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
77 changes: 53 additions & 24 deletions src/modules/neo4j/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,57 @@ Let's test it!

## Examples

Start a neo4j container and create a person node
Test with jest to create a person node in neo4j

```typescript
import neo4j from "neo4j-driver";
import { Neo4jContainer } from "testcontainers";
import neo4j, { Driver } from 'neo4j-driver'
import { Neo4jContainer, StartedNeo4jContainer } from 'testcontainers'

const container = await new Neo4jContainer().start();
let container: StartedNeo4jContainer
let driver: Driver
let session: Session

const driver = neo4j.driver(
container.getBoltUri(),
neo4j.auth.basic(container.getUsername(), container.getPassword())
);
beforeEach(async () => {
container = await new Neo4jContainer().withApoc().start()
driver = neo4j.driver(
container.getBoltUri(),
neo4j.auth.basic(container.getUsername(), container.getPassword())
)
})

const session = driver.session();
const personName = "Chris";
afterEach(async () => {
await session?.close()
await driver?.close()
await container?.stop()
})

try {
const result = await session.run("CREATE (a:Person {name: $name}) RETURN a", { name: personName });
describe('neo4j', () => {
jest.setTimeout(120000)

const singleRecord = result.records[0];
const node = singleRecord.get(0);
test('connect', async () => {
driver = await db()
const serverInfo = await driver.verifyConnectivity()
expect(serverInfo).toBeDefined()
})

test('create person', async () => {
session = driver.session()
const personName = 'Chris'

const result = await session.run(
'CREATE (a:Person {name: $name}) RETURN a',
{
name: personName,
}
)

const singleRecord = result.records[0]
const node = singleRecord.get(0)

expect(node.properties.name).toBe(personName)
})
})

expect(node.properties.name).toBe(personName);
} finally {
await session.close();
await driver.close();
await container.stop();
}
```

Install APOC plugin:
Expand All @@ -41,8 +64,14 @@ Install APOC plugin:
container = await new Neo4jContainer().withApoc().start();
```

Set custom password:

With APOC, ttl and custom password (by default the password is a random uuid):
```typescript
container = await new Neo4jContainer().withApoc().start();
```
container = await new Neo4jContainer()
.withApoc()
.withTtl(5)
.withPassword('super_secret')
.start();
```
## Contact
You miss something?
Create an issue or write me on [twitter](https://twitter.com/ltwlf)
4 changes: 4 additions & 0 deletions src/modules/neo4j/neo4j-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class Neo4jContainer extends GenericContainer {
);
}

public withTtl(schedule = 5) {
this.withEnv("NEO4J_apoc_ttl_enabled", "true").withEnv("NEO4J_apoc_ttl_schedule", schedule.toString());
}

protected async preCreate(dockerClient: DockerClient, boundPorts: BoundPorts): Promise<void> {
this.withEnv("NEO4J_AUTH", `${this.defaultUsername}/${this.password}`);
}
Expand Down

0 comments on commit 2064540

Please sign in to comment.