Directus stores data in collections, which are similar to tables in a traditional database. Each collection can have multiple fields with different data types such as strings, numbers, booleans, etc. Directus provides an API to interact with these collections, allowing you to create, read, update, and delete (CRUD) records.
TypeScript adds types to JavaScript. Types can be primitive types like number
, string
, boolean
, or more complex types such as interfaces, enums, and generics. By using types, TypeScript can catch type - related errors at compile - time, reducing bugs in your code.
When using TypeScript with Directus, the main goal is to have type - safe interactions with the Directus API. This means that the data you receive from the API and the data you send to the API are strongly typed, so you can rely on the TypeScript compiler to catch errors related to incorrect data types.
First, you need to install the Directus SDK and TypeScript in your project.
npm install @directus/sdk typescript --save
You can initialize the Directus client in your TypeScript project as follows:
import { Directus } from '@directus/sdk';
// Replace the URL with your actual Directus instance URL
const directus = new Directus('https://your-directus-instance.com');
// If you need authentication
const authenticate = async () => {
await directus.auth.login({
email: '[email protected]',
password: 'your-password'
});
};
authenticate().then(() => {
console.log('Authenticated successfully');
}).catch((error) => {
console.error('Authentication failed:', error);
});
To fetch data from Directus with type safety, you can define an interface for the data you expect to receive.
interface Article {
id: number;
title: string;
content: string;
published_at: string;
}
const getArticles = async () => {
const { data } = await directus.items<Article>('articles').readByQuery({
fields: ['id', 'title', 'content', 'published_at']
});
return data;
};
getArticles().then((articles) => {
console.log('Fetched articles:', articles);
}).catch((error) => {
console.error('Error fetching articles:', error);
});
For each collection in Directus, it’s a good practice to define an interface in TypeScript. This way, you can ensure that the data you receive from the API and the data you send to the API are of the correct type.
interface User {
id: string;
name: string;
email: string;
role: string;
}
When interacting with the Directus API, it’s important to handle errors properly. You can use try - catch blocks to catch any errors that occur during API calls.
const createUser = async (user: User) => {
try {
const { data } = await directus.items<User>('users').createOne(user);
console.log('User created successfully:', data);
} catch (error) {
console.error('Error creating user:', error);
}
};
Create a separate module or service to handle all Directus API calls. This makes your code more organized and easier to maintain.
// directusService.ts
import { Directus } from '@directus/sdk';
import { Article } from './interfaces';
const directus = new Directus('https://your-directus-instance.com');
export const getArticles = async () => {
const { data } = await directus.items<Article>('articles').readByQuery({
fields: ['id', 'title', 'content', 'published_at']
});
return data;
};
If your Directus collection has fields with fixed values, use enums in TypeScript.
enum UserRole {
ADMIN = 'admin',
EDITOR = 'editor',
VIEWER = 'viewer'
}
interface User {
id: string;
name: string;
email: string;
role: UserRole;
}
Combining Directus with TypeScript offers significant advantages in terms of code quality, maintainability, and developer experience. By leveraging TypeScript’s type system, you can ensure type - safe interactions with the Directus API, catch errors early, and write more robust applications. By following the usage methods, common practices, and best practices outlined in this blog, you can effectively use Directus with TypeScript in your projects.