MCPcopy
hub / github.com/alan2207/bulletproof-react / loadDb

Function loadDb

apps/nextjs-app/src/testing/mocks/db.ts:46–68  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

44const dbFilePath = 'mocked-db.json';
45
46export const loadDb = async () => {
47 // If we are running in a Node.js environment
48 if (typeof window === 'undefined') {
49 const { readFile, writeFile } = await import('fs/promises');
50 try {
51 const data = await readFile(dbFilePath, 'utf8');
52 return JSON.parse(data);
53 } catch (error: any) {
54 if (error?.code === 'ENOENT') {
55 const emptyDB = {};
56 await writeFile(dbFilePath, JSON.stringify(emptyDB, null, 2));
57 return emptyDB;
58 } else {
59 console.error('Error loading mocked DB:', error);
60 return null;
61 }
62 }
63 }
64 // If we are running in a browser environment
65 return Object.assign(
66 JSON.parse(window.localStorage.getItem('msw-db') || '{}'),
67 );
68};
69
70export const storeDb = async (data: string) => {
71 // If we are running in a Node.js environment

Callers 2

persistDbFunction · 0.70
initializeDbFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected