schema.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. HydratedDocument,
  3. Model,
  4. QueryWithHelpers,
  5. Schema,
  6. SchemaOptions,
  7. SchemaTypes,
  8. Types
  9. } from "mongoose";
  10. import { GetData } from "@/modules/DataModule/plugins/getData";
  11. import { BaseSchema } from "@/modules/DataModule/types/Schemas";
  12. import JobContext from "@/JobContext";
  13. import { NewsStatus } from "./NewsStatus";
  14. import config from "./config";
  15. export interface NewsSchema extends BaseSchema {
  16. title: string;
  17. markdown: string;
  18. status: NewsStatus;
  19. showToNewUsers: boolean;
  20. createdBy: Types.ObjectId;
  21. }
  22. export interface NewsQueryHelpers {
  23. published(
  24. this: QueryWithHelpers<
  25. any,
  26. HydratedDocument<NewsSchema>,
  27. NewsQueryHelpers
  28. >,
  29. published?: boolean
  30. ): QueryWithHelpers<
  31. HydratedDocument<NewsSchema>[],
  32. HydratedDocument<NewsSchema>,
  33. NewsQueryHelpers
  34. >;
  35. newest(
  36. this: QueryWithHelpers<
  37. any,
  38. HydratedDocument<NewsSchema>,
  39. NewsQueryHelpers
  40. >,
  41. showToNewUsers?: boolean
  42. ): QueryWithHelpers<
  43. HydratedDocument<NewsSchema>[],
  44. HydratedDocument<NewsSchema>,
  45. NewsQueryHelpers
  46. >;
  47. }
  48. export interface NewsModel
  49. extends Model<NewsSchema, NewsQueryHelpers>,
  50. GetData {
  51. published: (context: JobContext) => Promise<NewsSchema[]>;
  52. newest: (
  53. context: JobContext,
  54. payload: { showToNewUsers?: boolean }
  55. ) => Promise<NewsSchema[]>;
  56. }
  57. // eslint-disable-next-line @typescript-eslint/ban-types
  58. export const schema = new Schema<NewsSchema, NewsModel, {}, NewsQueryHelpers>(
  59. {
  60. title: {
  61. type: SchemaTypes.String,
  62. required: true
  63. },
  64. markdown: {
  65. type: SchemaTypes.String,
  66. required: true
  67. },
  68. status: {
  69. type: SchemaTypes.String,
  70. enum: Object.values(NewsStatus),
  71. default: NewsStatus.DRAFT,
  72. required: true
  73. },
  74. showToNewUsers: {
  75. type: SchemaTypes.Boolean,
  76. default: false,
  77. required: true
  78. },
  79. createdBy: {
  80. type: SchemaTypes.ObjectId,
  81. ref: "users",
  82. required: true
  83. }
  84. },
  85. config
  86. );
  87. export type NewsSchemaType = typeof schema;
  88. // eslint-disable-next-line @typescript-eslint/ban-types
  89. export type NewsSchemaOptions = SchemaOptions<NewsSchema, {}, NewsQueryHelpers>;