Node.js 呃。请参阅更新的答案,以了解具有备选方案的完全工作脚本。还要注意的是,绑定参数不需要转义。太棒了,谢谢你的更新答案!谢谢你的回答!我可以在我的大多数项目中使用现代Postgres,但我有一个案例,我还需要与v10.12兼容。你有没有其他的答案可以给年纪大的
Node.js 呃。请参阅更新的答案,以了解具有备选方案的完全工作脚本。还要注意的是,绑定参数不需要转义。太棒了,谢谢你的更新答案!谢谢你的回答!我可以在我的大多数项目中使用现代Postgres,但我有一个案例,我还需要与v10.12兼容。你有没有其他的答案可以给年纪大的,node.js,postgresql,sequelize.js,jsonb,Node.js,Postgresql,Sequelize.js,Jsonb,呃。请参阅更新的答案,以了解具有备选方案的完全工作脚本。还要注意的是,绑定参数不需要转义。太棒了,谢谢你的更新答案!谢谢你的回答!我可以在我的大多数项目中使用现代Postgres,但我有一个案例,我还需要与v10.12兼容。你有没有其他的答案可以给年纪大的博士后发?如果是这样的话,请作为第二个答案发布,我很乐意为这两个答案提供奖励。在旧版本中,我可能会使用绑定参数进行原始查询。请参阅更新的答案,以了解具有备选方案的完全工作脚本。还要注意的是,绑定参数不需要转义。太棒了,谢谢你的更新答案! +--
呃。请参阅更新的答案,以了解具有备选方案的完全工作脚本。还要注意的是,绑定参数不需要转义。太棒了,谢谢你的更新答案!谢谢你的回答!我可以在我的大多数项目中使用现代Postgres,但我有一个案例,我还需要与v10.12兼容。你有没有其他的答案可以给年纪大的博士后发?如果是这样的话,请作为第二个答案发布,我很乐意为这两个答案提供奖励。在旧版本中,我可能会使用绑定参数进行原始查询。请参阅更新的答案,以了解具有备选方案的完全工作脚本。还要注意的是,绑定参数不需要转义。太棒了,谢谢你的更新答案!
+----+-----------------------------------------+
| id | customizations JSONB |
+----+-----------------------------------------+
| 1 | {"color": "blue", "lights": "led", ...} |
+----+-----------------------------------------+
| 2 | {"color": "red"} |
+----+-----------------------------------------+
| 3 | {} |
+----+-----------------------------------------+
SELECT * FROM "Cars" WHERE EXISTS (
SELECT 1 FROM JSONB_EACH_TEXT("customizations") WHERE "value" ~* 'BLU'
);
const cars = await cars.findAll({
where: // ???? Maybe something like db.fn('EXISTS', ...), but how do I bind the parameter?
});
SELECT *
FROM cars
WHERE jsonb_path_query_array(customizations, '$.*')::TEXT ~* 'BLU'
where: Sequelize.where(
Sequelize.literal("jsonb_path_query_array(customizations, '$.*')::TEXT"),
Op.iRegexp,
'BLU'
)
// cars.js
const Sequelize = require('sequelize');
const path = 'postgresql://hal:hal@localhost:5432/hal'
const sequelize = new Sequelize(path);
let Car = sequelize.define('cars', {
id: { type: Sequelize.INTEGER, primaryKey: true },
customizations: Sequelize.JSONB
});
let cars = [{ id: 1, customizations: {color: "blue", lights: "led"} },
{ id: 2, customizations: {color: "red"} },
{ id: 3, customizations: {} }]
const search_pat = 'BLU';
const stmt = `
select distinct cars.*
from cars, jsonb_each_text(customizations) kv(kk, vv)
where vv ~* $search_pattern
`;
sequelize
.sync()
.then(() => {
Car.bulkCreate(cars)
})
.then(() => {
sequelize.query(stmt, {
bind: {search_pattern: search_pat},
type: sequelize.QueryTypes.SELECT,
model: Car,
mapToModel: true
}).then(cars => {console.log(cars);})
});
Executing (default): CREATE TABLE IF NOT EXISTS "cars" ("id" INTEGER , "customizations" JSONB, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'cars' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): INSERT INTO "cars" ("id","customizations","createdAt","updatedAt") VALUES (1,'{"color":"blue","lights":"led"}','2021-04-14 04:42:50.446 +00:00','2021-04-14 04:42:50.446 +00:00'),(2,'{"color":"red"}','2021-04-14 04:42:50.446 +00:00','2021-04-14 04:42:50.446 +00:00'),(3,'{}','2021-04-14 04:42:50.446 +00:00','2021-04-14 04:42:50.446 +00:00') RETURNING "id","customizations","createdAt","updatedAt";
Executing (default): select distinct cars.*
from cars, jsonb_each_text(customizations) kv(kk, vv)
where vv ~* $1
[
cars {
dataValues: {
id: 1,
customizations: [Object],
createdAt: 2021-04-14T04:42:50.446Z,
updatedAt: 2021-04-14T04:42:50.446Z
},
_previousDataValues: {
id: 1,
customizations: [Object],
createdAt: 2021-04-14T04:42:50.446Z,
updatedAt: 2021-04-14T04:42:50.446Z
},
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: undefined
},
isNewRecord: false
}
]
const stmt = `
SELECT *
FROM cars
WHERE EXISTS (
SELECT 1
FROM JSONB_EACH_TEXT(customizations) WHERE "value" ~* $search_pattern
)
`;