Skip to content

Commit

Permalink
feat: add search endpoint (#10)
Browse files Browse the repository at this point in the history
* feat: add search endpoint

* update: @autonomys/auto-drive package

* fix: update yarn install state

* update: simplify default query limit

Co-authored-by: Marc-Aurele Besner <[email protected]>

* update: explicitly import as type

* update: yarn lock

* fix: downgrade auto-drive package

* chore: update @autonomys/auto-drive package to version 0.5.0

---------

Co-authored-by: Marc-Aurele Besner <[email protected]>
  • Loading branch information
clostao and marc-aurele-besner authored Sep 19, 2024
1 parent 252d0ab commit 7fa91fc
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 137 deletions.
6 changes: 6 additions & 0 deletions backend/src/api/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export const saveMetadata = async (cid: string, metadata: OffchainMetadata) => {
return metadataRepository.setMetadata(cid, metadata);
};

export const searchMetadataByCID = async (cid: string, limit: number = 5) => {
return metadataRepository
.searchMetadataByCID(cid, limit)
.then((result) => result.map((entry) => entry.cid));
};

export const getAllMetadata = async () => {
return metadataRepository
.getAllMetadata()
Expand Down
19 changes: 19 additions & 0 deletions backend/src/repositories/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ const setMetadata = async (cid: string, metadata: OffchainMetadata) => {
);
};

const searchMetadataByCID = async (cid: string, limit: number | undefined) => {
const db = await initTable();

return db
.all<{ cid: string }[]>(
"SELECT cid FROM metadata WHERE cid LIKE ? LIMIT ?",
`%${cid}%`,
limit
)
.then((entries) => {
return entries.map((entry) => {
return {
cid: entry.cid,
};
});
});
};

/// TODO: pagination and filtering
export const getAllMetadata = async () => {
const db = await initTable();
Expand All @@ -58,4 +76,5 @@ export const metadataRepository = {
getMetadata,
setMetadata,
getAllMetadata,
searchMetadataByCID,
};
18 changes: 17 additions & 1 deletion backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { decode } from "@ipld/dag-pb";
import { NodeWithMetadata } from "./models/nodeWithMetadata.js";
import { getNode } from "./api/nodes.js";
import { getMetadata } from "./api/metadata.js";
import { getMetadata, searchMetadataByCID } from "./api/metadata.js";
import {
getHeadTransactionResults,
getNodeTransactionResult,
Expand Down Expand Up @@ -161,6 +161,22 @@ const createServer = async () => {
}
});

app.get("/metadata/search/:cid", async (req, res) => {
try {
const { cid } = req.params;
const limit = req.query.limit
? parseInt(req.query.limit as string)
: undefined;
const results = await searchMetadataByCID(cid, limit);
res.json(results);
} catch (error: any) {
console.error("Error searching metadata:", error);
res
.status(500)
.json({ error: "Failed to search metadata", details: error.message });
}
});

app.get("/metadata/:cid", async (req, res) => {
try {
const { cid } = req.params;
Expand Down
Binary file modified frontend/.yarn/install-state.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@autonomys/auto-drive": "^0.5.0",
"@autonomys/auto-drive": "=0.5.0",
"@headlessui/react": "^2.1.7",
"@ipld/dag-pb": "^4.1.2",
"@uidotdev/usehooks": "^2.4.1",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/NodeExplorer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { IPLDNodeData } from "@autonomys/auto-drive"
import type { IPLDNodeData } from "@autonomys/auto-drive"
import { ChevronDownIcon, ChevronRightIcon } from "lucide-react"
import { FC, useState } from "react"

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/models/NodeWithMetadata.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPLDNodeData } from "@autonomys/auto-drive";
import type { IPLDNodeData } from "@autonomys/auto-drive";

export interface NodeWithMetadata {
cid: string;
Expand Down
Loading

0 comments on commit 7fa91fc

Please sign in to comment.