npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@notion-headless-cms/cli

v2.0.3

Published

CLI tool for notion-headless-cms — generates schema files from Notion databases

Readme

@notion-headless-cms/cli

Notion データベースを introspect して TypeScript スキーマファイルを自動生成する CLI ツール。Prisma の prisma db pull に相当するワークフローを Notion に対して実現する。

インストール

pnpm add -D @notion-headless-cms/cli

クイックスタート

# 1. 設定ファイルのテンプレートを生成
npx nhc init

# 2. nhc.config.ts を編集(DB 名 / ID を設定)

# 3. Notion DB を introspect してスキーマを生成
NOTION_TOKEN=secret_xxx npx nhc generate

生成された nhc.schema.ts には DB 構造(schema 定数)だけが入る。@notion-headless-cms/notion-sourcenotionSource() に渡して createClient を組み立てる:

import { createClient, memoryCache } from "@notion-headless-cms/core";
import { notionSource } from "@notion-headless-cms/notion-source";
import { schema } from "./generated/nhc.schema";

const cms = createClient({
  sources: {
    notion: notionSource({
      schema,
      token: process.env.NOTION_TOKEN!,
      publishOptions: {
        posts: { publishedStatuses: ["公開済み"] },
      },
    }),
  },
  cache: [memoryCache()],
  swr: { ttlMs: 5 * 60_000 },
});

const posts = await cms.posts.list();

nhc.config.ts

defineConfig() で設定を定義し、default export する。env() は遅延評価の環境変数ヘルパー。

import "dotenv/config";
import { defineConfig, env } from "@notion-headless-cms/cli";

export default defineConfig({
  notionToken: env("NOTION_TOKEN"),
  output: "src/generated/nhc.schema.ts",
  collections: {
    posts: {
      dbName: "ブログ記事DB",
      // databaseId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // dbName の代わりに直接指定可
      publishedStatuses: ["公開済み"],
      accessibleStatuses: ["下書き", "公開済み"],
      // slugField: "slug",     // デフォルト "slug"
      // statusField: "status", // デフォルト "status"
      // 日本語など ASCII 変換不能なプロパティ名は明示マッピング必須
      // fieldMappings: { "タイトル": "title", "カテゴリ": "category" },
    },
  },
});

注意: publishedStatuses / accessibleStatusesnhc.config.ts に書いても生成ファイルには埋め込まれない。生成物は DB 構造(schema)のみを持ち、ランタイムの公開ステータスは notionSource({ publishOptions }) で指定する。nhc.config.ts に書く意義は「設定を一元管理する備忘録」のみ。

CollectionGenConfig オプション

| フィールド | 必須 | 説明 | |---|---|---| | dbName | (databaseId 未指定時) | Notion DB 名(完全一致で検索) | | databaseId | (dbName 未指定時) | Notion DB ID(指定時は dbName より優先) | | slugField | – | slug に使う TS フィールド名(デフォルト "slug") | | statusField | – | status に使う TS フィールド名(デフォルト "status") | | publishedStatuses | – | notionSource({ publishOptions }) への移行用メモ | | accessibleStatuses | – | 同上 | | fieldMappings | – | { Notion プロパティ名: TS フィールド名 } の明示マッピング |

コマンド一覧

nhc init

nhc.config.ts のテンプレートを生成。

Options:
  -o, --output <path>    出力先 (デフォルト: nhc.config.ts)
  -t, --template <name>  ランタイム別テンプレート (node / cloudflare-react-router / cloudflare-hono / next)
  -f, --force            既存ファイルを上書き

--template はランタイムに合った output パスと「次のステップ」(依存・binding・example 導線) を出力する。 例: nhc init --template cloudflare-react-router

nhc generate

nhc.config.ts を読み込み、Notion DB を introspect して nhc.schema.ts を生成。

Options:
  -c, --config <path>    設定ファイルパス (デフォルト: nhc.config.ts)
  -t, --token <token>    Notion API トークン (省略時は NOTION_TOKEN)
  --env-file <path>      任意の env ファイル (未指定なら .dev.vars 自動検出)
  -s, --silent           ログ抑制

プロパティ型マッピング

| Notion | TypeScript | フィールド型 | |---|---|---| | title | string \| nullslugField 指定時は string) | "title" | | rich_text | string \| null | "richText" | | select | string \| null | "select" | | status | "値1" \| "値2" \| ... \| null(literal union) | "status" | | multi_select | string[] | "multiSelect" | | date | string \| null | "date" | | number | number \| null | "number" | | checkbox | boolean | "checkbox" | | url | string \| null | "url" | | その他 | — | スキップ(コメント付き) |

生成ファイルの構造

// 自動生成 — 編集不要
import type { PropertyMap } from "@notion-headless-cms/core";
import type { SchemaMap } from "@notion-headless-cms/notion-source";

export const postsDataSourceId = "xxx-yyy-zzz";

export const postsProperties = {
  slug: { type: "richText" as const, notion: "URL" },
  status: { type: "status" as const, notion: "ステータス" },
  // ...
} as const satisfies PropertyMap;

export interface Post {
  id: string;
  lastEditedTime: string;
  slug: string;
  status: "公開済み" | "下書き" | null;
  // ...
}

// 全コレクションのスキーマ集約 (notionSource() に渡す)
export const schema = {
  posts: {
    dataSourceId: postsDataSourceId,
    properties: postsProperties,
    slugField: "slug",
    statusField: "status",
  },
} as const satisfies SchemaMap;

エラーコード

CLI が throw するエラーは CMSErrorcli/* 名前空間で分類される:

  • cli/config_invalidnhc.config.ts の内容不整合
  • cli/config_load_failed — 設定ファイル読み込み失敗
  • cli/schema_invalid — スキーマ/マッピング不整合
  • cli/generate_failednhc generate 処理失敗
  • cli/init_failednhc init 処理失敗
  • cli/notion_api_failed — Notion API 呼び出し失敗
  • cli/env_file_not_found--env-file 指定ファイルが存在しない

関連パッケージ