You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.1 KiB

11 months ago
  1. const prisma = require("../utils/prisma");
  2. const DocumentSyncRun = {
  3. statuses: {
  4. unknown: "unknown",
  5. exited: "exited",
  6. failed: "failed",
  7. success: "success",
  8. },
  9. save: async function (queueId = null, status = null, result = {}) {
  10. try {
  11. if (!this.statuses.hasOwnProperty(status))
  12. throw new Error(
  13. `DocumentSyncRun status ${status} is not a valid status.`
  14. );
  15. const run = await prisma.document_sync_executions.create({
  16. data: {
  17. queueId: Number(queueId),
  18. status: String(status),
  19. result: JSON.stringify(result),
  20. },
  21. });
  22. return run || null;
  23. } catch (error) {
  24. console.error(error.message);
  25. return null;
  26. }
  27. },
  28. get: async function (clause = {}) {
  29. try {
  30. const queue = await prisma.document_sync_executions.findFirst({
  31. where: clause,
  32. });
  33. return queue || null;
  34. } catch (error) {
  35. console.error(error.message);
  36. return null;
  37. }
  38. },
  39. where: async function (
  40. clause = {},
  41. limit = null,
  42. orderBy = null,
  43. include = {}
  44. ) {
  45. try {
  46. const results = await prisma.document_sync_executions.findMany({
  47. where: clause,
  48. ...(limit !== null ? { take: limit } : {}),
  49. ...(orderBy !== null ? { orderBy } : {}),
  50. ...(include !== null ? { include } : {}),
  51. });
  52. return results;
  53. } catch (error) {
  54. console.error(error.message);
  55. return [];
  56. }
  57. },
  58. count: async function (clause = {}, limit = null, orderBy = {}) {
  59. try {
  60. const count = await prisma.document_sync_executions.count({
  61. where: clause,
  62. ...(limit !== null ? { take: limit } : {}),
  63. ...(orderBy !== null ? { orderBy } : {}),
  64. });
  65. return count;
  66. } catch (error) {
  67. console.error("FAILED TO COUNT DOCUMENTS.", error.message);
  68. return 0;
  69. }
  70. },
  71. delete: async function (clause = {}) {
  72. try {
  73. await prisma.document_sync_executions.deleteMany({ where: clause });
  74. return true;
  75. } catch (error) {
  76. console.error(error.message);
  77. return false;
  78. }
  79. },
  80. };
  81. module.exports = { DocumentSyncRun };