2.0.4 • Published 11 months ago

airtable-schema-gen v2.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Airtable to Zod Code Generator

An Airtable to Zod Code Generator that generates zod schemas for Airtable records.

We also ship an accessor to handle Airtable's JS SDK.

Usage

npx airtable-schema-gen

CLI Options

--version              Show version number                       [boolean]
--api-key              Your Airtable api key. Reads AIRTABLE_API_KEY in en
                       vironment. (required)                      [string]
--base-id              Your Airtable base id. Reads AIRTABLE_BASE_ID in en
                       vironment. (required)                      [string]
--base-name            Your Airtable base name. Used for the accessor. Rea
                       ds AIRTABLE_BASE_NAME in environment. (optional: de
                       faults to 'default')                       [string]
--table-ids            Space separated list of Airtable table IDs to inclu
                       de (optional).                              [array]
--out-dir              Output directory. (optional: defaults to 'out')
                                                                  [string]
--schema-out-dir       Output directory for the schemas file.     [string]
--accessor-out-dir     Output directory for the accessor file.    [string]
-v, --verbose              Enable verbose logging.  [boolean] [default: false]
-h, --help                 Show help                                 [boolean]

Example

import Airtable from "airtable"
import { defaultBaseAccessor } from "./defaultBase.accessor"
import * as defaultSchemas from "./defaultBase.schemas"

const apiKey = process.env.AIRTABLE_API_KEY!

Airtable.configure({
  apiKey,
  endpointUrl: "https://api.airtable.com",
})

// You can export this from here or create an airtable object,
// add this as a key, and export the airtable object instead.
export const defaultBase = {
  ...defaultSchemas,
  base: Airtable.base(defaultBaseAccessor.id)
  tables: defaultTables,
}

Then somewhere else:

import { defaultBase } from "./wherever-you-keep-this"

const { base, tables, exampleRecordSchema } = defaultBase

const exampleData = await base(tables.example.id)
  .select()
  .then((records) =>
    records.map((record) => {
      const result = exampleRecordSchema.parse(record.fields)
      // add the id to the schema if you want
      return { ...result, id: record.id }
      // otherwise return the result
      return result
    }),
  )
2.0.4

11 months ago

2.0.3

11 months ago

2.0.2

11 months ago

2.0.1

11 months ago

2.0.0

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago