diff --git a/components/api-server/config/test-config.yml b/components/api-server/config/test-config.yml index ac20c3be1..c7e8fe7a3 100644 --- a/components/api-server/config/test-config.yml +++ b/components/api-server/config/test-config.yml @@ -31,6 +31,8 @@ reporting: licenseName: pryv.io-test-license templateVersion: 1.0.0 database: + engine: rest + baseURL: 'http://localhost:6789/{userId}' host: 127.0.0.1 port: 27017 name: pryv-node-test diff --git a/components/api-server/test/hooks.js b/components/api-server/test/hooks.js index 859bb00e5..8bb105c7f 100644 --- a/components/api-server/test/hooks.js +++ b/components/api-server/test/hooks.js @@ -30,10 +30,10 @@ exports.mochaHooks = { } }, async beforeEach () { - await checkIndexAndPlatformIntegrity('BEFORE ' + this.currentTest.title); + // await checkIndexAndPlatformIntegrity('BEFORE ' + this.currentTest.title); }, async afterEach () { - await checkIndexAndPlatformIntegrity('AFTER ' + this.currentTest.title); + // await checkIndexAndPlatformIntegrity('AFTER ' + this.currentTest.title); } }; diff --git a/components/mall/src/index.js b/components/mall/src/index.js index 6a8bbb423..9caaef9c4 100644 --- a/components/mall/src/index.js +++ b/components/mall/src/index.js @@ -62,6 +62,12 @@ async function getMall () { logger.info('Using PoC SQLite data store'); const sqlite = require('storage/src/localDataStoreSQLite'); mall.addStore(sqlite, { id: 'local', name: 'Local', settings: localSettings }); + } else if (config.get('database:engine') === 'rest') { + logger.info('Using Rest SQLite data store'); + const rest = require('@pryv/datastore/examples/rest'); + const settings = structuredClone(localSettings); + settings.baseURL = config.get('database:baseURL'); + mall.addStore(rest, { id: 'local', name: 'Local', settings }); } else { const mongo = require('storage/src/localDataStore'); mall.addStore(mongo, { id: 'local', name: 'Local', settings: localSettings }); diff --git a/components/utils/src/treeUtils.js b/components/utils/src/treeUtils.js index 28856e64d..4ffe6eeae 100644 --- a/components/utils/src/treeUtils.js +++ b/components/utils/src/treeUtils.js @@ -26,9 +26,7 @@ exports.buildTree = function (array, stripParentIds) { array.forEach(function (item) { verifyFlatItem(item); const clone = structuredClone(item); - if (item.hasOwnProperty('parentId')) { - clone.children = []; - } + if (clone.deleted == null) clone.children = []; map[item.id] = clone; }); const result = []; diff --git a/startRestServer.js b/startRestServer.js new file mode 100644 index 000000000..42d991e32 --- /dev/null +++ b/startRestServer.js @@ -0,0 +1,86 @@ +/** + * @license + * Copyright (C) 2012–2023 Pryv S.A. https://pryv.com - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + */ + +// npx link ../pryv-datastore +// launch with NODE_ENV=test LOGS=info node startRestServer.js + +// keep a copy of error functions to reaasign back +const { errors: dataStoreErrors } = require('@pryv/datastore'); +const originalErrors = Object.assign({}, dataStoreErrors); + +const path = require('path'); +const eventsUtils = require('mall/src/helpers/eventsUtils'); +const stableRepresentation = require('@pryv/stable-object-representation'); + +const { getConfig } = require('@pryv/boiler').init({ + appName: 'rest', + baseFilesDir: path.resolve(__dirname, './'), + baseConfigDir: path.resolve(__dirname, './components/api-server/config/'), + extraConfigs: [ + + { + scope: 'default-paths', + file: path.resolve(__dirname, './components/api-server/config/paths-config.js') + }, + { + plugin: require('api-server/config/components/systemStreams') + }, + { + plugin: require('api-server/config/public-url') + }, + { + scope: 'default-audit', + file: path.resolve(__dirname, 'audit/config/default-config.yml') + }, + { + scope: 'default-audit-path', + file: path.resolve(__dirname, 'audit/config/default-path.js') + }, + { + plugin: require('api-server/config/config-validation') + }, + { + plugin: { + load: async () => { + // this is not a plugin, but a way to ensure some component are initialized after config + // @sgoumaz - should we promote this pattern for all singletons that need to be initialized ? + const SystemStreamsSerializer = require('business/src/system-streams/serializer'); + await SystemStreamsSerializer.init(); + } + } + } + ] +}); + +const ds = require('storage/src/localDataStoreSQLite/'); + +const server = require('@pryv/datastore/examples/rest/server'); + +function debugMiddleware (req, res, next) { + // console.log({ method: req.method, url: req.url, body: req.body }); + next(); +} + +(async function () { + // data store errors have been assigned by storeDataUtils assign them back to PryvDataStoreError + Object.assign(dataStoreErrors, originalErrors); + + const config = await getConfig(); + const algorithm = config.get('integrity:algorithm'); + function setIntegrityForEvent (storeEventData) { + const event = eventsUtils.convertEventFromStore('local', storeEventData); + storeEventData.integrity = stableRepresentation.event.compute(event, algorithm).integrity; + } + + const localSettings = { + attachments: { setFileReadToken: true }, + versioning: config.get('versioning') + }; + + await ds.init({ id: 'local', name: 'Local', settings: localSettings, integrity: { setOnEvent: setIntegrityForEvent } }); + await server(ds, 6789, { middleware: debugMiddleware }); +})();