summaryrefslogtreecommitdiffstats
path: root/src/app/_layout.tsx
blob: e2eb93610f16ecf0b19303fdecd7cf0db640d656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Slot, Stack } from "expo-router";
import React, { Suspense } from 'react'
import AppContextProvider from "../context/AppContextProvider";
import { ActivityIndicator, Text, View } from "react-native";
import { openDatabaseSync, SQLiteProvider } from "expo-sqlite";
import { drizzle } from "drizzle-orm/expo-sqlite";
import { sql } from 'drizzle-orm';
import { migrate, useMigrations } from "drizzle-orm/expo-sqlite/migrator";
import migrations from "../../drizzle/migrations";
type Props = {}
export const DATABASE_NAME = 'prueba3Drizzle';
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
function LoadingScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <ActivityIndicator size="large" color="#0000ff" />
      <Text >Cargando la app...</Text>
    </View>
  );
}
const RootLayout = (props: Props) => {
  return (
    <AppContextProvider>
      <Suspense fallback={<LoadingScreen />}>
        <SQLiteProvider
          databaseName={DATABASE_NAME}
          options={{ enableChangeListener: true }}
          useSuspense
          onInit={async (database) => {
            try {
              console.log("Simulando carga lenta...");
              await sleep(2000); // 2 segundos retraso 
              const db = drizzle(database);
              await migrate(db, migrations);
              console.log("Migration success");

              // Activa FK aquí (después de migraciones)
              await db.run(sql`PRAGMA foreign_keys = ON`);
              console.log("Foreign keys enabled");

            } catch (error) {
              console.error("Migration error", error);
            }
          }}
        >
          <Stack
            screenOptions={{ headerShown: false }}
          
          />

        </SQLiteProvider>
      </Suspense>
    </AppContextProvider>

  )
}

export default RootLayout;