Add npub index to names, add get name by npub method

This commit is contained in:
artur 2024-02-07 08:43:15 +03:00
parent 08033079ea
commit f592b08485
3 changed files with 52 additions and 18 deletions

View File

@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX "Names_npub_idx" ON "Names"("npub");

View File

@ -1,6 +1,3 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
@ -11,26 +8,28 @@ datasource db {
}
model PushSubs {
id Int @id @default(autoincrement())
pushId String @unique
timestamp BigInt
npub String
id Int @id @default(autoincrement())
pushId String @unique
timestamp BigInt
npub String
pushSubscription String
relays String
relays String
}
model NpubData {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
timestamp BigInt
npub String @unique
data String
pwh2 String
salt String
npub String @unique
data String
pwh2 String
salt String
}
model Names {
id Int @id @default(autoincrement())
name String @unique
npub String
id Int @id @default(autoincrement())
name String @unique
npub String
timestamp BigInt
@@index([npub])
}

View File

@ -730,7 +730,40 @@ app.post(NAME_PATH, async (req, res) => {
});
} catch (e) {
console.log(new Date(), "error req from ", req.ip, e.toString())
res.status(400).send({
res.status(500).send({
error: "Internal error"
});
}
})
app.get(NAME_PATH, async (req, res) => {
try {
const { npub } = req.query;
if (!npub) {
res
.status(400)
.send({ error: "Specify npub" });
return;
}
const recs = await prisma.names.findMany({
where: {
npub
}
})
console.log("npub", npub, recs);
const data = {
names: recs.map(r => r.name)
}
res
.status(200)
.send(data);
} catch (e) {
console.log(new Date(), "error req from ", req.ip, e.toString())
res.status(500).send({
error: "Internal error"
});
}
@ -778,7 +811,7 @@ app.get(JSON_PATH, async (req, res) => {
} catch (e) {
console.log(new Date(), "error req from ", req.ip, e.toString())
res.status(400).send({
res.status(500).send({
error: "Internal error"
});
}