Last active
February 9, 2019 17:41
-
-
Save jonasholtkamp/055de46f83eac8a11f66388d23e63207 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 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' | |
] | |
} | |
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 (stream) => { | |
await createEventStoreUser() | |
const connectionSettings = { | |
defaultUserCredentials: new esClient.UserCredentials('testuser', 'testuser'), | |
port: '1115', | |
defaultNumberOfEventsPerSlice: 10, | |
useSslConnection: 'true', | |
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:1115') | |
await new Promise((resolve) => { | |
esConnection.once('connected', resolve) | |
esConnection.connect() | |
console.log('Connected') | |
}) | |
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() | |
} | |
run(`test-${uuid()}`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment