【问题标题】:solution approach for the same nodejs methods相同nodejs方法的解决方法
【发布时间】:2021-02-25 09:56:04
【问题描述】:

我为每个集合提供单独的服务。每个都有相同的标准方法 (CRUD)。

例如

评论服务(更新评论、删除评论)

  static updateComment = async (args: { commentID: string; commentData: IComment }): Promise<IComment> => {
    const response = (await Comment.updateOne({ _id: args.commentID }, args.commentData)) as IComment;
    return response;
  };

  static deleteComment = async (args: { commentID: string }): Promise<IComment> => {
    const response: IComment = (await Comment.deleteOne({ _id: args.commentID })) as IComment;
    return response;
  };

事件服务(更新事件、删除事件)

  static updateEvent = async (args: { eventID: string; eventData: IEvent }): Promise<IEvent> => {
    const response: IEvent = (await Event.updateOne({ _id: args.eventID }, args.eventData)) as IEvent;
    return response;
  };

  static deleteEvent = async (args: { eventID: string }): Promise<IEvent> => {
    const response: IEvent = (await Event.deleteOne({ _id: args.eventID })) as IEvent;
    return response;
  };

现在我的问题 总结这些标准方法有哪些可能性?通用是正确的方法吗?

希望你能帮助我,在此先感谢:)

【问题讨论】:

    标签: node.js typescript generics mongoose


    【解决方案1】:

    创建一个您的具体服务类扩展的通用服务超类怎么样?您的具体类仍然可以实现自定义行为,但所有常见逻辑都被移到超类中。比如:

    import mongoose, {Model, Schema,} from "mongoose";
    
    interface IComment extends mongoose.Document {
        commentData: string;
    }
    
    const CommentSchema: Schema = new Schema({
        commentData: {type: String, required: true},
    });
    
    interface IEvent extends mongoose.Document {
        name: string;
        date: string;
    }
    
    const EventSchema: Schema = new Schema({
        name: {type: String, required: true},
        date: {type: String, required: true},
    });
    
    export class GenericDataService<S extends mongoose.Document> {
        private dataModel: Model<S>;
    
        constructor(collection: string, schema: Schema) {
            this.dataModel = mongoose.model(collection, schema);
        }
    
        async read(id: string): Promise<S> {
            const response = await this.dataModel.findById(id);
            return response;
        }
       
        // implement the rest of crud operations
    }
    
    export class CommentDataService extends GenericDataService<IComment> {
        // implement custom methods if needed
    }
    
    export class EventDataService extends GenericDataService<IEvent> {
        // implement custom methods if needed
    }
    

    然后你可以像这样使用它,打字稿会自动推断出正确的类型:

    const commentDataService = new CommentDataService('Comment', CommentSchema);
    const commentResult = await commentDataService.read("commentId"); // commentResult is typeof IComment
    
    const eventDataService = new EventDataService('Event', EventSchema);
    const eventResult = await eventDataService.read("eventId"); // eventResult is typeof IEvent
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2021-09-14
      • 1970-01-01
      • 2014-07-28
      • 2012-10-11
      • 1970-01-01
      • 2018-06-01
      相关资源
      最近更新 更多