• Mapping foreign key value to primary key value

    This functionality is almost similar to mongodb lookup https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/

    Type Parameters

    • TPrimary extends string | number

    • TForeign extends object

    • TResult extends object

    Parameters

    • primaryItems: TPrimary[]

      Array of primary object data or string or number

    • foreignItems: TForeign[]

      Array of foreign object data

    • options: Pick<MappingOptions, "foreignKey">

      mapping options

      • asField: path for the result of mapping
      • foreignKey: foreign key path
      • primaryKey: primary key path

    Returns TResult[]

    Array of primary data with foreign data

    Example: array of objects

    const primaryArray = [
    { id: 1, userId: 1 },
    { id: 2, userId: 2 },
    ];
    const userArray = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Mary' },
    ];
    const result = mapping(
    primaryArray,
    userArray,
    { foreignKey: 'id', primaryKey: 'userId', asField: 'user' },
    );
    expect(result).toEqual([
    { id: 1, user: { id: 1, name: 'John' }, userId: 1 },
    { id: 2, user: { id: 2, name: 'Mary' }, userId: 2 },
    ]);

    Example: array of objects with array of primary key

    const primaryArray = [
    { id: 1, users: [1, 2] },
    ];
    const userArray = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Mary' },
    ];
    const result = mapping(
    primaryArray,
    userArray,
    { foreignKey: 'id', primaryKey: 'users' },
    );
    expect(result).toEqual([
    { id: 1, users: [{ id: 1, name: 'John' }, { id: 2, name: 'Mary' }] },
    ]);

    Example: array of strings or numbers

    const primaryArray = [1, 2];
    const userArray = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Mary' },
    ];
    const result = mapping(
    primaryArray,
    userArray,
    { foreignKey: 'id' },
    );
    expect(result).toEqual([
    { id: 1, name: 'John' },
    { id: 2, name: 'Mary' },
    ]);
  • Type Parameters

    • TPrimary extends object

    • TForeign extends object

    • TResult extends object

    Parameters

    Returns TResult[]

Generated using TypeDoc