Skip to content

Commit

Permalink
Fix aliases (#7)
Browse files Browse the repository at this point in the history
* fix alias type assignment

* make aliases arrays

* remove wrong code

* formatting
  • Loading branch information
falmanna authored Oct 18, 2023
1 parent be4a052 commit 546e508
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {defineHook} from '@directus/extensions-sdk';
import { defineHook } from '@directus/extensions-sdk';
import type { CollectionsOverview, FieldOverview, SchemaOverview } from "@directus/shared/types";
import type { Command } from "commander";
import { mkdir, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import pluralize from 'pluralize';
import {mkdir, writeFile} from "node:fs/promises";
import {dirname} from "node:path";
import type {Command} from "commander";
import type {CollectionsOverview, FieldOverview, SchemaOverview} from "@directus/shared/types";

type Collection = CollectionsOverview[''];

Expand Down Expand Up @@ -35,25 +35,26 @@ function fieldToRelationType(field: FieldOverview, collection: Collection, schem
return `${targetClassName} | ${keyType}`;
}


function aliasToType(field: FieldOverview, collection: Collection, schema: SchemaOverview): string | null {
const relation = schema.relations.find(r => r?.meta?.one_collection === collection.collection && r?.meta?.one_field);
const relation = schema.relations.find(r => r?.meta?.one_collection === collection.collection && r?.meta?.one_field === field.field);
if (!relation) {
return null;
}
return className(schema.collections[relation.meta.many_collection]);
return `${className(schema.collections[relation.meta.many_collection])}[]`;
}

function fieldTypeToJsType(field: FieldOverview, collection: Collection): string {
switch (field.type) {
case"boolean":
case "boolean":
return "boolean";
case "integer":
case "float":
case "decimal":
case "bigInteger":
return "number";
case "dateTime":
case"date":
case "date":
case "time":
case "timestamp":
// TODO: Validate this
Expand Down Expand Up @@ -168,8 +169,8 @@ ${exportKeyword} type Collections = {
return source;
}

export default defineHook(async ({init}, {services, getSchema, database, logger}) => {
init('cli.after', ({program}: any) => {
export default defineHook(async ({ init }, { services, getSchema, database, logger }) => {
init('cli.after', ({ program }: any) => {

const modelTypesCommand: Command = program.command('models')
.description('Export the currently connected database to .d.ts files');
Expand Down

0 comments on commit 546e508

Please sign in to comment.