将通用类型作为funciton中的参数传递

发布于 2025-01-31 09:27:56 字数 1891 浏览 2 评论 0原文

我正在尝试使用AWS数据存储设置CRUD服务,以更加整理我的代码库,并在需要时切换后端,我很难弄清楚如何通过类型(而不是变量)传递给datastore的功能。我现在所拥有的如下:

import { DataStore } from "@aws-amplify/datastore";

export default class CrudService<T> {
  constructor() {}

  public static async post(data: any): Promise<any> {
    return await DataStore.save(data);
  }

  public static async get(id: string): Promise<any> {
    return await DataStore.query(T, id); // This doesn't work...
  }

  public static async put(data: any): Promise<any> {
    return await DataStore.save(data);
  }

  public static async remove(id: string): Promise<any> {
    return await DataStore.delete(T, id); // This too doesn't work...
  }
}

但是,这有效

  import { UserModel } from "../models";

  public static async read(id: string): Promise<any> {
    return await DataStore.query(UserModel, id);
  }

,但是我想让这个crudservice能够与任何具有其各自型号类型的API一起使用,以便当我想使用它时,我会这样使用它:

import { IApi } from "../interfaces";
import { UserModel } from "../models";
import { CrudService } from "../services";

export default class UsersAPI extends CrudService<UserModel> implements IApi<UserModel> {  
  public create = async (data: UserModel): Promise<UserModel> => {
    return await UsersAPI.post(data)
    // return await DataStore.save(data);
  };

  public read = async (id: string): Promise<UserModel[]> => {
    return await UsersAPI.get(id);
    // return await DataStore.query(UserModel, id);
  };

  public update = async (data: UserModel): Promise<UserModel> => {
    return await UsersAPI.put(data);
    // return await DataStore.save(data);
  };

  public delete = async (id: string): Promise<any> => {
    return await UsersAPI.remove(id);
    // return await DataStore.delete(UserModel, id);
  };
}

I'm trying to setup a Crud Service with AWS Datastore to more organize my codebase and be able to switch backends when needed, I'm having a hard time trying to figure out how to pass in the Class type, not a variable, to the function for Datastore. What I have now is as follows:

import { DataStore } from "@aws-amplify/datastore";

export default class CrudService<T> {
  constructor() {}

  public static async post(data: any): Promise<any> {
    return await DataStore.save(data);
  }

  public static async get(id: string): Promise<any> {
    return await DataStore.query(T, id); // This doesn't work...
  }

  public static async put(data: any): Promise<any> {
    return await DataStore.save(data);
  }

  public static async remove(id: string): Promise<any> {
    return await DataStore.delete(T, id); // This too doesn't work...
  }
}

However this works

  import { UserModel } from "../models";

  public static async read(id: string): Promise<any> {
    return await DataStore.query(UserModel, id);
  }

But I want to make this CrudService to be able to use with any api with its respective model type so that when I want to use it I use it like this:

import { IApi } from "../interfaces";
import { UserModel } from "../models";
import { CrudService } from "../services";

export default class UsersAPI extends CrudService<UserModel> implements IApi<UserModel> {  
  public create = async (data: UserModel): Promise<UserModel> => {
    return await UsersAPI.post(data)
    // return await DataStore.save(data);
  };

  public read = async (id: string): Promise<UserModel[]> => {
    return await UsersAPI.get(id);
    // return await DataStore.query(UserModel, id);
  };

  public update = async (data: UserModel): Promise<UserModel> => {
    return await UsersAPI.put(data);
    // return await DataStore.save(data);
  };

  public delete = async (id: string): Promise<any> => {
    return await UsersAPI.remove(id);
    // return await DataStore.delete(UserModel, id);
  };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文