Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/publish.yml → .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build React Native Windows and Publish to NPM
name: CD

on:
push:
Expand All @@ -9,7 +9,6 @@ permissions:

jobs:
build-react-native-windows:
name: Build & Test React Native Windows
runs-on: windows-2022

steps:
Expand All @@ -33,7 +32,6 @@ jobs:
working-directory: ./example

publish-to-npm:
name: Publish Package to NPM
runs-on: ubuntu-latest
needs: build-react-native-windows

Expand All @@ -60,5 +58,6 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# needed to release on github unless we use release-it ^19
# https://github.com/release-it/release-it/issues/1238
# Use release-it ^19 or set NODE_DEBUG
NODE_DEBUG: release-it:*
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: CI
on:
push:
branches:
- main
# push:
# branches:
# - main
pull_request:
branches:
- main
Expand Down Expand Up @@ -51,7 +51,6 @@ jobs:
run: yarn prepare && yarn codegen

build-react-native-windows:
name: Build & Test React Native Windows
runs-on: windows-2022

steps:
Expand Down
102 changes: 75 additions & 27 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TextInput,
Alert,
ActivityIndicator,
Modal,
} from 'react-native';
import { openDatabase, SQLiteDatabase } from 'react-native-sqlite-windows';

Expand All @@ -32,6 +33,9 @@ const App = () => {
const [email, setEmail] = useState('');
const [age, setAge] = useState('');

const [editVisible, setEditVisible] = useState(false);
const [editUser, setEditUser] = useState<any | null>(null);
const [editName, setEditName] = useState('');
// Initialize database
const initDatabase = async () => {
try {
Expand Down Expand Up @@ -125,7 +129,7 @@ const App = () => {
setLoading(true);
const result = await db.executeSql(
'INSERT INTO users (name, email, age) VALUES (?, ?, ?)',
[name, email, parseInt(age)]
[name, email, parseInt(age, 10)]
);

// Ensure insertId is valid number
Expand All @@ -148,27 +152,27 @@ const App = () => {
}
};

// Update user
// const updateUser = async (id: number, newName: string) => {
// if (!db) return;

// try {
// setLoading(true);
// const result = await db.executeSql(
// 'UPDATE users SET name = ? WHERE id = ?',
// [newName, id]
// );

// setStatus(`Updated ${result.rowsAffected} user(s)`);
// Alert.alert('Success', `User updated successfully`);
// await loadUsers();
// } catch (error) {
// setStatus(`Error updating user: ${error}`);
// Alert.alert('Update Error', String(error));
// } finally {
// setLoading(false);
// }
// };
//Update user
const updateUser = async (id: number, newName: string) => {
if (!db) return;

try {
setLoading(true);
const result = await db.executeSql(
'UPDATE users SET name = ? WHERE id = ?',
[newName, id]
);

setStatus(`Updated ${result.rowsAffected} user(s)`);
Alert.alert('Success', `User updated successfully`);
await loadUsers();
} catch (error) {
setStatus(`Error updating user: ${error}`);
Alert.alert('Update Error', String(error));
} finally {
setLoading(false);
}
};

// Delete user
const deleteUser = async (id: number, userName: string) => {
Expand Down Expand Up @@ -486,14 +490,14 @@ const App = () => {
<Text style={styles.userDetail}>Age: {user.age}</Text>
</View>
<View style={styles.userActions}>
{/* <Button
<Button
title="Edit"
onPress={() => {
Alert.alert('Update Name', 'Enter new name:', (text) =>
updateUser(user.id, text)
);
setEditUser(user);
setEditName(user.name);
setEditVisible(true);
}}
/> */}
/>
<Button
title="Delete"
color="red"
Expand All @@ -505,6 +509,37 @@ const App = () => {
)}
</View>
)}
{/* Edit Modal */}
<Modal visible={editVisible} transparent animationType="fade">
<View style={styles.modalOverlay}>
<View style={styles.modalContainer}>
<Text style={styles.sectionTitle}>Edit User</Text>
<TextInput
style={styles.input}
placeholder="Enter new name"
value={editName}
onChangeText={setEditName}
/>
<View style={styles.buttonRow}>
<Button
title="Save"
onPress={() => {
if (editUser && editName.trim()) {
updateUser(editUser.id, editName.trim());
setEditVisible(false);
Alert.alert('Success', 'User updated');
}
}}
/>
<Button
title="Cancel"
color="gray"
onPress={() => setEditVisible(false)}
/>
</View>
</View>
</View>
</Modal>
</ScrollView>
</SafeAreaView>
);
Expand Down Expand Up @@ -604,6 +639,19 @@ const styles = StyleSheet.create({
fontSize: 16,
padding: 20,
},
modalOverlay: {
flex: 1,
backgroundColor: 'rgba(0,0,0,0.4)',
justifyContent: 'center',
alignItems: 'center',
},
modalContainer: {
backgroundColor: 'white',
padding: 20,
borderRadius: 12,
width: '80%',
elevation: 5,
},
});

export default App;
17 changes: 13 additions & 4 deletions windows/ReactNativeSqliteWindows/ReactNativeSqliteWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ReactNativeSqliteWindows.h"
#include "sqlite3.h" // native SQLite C API
#include <filesystem>
#include <winrt/Windows.Storage.h>

namespace winrt::ReactNativeSqliteWindows
{
Expand All @@ -24,11 +25,19 @@ namespace winrt::ReactNativeSqliteWindows

std::wstring ReactNativeSqliteWindows::GetDatabasePath(std::string const& name)
{

using namespace winrt::Windows::Storage;

// Convert the UTF-8 db name to UTF-16 for Windows
std::wstring wname(name.begin(), name.end());
std::filesystem::path folder = std::filesystem::temp_directory_path();
std::filesystem::path fullPath = folder / (wname + L".db");
OutputDebugStringW((L"GetDatabasePath: " + fullPath.wstring() + L"\n").c_str());
return fullPath.wstring();

// Get the app’s local storage folder (safe, persistent)
auto localFolder = ApplicationData::Current().LocalFolder().Path();

// Combine it with the database file name
std::filesystem::path dbPath = std::filesystem::path(localFolder.c_str()) / (wname + L".db");

return dbPath.wstring();
}

void ReactNativeSqliteWindows::open(std::string dbName, React::ReactPromise<std::string>&& promise) noexcept {
Expand Down