/zoq

Convert Zod to BigQuery Schema

Primary LanguageTypeScriptApache License 2.0Apache-2.0

Zoq GitHub Actions NPM Codecov

Convert Zod to BigQuery Schema.

Installation

npm install -S zoq

Usage

import { BigQuery } from "@google-cloud/bigquery";
import { z } from "zod";
import { convert, RegExpDate, RegExpTime } from "zoq";

async function createTable() {
  const definition = z.object({
    bool: z.boolean().nullable(),
    numeric: z.number().nullable(),
    int64: z.number().int().nullable(),
    bigint: z.bigint().nullable(),
    string: z.string().nullable(),
    timestamp: z.date().nullable(),
    date: z.string().regex(RegExpDate).nullable(),
    time: z.string().regex(RegExpTime).nullable(),
  });
  const schema = convert(definition);

  console.log(schema);
  // outputs -> [
  //   { name: "bool", type: "BOOL", mode: "NULLABLE" },
  //   { name: "numeric", type: "NUMERIC", mode: "NULLABLE" },
  //   { name: "int64", type: "INT64", mode: "NULLABLE" },
  //   { name: "bigint", type: "BIGNUMERIC", mode: "NULLABLE" },
  //   { name: "string", type: "STRING", mode: "NULLABLE" },
  //   { name: "timestamp", type: "TIMESTAMP", mode: "NULLABLE" },
  //   { name: "date", type: "DATE", mode: "NULLABLE" },
  //   { name: "time", type: "TIME", mode: "NULLABLE" },
  // ];

  await new BigQuery()
    .dataset("dataset_id")
    .createTable("table_id", { schema });
}