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 、定義函數:

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

調用:

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
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章