named-query.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. import type { Model, Document } from 'mongoose';
  3. import {
  4. Schema,
  5. } from 'mongoose';
  6. import type { INamedQuery } from '~/interfaces/named-query';
  7. import { SearchDelegatorName } from '~/interfaces/named-query';
  8. import loggerFactory from '../../utils/logger';
  9. import { getOrCreateModel } from '../util/mongoose-utils';
  10. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  11. const logger = loggerFactory('growi:models:named-query');
  12. export interface NamedQueryDocument extends INamedQuery, Document {}
  13. export type NamedQueryModel = Model<NamedQueryDocument>
  14. const schema = new Schema<NamedQueryDocument, NamedQueryModel>({
  15. name: { type: String, required: true, unique: true },
  16. aliasOf: { type: String },
  17. delegatorName: { type: String, enum: SearchDelegatorName },
  18. creator: {
  19. type: Schema.Types.ObjectId, ref: 'User', index: true, default: null,
  20. },
  21. });
  22. schema.pre('validate', async function(this, next) {
  23. if (this.aliasOf == null && this.delegatorName == null) {
  24. throw Error('Either of aliasOf and delegatorNameName must not be null.');
  25. }
  26. next();
  27. });
  28. export default getOrCreateModel<NamedQueryDocument, NamedQueryModel>('NamedQuery', schema);