将通用类型作为funciton中的参数传递
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论