From fea02bfad3ff7f4cce7f04710c3e849d9298daf0 Mon Sep 17 00:00:00 2001 From: GetPsyched Date: Mon, 27 May 2024 00:19:21 +0530 Subject: [PATCH] feat: add types for typesense documents --- server/typesense/collections/clubs.ts | 23 +++++++ server/typesense/collections/committees.ts | 28 +++++++++ server/typesense/collections/courses.ts | 21 +++++++ server/typesense/collections/departments.ts | 29 +++++++++ server/typesense/collections/faculty.ts | 37 +++++++++++ server/typesense/collections/index.ts | 25 ++++++++ server/typesense/collections/sections.ts | 22 +++++++ server/typesense/collections/staff.ts | 35 +++++++++++ server/typesense/index.ts | 2 +- server/typesense/schema.ts | 70 --------------------- server/typesense/utils.ts | 21 ++++++- 11 files changed, 240 insertions(+), 73 deletions(-) create mode 100644 server/typesense/collections/clubs.ts create mode 100644 server/typesense/collections/committees.ts create mode 100644 server/typesense/collections/courses.ts create mode 100644 server/typesense/collections/departments.ts create mode 100644 server/typesense/collections/faculty.ts create mode 100644 server/typesense/collections/index.ts create mode 100644 server/typesense/collections/sections.ts create mode 100644 server/typesense/collections/staff.ts delete mode 100644 server/typesense/schema.ts diff --git a/server/typesense/collections/clubs.ts b/server/typesense/collections/clubs.ts new file mode 100644 index 00000000..720136f1 --- /dev/null +++ b/server/typesense/collections/clubs.ts @@ -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 +>[number]; diff --git a/server/typesense/collections/committees.ts b/server/typesense/collections/committees.ts new file mode 100644 index 00000000..867fc312 --- /dev/null +++ b/server/typesense/collections/committees.ts @@ -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 +>[number]; diff --git a/server/typesense/collections/courses.ts b/server/typesense/collections/courses.ts new file mode 100644 index 00000000..1620d48b --- /dev/null +++ b/server/typesense/collections/courses.ts @@ -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 +>[number]; diff --git a/server/typesense/collections/departments.ts b/server/typesense/collections/departments.ts new file mode 100644 index 00000000..0f351b59 --- /dev/null +++ b/server/typesense/collections/departments.ts @@ -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 +>[number]; diff --git a/server/typesense/collections/faculty.ts b/server/typesense/collections/faculty.ts new file mode 100644 index 00000000..8290b585 --- /dev/null +++ b/server/typesense/collections/faculty.ts @@ -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 +>[number]; diff --git a/server/typesense/collections/index.ts b/server/typesense/collections/index.ts new file mode 100644 index 00000000..16c35dcd --- /dev/null +++ b/server/typesense/collections/index.ts @@ -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'; diff --git a/server/typesense/collections/sections.ts b/server/typesense/collections/sections.ts new file mode 100644 index 00000000..d075722b --- /dev/null +++ b/server/typesense/collections/sections.ts @@ -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 +>[number]; diff --git a/server/typesense/collections/staff.ts b/server/typesense/collections/staff.ts new file mode 100644 index 00000000..cc762816 --- /dev/null +++ b/server/typesense/collections/staff.ts @@ -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 +>[number]; diff --git a/server/typesense/index.ts b/server/typesense/index.ts index 06793f00..9eda410c 100644 --- a/server/typesense/index.ts +++ b/server/typesense/index.ts @@ -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'; diff --git a/server/typesense/schema.ts b/server/typesense/schema.ts deleted file mode 100644 index ce1b4c2c..00000000 --- a/server/typesense/schema.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { type CollectionCreateSchema } from 'typesense/lib/Typesense/Collections'; - -export const clubs: CollectionCreateSchema = { - name: 'clubs', - fields: [ - { name: 'alias', type: 'string', optional: true }, - { name: 'name', type: 'string' }, - { name: 'tagline', type: 'string' }, - { name: 'urlName', type: 'string' }, - ], -}; - -export const committees: CollectionCreateSchema = { - name: 'committees', - fields: [ - { name: 'committeeType', type: 'string' }, - { name: 'name', type: 'string' }, - { name: 'nomination', type: 'string', optional: true }, - { name: 'servingAs', type: 'string' }, - ], -}; - -export const courses: CollectionCreateSchema = { - name: 'courses', - fields: [ - { name: 'code', type: 'string' }, - { name: 'title', type: 'string' }, - ], -}; - -export const departments: CollectionCreateSchema = { - name: 'departments', - fields: [ - { name: 'name', type: 'string' }, - { name: 'majors', type: 'string[]' }, - { name: 'urlName', type: 'string' }, - ], -}; - -export const faculty: 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 sections: CollectionCreateSchema = { - name: 'sections', - fields: [ - { name: 'email', type: 'string' }, - { name: 'name', type: 'string' }, - { name: 'urlName', type: 'string' }, - ], -}; - -export const staff: 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' }, - ], -}; diff --git a/server/typesense/utils.ts b/server/typesense/utils.ts index f2725e10..1418608e 100644 --- a/server/typesense/utils.ts +++ b/server/typesense/utils.ts @@ -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'], @@ -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[]; + [x in (typeof collections)[number]]: SearchResponseHit< + | ClubsDocumentSchema + | CommitteesDocumentSchema + | CoursesDocumentSchema + | DepartmentsDocumentSchema + | FacultyDocumentSchema + | SectionsDocumentSchema + | StaffDocumentSchema + >[]; }; }