|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
} from 'pinia-orm'
|
|
|
import { v4 as uuid4 } from 'uuid'
|
|
|
import * as _ from 'lodash-es'
|
|
|
-import { computed } from 'vue'
|
|
|
+import {computed, type ComputedRef} from 'vue'
|
|
|
import type ApiRequestService from './apiRequestService'
|
|
|
import UrlUtils from '~/services/utils/urlUtils'
|
|
|
import type ApiModel from '~/models/ApiModel'
|
|
|
@@ -35,13 +35,13 @@ class EntityManager {
|
|
|
* A method to retrieve the repository of a given ApiResource
|
|
|
* @protected
|
|
|
*/
|
|
|
- protected _getRepo: (model: typeof ApiResource) => Repository<ApiResource>
|
|
|
+ protected _getRepo: <T extends typeof ApiResource>(model: T) => Repository<InstanceType<T>>
|
|
|
|
|
|
protected _getProfileMask: () => object
|
|
|
|
|
|
public constructor(
|
|
|
apiRequestService: ApiRequestService,
|
|
|
- getRepo: (model: typeof ApiResource) => Repository<ApiResource>,
|
|
|
+ getRepo: <T extends typeof ApiResource>(model: T) => Repository<InstanceType<T>>,
|
|
|
getProfileMask: () => object,
|
|
|
) {
|
|
|
this.apiRequestService = apiRequestService
|
|
|
@@ -54,7 +54,7 @@ class EntityManager {
|
|
|
*
|
|
|
* @param model
|
|
|
*/
|
|
|
- public getRepository(model: typeof ApiResource): Repository<ApiResource> {
|
|
|
+ public getRepository<T extends typeof ApiResource>(model: T): Repository<InstanceType<T>> {
|
|
|
return this._getRepo(model)
|
|
|
}
|
|
|
|
|
|
@@ -63,7 +63,7 @@ class EntityManager {
|
|
|
*
|
|
|
* @param model
|
|
|
*/
|
|
|
- public getQuery(model: typeof ApiResource): PiniaOrmQuery<ApiResource> {
|
|
|
+ public getQuery<T extends typeof ApiResource>(model: T): PiniaOrmQuery<InstanceType<T>> {
|
|
|
// TODO: quid des uuid?
|
|
|
return this.getRepository(model).where((val) => Number.isInteger(val.id))
|
|
|
}
|
|
|
@@ -80,8 +80,8 @@ class EntityManager {
|
|
|
* @param instance
|
|
|
*/
|
|
|
// noinspection JSMethodCanBeStatic
|
|
|
- public cast(model: typeof ApiResource, instance: ApiResource): ApiResource {
|
|
|
- return new model(instance)
|
|
|
+ public cast<T extends typeof ApiResource>(model: T, instance: InstanceType<T>): InstanceType<T> {
|
|
|
+ return new model(instance) as InstanceType<T>
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -115,13 +115,13 @@ class EntityManager {
|
|
|
* @param model
|
|
|
* @param properties
|
|
|
*/
|
|
|
- public newInstance(
|
|
|
- model: typeof ApiResource,
|
|
|
+ public newInstance<T extends typeof ApiResource>(
|
|
|
+ model: T,
|
|
|
properties: object = {},
|
|
|
- ): ApiResource {
|
|
|
+ ): InstanceType<T> {
|
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
- const instance = repository.make(properties)
|
|
|
+ const instance = repository.make(properties)
|
|
|
|
|
|
if (
|
|
|
!Object.prototype.hasOwnProperty.call(properties, 'id') ||
|
|
|
@@ -142,7 +142,7 @@ class EntityManager {
|
|
|
* @param permanent Is the change already persisted in the datasource? If this is the case, the initial state of this
|
|
|
* record is also updated.
|
|
|
*/
|
|
|
- public save(instance: ApiResource, permanent: boolean = false): ApiResource {
|
|
|
+ public save<T extends ApiResource>(instance: T, permanent: boolean = false): T {
|
|
|
const model = this.getModel(instance)
|
|
|
|
|
|
this.validateEntity(instance)
|
|
|
@@ -151,7 +151,7 @@ class EntityManager {
|
|
|
this.saveInitialState(model, instance)
|
|
|
}
|
|
|
|
|
|
- return this.getRepository(model).save(instance)
|
|
|
+ return this.getRepository(model).save(instance) as T
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -161,8 +161,7 @@ class EntityManager {
|
|
|
* @param model
|
|
|
* @param id
|
|
|
*/
|
|
|
- // @ts-expect-error TODO: corriger au moment de l'implémentation des types génériques
|
|
|
- public find<T extends ApiResource>(model: typeof T, id: number | string): T {
|
|
|
+ public find<T extends typeof ApiResource>(model: T, id: number | string): T {
|
|
|
const repository = this.getRepository(model)
|
|
|
return repository.find(id) as T
|
|
|
}
|
|
|
@@ -173,10 +172,10 @@ class EntityManager {
|
|
|
* @param model Model of the object to fetch
|
|
|
* @param id Id of the object to fetch
|
|
|
*/
|
|
|
- public async fetch(
|
|
|
- model: typeof ApiResource,
|
|
|
+ public async fetch <T extends typeof ApiResource>(
|
|
|
+ model: T,
|
|
|
id?: number | null,
|
|
|
- ): Promise<ApiResource> {
|
|
|
+ ): Promise<InstanceType<T>> {
|
|
|
// Else, get the object from the API
|
|
|
const url = UrlUtils.join('api', model.entity, id ? String(id) : '')
|
|
|
const response = await this.apiRequestService.get(url)
|
|
|
@@ -317,9 +316,9 @@ class EntityManager {
|
|
|
* @param instance
|
|
|
* @param instance
|
|
|
*/
|
|
|
- public async delete(instance: ApiModel) {
|
|
|
+ public async delete<T extends ApiResource>(instance: T) {
|
|
|
const model = this.getModel(instance)
|
|
|
- instance = this.cast(model, instance)
|
|
|
+ instance = this.cast(model, instance) as T
|
|
|
|
|
|
console.log('delete', instance)
|
|
|
|
|
|
@@ -345,7 +344,7 @@ class EntityManager {
|
|
|
* @param model
|
|
|
* @param instance
|
|
|
*/
|
|
|
- public reset(instance: ApiResource) {
|
|
|
+ public reset<T extends ApiResource>(instance: T) {
|
|
|
const model = this.getModel(instance)
|
|
|
|
|
|
const initialInstance = this.getInitialStateOf(model, instance.id)
|
|
|
@@ -370,7 +369,7 @@ class EntityManager {
|
|
|
*
|
|
|
* @param model
|
|
|
*/
|
|
|
- public flush(model: typeof ApiModel) {
|
|
|
+ public flush<T extends typeof ApiResource>(model: T) {
|
|
|
const repository = this.getRepository(model)
|
|
|
repository.flush()
|
|
|
}
|
|
|
@@ -384,7 +383,7 @@ class EntityManager {
|
|
|
* @param model
|
|
|
* @param id
|
|
|
*/
|
|
|
- public isNewInstance(model: typeof ApiModel, id: number | string): boolean {
|
|
|
+ public isNewInstance<T extends typeof ApiResource>(model: T, id: number | string): boolean {
|
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
const item = repository.find(id)
|
|
|
@@ -404,7 +403,7 @@ class EntityManager {
|
|
|
* @param instance
|
|
|
* @private
|
|
|
*/
|
|
|
- protected saveInitialState(model: typeof ApiResource, instance: ApiResource) {
|
|
|
+ protected saveInitialState<T extends typeof ApiResource>(model: T, instance: InstanceType<T>) {
|
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
// Clone and prefix id
|
|
|
@@ -421,10 +420,10 @@ class EntityManager {
|
|
|
* @param id
|
|
|
* @private
|
|
|
*/
|
|
|
- protected getInitialStateOf(
|
|
|
- model: typeof ApiResource,
|
|
|
+ protected getInitialStateOf<T extends typeof ApiResource>(
|
|
|
+ model: T,
|
|
|
id: string | number,
|
|
|
- ): ApiResource | null {
|
|
|
+ ): InstanceType<T> | null {
|
|
|
const repository = this.getRepository(model)
|
|
|
|
|
|
// Find the clone by id
|
|
|
@@ -446,8 +445,8 @@ class EntityManager {
|
|
|
* @param tempInstanceId
|
|
|
* @private
|
|
|
*/
|
|
|
- protected removeTempAfterPersist(
|
|
|
- model: typeof ApiResource,
|
|
|
+ protected removeTempAfterPersist<T extends typeof ApiResource>(
|
|
|
+ model: T,
|
|
|
tempInstanceId: number | string,
|
|
|
) {
|
|
|
const repository = this.getRepository(model)
|