Skip to content

Commit

Permalink
Normalize read-model Store API. Add functional tests. (#1837)
Browse files Browse the repository at this point in the history
Fix #1782
Fix #1794
Fix #1740
Fix #1422
  • Loading branch information
MrCheater authored Apr 29, 2021
1 parent 77ce815 commit 9a925e1
Show file tree
Hide file tree
Showing 95 changed files with 4,634 additions and 1,441 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ module.exports = {
'resolver',
'resolvers',
'respawn',
'Retryable',
'rgba',
'rmdir',
'rollbacking',
Expand Down
16 changes: 8 additions & 8 deletions examples/shopping-list-with-hooks/test/unit/read_models.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists).toEqual([])
})
Expand All @@ -46,9 +46,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists[0]).toMatchObject({
id: aggregateId,
Expand Down Expand Up @@ -77,9 +77,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists[0]).toMatchObject({
id: aggregateId,
Expand All @@ -105,9 +105,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists.length).toEqual(0)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists).toEqual([])
})
Expand All @@ -47,9 +47,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists[0]).toMatchObject({
id: aggregateId,
Expand Down Expand Up @@ -79,9 +79,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists[0]).toMatchObject({
id: aggregateId,
Expand All @@ -107,9 +107,9 @@ describe('read-models', () => {
name: 'ShoppingLists',
projection,
resolvers,
adapter,
})
.all()
.withAdapter(adapter)
.query('all', {})

expect(shoppingLists.length).toEqual(0)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ import getReplicationState from './get-replication-state'
import resetReplication from './reset-replication'

import _createResource from './resource/create'
import _disposeResource from './resource/dispose'
import _destroyResource from './resource/destroy'

import type {
CloudResource,
CloudResourcePool,
CloudResourceOptions,
ConnectionDependencies,
Expand Down Expand Up @@ -132,22 +130,13 @@ const cloudPool: CloudResourcePool = {
}

const createResource = _createResource.bind(null, cloudPool)
const disposeResource = _disposeResource.bind(
null,
cloudPool as CloudResourcePool & CloudResource
)
const destroyResource = _destroyResource.bind(null, cloudPool)

Object.assign(cloudPool, {
createResource,
disposeResource,
destroyResource,
})

export {
createResource as create,
disposeResource as dispose,
destroyResource as destroy,
}
export { createResource as create, destroyResource as destroy }

export type { PostgresqlAdapterConfig, CloudResourcePool, CloudResourceOptions }

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export type ConnectionDependencies = {

export type CloudResource = {
createResource: (options: CloudResourceOptions) => Promise<any>
disposeResource: (options: CloudResourceOptions) => Promise<any>
destroyResource: (options: CloudResourceOptions) => Promise<any>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ import connect from '../src/connect'
import dispose from '../src/dispose'

import createResource from '../src/resource/create'
import disposeResource from '../src/resource/dispose'
import destroyResource from '../src/resource/destroy'

import {
create as exportedCreate,
destroy as exportedDestroy,
dispose as exportedDispose,
} from '../src'
import { create as exportedCreate, destroy as exportedDestroy } from '../src'
import {
CloudResource,
CloudResourceOptions,
Expand All @@ -37,11 +32,9 @@ jest.mock('../src/connect', () => jest.fn())
jest.mock('../src/dispose', () => jest.fn())

jest.mock('../src/resource/create', () => jest.fn())
jest.mock('../src/resource/dispose', () => jest.fn())
jest.mock('../src/resource/destroy', () => jest.fn())

const mCreateResource = mocked(createResource)
const mDisposeResource = mocked(disposeResource)
const mDestroyResource = mocked(destroyResource)

describe('as cloud resource', () => {
Expand Down Expand Up @@ -75,14 +68,12 @@ describe('as cloud resource', () => {
completePool = {
...cloudPool,
createResource: exportedCreate,
disposeResource: exportedDispose,
destroyResource: exportedDestroy,
}
})

afterEach(() => {
mCreateResource.mockClear()
mDisposeResource.mockClear()
mDestroyResource.mockClear()
})

Expand All @@ -95,9 +86,4 @@ describe('as cloud resource', () => {
exportedDestroy(options)
expect(mDestroyResource).toHaveBeenCalledWith(completePool, options)
})

test('dispose', () => {
exportedDispose(options)
expect(mDisposeResource).toHaveBeenCalledWith(completePool, options)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"@babel/runtime": "7.9.6",
"@resolve-js/eventstore-base": "0.30.0",
"path-toolkit": "1.2.1",
"uuid": "8.3.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ const createAdapter = <
options: AdapterOptions
): AdapterApi<AdapterPool> => {
const {
makeSplitNestedPath,
withPerformanceTracer,
wrapConnect,
wrapDisconnect,
wrapDispose,
wrapOperation,
} = imports
const splitNestedPath = makeSplitNestedPath(imports)

const {
connect,
Expand Down Expand Up @@ -95,7 +97,7 @@ const createAdapter = <
const { performanceTracer, monitoring, ...adapterOptions } = options

const pool: BaseAdapterPool<AdapterPool> = {
commonAdapterPool: { performanceTracer, monitoring },
commonAdapterPool: { performanceTracer, splitNestedPath, monitoring },
adapterPoolMap: new Map(),
withPerformanceTracer,
performanceTracer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
//eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import PathToolkit from 'path-toolkit'

import _createAdapter from './create-adapter'
import makeSplitNestedPath from './make-split-nested-path'
import withPerformanceTracer from './with-performance-tracer'
import wrapConnect from './wrap-connect'
import wrapDisconnect from './wrap-disconnect'
Expand All @@ -8,6 +13,8 @@ import { CreateAdapterMethod } from './types'
export * from './types'

const createAdapter = _createAdapter.bind(null, {
PathToolkit,
makeSplitNestedPath,
withPerformanceTracer,
wrapConnect,
wrapDisconnect,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MakeSplitNestedPathMethod, SplitNestedPathMethod } from './types'

const makeSplitNestedPath: MakeSplitNestedPathMethod = (imports) => {
const pathToolkit = new imports.PathToolkit()
pathToolkit.setOptions({
cache: false,
force: false,
simple: false,
defaultReturnVal: undefined,
separators: {
'.': {
exec: 'property',
},
},
prefixes: {},
containers: {
"'": {
closer: "'",
exec: `${'single'}${'quote'}`,
},
'"': {
closer: '"',
exec: `${'double'}${'quote'}`,
},
},
})

const splitNestedPath: SplitNestedPathMethod = (input) => {
if (input == null || input.constructor !== String) {
throw new Error(`Invalid json path ${input}: must be string`)
}
const output = pathToolkit.getTokens(input)
if (output == null) {
throw new Error(`Invalid json path ${input}: parse failed`)
}

return output.t
}

return splitNestedPath
}

export default makeSplitNestedPath
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ export type EventstoreAdapterLike = {
gatherSecretsFromEvents: EventStoreAdapter['gatherSecretsFromEvents']
}

export type SplitNestedPathMethod = (input: string) => Array<string>

export type CommonAdapterPool = {
monitoring?: MonitoringLike
performanceTracer?: PerformanceTracerLike
splitNestedPath: SplitNestedPathMethod
}

export type CommonAdapterOptions = {
Expand Down Expand Up @@ -407,7 +410,24 @@ export type WrapOperationMethod = <
...args: WrappedAdapterOperationParameters<AdapterPool, MethodImpl>
) => ReturnType<MethodImpl>

export type PathToolkitLibInstance = {
getTokens: (
input: string
) => { t: Array<string>; simple: number | boolean } | null | undefined
setOptions: (options: any) => void
}

export type PathToolkitLib = {
new (): PathToolkitLibInstance
}

export type MakeSplitNestedPathMethod = (
imports: BaseAdapterImports
) => SplitNestedPathMethod

export type BaseAdapterImports = {
PathToolkit: PathToolkitLib
makeSplitNestedPath: MakeSplitNestedPathMethod
withPerformanceTracer: WithPerformanceTracerMethod
wrapConnect: WrapConnectMethod
wrapDisconnect: WrapDisconnectMethod
Expand Down
Loading

0 comments on commit 9a925e1

Please sign in to comment.