ColumnSet Helpers - 仅在默认情况下使用 Raw
ColumnSet Helpers - using Raw only when default
我正在尝试使用 ColumnSet 助手来生成插入和更新查询,但我有一个列,如果传入,我想使用 pg-promise query-formatter 格式化数据,否则默认为 :raw (^)
,在本例中为 now()
.
代码示例是这样的:
const cs = new helpers.ColumnSet([
'lastname',
{
name: 'rental_date',
mod: '^',
def: 'now()'
}
], { table: { table: 'book_rental', schema: 'public' } })
let rental1 =
{
lastname: 'Mueller', rental_date: '2020-05-01T12:15:063Z'
};
let rental2 =
{
lastname: 'Smith'
};
let insert = helpers.insert(rental1, cs)
db.result(insert)
.then(data => res.json({ message: 'Ok!' }))
.catch(err => res.json({ message: 'Not ok!' }))
出租 1 应该 INSERT INTO (last_name, rental_date) VALUES ('Mueller', '2020-05-01T12:15:063Z' )
而出租 2 应该 INSERT INTO (last_name, rental_date) VALUES ('Smith', now() )
。但是,这会引发错误,因为 Rental1 的格式也为 :raw.
这可能是一个常见的用例,所以我可能遗漏了一些东西......我如何使用助手实现这一点,以便只有在触发默认值时,才使用 :raw 模式?
From the Raw Text
documentation:
Such variables cannot be null
or undefined
, because of the ambiguous meaning in this case, and those values will throw error Values null/undefined cannot be used as raw text
.
默认值,通过 属性 def
are documented 提供,仅在缺少 属性 时使用:
Default value for the property, to be used only when the source object doesn't have the property.
这意味着你在制造一个不成立的逻辑矛盾。
您的情况可以有任意数量的有效解决方案,因为库的语法非常灵活。以下只是您可以选择的一些示例。
方法一
{
name: 'rental_date',
init: c => c.exists ? c.value : {toPostgres: () => 'now()', rawText: true}
}
方法二
{
name: 'rental_date',
mod: ':raw',
init: c => c.exists ? pgp.as.text(c.value) : 'now()'
}
方法 3 - 干净且可重复使用的示例,使用现代语法:
const rawText = text => ({toPostgres: () => text, rawText: true});
{
name: 'rental_date',
init: c => c.value ?? rawText('now()')
}
方法4 - 混合使用def
:
{
name: 'rental_date',
def: 'now()',
mod: ':raw',
init: c => c.exists ? pgp.as.text(c.value) : c.value
}
方法 5:
const rawText = text => ({toPostgres: () => text, rawText: true});
{
name: 'rental_date',
def: rawText('now()')
}
...等等。
也许解决方案 5 最适合您的需求,它是最干净且可重复使用的。
我正在尝试使用 ColumnSet 助手来生成插入和更新查询,但我有一个列,如果传入,我想使用 pg-promise query-formatter 格式化数据,否则默认为 :raw (^)
,在本例中为 now()
.
代码示例是这样的:
const cs = new helpers.ColumnSet([
'lastname',
{
name: 'rental_date',
mod: '^',
def: 'now()'
}
], { table: { table: 'book_rental', schema: 'public' } })
let rental1 =
{
lastname: 'Mueller', rental_date: '2020-05-01T12:15:063Z'
};
let rental2 =
{
lastname: 'Smith'
};
let insert = helpers.insert(rental1, cs)
db.result(insert)
.then(data => res.json({ message: 'Ok!' }))
.catch(err => res.json({ message: 'Not ok!' }))
出租 1 应该 INSERT INTO (last_name, rental_date) VALUES ('Mueller', '2020-05-01T12:15:063Z' )
而出租 2 应该 INSERT INTO (last_name, rental_date) VALUES ('Smith', now() )
。但是,这会引发错误,因为 Rental1 的格式也为 :raw.
这可能是一个常见的用例,所以我可能遗漏了一些东西......我如何使用助手实现这一点,以便只有在触发默认值时,才使用 :raw 模式?
From the Raw Text
documentation:
Such variables cannot be
null
orundefined
, because of the ambiguous meaning in this case, and those values will throw errorValues null/undefined cannot be used as raw text
.
默认值,通过 属性 def
are documented 提供,仅在缺少 属性 时使用:
Default value for the property, to be used only when the source object doesn't have the property.
这意味着你在制造一个不成立的逻辑矛盾。
您的情况可以有任意数量的有效解决方案,因为库的语法非常灵活。以下只是您可以选择的一些示例。
方法一
{
name: 'rental_date',
init: c => c.exists ? c.value : {toPostgres: () => 'now()', rawText: true}
}
方法二
{
name: 'rental_date',
mod: ':raw',
init: c => c.exists ? pgp.as.text(c.value) : 'now()'
}
方法 3 - 干净且可重复使用的示例,使用现代语法:
const rawText = text => ({toPostgres: () => text, rawText: true});
{
name: 'rental_date',
init: c => c.value ?? rawText('now()')
}
方法4 - 混合使用def
:
{
name: 'rental_date',
def: 'now()',
mod: ':raw',
init: c => c.exists ? pgp.as.text(c.value) : c.value
}
方法 5:
const rawText = text => ({toPostgres: () => text, rawText: true});
{
name: 'rental_date',
def: rawText('now()')
}
...等等。
也许解决方案 5 最适合您的需求,它是最干净且可重复使用的。