From fdf36e7c1bfc6b422fcc59736c6662851cb0bf02 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 10:56:39 +0300 Subject: [PATCH 01/29] Rework model-getter helper --- workers/loc.api/sync/schema/helpers/index.js | 7 +------ workers/loc.api/sync/schema/models/index.js | 9 ++++----- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/workers/loc.api/sync/schema/helpers/index.js b/workers/loc.api/sync/schema/helpers/index.js index a38bd6c0..48d796ab 100644 --- a/workers/loc.api/sync/schema/helpers/index.js +++ b/workers/loc.api/sync/schema/helpers/index.js @@ -42,12 +42,7 @@ const getModelsMap = (params = {}) => { return cloneSchema(models, omittedFields) } -const getModelOf = (tableName, models) => { - return { ...getModelsMap({ models }).get(tableName) } -} - module.exports = { cloneSchema, - getModelsMap, - getModelOf + getModelsMap } diff --git a/workers/loc.api/sync/schema/models/index.js b/workers/loc.api/sync/schema/models/index.js index 31e2994a..b0758f2f 100644 --- a/workers/loc.api/sync/schema/models/index.js +++ b/workers/loc.api/sync/schema/models/index.js @@ -44,7 +44,7 @@ const progress = require('./progress') const syncQueue = require('./sync-queue') const syncUserSteps = require('./sync-user-steps') -const _models = new Map([ +const models = new Map([ [TABLES_NAMES.USERS, users], [TABLES_NAMES.SUB_ACCOUNTS, subAccounts], [TABLES_NAMES.LEDGERS, ledgers], @@ -80,19 +80,18 @@ const _models = new Map([ ]) const { - getModelsMap: _getModelsMap, - getModelOf: _getModelOf + getModelsMap: _getModelsMap } = require('../helpers') const getModelsMap = (params = {}) => { return _getModelsMap({ ...params, - models: params?.models ?? _models + models: params?.models ?? models }) } const getModelOf = (tableName) => { - return _getModelOf(tableName, _models) + return models.get(tableName) } module.exports = { From cde57c58de2aa3b09d6c822cda062e81364a66d6 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 11:19:34 +0300 Subject: [PATCH 02/29] Rework model-map-getter helper --- workers/loc.api/sync/schema/helpers/index.js | 24 +------------------- workers/loc.api/sync/schema/models/index.js | 11 ++------- 2 files changed, 3 insertions(+), 32 deletions(-) diff --git a/workers/loc.api/sync/schema/helpers/index.js b/workers/loc.api/sync/schema/helpers/index.js index 48d796ab..9e1e79d4 100644 --- a/workers/loc.api/sync/schema/helpers/index.js +++ b/workers/loc.api/sync/schema/helpers/index.js @@ -2,13 +2,6 @@ const { omit, cloneDeep } = require('lib-js-util-base') -const { - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME -} = require('../models/model/db.service.field.names') - const cloneSchema = (map, omittedFields = []) => { const arr = [...map].map(([key, schema]) => { const normalizedSchema = omit(schema, omittedFields) @@ -28,21 +21,6 @@ const cloneSchema = (map, omittedFields = []) => { return new Map(arr) } -const getModelsMap = (params = {}) => { - const { - models, - omittedFields = [ - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME - ] - } = { ...params } - - return cloneSchema(models, omittedFields) -} - module.exports = { - cloneSchema, - getModelsMap + cloneSchema } diff --git a/workers/loc.api/sync/schema/models/index.js b/workers/loc.api/sync/schema/models/index.js index b0758f2f..6731b7b6 100644 --- a/workers/loc.api/sync/schema/models/index.js +++ b/workers/loc.api/sync/schema/models/index.js @@ -79,15 +79,8 @@ const models = new Map([ [TABLES_NAMES.SYNC_USER_STEPS, syncUserSteps] ]) -const { - getModelsMap: _getModelsMap -} = require('../helpers') - -const getModelsMap = (params = {}) => { - return _getModelsMap({ - ...params, - models: params?.models ?? models - }) +const getModelsMap = () => { + return new Map(...models) } const getModelOf = (tableName) => { From e5eb89828f0d188483313d1b7223f086f16e916e Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 11:52:03 +0300 Subject: [PATCH 03/29] Rework movements service to use new model interface --- workers/loc.api/sync/movements/index.js | 14 ++++++++------ workers/loc.api/sync/schema/index.js | 6 ++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/workers/loc.api/sync/movements/index.js b/workers/loc.api/sync/movements/index.js index 2040c91f..95e3b0a5 100644 --- a/workers/loc.api/sync/movements/index.js +++ b/workers/loc.api/sync/movements/index.js @@ -25,10 +25,12 @@ class Movements { this.ALLOWED_COLLS = ALLOWED_COLLS this.authenticator = authenticator - this.movementsModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.MOVEMENTS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.movementsModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.MOVEMENTS) + .getModelFields() + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() } async getMovements (params = {}) { @@ -177,7 +179,7 @@ class Movements { end = Date.now(), filter: _filter, sort = [['mts', -1], ['id', -1]], - projection = this.ledgersModel, + projection = this.ledgersModelFields, exclude = ['user_id'], isExcludePrivate = true, isWithdrawals = false, @@ -227,7 +229,7 @@ class Movements { end = Date.now(), filter: _filter, sort = [['mts', -1], ['id', -1]], - projection = this.ledgersModel, + projection = this.ledgersModelFields, exclude = ['user_id'], isExcludePrivate = true, isWithdrawals = false, diff --git a/workers/loc.api/sync/schema/index.js b/workers/loc.api/sync/schema/index.js index 34ca4177..ad9cd499 100644 --- a/workers/loc.api/sync/schema/index.js +++ b/workers/loc.api/sync/schema/index.js @@ -2,7 +2,8 @@ const { SUPPORTED_DB_VERSION, - getModelsMap + getModelsMap, + getModelOf } = require('./models') const { getMethodCollMap @@ -11,5 +12,6 @@ const { module.exports = { SUPPORTED_DB_VERSION, getMethodCollMap, - getModelsMap + getModelsMap, + getModelOf } From 143bfbeaf212683d6893bfd7ab4a791809fb4cae Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 11:55:34 +0300 Subject: [PATCH 04/29] Rework performingLoan service to use new model interface --- workers/loc.api/sync/performing.loan/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/sync/performing.loan/index.js b/workers/loc.api/sync/performing.loan/index.js index 758c8dc7..50c04d73 100644 --- a/workers/loc.api/sync/performing.loan/index.js +++ b/workers/loc.api/sync/performing.loan/index.js @@ -35,8 +35,9 @@ class PerformingLoan { this.ledgersMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.LEDGERS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() } async _getLedgers ({ @@ -47,7 +48,7 @@ class PerformingLoan { filter = { $eq: { _isMarginFundingPayment: 1 } }, - projection = this.ledgersModel + projection = this.ledgersModelFields }) { const user = await this.authenticator .verifyRequestUser({ auth }) From 766a91f0c631474daac0a4e2b262d97e8abb4590 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 12:15:27 +0300 Subject: [PATCH 05/29] Rework positionsSnapshot service to use new model interface --- workers/loc.api/sync/positions.snapshot/index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/workers/loc.api/sync/positions.snapshot/index.js b/workers/loc.api/sync/positions.snapshot/index.js index 151d08d4..965afaa8 100644 --- a/workers/loc.api/sync/positions.snapshot/index.js +++ b/workers/loc.api/sync/positions.snapshot/index.js @@ -45,10 +45,12 @@ class PositionsSnapshot { this.currencyConverter = currencyConverter this.authenticator = authenticator - this.positionsHistoryModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.POSITIONS_HISTORY) - this.positionsSnapshotModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.POSITIONS_SNAPSHOT) + this.positionsHistoryModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.POSITIONS_HISTORY) + .getModelFields() + this.positionsSnapshotModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.POSITIONS_SNAPSHOT) + .getModelFields() this.positionsSnapshotMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.POSITIONS_SNAPSHOT) this.positionsSnapshotSymbolFieldName = this.positionsSnapshotMethodColl.symbolFieldName @@ -67,7 +69,7 @@ class PositionsSnapshot { $gte: { mtsUpdate: endMts } }, sort: [['mtsUpdate', -1]], - projection: this.positionsHistoryModel, + projection: this.positionsHistoryModelFields, exclude: ['user_id'], isExcludePrivate: true } @@ -105,7 +107,7 @@ class PositionsSnapshot { ...lteFilter }, sort: _sort, - projection: this.positionsSnapshotModel, + projection: this.positionsSnapshotModelFields, exclude: ['user_id'], isExcludePrivate: true } @@ -662,7 +664,7 @@ class PositionsSnapshot { $lte: { mtsUpdate: end } }, sort: [['mtsUpdate', -1], ['id', -1]], - projection: this.positionsHistoryModel, + projection: this.positionsHistoryModelFields, exclude: ['user_id'], isExcludePrivate: true } From 29ff9458b6272f0cb85126baf27deeb2062ea0fb Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 12:19:55 +0300 Subject: [PATCH 06/29] Rework summaryByAsset service to use new model interface --- workers/loc.api/sync/summary.by.asset/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/sync/summary.by.asset/index.js b/workers/loc.api/sync/summary.by.asset/index.js index 283fdb49..fa23c704 100644 --- a/workers/loc.api/sync/summary.by.asset/index.js +++ b/workers/loc.api/sync/summary.by.asset/index.js @@ -34,8 +34,9 @@ class SummaryByAsset { this.ledgersMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.LEDGERS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() this.ledgersSymbolFieldName = this.ledgersMethodColl.symbolFieldName } @@ -269,7 +270,7 @@ class SummaryByAsset { user_id: auth._id }, sort: [['mts', 1], ['id', 1]], - projection: this.ledgersModel + projection: this.ledgersModelFields } ) } From 8465b60829f8f5af395dce281cbc903254655691 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 12:22:21 +0300 Subject: [PATCH 07/29] Rework totalFeesReport service to use new model interface --- workers/loc.api/sync/total.fees.report/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/sync/total.fees.report/index.js b/workers/loc.api/sync/total.fees.report/index.js index f580d44c..d1e8b22b 100644 --- a/workers/loc.api/sync/total.fees.report/index.js +++ b/workers/loc.api/sync/total.fees.report/index.js @@ -38,8 +38,9 @@ class TotalFeesReport { this.ledgersMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.LEDGERS) - this.ledgersModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.LEDGERS) + this.ledgersModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.LEDGERS) + .getModelFields() } async getTotalFeesReport (args = {}) { @@ -100,7 +101,7 @@ class TotalFeesReport { end, symbol, filter, - projection = this.ledgersModel + projection = this.ledgersModelFields }) { const user = await this.authenticator .verifyRequestUser({ auth }) From ae9ccdb8e2908dc326ee4cdd0463fdf1e8912c59 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 12:25:41 +0300 Subject: [PATCH 08/29] Rework trades service to use new model interface --- workers/loc.api/sync/trades/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/sync/trades/index.js b/workers/loc.api/sync/trades/index.js index fc5bdac0..4d355d68 100644 --- a/workers/loc.api/sync/trades/index.js +++ b/workers/loc.api/sync/trades/index.js @@ -41,8 +41,9 @@ class Trades { this.tradesMethodColl = this.syncSchema.getMethodCollMap() .get(this.SYNC_API_METHODS.TRADES) - this.tradesModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.TRADES) + this.tradesModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.TRADES) + .getModelFields() } async _getTrades ({ @@ -71,7 +72,7 @@ class Trades { ...symbFilter }, sort: [['mtsCreate', -1]], - projection: this.tradesModel, + projection: this.tradesModelFields, exclude: ['user_id'], isExcludePrivate: true } From 5a0cde1b191701830627c77bfea1023b37920768 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 12:28:08 +0300 Subject: [PATCH 09/29] Rework transactionTaxReport service to use new model interface --- workers/loc.api/sync/transaction.tax.report/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/sync/transaction.tax.report/index.js b/workers/loc.api/sync/transaction.tax.report/index.js index 4b1da0f7..6817c7d2 100644 --- a/workers/loc.api/sync/transaction.tax.report/index.js +++ b/workers/loc.api/sync/transaction.tax.report/index.js @@ -70,8 +70,9 @@ class TransactionTaxReport { this.currencyConverter = currencyConverter this.processMessageManager = processMessageManager - this.tradesModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.TRADES) + this.tradesModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.TRADES) + .getModelFields() } async makeTrxTaxReportInBackground (args = {}) { @@ -442,7 +443,7 @@ class TransactionTaxReport { ...symbFilter }, sort: [['mtsCreate', -1]], - projection: this.tradesModel, + projection: this.tradesModelFields, exclude: ['user_id'], isExcludePrivate: false } From 272e84de17cef151e85143dba35d449fbb4a278b Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Fri, 30 Aug 2024 12:30:14 +0300 Subject: [PATCH 10/29] Rework weightedAveragesReport service to use new model interface --- workers/loc.api/sync/weighted.averages.report/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/sync/weighted.averages.report/index.js b/workers/loc.api/sync/weighted.averages.report/index.js index a7b30365..5fe10920 100644 --- a/workers/loc.api/sync/weighted.averages.report/index.js +++ b/workers/loc.api/sync/weighted.averages.report/index.js @@ -33,8 +33,9 @@ class WeightedAveragesReport extends BaseWeightedAveragesReport { this.syncSchema = syncSchema this.ALLOWED_COLLS = ALLOWED_COLLS - this.tradesModel = this.syncSchema.getModelsMap() - .get(this.ALLOWED_COLLS.TRADES) + this.tradesModelFields = this.syncSchema + .getModelOf(this.ALLOWED_COLLS.TRADES) + .getModelFields() // Used to switch data fetching from DB for framework mode this._isNotCalcTakenFromBfxApi = true @@ -63,7 +64,7 @@ class WeightedAveragesReport extends BaseWeightedAveragesReport { ...symbFilter }, sort: [['mtsCreate', -1]], - projection: this.tradesModel, + projection: this.tradesModelFields, exclude: ['user_id'], isExcludePrivate: true } From a51d05c680702ee53cdd9a0a6adfcec25dfe9e54 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Mon, 2 Sep 2024 15:36:09 +0300 Subject: [PATCH 11/29] Clone sync schema keeping initial model instance --- workers/loc.api/sync/schema/helpers/index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/workers/loc.api/sync/schema/helpers/index.js b/workers/loc.api/sync/schema/helpers/index.js index 9e1e79d4..6179ea6c 100644 --- a/workers/loc.api/sync/schema/helpers/index.js +++ b/workers/loc.api/sync/schema/helpers/index.js @@ -1,18 +1,24 @@ 'use strict' const { omit, cloneDeep } = require('lib-js-util-base') +const Model = require('../models/model') const cloneSchema = (map, omittedFields = []) => { const arr = [...map].map(([key, schema]) => { const normalizedSchema = omit(schema, omittedFields) - const clonedSchema = cloneDeep(normalizedSchema) + const clonedSchema = {} + + for (const [propName, value] of Object.entries(normalizedSchema)) { + if ( + typeof value === 'function' || + value instanceof Model + ) { + clonedSchema[propName] = value - for (const [propName, value] of Object.entries(schema)) { - if (typeof value !== 'function') { continue } - clonedSchema[propName] = value + clonedSchema[propName] = cloneDeep(value) } return [key, clonedSchema] From d65c6e305cf7a3bcd15bc661e87029b6177b03e8 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Mon, 2 Sep 2024 15:46:49 +0300 Subject: [PATCH 12/29] Add getter for model field keys to model class --- workers/loc.api/sync/schema/models/model/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/workers/loc.api/sync/schema/models/model/index.js b/workers/loc.api/sync/schema/models/model/index.js index 28742297..abe03837 100644 --- a/workers/loc.api/sync/schema/models/model/index.js +++ b/workers/loc.api/sync/schema/models/model/index.js @@ -13,6 +13,7 @@ const BaseModel = require('./base.model') class Model extends BaseModel { #modelFields = {} + #modelFieldKeys = [] #opts = { hasNoUID: false, hasCreateUpdateMtsTriggers: false @@ -102,6 +103,12 @@ class Model extends BaseModel { : this.#modelFields } + getModelFieldKeys (opts) { + return opts?.isCloned + ? cloneDeep(this.#modelFieldKeys) + : this.#modelFieldKeys + } + getTriggers (opts) { return this.#getServiceFields( BaseModel.TRIGGER_FIELD_NAME, @@ -198,6 +205,7 @@ class Model extends BaseModel { for (const [name, value] of Object.entries(this)) { if (this.#isNotDbServiceField(name)) { this.#modelFields[name] = value + this.#modelFieldKeys.push(name) } } @@ -206,6 +214,7 @@ class Model extends BaseModel { } freezeAndSealObjectDeeply(this.#modelFields) + freezeAndSealObjectDeeply(this.#modelFieldKeys) } #isNotDbServiceField (fieldName) { From 39cafa979390cc6e8f1b20627d4cb114caca3806 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Mon, 2 Sep 2024 15:56:14 +0300 Subject: [PATCH 13/29] Add method to check model has field name --- workers/loc.api/sync/schema/models/model/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/workers/loc.api/sync/schema/models/model/index.js b/workers/loc.api/sync/schema/models/model/index.js index abe03837..f2abeff0 100644 --- a/workers/loc.api/sync/schema/models/model/index.js +++ b/workers/loc.api/sync/schema/models/model/index.js @@ -109,6 +109,10 @@ class Model extends BaseModel { : this.#modelFieldKeys } + hasModelFieldName (fieldName) { + return !!this.#modelFields[fieldName] + } + getTriggers (opts) { return this.#getServiceFields( BaseModel.TRIGGER_FIELD_NAME, From ba19eec68bdfecddae0b49ff29e5b8ba83de7cf6 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Mon, 2 Sep 2024 16:03:26 +0300 Subject: [PATCH 14/29] Use model interface to get query --- .../loc.api/sync/dao/helpers/find-in-coll-by/get-query.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js b/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js index 08d11a50..db774107 100644 --- a/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js +++ b/workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js @@ -25,7 +25,10 @@ module.exports = (args, methodColl, opts) => { filter } = getFilterParams(args, methodColl, { isPublic }) - const _model = { ...model, ...additionalModel } + const modelFields = { + ...model.getModelFields(), + ...additionalModel + } const exclude = isPublic ? ['_id', 'user_id'] : ['_id'] const { @@ -46,7 +49,7 @@ module.exports = (args, methodColl, opts) => { } = getGroupQuery(methodColl) const { subQuery, subQueryValues } = getSubQuery(methodColl) const projection = getProjectionQuery( - _model, + modelFields, exclude, isExcludePrivate ) From fc9ed3a7720a388cf48433e8f810567b73af8541 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Mon, 2 Sep 2024 16:04:22 +0300 Subject: [PATCH 15/29] Use model interface to get projection --- workers/loc.api/sync/dao/helpers/get-projection-query.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/workers/loc.api/sync/dao/helpers/get-projection-query.js b/workers/loc.api/sync/dao/helpers/get-projection-query.js index f014fbc1..37177aac 100644 --- a/workers/loc.api/sync/dao/helpers/get-projection-query.js +++ b/workers/loc.api/sync/dao/helpers/get-projection-query.js @@ -1,9 +1,14 @@ 'use strict' +const Model = require('../../schema/models/model') + const _getProjArr = (model) => { if (Array.isArray(model)) { return model } + if (model instanceof Model) { + return model.getModelFieldKeys() + } if ( model && typeof model === 'object' From 7863f80812c5adce77680b726f1dbbe13ad59d07 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Mon, 2 Sep 2024 16:05:22 +0300 Subject: [PATCH 16/29] Use model interface to get filter query --- .../sync/dao/helpers/get-insertable-array-objects-filter.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js b/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js index ed13a361..58d4e945 100644 --- a/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js +++ b/workers/loc.api/sync/dao/helpers/get-insertable-array-objects-filter.js @@ -16,10 +16,7 @@ const getFieldsFilters = ( const _params = { ...params } const field = _params[fieldName] - if ( - Object.keys(model) - .some(key => key === modelFieldName) - ) { + if (model.hasModelFieldName(modelFieldName)) { if (typeof field === 'boolean') { return { ...accum, From 06b7604bf93fe4ef4fbdeac870ba78ac95b36721 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 3 Sep 2024 09:24:30 +0300 Subject: [PATCH 17/29] Use model interface in sync user step manager --- .../data.inserter/sync.user.step.manager/index.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js b/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js index 91b42c01..d3a38565 100644 --- a/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js +++ b/workers/loc.api/sync/data.inserter/sync.user.step.manager/index.js @@ -213,24 +213,22 @@ class SyncUserStepManager { const hasUserIdField = ( Number.isInteger(userId) ) - const hasUserIdFieldInModel = ( - typeof model?.user_id === 'string' - ) + const hasUserIdFieldInModel = model + .hasModelFieldName('user_id') const hasSubUserIdField = ( Number.isInteger(subUserId) ) - const hasSubUserIdFieldInModel = ( - typeof model?.subUserId === 'string' - ) + const hasSubUserIdFieldInModel = model + .hasModelFieldName('subUserId') const hasSymbolField = ( symbolFieldName && - typeof model?.[symbolFieldName] === 'string' && + model.hasModelFieldName(symbolFieldName) && symbol && typeof symbol === 'string' ) const hasTimeframeField = ( timeframeFieldName && - typeof model?.[timeframeFieldName] === 'string' && + model.hasModelFieldName(timeframeFieldName) && timeframe && typeof timeframe === 'string' ) From a239d055cedc62a3e3bc3ee41419860977c916df Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 3 Sep 2024 09:49:25 +0300 Subject: [PATCH 18/29] Use model interface to normalize api data --- workers/loc.api/sync/data.inserter/helpers/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workers/loc.api/sync/data.inserter/helpers/utils.js b/workers/loc.api/sync/data.inserter/helpers/utils.js index 79ba3441..4778a100 100644 --- a/workers/loc.api/sync/data.inserter/helpers/utils.js +++ b/workers/loc.api/sync/data.inserter/helpers/utils.js @@ -16,7 +16,7 @@ const normalizeApiData = ( return data } - const modelKeys = Object.keys(model) + const modelKeys = model.getModelFieldKeys() if (modelKeys.length === 0) { return data From 3fcef1103b93c5134e2fce260edfb1a4d48abc5a Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 3 Sep 2024 09:58:02 +0300 Subject: [PATCH 19/29] Use model interface in data inserter --- workers/loc.api/sync/data.inserter/index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/workers/loc.api/sync/data.inserter/index.js b/workers/loc.api/sync/data.inserter/index.js index a67a04a5..815e1bd6 100644 --- a/workers/loc.api/sync/data.inserter/index.js +++ b/workers/loc.api/sync/data.inserter/index.js @@ -539,11 +539,7 @@ class DataInserter extends EventEmitter { _args.params.notThrowError = true const currIterationArgs = cloneDeep(_args) - const { subUserId } = model ?? {} - const hasNotSubUserField = ( - !subUserId || - typeof subUserId !== 'string' - ) + const hasNotSubUserField = !model.hasModelFieldName('subUserId') const { auth: _auth } = _args ?? {} const { session } = _auth ?? {} const sessionAuth = isPublic || hasNotSubUserField From c005a6860beaf6c02afd17ce738dd92cd4332711 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 3 Sep 2024 14:39:14 +0300 Subject: [PATCH 20/29] Rework dao models map getter --- workers/loc.api/sync/dao/dao.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/workers/loc.api/sync/dao/dao.js b/workers/loc.api/sync/dao/dao.js index 41af7e4f..f9a5cccb 100644 --- a/workers/loc.api/sync/dao/dao.js +++ b/workers/loc.api/sync/dao/dao.js @@ -25,8 +25,8 @@ class DAO { this.processMessageManagerFactory = processMessageManagerFactory } - _getModelsMap (params) { - return this.syncSchema.getModelsMap(params) + _getModelsMap () { + return this.syncSchema.getModelsMap() } _getMethodCollMap (params) { From 0d955f0449ae1db8220c7dd5129f9e0dc849c753 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 3 Sep 2024 14:39:39 +0300 Subject: [PATCH 21/29] Rework dao table creation method --- workers/loc.api/sync/dao/dao.better.sqlite.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/workers/loc.api/sync/dao/dao.better.sqlite.js b/workers/loc.api/sync/dao/dao.better.sqlite.js index eaa1ef2a..ace0d136 100644 --- a/workers/loc.api/sync/dao/dao.better.sqlite.js +++ b/workers/loc.api/sync/dao/dao.better.sqlite.js @@ -221,14 +221,7 @@ class BetterSqliteDAO extends DAO { } _createTablesIfNotExists (opts = {}) { - const models = this._getModelsMap({ - models: opts?.models, - omittedFields: [ - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME - ] - }) + const models = opts?.models ?? this._getModelsMap() const sql = getTableCreationQuery(models, opts) return this.query({ From 0b31a9d859c4b7e7505a909c15d491c7307ebf9d Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 3 Sep 2024 14:40:02 +0300 Subject: [PATCH 22/29] Use model interface to get table creation query --- .../sync/dao/helpers/get-table-creation-query.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/workers/loc.api/sync/dao/helpers/get-table-creation-query.js b/workers/loc.api/sync/dao/helpers/get-table-creation-query.js index 83f9f5f3..50315d16 100644 --- a/workers/loc.api/sync/dao/helpers/get-table-creation-query.js +++ b/workers/loc.api/sync/dao/helpers/get-table-creation-query.js @@ -1,13 +1,10 @@ 'use strict' -const { - CONSTR_FIELD_NAME -} = require('../../schema/models/model/db.service.field.names') - const _getConstraintsQuery = (name, model) => { - const constraintsArr = Array.isArray(model[CONSTR_FIELD_NAME]) - ? model[CONSTR_FIELD_NAME] - : [model[CONSTR_FIELD_NAME]] + const modelConstraints = model.getConstraints() + const constraintsArr = Array.isArray(modelConstraints) + ? modelConstraints + : [modelConstraints] return constraintsArr.reduce((accum, item) => { const _constraints = ( @@ -43,8 +40,7 @@ module.exports = (models = [], opts = {}) => { const _name = `${prefix}${name}` const constraints = _getConstraintsQuery(_name, model) - const keys = Object.keys(model) - .filter((field) => field !== CONSTR_FIELD_NAME) + const keys = model.getModelFieldKeys() const columnDefs = keys.reduce((accum, field, i, arr) => { const isLast = arr.length === (i + 1) const type = model[field] From 0c8e55479504c9e4cfda534b9e5f7a81d8a00721 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 4 Sep 2024 07:52:51 +0300 Subject: [PATCH 23/29] Add getter for model instance to dao --- workers/loc.api/sync/dao/dao.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/workers/loc.api/sync/dao/dao.js b/workers/loc.api/sync/dao/dao.js index f9a5cccb..9c236d6a 100644 --- a/workers/loc.api/sync/dao/dao.js +++ b/workers/loc.api/sync/dao/dao.js @@ -29,6 +29,10 @@ class DAO { return this.syncSchema.getModelsMap() } + _getModelOf (tableName) { + return this.syncSchema.getModelOf(tableName) + } + _getMethodCollMap (params) { return this.syncSchema.getMethodCollMap(params) } From 7d25c40a07c829a209b49a5136a079027c47d493 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 4 Sep 2024 07:53:28 +0300 Subject: [PATCH 24/29] Use model interface to move temp table data to main --- workers/loc.api/sync/dao/dao.better.sqlite.js | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/workers/loc.api/sync/dao/dao.better.sqlite.js b/workers/loc.api/sync/dao/dao.better.sqlite.js index ace0d136..33596821 100644 --- a/workers/loc.api/sync/dao/dao.better.sqlite.js +++ b/workers/loc.api/sync/dao/dao.better.sqlite.js @@ -40,12 +40,7 @@ const { RemoveElemsLeaveLastNRecordsError } = require('../../errors') -const { - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME -} = require('../schema/models/model/db.service.field.names') +const Model = require('../schema/models/model') const ALLOWED_COLLS = require('../schema/allowed.colls') const SYNC_QUEUE_STATES = require('../sync.queue/sync.queue.states') const DB_WORKER_ACTIONS = require( @@ -481,16 +476,6 @@ class BetterSqliteDAO extends DAO { return false } - const modelsMap = this._getModelsMap({ - omittedFields: [ - '_id', - CONSTR_FIELD_NAME, - TRIGGER_FIELD_NAME, - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME - ] - }) - await this._beginTrans(async () => { const tableNames = await this.getTablesNames({ doNotQueueQuery }) const filteredTempTableNames = tableNames.filter((name) => ( @@ -501,12 +486,16 @@ class BetterSqliteDAO extends DAO { for (const tempName of filteredTempTableNames) { const name = tempName.replace(namePrefix, '') - const model = modelsMap.get(name) - const projection = Object.keys(model).join(', ') + const model = this._getModelOf(name) if (!model) { continue } + + const projection = model.getModelFieldKeys() + .filter((fieldName) => fieldName !== Model.UID_FIELD_NAME) + .join(', ') + if (isStrictEqual) { sqlArr.push(`DELETE FROM ${name}`) } From ff5412baf559e072174be50a4754f70881b29cb3 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 4 Sep 2024 07:57:56 +0300 Subject: [PATCH 25/29] Use model interface to get trigger creation query --- .../sync/dao/helpers/get-trigger-creation-query.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js b/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js index c619d05c..bacccca5 100644 --- a/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js +++ b/workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js @@ -1,9 +1,5 @@ 'use strict' -const { - TRIGGER_FIELD_NAME -} = require('../../schema/models/model/db.service.field.names') - const _getTriggersQuery = ( name, model, @@ -13,9 +9,10 @@ const _getTriggersQuery = ( shouldNotAddIfNotExistsStm } = opts ?? {} - const triggersArr = Array.isArray(model[TRIGGER_FIELD_NAME]) - ? model[TRIGGER_FIELD_NAME] - : [model[TRIGGER_FIELD_NAME]] + const modelTriggers = model.getTriggers() + const triggersArr = Array.isArray(modelTriggers) + ? modelTriggers + : [modelTriggers] return triggersArr.reduce((accum, item) => { if ( From 8a3512353729371b55506e3ecac97672230ef46d Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 4 Sep 2024 07:58:19 +0300 Subject: [PATCH 26/29] Rework dao trigger creation method --- workers/loc.api/sync/dao/dao.better.sqlite.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/workers/loc.api/sync/dao/dao.better.sqlite.js b/workers/loc.api/sync/dao/dao.better.sqlite.js index 33596821..ff596027 100644 --- a/workers/loc.api/sync/dao/dao.better.sqlite.js +++ b/workers/loc.api/sync/dao/dao.better.sqlite.js @@ -227,10 +227,7 @@ class BetterSqliteDAO extends DAO { } _createTriggerIfNotExists (opts = {}) { - const models = this._getModelsMap({ - models: opts?.models, - omittedFields: [] - }) + const models = opts?.models ?? this._getModelsMap() const sql = getTriggerCreationQuery(models, opts) return this.query({ From 5d96e9637cc424162cb8c4a027062a32892fc776 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 4 Sep 2024 07:59:18 +0300 Subject: [PATCH 27/29] Use model interface to get index creation query --- .../dao/helpers/get-index-creation-query.js | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/workers/loc.api/sync/dao/helpers/get-index-creation-query.js b/workers/loc.api/sync/dao/helpers/get-index-creation-query.js index 87119b6a..0f9d495f 100644 --- a/workers/loc.api/sync/dao/helpers/get-index-creation-query.js +++ b/workers/loc.api/sync/dao/helpers/get-index-creation-query.js @@ -1,10 +1,5 @@ 'use strict' -const { - INDEX_FIELD_NAME, - UNIQUE_INDEX_FIELD_NAME -} = require('../../schema/models/model/db.service.field.names') - const _getIndexQuery = ( name, fields = [], @@ -67,18 +62,20 @@ const _getIndexQueryFromModel = ( shouldNotAddIfNotExistsStm } = opts ?? {} + const modelUniqueIndexies = model.getUniqueIndexies() + const modelIndexies = model.getIndexies() const uniqueIndexFields = ( - model[UNIQUE_INDEX_FIELD_NAME] && - typeof model[UNIQUE_INDEX_FIELD_NAME] === 'string' + modelUniqueIndexies && + typeof modelUniqueIndexies === 'string' ) - ? model[UNIQUE_INDEX_FIELD_NAME].split(' ') - : model[UNIQUE_INDEX_FIELD_NAME] + ? modelUniqueIndexies.split(' ') + : modelUniqueIndexies const indexFields = ( - model[INDEX_FIELD_NAME] && - typeof model[INDEX_FIELD_NAME] === 'string' + modelIndexies && + typeof modelIndexies === 'string' ) - ? model[INDEX_FIELD_NAME].split(' ') - : model[INDEX_FIELD_NAME] + ? modelIndexies.split(' ') + : modelIndexies const uniqueIndexiesArr = _getIndexQuery( name, From 0a749a28329e3e807c07012e74016b99f80c91cc Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 4 Sep 2024 07:59:39 +0300 Subject: [PATCH 28/29] Rework dao index creation method --- workers/loc.api/sync/dao/dao.better.sqlite.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/workers/loc.api/sync/dao/dao.better.sqlite.js b/workers/loc.api/sync/dao/dao.better.sqlite.js index ff596027..d54cd35b 100644 --- a/workers/loc.api/sync/dao/dao.better.sqlite.js +++ b/workers/loc.api/sync/dao/dao.better.sqlite.js @@ -238,10 +238,7 @@ class BetterSqliteDAO extends DAO { } _createIndexisIfNotExists (opts = {}) { - const models = this._getModelsMap({ - models: opts?.models, - omittedFields: [] - }) + const models = opts?.models ?? this._getModelsMap() const sql = getIndexCreationQuery(models, opts) return this.query({ From 02b3791721ed02a07631ec24b157c99d75eb2e67 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 4 Sep 2024 09:19:19 +0300 Subject: [PATCH 29/29] Fix model map getter --- workers/loc.api/sync/schema/models/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workers/loc.api/sync/schema/models/index.js b/workers/loc.api/sync/schema/models/index.js index 6731b7b6..0a6f14be 100644 --- a/workers/loc.api/sync/schema/models/index.js +++ b/workers/loc.api/sync/schema/models/index.js @@ -80,7 +80,7 @@ const models = new Map([ ]) const getModelsMap = () => { - return new Map(...models) + return new Map(models) } const getModelOf = (tableName) => {