温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

magic-api如何支持posgresql的jsonb数据

发布时间:2021-07-10 10:46:39 来源:亿速云 阅读:128 作者:chen 栏目:编程语言

本篇内容介绍了“magic-api如何支持posgresql的jsonb数据”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

#magic-api#

定义表:

CREATE TABLE public.tb_form_schema (
	id int8 NOT NULL,
	"name" varchar(50) NULL,
	form_schema jsonb NULL,
	CONSTRAINT tb_form_schema_pk PRIMARY KEY (id)
);

magic-api 将接口等信息存储到数据库的表 

CREATE TABLE public.magic_api_file (
	file_path varchar(100) NULL,
	id int8 NOT NULL DEFAULT nextval('tb_ba_id_seq'::regclass),
	file_content text NULL,
	CONSTRAINT magic_api_file_pk PRIMARY KEY (id)
);

有两种方案:

方案 1、创建function类:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.postgresql.util.PGobject;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.config.MagicFunction;
import org.ssssssss.script.annotation.Comment;
import org.ssssssss.script.annotation.Function;
@Slf4j
@Component
public class JsonFunction implements MagicFunction {
    @Function
    @Comment("将jsonp数据转为JSONObject")
    public JSONObject toJson(PGobject p){
        log.info("数据:{}",p);

        if(p==null){
            return null;
        }
        if(StringUtils.isEmpty(p.getValue())){
            return null;
        }
        switch (p.getType().toLowerCase()){
            case "json":
            case "jsonb":
                return JSON.parseObject(p.getValue());
        }
        return null;
    }
}

方案 2 、定义函数:

magic-api如何支持posgresql的jsonb数据

import "com.alibaba.fastjson.JSON" as JSON;
import "org.apache.commons.lang3.StringUtils" as StringUtils;
if(v==null){
    return null
}
if(StringUtils.isEmpty(v.getValue())){
    return null
}
var vt=v.getType()
if(vt =="jsonb"){
    return JSON.parseObject(v.getValue())
}
return v

调用:

magic-api如何支持posgresql的jsonb数据

import "@/conver/toJSON" as toJSON
var list=
select t.id,t.formSchema from 
(db.camel().select("select * from magic_api_file")) b 
left join 
(
 db.hyLowCode.camel().select("select * from tb_form_schema")
) t
on t.id = b.id where t.id=b.id
return list.each(it=>it.formSchema=toJSON(it.formSchema))

运行结果:

{
    "code": 1,
    "message": "success",
    "data": [
        {
            "id": 3,
            "formSchema": null
        },
        {
            "id": 2,
            "formSchema": {
                "sdf": "sdf"
            }
        }
    ],
    "timestamp": 1618298030334,
    "executeTime": 41
}

“magic-api如何支持posgresql的jsonb数据”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI