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

perf(core): increased throughput #145

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

- [#138](https://github.com/mia-platform/crud-service/pull/138) patch import route validate the presence for the `_id` field
- [#145](https://github.com/mia-platform/crud-service/pull/145) increased get response performances

## 6.8.0 - 2023-07-11

Expand Down
64 changes: 28 additions & 36 deletions lib/AdditionalCaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,68 +26,60 @@ const { getPathFromPointer } = require('./JSONPath.utils')
class AdditionalCaster {
constructor(collectionDefinition) {
this._collectionSchema = collectionDefinition.schema ?? fieldsToSchema(collectionDefinition.fields)
}

castResultsAsStream() {
return through2.obj((chunk, _, callback) => {
const castedChunk = this.castItem(chunk)
callback(null, castedChunk)
})
}

castItem(item) {
let response = { ...item, _id: item._id?.toString() }
const pathToCoordinates = JSONPath({
json: response,
path: '$..[?(@property === "coordinates")]',
this._pathsToObjectIds = JSONPath({
json: this._collectionSchema.properties,
resultType: 'pointer',
path: '$..[?(@.type === "ObjectId")]',
})
.map(getPathFromPointer)

const pathToDates = JSONPath({
json: response,
path: '$..[?(Object.prototype.toString.call(@) === "[object Date]")]',
this._pathsToGeoPoint = JSONPath({
json: this._collectionSchema.properties,
path: '$..[?(@.type === "GeoPoint")]',
resultType: 'pointer',
})
.map(getPathFromPointer)

const pathsToObjectIds = JSONPath({
this._pathToCoordinates = this._pathsToGeoPoint.map(path => `${path}.coordinates`)

this._pathToDates = JSONPath({
json: this._collectionSchema.properties,
path: '$..[?(@.format === "date-time")]',
resultType: 'pointer',
path: '$..[?(@.type === "ObjectId")]',
})
.map(getPathFromPointer)
}

const pathsToGeoPoint = JSONPath({
json: this._collectionSchema.properties,
path: '$..[?(@.type === "GeoPoint")]',
resultType: 'pointer',
castResultsAsStream() {
return through2.obj((chunk, _, callback) => {
const castedChunk = this.castItem(chunk)
callback(null, castedChunk)
})
.map(getPathFromPointer)
}

const geoFieldRegexs = pathsToGeoPoint.map(path => new RegExp(`${path.split('.').join('\\.(\\d+\\.)?')}\\.(\\d+\\.)?coordinates`))
castItem(item) {
let response = { ...item, _id: item._id?.toString() }

for (const path of pathToCoordinates) {
if (geoFieldRegexs.some(geoFieldRegex => geoFieldRegex.test(path))) {
const pathLevels = path
.split('.')
const oneLevelUpPath = pathLevels
.slice(0, pathLevels.length - 1)
.join('.')
response = lset(response, oneLevelUpPath, lget(response, path))
for (const path of this._pathsToGeoPoint) {
const coordinatesPath = `${path}.coordinates`
const value = lget(response, coordinatesPath)
if (value) {
response = lset(response, path, value)
}
}

for (const path of pathsToObjectIds) {
for (const path of this._pathsToObjectIds) {
const value = lget(response, path)
if (value) {
response = lset(response, path, value.toString())
}
}

for (const path of pathToDates) {
for (const path of this._pathToDates) {
const value = lget(response, path)
if (value) {
// We want also support $dateToString mongo operator
// So we are not always sure that value is a Date
if (value instanceof Date) {
response = lset(response, path, value.toISOString())
}
}
Expand Down