@contentrain/generator
@contentrain/generator
is a type and schema generator tool for the Contentrain SDK. It generates TypeScript interfaces for your content models and can be used via CLI or programmatically.
Installation
You can install the package via npm:
npm install @contentrain/generator
Usage
CLI Usage
The CLI tool allows you to generate type definitions for your models.
contentrain-generate
contentrain-generate --models ./content --output ./types
Programmatic Usage
You can use the ContentrainGenerator
class to generate type definitions programmatically.
import { ContentrainGenerator } from '@contentrain/generator';
const generator = new ContentrainGenerator({
modelsDir: './content',
outputDir: './types'
});
await generator.generateTypes();
Configuration
The ContentrainGenerator
class supports the following configuration options:
interface GeneratorConfig {
modelsDir?: string;
outputDir?: string;
}
Additionally, you can provide configuration via a contentrain-config.json
file in the root directory. This file will be merged with the default configuration.
Generated Types
This tool generates TypeScript interfaces for your content models. Example:
export interface BaseContentrainModel {
ID: string;
createdAt: string;
updatedAt: string;
status: 'draft' | 'changed' | 'publish';
}
interface IBlogPost extends BaseContentrainModel {
title: string;
content: string;
author: string;
tags: string[];
}
export type ContentrainTypeMap = {
'blog-posts': IBlogPost;
'authors': IAuthor;
}
CLI Options
The CLI tool supports the following options:
Options:
--models Path to the models directory [string] [default: "contentrain/models"]
--output Output path for types [string] [default: "contentrain/types"]
--help Show help [boolean]
--version Show version number [boolean]
License
MIT