Feat: implement queries using task for easier usage
This commit is contained in:
parent
47614c9637
commit
440b8e7f61
2 changed files with 98 additions and 37 deletions
|
|
@ -1,47 +1,92 @@
|
|||
const _ = require('lodash/fp')
|
||||
const getSchema = () => 'public'
|
||||
|
||||
const concatSchema = (qry, tables) => {
|
||||
const schemaName = 'public' // fetch schema name from Async Local Storage here
|
||||
let query = qry
|
||||
_.forEach(tableName => { query = query.replace(tableName, `${schemaName}.${tableName}`) }, tables)
|
||||
return query
|
||||
const getDefaultSchema = () => 'public2'
|
||||
|
||||
const any = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.any(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const any = (db, dbContext, qry, tables, variables) => {
|
||||
const query = concatSchema(qry, tables)
|
||||
return db.any(query, variables)
|
||||
const none = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.none(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const manyOrNone = (db, dbContext, qry, tables, variables) => {
|
||||
const query = concatSchema(qry, tables)
|
||||
return db.manyOrNone(query, variables)
|
||||
const one = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.one(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const none = (db, dbContext, qry, tables, variables) => {
|
||||
const query = concatSchema(qry, tables)
|
||||
return db.none(query, variables)
|
||||
const oneOrNone = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.oneOrNone(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const many = (db, dbContext, qry, tables, variables) => {
|
||||
const query = concatSchema(qry, tables)
|
||||
return db.many(query, variables)
|
||||
const manyOrNone = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.manyOrNone(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const oneOrNone = (db, dbContext, qry, tables, variables) => {
|
||||
const query = concatSchema(qry, tables)
|
||||
return db.oneOrNone(query, variables)
|
||||
const many = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.many(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const one = (db, dbContext, qry, tables, variables) => {
|
||||
const query = concatSchema(qry, tables)
|
||||
return db.one(query, variables)
|
||||
const result = (obj, query, variables, cb, thisArg) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.result(query, variables, cb, thisArg).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const result = (db, dbContext, qry, tables, variables, cb) => {
|
||||
const query = concatSchema(qry, tables)
|
||||
console.log(query)
|
||||
return db.result(query, variables).then(r => {
|
||||
return cb ? cb(r) : r
|
||||
const query = (obj, query, values, qrm) => {
|
||||
const schema = getSchema()
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.query(query, values, qrm).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -52,5 +97,6 @@ module.exports = {
|
|||
many,
|
||||
oneOrNone,
|
||||
one,
|
||||
result
|
||||
result,
|
||||
query
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue