Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add search endpoint #10

Merged
merged 9 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions backend/src/api/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export const saveMetadata = async (cid: string, metadata: OffchainMetadata) => {
return metadataRepository.setMetadata(cid, metadata);
};

export const searchMetadataByCID = async (cid: string, limit?: number) => {
clostao marked this conversation as resolved.
Show resolved Hide resolved
const defaultLimit = 5;
clostao marked this conversation as resolved.
Show resolved Hide resolved
return metadataRepository
.searchMetadataByCID(cid, limit ?? defaultLimit)
clostao marked this conversation as resolved.
Show resolved Hide resolved
.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.2",
"@headlessui/react": "^2.1.7",
"@ipld/dag-pb": "^4.1.2",
"@uidotdev/usehooks": "^2.4.1",
Expand Down
10 changes: 5 additions & 5 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ __metadata:
languageName: node
linkType: hard

"@autonomys/auto-drive@npm:^0.5.0":
version: 0.5.0
resolution: "@autonomys/auto-drive@npm:0.5.0"
"@autonomys/auto-drive@npm:^0.5.2":
version: 0.5.2
resolution: "@autonomys/auto-drive@npm:0.5.2"
dependencies:
"@ipld/dag-pb": "npm:^4.1.2"
blake3: "npm:1.1.0"
multiformats: "npm:^13.2.2"
protobufjs: "npm:^7.4.0"
protons: "npm:^7.6.0"
protons-runtime: "npm:^5.5.0"
checksum: 10c0/83137e75afcbe5029387118b9353ec6f4026403622fac491f78c354fd49494adeac700a747ecf67eb8d44418f6877f0575cd208b2e092796692f9489fda5e5db
checksum: 10c0/d66d26bb1495ffea46470675933c6c936b50bce80f5917826f03949d4a9393f5676150bfda08f633c6297df1d8b829afaae8a6dd8b1da89996d06d69e6ee481c
languageName: node
linkType: hard

Expand Down Expand Up @@ -2156,7 +2156,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "frontend@workspace:."
dependencies:
"@autonomys/auto-drive": "npm:^0.5.0"
"@autonomys/auto-drive": "npm:^0.5.2"
"@headlessui/react": "npm:^2.1.7"
"@ipld/dag-pb": "npm:^4.1.2"
"@types/node": "npm:^20"
Expand Down