项目从akmon迁入到mall

This commit is contained in:
comlibmb
2026-01-21 12:12:22 +08:00
parent cf8236e175
commit d7f95f7fa5
165 changed files with 69160 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
// 示例:如何使用 AkSupa 的 executeAs<T>() 类型转换功能
// 定义数据类型
export type User = {
id: number;
name: string;
email: string;
created_at: string;
avatar_url?: string;
}
export type Post = {
id: number;
title: string;
content: string;
user_id: number;
created_at: string;
updated_at: string;
}
// 使用示例
import AkSupa from '@/components/supadb/aksupa.uts';
export async function demonstrateTypedQueries() {
const supa = new AkSupa('https://your-project.supabase.co', 'your-anon-key');
// 1. 查询数据 - 使用链式调用 + executeAs<T>()
const usersResult = await supa
.from('users')
.select('*')
.eq('status', 'active')
.limit(10)
.executeAs<User[]>();
// 现在 usersResult.data 是 User[] 类型,而不是 UTSJSONObject
if (usersResult.data != null) {
usersResult.data.forEach(user => {
console.log(`用户: ${user.name}, 邮箱: ${user.email}`);
});
}
// 2. 单条记录查询
const userResult = await supa
.from('users')
.select('*')
.eq('id', 1)
.single()
.executeAs<User>();
if (userResult.data != null) {
console.log(`用户名: ${userResult.data.name}`);
}
// 3. 插入数据
const newUser = {
name: '新用户',
email: 'newuser@example.com'
} as UTSJSONObject;
const insertResult = await supa
.from('users')
.insert(newUser)
.executeAs<User[]>();
// 4. 更新数据
const updateResult = await supa
.from('users')
.update({ name: '更新的名称' } as UTSJSONObject)
.eq('id', 1)
.executeAs<User[]>();
// 5. 删除数据
const deleteResult = await supa
.from('users')
.delete()
.eq('id', 1)
.executeAs<User[]>();
// 6. RPC 调用
const rpcResult = await supa
.from('') // RPC 不需要 table
.rpc('get_user_stats', { user_id: 1 } as UTSJSONObject)
.executeAs<{ total_posts: number; total_likes: number }>();
// 7. 复杂查询示例
const complexQuery = await supa
.from('posts')
.select('*, users!posts_user_id_fkey(*)')
.eq('status', 'published')
.gt('created_at', '2024-01-01')
.order('created_at', { ascending: false })
.limit(20)
.executeAs<Post[]>();
return {
users: usersResult.data,
user: userResult.data,
newUser: insertResult.data,
updated: updateResult.data,
deleted: deleteResult.data,
stats: rpcResult.data,
posts: complexQuery.data
};
}
// 平台兼容性说明:
//
// Android 平台uni-app x 3.90+
// - 使用 UTSJSONObject.parse() 进行真正的类型转换
// - 数据会被正确解析为指定的类型 T
// - 如果转换失败,会 fallback 到原始数据
//
// 其他平台Web、iOS、HarmonyOS
// - 使用 as 进行类型断言
// - 这只是 TypeScript 编译时的类型提示,运行时仍然是原始数据
// - 但提供了更好的开发体验和类型安全
//
// 使用优势:
// 1. 统一的 API - 只需要记住 executeAs<T>() 一个方法
// 2. 链式调用 - 可以和所有其他方法组合使用
// 3. 类型安全 - 编译时类型检查运行时类型转换Android
// 4. 简洁明了 - 不需要多个 selectAs/insertAs/updateAs 等方法