Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong type for M2M relations? #21

Closed
linaGirl opened this issue Feb 13, 2023 · 6 comments
Closed

Wrong type for M2M relations? #21

linaGirl opened this issue Feb 13, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@linaGirl
Copy link

Thanks for the great extension!

Looks like there is a problem with M2M relations when creating types for TypeScript:

Database

CREATE TABLE "scene" (
    `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL
  , `name` varchar(255) NOT NULL
)
CREATE TABLE "audioScene" (
    `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL
  , `name` varchar(255) NOT NULL 
)
CREATE TABLE "scene_audioScene" (
    `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, 
   ,`scene` integer NOT NULL
   ,`audioScene` integer NOT NULL
   ,CONSTRAINT `scene_audioscene_scene_foreign` FOREIGN KEY (`scene`) REFERENCES `scene` (`id`) 
   ,CONSTRAINT `scene_audioscene_audioscene_foreign` FOREIGN KEY (`audioScene`) REFERENCES `audioScene` (`id`)
)

Generated Types

export type AudioScene = {
  id: number;
  name: string;
  scene: string & SceneAudioScene[];
};
export type Scene = {
  audioScene: string & SceneAudioScene[];
  id: number;
  name: string;
};
export type SceneAudioScene = {
  audioScene: number & AudioScene;
  id: number;
  scene: number & Scene;
};

Why does the scene M2M reference scene on the Type AudioScene have the type string & SceneAudioScene[]; Seems to be that it never will be a string. Same for the reference audioScene on the type Scene.

@maltejur maltejur added the bug Something isn't working label Feb 13, 2023
@maltejur
Copy link
Owner

Oh you are right. The type returned by the directus API doesn't seem to match the type used by the SDK in this case. Since we still have to cover the case that the Directus SDK just returns the item IDs instead of the actual relational items, would any[] be a good alternative? So for example:

export type Scene = {
  audioScene: any[] & SceneAudioScene[];

@maltejur
Copy link
Owner

On a second look the Directus API doesn't even return something wrong, I just use "string" as the default type if no other one is given.

@linaGirl
Copy link
Author

Since the SDK returns an empty array if there are no relations or nothing if the relation is not requested audioScene?: SceneAudioScene[]; could be fine?

@maltejur
Copy link
Owner

maltejur commented Feb 13, 2023

The problem is that sometimes the list just contains the foreign ids of the relational fields, and those are pretty complicated to find out. This is why I would suggest using any[] as the type there. See this example:

image

@linaGirl
Copy link
Author

Ok, i see. Your suggestion seems to be the correct one!

audioScene?: any[] & SceneAudioScene[];

@maltejur
Copy link
Owner

Fixed in eb83e90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants