named-query.ts 1.1 KB

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