Retrieve all asset categories, optionally filtering by active status:
import { listAssetCategories } from '@/services/assetCategoryService';// Get all categories (including inactive)const allCategories = await listAssetCategories();// Get only active categoriesconst activeCategories = await listAssetCategories({ includeInactive: false});console.log(`Found ${allCategories.length} total categories`);console.log(`Active categories: ${activeCategories.length}`);
The listAssetCategories function (assetCategoryService.ts:8-24) returns categories sorted alphabetically by name.
Define the category name and optional description:
import type { AssetCategoryInsert } from '@/types/AssetCategory';const newCategory: AssetCategoryInsert = { name: 'Electrical Equipment', description: 'Electrical panels, transformers, and power distribution equipment', is_active: true};
2
Create the Category
Use the createAssetCategory function:
import { createAssetCategory } from '@/services/assetCategoryService';await createAssetCategory(newCategory);console.log('Category created successfully');
3
Verify Creation
Reload your category list to confirm the new category appears.
The createAssetCategory function (assetCategoryService.ts:26-29) throws an error if creation fails. Wrap calls in try-catch blocks for proper error handling.
Deactivating a category does NOT affect existing assets assigned to that category. Assets retain their category assignment, but the category won’t appear in new asset creation forms.
When a category is deactivated:
Existing assets keep their category_id reference
Category dropdown lists exclude the inactive category
import { deleteAssetCategory } from '@/services/assetCategoryService';await deleteAssetCategory(5);console.log('Category permanently deleted');
Hard Deletion Alert: The deleteAssetCategory function (assetCategoryService.ts:51-57) permanently removes the category from the database.Before deleting:
Verify no assets are currently using this category
Consider deactivating instead of deleting to preserve historical data
The v_assets view automatically includes category information:
import { getAssets } from '@/services/assetsService';const assets = await getAssets();// Each asset includes category_name from the joined tableassets.forEach(asset => { console.log(`Asset: ${asset.code}`); console.log(`Category: ${asset.category_name ?? 'Uncategorized'}`);});
The join happens in the database view definition, providing optimal performance for category filtering and display (assetsService.ts:47-53).