Skip to content

NextJS server action to interact with the list of given reservoirs #67

@brauliodiez

Description

@brauliodiez

Use it on the search option (typeahead)

Some tips, the server action (review is just rough code):

front/src/pods/embalse-search/embalse-search.repository.ts

"use server";

import { getDb } from "@/lib/mongodb";
import type { Embalse } from "./api/api.model";

export async function getEmbalsesFromDb(): Promise<Embalse[]> {
  const db = getDb();
  const docs = await db
    .collection("embalses")
    .find({}, { projection: { _id: 1, nombre: 1, slug: 1, provincia: 1 } })
    .toArray();

  return docs.map((doc) => ({
    _id: doc.slug ?? "",    
    name: doc.nombre ?? "",
    province: doc.provincia ?? "",
  }));
}

And in the embalse.api.ts

import { Embalse } from "./api.model";
import { getEmbalsesFromDb } from "../embalse-search.repository";

export const getEmbalsesCollection = async (): Promise<Embalse[]> => {
  return getEmbalsesFromDb();
};

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions