This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: persistent storage connection rework
- Loading branch information
Showing
6 changed files
with
225 additions
and
70 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { ChatRoomListItem, Contact, MessageItem } from "../components/Types"; | ||
import logger from "../Logger"; | ||
import { | ||
fetchChatRooms, | ||
fetchContacts, | ||
fetchMessages, | ||
getDbConnection, | ||
insertContact, | ||
insertMessage, | ||
} from "./SqlStorageMiddlewares"; | ||
|
||
export const getContacts = async (): Promise<Contact[] | void> => { | ||
logger.info("Getting chat rooms."); | ||
const contacts: Contact[] = Array<Contact>(); | ||
logger.info("Obtaining database session."); | ||
const dbSession = await getDbConnection(); | ||
const results = await fetchContacts(dbSession); | ||
if (results === undefined) { | ||
logger.warn("Failed to fetch contacts."); | ||
} else { | ||
results.forEach((result) => { | ||
for (let index = 0; index < result.rows.length; index++) { | ||
contacts.push(result.rows.item(index)); | ||
} | ||
}); | ||
} | ||
logger.info("Returning contacts list."); | ||
return contacts; | ||
}; | ||
|
||
export const saveContact = async (contact: Contact): Promise<boolean> => { | ||
logger.info("Saving new contact to SQL database."); | ||
logger.info("Obtaining database session."); | ||
const dbSession = await getDbConnection(); | ||
logger.info("Dispatching database middleware."); | ||
const results = await insertContact(dbSession, contact); | ||
if (results) { | ||
logger.info("Database middleware dispatched. contact saved successfully."); | ||
return true; | ||
} else { | ||
logger.error("Database middleware dispatched. Failed to save a contact."); | ||
return false; | ||
} | ||
}; | ||
|
||
export const getChatRooms = async (): Promise<ChatRoomListItem[] | void> => { | ||
logger.info("Getting chat rooms."); | ||
const chatRooms: ChatRoomListItem[] = Array<ChatRoomListItem>(); | ||
logger.info("Obtaining database session."); | ||
const dbSession = await getDbConnection(); | ||
const results = await fetchChatRooms(dbSession); | ||
if (results === undefined) { | ||
logger.warn("Failed to fetch chat rooms."); | ||
} else { | ||
results.forEach((result) => { | ||
for (let index = 0; index < result.rows.length; index++) { | ||
chatRooms.push(result.rows.item(index)); | ||
} | ||
}); | ||
} | ||
logger.info("Returning chat rooms list."); | ||
return chatRooms; | ||
}; | ||
|
||
export const getMessages = async (contact: Contact): Promise<MessageItem[] | void> => { | ||
logger.info("Getting messages with a provided contact."); | ||
const messages: MessageItem[] = Array<MessageItem>(); | ||
logger.info("Obtaining database session."); | ||
const dbSession = await getDbConnection(); | ||
logger.info("Dispatching database middleware."); | ||
const results = await fetchMessages(dbSession, contact.id); | ||
if (results === undefined) { | ||
logger.error( | ||
"Database middleware dispatched. Failed to get messages for the provided contact.", | ||
); | ||
} else { | ||
results.forEach((result) => { | ||
for (let index = 0; index < result.rows.length; index++) { | ||
messages.push(result.rows.item(index)); | ||
} | ||
}); | ||
logger.info( | ||
"Database middleware dispatched successfully. Returning chat rooms list.", | ||
); | ||
return messages; | ||
} | ||
}; | ||
|
||
export const saveMessage = async ( | ||
contact: Contact, | ||
message: MessageItem, | ||
): Promise<boolean> => { | ||
logger.info("Saving new message to SQL database."); | ||
logger.info("Obtaining database session."); | ||
const dbSession = await getDbConnection(); | ||
logger.info("Dispatching database middleware."); | ||
const results = await insertMessage(dbSession, contact.id, message); | ||
if (results) { | ||
logger.info("Database middleware dispatched. Message saved successfully."); | ||
return true; | ||
} else { | ||
logger.error("Database middleware dispatched. Failed to save a message."); | ||
return false; | ||
} | ||
}; |
File renamed without changes.
120 changes: 120 additions & 0 deletions
120
sdex-encrypted-communicator/databases/SqlStorageMiddlewares.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { | ||
enablePromise, | ||
openDatabase, | ||
ResultSet, | ||
SQLiteDatabase, | ||
} from "react-native-sqlite-storage"; | ||
import { Contact, MessageItem } from "../components/Types"; | ||
import logger from "../Logger"; | ||
|
||
enablePromise(true); | ||
|
||
export const getDbConnection = async () => { | ||
return openDatabase({ | ||
name: "sdex-communicator.db", | ||
createFromLocation: "sdex-communicator.db", | ||
location: "default", | ||
}); | ||
}; | ||
|
||
export const insertContact = async ( | ||
dbSession: SQLiteDatabase, | ||
contact: Contact, | ||
): Promise<boolean> => { | ||
try { | ||
logger.info("Inserting a contact to the SQL database."); | ||
logger.info("Executing query."); | ||
const results = await dbSession.executeSql( | ||
`INSERT INTO contacts (name, surname, publicKey) | ||
VALUES(${contact.name}, ${contact.surname}, ${contact.publicKey});`, | ||
); | ||
logger.info("Query executed successfully."); | ||
logger.debug("Results: " + results); | ||
return true; | ||
} catch (error) { | ||
logger.error("SQL db connection error: " + error); | ||
return false; | ||
} | ||
}; | ||
|
||
export const fetchChatRooms = async ( | ||
dbSession: SQLiteDatabase, | ||
): Promise<[ResultSet] | void> => { | ||
try { | ||
logger.info("Fetching chat rooms from the SQL database."); | ||
logger.info("Executing query."); | ||
const results = dbSession.executeSql( | ||
`SELECT c.name, c.surname, m.time, SUM(m.unread) AS unread | ||
FROM (select * from messages order by time DESC) as m | ||
JOIN contacts AS c ON m.fk_contact_id = c.contact_id | ||
GROUP BY c.name, c.surname | ||
ORDER BY m.time DESC;`, | ||
); | ||
logger.info("Query executed. Returning results."); | ||
logger.debug("Results: " + results); | ||
return results; | ||
} catch (error) { | ||
logger.error("SQL db connection error: " + error); | ||
} | ||
}; | ||
|
||
export const fetchContacts = async ( | ||
dbSession: SQLiteDatabase, | ||
): Promise<[ResultSet] | void> => { | ||
try { | ||
logger.info("Fetching contacts from the SQL database."); | ||
logger.info("Executing query."); | ||
const results = dbSession.executeSql( | ||
`SELECT contactId, name, surname, publicKey | ||
FROM contacts | ||
ORDER BY surname, name;`, | ||
); | ||
logger.info("Query executed. Returning results."); | ||
logger.debug("Results: " + results); | ||
return results; | ||
} catch (error) { | ||
logger.error("SQL db connection error: " + error); | ||
} | ||
}; | ||
|
||
export const fetchMessages = async ( | ||
dbSession: SQLiteDatabase, | ||
contactId: number, | ||
): Promise<[ResultSet] | void> => { | ||
try { | ||
logger.info("Fetching messages for a specified contact from the SQL database."); | ||
logger.info("Executing query."); | ||
const results = dbSession.executeSql( | ||
`SELECT contactId, text, mediaPath, time | ||
FROM messages | ||
WHERE contactId = ${contactId} | ||
ORDER BY time;`, | ||
); | ||
logger.info("Query executed. Returning results."); | ||
logger.debug("Results: " + results); | ||
return results; | ||
} catch (error) { | ||
logger.error("SQL db connection error: " + error); | ||
} | ||
}; | ||
|
||
export const insertMessage = async ( | ||
dbSession: SQLiteDatabase, | ||
contactId: number, | ||
message: MessageItem, | ||
): Promise<boolean> => { | ||
try { | ||
logger.info("Inserting a message to the SQL database."); | ||
logger.info("Executing query."); | ||
const results = await dbSession.executeSql( | ||
`INSERT INTO messages (contactId, text, mediaPath, time) | ||
VALUES(${contactId}, ${message.text}, ${message.mediaPath}, ${message.time});`, | ||
); | ||
logger.info("Query executed successfully."); | ||
logger.debug("Results: " + results); | ||
return true; | ||
} catch (error) { | ||
logger.error("SQL db connection error: " + error); | ||
return false; | ||
} | ||
}; |
24 changes: 0 additions & 24 deletions
24
sdex-encrypted-communicator/databases/asyncStorageMiddlewares.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.