Skip to content

Commit

Permalink
Add citext[] type
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Apr 18, 2021
1 parent 3e778f2 commit e89e64e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async function updateTypes(db, options) {
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" ? 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";
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" || c.udt === "_citext") ? "string[]" : c.type.startsWith("timestamp") || c.type === "date" ? "Date" : "string";

if (c.type === "USER-DEFINED") {
var _enums$find;
Expand Down
14 changes: 9 additions & 5 deletions main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ beforeAll(async function setup() {
await createDatabase();

await db.raw(`CREATE DOMAIN user_id AS TEXT CHECK(VALUE ~ '^[0-9a-z]{6}$')`);
await db.raw(
"CREATE TYPE identity_provider AS ENUM ('google', 'facebook', 'linkedin')"
);
await db.raw(`CREATE TYPE identity_provider AS ENUM ('google', 'facebook', 'linkedin')`); // prettier-ignore

await db.schema.createTable("user", (table) => {
table.specificType("id", "user_id").notNullable().primary();
table.text("name").notNullable();
table.text("name_null");
table.specificType("roles", "text[]").notNullable();
table.specificType("roles_null", "text[]");
table.specificType("roles_citext", "citext[]").notNullable();
table.jsonb("credentials").notNullable().defaultTo("{}");
table.jsonb("credentials_null");
table.jsonb("events").notNullable().defaultTo("[]");
Expand Down Expand Up @@ -65,6 +64,7 @@ test("updateTypes", async function () {
name_null: string | null;
roles: string[];
roles_null: string[] | null;
roles_citext: string[];
credentials: Record<string, unknown>;
credentials_null: unknown | null;
events: unknown[];
Expand All @@ -80,6 +80,7 @@ test("updateTypes", async function () {
name_null?: Knex.Raw | string | null;
roles: Knex.Raw | string[];
roles_null?: Knex.Raw | string[] | null;
roles_citext: Knex.Raw | string[];
credentials?: Knex.Raw | string;
credentials_null?: Knex.Raw | string | null;
events?: Knex.Raw | string;
Expand Down Expand Up @@ -108,8 +109,11 @@ async function createDatabase(): Promise<void> {
}
}

await db.schema.raw("drop schema if exists public cascade");
await db.schema.raw("create schema public");
await db.schema.raw("DROP SCHEMA IF EXISTS public CASCADE");
await db.schema.raw("CREATE SCHEMA public");
await db.raw(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
await db.raw(`CREATE EXTENSION IF NOT EXISTS "hstore"`);
await db.raw(`CREATE EXTENSION IF NOT EXISTS "citext"`);
}

function toString(stream: PassThrough): Promise<string> {
Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function toType(
: c.default?.startsWith("'[")
? "unknown[]"
: "unknown"
: c.type === "ARRAY" && c.udt === "_text"
: c.type === "ARRAY" && (c.udt === "_text" || c.udt === "_citext")
? "string[]"
: c.type.startsWith("timestamp") || c.type === "date"
? "Date"
Expand Down
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.3",
"version": "0.1.4",
"description": "Generates TypeScript definitions (types) from a (PostgreSQL) database schema.",
"keywords": [
"database",
Expand Down

0 comments on commit e89e64e

Please sign in to comment.