Skip to content

Commit

Permalink
message-store: postgres: update getStream
Browse files Browse the repository at this point in the history
add support for sql condition
  • Loading branch information
sid-360 committed Jul 27, 2020
1 parent 944d87f commit 9e939d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion message-store/postgres/get/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports.createGetStream = (options) => {
streamName,
options.position,
options.batchSize,
null
options.condition
]

const parameters = '$1::varchar, $2::bigint, $3::bigint, $4::varchar'
Expand Down
30 changes: 30 additions & 0 deletions message-store/postgres/test/message-store-postgres.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ describe('message-store-postgres', () => {
})
})

describe('getStream', () => {
describe('sql condition', () => {
describe('when not provided', () => {
it('parameter defaults to null, returns all results', async () => {
const log = createTestLog()
const messageStore = createMessageStore({ log })
const { streamName } = await examplePut(messageStore, { count: 3 })

const results = await messageStore.getStream(streamName)

expect(results.length).toBe(3)
})
})

describe('when provided', () => {
it('limits the results based on given sql condition', async () => {
const log = createTestLog()
const messageStore = createMessageStore({ log })
const { streamName, messages } = await examplePut(messageStore, { count: 3, trackMessages: true })

const CONDITION_SQL = 'messages.position = 1'
const results = await messageStore.getStream(streamName, { condition: CONDITION_SQL })

expect(results.length).toBe(1)
expect(results[0]).toMatchObject(messages[1])
})
})
})
})

describe('get-last', () => {
describe('connection error', () => {
it('propagates error', async () => {
Expand Down
6 changes: 6 additions & 0 deletions test/postgres-automated-test-schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- configurations

ALTER DATABASE message_store SET message_store.sql_condition TO on;

-- automated_tests

CREATE TABLE IF NOT EXISTS message_store.automated_tests (
id UUID NOT NULL,
time TIMESTAMP WITHOUT TIME ZONE DEFAULT (now() AT TIME ZONE 'utc') NOT NULL
Expand Down

0 comments on commit 9e939d7

Please sign in to comment.