54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
'use server'
|
|
|
|
import { Client, Databases, Storage, ID } from 'node-appwrite';
|
|
import { revalidatePath } from 'next/cache';
|
|
|
|
const ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || 'https://appwrite.almeira.com.co/v1';
|
|
const PROJECT_ID = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || '69b84df5003bd8957c5c';
|
|
const DATABASE_ID = 'main';
|
|
|
|
function getAdminClient() {
|
|
return new Client()
|
|
.setEndpoint(ENDPOINT)
|
|
.setProject(PROJECT_ID)
|
|
.setKey(process.env.APPWRITE_API_KEY || '');
|
|
}
|
|
|
|
export async function createDocumentServer(collectionId: string, data: any) {
|
|
const client = getAdminClient();
|
|
const databases = new Databases(client);
|
|
const result = await databases.createDocument(DATABASE_ID, collectionId, ID.unique(), data);
|
|
revalidatePath('/');
|
|
return result;
|
|
}
|
|
|
|
export async function updateDocumentServer(collectionId: string, documentId: string, data: any) {
|
|
const client = getAdminClient();
|
|
const databases = new Databases(client);
|
|
const result = await databases.updateDocument(DATABASE_ID, collectionId, documentId, data);
|
|
revalidatePath('/');
|
|
return result;
|
|
}
|
|
|
|
export async function deleteDocumentServer(collectionId: string, documentId: string) {
|
|
const client = getAdminClient();
|
|
const databases = new Databases(client);
|
|
const result = await databases.deleteDocument(DATABASE_ID, collectionId, documentId);
|
|
revalidatePath('/');
|
|
return result;
|
|
}
|
|
|
|
export async function uploadMediaServer(file: File) {
|
|
const client = getAdminClient();
|
|
const storage = new Storage(client);
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
// Nota: node-appwrite requiere un objeto con buffer y filename
|
|
const result = await storage.createFile('media', ID.unique(), {
|
|
buffer: buffer,
|
|
filename: file.name,
|
|
size: file.size,
|
|
mimeType: file.type
|
|
} as any);
|
|
return result;
|
|
}
|