BetaBlox / skybolt

A starter template for a full stack nestjs/react monorepo
0 stars 0 forks source link

Make it more obvious how to add related data in the admin #129

Closed johnrake-betablox closed 1 month ago

johnrake-betablox commented 1 month ago
attributeTypes: [
      {
        name: 'user',
        type: AdminFieldType.RELATIONSHIP_HAS_ONE,
        sourceKey: 'userId',
        modelName: 'user',
      },
      { name: 'userId', type: AdminFieldType.INTEGER },
      { name: 'preferredName', type: AdminFieldType.STRING },
      { name: 'attire', type: AdminFieldType.STRING },
      { name: 'spouseFirstName', type: AdminFieldType.STRING },
      { name: 'spouseLastName', type: AdminFieldType.STRING },
      { name: 'spousePreferredName', type: AdminFieldType.STRING },
      { name: 'spousePronouns', type: AdminFieldType.STRING },
      { name: 'spouseAttire', type: AdminFieldType.STRING },
      { name: 'spouseBirthday', type: AdminFieldType.STRING },
      { name: 'hasEstimatedGuestCount', type: AdminFieldType.BOOLEAN },
      { name: 'guestCount', type: AdminFieldType.INTEGER },
      { name: 'guestNotes', type: AdminFieldType.TEXT },
      { name: 'budget', type: AdminFieldType.INTEGER },
      { name: 'budgetFeeling', type: AdminFieldType.STRING },
      { name: 'websiteUrl', type: AdminFieldType.STRING },
      { name: 'pinterestUrl', type: AdminFieldType.STRING },
      {
        name: 'venue',
        type: AdminFieldType.RELATIONSHIP_HAS_ONE,
        sourceKey: 'venueId',
        modelName: 'venue',
      },
      { name: 'venueId', type: AdminFieldType.INTEGER },
      {
        name: 'planner',
        type: AdminFieldType.RELATIONSHIP_HAS_ONE,
        sourceKey: 'plannerId',
        modelName: 'planner',
      },
      { name: 'plannerId', type: AdminFieldType.INTEGER },
      { name: 'officialDate', type: AdminFieldType.DATETIME },
      { name: 'createdAt', type: AdminFieldType.DATETIME },
      { name: 'updatedAt', type: AdminFieldType.DATETIME },
    ],
johnrake-betablox commented 1 month ago

Handled with discriminate union types

export type AdminAttributeType =
  // Basic types with no additional properties
  | {
      name: string;
      type:
        | AdminFieldType.STRING
        | AdminFieldType.TEXT
        | AdminFieldType.PASSWORD
        | AdminFieldType.BOOLEAN
        | AdminFieldType.INTEGER
        | AdminFieldType.JSON
        | AdminFieldType.DATETIME;
    }
  // Select type with options
  | {
      name: string;
      type: AdminFieldType.SELECT;
      options: SelectOption[]; // Required for the SELECT type
    }
  // Has One relationship
  | {
      name: string;
      type: AdminFieldType.RELATIONSHIP_HAS_ONE;
      modelName: string; // The related model's name
      sourceKey: string; // The field in the related model
      relatedAttributes?: RelatedAttribute[];
    }
  // Has Many relationship
  | {
      name: string;
      type: AdminFieldType.RELATIONSHIP_HAS_MANY;
      modelName: string; // The related model's name
      relationField: string; // The field in the related model
      relatedAttributes?: RelatedAttribute[];
    };