generated from vtex-apps/service-example
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { InstanceOptions, IOContext } from '@vtex/api' | ||
import type { PaginationArgs, WithMetadata } from '@vtex/clients' | ||
import { MasterDataEntity } from '@vtex/clients' | ||
|
||
type ScrollInput<K> = { | ||
fields: Array<ThisType<K> | '_all'> | ||
sort?: string | ||
size?: number | ||
mdToken?: string | ||
} | ||
|
||
export const withCustomSchema = <TEntity extends Record<string, unknown>>( | ||
schema: string, | ||
_MasterdataClient: new ( | ||
context: IOContext, | ||
options?: InstanceOptions | ||
) => MasterDataEntity<TEntity> | ||
): new ( | ||
context: IOContext, | ||
options?: InstanceOptions | ||
) => MasterDataEntity<TEntity> => { | ||
return class extends MasterDataEntity<TEntity> { | ||
public dataEntity: string | ||
public schema: string | ||
public client: MasterDataEntity<TEntity> | ||
|
||
constructor(ctx: IOContext, options?: InstanceOptions) { | ||
super(ctx, options) | ||
this.client = new _MasterdataClient(ctx, options) | ||
this.dataEntity = this.client.dataEntity | ||
this.schema = schema | ||
this.client.schema = schema | ||
} | ||
|
||
public save(entity: TEntity) { | ||
return this.client.save(entity) | ||
} | ||
|
||
public update(id: string, fields: Partial<TEntity>) { | ||
return this.client.update(id, fields) | ||
} | ||
|
||
public saveOrUpdate(fields: TEntity & { id: string }) { | ||
return this.client.saveOrUpdate(fields) | ||
} | ||
|
||
public delete(id: string) { | ||
return this.client.delete(id) | ||
} | ||
|
||
// These es-lint disables are needed because I have to extend the class and its methods surpass the max params. | ||
// eslint-disable-next-line max-params | ||
public search<K extends keyof WithMetadata<TEntity>>( | ||
pagination: PaginationArgs, | ||
fields: Array<ThisType<K> | '_all'>, | ||
sort?: string, | ||
where?: string | ||
): Promise<Array<Pick<WithMetadata<TEntity>, K>>> { | ||
return this.client.search(pagination, fields, sort, where) | ||
} | ||
|
||
// eslint-disable-next-line max-params | ||
public searchRaw<K extends keyof WithMetadata<TEntity>>( | ||
pagination: PaginationArgs, | ||
fields: Array<ThisType<K> | '_all'>, | ||
sort?: string, | ||
where?: string | ||
): Promise<{ | ||
data: Array<Pick<WithMetadata<TEntity>, K>> | ||
pagination: { total: number; page: number; pageSize: number } | ||
}> { | ||
return this.client.searchRaw(pagination, fields, sort, where) | ||
} | ||
|
||
public get<K extends keyof WithMetadata<TEntity>>( | ||
id: string, | ||
fields: Array<ThisType<K> | '_all'> | ||
) { | ||
return this.client.get(id, fields) | ||
} | ||
|
||
public async scroll<K extends keyof WithMetadata<TEntity>>( | ||
input: ScrollInput<K> | ||
) { | ||
return this.client.scroll(input) | ||
} | ||
} | ||
} |