Skip to content

Commit 229771b

Browse files
committed
Use string for jsonb fields in Record types
1 parent e747f31 commit 229771b

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

main.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ async function updateTypes(db, options) {
100100
}
101101
}
102102

103-
function toType(c, enums, overrides, record = false) {
103+
function toType(c, enums, overrides, isRecord = false) {
104104
var _c$default, _c$default2;
105105

106-
let type = ["integer", "numeric", "decimal", "bigint"].includes(c.type) ? "number" : c.type === "boolean" ? "boolean" : c.type === "jsonb" ? (_c$default = c.default) !== null && _c$default !== void 0 && _c$default.startsWith("'{") ? "Record<string, unknown>" : (_c$default2 = c.default) !== null && _c$default2 !== void 0 && _c$default2.startsWith("'[") ? "unknown[]" : "unknown" : c.type === "ARRAY" && c.udt === "_text" ? "string[]" : c.type.startsWith("timestamp") || c.type === "date" ? "Date" : "string";
106+
let type = ["integer", "numeric", "decimal", "bigint"].includes(c.type) ? "number" : c.type === "boolean" ? "boolean" : c.type === "jsonb" ? isRecord ? "string" : (_c$default = c.default) !== null && _c$default !== void 0 && _c$default.startsWith("'{") ? "Record<string, unknown>" : (_c$default2 = c.default) !== null && _c$default2 !== void 0 && _c$default2.startsWith("'[") ? "unknown[]" : "unknown" : c.type === "ARRAY" && c.udt === "_text" ? "string[]" : c.type.startsWith("timestamp") || c.type === "date" ? "Date" : "string";
107107

108108
if (c.type === "USER-DEFINED") {
109109
var _enums$find;
@@ -117,9 +117,9 @@ function toType(c, enums, overrides, record = false) {
117117
}
118118
}
119119

120-
if (type === "Date" && record) {
120+
if (type === "Date" && isRecord) {
121121
type = "Date | string";
122122
}
123123

124-
return `${record ? "Knex.Raw | " : ""}${type}${c.nullable ? " | null" : ""}`;
124+
return `${isRecord ? "Knex.Raw | " : ""}${type}${c.nullable ? " | null" : ""}`;
125125
}

main.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ test("updateTypes", async function () {
8080
name_null?: Knex.Raw | string | null;
8181
roles: Knex.Raw | string[];
8282
roles_null?: Knex.Raw | string[] | null;
83-
credentials?: Knex.Raw | Record<string, unknown>;
84-
credentials_null?: Knex.Raw | unknown | null;
85-
events?: Knex.Raw | unknown[];
83+
credentials?: Knex.Raw | string;
84+
credentials_null?: Knex.Raw | string | null;
85+
events?: Knex.Raw | string;
8686
followers: Knex.Raw | number;
8787
followers_null?: Knex.Raw | number | null;
8888
created_at: Knex.Raw | Date | string;

main.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ function toType(
146146
c: Column,
147147
enums: Enum[],
148148
overrides: Record<string, string>,
149-
record = false
149+
isRecord = false
150150
): string {
151151
let type = ["integer", "numeric", "decimal", "bigint"].includes(c.type)
152152
? "number"
153153
: c.type === "boolean"
154154
? "boolean"
155155
: c.type === "jsonb"
156-
? c.default?.startsWith("'{")
156+
? isRecord
157+
? "string"
158+
: c.default?.startsWith("'{")
157159
? "Record<string, unknown>"
158160
: c.default?.startsWith("'[")
159161
? "unknown[]"
@@ -171,9 +173,11 @@ function toType(
171173
}
172174
}
173175

174-
if (type === "Date" && record) {
176+
if (type === "Date" && isRecord) {
175177
type = "Date | string";
176178
}
177179

178-
return `${record ? "Knex.Raw | " : ""}${type}${c.nullable ? " | null" : ""}`;
180+
return `${isRecord ? "Knex.Raw | " : ""}${type}${
181+
c.nullable ? " | null" : ""
182+
}`;
179183
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "knex-types",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Generates TypeScript definitions (types) from a (PostgreSQL) database schema.",
55
"keywords": [
66
"database",

0 commit comments

Comments
 (0)