通过附加到 SQLite 上的现有数组来更新 Sequelize 列
Update Sequelize column with appending to the existing array on SQLite
我有一个模型看起来像
import {Optional, Model, Sequelize, DataTypes } from 'sequelize';
/*This is the Product model used to save the data about products*/
export interface ProductAttributes {
productId: number;
title: string;
userName: string;
sellerReview: string;
}
export interface ProductCreationAttributes extends Optional<ProductAttributes, 'productId'> { }
export class Product extends Model<ProductAttributes, ProductCreationAttributes> implements ProductAttributes {
productId!: number;
title!: string;
userName!: string;
sellerReview: string;
public static initialize(sequelize: Sequelize) {
Product.init({
productId: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
title: {
type: DataTypes.STRING,
allowNull: false
},
userName: {
type: DataTypes.STRING,
allowNull: false
},
sellerReview: {
type: DataTypes.STRING,
get: function () {
return JSON.parse(this.getDataValue('sellerReview'));
},
set: function (val) {
return this.setDataValue('sellerReview', JSON.stringify(val));
}
}
},
{
sequelize,
tableName: 'products'
}
);
}
}
我有一个控制器,我将从中插入值并将值更新到模型中,如下所示:
import express from 'express';
import { Router, Request, Response, RequestHandler } from 'express';
import { Product } from '../models/product.model';
import { Readable } from 'stream';
import { Sequelize, where } from 'sequelize';
/**
* This method is to add new products/services to the products model.
*/
productController.post('/add',
(req: Request, res: Response) => {
Product.create(req.body)
.then(product_added => res.send(product_added))
.catch(err => res.status(500).send(err));
});
/**
* This method is to edit a product in the products model.
*/
productController.put('/edit/:id', (req: Request, res: Response) => {
Product.findByPk(req.params.id)
.then(found => {
if (found != null) {
found.update(req.body).then(() => {
res.status(200).send('Product updated successfully.');
});
} else {
res.status(404).send('Product not found.');
}
})
.catch(err => res.status(500).send(err));
});
productController.put('/addNewReview/:id', (req: Request, res: Response) => {
Product.findByPk(req.params.id)
.then(found => {
if (found != null) {
found.update(
{sellerReview: Sequelize.literal( req.body.sellerReview)},
{where: {productId: req.params.id}}
).then(() => {
res.status(200).send('Riview added successfully.');
});
} else {
res.status(404).send('Product not found.');
}
})
.catch(err => res.status(500).send(err));
});
export const ProductController: Router = productController;
我想通过将新评论连接到 'sellerReview' 列的现有值来更新该行。例如:
现有列是 ["review1", "review2", "review3"].
当我更新它以添加 ["review4"]
时,我希望它是
["review1", "review2", "review3", "review4"]
.
创建新行的输入如下:
{
"title": "product1",
"userName": "user1",
"sellerReview": ["review1", "review2", "review3"]
}
待更新,
{
"title": "product1",
"userName": "user1",
"sellerReview": ["review4"]
}
使用 'Sequelize.literal' 对我不起作用。有人可以帮我解决这个问题吗?
提前致谢!!
因为您对 sellerReview
字段使用 setter
和 getter
,所以您不能使用 Sequelize.literal 或 Sequelize.fn。
所以您可以从 found
中获取 sellerReview
的现有值并将其与新值组合,如下所示:
found.update({sellerReview: found.sellerReview.concat(req.body.sellerReview)}, { where: {productId: req.params.id}})
我有一个模型看起来像
import {Optional, Model, Sequelize, DataTypes } from 'sequelize';
/*This is the Product model used to save the data about products*/
export interface ProductAttributes {
productId: number;
title: string;
userName: string;
sellerReview: string;
}
export interface ProductCreationAttributes extends Optional<ProductAttributes, 'productId'> { }
export class Product extends Model<ProductAttributes, ProductCreationAttributes> implements ProductAttributes {
productId!: number;
title!: string;
userName!: string;
sellerReview: string;
public static initialize(sequelize: Sequelize) {
Product.init({
productId: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
title: {
type: DataTypes.STRING,
allowNull: false
},
userName: {
type: DataTypes.STRING,
allowNull: false
},
sellerReview: {
type: DataTypes.STRING,
get: function () {
return JSON.parse(this.getDataValue('sellerReview'));
},
set: function (val) {
return this.setDataValue('sellerReview', JSON.stringify(val));
}
}
},
{
sequelize,
tableName: 'products'
}
);
}
}
我有一个控制器,我将从中插入值并将值更新到模型中,如下所示:
import express from 'express';
import { Router, Request, Response, RequestHandler } from 'express';
import { Product } from '../models/product.model';
import { Readable } from 'stream';
import { Sequelize, where } from 'sequelize';
/**
* This method is to add new products/services to the products model.
*/
productController.post('/add',
(req: Request, res: Response) => {
Product.create(req.body)
.then(product_added => res.send(product_added))
.catch(err => res.status(500).send(err));
});
/**
* This method is to edit a product in the products model.
*/
productController.put('/edit/:id', (req: Request, res: Response) => {
Product.findByPk(req.params.id)
.then(found => {
if (found != null) {
found.update(req.body).then(() => {
res.status(200).send('Product updated successfully.');
});
} else {
res.status(404).send('Product not found.');
}
})
.catch(err => res.status(500).send(err));
});
productController.put('/addNewReview/:id', (req: Request, res: Response) => {
Product.findByPk(req.params.id)
.then(found => {
if (found != null) {
found.update(
{sellerReview: Sequelize.literal( req.body.sellerReview)},
{where: {productId: req.params.id}}
).then(() => {
res.status(200).send('Riview added successfully.');
});
} else {
res.status(404).send('Product not found.');
}
})
.catch(err => res.status(500).send(err));
});
export const ProductController: Router = productController;
我想通过将新评论连接到 'sellerReview' 列的现有值来更新该行。例如:
现有列是 ["review1", "review2", "review3"].
当我更新它以添加 ["review4"]
时,我希望它是
["review1", "review2", "review3", "review4"]
.
创建新行的输入如下:
{
"title": "product1",
"userName": "user1",
"sellerReview": ["review1", "review2", "review3"]
}
待更新,
{
"title": "product1",
"userName": "user1",
"sellerReview": ["review4"]
}
使用 'Sequelize.literal' 对我不起作用。有人可以帮我解决这个问题吗?
提前致谢!!
因为您对 sellerReview
字段使用 setter
和 getter
,所以您不能使用 Sequelize.literal 或 Sequelize.fn。
所以您可以从 found
中获取 sellerReview
的现有值并将其与新值组合,如下所示:
found.update({sellerReview: found.sellerReview.concat(req.body.sellerReview)}, { where: {productId: req.params.id}})