UNPKG

@padhariyavishal/nextpress

Version:
67 lines (58 loc) 1.97 kB
import { QueryResult } from "mysql2"; import { npdb } from "../lib/config"; import { getCollectionArgs } from "./types"; class CollectionModel { /** * * @param args Object of the arguments to fetch the collections. */ protected async get(args: getCollectionArgs): Promise<QueryResult> { let query = ` SELECT np_collections.*, np_users.username AS author_name, np_collection_status.status AS status_name, np_collection_types.name AS type_name, np_categories.name AS category_name, np_categories.uri AS category_uri FROM np_collections JOIN np_users ON np_collections.author = np_users.id JOIN np_collection_status ON np_collections.status = np_collection_status.id JOIN np_collection_types ON np_collections.type = np_collection_types.id JOIN np_categories ON np_collections.id = np_categories.collection_id; WHERE np_collection_types.name = "${args?.ctype}" AND np_collection_status.status = "${args?.status}" `; if (args?.s) { query += ` ( np_collections.title LIKE '%${args?.s}%' OR np_collections.content LIKE '%${args?.s}%' OR np_collections.uri LIKE '%${args?.s}%' OR np_users.username LIKE '%${args?.s}%' OR np_categories.name LIKE '%${args?.s}%' )`; } if (args?.by_author) { query += `AND np_users.id= ${args?.by_author}`; } if (args?.parent) { query += `AND np_collections.parent= ${args?.parent}`; } if (args?.by_category) { query += `AND np_categories.${args?.by_category?.key} = '${args?.by_category?.value}'`; } if (args?.id) { query += ` AND np_collections.id= ${args?.id}`; } query += `;`; const [rows] = await npdb.query(query); return rows; } } module.exports = { CollectionModel };