stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
45 lines (39 loc) • 1.04 kB
text/typescript
import { tables } from '../../schema';
import { SqliteClient } from '../../SqliteClient';
import type { TableRowJoinedUser } from '../../types';
export const selectMessageForId = async (
msgId?: string,
): Promise<TableRowJoinedUser<'messages'> | undefined> => {
if (!msgId) {
return undefined;
}
const messagesColumnNames = Object.keys(tables.messages.columns)
.map((name) => `'${name}', a.${name}`)
.join(', ');
const userColumnNames = Object.keys(tables.users.columns)
.map((name) => `'${name}', b.${name}`)
.join(', ');
SqliteClient.logger?.('info', 'selectMessagesForId', {
msgId,
});
const result = await SqliteClient.executeSql(
`SELECT
json_object(
'user', json_object(
${userColumnNames}
),
${messagesColumnNames}
) as value
FROM (
SELECT
*
FROM messages
WHERE id = ?
) a
LEFT JOIN
users b
ON b.id = a.userId`,
[msgId],
);
return result[0] ? JSON.parse(result[0].value) : undefined;
};