Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
if (doc.limit) {
data.n = Math.min(Math.abs(doc.limit), data.n)
}
} catch (e) {
console.error(e)
}
socket.write(build(reqId, data, data))
return true
}
if (commandName === 'update') {
const update = doc.updates[0]
const where = mongoToPostgres('data', update.q, getArrayPaths(rawCollectionName))
let res
try {
const newValue = mongoToPostgres.convertUpdate('data', update.u)
// TODO (handle multi)
await createTable(collectionName)
res = await doQuery(databaseName, `UPDATE ${collectionName} SET data = ${newValue} WHERE ${where}`)
if (res.rowCount === 0 && update.upsert) {
const changes = update.u || {}
if (mongoToPostgres.countUpdateSpecialKeys(changes) === 0) {
// TODO: expand dot notation
_.assign(changes, update.q)
} else {
changes['$set'] = changes['$set'] || {}
_.assign(changes['$set'], update.q)
}
const newValue = mongoToPostgres.convertUpdate('data', changes, true)
async function insert() {
const res = await doQuery(databaseName, `INSERT INTO ${collectionName} VALUES (${newValue})`)
const data = { n: res.rowCount, nInserted: res.rowCount, updatedExisting: false, ok: 1 }
let res
try {
const newValue = mongoToPostgres.convertUpdate('data', update.u)
// TODO (handle multi)
await createTable(collectionName)
res = await doQuery(databaseName, `UPDATE ${collectionName} SET data = ${newValue} WHERE ${where}`)
if (res.rowCount === 0 && update.upsert) {
const changes = update.u || {}
if (mongoToPostgres.countUpdateSpecialKeys(changes) === 0) {
// TODO: expand dot notation
_.assign(changes, update.q)
} else {
changes['$set'] = changes['$set'] || {}
_.assign(changes['$set'], update.q)
}
const newValue = mongoToPostgres.convertUpdate('data', changes, true)
async function insert() {
const res = await doQuery(databaseName, `INSERT INTO ${collectionName} VALUES (${newValue})`)
const data = { n: res.rowCount, nInserted: res.rowCount, updatedExisting: false, ok: 1 }
socket.write(build(reqId, data, {}))
}
await tryOrCreateTable(insert, databaseName, collectionName)
return true
} else {
lastResult.n = res.rowCount
socket.write(build(reqId, lastResult, lastResult))
return true
}
} catch (e) {
console.error(e)
if (e.message.includes('_id')) {
const newValues = doc.documents.map((values) => '(' + mongoToPostgres.convertUpdate('data', values) + ')')
async function insert() {