以下是我的问题:
我正在使用StrongLoop来处理现有的MySQL数据库。数据库使用二进制16数据类型来存储主键和外键。当我使用Strongloop工具创建模型时,数据将作为JSON中的字符串数组发送到客户端。我希望JSON包含转换后的36个字符串。在数据库中,我有bintouuid和uuidtobin函数,它们将数据转换成36个字符的格式。任何人都可以提供我将在模型或REST服务中使用的代码来扩展它以将数组(或二进制数据)转换为所需的字符串格式吗?
以下是该方案的详细信息:
表脚本如下所示:
CREATE TABLE `alert` (
`ID` binary(16) NOT NULL,
`Subject` varchar(255),
`Text` text NOT NULL,
`Read` int(11) DEFAULT '0',
`AddedDate` timestamp NULL DEFAULT NULL,
`LastUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`IsDeleted` bit(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
我有从二进制格式创建和转换uuids的功能:
newUuidToBin()
CREATE DEFINER=`root`@`localhost` FUNCTION `newuuidtobin`() RETURNS binary(16)
BEGIN
DECLARE UUID char(37);
SET UUID = UUID();
RETURN uuidtobin(UUID);
END
uuidToBin(UUID char(36))
CREATE DEFINER=`root`@`localhost` FUNCTION `uuidtobin`(UUID char(36)) RETURNS binary(16)
BEGIN
RETURN CONCAT(UNHEX(LEFT(UUID,8)),UNHEX(MID(UUID,10,4)),UNHEX(MID(UUID,15,4)),UNHEX(MID(UUID,20,4)),UNHEX(RIGHT(UUID,12)));
END
binToUuid(UUID BINARY(16))
CREATE DEFINER=`root`@`localhost` FUNCTION `bintouuid`(UUID BINARY(16)) RETURNS char(36) CHARSET utf8
BEGIN
RETURN concat(HEX(LEFT(uuid,4)),'-', HEX(MID(uuid,5,2)),'-', HEX(MID(uuid,7,2)),'-',HEX(MID(uuid,9,2)),'-',HEX(RIGHT(uuid,6)));
END
我有一个触发器,在创建记录时创建二进制uuid:
CREATE TRIGGER before_insert_alert
BEFORE INSERT
ON road.alert
FOR EACH ROW
SET new.id = newuuidtobin();
模型的默认Alert.json如下所示:
{
"name": "Alert",
"base": "PersistedModel",
"idInjection": false,
"mysql": {
"schema": "messaging",
"table": "alert"
},
"properties": {
"id": {
"type": "Binary",
"id": true,
"required": true,
"length": 16,
"precision": null,
"scale": null,
"mysql": {
"columnName": "ID",
"dataType": "binary",
"dataLength": 16,
"dataPrecision": null,
"dataScale": null,
"nullable": "N"
},
"_selectable": false
},
"subject": {
"type": "String",
"required": false,
"length": 65535,
"precision": null,
"scale": null,
"mysql": {
"columnName": "Subject",
"dataType": "text",
"dataLength": 65535,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
},
"_selectable": true
},
"text": {
"type": "String",
"required": true,
"length": 255,
"precision": null,
"scale": null,
"mysql": {
"columnName": "Text",
"dataType": "varchar",
"dataLength": 255,
"dataPrecision": null,
"dataScale": null,
"nullable": "N"
},
"_selectable": false
},
"read": {
"type": "Number",
"required": false,
"length": null,
"precision": 10,
"scale": 0,
"mysql": {
"columnName": "Read",
"dataType": "int",
"dataLength": null,
"dataPrecision": 10,
"dataScale": 0,
"nullable": "Y"
},
"_selectable": true
},
"addeddate": {
"type": "Date",
"required": false,
"length": null,
"precision": null,
"scale": null,
"mysql": {
"columnName": "AddedDate",
"dataType": "timestamp",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "Y"
},
"_selectable": true
},
"lastupdated": {
"type": "Date",
"required": true,
"length": null,
"precision": null,
"scale": null,
"mysql": {
"columnName": "LastUpdated",
"dataType": "timestamp",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "N"
},
"_selectable": false
},
"isdeleted": {
"type": "Binary",
"required": true,
"length": null,
"precision": 1,
"scale": null,
"mysql": {
"columnName": "IsDeleted",
"dataType": "bit",
"dataLength": null,
"dataPrecision": 1,
"dataScale": null,
"nullable": "N"
},
"_selectable": false
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
使用get访问REST端点时,
http://localhost:3000/api/Alerts
......我得到了这个结果。注意:id以数组的形式返回。
[
{
"id": [
97,
55,
50,
102,
52,
49,
50,
48,
45,
57,
53,
54,
97,
45,
49,
49
],
"subject": "uuids",
"text": "my message",
"read": 0,
"addeddate": null,
"lastupdated": "2015-01-05T23:10:03.000Z",
"isdeleted": [
0
]
},
.
.
.
]
...但我希望结果如下:
[
{
"id": "C39BC3A2-381F-5568-11C3-A0C2B66E64EF"
"subject": "uuids",
"text": "my message",
"read": 0,
"addeddate": null,
"lastupdated": "2015-01-05T23:10:03.000Z",
"isdeleted": [
0
]
},
.
.
.
]
在更新时,REST端点应使用mysql函数转换字符串:
update alert set `read` = 1 where id = uuidtobin('C39BC3A2-381F-5568-11C3-A0C2B66E64EF');
如何扩展模型以使用mysql函数选择在json结果中显示id为uuid字符串的数据,并使用uuid 36字符串更新记录?
答案 0 :(得分:0)
您可以使用远程挂钩操作响应,然后再将其发送回来。见http://docs.strongloop.com/display/LB/Remote+hooks
另一种可能的解决方案是使用模型挂钩在保存之前/之后修改数据。见http://docs.strongloop.com/display/LB/Model+hooks