ability to query by p as well for dictionary api

This commit is contained in:
adueck 2022-11-09 13:56:42 +05:00
parent 685b85bd48
commit 30f51385f3
10 changed files with 47 additions and 33 deletions

View File

@ -9,7 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@lingdocs/inflect": "^5.1.8",
"@lingdocs/inflect": "5.1.10",
"base64url": "^3.0.1",
"bcryptjs": "^2.4.3",
"connect-redis": "^6.0.0",
@ -219,9 +219,9 @@
}
},
"node_modules/@lingdocs/inflect": {
"version": "5.1.8",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.8.tgz",
"integrity": "sha512-jk9qlJvDMFpw6FIQ6XCun3zfv679aG9uoM17P7rIeNDrjOxH00brRbBD3amaVC6UvpU0mxSC9n9dnubncrd9lg==",
"version": "5.1.10",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.10.tgz",
"integrity": "sha512-LPJqXSnb2zLWLfMXMTCs/1NOGYQnzPHbKu5h/SdvFYobCxnk2tgngtSWcCmeqYbNXKqQvbDwqmseLlcnUZKysA==",
"license": "MIT",
"dependencies": {
"pbf": "^3.2.1",
@ -5224,9 +5224,9 @@
}
},
"@lingdocs/inflect": {
"version": "5.1.8",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.8.tgz",
"integrity": "sha512-jk9qlJvDMFpw6FIQ6XCun3zfv679aG9uoM17P7rIeNDrjOxH00brRbBD3amaVC6UvpU0mxSC9n9dnubncrd9lg==",
"version": "5.1.10",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.10.tgz",
"integrity": "sha512-LPJqXSnb2zLWLfMXMTCs/1NOGYQnzPHbKu5h/SdvFYobCxnk2tgngtSWcCmeqYbNXKqQvbDwqmseLlcnUZKysA==",
"requires": {
"pbf": "^3.2.1",
"rambda": "^7.3.0"

View File

@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@lingdocs/inflect": "^5.1.8",
"@lingdocs/inflect": "5.1.10",
"base64url": "^3.0.1",
"bcryptjs": "^2.4.3",
"connect-redis": "^6.0.0",

View File

@ -7,6 +7,7 @@ import {
readDictionaryInfo,
Types as T,
typePredicates as tp,
entryOfFull,
} from "@lingdocs/inflect"
export let collection: Collection<any> | undefined = undefined;
@ -56,15 +57,18 @@ function getOneByTs(ts: number): T.DictionaryEntry {
return entry;
}
export async function getEntries(ids: number[]): Promise<{
export async function getEntries(ids: (number | string)[]): Promise<{
results: (T.DictionaryEntry | T.VerbEntry)[],
notFound: number[],
notFound: (number | string)[],
}> {
if (!collection) {
throw new Error("dictionary not initialized");
}
const results: (T.DictionaryEntry | T.VerbEntry)[] = collection.find({
"ts": { "$in": ids },
"$or": [
{ "ts": { "$in": ids }},
{ "p": { "$in": ids }},
],
}).map(x => {
const { $loki, meta, ...entry } = x;
return entry;
@ -85,9 +89,10 @@ export async function getEntries(ids: number[]): Promise<{
});
return {
results,
notFound: ids.filter(id => !results.find(x => (
"entry" in x ? x.entry.ts === id : x.ts === id
))),
notFound: ids.filter(id => !results.find(x => {
const entry = entryOfFull(x);
return entry.p === id || entry.ts === id;
})),
};
}

View File

@ -17,7 +17,7 @@ dictionaryRouter.post("/entries", async (req, res, next) => {
if (!collection) {
return res.send({ ok: false, message: "dictionary not ready" });
}
const ids = req.body.ids as number[];
const ids = req.body.ids as (number | string)[];
if (!Array.isArray(ids)) {
return res.status(400).send({ ok: false, error: "invalid query" });
}
@ -29,7 +29,10 @@ dictionaryRouter.get("/entries/:id", async (req, res, next) => {
if (!collection) {
return res.send({ ok: false, message: "dictionary not ready" });
}
const ids = req.params.id.split(",").map(unary(parseInt));
const ids = req.params.id.split(",").map(x => {
const n = parseInt(x);
return Number.isNaN(n) ? x : n;
});
const results = await getEntries(ids);
return res.send(results);
});

6
account/src/test.http Normal file
View File

@ -0,0 +1,6 @@
POST http://localhost:4000/dictionary/entries
content-type: application/json
{
"ids": ["کور", 1527815306]
}

View File

@ -105,10 +105,10 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"
"@lingdocs/inflect@^5.1.8":
"integrity" "sha512-jk9qlJvDMFpw6FIQ6XCun3zfv679aG9uoM17P7rIeNDrjOxH00brRbBD3amaVC6UvpU0mxSC9n9dnubncrd9lg=="
"resolved" "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.8.tgz"
"version" "5.1.8"
"@lingdocs/inflect@5.1.10":
"integrity" "sha512-LPJqXSnb2zLWLfMXMTCs/1NOGYQnzPHbKu5h/SdvFYobCxnk2tgngtSWcCmeqYbNXKqQvbDwqmseLlcnUZKysA=="
"resolved" "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.10.tgz"
"version" "5.1.10"
dependencies:
"pbf" "^3.2.1"
"rambda" "^7.3.0"

View File

@ -7,7 +7,7 @@
"name": "functions",
"dependencies": {
"@google-cloud/storage": "^5.8.1",
"@lingdocs/inflect": "5.1.8",
"@lingdocs/inflect": "5.1.10",
"@types/cors": "^2.8.10",
"@types/google-spreadsheet": "^3.0.2",
"@types/react": "^18.0.21",
@ -391,9 +391,9 @@
}
},
"node_modules/@lingdocs/inflect": {
"version": "5.1.8",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.8.tgz",
"integrity": "sha512-jk9qlJvDMFpw6FIQ6XCun3zfv679aG9uoM17P7rIeNDrjOxH00brRbBD3amaVC6UvpU0mxSC9n9dnubncrd9lg==",
"version": "5.1.10",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.10.tgz",
"integrity": "sha512-LPJqXSnb2zLWLfMXMTCs/1NOGYQnzPHbKu5h/SdvFYobCxnk2tgngtSWcCmeqYbNXKqQvbDwqmseLlcnUZKysA==",
"license": "MIT",
"dependencies": {
"pbf": "^3.2.1",
@ -3499,9 +3499,9 @@
}
},
"@lingdocs/inflect": {
"version": "5.1.8",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.8.tgz",
"integrity": "sha512-jk9qlJvDMFpw6FIQ6XCun3zfv679aG9uoM17P7rIeNDrjOxH00brRbBD3amaVC6UvpU0mxSC9n9dnubncrd9lg==",
"version": "5.1.10",
"resolved": "https://npm.lingdocs.com/@lingdocs%2finflect/-/inflect-5.1.10.tgz",
"integrity": "sha512-LPJqXSnb2zLWLfMXMTCs/1NOGYQnzPHbKu5h/SdvFYobCxnk2tgngtSWcCmeqYbNXKqQvbDwqmseLlcnUZKysA==",
"requires": {
"pbf": "^3.2.1",
"rambda": "^7.3.0"

View File

@ -14,7 +14,7 @@
"main": "lib/functions/src/index.js",
"dependencies": {
"@google-cloud/storage": "^5.8.1",
"@lingdocs/inflect": "5.1.8",
"@lingdocs/inflect": "5.1.10",
"@types/cors": "^2.8.10",
"@types/google-spreadsheet": "^3.0.2",
"@types/react": "^18.0.21",

View File

@ -7,7 +7,7 @@
"private": true,
"dependencies": {
"@fortawesome/fontawesome-free": "^5.15.2",
"@lingdocs/ps-react": "5.1.8",
"@lingdocs/ps-react": "5.1.10",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",

View File

@ -2349,10 +2349,10 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"
"@lingdocs/ps-react@5.1.8":
version "5.1.8"
resolved "https://npm.lingdocs.com/@lingdocs%2fps-react/-/ps-react-5.1.8.tgz#3bdad675ae8d2ea4f77abe572a4b33574e2ce986"
integrity sha512-kcOzjRHjw6rOqX2o0KAv0o1B9apzYwQob9/s0vpl1d4HCV+SbmCnuAy+d0LCzmbhDNhZX5wu0ZZjVhaQMtQ17A==
"@lingdocs/ps-react@5.1.10":
version "5.1.10"
resolved "https://npm.lingdocs.com/@lingdocs%2fps-react/-/ps-react-5.1.10.tgz#155646c436c7dc2644c60815cef6b914ba7fef69"
integrity sha512-3PtfEZBmNCa6ptlWezLXpHrngcRmPc8QKguGiXI48TnVxSvAaSH63FTwRQJIDznYasq3bnDSH/7Sta8SX8vvBA==
dependencies:
"@formkit/auto-animate" "^1.0.0-beta.3"
classnames "^2.2.6"