|
|
@@ -3,6 +3,7 @@ import ApiResource from "~/models/ApiResource";
|
|
|
import {AssociativeArray, Collection} from "~/types/data";
|
|
|
import {AsyncData} from "#app";
|
|
|
import {ComputedRef, Ref} from "vue";
|
|
|
+import {v4 as uuid4} from "uuid";
|
|
|
|
|
|
interface useEntityFetchReturnType {
|
|
|
fetch: (model: typeof ApiResource, id: number) => AsyncData<ApiResource, ApiResource | true>,
|
|
|
@@ -11,19 +12,18 @@ interface useEntityFetchReturnType {
|
|
|
getRef: <T extends ApiResource>(model: typeof T, id: Ref<number | null>) => ComputedRef<null | T>
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// TODO: améliorer le typage des fonctions sur le modèle de getRef
|
|
|
export const useEntityFetch = (lazy: boolean = false): useEntityFetchReturnType => {
|
|
|
const { em } = useEntityManager()
|
|
|
|
|
|
const fetch = (model: typeof ApiResource, id: number) => useAsyncData(
|
|
|
- model.entity + '_' + id,
|
|
|
+ model.entity + '_' + id + '_' + uuid4(),
|
|
|
() => em.fetch(model, id, true),
|
|
|
{ lazy }
|
|
|
)
|
|
|
|
|
|
const fetchCollection = (model: typeof ApiResource, parent: ApiResource | null = null, query: Ref<AssociativeArray | null> = ref(null)) => useAsyncData(
|
|
|
- model.entity + '_many',
|
|
|
+ model.entity + '_many_' + uuid4(),
|
|
|
() => em.fetchCollection(model, parent, query.value),
|
|
|
{ lazy }
|
|
|
)
|