Skip to main content

Product Categories

Overview

Manage product categories. Check here to see more information on the Category class: Category

Understanding the Category Hierarchy

  • The parent categories have null parentId properties
  • The level 1 subcategories have one parent category
  • The level 2 subcategories have two parent categories joined by /. E.g. mens-wears/jeans

Create Category

  // upload an optional image if provided. 
const res = await storage.createFile(
"PRODUCT_CATEGORIES_IMAGE_BUCKET_ID",
ID.unique(),
selectedFile
);

const fileId = res.$id;

try {
const category = new ProductCategory(
fileId,
new Date().toISOString(),
new Date().toISOString(),
'Category 1',
'0', // optional... we can use the search function to find a parent category
'Category 1 description'
);

await database.createDocument(
"[DATABASE_ID]",
"[PRODUCT_CATEGORY_COLLECTION_ID]",
category.$id,
category.toMap()
);

console.log('Category created:', category);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error creating category : ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}

List Categories

The method below can be used to fetch root, parent or child categories of a category. The configuration is as follows:

  try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[PRODUCT_CATEGORY_COLLECTION_ID]",
[
Query.limit(100),
Query.isNull('parentId'), // use this to fetch root categories
// Query.equal('parentId', '[PARENT_CATEGORY_ID]'), // use this to fetch child categories
]
);

const categories = res.documents.map((doc) => ProductCategory.fromMap(doc));

const total = res.total; // total number of categories ...

console.log('Categories fetched:', categories);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching categories: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}

Search Categories

  try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[PRODUCT_CATEGORY_COLLECTION_ID]",
[Query.search('name', searchQuery)]
);

const categories = res.documents.map((doc) => ProductCategory.fromMap(doc));

console.log('Categories fetched:', categories);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching categories: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}

Delete A Category

  try {
// delete the category image if it exists
storage.deleteFile("PRODUCT_CATEGORIES_IMAGE_BUCKET_ID", "[FILE_ID]");
await database.deleteDocument(
"[DATABASE_ID]",
"[PRODUCT_CATEGORY_COLLECTION_ID]",
"[CATEGORY_ID]"
);

console.log('Category deleted successfully');
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error deleting category: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
note

The file ID is always the same as the category ID

Update A Category

  try {
await database.updateDocument(
"[DATABASE_ID]",
"[PRODUCT_CATEGORY_COLLECTION_ID]",
"[CATEGORY_ID]",
{
"name": "Category 1",
"description": "Category 1 description"
}
);

console.log('Category updated successfully');
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error updating category: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
info

You can also use the upload and delete image methods to create functionality for uploading and deleting images outside of creating or deleting a category.