Created
February 22, 2019 09:32
-
-
Save jonasholtkamp/a1909ecedad5f16ec4dbbe9f0c8007da to your computer and use it in GitHub Desktop.
Accompanying code snippet for https://github.com/nicdex/node-eventstore-client/issues/65
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const esClient = require('node-eventstore-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' | |
] | |
} | |
const response = await axios.post('/users/', payload, callOptions) | |
if (response.status !== 201) throw new Error(`Status Code: ${response.status}`) | |
} | |
const setDefaultAclMetadata = async () => { | |
const callOptions = { | |
baseURL: `http://localhost:2113`, | |
headers: { | |
'ES-EventType': 'settings', | |
'ES-EventId': uuid() | |
}, | |
auth: { | |
username: 'admin', | |
password: 'changeit' | |
} | |
} | |
const payload = { | |
'$userStreamAcl': { | |
'$r': '$admins', | |
'$w': '$admins', | |
'$d': '$admins', | |
'$mr': '$all', | |
'$mw': '$all' | |
}, | |
'$systemStreamAcl': { | |
'$r': '$admins', | |
'$w': '$admins', | |
'$d': '$admins', | |
'$mr': '$admins', | |
'$mw': '$admins' | |
} | |
} | |
const response = await axios.post(`/streams/$settings`, payload, callOptions) | |
if (response.status !== 201) throw new Error(`Status Code: ${response.status}`) | |
} | |
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 (stream) => { | |
await createEventStoreUser() | |
const connectionSettings = { | |
defaultUserCredentials: new esClient.UserCredentials('testuser', 'testuser'), | |
defaultNumberOfEventsPerSlice: 10, | |
useSslConnection: 'true', | |
maxReconnections: '10', | |
reconnectionDelay: '10000', | |
heartbeatInterval: 5000, | |
heartbeatTimeout: 5000 | |
} | |
const esConnection = esClient.createConnection(connectionSettings, 'tcp://localhost:1115') | |
await new Promise((resolve) => { | |
esConnection.once('connected', resolve) | |
esConnection.connect() | |
}) | |
await setDefaultAclMetadata() | |
const event = () => esClient.createJsonEventData( | |
uuid(), | |
{ thisIs: 'payload' }, | |
{ thisIs: 'metadata' }, | |
'TestType' | |
) | |
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}`) | |
} | |
} | |
} | |
return esConnection.close() | |
} | |
const loop = async () => { | |
for (i = 0; i <= 10000;) { | |
console.log(`Run ${i++}`) | |
await run(`test-${uuid()}`) | |
} | |
} | |
loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment