typegen

package
v0.9.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package typegen turns a realm's loaded config into TypeScript types: documents, queries, functions, configuration, templates and roles. Everything comes from the same registry the server enforces; nothing is enforced at runtime from the generated files.

Index

Constants

View Source
const (
	TypesFile = "types.d.ts"
	InfoFile  = "realm.json"
)

File names written by `backd types`.

View Source
const DartTypesFile = "types.dart"

DartTypesFile is the file `backd types --lang dart` writes.

View Source
const DefaultGoPackage = "backdtypes"

DefaultGoPackage is the package name of types.go when none is asked for.

View Source
const GoTypesFile = "types.go"

GoTypesFile is the file `backd types --lang go` writes.

View Source
const MigrationsFile = "migrations.ts"

MigrationsFile is the runtime module `backd types --lang ts` writes next to the types: the collections' own migrations, as TypeScript functions apps run with `upgrade`.

View Source
const PythonTypesFile = "types.py"

PythonTypesFile is the file `backd types --lang python` writes.

View Source
const SchemaTypesFile = "schema.d.ts"

SchemaTypesFile is the name of the types file of a versioned collection, written next to its schema.vN.json files (design/schema-versioning.md §11.1). The migrations import from it.

View Source
const Version = "1"

Version is the generator's version: it changes when the output format does.

Variables

This section is empty.

Functions

func Diff

func Diff(old, now Info) []string

Diff lists the type-level differences between two generations, one line each, for pull request reviews: `+` something was added, `-` removed, `~` its type changed. Empty when the types are the same.

func Migrations added in v0.9.11

func Migrations(reg *registry.Registry, realm string) (content string, ok bool, err error)

Migrations returns the contents of migrations.ts for the realm: one `upgrade.<collection>` per collection that has migration steps, built from its schema.d.ts types and its schema.vN.ts files. ok is false when the realm has none. The module imports the realm's types from "./types".

func SchemaTypes added in v0.9.11

func SchemaTypes(c *registry.Collection) (content string, warnings []string, ok bool, err error)

SchemaTypes returns the contents of schema.d.ts for a versioned collection: V1…VN, the fields of each kept version (read from its JSON file), Meta, and Current, the highest version. ok is false for a collection without schema.vN.json files. Warnings are what the generator could not express, with where.

func SchemaTypesPath added in v0.9.11

func SchemaTypesPath(c *registry.Collection) string

SchemaTypesPath is where a collection's schema.d.ts lives.

func WriteSchemaFiles added in v0.9.11

func WriteSchemaFiles(reg *registry.Registry, realm, db string) (written []string, warnings []string, err error)

WriteSchemaFiles writes the schema.d.ts of every versioned collection of the realm that is missing or stale, and returns the paths it wrote.

Types

type FunctionsOutput

type FunctionsOutput struct {
	Types    string
	Warnings []string
}

FunctionsOutput is a functions project's backd.d.ts.

func GenerateFunctions

func GenerateFunctions(reg *registry.Registry, realm, database, config string) (*FunctionsOutput, error)

GenerateFunctions produces the backd.d.ts of one database's functions project: the realm's types plus defineFunction and a context typed per function.

type Info

type Info struct {
	Generator string `json:"generator"`
	Realm     string `json:"realm"`
	// SchemaVersions are the current schema versions of the realm's versioned collections
	// ("main/assets": 3), which the clients send as X-Backd-Schema-Version.
	SchemaVersions map[string]int `json:"schema_versions,omitempty"`
	// Fingerprint identifies the generated types (a hash of types.d.ts without its header):
	// what the JavaScript client sends to know whether its types are stale.
	Fingerprint string `json:"fingerprint"`
	// Config is the config directory's fingerprint, when a caller asks to record it; `backd types`
	// leaves it out, so unrelated changes to the config don't touch the committed realm.json.
	Config  string            `json:"config,omitempty"`
	Entries map[string]string `json:"entries"`
}

Info is realm.json: what the generated types were made from, and the type-level entries `--diff` compares.

type Options

type Options struct {
	Realm    string
	Database string // empty: every database of the realm
	// Lang is the language to emit: "ts" (the default), "dart", "go" or "python".
	Lang string
	// Package is the package name of the Go file (--lang go); "backdtypes" when empty.
	Package string
	// Config is registry.Fingerprint of the loaded config; recorded in realm.json.
	Config string
}

Options select what to generate.

type Output

type Output struct {
	Types    string   // contents of types.d.ts
	Info     Info     // contents of realm.json
	Warnings []string // things the generator couldn't express, with where
}

Output is what the generator produces for one realm.

func Generate

func Generate(reg *registry.Registry, o Options) (*Output, error)

Generate produces the types of one realm.

With Lang "dart" the emitted Types are Dart; the Info (entries and fingerprint) still comes from the TypeScript pass, so realm.json is the same file for every language and the stale-types check the server runs matches either client's fingerprint.

type SchemaFileState added in v0.9.11

type SchemaFileState struct {
	Collection *registry.Collection
	Path       string
	Want       string
	Warnings   []string
	// Missing and Stale say the file is not there, or differs.
	Missing, Stale bool
}

SchemaFileState is how a collection's schema.d.ts stands against what the generator makes.

func CheckSchemaFiles added in v0.9.11

func CheckSchemaFiles(reg *registry.Registry, realm, db string) ([]SchemaFileState, error)

CheckSchemaFiles compares the schema.d.ts of every versioned collection of the realm (or of database db, when set) with what the generator makes. Collections without versions have none.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL