link.queries.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const bcrypt = require("bcryptjs");
  2. const CustomError = require("../utils").CustomError;
  3. const redis = require("../redis");
  4. const knex = require("../knex");
  5. const selectable = [
  6. "links.id",
  7. "links.address",
  8. "links.banned",
  9. "links.created_at",
  10. "links.domain_id",
  11. "links.updated_at",
  12. "links.password",
  13. "links.description",
  14. "links.expire_in",
  15. "links.target",
  16. "links.visit_count",
  17. "links.user_id",
  18. "links.uuid",
  19. "domains.address as domain"
  20. ];
  21. function normalizeMatch(match) {
  22. const newMatch = { ...match };
  23. if (newMatch.address) {
  24. newMatch["links.address"] = newMatch.address;
  25. delete newMatch.address;
  26. }
  27. if (newMatch.user_id) {
  28. newMatch["links.user_id"] = newMatch.user_id;
  29. delete newMatch.user_id;
  30. }
  31. if (newMatch.uuid) {
  32. newMatch["links.uuid"] = newMatch.uuid;
  33. delete newMatch.uuid;
  34. }
  35. return newMatch;
  36. };
  37. async function total(match, params) {
  38. const query = knex("links")
  39. .where(normalizeMatch(match));
  40. if (params?.search) {
  41. query.andWhereRaw(
  42. "concat_ws(' ', description, links.address, target, domains.address) ILIKE '%' || ? || '%'",
  43. [params.search]
  44. );
  45. }
  46. query.leftJoin("domains", "links.domain_id", "domains.id");
  47. query.count("links.id");
  48. const [{ count }] = await query;
  49. return typeof count === "number" ? count : parseInt(count);
  50. }
  51. async function get(match, params) {
  52. const query = knex("links")
  53. .select(...selectable)
  54. .where(normalizeMatch(match))
  55. .offset(params.skip)
  56. .limit(params.limit)
  57. .orderBy("created_at", "desc");
  58. if (params?.search) {
  59. query.andWhereRaw(
  60. "concat_ws(' ', description, links.address, target, domains.address) ILIKE '%' || ? || '%'",
  61. [params.search]
  62. );
  63. }
  64. query.leftJoin("domains", "links.domain_id", "domains.id");
  65. const links = await query;
  66. return links;
  67. }
  68. async function find(match) {
  69. if (match.address && match.domain_id) {
  70. const key = redis.key.link(match.address, match.domain_id);
  71. const cachedLink = await redis.client.get(key);
  72. if (cachedLink) return JSON.parse(cachedLink);
  73. }
  74. const link = await knex("links")
  75. .select(...selectable)
  76. .where(normalizeMatch(match))
  77. .leftJoin("domains", "links.domain_id", "domains.id")
  78. .first();
  79. if (link) {
  80. const key = redis.key.link(link.address, link.domain_id);
  81. redis.client.set(key, JSON.stringify(link), "EX", 60 * 60 * 2);
  82. }
  83. return link;
  84. }
  85. async function create(params) {
  86. let encryptedPassword = null;
  87. if (params.password) {
  88. const salt = await bcrypt.genSalt(12);
  89. encryptedPassword = await bcrypt.hash(params.password, salt);
  90. }
  91. const [link] = await knex(
  92. "links"
  93. ).insert(
  94. {
  95. password: encryptedPassword,
  96. domain_id: params.domain_id || null,
  97. user_id: params.user_id || null,
  98. address: params.address,
  99. description: params.description || null,
  100. expire_in: params.expire_in || null,
  101. target: params.target
  102. },
  103. "*"
  104. );
  105. return link;
  106. }
  107. async function remove(match) {
  108. const link = await knex("links").where(match).first();
  109. if (!link) {
  110. return { isRemoved: false, error: "Could not find the link.", link: null }
  111. };
  112. const deletedLink = await knex("links").where("id", link.id).delete();
  113. redis.remove.link(link);
  114. return { isRemoved: !!deletedLink, link };
  115. }
  116. async function batchRemove(match) {
  117. const deleteQuery = knex("links");
  118. const findQuery = knex("links");
  119. Object.entries(match).forEach(([key, value]) => {
  120. findQuery.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  121. deleteQuery.andWhere(key, ...(Array.isArray(value) ? value : [value]));
  122. });
  123. const links = await findQuery;
  124. links.forEach(redis.remove.link);
  125. await deleteQuery.delete();
  126. }
  127. async function update(match, update) {
  128. if (update.password) {
  129. const salt = await bcrypt.genSalt(12);
  130. update.password = await bcrypt.hash(update.password, salt);
  131. }
  132. const links = await knex("links")
  133. .where(match)
  134. .update({ ...update, updated_at: new Date().toISOString() }, "*");
  135. links.forEach(redis.remove.link);
  136. return links;
  137. }
  138. function incrementVisit(match) {
  139. return knex("links").where(match).increment("visit_count", 1);
  140. }
  141. module.exports = {
  142. normalizeMatch,
  143. batchRemove,
  144. create,
  145. find,
  146. get,
  147. incrementVisit,
  148. remove,
  149. total,
  150. update,
  151. }