chore: Remove environment variable DATABASE_ID and KV_NAMESPACE_ID, auto-pagination kvNamespaces

This commit is contained in:
sunny
2025-03-06 14:26:10 +08:00
parent b75d9ada43
commit 16bc357973
4 changed files with 66 additions and 124 deletions

View File

@@ -5,120 +5,88 @@ const CF_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID!;
const CF_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
const CUSTOM_DOMAIN = process.env.CUSTOM_DOMAIN;
const PROJECT_NAME = process.env.PROJECT_NAME || "moemail";
const DB_NAME = process.env.DATABASE_NAME || "moemail-db";
const KV_NAMESPACE_NAME = process.env.KV_NAME || "moemail-kv";
const DATABASE_NAME = process.env.DATABASE_NAME || "moemail-db";
const KV_NAMESPACE_NAME = process.env.KV_NAMESPACE_NAME || "moemail-kv";
const client = new Cloudflare({
apiKey: CF_API_TOKEN,
});
export const getPages = async () => {
try {
const projectInfo = await client.pages.projects.get(PROJECT_NAME, {
account_id: CF_ACCOUNT_ID,
});
const projectInfo = await client.pages.projects.get(PROJECT_NAME, {
account_id: CF_ACCOUNT_ID,
});
return projectInfo;
} catch (error) {
throw error;
}
return projectInfo;
};
export const createPages = async () => {
try {
console.log(`🆕 Creating new Cloudflare Pages project: "${PROJECT_NAME}"`);
console.log(`🆕 Creating new Cloudflare Pages project: "${PROJECT_NAME}"`);
const project = await client.pages.projects.create({
const project = await client.pages.projects.create({
account_id: CF_ACCOUNT_ID,
name: PROJECT_NAME,
production_branch: "main",
});
if (CUSTOM_DOMAIN) {
console.log("🔗 Setting pages domain...");
await client.pages.projects.domains.create(PROJECT_NAME, {
account_id: CF_ACCOUNT_ID,
name: PROJECT_NAME,
production_branch: "main",
name: CUSTOM_DOMAIN?.split("://")[1],
});
if (CUSTOM_DOMAIN) {
console.log("🔗 Setting pages domain...");
await client.pages.projects.domains.create(PROJECT_NAME, {
account_id: CF_ACCOUNT_ID,
name: CUSTOM_DOMAIN?.split("://")[1],
});
console.log("✅ Pages domain set successfully");
}
console.log("✅ Project created successfully");
return project;
} catch (error) {
throw error;
console.log("✅ Pages domain set successfully");
}
console.log("✅ Project created successfully");
return project;
};
export const getDatabase = async () => {
try {
const database = await client.d1.database.get(DB_NAME, {
account_id: CF_ACCOUNT_ID,
});
const database = await client.d1.database.get(DATABASE_NAME, {
account_id: CF_ACCOUNT_ID,
});
return database;
} catch (error) {
throw error;
}
return database;
};
export const createDatabase = async () => {
try {
console.log(`🆕 Creating new D1 database: "${DB_NAME}"`);
const database = await client.d1.database.create({
account_id: CF_ACCOUNT_ID,
name: DB_NAME,
});
console.log("✅ Database created successfully");
console.log(`🆕 Creating new D1 database: "${DATABASE_NAME}"`);
return database;
} catch (error) {
throw error;
}
};
const database = await client.d1.database.create({
account_id: CF_ACCOUNT_ID,
name: DATABASE_NAME,
});
export const getKVNamespace = async (namespaceId: string) => {
if (!namespaceId) {
throw new Error("KV namespace ID is required");
}
try {
const kvNamespace = await client.kv.namespaces.get(namespaceId, {
account_id: CF_ACCOUNT_ID,
});
console.log("✅ Database created successfully");
return kvNamespace;
} catch (error) {
throw error;
}
return database;
};
export const getKVNamespaceList = async () => {
try {
const kvNamespaces = await client.kv.namespaces.list({
account_id: CF_ACCOUNT_ID,
});
const kvNamespaces = [];
return kvNamespaces;
} catch (error) {
throw error;
for await (const namespace of client.kv.namespaces.list({
account_id: CF_ACCOUNT_ID,
})) {
kvNamespaces.push(namespace);
}
}
return kvNamespaces;
};
export const createKVNamespace = async () => {
try {
console.log(`🆕 Creating new KV namespace: "${KV_NAMESPACE_NAME}"`);
const kvNamespace = await client.kv.namespaces.create({
account_id: CF_ACCOUNT_ID,
title: KV_NAMESPACE_NAME,
});
console.log("✅ KV namespace created successfully");
console.log(`🆕 Creating new KV namespace: "${KV_NAMESPACE_NAME}"`);
return kvNamespace;
} catch (error) {
throw error;
}
};
const kvNamespace = await client.kv.namespaces.create({
account_id: CF_ACCOUNT_ID,
title: KV_NAMESPACE_NAME,
});
console.log("✅ KV namespace created successfully");
return kvNamespace;
};