link.queries.js 4.4 KB

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