返回 Skill 列表
extension
分类: 开发与工程无需 API Key

implementing-service-layer

定义了一种将Appwrite数据库调用抽象到专用服务文件中的模式。用于保持组件的整洁和可复用性。

person作者: jakexiaohubgithub

Appwrite Service Layer Pattern

When to use this skill

  • Before writing database logic in a component.
  • When a database query needs to be reused across multiple pages or actions.

Folder Structure

  • services/tours.ts
  • services/bookings.ts
  • services/auth.ts

Example Pattern

import { databases, DATABASE_ID, COLLECTIONS } from '@/lib/appwrite';
import { Query } from 'appwrite';
import { Tour } from '@/types';

export const TourService = {
    async getAll(limit = 10) {
        return await databases.listDocuments<Tour>(
            DATABASE_ID,
            COLLECTIONS.TOURS,
            [Query.limit(limit), Query.orderDesc('$createdAt')]
        );
    },
    async getById(id: string) {
        return await databases.getDocument<Tour>(DATABASE_ID, COLLECTIONS.TOURS, id);
    }
};

Instructions

  • Abstraction: Components should call TourService.getAll() rather than the Appwrite SDK directly.
  • Error Handling: Catch errors in the service or re-throw them with descriptive messages.