API 文档暂时还没有,下面是使用的 demo
use node-postgres easier
const {PgHelper} = require('@c_kai/pg-helper');
// detail https://node-postgres.com/api/pool
const pgHelper = new PgHelper({
host,
user,
password,
database,
port: 5432,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});
const result = await pgHelper.insert([{
percentage: 0,
type: 'public',
params: {},
created_by: 1,
status: 'created',
job_uuid: '103',
},{
percentage: 0,
type: 'public',
params: {},
created_by: 1,
status: 'created',
job_uuid: '104',
}], {
tableName: 'jobs',
returning: true,
});
const result = await pgHelper.delete({}, {
tableName: 'jobs',
transaction,
});
const result = await pgHelper.update({
type: 'pravate',
status: 'gg',
}, {
update: ['type', 'status'],
tableName: 'jobs',
returning: ['id'],
transaction,
});
const result = await pgHelper.select({
percentage: 0,
}, {
where: {
percentage: '= {percentage}'
or: {
id: '=1',
}
},
schemaName: 'public',
tableName: 'jobs',
autoHump: false,
count: true,
});
await pgHelper.runSql('select now()');
// with params
await pgHelper.runSql('select power({a}, {b})', { a: 2, b: 4 });
await pgHelper.runTSql([
{
sql: 'select now()',
},
{
sql: 'select power({a}, {b})',
params: { a: 2, b: 4}
}
])
OR
let transaction;
try {
transaction = await pgHelper.getTransaction();
let result4 = await pgHelper.runSql('select {str1} || {str2}', {
str1: 'xiao',
str2: 'hong',
}, {
transaction,
});
transaction.commit();
console.log(result4)
} catch (error) {
transaction.rollback();
}
PgHelper
Class
Boolean
- 如果autoHump
为true返回字段的名称会格式化为驼峰Boolean
- 如果returning
为true返回结果会包含更新、插入、修改的数据Object
- 替换默认的日志需要包含info
、error
两个函数Function
Array<Object>
- 插入表的数据,其中Object
的key需要和字段一一对应String
- 表名称String
- 表名称;default: public
Boolean|Array
- 如果returning
为true,返回结果会包含插入的数据,为数组时返回数组包含的字段same as pg.queries
Function
params Object
- 模版参数,其中Object
的key需要和SQL模版中{key}
值一一对应
options
options.tableNameString
- 表名称
options.schemaNameString
- 表名称;default: public
options.returning Boolean|Array
- 如果returning
为true,返回结果会包含删除的数据,为数组时返回数组包含的字段
options.whereObject
构建where sql
{
id: '>10',
type: '={type}',
or:{
id:'= any({ids})'
}
}
// sql
//where (id > 0 and type={type} or (id = any({ids} ) )
Function
params Object
- 模版参数,其中Object
的key需要和SQL模版中{key}
值一一对应
options
options.tableNameString
- 表名称
options.schemaNameString
- 表名称;default: public
options.returning Boolean|Array
- 如果returning
为true,返回结果会包含更新的数据,为数组时返回数组包含的字段
options.whereObject
构建where sql
{
id: '>10',
type: '={type}',
or:{
id:'= any({ids})'
}
}
// sql
//where (id > 0 and type={type} or (id = any({ids} ) )
options. updateArray|Object
- 需要更新的字段
['name', 'type']
// name = {name},type={type}
OR
{
name: 'name',
type: 'myType',
}
name = {name},type={myType}
same as pg.queries
Function
params Object
- 模版参数,其中Object
的key需要和SQL模版中{key}
值一一对应
options
options.tableNameString
- 表名称
options.schemaNameString
- 表名称;default: public
options.whereObject
构建where sql
{
id: '>10',
type: '={type}',
or:{
id:'= any({ids})'
}
}
// sql
//where (id > 0 and type={type} or (id = any({ids} ) )
options.limit int
- limit number
options.offset int
-offset number
options.count Boolean
-是否返回查询的行数
options.include array
- 返回的字段数组default*
options.order array
构建ordersql
['id', ['type', 'desc'], [''name'', 'asc']]
// order by id, type desc, name asc
same as pg.queries
![]() |
1
bleepbloop 8 天前
看了一下,好像不能防注入?
|
![]() |
2
KouShuiYu 8 天前
@bleepbloop 可以防止注入 sql 中的{params}会被替换为$n 的形式
|
![]() |
3
KouShuiYu 8 天前
|
![]() |
4
KouShuiYu 8 天前
官方提供的这种方式感觉太不好用了
```js const query = { text: 'INSERT INTO users(name, email) VALUES($1, $2)', values: ['brianc', '[email protected]'], } ``` 我改成了这种形式 ``` const query = { text: 'INSERT INTO users(name, email) VALUES({name}, {email})', values: { name: 'brianc', email: '[email protected]'] } `` |
![]() |
5
KouShuiYu 8 天前
官方提供的这种方式感觉太不好用了
```js const query = { text: 'INSERT INTO users(name, email) VALUES($1, $2)', values: ['brianc', '[email protected]'], } ``` 我改成了这种形式 ```js const query = { text: 'INSERT INTO users(name, email) VALUES({name}, {email})', values: { name: 'brianc', email: '[email protected]'] } ``` |
![]() |
6
KouShuiYu 8 天前
额,好像不支持代码块
|
![]() |
7
fucUup 8 天前
js 读写数据库?????
小心清库跑路 |
![]() |
10
fucUup 8 天前
这种交付物, 接近裸写 sql, 不仅分库分表 一旦表动难维护, 还有容易清库跑路嫌疑
|
![]() |
11
RockShake 8 天前
做小项目没问题的,楼主说了这是 ORM 的替代方案
|
12
0clickjacking0 8 天前
orm 是真的可以杜绝 sql 注入的
|
13
xieren58 8 天前
knexjs 不好用吗?
|
![]() |
14
KouShuiYu 8 天前
@xieren58 这个用起来比较简单,对照的是 https://github.com/felixfbecker/node-sql-template-strings,我又加上了方便单表 CURD 的四个方法
|
15
ERRASYNCTYPE 7 天前
哈哈哈哈跟我自己做的一个好像,不过我用 gt gte lt lte 这些 mongodb 的保留词来做 where 构造
|
![]() |
16
KouShuiYu 7 天前
@ERRASYNCTYPE 考虑到 where 表达式情况太复杂了,还要额外加好多接口,所以就放开了
|
17
ERRASYNCTYPE 7 天前
@KouShuiYu 确实,还有 or between 什么的要处理,所以我都是自己根据自己业务去个别调整来着,不完备
|
18
felixin 7 天前 via Android
看一下 slonik
|