New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@wmfs/pg-model

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wmfs/pg-model - npm Package Compare versions

Comparing version

to
1.1.0

.releaserc.json

183

lib/Model/actions/Creator.js

@@ -20,2 +20,3 @@ 'use strict'

this.primaryKeyColumns = model.pkColumnNames
this.returning = ` RETURNING ${this.primaryKeyColumns.join(', ')};`
}

@@ -25,97 +26,125 @@

const script = []
if (Array.isArray(jsonData)) {
jsonData.forEach(jD => this.addInsertStatementToScript(script, jD, options, preStatementHook))
} else {
this.addInsertStatementToScript(script, jsonData, options, preStatementHook)
}
return script
} // addStatements
const createPostStatementHook = (result, ctx) => {
if (!ctx.hasOwnProperty('returnValue')) {
const returnValue = {}
_.forOwn(result.rows[0], (value, columnName) => {
returnValue[_.camelCase(columnName)] = value
})
ctx.returnValue = {
idProperties: returnValue
}
}
if (!ctx.hasOwnProperty('lastCreatedPrimaryKey')) {
ctx.lastCreatedPrimaryKey = {}
}
ctx.lastCreatedPrimaryKey[this.fullTableName] = result.rows[0]
addInsertStatementToScript (script, doc, options, preStatementHook) {
const parsedDoc = this.model.parseDoc(doc, {includeNullFks: true})
const statement =
this.insertStatement(parsedDoc) +
this.upsertStatement(parsedDoc, options) +
this.returningStatement()
const scriptEntry = {
sql: statement,
params: parsedDoc.keyAndAttributeValues,
columnNames: parsedDoc.keyAndAttributeColumns,
postStatementHook: this.postCreateStatementHook.bind(this)
}
const addInsertStatementToScript = (doc) => {
const parsedDoc = this.model.parseDoc(doc, {includeNullFks: true})
let statement = `${this.prefix} (${parsedDoc.keyAndAttributeColumns.join(',')}) VALUES (${generateValuePlaceholders(parsedDoc.keyAndAttributeColumns.length)})`
if (preStatementHook) {
scriptEntry.preStatementHook = preStatementHook.bind(this)
}
if (options.hasOwnProperty('upsert') && options.upsert) {
const updateColumns = []
const updatePlaceholders = []
const whereParts = []
let i = -1
script.push(scriptEntry)
parsedDoc.keyAndAttributeColumns.forEach(columnName => {
i++
if (this.primaryKeyColumns.indexOf(columnName) === -1) {
updateColumns.push(columnName)
updatePlaceholders.push('$' + (i + 1))
} else {
whereParts.push('a.' + columnName + '=$' + (parsedDoc.keyAndAttributeColumns.indexOf(columnName) + 1))
}
})
let setMissingPropertiesToNull
if (options.hasOwnProperty('setMissingPropertiesToNull')) {
setMissingPropertiesToNull = options.setMissingPropertiesToNull
} else {
setMissingPropertiesToNull = true
}
if (setMissingPropertiesToNull) {
parsedDoc.missingAttributeColumnNames.forEach(column => {
updateColumns.push(column)
updatePlaceholders.push('null')
})
}
let conflictClause
if (updateColumns.length > 0 && whereParts.length > 0) {
conflictClause = ` ON CONFLICT (${this.primaryKeyColumns.join(', ')}) DO UPDATE SET (${updateColumns.join(',')})=(${updatePlaceholders.join(',')}) WHERE ${whereParts.join(' AND ')}`
} else {
conflictClause = ' ON CONFLICT DO NOTHING'
}
statement += conflictClause
}
_.forOwn(parsedDoc.subDocs, (subDoc, propertyId) => {
const subModel = this.model.subModels[propertyId]
statement += ` RETURNING ${this.primaryKeyColumns.join(', ')};`
const subScript = subModel.model.creator.makeStatements(
doc[propertyId],
options,
getPreStatementHookFunction(this.fullTableName, subModel.columnJoin)
)
script.push(...subScript)
})
} // addInsertStatementsToScript
const scriptEntry = {
sql: statement,
params: parsedDoc.keyAndAttributeValues,
columnNames: parsedDoc.keyAndAttributeColumns,
postStatementHook: createPostStatementHook.bind(this)
insertStatement (parsedDoc) {
this.pushCreatedBy(parsedDoc)
return `${this.prefix} (${parsedDoc.keyAndAttributeColumns.join(',')}) VALUES (${generateValuePlaceholders(parsedDoc.keyAndAttributeColumns.length)})`
} // insertStatement
upsertStatement (parsedDoc, options) {
if (!(options.hasOwnProperty('upsert') && options.upsert)) {
return ''
}
const updateColumns = []
const updatePlaceholders = []
const whereParts = []
let i = -1
for (const columnName of parsedDoc.keyAndAttributeColumns) {
i++
if (this.model.createdByField === columnName) {
continue
}
if (preStatementHook) {
scriptEntry.preStatementHook = preStatementHook.bind(this)
if (this.primaryKeyColumns.indexOf(columnName) === -1) {
updateColumns.push(columnName)
updatePlaceholders.push('$' + (i + 1))
} else {
whereParts.push('a.' + columnName + '=$' + (parsedDoc.keyAndAttributeColumns.indexOf(columnName) + 1))
}
} // for ...
script.push(scriptEntry)
if (this.model.modifiedByField) {
updateColumns.push(this.model.modifiedByField)
parsedDoc.keyAndAttributeValues.push(this.model.currentUserFn())
updatePlaceholders.push('$' + (parsedDoc.keyAndAttributeValues.length))
}
_.forOwn(parsedDoc.subDocs, (subDoc, propertyId) => {
const subModel = this.model.subModels[propertyId]
const setMissingPropertiesToNull = (options.hasOwnProperty('setMissingPropertiesToNull'))
? options.setMissingPropertiesToNull
: true
const subScript = subModel.model.creator.makeStatements(
doc[propertyId],
options,
getPreStatementHookFunction(this.fullTableName, subModel.columnJoin)
)
script.push(...subScript)
if (setMissingPropertiesToNull) {
parsedDoc.missingAttributeColumnNames.forEach(column => {
updateColumns.push(column)
updatePlaceholders.push('null')
})
}
if (_.isArray(jsonData)) {
jsonData.forEach(
addInsertStatementToScript
)
} else {
addInsertStatementToScript(jsonData)
const conflictClause = (updateColumns.length > 0 && whereParts.length > 0)
? ` ON CONFLICT (${this.primaryKeyColumns.join(', ')}) DO UPDATE SET (${updateColumns.join(',')})=(${updatePlaceholders.join(',')}) WHERE ${whereParts.join(' AND ')}`
: ' ON CONFLICT DO NOTHING'
return conflictClause
} // upsertStatement
returningStatement () {
return this.returning
} // returningStatement
pushCreatedBy (parsedDoc) {
if (this.model.createdByField) {
parsedDoc.keyAndAttributeColumns.push(this.model.createdByField)
parsedDoc.keyAndAttributeValues.push(this.model.currentUserFn())
}
return script
} // addStatements
}
} // pushCreatedBy
postCreateStatementHook (result, ctx) {
if (!ctx.hasOwnProperty('returnValue')) {
const returnValue = {}
_.forOwn(result.rows[0], (value, columnName) => {
returnValue[_.camelCase(columnName)] = value
})
ctx.returnValue = {
idProperties: returnValue
}
}
if (!ctx.hasOwnProperty('lastCreatedPrimaryKey')) {
ctx.lastCreatedPrimaryKey = {}
}
ctx.lastCreatedPrimaryKey[this.fullTableName] = result.rows[0]
} // postCreateStatementHook
} // class Creator
module.exports = Creator

@@ -28,2 +28,3 @@ 'use strict'

const parsedDoc = _this.model.parseDoc(jsonData)
this.addModifiedBy(parsedDoc)
const set = []

@@ -122,4 +123,11 @@ let i = 0

}
addModifiedBy (parsedDoc) {
if (this.model.modifiedByField) {
parsedDoc.attributeColumns.push(this.model.modifiedByField)
parsedDoc.attributeValues.push(this.model.currentUserFn())
}
}
}
module.exports = Updater

@@ -76,2 +76,12 @@ 'use strict'

this.currentUserFn = () => null
this.createdByField = null
this.modifiedByField = null
if (options.service) {
const service = options.service
this.currentUserFn = service.currentUser ? () => service.currentUser() : () => null
this.createdByField = service.createdByField ? service.createdByField : null
this.modifiedByField = service.modifiedByField ? service.modifiedByField : null
}
this.promised = (...args) => promised(this, ...args)

@@ -78,0 +88,0 @@ }

{
"name": "@wmfs/pg-model",
"version": "1.0.2",
"version": "1.1.0",
"description": "Takes a relational database structure and returns model objects for noSQL-like abilities.",

@@ -33,7 +33,10 @@ "author": "West Midlands Fire Service",

"codecov": "3.0.3",
"conventional-changelog-metahub": "2.0.2",
"cz-conventional-changelog": "2.1.0",
"mocha": "5.2.0",
"nyc": "12.0.2",
"semantic-release": "^15.5.1",
"semantic-release": "15.8.1",
"standard": "11.0.1",
"@semantic-release/changelog": "3.0.0",
"@semantic-release/git": "7.0.1",
"@wmfs/hl-pg-client": "1.1.3",

@@ -46,3 +49,3 @@ "@wmfs/pg-diff-sync": "1.0.0"

"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
"semantic-release": "semantic-release"
},

@@ -56,20 +59,3 @@ "config": {

"access": "public"
},
"release": {
"analyzeCommits": {
"preset": "angular",
"releaseRules": [
{
"type": "build",
"scope": "deps",
"release": "minor"
},
{
"type": "build",
"scope": "deps-dev",
"release": "patch"
}
]
}
}
}

Sorry, the diff of this file is not supported yet