Skip to content

Commit

Permalink
feat: add types for typesense documents
Browse files Browse the repository at this point in the history
  • Loading branch information
GetPsyched committed May 26, 2024
1 parent 97fb6a3 commit fea02bf
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 73 deletions.
23 changes: 23 additions & 0 deletions server/typesense/collections/clubs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';

import { db } from '~/server/db';

export const clubsSchema: CollectionCreateSchema = {
name: 'clubs',
fields: [
{ name: 'alias', type: 'string', optional: true },
{ name: 'name', type: 'string' },
{ name: 'tagline', type: 'string' },
{ name: 'urlName', type: 'string' },
],
};

export const populateClubs = async () => {
return await db.query.clubs.findMany({
columns: { alias: true, name: true, tagline: true, urlName: true },
});
};

export type ClubsDocumentSchema = Awaited<
ReturnType<typeof populateClubs>
>[number];
28 changes: 28 additions & 0 deletions server/typesense/collections/committees.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';

import { db } from '~/server/db';

export const committeesSchema: CollectionCreateSchema = {
name: 'committees',
fields: [
{ name: 'committeeType', type: 'string' },
{ name: 'name', type: 'string' },
{ name: 'nomination', type: 'string', optional: true },
{ name: 'servingAs', type: 'string' },
],
};

export const populateCommittees = async () => {
return await db.query.committeeMembers.findMany({
columns: {
committeeType: true,
name: true,
nomination: true,
servingAs: true,
},
});
};

export type CommitteesDocumentSchema = Awaited<
ReturnType<typeof populateCommittees>
>[number];
21 changes: 21 additions & 0 deletions server/typesense/collections/courses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';

import { db } from '~/server/db';

export const coursesSchema: CollectionCreateSchema = {
name: 'courses',
fields: [
{ name: 'code', type: 'string' },
{ name: 'title', type: 'string' },
],
};

export const populateCourses = async () => {
return await db.query.courses.findMany({
columns: { code: true, title: true },
});
};

export type CoursesDocumentSchema = Awaited<
ReturnType<typeof populateCourses>
>[number];
29 changes: 29 additions & 0 deletions server/typesense/collections/departments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';

import { db } from '~/server/db';

export const departmentsSchema: CollectionCreateSchema = {
name: 'departments',
fields: [
{ name: 'name', type: 'string' },
{ name: 'majors', type: 'string[]' },
{ name: 'urlName', type: 'string' },
],
};

export const populateDepartments = async () => {
return (
await db.query.departments.findMany({
columns: { name: true, urlName: true },
with: { majors: { columns: { name: true } } },
})
).map(({ name, majors, urlName }) => ({
name,
majors: majors.map(({ name }) => name),
urlName,
}));
};

export type DepartmentsDocumentSchema = Awaited<
ReturnType<typeof populateDepartments>
>[number];
37 changes: 37 additions & 0 deletions server/typesense/collections/faculty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';

import { db } from '~/server/db';

export const facultySchema: CollectionCreateSchema = {
name: 'faculty',
fields: [
{ name: 'designation', type: 'string', index: false, optional: true },
{ name: 'email', type: 'string' },
{ name: 'employeeId', type: 'string', index: false, optional: true },
{ name: 'name', type: 'string' },
{ name: 'officeAddress', type: 'string', index: false, optional: true },
{ name: 'telephone', type: 'string' },
],
};

export const populateFaculty = async () => {
return (
await db.query.faculty.findMany({
columns: { designation: true, employeeId: true, officeAddress: true },
with: {
person: { columns: { email: true, name: true, telephone: true } },
},
})
).map(({ designation, employeeId, officeAddress, person }) => ({
designation,
email: person.email,
employeeId,
name: person.name,
officeAddress,
telephone: person.telephone,
}));
};

export type FacultyDocumentSchema = Awaited<
ReturnType<typeof populateFaculty>
>[number];
25 changes: 25 additions & 0 deletions server/typesense/collections/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { clubsSchema } from './clubs';
import { committeesSchema } from './committees';
import { coursesSchema } from './courses';
import { departmentsSchema } from './departments';
import { facultySchema } from './faculty';
import { sectionsSchema } from './sections';
import { staffSchema } from './staff';

export const schema = {
clubs: clubsSchema,
committees: committeesSchema,
courses: coursesSchema,
departments: departmentsSchema,
faculty: facultySchema,
sections: sectionsSchema,
staff: staffSchema,
};

export * from './clubs';
export * from './committees';
export * from './courses';
export * from './departments';
export * from './faculty';
export * from './sections';
export * from './staff';
22 changes: 22 additions & 0 deletions server/typesense/collections/sections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';

import { db } from '~/server/db';

export const sectionsSchema: CollectionCreateSchema = {
name: 'sections',
fields: [
{ name: 'email', type: 'string' },
{ name: 'name', type: 'string' },
{ name: 'urlName', type: 'string' },
],
};

export const populateSections = async () => {
return await db.query.sections.findMany({
columns: { email: true, name: true, urlName: true },
});
};

export type SectionsDocumentSchema = Awaited<
ReturnType<typeof populateSections>
>[number];
35 changes: 35 additions & 0 deletions server/typesense/collections/staff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections';

import { db } from '~/server/db';

export const staffSchema: CollectionCreateSchema = {
name: 'staff',
fields: [
{ name: 'designation', type: 'string', index: false, optional: true },
{ name: 'email', type: 'string' },
{ name: 'employeeId', type: 'string', index: false, optional: true },
{ name: 'name', type: 'string' },
{ name: 'telephone', type: 'string' },
],
};

export const populateStaff = async () => {
return (
await db.query.staff.findMany({
columns: { designation: true, employeeId: true },
with: {
person: { columns: { email: true, name: true, telephone: true } },
},
})
).map(({ designation, employeeId, person }) => ({
designation,
email: person.email,
employeeId,
name: person.name,
telephone: person.telephone,
}));
};

export type StaffDocumentSchema = Awaited<
ReturnType<typeof populateStaff>
>[number];
2 changes: 1 addition & 1 deletion server/typesense/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export const typesense =

if (env.NODE_ENV !== 'production') globalForTypesense.typesense = typesense;

export * as schema from './schema';
export * from './collections';
export * from './utils';
70 changes: 0 additions & 70 deletions server/typesense/schema.ts

This file was deleted.

21 changes: 19 additions & 2 deletions server/typesense/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { type SearchResponseHit } from 'typesense/lib/Typesense/Documents';

import { getKeys } from '~/lib/utils';

import { typesense } from '.';
import {
ClubsDocumentSchema,
CommitteesDocumentSchema,
CoursesDocumentSchema,
DepartmentsDocumentSchema,
FacultyDocumentSchema,
SectionsDocumentSchema,
StaffDocumentSchema,
typesense,
} from '.';

const queryFields = {
clubs: ['alias', 'name', 'tagline'],
Expand Down Expand Up @@ -32,6 +41,14 @@ export async function search(
[collection_name as keyof typeof queryFields]: hits ?? [],
}))
.reduce((acc, obj) => ({ ...acc, ...obj }), {}) as {
[x in (typeof collections)[number]]: SearchResponseHit<object>[];
[x in (typeof collections)[number]]: SearchResponseHit<
| ClubsDocumentSchema
| CommitteesDocumentSchema
| CoursesDocumentSchema
| DepartmentsDocumentSchema
| FacultyDocumentSchema
| SectionsDocumentSchema
| StaffDocumentSchema
>[];
};
}

0 comments on commit fea02bf

Please sign in to comment.