@soinlabs/db
Advanced tools
Comparing version 1.1.0-alpha.66 to 1.1.0-alpha.67
@@ -199,3 +199,3 @@ const AbstractEntityInstance = require('./AbstractEntityInstance') | ||
removeUnusedFields(values) { | ||
const modelColumns = this.getNonVirtualColumns() | ||
const modelColumns = this.getNonVirtualColumns(false) | ||
@@ -214,4 +214,40 @@ const result = {} | ||
} | ||
columnsToFields(values) { | ||
const result = {} | ||
for (const [key, value] of Object.entries(values)) { | ||
result[this._dbModel.columns[key].fieldName] = value | ||
} | ||
return result | ||
} | ||
fieldsToColumns(values) { | ||
const result = {} | ||
for (const [key, value] of Object.entries(values)) { | ||
const col = Object.entries(this._dbModel.columns).find( | ||
x => x[1].fieldName === key | ||
) | ||
result[col ? col[0] : key] = value | ||
} | ||
return result | ||
} | ||
columnsAsFields(values) { | ||
return values.map(key => this._dbModel.columns[key].fieldName) | ||
} | ||
fieldsAsColumns(values) { | ||
return values.map(key => { | ||
const col = Object.entries(this._dbModel.columns).find( | ||
x => x[1].fieldName === key | ||
) | ||
return col ? col[0] : key | ||
}) | ||
} | ||
} | ||
module.exports = AbstractEntity |
@@ -192,3 +192,3 @@ /* eslint-disable no-nested-ternary */ | ||
const val = this._parseColumn(value) | ||
columns[key] = val | ||
columns[value.fieldName || key] = val | ||
} else { | ||
@@ -204,7 +204,5 @@ model.columns[key].isVirtual = true | ||
if (model.options.createdAt !== false) { | ||
columns.createdAt = { | ||
columns[model.options.createdAt || 'createdAt'] = { | ||
createDate: true, | ||
nullable: false, | ||
fieldName: model.options.createdAt | ||
? model.options.createdAt | ||
: 'createdAt', | ||
default: () => 'CURRENT_TIMESTAMP', | ||
@@ -215,7 +213,5 @@ type: 'timestamp with time zone', | ||
if (model.options.updatedAt !== false) { | ||
columns.updatedAt = { | ||
columns[model.options.updatedAt || 'updatedAt'] = { | ||
updateDate: true, | ||
nullable: false, | ||
fieldName: model.options.updatedAt | ||
? model.options.updatedAt | ||
: 'updatedAt', | ||
default: () => 'CURRENT_TIMESTAMP', | ||
@@ -222,0 +218,0 @@ type: 'timestamp with time zone', |
@@ -60,2 +60,3 @@ const AbstractEntity = require('../common/abstract/AbstractEntity') | ||
generateFindAll(model, connector) { | ||
const self = this | ||
return async options => { | ||
@@ -75,3 +76,5 @@ const connection = await connector.getConnection() | ||
const rawResult = await queryBuilder.getRawMany() | ||
result = transformer.transformMany(rawResult, null, options) | ||
result = transformer | ||
.transformMany(rawResult, null, options) | ||
.map(x => self.fieldsToColumns(x)) | ||
} else { | ||
@@ -84,3 +87,5 @@ const queryBuilder = connection | ||
const rawResult = await queryBuilder.getRawMany() | ||
result = transformer.transformMany(rawResult, null, options) | ||
result = transformer | ||
.transformMany(rawResult, null, options) | ||
.map(x => self.fieldsToColumns(x)) | ||
} | ||
@@ -92,2 +97,3 @@ return result | ||
generateFindOne(model, connector, dbModel) { | ||
const self = this | ||
return async options => { | ||
@@ -119,3 +125,6 @@ const connection = await connector.getConnection() | ||
const results = transformer.transformMany(rawResult, dbModel, options) | ||
result = results && results.length > 0 ? results[0] : null | ||
result = | ||
results && results.length > 0 | ||
? self.fieldsToColumns(results[0]) | ||
: null | ||
} else { | ||
@@ -129,3 +138,5 @@ const queryBuilder = options.transaction.manager | ||
result = rawResult | ||
? transformer.transform(rawResult, dbModel, options) | ||
? self.fieldsToColumns( | ||
transformer.transform(rawResult, dbModel, options) | ||
) | ||
: null | ||
@@ -152,3 +163,6 @@ } | ||
const results = transformer.transformMany(rawResult, dbModel, options) | ||
result = results && results.length > 0 ? results[0] : null | ||
result = | ||
results && results.length > 0 | ||
? self.fieldsToColumns(results[0]) | ||
: null | ||
} else { | ||
@@ -162,3 +176,5 @@ const queryBuilder = connection | ||
result = rawResult | ||
? transformer.transform(rawResult, dbModel, options) | ||
? self.fieldsToColumns( | ||
transformer.transform(rawResult, dbModel, options) | ||
) | ||
: null | ||
@@ -173,3 +189,5 @@ } | ||
return async (values, options) => { | ||
const transformValues = self.removeUnusedFields(values) | ||
const transformValues = self.columnsToFields( | ||
self.removeUnusedFields(values) | ||
) | ||
const connection = await connector.getConnection() | ||
@@ -189,3 +207,3 @@ let result | ||
await queryBuilder.execute() | ||
result = transformValues | ||
result = self.fieldsToColumns(transformValues) | ||
} else { | ||
@@ -199,3 +217,3 @@ const queryBuilder = connection | ||
await queryBuilder.execute() | ||
result = transformValues | ||
result = self.fieldsToColumns(transformValues) | ||
} | ||
@@ -209,3 +227,5 @@ return result | ||
return async (values, options) => { | ||
const transformValues = values.map(val => self.removeUnusedFields(val)) | ||
const transformValues = values.map(val => | ||
self.columnsToFields(self.removeUnusedFields(val)) | ||
) | ||
if (!(transformValues && transformValues.length > 0)) { | ||
@@ -229,3 +249,3 @@ return [] | ||
await queryBuilder.execute() | ||
result = transformValues | ||
result = transformValues.map(x => self.fieldsToColumns(x)) | ||
// TODO: Id not returning??? | ||
@@ -240,3 +260,3 @@ } else { | ||
await queryBuilder.execute() | ||
result = transformValues | ||
result = transformValues.map(x => self.fieldsToColumns(x)) | ||
// TODO: Id not returning??? | ||
@@ -251,3 +271,5 @@ } | ||
return async (values, options) => { | ||
const transformValues = self.removeUnusedFields(values) | ||
const transformValues = self.columnsToFields( | ||
self.removeUnusedFields(values) | ||
) | ||
const connection = await connector.getConnection() | ||
@@ -288,13 +310,13 @@ const repository = connection.getRepository(model.name) | ||
// Add returning | ||
let returningColumns = options.returning | ||
let returningColumns = [] | ||
if ( | ||
returningColumns === '*' || | ||
returningColumns === true || | ||
!returningColumns | ||
options.returning === '*' || | ||
options.returning === true || | ||
!options.returning | ||
) { | ||
returningColumns = this.getNonVirtualColumns() | ||
queryBuilder.returning(returningColumns) | ||
returningColumns = this.columnsAsFields(this.getNonVirtualColumns()) | ||
} else { | ||
queryBuilder.returning(options.returning) | ||
returningColumns = this.columnsAsFields(options.returning) | ||
} | ||
queryBuilder.returning(returningColumns) | ||
// const sql = queryBuilder.getSql() | ||
@@ -313,3 +335,3 @@ // Execute Query | ||
} | ||
response.push(result) | ||
response.push(result.map(x => self.fieldsToColumns(x))) | ||
} else { | ||
@@ -350,13 +372,13 @@ const rawResult = await options.transaction.manager.update( | ||
// Add returning | ||
let returningColumns = options.returning | ||
let returningColumns = [] | ||
if ( | ||
returningColumns === '*' || | ||
returningColumns === true || | ||
!returningColumns | ||
options.returning === '*' || | ||
options.returning === true || | ||
!options.returning | ||
) { | ||
returningColumns = this.getNonVirtualColumns() | ||
queryBuilder.returning(returningColumns) | ||
returningColumns = this.columnsAsFields(this.getNonVirtualColumns()) | ||
} else { | ||
queryBuilder.returning(options.returning) | ||
returningColumns = this.columnsAsFields(options.returning) | ||
} | ||
queryBuilder.returning(returningColumns) | ||
// const sql = queryBuilder.getSql() | ||
@@ -375,3 +397,3 @@ // Execute Query | ||
} | ||
response.push(result) | ||
response.push(result.map(x => self.fieldsToColumns(x))) | ||
} else { | ||
@@ -378,0 +400,0 @@ const rawResult = await repository.update( |
{ | ||
"name": "@soinlabs/db", | ||
"version": "1.1.0-alpha.66", | ||
"version": "1.1.0-alpha.67", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "repository": "", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
228864
7266