good forst commit
This commit is contained in:
@@ -26,13 +26,13 @@ describe('MESSAGES_INDEX', () => {
|
||||
});
|
||||
|
||||
describe('configureIndex', () => {
|
||||
it('sets searchable, filterable, and sortable attributes on the correct index', async () => {
|
||||
it('sets searchable, filterable (including tenantId), and sortable attributes on the correct index', async () => {
|
||||
const { client, mockIndex, mockUpdateSettings } = makeMockClient();
|
||||
await configureIndex(client);
|
||||
expect(mockIndex).toHaveBeenCalledWith('tower-messages');
|
||||
expect(mockUpdateSettings).toHaveBeenCalledWith({
|
||||
searchableAttributes: ['content', 'senderName', 'sourceGroupName'],
|
||||
filterableAttributes: ['sourceGroupId', 'tags', 'platform'],
|
||||
filterableAttributes: ['tenantId', 'sourceGroupId', 'tags', 'platform'],
|
||||
sortableAttributes: ['approvedAt'],
|
||||
});
|
||||
});
|
||||
@@ -43,6 +43,7 @@ describe('indexMessage', () => {
|
||||
const { client, mockIndex, mockAddDocuments } = makeMockClient();
|
||||
const doc: MeiliDocument = {
|
||||
id: 'msg-1',
|
||||
tenantId: 'tnt-1',
|
||||
content: 'Hello community',
|
||||
senderName: 'Alice',
|
||||
sourceGroupId: 'grp-1',
|
||||
|
||||
@@ -4,6 +4,7 @@ export { MeiliSearch } from 'meilisearch';
|
||||
|
||||
export interface MeiliDocument {
|
||||
id: string; // DB Message.id
|
||||
tenantId: string; // tenant owner — required for multi-tenant filter
|
||||
content: string;
|
||||
senderName: string; // empty string when null
|
||||
sourceGroupId: string;
|
||||
@@ -20,15 +21,22 @@ export function createMeiliClient(url: string, masterKey: string): MeiliSearch {
|
||||
}
|
||||
|
||||
export async function configureIndex(client: MeiliSearch): Promise<void> {
|
||||
// Ensure the index exists with the correct primary key
|
||||
// (Meilisearch v1.11 can't infer it when multiple fields end with `id`)
|
||||
try {
|
||||
await client.getIndex(MESSAGES_INDEX);
|
||||
} catch {
|
||||
await client.createIndex(MESSAGES_INDEX, { primaryKey: 'id' });
|
||||
}
|
||||
await client.index(MESSAGES_INDEX).updateSettings({
|
||||
searchableAttributes: ['content', 'senderName', 'sourceGroupName'],
|
||||
filterableAttributes: ['sourceGroupId', 'tags', 'platform'],
|
||||
filterableAttributes: ['tenantId', 'sourceGroupId', 'tags', 'platform'],
|
||||
sortableAttributes: ['approvedAt'],
|
||||
});
|
||||
}
|
||||
|
||||
export async function indexMessage(client: MeiliSearch, doc: MeiliDocument): Promise<void> {
|
||||
await client.index(MESSAGES_INDEX).addDocuments([doc]);
|
||||
await client.index(MESSAGES_INDEX).addDocuments([doc], { primaryKey: 'id' });
|
||||
}
|
||||
|
||||
export async function deleteMessage(client: MeiliSearch, id: string): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user