Skip to content

Commit

Permalink
Use string for jsonb fields in Record types
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Apr 14, 2021
1 parent e747f31 commit 229771b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ async function updateTypes(db, options) {
}
}

function toType(c, enums, overrides, record = false) {
function toType(c, enums, overrides, isRecord = false) {
var _c$default, _c$default2;

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";
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";

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

if (type === "Date" && record) {
if (type === "Date" && isRecord) {
type = "Date | string";
}

return `${record ? "Knex.Raw | " : ""}${type}${c.nullable ? " | null" : ""}`;
return `${isRecord ? "Knex.Raw | " : ""}${type}${c.nullable ? " | null" : ""}`;
}
6 changes: 3 additions & 3 deletions main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ test("updateTypes", async function () {
name_null?: Knex.Raw | string | null;
roles: Knex.Raw | string[];
roles_null?: Knex.Raw | string[] | null;
credentials?: Knex.Raw | Record<string, unknown>;
credentials_null?: Knex.Raw | unknown | null;
events?: Knex.Raw | unknown[];
credentials?: Knex.Raw | string;
credentials_null?: Knex.Raw | string | null;
events?: Knex.Raw | string;
followers: Knex.Raw | number;
followers_null?: Knex.Raw | number | null;
created_at: Knex.Raw | Date | string;
Expand Down
12 changes: 8 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ function toType(
c: Column,
enums: Enum[],
overrides: Record<string, string>,
record = false
isRecord = false
): string {
let type = ["integer", "numeric", "decimal", "bigint"].includes(c.type)
? "number"
: c.type === "boolean"
? "boolean"
: c.type === "jsonb"
? c.default?.startsWith("'{")
? isRecord
? "string"
: c.default?.startsWith("'{")
? "Record<string, unknown>"
: c.default?.startsWith("'[")
? "unknown[]"
Expand All @@ -171,9 +173,11 @@ function toType(
}
}

if (type === "Date" && record) {
if (type === "Date" && isRecord) {
type = "Date | string";
}

return `${record ? "Knex.Raw | " : ""}${type}${c.nullable ? " | null" : ""}`;
return `${isRecord ? "Knex.Raw | " : ""}${type}${
c.nullable ? " | null" : ""
}`;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knex-types",
"version": "0.1.2",
"version": "0.1.3",
"description": "Generates TypeScript definitions (types) from a (PostgreSQL) database schema.",
"keywords": [
"database",
Expand Down

0 comments on commit 229771b

Please sign in to comment.