Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework db model usage to use new model interface #412

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fdf36e7
Rework model-getter helper
ZIMkaRU Aug 30, 2024
cde57c5
Rework model-map-getter helper
ZIMkaRU Aug 30, 2024
e5eb898
Rework movements service to use new model interface
ZIMkaRU Aug 30, 2024
143bfbe
Rework performingLoan service to use new model interface
ZIMkaRU Aug 30, 2024
766a91f
Rework positionsSnapshot service to use new model interface
ZIMkaRU Aug 30, 2024
29ff945
Rework summaryByAsset service to use new model interface
ZIMkaRU Aug 30, 2024
8465b60
Rework totalFeesReport service to use new model interface
ZIMkaRU Aug 30, 2024
ae9ccdb
Rework trades service to use new model interface
ZIMkaRU Aug 30, 2024
5a0cde1
Rework transactionTaxReport service to use new model interface
ZIMkaRU Aug 30, 2024
272e84d
Rework weightedAveragesReport service to use new model interface
ZIMkaRU Aug 30, 2024
a51d05c
Clone sync schema keeping initial model instance
ZIMkaRU Sep 2, 2024
d65c6e3
Add getter for model field keys to model class
ZIMkaRU Sep 2, 2024
39cafa9
Add method to check model has field name
ZIMkaRU Sep 2, 2024
ba19eec
Use model interface to get query
ZIMkaRU Sep 2, 2024
fc9ed3a
Use model interface to get projection
ZIMkaRU Sep 2, 2024
7863f80
Use model interface to get filter query
ZIMkaRU Sep 2, 2024
06b7604
Use model interface in sync user step manager
ZIMkaRU Sep 3, 2024
a239d05
Use model interface to normalize api data
ZIMkaRU Sep 3, 2024
3fcef11
Use model interface in data inserter
ZIMkaRU Sep 3, 2024
c005a68
Rework dao models map getter
ZIMkaRU Sep 3, 2024
0d955f0
Rework dao table creation method
ZIMkaRU Sep 3, 2024
0b31a9d
Use model interface to get table creation query
ZIMkaRU Sep 3, 2024
0c8e554
Add getter for model instance to dao
ZIMkaRU Sep 4, 2024
7d25c40
Use model interface to move temp table data to main
ZIMkaRU Sep 4, 2024
ff5412b
Use model interface to get trigger creation query
ZIMkaRU Sep 4, 2024
8a35123
Rework dao trigger creation method
ZIMkaRU Sep 4, 2024
5d96e96
Use model interface to get index creation query
ZIMkaRU Sep 4, 2024
0a749a2
Rework dao index creation method
ZIMkaRU Sep 4, 2024
02b3791
Fix model map getter
ZIMkaRU Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 10 additions & 34 deletions workers/loc.api/sync/dao/dao.better.sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -221,14 +216,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({
Expand All @@ -239,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({
Expand All @@ -253,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({
Expand Down Expand Up @@ -488,16 +470,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) => (
Expand All @@ -508,12 +480,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}`)
}
Expand Down
8 changes: 6 additions & 2 deletions workers/loc.api/sync/dao/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ class DAO {
this.processMessageManagerFactory = processMessageManagerFactory
}

_getModelsMap (params) {
return this.syncSchema.getModelsMap(params)
_getModelsMap () {
return this.syncSchema.getModelsMap()
}

_getModelOf (tableName) {
return this.syncSchema.getModelOf(tableName)
}

_getMethodCollMap (params) {
Expand Down
7 changes: 5 additions & 2 deletions workers/loc.api/sync/dao/helpers/find-in-coll-by/get-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -46,7 +49,7 @@ module.exports = (args, methodColl, opts) => {
} = getGroupQuery(methodColl)
const { subQuery, subQueryValues } = getSubQuery(methodColl)
const projection = getProjectionQuery(
_model,
modelFields,
exclude,
isExcludePrivate
)
Expand Down
23 changes: 10 additions & 13 deletions workers/loc.api/sync/dao/helpers/get-index-creation-query.js
Original file line number Diff line number Diff line change
@@ -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 = [],
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions workers/loc.api/sync/dao/helpers/get-projection-query.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
14 changes: 5 additions & 9 deletions workers/loc.api/sync/dao/helpers/get-table-creation-query.js
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down Expand Up @@ -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]
Expand Down
11 changes: 4 additions & 7 deletions workers/loc.api/sync/dao/helpers/get-trigger-creation-query.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict'

const {
TRIGGER_FIELD_NAME
} = require('../../schema/models/model/db.service.field.names')

const _getTriggersQuery = (
name,
model,
Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion workers/loc.api/sync/data.inserter/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const normalizeApiData = (
return data
}

const modelKeys = Object.keys(model)
const modelKeys = model.getModelFieldKeys()

if (modelKeys.length === 0) {
return data
Expand Down
6 changes: 1 addition & 5 deletions workers/loc.api/sync/data.inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
14 changes: 8 additions & 6 deletions workers/loc.api/sync/movements/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions workers/loc.api/sync/performing.loan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ({
Expand All @@ -47,7 +48,7 @@ class PerformingLoan {
filter = {
$eq: { _isMarginFundingPayment: 1 }
},
projection = this.ledgersModel
projection = this.ledgersModelFields
}) {
const user = await this.authenticator
.verifyRequestUser({ auth })
Expand Down
16 changes: 9 additions & 7 deletions workers/loc.api/sync/positions.snapshot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -67,7 +69,7 @@ class PositionsSnapshot {
$gte: { mtsUpdate: endMts }
},
sort: [['mtsUpdate', -1]],
projection: this.positionsHistoryModel,
projection: this.positionsHistoryModelFields,
exclude: ['user_id'],
isExcludePrivate: true
}
Expand Down Expand Up @@ -105,7 +107,7 @@ class PositionsSnapshot {
...lteFilter
},
sort: _sort,
projection: this.positionsSnapshotModel,
projection: this.positionsSnapshotModelFields,
exclude: ['user_id'],
isExcludePrivate: true
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading