diff --git a/examples/hacker-news/config.app.js b/examples/hacker-news/config.app.js index f87a45f3cf..3fd2a5aa53 100644 --- a/examples/hacker-news/config.app.js +++ b/examples/hacker-news/config.app.js @@ -26,7 +26,8 @@ const appConfig = { { name: 'HackerNews', projection: 'common/read-models/hacker-news.projection.js', - resolvers: 'common/read-models/hacker-news.resolvers.js' + resolvers: 'common/read-models/hacker-news.resolvers.js', + adapterName: 'default' } ] } diff --git a/examples/hacker-news/config.dev.js b/examples/hacker-news/config.dev.js index cda94922b4..baa0edd827 100644 --- a/examples/hacker-news/config.dev.js +++ b/examples/hacker-news/config.dev.js @@ -2,12 +2,13 @@ const devConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - HackerNews: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], jwtCookie: { name: 'jwt', maxAge: 31536000000 diff --git a/examples/hacker-news/config.prod.js b/examples/hacker-news/config.prod.js index 1795c16539..5189dc1aaf 100644 --- a/examples/hacker-news/config.prod.js +++ b/examples/hacker-news/config.prod.js @@ -2,12 +2,13 @@ const prodConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'production', - readModelAdapters: { - HackerNews: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], jwtCookie: { name: 'jwt', maxAge: 31536000000 diff --git a/examples/hacker-news/config.test-functional.js b/examples/hacker-news/config.test-functional.js index 4cc46e688b..378e3747ed 100644 --- a/examples/hacker-news/config.test-functional.js +++ b/examples/hacker-news/config.test-functional.js @@ -2,12 +2,13 @@ const testFunctionalConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - HackerNews: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], jwtCookie: { name: 'jwt', maxAge: 31536000000 diff --git a/examples/hello-world/config.app.js b/examples/hello-world/config.app.js index c8de124d45..f41ce9c360 100644 --- a/examples/hello-world/config.app.js +++ b/examples/hello-world/config.app.js @@ -11,7 +11,8 @@ const appConfig = { { name: 'read-model-name', projection: 'common/read-models/read-model-name.projection.js', - resolvers: 'common/read-models/read-model-name.resolvers.js' + resolvers: 'common/read-models/read-model-name.resolvers.js', + adapterName: 'default' } ], viewModels: [ diff --git a/examples/hello-world/config.dev.js b/examples/hello-world/config.dev.js index c767a68d78..4909157595 100644 --- a/examples/hello-world/config.dev.js +++ b/examples/hello-world/config.dev.js @@ -7,12 +7,13 @@ const devConfig = { staticDir: 'static', distDir: 'dist', - readModelAdapters: { - 'read-model-name': { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], storageAdapter: { module: 'resolve-storage-lite', options: { diff --git a/examples/hello-world/config.prod.js b/examples/hello-world/config.prod.js index 1ed6cb8d70..c78e79b5d9 100644 --- a/examples/hello-world/config.prod.js +++ b/examples/hello-world/config.prod.js @@ -7,12 +7,13 @@ const prodConfig = { staticDir: 'static', distDir: 'dist', - readModelAdapters: { - 'read-model-name': { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], storageAdapter: { module: 'resolve-storage-lite', options: { diff --git a/examples/hello-world/config.test_functional.js b/examples/hello-world/config.test_functional.js index 1e5273e536..8abd1bc671 100644 --- a/examples/hello-world/config.test_functional.js +++ b/examples/hello-world/config.test_functional.js @@ -7,12 +7,13 @@ const testFunctionalConfig = { staticDir: 'static', distDir: 'dist', - readModelAdapters: { - 'read-model-name': { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], storageAdapter: { module: 'resolve-storage-lite', options: { diff --git a/examples/shopping-list-advanced/config.app.js b/examples/shopping-list-advanced/config.app.js index 10ca82fe78..8e263a1bb3 100644 --- a/examples/shopping-list-advanced/config.app.js +++ b/examples/shopping-list-advanced/config.app.js @@ -36,7 +36,8 @@ const appConfig = { { name: 'ShoppingLists', projection: 'domain/read-models/shopping_lists.projection.js', - resolvers: 'domain/read-models/shopping_lists.resolvers.js' + resolvers: 'domain/read-models/shopping_lists.resolvers.js', + adapterName: 'default' } ], jwtCookie: { diff --git a/examples/shopping-list-advanced/config.dev.js b/examples/shopping-list-advanced/config.dev.js index cf0e6086bb..e1fccfeb72 100644 --- a/examples/shopping-list-advanced/config.dev.js +++ b/examples/shopping-list-advanced/config.dev.js @@ -2,12 +2,13 @@ const devConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - ShoppingLists: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - } + ] } export default devConfig diff --git a/examples/shopping-list-advanced/config.prod.js b/examples/shopping-list-advanced/config.prod.js index 6cc38dd0a0..6438b18262 100644 --- a/examples/shopping-list-advanced/config.prod.js +++ b/examples/shopping-list-advanced/config.prod.js @@ -2,12 +2,13 @@ const prodConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'production', - readModelAdapters: { - ShoppingLists: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - } + ] } export default prodConfig diff --git a/examples/shopping-list-advanced/config.test_functional.js b/examples/shopping-list-advanced/config.test_functional.js index 5f2ae9da93..95bdf2cc53 100644 --- a/examples/shopping-list-advanced/config.test_functional.js +++ b/examples/shopping-list-advanced/config.test_functional.js @@ -3,12 +3,13 @@ const testFunctionalConfig = { polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - ShoppingLists: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], storageAdapter: { module: 'resolve-storage-lite', options: {} diff --git a/examples/shopping-list/config.app.js b/examples/shopping-list/config.app.js index de4b88d95d..eb684d7966 100644 --- a/examples/shopping-list/config.app.js +++ b/examples/shopping-list/config.app.js @@ -26,7 +26,8 @@ const appConfig = { { name: 'ShoppingLists', projection: 'common/read-models/shopping_lists.projection.js', - resolvers: 'common/read-models/shopping_lists.resolvers.js' + resolvers: 'common/read-models/shopping_lists.resolvers.js', + adapterName: 'default' } ], apiHandlers: [ diff --git a/examples/shopping-list/config.dev.js b/examples/shopping-list/config.dev.js index cf0e6086bb..e1fccfeb72 100644 --- a/examples/shopping-list/config.dev.js +++ b/examples/shopping-list/config.dev.js @@ -2,12 +2,13 @@ const devConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - ShoppingLists: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - } + ] } export default devConfig diff --git a/examples/shopping-list/config.prod.js b/examples/shopping-list/config.prod.js index 6cc38dd0a0..6438b18262 100644 --- a/examples/shopping-list/config.prod.js +++ b/examples/shopping-list/config.prod.js @@ -2,12 +2,13 @@ const prodConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'production', - readModelAdapters: { - ShoppingLists: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - } + ] } export default prodConfig diff --git a/examples/shopping-list/config.test_functional.js b/examples/shopping-list/config.test_functional.js index 5f2ae9da93..95bdf2cc53 100644 --- a/examples/shopping-list/config.test_functional.js +++ b/examples/shopping-list/config.test_functional.js @@ -3,12 +3,13 @@ const testFunctionalConfig = { polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - ShoppingLists: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], storageAdapter: { module: 'resolve-storage-lite', options: {} diff --git a/examples/with-saga/config.app.js b/examples/with-saga/config.app.js index b9d5a992d2..7971c3b381 100644 --- a/examples/with-saga/config.app.js +++ b/examples/with-saga/config.app.js @@ -4,7 +4,8 @@ const appConfig = { { name: 'default', projection: 'common/read-models/default.projection.js', - resolvers: 'common/read-models/default.resolvers.js' + resolvers: 'common/read-models/default.resolvers.js', + adapterName: 'default' } ], viewModels: [ diff --git a/examples/with-saga/config.dev.js b/examples/with-saga/config.dev.js index 85166ac902..e1fccfeb72 100644 --- a/examples/with-saga/config.dev.js +++ b/examples/with-saga/config.dev.js @@ -2,12 +2,13 @@ const devConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - default: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - } + ] } export default devConfig diff --git a/examples/with-saga/config.prod.js b/examples/with-saga/config.prod.js index 7b4133a740..6438b18262 100644 --- a/examples/with-saga/config.prod.js +++ b/examples/with-saga/config.prod.js @@ -2,12 +2,13 @@ const prodConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'production', - readModelAdapters: { - default: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - } + ] } export default prodConfig diff --git a/examples/with-saga/config.test_functional.js b/examples/with-saga/config.test_functional.js index eff8aec84c..900fb576ac 100644 --- a/examples/with-saga/config.test_functional.js +++ b/examples/with-saga/config.test_functional.js @@ -2,12 +2,13 @@ const testFunctionalConfig = { port: 3000, polyfills: ['@babel/polyfill'], mode: 'development', - readModelAdapters: { - default: { + readModelAdapters: [ + { + name: 'default', module: 'resolve-readmodel-memory', options: {} } - }, + ], storageAdapter: { module: 'resolve-storage-lite', options: {} diff --git a/packages/core/resolve-query/src/constants.js b/packages/core/resolve-query/src/constants.js index 0d853d16df..f5c05acc04 100644 --- a/packages/core/resolve-query/src/constants.js +++ b/packages/core/resolve-query/src/constants.js @@ -6,5 +6,6 @@ export const modelTypes = { export const errors = { duplicateName: 'A read/view name is not unique', modelNotFound: 'A read/view model is not defined', + missingAdapter: 'Read model adapter not found', disposed: 'A query is disposed' } diff --git a/packages/core/resolve-query/src/query/create-query.js b/packages/core/resolve-query/src/query/create-query.js index d68167e3b1..2776c81ea0 100644 --- a/packages/core/resolve-query/src/query/create-query.js +++ b/packages/core/resolve-query/src/query/create-query.js @@ -13,7 +13,13 @@ const createQuery = ( getDeserializer, dispose, getExecutors, - { eventStore, viewModels, readModels, snapshotAdapter } + { + eventStore, + viewModels, + readModels, + snapshotAdapter, + readModelAdaptersCreators + } ) => { const repository = { executors: new Map(), @@ -26,7 +32,12 @@ const createQuery = ( getExecutor } - initReadModels({ ...repository, eventStore, readModels, snapshotAdapter }) + initReadModels({ + ...repository, + eventStore, + readModels, + readModelAdaptersCreators + }) initViewModels({ ...repository, eventStore, viewModels, snapshotAdapter }) checkInitErrors(repository) diff --git a/packages/core/resolve-query/src/query/init-read-models.js b/packages/core/resolve-query/src/query/init-read-models.js index 3ed56a9f8b..ca9850807d 100644 --- a/packages/core/resolve-query/src/query/init-read-models.js +++ b/packages/core/resolve-query/src/query/init-read-models.js @@ -6,6 +6,7 @@ const initReadModels = ({ executorTypes, errorMessages, eventStore, + readModelAdaptersCreators, readModels }) => { for (const readModel of readModels) { @@ -13,10 +14,23 @@ const initReadModels = ({ errorMessages.push(`${errors.duplicateName} ${readModel}`) } + const adapterCreator = readModelAdaptersCreators.find( + ({ name }) => name === readModel.adapterName + ) + if (adapterCreator == null) { + throw new Error( + `${errors.missingAdapter} ${readModel.name}: ${readModel.adapterName}` + ) + } + + const adapter = adapterCreator.factory({ + metaName: `__ResolveMeta__${readModel.name}` + }) + const executor = createReadModel({ projection: readModel.projection, resolvers: readModel.resolvers, - adapter: readModel.adapter(), + adapter, eventStore }) diff --git a/packages/core/resolve-runtime/src/cloud_entry.js b/packages/core/resolve-runtime/src/cloud_entry.js index 5c2840c171..ba3e3b7eb5 100644 --- a/packages/core/resolve-runtime/src/cloud_entry.js +++ b/packages/core/resolve-runtime/src/cloud_entry.js @@ -15,14 +15,14 @@ import mainHandler from './handlers/main_handler' const initResolve = async ( { snapshotAdapter: createSnapshotAdapter, - storageAdapter: createStorageAdapter + storageAdapter: createStorageAdapter, + readModelAdapters: readModelAdaptersCreators }, resolve ) => { const storageAdapter = createStorageAdapter() const eventStore = createEventStore({ storage: storageAdapter }) const { aggregates, readModels, viewModels } = resolve - const snapshotAdapter = createSnapshotAdapter() const executeCommand = createCommandExecutor({ @@ -35,6 +35,7 @@ const initResolve = async ( eventStore, viewModels, readModels, + readModelAdaptersCreators, snapshotAdapter }) diff --git a/packages/core/resolve-runtime/src/local_entry.js b/packages/core/resolve-runtime/src/local_entry.js index a91eaccdfc..1e35466ebc 100644 --- a/packages/core/resolve-runtime/src/local_entry.js +++ b/packages/core/resolve-runtime/src/local_entry.js @@ -219,7 +219,10 @@ const emptyWorker = async () => { } const initDomain = async ( - { snapshotAdapter: createSnapshotAdapter }, + { + snapshotAdapter: createSnapshotAdapter, + readModelAdapters: readModelAdaptersCreators + }, resolve ) => { const { eventStore, aggregates, readModels, viewModels } = resolve @@ -235,6 +238,7 @@ const initDomain = async ( eventStore, viewModels, readModels, + readModelAdaptersCreators, snapshotAdapter }) diff --git a/packages/core/resolve-scripts/configs/default.resolve.config.json b/packages/core/resolve-scripts/configs/default.resolve.config.json index f251d9f871..9fde2ccd03 100644 --- a/packages/core/resolve-scripts/configs/default.resolve.config.json +++ b/packages/core/resolve-scripts/configs/default.resolve.config.json @@ -37,7 +37,13 @@ "module": "resolve-snapshot-lite", "options": {} }, - "readModelAdapters": {}, + "readModelAdapters": [ + { + "name": "default", + "module": "resolve-readmodel-memory", + "options": {} + } + ], "jwtCookie": { "name": "jwt", "maxAge": 31536000000 diff --git a/packages/core/resolve-scripts/configs/schema.resolve.config.json b/packages/core/resolve-scripts/configs/schema.resolve.config.json index 20d566a7c3..906d0d4dc8 100644 --- a/packages/core/resolve-scripts/configs/schema.resolve.config.json +++ b/packages/core/resolve-scripts/configs/schema.resolve.config.json @@ -17,11 +17,21 @@ "required": ["module", "options"], "additionalProperties": false }, - "adaptersMap": { + "readModelAdapter": { "type": "object", - "additionalProperties": { - "$ref": "#/definitions/adapter" - } + "properties": { + "name": { + "type": "string" + }, + "module": { + "type": "string" + }, + "options": { + "type": "object" + } + }, + "required": ["name", "module", "options"], + "additionalProperties": false }, "resourceInstance": { "type": "string" @@ -49,16 +59,7 @@ "type": "string" }, "options": { - "type": "object", - "properties": { - "client": { - "type": "object" - }, - "server": { - "type": "object" - } - }, - "additionalProperties": false + "type": "object" } }, "required": ["module", "options"], @@ -141,9 +142,12 @@ { "$ref": "#/definitions/resourceInstance" }, { "$ref": "#/definitions/resourceConstructor" } ] + }, + "adapterName": { + "type": "string" } }, - "required": ["name", "projection", "resolvers"], + "required": ["name", "projection", "resolvers", "adapterName"], "additionalProperties": false }, "saga": { @@ -316,6 +320,12 @@ "$ref": "#/definitions/apiHandler" } }, + "readModelAdapters": { + "type": "array", + "items": { + "$ref": "#/definitions/readModelAdapter" + } + }, "busAdapter": { "$ref": "#/definitions/adapter" }, @@ -325,9 +335,6 @@ "subscribeAdapter": { "$ref": "#/definitions/subscribeAdapter" }, - "readModelAdapters": { - "$ref": "#/definitions/adaptersMap" - }, "snapshotAdapter": { "$ref": "#/definitions/adapter" }, diff --git a/packages/core/resolve-scripts/src/alias/$resolve.readModelAdapters.js b/packages/core/resolve-scripts/src/alias/$resolve.readModelAdapters.js new file mode 100644 index 0000000000..74eac353ce --- /dev/null +++ b/packages/core/resolve-scripts/src/alias/$resolve.readModelAdapters.js @@ -0,0 +1,58 @@ +import { + message, + RESOURCE_CONSTRUCTOR_ONLY, + RUNTIME_ENV_ANYWHERE, + IMPORT_CONSTRUCTOR +} from '../constants' +import { checkRuntimeEnv } from '../declare_runtime_env' +import importResource from '../import_resource' + +export default ({ resolveConfig, isClient }) => { + if (!resolveConfig.readModelAdapters) { + throw new Error(`${message.configNotContainSectionError}.readModelAdapters`) + } + if (isClient) { + throw new Error(`${message.serverAliasInClientCodeError}.readModelAdapters`) + } + + const imports = [] + const constants = [``] + const exports = [`const readModelAdapters = []`] + + for (let index = 0; index < resolveConfig.readModelAdapters.length; index++) { + const readModelAdapter = resolveConfig.readModelAdapters[index] + + if (checkRuntimeEnv(readModelAdapter.name)) { + throw new Error( + `${message.clientEnvError}.readModelAdapters[${index}].name` + ) + } + constants.push( + `const name_${index} = ${JSON.stringify(readModelAdapter.name)}` + ) + + importResource({ + resourceName: `factory_${index}`, + resourceValue: { + module: readModelAdapter.module, + options: readModelAdapter.options + }, + runtimeMode: RUNTIME_ENV_ANYWHERE, + importMode: RESOURCE_CONSTRUCTOR_ONLY, + instanceMode: IMPORT_CONSTRUCTOR, + imports, + constants + }) + + exports.push(`readModelAdapters.push({ + name: name_${index}, + factory: factory_${index} + })`) + } + + exports.push(`export default readModelAdapters`) + + return { + code: [...imports, ...constants, ...exports].join('\r\n') + } +} diff --git a/packages/core/resolve-scripts/src/alias/$resolve.readModels.js b/packages/core/resolve-scripts/src/alias/$resolve.readModels.js index 34cac102a2..541b1fd095 100644 --- a/packages/core/resolve-scripts/src/alias/$resolve.readModels.js +++ b/packages/core/resolve-scripts/src/alias/$resolve.readModels.js @@ -2,9 +2,6 @@ import { message, RUNTIME_ENV_NOWHERE, RESOURCE_ANY, - RESOURCE_CONSTRUCTOR_ONLY, - RUNTIME_ENV_ANYWHERE, - IMPORT_CONSTRUCTOR, IMPORT_INSTANCE } from '../constants' import importBabel from '../import_babel' @@ -30,6 +27,15 @@ export default ({ resolveConfig, isClient }) => { } constants.push(`const name_${index} = ${JSON.stringify(readModel.name)}`) + if (checkRuntimeEnv(readModel.adapterName)) { + throw new Error( + `${message.clientEnvError}.readModels[${index}].adapterName` + ) + } + constants.push( + `const adapterName_${index} = ${JSON.stringify(readModel.adapterName)}` + ) + importResource({ resourceName: `resolvers_${index}`, resourceValue: readModel.resolvers, @@ -69,24 +75,12 @@ export default ({ resolveConfig, isClient }) => { ) } - const readModelAdapter = - resolveConfig.hasOwnProperty('readModelAdapters') && - resolveConfig.readModelAdapters.hasOwnProperty(readModel.name) - ? resolveConfig.readModelAdapters[readModel.name] - : null - - if (!readModelAdapter) { - throw new Error( - `${message.configNotContainSectionError}.readModelAdapters[${ - readModel.name - }]` - ) - } - exports.push(`readModels.push({`, ` name: name_${index}`) exports.push(`, resolvers: resolvers_${index}`) if (!isClient) { + exports.push(`, adapterName: adapterName_${index}`) + importResource({ resourceName: `projection_${index}`, resourceValue: readModel.projection, @@ -99,18 +93,6 @@ export default ({ resolveConfig, isClient }) => { }) exports.push(`, projection: projection_${index}`) exports.push(`, invariantHash: projection_${index}_hash`) - - importResource({ - resourceName: `read_model_adapter_${index}`, - resourceValue: readModelAdapter, - runtimeMode: RUNTIME_ENV_ANYWHERE, - importMode: RESOURCE_CONSTRUCTOR_ONLY, - instanceMode: IMPORT_CONSTRUCTOR, - imports, - constants - }) - - exports.push(`, adapter: read_model_adapter_${index}`) } exports.push(`})`, ``) } diff --git a/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.assemblies.test.js.snap b/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.assemblies.test.js.snap index 3591d8b6a4..7728ec7a53 100644 --- a/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.assemblies.test.js.snap +++ b/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.assemblies.test.js.snap @@ -6,6 +6,7 @@ Object { import busAdapter from '$resolve.busAdapter' import constants from '$resolve.constants' import customConstants from '$resolve.customConstants' +import readModelAdapters from '$resolve.readModelAdapters' import redux from '$resolve.redux' import routes from '$resolve.routes' import seedClientEnvs from '$resolve.seedClientEnvs' @@ -18,6 +19,7 @@ export default { busAdapter, constants, customConstants, +readModelAdapters, redux, routes, seedClientEnvs, diff --git a/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.readModels.test.js.snap b/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.readModels.test.js.snap index 280304871d..e914a93634 100644 --- a/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.readModels.test.js.snap +++ b/packages/core/resolve-scripts/test/alias/__snapshots__/$resolve.readModels.test.js.snap @@ -4,6 +4,7 @@ exports[`base config works correctly [client] 1`] = ` " const name_0 = \\"Todos\\" +const adapterName_0 = undefined const resolvers_0 = { todos() {}, items() {} @@ -24,7 +25,6 @@ exports[`base config works correctly [server] 1`] = ` " import resolvers_0_instance from \\"/core/resolve-scripts/test/alias/files/testResolvers.js\\" import projection_0_instance from \\"/core/resolve-scripts/test/alias/files/testProjection.js\\" -import read_model_adapter_0_constructor from \\"resolve-readmodel-memory\\" const constructorBindFunction = ( constructorFunction, @@ -39,27 +39,19 @@ const constructorBindFunction = const idempotentFunction = value => value const name_0 = \\"Todos\\" +const adapterName_0 = undefined const resolvers_0 = resolvers_0_instance const projection_0_hash = \\"e111237317f05b862c7ea099ea1311369f25a01c6ce12768f20cd638ccb3ceec273e155dcbe328fed29eecc553502a19b162f0194f507b2a8a64f3dbc01b5322\\" const projection_0 = projection_0_instance -const read_model_adapter_0_imports = { } -const read_model_adapter_0_options = {} -const read_model_adapter_0 = constructorBindFunction.bind(null, - read_model_adapter_0_constructor, - read_model_adapter_0_options, - read_model_adapter_0_imports - ) - Object.assign(read_model_adapter_0, read_model_adapter_0_constructor) - const readModels = [] readModels.push({ name: name_0 , resolvers: resolvers_0 +, adapterName: adapterName_0 , projection: projection_0 , invariantHash: projection_0_hash -, adapter: read_model_adapter_0 }) export default readModels @@ -70,11 +62,13 @@ exports[`base(v2) config works correctly [client] 1`] = ` " const name_0 = \\"Todos\\" +const adapterName_0 = undefined const resolvers_0 = { todos() {}, items() {} } const name_1 = \\"Items\\" +const adapterName_1 = undefined const resolvers_1 = { todos() {}, items() {} @@ -100,10 +94,8 @@ exports[`base(v2) config works correctly [server] 1`] = ` " import resolvers_0_instance from \\"/core/resolve-scripts/test/alias/files/testResolvers.js\\" import projection_0_instance from \\"/core/resolve-scripts/test/alias/files/testProjection.js\\" -import read_model_adapter_0_constructor from \\"resolve-readmodel-memory\\" import resolvers_1_instance from \\"/core/resolve-scripts/test/alias/files/testResolvers.js\\" import projection_1_instance from \\"/core/resolve-scripts/test/alias/files/testProjection.js\\" -import read_model_adapter_1_constructor from \\"resolve-readmodel-memory\\" const constructorBindFunction = ( constructorFunction, @@ -118,48 +110,32 @@ const constructorBindFunction = const idempotentFunction = value => value const name_0 = \\"Todos\\" +const adapterName_0 = undefined const resolvers_0 = resolvers_0_instance const projection_0_hash = \\"e111237317f05b862c7ea099ea1311369f25a01c6ce12768f20cd638ccb3ceec273e155dcbe328fed29eecc553502a19b162f0194f507b2a8a64f3dbc01b5322\\" const projection_0 = projection_0_instance -const read_model_adapter_0_imports = { } -const read_model_adapter_0_options = {} -const read_model_adapter_0 = constructorBindFunction.bind(null, - read_model_adapter_0_constructor, - read_model_adapter_0_options, - read_model_adapter_0_imports - ) - Object.assign(read_model_adapter_0, read_model_adapter_0_constructor) - const name_1 = \\"Items\\" +const adapterName_1 = undefined const resolvers_1 = resolvers_1_instance const projection_1_hash = \\"e111237317f05b862c7ea099ea1311369f25a01c6ce12768f20cd638ccb3ceec273e155dcbe328fed29eecc553502a19b162f0194f507b2a8a64f3dbc01b5322\\" const projection_1 = projection_1_instance -const read_model_adapter_1_imports = { } -const read_model_adapter_1_options = {} -const read_model_adapter_1 = constructorBindFunction.bind(null, - read_model_adapter_1_constructor, - read_model_adapter_1_options, - read_model_adapter_1_imports - ) - Object.assign(read_model_adapter_1, read_model_adapter_1_constructor) - const readModels = [] readModels.push({ name: name_0 , resolvers: resolvers_0 +, adapterName: adapterName_0 , projection: projection_0 , invariantHash: projection_0_hash -, adapter: read_model_adapter_0 }) readModels.push({ name: name_1 , resolvers: resolvers_1 +, adapterName: adapterName_1 , projection: projection_1 , invariantHash: projection_1_hash -, adapter: read_model_adapter_1 }) export default readModels @@ -170,6 +146,7 @@ exports[`config + process.env works correctly [client] 1`] = ` " const name_0 = \\"Todos\\" +const adapterName_0 = undefined const resolvers_0 = { todos() {}, items() {} @@ -188,7 +165,6 @@ export default readModels exports[`config + process.env works correctly [server] 1`] = ` " -import interopRequireDefault from \\"@babel/runtime/helpers/interopRequireDefault\\" import resolvers_0_instance from \\"/core/resolve-scripts/test/alias/files/testResolvers.js\\" import projection_0_instance from \\"/core/resolve-scripts/test/alias/files/testProjection.js\\" const constructorBindFunction = @@ -205,32 +181,19 @@ const constructorBindFunction = const idempotentFunction = value => value const name_0 = \\"Todos\\" +const adapterName_0 = undefined const resolvers_0 = resolvers_0_instance const projection_0_hash = \\"e111237317f05b862c7ea099ea1311369f25a01c6ce12768f20cd638ccb3ceec273e155dcbe328fed29eecc553502a19b162f0194f507b2a8a64f3dbc01b5322\\" const projection_0 = projection_0_instance -const read_model_adapter_0_constructor = ((moduleOrFile) => { - return interopRequireDefault(__non_webpack_require__(moduleOrFile)).default -})(process.env[\\"READ_MODEL_TODOS_ADAPTER\\"]) -const read_model_adapter_0_imports = { } -const read_model_adapter_0_options = { - \\"size\\": process.env[\\"READ_MODEL_TODOS_OPTIONS_SIZE\\"] -} -const read_model_adapter_0 = constructorBindFunction.bind(null, - read_model_adapter_0_constructor, - read_model_adapter_0_options, - read_model_adapter_0_imports - ) - Object.assign(read_model_adapter_0, read_model_adapter_0_constructor) - const readModels = [] readModels.push({ name: name_0 , resolvers: resolvers_0 +, adapterName: adapterName_0 , projection: projection_0 , invariantHash: projection_0_hash -, adapter: read_model_adapter_0 }) export default readModels diff --git a/packages/core/resolve-testing-tools/src/create-read-model.js b/packages/core/resolve-testing-tools/src/create-read-model.js index 10668dd0b2..fc15f889fb 100644 --- a/packages/core/resolve-testing-tools/src/create-read-model.js +++ b/packages/core/resolve-testing-tools/src/create-read-model.js @@ -10,7 +10,7 @@ const createReadModel = ( applyEvent, applyEvents }, - { name, projection, resolvers, adapter = createReadModelAdapter } + { name, projection, resolvers, adapterName } ) => { const storage = createStorageAdapter() const bus = createBusAdapter() @@ -20,12 +20,18 @@ const createReadModel = ( const query = createQuery({ eventStore, viewModels: [], + readModelAdaptersCreators: [ + { + name: adapterName, + factory: createReadModelAdapter + } + ], readModels: [ { name, + adapterName, projection, - resolvers, - adapter + resolvers } ] }) @@ -37,7 +43,6 @@ const createReadModel = ( modelName: name, projection, resolvers, - adapter, storage, bus, query, diff --git a/packages/core/resolve-testing-tools/test/create-read-model.test.js b/packages/core/resolve-testing-tools/test/create-read-model.test.js index c96e22d8e4..7abd65a9a8 100644 --- a/packages/core/resolve-testing-tools/test/create-read-model.test.js +++ b/packages/core/resolve-testing-tools/test/create-read-model.test.js @@ -20,15 +20,13 @@ test('method "createReadModel" should create read model { resolvers, applyEvent, const name = 'name' const projection = { Init: () => {} } const resolvers = { test: async () => 42 } - const adapter = { - /* adapter */ - } + const adapterName = 'default' const createQuery = sinon.stub().returns(query) const createStorageAdapter = sinon.stub().returns(storage) const createBusAdapter = sinon.stub().returns(bus) const createEventStore = sinon.stub().returns(eventStore) - const createReadModelAdapter = sinon.stub().returns(adapter) + const createReadModelAdapter = sinon.stub() const createResolver = sinon.stub() const createResolvers = sinon.stub().returns(resolvers) const applyEvent = sinon.stub() @@ -46,7 +44,7 @@ test('method "createReadModel" should create read model { resolvers, applyEvent, applyEvent, applyEvents }, - { name, projection, resolvers, adapter } + { name, projection, resolvers, adapterName } ) const pool = { @@ -56,7 +54,6 @@ test('method "createReadModel" should create read model { resolvers, applyEvent, modelName: name, projection, resolvers, - adapter, storage, bus, query, diff --git a/packages/modules/resolve-module-comments/src/common/defaults.js b/packages/modules/resolve-module-comments/src/common/defaults.js index ba1d669141..4facd60b8a 100644 --- a/packages/modules/resolve-module-comments/src/common/defaults.js +++ b/packages/modules/resolve-module-comments/src/common/defaults.js @@ -18,10 +18,7 @@ export const aggregateName = 'Comments' export const readModelName = 'Comments' // Read Model Adapter -export const readModelAdapter = { - module: 'resolve-readmodel-memory', - options: {} -} +export const readModelAdapterName = 'default' // Comments Table Name export const commentsTableName = 'Comments' diff --git a/packages/modules/resolve-module-comments/src/common/index.js b/packages/modules/resolve-module-comments/src/common/index.js index e3cab6061a..dfb34cb512 100644 --- a/packages/modules/resolve-module-comments/src/common/index.js +++ b/packages/modules/resolve-module-comments/src/common/index.js @@ -68,6 +68,7 @@ export default ({ readModels: [ { name: options.readModelName, + adapterName: options.readModelAdapterName, projection: { module: 'resolve-module-comments/lib/common/read-models/comments.projection.js', @@ -82,9 +83,6 @@ export default ({ } } ], - readModelAdapters: { - [options.readModelName]: options.readModelAdapter - }, redux: { reducers: { [options.reducerName]: { diff --git a/packages/modules/resolve-module-comments/src/common/inject-defaults.js b/packages/modules/resolve-module-comments/src/common/inject-defaults.js index 51dd3ea49b..40d6591105 100644 --- a/packages/modules/resolve-module-comments/src/common/inject-defaults.js +++ b/packages/modules/resolve-module-comments/src/common/inject-defaults.js @@ -5,7 +5,7 @@ const injectDefaults = callback => ( { aggregateName = defaults.aggregateName, readModelName = defaults.readModelName, - readModelAdapter = defaults.readModelAdapter, + readModelAdapterName = defaults.readModelAdapterName, commentsTableName = defaults.commentsTableName, reducerName = defaults.reducerName, eventTypes: { @@ -32,7 +32,7 @@ const injectDefaults = callback => ( { aggregateName, readModelName, - readModelAdapter, + readModelAdapterName, commentsTableName, reducerName, eventTypes: {