Drizzle Columns Schema JSON 将我的 JSON 作为文本存储在 PostgreSQL 中

pab*_*pab 4 postgresql orm json drizzle typescript

我使用 Drizzle 作为 Typescript 后端,为一些 API 端点提供服务。我的数据库是 Postgresql,有一个 JSON 列。

export const transactions = pgTable("transactions", {
    id: serial("id").primaryKey(),
    my_json: json('my_json')
});
Run Code Online (Sandbox Code Playgroud)

我如果尝试从 supabase 表编辑器存储{"hello":"world"},这就是我得到的:

在此输入图像描述

如果我尝试使用 TS/Drizzle 插入我的 {"hello":"world"} ,这就是我得到的:

在此输入图像描述

不知怎的,我找不到设置或方法让 drizzle 将其存储为真正的 JSON 对象而不是它的字符串版本。

Key*_*eyo 6

目前(2023 年 6 月)由于错误而无法实现:https://github.com/drizzle-team/drizzle-orm/issues/724

您需要执行原始 DML 查询: db.execute(sql`INSERT INTO table(foo_id, foo_json)...VALUES(123,${yourObject})`)

例如,这对我有用。

const statement = sql`
        INSERT INTO wikidata_article (wikidata_id, category, grade, en_raw_length, en_url_title, labels, sitelinks)
        VALUES (${articleDetails.wikiDataId}, ${articleDetails.category}, ${articleDetails.grade}, ${articleDetails.enBytes}, ${articleDetails.enUrlTitle}, ${articleDetails.labels}, ${articleDetails.sitelinks})
        ON CONFLICT (wikidata_id) DO UPDATE
        SET 
            category = ${articleDetails.category},
            grade = ${articleDetails.grade},    
            en_raw_length = ${articleDetails.enBytes},
            en_url_title = ${articleDetails.enUrlTitle},
            labels = ${articleDetails.labels},
            sitelinks = ${articleDetails.sitelinks}
        `;
        await db.execute(statement);
Run Code Online (Sandbox Code Playgroud)