Fauna Typed is a powerful command-line tool designed to generate TypeScript type definitions based on your Fauna database schema. By automating the creation of type-safe interfaces, it ensures seamless integration between your Fauna database and your TypeScript projects, enhancing developer productivity and reducing runtime errors.
-
Automated Type Generation: Quickly generate TypeScript types reflecting your Fauna collections and their schemas.
-
Seamless Integration: Easily integrate the generated types into your existing TypeScript projects.
You can install Fauna Typed as a development dependency with the package manager of your choice.
pnpm add -D fauna-typed
npm install --save-dev fauna-typed
yarn add -D fauna-typed
After installation, you can use the CLI to generate TypeScript types based on your Fauna database schema.
npx fauna-typed --secret=YOUR_FAUNA_ADMIN_KEY
-
-s, --secret <faunaSecret>
Description: Fauna admin secret.
Required: Yes, unless theFAUNA_ADMIN_KEY
environment variable is set. -
-d, --dir <directory>
Description: Directory to generate types files.
Default:src/fauna-typed
-
-h, --help
Description: Display help for the command.
npx fauna-typed --secret="YOUR_FAUNA_ADMIN_KEY"
This command generates the TypeScript types in the default directory src/fauna-typed
.
npx fauna-typed --secret="YOUR_FAUNA_ADMIN_KEY" --dir="path/to/custom-dir"
npx fauna-typed -s "YOUR_FAUNA_ADMIN_KEY" -d "path/to/custom-dir"
Note: When using short flags (-s
), do not use an equal sign (=
). Instead, separate the flag and its value with a space or no space.
Upon successful execution, the CLI generates two key files in your specified directory:
-
custom.ts
Contains the TypeScript type definitions based on your Fauna schema. -
system.ts
Contains the Fauna system type definitions like Collection, Role, AccessProvider, Function, etc.
your-project/
├── src/
│ ├── fauna-typed/
│ │ ├── custom.ts
│ │ └── system.ts
│ └── ...
└── ...
The generated types provide seamless integration between your Fauna database and your TypeScript codebase.
Example: User.all()
import { Client, fql } from 'fauna';
import type { Page, Document } from 'fauna-typed/system';
import { User } from 'fauna-typed/custom';
const client = new Client({
secret: '<YOUR_FAUNA_KEY>'
});
const response = await client.query<Page<Document<User>>>(fql`User.all()`); // Returned type: QuerySuccess<Page<Document<User>>>
const page = response.data; // type: Page<Document<User>>
const data = page.data; // type: Array<Document<User>>
Example: User.all().first()
import { Client, fql } from 'fauna';
import type { Document } from 'fauna-typed/system';
import { User } from 'fauna-typed/custom';
const client = new Client({
secret: '<YOUR_FAUNA_KEY>'
});
const response = await client.query<Document<User>>(fql`User.all().first()`); // Returned type: QuerySuccess<Document<User>>
const data = response.data; // type: Document<User>
Example: Collection.all().first()
import { Client, fql } from 'fauna';
import type { NamedDocument, Collection } from 'fauna-typed/system';
const client = new Client({
secret: '<YOUR_FAUNA_KEY>'
});
const response = await client.query<NamedDocument<Collection>>(fql`Collection.all().first()`); // Returned type: QuerySuccess<NamedDocument<Collection>>
const data = response.data; // type: NamedDocument<Collection>
If you wish to contribute to Fauna Typed or customize it further, follow these steps:
-
Clone the Repository
git clone https://github.com/yourusername/fauna-typed.git cd fauna-typed
-
Install Dependencies
pnpm install
-
Build the Project
pnpm build
-
Run in Development Mode
pnpm dev --secret="YOUR_FAUNA_ADMIN_KEY"
-
Fork the Repository
-
Create a New Branch
git checkout -b feature/YourFeatureName
-
Make Your Changes
-
Commit Your Changes
git commit -m "Add Your Feature"
-
Push to Your Fork
git push origin feature/YourFeatureName
-
Open a Pull Request
This project is licensed under the MIT License.