Skip to content

Commit

Permalink
Clarify objectList by renaming and refactoring the functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarah committed Oct 3, 2023
1 parent 511caea commit 2c5a205
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function ObjectListCount({communityId, locale}: MembershipCountProps) {

let objectLists = [];
try {
objectLists = await objectList.getListsByCommunityId(communityId);
objectLists = await objectList.getByCommunityId(communityId);
} catch (err) {
console.error(err);
return t('objectListCountError');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ListItem {
description?: string;
}
export async function addList(listItem: ListItem) {
const response = await objectList.createListForCommunity(listItem);
const response = await objectList.create(listItem);
const community = await getCommunityById(listItem.communityId);
revalidatePath(`/[locale]/communities/${community.slug}`, 'page');

Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './object-list';
export * as objectList from './object-list';
56 changes: 22 additions & 34 deletions packages/database/src/object-list.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
import {objectLists, insertObjectItemSchema, objectItems} from './db/schema';
import {db} from './db/connection';
import {iriToHash} from './iri-to-hash';
import {DBQueryConfig} from 'drizzle-orm';

async function getListsByCommunityId(communityId: string) {
return db.query.objectLists.findMany({
where: (objectLists, {eq}) => eq(objectLists.communityId, communityId),
});
}

interface GetCommunityListsWithObjectsProps {
communityId: string;
interface Option {
withObjects?: boolean;
limitObjects?: number;
}

async function getCommunityListsWithObjects({
communityId,
limitObjects,
}: GetCommunityListsWithObjectsProps) {
const objectOptions = limitObjects ? {limit: limitObjects} : true;
export async function getByCommunityId(
communityId: string,
{withObjects, limitObjects}: Option = {withObjects: false}
) {
const options: DBQueryConfig = {};

if (withObjects) {
options.with = {
objects: limitObjects ? {limit: limitObjects} : true,
};
}

return db.query.objectLists.findMany({
...options,
where: (objectLists, {eq}) => eq(objectLists.communityId, communityId),
with: {
objects: objectOptions,
},
});
}

interface CreateListForCommunityProps {
interface CreateProps {
communityId: string;
createdBy: string;
name: string;
createdBy: string;
description?: string;
}

async function createListForCommunity({
export async function create({
communityId,
name,
description,
createdBy,
}: CreateListForCommunityProps) {
description,
}: CreateProps) {
return db.insert(objectLists).values({
communityId,
name,
Expand All @@ -48,17 +47,13 @@ async function createListForCommunity({
});
}

interface AddObjectToListProps {
interface AddObjectProps {
listId: number;
objectIri: string;
userId: string;
}

async function addObjectToList({
listId,
objectIri,
userId,
}: AddObjectToListProps) {
export async function addObject({listId, objectIri, userId}: AddObjectProps) {
const objectItem = insertObjectItemSchema.parse({
listId,
objectIri,
Expand All @@ -68,10 +63,3 @@ async function addObjectToList({

return db.insert(objectItems).values(objectItem);
}

export const objectList = {
getListsByCommunityId,
getCommunityListsWithObjects,
createListForCommunity,
addObjectToList,
};

0 comments on commit 2c5a205

Please sign in to comment.