Skip to content

Commit

Permalink
feat(core): Support for custom fields on Asset entity
Browse files Browse the repository at this point in the history
Relates to #684
  • Loading branch information
michaelbromley committed Feb 18, 2021
1 parent 7ba27f2 commit 60b6171
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/admin-ui/src/lib/core/src/common/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,7 @@ export type Asset = Node & {
source: Scalars['String'];
preview: Scalars['String'];
focalPoint?: Maybe<Coordinate>;
customFields?: Maybe<Scalars['JSON']>;
};

export type MimeTypeError = ErrorResult & {
Expand All @@ -1142,6 +1143,7 @@ export type AssetListOptions = {
export type CreateAssetInput = {
file: Scalars['Upload'];
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type CoordinateInput = {
Expand All @@ -1166,6 +1168,7 @@ export type UpdateAssetInput = {
name?: Maybe<Scalars['String']>;
focalPoint?: Maybe<CoordinateInput>;
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type AssignAssetsToChannelInput = {
Expand Down Expand Up @@ -4802,6 +4805,7 @@ export type CustomFields = {
__typename?: 'CustomFields';
Address: Array<CustomFieldConfig>;
Administrator: Array<CustomFieldConfig>;
Asset: Array<CustomFieldConfig>;
Channel: Array<CustomFieldConfig>;
Collection: Array<CustomFieldConfig>;
Customer: Array<CustomFieldConfig>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ export type Asset = Node & {
source: Scalars['String'];
preview: Scalars['String'];
focalPoint?: Maybe<Coordinate>;
customFields?: Maybe<Scalars['JSON']>;
};

export type MimeTypeError = ErrorResult & {
Expand All @@ -955,6 +956,7 @@ export type AssetListOptions = {
export type CreateAssetInput = {
file: Scalars['Upload'];
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type CoordinateInput = {
Expand All @@ -979,6 +981,7 @@ export type UpdateAssetInput = {
name?: Maybe<Scalars['String']>;
focalPoint?: Maybe<CoordinateInput>;
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type AssignAssetsToChannelInput = {
Expand Down Expand Up @@ -4513,6 +4516,7 @@ export type NativeAuthInput = {
export type CustomFields = {
Address: Array<CustomFieldConfig>;
Administrator: Array<CustomFieldConfig>;
Asset: Array<CustomFieldConfig>;
Channel: Array<CustomFieldConfig>;
Collection: Array<CustomFieldConfig>;
Customer: Array<CustomFieldConfig>;
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ export type Asset = Node & {
source: Scalars['String'];
preview: Scalars['String'];
focalPoint?: Maybe<Coordinate>;
customFields?: Maybe<Scalars['JSON']>;
};

export type Coordinate = {
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ export type Asset = Node & {
source: Scalars['String'];
preview: Scalars['String'];
focalPoint?: Maybe<Coordinate>;
customFields?: Maybe<Scalars['JSON']>;
};

export type MimeTypeError = ErrorResult & {
Expand All @@ -1105,6 +1106,7 @@ export type AssetListOptions = {
export type CreateAssetInput = {
file: Scalars['Upload'];
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type CoordinateInput = {
Expand All @@ -1129,6 +1131,7 @@ export type UpdateAssetInput = {
name?: Maybe<Scalars['String']>;
focalPoint?: Maybe<CoordinateInput>;
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type AssignAssetsToChannelInput = {
Expand Down Expand Up @@ -4764,6 +4767,7 @@ export type CustomFields = {
__typename?: 'CustomFields';
Address: Array<CustomFieldConfig>;
Administrator: Array<CustomFieldConfig>;
Asset: Array<CustomFieldConfig>;
Channel: Array<CustomFieldConfig>;
Collection: Array<CustomFieldConfig>;
Customer: Array<CustomFieldConfig>;
Expand Down
1 change: 1 addition & 0 deletions packages/core/e2e/custom-field-relations.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ describe('Custom field relations', () => {
'height',
'source',
'preview',
'customFields',
]);
});

Expand Down
4 changes: 4 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ export type Asset = Node & {
source: Scalars['String'];
preview: Scalars['String'];
focalPoint?: Maybe<Coordinate>;
customFields?: Maybe<Scalars['JSON']>;
};

export type MimeTypeError = ErrorResult & {
Expand All @@ -955,6 +956,7 @@ export type AssetListOptions = {
export type CreateAssetInput = {
file: Scalars['Upload'];
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type CoordinateInput = {
Expand All @@ -979,6 +981,7 @@ export type UpdateAssetInput = {
name?: Maybe<Scalars['String']>;
focalPoint?: Maybe<CoordinateInput>;
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type AssignAssetsToChannelInput = {
Expand Down Expand Up @@ -4513,6 +4516,7 @@ export type NativeAuthInput = {
export type CustomFields = {
Address: Array<CustomFieldConfig>;
Administrator: Array<CustomFieldConfig>;
Asset: Array<CustomFieldConfig>;
Channel: Array<CustomFieldConfig>;
Collection: Array<CustomFieldConfig>;
Customer: Array<CustomFieldConfig>;
Expand Down
1 change: 1 addition & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export type Asset = Node & {
source: Scalars['String'];
preview: Scalars['String'];
focalPoint?: Maybe<Coordinate>;
customFields?: Maybe<Scalars['JSON']>;
};

export type Coordinate = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export type CustomFieldConfig =
export interface CustomFields {
Address?: CustomFieldConfig[];
Administrator?: CustomFieldConfig[];
Asset?: CustomFieldConfig[];
Channel?: CustomFieldConfig[];
Collection?: CustomFieldConfig[];
Customer?: CustomFieldConfig[];
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const defaultConfig: RuntimeVendureConfig = {
customFields: {
Address: [],
Administrator: [],
Asset: [],
Channel: [],
Collection: [],
Customer: [],
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/entity/asset/asset.entity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { AssetType } from '@vendure/common/lib/generated-types';
import { DeepPartial } from '@vendure/common/lib/shared-types';
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne } from 'typeorm';
import { Column, Entity, JoinTable, ManyToMany } from 'typeorm';

import { Channel } from '..';
import { ChannelAware, Taggable } from '../../common/types/common-types';
import { Address } from '../address/address.entity';
import { HasCustomFields } from '../../config/custom-field/custom-field-types';
import { VendureEntity } from '../base/base.entity';
import { CustomCustomerFields } from '../custom-entity-fields';
import { Channel } from '../channel/channel.entity';
import { CustomAssetFields } from '../custom-entity-fields';
import { Tag } from '../tag/tag.entity';
import { User } from '../user/user.entity';

/**
* @description
Expand All @@ -18,7 +17,7 @@ import { User } from '../user/user.entity';
* @docsCategory entities
*/
@Entity()
export class Asset extends VendureEntity implements Taggable, ChannelAware {
export class Asset extends VendureEntity implements Taggable, ChannelAware, HasCustomFields {
constructor(input?: DeepPartial<Asset>) {
super(input);
}
Expand Down Expand Up @@ -49,4 +48,7 @@ export class Asset extends VendureEntity implements Taggable, ChannelAware {
@ManyToMany(type => Channel)
@JoinTable()
channels: Channel[];

@Column(type => CustomAssetFields)
customFields: CustomAssetFields;
}
1 change: 1 addition & 0 deletions packages/core/src/entity/custom-entity-fields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export class CustomAddressFields {}
export class CustomAdministratorFields {}
export class CustomAssetFields {}
export class CustomFacetFields {}
export class CustomFacetFieldsTranslation {}
export class CustomFacetValueFields {}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/entity/register-custom-entity-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { VendureConfig } from '../config/vendure-config';
import {
CustomAddressFields,
CustomAdministratorFields,
CustomAssetFields,
CustomChannelFields,
CustomCollectionFields,
CustomCollectionFieldsTranslation,
Expand Down Expand Up @@ -214,6 +215,7 @@ function getDefault(customField: CustomFieldConfig, dbEngine: ConnectionOptions[
export function registerCustomEntityFields(config: VendureConfig) {
registerCustomFieldsForEntity(config, 'Address', CustomAddressFields);
registerCustomFieldsForEntity(config, 'Administrator', CustomAdministratorFields);
registerCustomFieldsForEntity(config, 'Asset', CustomAssetFields);
registerCustomFieldsForEntity(config, 'Collection', CustomCollectionFields);
registerCustomFieldsForEntity(config, 'Collection', CustomCollectionFieldsTranslation, true);
registerCustomFieldsForEntity(config, 'Customer', CustomCustomerFields);
Expand Down
6 changes: 1 addition & 5 deletions packages/dev-server/dev-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ export const devConfig: VendureConfig = {
paymentMethodHandlers: [dummyPaymentHandler],
},
customFields: {
/*Administrator: [
{ name: 'profileLink', type: 'string' },
{ name: 'avatar', type: 'relation', entity: Asset },
],
Channel: [{ name: 'description', type: 'string' }],*/
/*Asset: [{ name: 'description', type: 'string' }],*/
},
logger: new DefaultLogger({ level: LogLevel.Info }),
importExportOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ export type Asset = Node & {
source: Scalars['String'];
preview: Scalars['String'];
focalPoint?: Maybe<Coordinate>;
customFields?: Maybe<Scalars['JSON']>;
};

export type MimeTypeError = ErrorResult & {
Expand All @@ -955,6 +956,7 @@ export type AssetListOptions = {
export type CreateAssetInput = {
file: Scalars['Upload'];
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type CoordinateInput = {
Expand All @@ -979,6 +981,7 @@ export type UpdateAssetInput = {
name?: Maybe<Scalars['String']>;
focalPoint?: Maybe<CoordinateInput>;
tags?: Maybe<Array<Scalars['String']>>;
customFields?: Maybe<Scalars['JSON']>;
};

export type AssignAssetsToChannelInput = {
Expand Down Expand Up @@ -4513,6 +4516,7 @@ export type NativeAuthInput = {
export type CustomFields = {
Address: Array<CustomFieldConfig>;
Administrator: Array<CustomFieldConfig>;
Asset: Array<CustomFieldConfig>;
Channel: Array<CustomFieldConfig>;
Collection: Array<CustomFieldConfig>;
Customer: Array<CustomFieldConfig>;
Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 60b6171

Please sign in to comment.