stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
37 lines (30 loc) • 1.07 kB
text/typescript
import type { Schema } from '../schema';
import type { PreparedQueries, TableColumnNames, TableColumnValue } from '../types';
export const appendWhereClause = <T extends keyof Schema>(
selectQuery: string,
whereCondition?: Partial<{ [k in TableColumnNames<T>]: TableColumnValue | TableColumnValue[] }>,
): PreparedQueries => {
if (!whereCondition) {
return [selectQuery, []];
}
const whereClause: string[] = [];
const whereParams: TableColumnValue[] = [];
for (const key in whereCondition) {
const value = whereCondition[key];
if (value === undefined) {
continue;
}
if (Array.isArray(value)) {
const questionMarks = Array(Object.keys(value).length).fill('?').join(',');
whereClause.push(`${key} in (${questionMarks})`);
whereParams.push(...value);
} else {
whereClause.push(`${key} = ?`);
whereParams.push(value);
}
}
if (!whereParams.length && !whereClause.length) {
return [selectQuery, []];
}
return [`${selectQuery} WHERE ${whereClause.join(' AND ')}`, whereParams];
};