Skip to content

Generate relations for entities #4

@RWOverdijk

Description

@RWOverdijk

Either through patching existing entities or by generating entities that don't exist yet. Or both. Snippet:

class SomeGenerator {
public async generateEntity (output: Output, { name, field }: { name: string, field: string[] }) {
    const fields = Array.isArray(field) ? field : [ field ];
    const { pascalCased, camelCased } = this.generatorService.formatNames(name);

    const relationRegex = /^(?:([\w]+\b)?(\*)?(-|<>|>|<)?(\*)?([\w]+)(?:\.)?(\w+)?)$/g;
    const relationsMap: { [key: string]: string } = {
      '-': 'oneToOne',
      '>': 'manyToOne',
      '<': 'oneToMany',
      '<>': 'manyToMany',
    };

    const relations = fields.map(f => {
      const matches = relationRegex.exec(f);

      return {
        entity: pascalCased,
        relation: relationsMap[matches[3] || '-'],
        property: matches[1] || this.generatorService.formatNames(matches[5]).camelCased,
        owning: !!matches[2] || !matches[4],
        other: {
          property: matches[6] || camelCased,
          entity: matches[5],
          owning: !!matches[4],
        },
      };
    });

    output.addData(relations);
  }
}

Example use:

  • stix generate:entity user user -r profile>Profile.user

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions