Skip to content

Instantly share code, notes, and snippets.

@nicdex
Created February 10, 2019 21:03
Show Gist options
  • Save nicdex/42457c19294965bdddc80679083f32f5 to your computer and use it in GitHub Desktop.
Save nicdex/42457c19294965bdddc80679083f32f5 to your computer and use it in GitHub Desktop.
//const esClient = require('node-eventstore-client')
const esClient = require('../src/client')
const uuid = require('uuid/v4')
const axios = require('axios')
const Long = require('long')
const createEventStoreUser = async () => {
const callOptions = {
baseURL: `http://localhost:2113`,
timeout: 5000,
auth: {
username: 'admin',
password: 'changeit'
}
}
const payload = {
loginName: 'testuser',
password: 'testuser',
groups: [
'test-r',
'test-w',
'test-d',
'test-mr',
'test-mw'
]
}
await axios.post('/users/', payload, callOptions)
}
const setAcls = async (esConnection, stream, currentMetadata, expectedVersion) => {
return esConnection.setStreamMetadataRaw(stream, expectedVersion, {
...currentMetadata,
$acl: {
$w: `test-w`,
$r: `test-r`,
$d: `test-d`,
$mw: `test-mw`,
$mr: `test-mr`
}
})
}
const getStreamMetadata = async (esConnection, stream) => {
const metadataRaw = await esConnection.getStreamMetadataRaw(stream)
const acls = metadataRaw.streamMetadata['$acl']
const tb = metadataRaw.streamMetadata['$tb']
const lastEventNumber = new Long(metadataRaw.metastreamVersion.low, metadataRaw.metastreamVersion.high, metadataRaw.metastreamVersion.unsigned)
return { acls, tb, lastEventNumber, rawMetadata: metadataRaw }
}
const run = async () => {
await createEventStoreUser()
const connectionSettings = {
defaultUserCredentials: new esClient.UserCredentials('testuser', 'testuser'),
//port: '1115',
defaultNumberOfEventsPerSlice: 10,
//useSslConnection: 'false',
//verboseLogging: 'false',
maxReconnections: '10',
reconnectionDelay: '10000',
//targetHost: 'localhost',
//browser: 'http://localhost:2113/web/index.html#/streams',
//url: 'tcp://localhost:1115',
heartbeatInterval: 5000,
heartbeatTimeout: 5000
}
const esConnection = esClient.createConnection(connectionSettings, 'tcp://localhost:1113') //:1115')
await new Promise((resolve) => {
esConnection.once('connected', resolve)
esConnection.connect()
})
console.log('Connected')
const event = () => esClient.createJsonEventData(
uuid(),
{ thisIs: 'payload' },
{ thisIs: 'metadata' },
'TestType'
)
const test = async (stream) =>
{
console.log(`Working with stream ${stream}`)
await setAcls(esConnection, stream, {}, esClient.expectedVersion.noStream)
await esConnection.appendToStream(stream, esClient.expectedVersion.noStream, event())
console.log(`Read: ${(await esConnection.readStreamEventsBackward(stream, 0, 10, false)).status}`)
await esConnection.deleteStream(stream, 0, false)
try {
console.log(`Read: ${(await esConnection.readStreamEventsBackward(stream, 0, 10, false)).status}`)
} catch (e) {
if (e.name === 'AccessDeniedError') {
console.log('AccessDeniedError')
const {tb, lastEventNumber, rawMetadata} = await
getStreamMetadata(esConnection, stream)
const isSoftDeleted = tb && Long.MAX_VALUE.eq(tb)
if (isSoftDeleted) {
console.log('was soft-deleted')
await setAcls(esConnection, stream, rawMetadata.streamMetadata, lastEventNumber)
console.log(`Read: ${(await esConnection.readStreamEventsBackward(stream, 0, 10, false)).status}`)
}
}
}
const {tb, lastEventNumber, rawMetadata} = await
getStreamMetadata(esConnection, stream)
await setAcls(esConnection, stream, rawMetadata.streamMetadata, lastEventNumber)
console.log(`Read: ${(await esConnection.readStreamEventsBackward(stream, 0, 10, false)).status}`)
}
for(let i=0;i<1000;i++)
await test(`test-${uuid()}`)
return esConnection.close()
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment