MCPcopy Create free account
hub / github.com/cloudflare/templates / selectDataSource

Function selectDataSource

react-postgres-fullstack-template/api/lib/utils.js:8–24  ·  view source on GitHub ↗
(c, dbLogic, mockLogic)

Source from the content-addressed store, hash-verified

6 * @returns {Response} API response
7 */
8export async function selectDataSource(c, dbLogic, mockLogic) {
9 try {
10 // Use mock data if database is not available
11 if (!c.env.DB_AVAILABLE) {
12 return await mockLogic(c);
13 }
14
15 // Use database if available
16 return await dbLogic(c);
17 } catch (e) {
18 console.error("API Error:", e);
19 return Response.json(
20 { error: e instanceof Error ? e.message : e },
21 { status: 500 },
22 );
23 }
24}
25
26/**
27 * Contains mock data logic functions for book-related endpoints

Callers 2

book-related.jsFile · 0.90
books.jsFile · 0.90

Calls 4

errorMethod · 0.65
jsonMethod · 0.65
mockLogicFunction · 0.50
dbLogicFunction · 0.50

Tested by

no test coverage detected