Initial commit from template
Some checks failed
Cloudflare Worker API Template / Deploy to ${{ github.ref_name }} environment (push) Failing after 15s
Some checks failed
Cloudflare Worker API Template / Deploy to ${{ github.ref_name }} environment (push) Failing after 15s
This commit is contained in:
91
src/services/local-db-service.js
Normal file
91
src/services/local-db-service.js
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Local SQLite database service for development
|
||||
*/
|
||||
|
||||
// This service provides a fallback for when Cloudflare D1 is not available
|
||||
// It uses in-memory storage for development and testing
|
||||
|
||||
// In-memory database store
|
||||
const inMemoryDb = {
|
||||
users: new Map()
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the local database
|
||||
*/
|
||||
export function initLocalDb() {
|
||||
console.log('Initializing local in-memory database');
|
||||
// Nothing to do for in-memory database
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save user data to local storage
|
||||
* @param {Object} userData - The user data to save
|
||||
* @returns {Promise<Object>} - Result of the operation
|
||||
*/
|
||||
export async function saveUserDataLocal(userData) {
|
||||
try {
|
||||
if (!userData || !userData.uid) {
|
||||
return { success: false, error: 'Missing required user data' };
|
||||
}
|
||||
|
||||
const { uid } = userData;
|
||||
const existingUser = inMemoryDb.users.get(uid);
|
||||
const isUpdate = !!existingUser;
|
||||
|
||||
// Add timestamp
|
||||
const now = new Date().toISOString();
|
||||
const userWithTimestamp = {
|
||||
...userData,
|
||||
created_at: existingUser?.created_at || now,
|
||||
updated_at: now
|
||||
};
|
||||
|
||||
// Save to in-memory database
|
||||
inMemoryDb.users.set(uid, userWithTimestamp);
|
||||
|
||||
console.log(`User data ${isUpdate ? 'updated' : 'saved'} in local database:`, uid);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `User data ${isUpdate ? 'updated' : 'saved'} successfully in local database`,
|
||||
updated: isUpdate,
|
||||
uid
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error saving user data to local database:', error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user data by uid from local storage
|
||||
* @param {string} uid - The user's unique identifier
|
||||
* @returns {Promise<Object|null>} - User data or null if not found
|
||||
*/
|
||||
export async function getUserByUidLocal(uid) {
|
||||
try {
|
||||
if (!uid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return inMemoryDb.users.get(uid) || null;
|
||||
} catch (error) {
|
||||
console.error('Error getting user data from local database:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all users from local storage
|
||||
* @returns {Promise<Array>} - Array of users
|
||||
*/
|
||||
export async function getAllUsersLocal() {
|
||||
try {
|
||||
return Array.from(inMemoryDb.users.values());
|
||||
} catch (error) {
|
||||
console.error('Error getting all users from local database:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user