Products
Create, list, search, edit, delete, and manage products
See here for more information on the Product model: Product Details
Create Product
- Uploading an image is compulsory unless the status is set to
ProductStatus.draft - Multiple images can be selected for a simple product or the parent product of a variable product, otherwise, only one image is allowed.
// upload image(s) first if provided. Ensure to do your checks based on the instructions above
const imageIds: string[] = [];
for (const image of images) {
const res = await storage.createFile(
"PRODUCT_IMAGE_BUCKET_ID",
ID.unique(),
selectedFile
);
const fileId = res.$id;
imageIds.push(fileId);
}
try {
const product = new Product(
ID.unique(),
new Date().toISOString(),
new Date().toISOString(),
acct.$id,
'Product 1',
ProductType.simple,
'Product 1 description',
100,
ProductStatus.published,
true,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
imageIds,
);
await database.createDocument(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]",
product.$id,
product.toMap()
);
console.log('Product created:', product);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error creating product : ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
List All Products
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]"
);
const products = res.documents.map((doc) => Product.fromMap(doc));
const total = res.total; // total number of products ...keep this for pagination and to display to user
console.log('Products fetched:', products);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching products: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
- A list of 20 five products will be returned by a request.
- The total number of products will be returned by a request which can be used for pagination.
- See how pagination is a handled below.
List products by status
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]",
[Query.equal('status', ProductStatus.published)] // filter by status here
);
const products = res.documents.map((doc) => Product.fromMap(doc));
console.log('Products fetched:', products);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching products: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Pagination
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]",
[Query.cursorAfter('LAST_PRODUCT_ID')]
);
const moreProducts = res.documents.map((doc) => Product.fromMap(doc));
console.log('Products fetched:', moreProducts);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching products: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Search Products
When a search query is input, the search function searches the name, desc and shortDesc of the product.
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]",
[
Query.or([
Query.search('name', searchQuery),
Query.search('desc', searchQuery),
Query.search('shortDesc', searchQuery),
]),
Query.limit(100),
]
);
const products = res.documents.map((doc) => Product.fromMap(doc));
console.log('Products fetched:', products);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error fetching products: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Delete A Product
try {
// first delete all images associated with the product
for (const imageId of product.imageIds) {
storage.deleteFile("[PRODUCT_IMAGE_BUCKET_ID]", imageId);
}
await database.deleteDocument(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]",
"[PRODUCT_ID]"
);
console.log('Product deleted successfully');
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error deleting product: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Edit/Update A Product
try {
await database.updateDocument(
"[DATABASE_ID]",
"[PRODUCT_COLLECTION_ID]",
"[PRODUCT_ID]",
{
"status": ProductStatus.published,
"featured": true
}
);
console.log('Product updated successfully');
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error updating product: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Assign A Product To A Vendor
To assign a product to a vendor, we need to search through the list of vendors by their businessName and select the vendor to assign the product to.
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[VENDOR_COLLECTION_ID]",
[Query.search('businessName', searchQuery)]
);
const vendors = res.documents.map((doc) => Vendor.fromMap(doc));
console.log('vendors fetched:', vendors);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error searching vendors: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
Assign Product Category
To assign a product to a category, we need to search through the list of categories by their name and select the category to assign the product to.
try {
const res = await database.listDocuments(
"[DATABASE_ID]",
"[CATEGORY_COLLECTION_ID]",
[Query.search('name', searchQuery)]
);
const categories = res.documents.map((doc) => Category.fromMap(doc));
console.log('categories fetched:', categories);
} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error searching categories: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
When a category is selected, we join its ID to its parent ID with a '/'. E.g: [PARENT_CATEGORY_ID]/[CATEGORY_ID]
Multiple categories can be assigned to a product.
You can also use the upload and delete image methods to create functionality for uploading and deleting images outside of creating or deleting a product.