Commit bddd5725 authored by duanjinfei's avatar duanjinfei

add function

parent d33273f7
......@@ -6,6 +6,34 @@
import "jsr:@supabase/functions-js/edge-runtime.d.ts"
import { createClient } from 'jsr:@supabase/supabase-js@2'
// 分页查询函数
async function fetchAllData(supabase, table: string, pageSize: number = 1000) {
let allData: any[] = [];
let offset = 0;
while (true) {
const { data, error } = await supabase
.from(table)
.select('*') // 可以根据需要调整字段
.order('created_at', { ascending: true }) // 按照创建时间排序
.range(offset, offset + pageSize - 1); // 分页范围
if (error) {
console.error(`Error fetching data from ${table}:`, error);
throw error;
}
if (data && data.length > 0) {
allData = allData.concat(data); // 合并当前页数据
offset += pageSize; // 移动到下一页
} else {
break; // 如果没有更多数据,则退出循环
}
}
return allData;
}
Deno.serve(async (req) => {
try {
// const { name } = await req.json()
......@@ -16,14 +44,14 @@ Deno.serve(async (req) => {
)
// 获取所有 app 数据
const allApps = await fetchAllData('app');
const allApps = await fetchAllData(supabase, 'app');
// 获取当前时间的整点时间戳
const timestamp = Math.floor(Date.now() / 3600000) * 3600;
// 上传所有 app 数据到 storage
const allAppsJson = JSON.stringify(allApps);
const allAppsFileName = `app_all_${timestamp}.json`;
const bucketName = 'your-bucket-name'; // 替换为存储桶名称
const bucketName = 'cache'; // 替换为存储桶名称
const { error: allAppsUploadError } = await supabase.storage
.from(bucketName)
......@@ -90,33 +118,7 @@ Deno.serve(async (req) => {
})
// 分页查询函数
async function fetchAllData(table: string, pageSize: number = 1000) {
let allData: any[] = [];
let offset = 0;
while (true) {
const { data, error } = await supabase
.from(table)
.select('*') // 可以根据需要调整字段
.order('created_at', { ascending: true }) // 按照创建时间排序
.range(offset, offset + pageSize - 1); // 分页范围
if (error) {
console.error(`Error fetching data from ${table}:`, error);
throw error;
}
if (data && data.length > 0) {
allData = allData.concat(data); // 合并当前页数据
offset += pageSize; // 移动到下一页
} else {
break; // 如果没有更多数据,则退出循环
}
}
return allData;
}
/* To invoke locally:
......
......@@ -42,7 +42,11 @@ Deno.serve(async (req) => {
return new Response(JSON.stringify({ error: 'Failed to upload file' }), { status: 500 });
}
return new Response(JSON.stringify({ data }), {
return new Response(JSON.stringify({
code: 200,
data: null,
message: 'success'
}), {
headers: { 'Content-Type': 'application/json' },
status: 200,
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment