Skip to content

Commit

Permalink
Postgers adapter is still being worked on. Not ready to be deployed
Browse files Browse the repository at this point in the history
  • Loading branch information
devcshort committed Jun 6, 2024
1 parent 50a3264 commit aacda53
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 79 deletions.
84 changes: 7 additions & 77 deletions src/lib/adapters/database/PostgresDatabaseAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
Address,
IResource,
ServiceArea,
Location,
PhoneNumber,
Taxonomy,
} from '@/types/resource';
import { IResource } from '@/types/resource';
import { BaseDatabaseAdapter, Config } from './BaseDatabaseAdapter';
import postgres from '@/lib/postgres';
import { IRedirect } from '@/types/redirect';
import { Session } from 'next-auth';

export class PostgresDatabaseAdapter extends BaseDatabaseAdapter {
findResourceById(id: string, config: Config): IResource | Promise<IResource> {
throw new Error('Method not implemented.');
}
findRedirectById(id: string): IRedirect | Promise<IRedirect> {
throw new Error('Method not implemented.');
}
addResourceToFavoriteList(body: any, session: Session) {
throw new Error('Method not implemented.');
}
Expand Down Expand Up @@ -39,72 +37,4 @@ export class PostgresDatabaseAdapter extends BaseDatabaseAdapter {
) {
throw new Error('Method not implemented.');
}
async findResourceById(id: string, config: Config): Promise<IResource> {
const record = await postgres.resource.findUnique({
where: {
id: id,
},
include: {
translations: {
where: {
locale: config.locale,
},
},
},
});

if (!record) throw new Error(this.notFound);

const translation = record?.translations?.[0];
const organization = translation?.organization as {
name?: string;
description?: string;
};

return {
id: record.id,
email: record?.email ?? null,
phone: record?.phone_number ?? null,
website: record?.website ?? null,
addresses: (record?.addresses as Address[]) ?? null,
phoneNumbers: (record?.phone_numbers as PhoneNumber[]) ?? null,
serviceArea: (record?.service_area as ServiceArea) ?? null,
location: (record?.location as Location) ?? null,
lastAssuredDate: record?.last_assured_date?.toISOString() ?? null,
createdAt: record?.created_at?.toISOString() ?? null,
name: translation?.name ?? null,
description: translation?.description ?? null,
fees: translation?.fees ?? null,
hours: translation?.hours ?? null,
locale: translation?.locale ?? null,
eligibilities: translation?.eligibilities ?? null,
applicationProcess: translation?.application_process ?? null,
taxonomies: (translation?.taxonomies as Taxonomy[]) ?? null,
requiredDocuments: (translation?.required_documents as string[]) ?? null,
languages: (translation?.languages as string[]) ?? null,
organization: {
name: organization?.name ?? null,
description: organization?.description ?? null,
},
service: {
name: translation?.service_name ?? null,
},
};
}

async findRedirectById(id: string): Promise<IRedirect> {
const record = await postgres.redirect.findUnique({
where: {
old_resource_id: id,
},
});

if (!record) return null;

return {
id: record.id,
oldId: record.old_resource_id,
newId: record.new_resource_id,
};
}
}
2 changes: 0 additions & 2 deletions src/lib/adapters/database/get-database-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { MongoDatabaseAdapter } from './MongoDatabaseAdapter';
import { PostgresDatabaseAdapter } from './PostgresDatabaseAdapter';
import { serverSideAppConfig } from '@/lib/server-utils';
import { BaseDatabaseAdapter } from './BaseDatabaseAdapter';

// Supported databases
const databaseMapping = {
mongodb: MongoDatabaseAdapter,
postgres: PostgresDatabaseAdapter,
};

// Function to get the adapter with the correct return type
Expand Down

0 comments on commit aacda53

Please sign in to comment.