blob: 58b1d2d537497b956c6fe43411c4ea2338c2fa0f (
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
|
import { StyleSheet, Text, View, Button} from 'react-native'
import React from 'react'
import { useSQLiteContext } from 'expo-sqlite';
import { drizzle, useLiveQuery } from 'drizzle-orm/expo-sqlite';
import { personas } from '../db/schema';
import { SafeAreaView } from 'react-native-safe-area-context';
const GuardarPersona = () => {
const db = useSQLiteContext();
const drizzleDb = drizzle(db, { schema: { personas } });
const { data: lista } = useLiveQuery(drizzleDb.select().from(personas));
async function almacenarPersona(){
let edadAleatoria = Math.trunc(Math.random()*100);
let nombreAleatorio = "persona_"+Math.trunc(Math.random()*10000);
await drizzleDb.insert(personas).values({ nombre: nombreAleatorio, edad: edadAleatoria });
}
return (
<SafeAreaView style={{ flex: 1, justifyContent: 'flex-start', alignItems: 'center' }}>
<Text style={{fontSize: 24, fontWeight: 'bold', marginBottom: 20 }}>Guardar Persona</Text>
<Button title='Guardar Persona' onPress={almacenarPersona} />
<View style={{marginTop:20, flex: 1}}>
<Text>Lista de personas guardadas:</Text>
{lista?.map((p)=><Text key={p.id}>{p.id} - {p.nombre} - {p.edad} años</Text>)}
</View>
</SafeAreaView>
)
}
export default GuardarPersona
const styles = StyleSheet.create({})
|