-
Notifications
You must be signed in to change notification settings - Fork 5
allow to provide an existing id when creating an entity #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@graphprotocol/grc-20": minor | ||
| --- | ||
|
|
||
| allow to provide an existing id when creating an entity, image, type or property |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { type Id, assertValid } from '../id.js'; | ||
| import { Image } from '../image.js'; | ||
| import { uploadImage } from '../ipfs.js'; | ||
| import type { CreateResult, Op } from '../types.js'; | ||
|
|
@@ -7,11 +8,13 @@ type CreateImageParams = | |
| blob: Blob; | ||
| name?: string; | ||
| description?: string; | ||
| id?: Id; | ||
| } | ||
| | { | ||
| url: string; | ||
| name?: string; | ||
| description?: string; | ||
| id?: Id; | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -25,22 +28,27 @@ type CreateImageParams = | |
| * url: 'https://example.com/image.png', | ||
| * name: 'name of the image', // optional | ||
| * description: 'description of the image', // optional | ||
| * id: imageId, // optional and will be generated if not provided | ||
| * }); | ||
| * | ||
| * const { id, ops } = createImage({ | ||
| * blob: new Blob(…), | ||
| * name: 'name of the image', // optional | ||
| * description: 'description of the image', // optional | ||
| * id: imageId, // optional and will be generated if not provided | ||
| * }); | ||
| * ``` | ||
| * @param params – {@link CreateImageParams} | ||
| * @returns – {@link CreateResult} | ||
| * @throws Will throw an IpfsUploadError if the image cannot be uploaded to IPFS | ||
| */ | ||
| export const createImage = async ({ name, description, ...params }: CreateImageParams): Promise<CreateResult> => { | ||
| export const createImage = async ({ name, description, id: providedId, ...params }: CreateImageParams): Promise<CreateResult> => { | ||
| if (providedId) { | ||
| assertValid(providedId, '`id` in `createImage`'); | ||
| } | ||
| const ops: Array<Op> = []; | ||
| const { cid, dimensions } = await uploadImage(params); | ||
| const { id, ops: imageOps } = Image.make({ cid, dimensions }); | ||
| const { id, ops: imageOps } = Image.make({ cid, dimensions, id: providedId }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question |
||
| ops.push(...imageOps); | ||
| ops.push(...createDefaultProperties({ entityId: id, name, description })); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,15 +32,19 @@ type CreatePropertyParams = DefaultProperties & | |
| * type: 'TEXT' | ||
| * description: 'description of the property', | ||
| * cover: imageEntityId, | ||
| * id: propertyId, // optional and will be generated if not provided | ||
| * }); | ||
| * ``` | ||
| * @param params – {@link CreatePropertyParams} | ||
| * @returns – {@link CreateResult} | ||
| * @throws Will throw an error if any provided ID is invalid | ||
| */ | ||
| export const createProperty = (params: CreatePropertyParams): CreateResult => { | ||
| const { name, description, cover } = params; | ||
| const entityId = generate(); | ||
| const { id, name, description, cover } = params; | ||
| if (id) { | ||
| assertValid(id, '`id` in `createProperty`'); | ||
| } | ||
| const entityId = id ?? generate(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question |
||
| const ops: Op[] = []; | ||
|
|
||
| ops.push(...createDefaultProperties({ entityId, name, description, cover })); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,19 @@ type CreateTypeParams = DefaultProperties & { | |
| * name: 'name of the type', | ||
| * description: 'description of the type', | ||
| * cover: imageEntityId, | ||
| * id: typeId, // optional and will be generated if not provided | ||
| * properties: [propertyEntityId1, propertyEntityId2], | ||
| * }); | ||
| * ``` | ||
| * @param params – {@link CreateTypeParams} | ||
| * @returns – {@link CreateResult} | ||
| * @throws Will throw an error if any provided ID is invalid | ||
| */ | ||
| export const createType = ({ name, description, cover, properties }: CreateTypeParams): CreateResult => { | ||
| const id = generate(); | ||
| export const createType = ({ id: providedId, name, description, cover, properties }: CreateTypeParams): CreateResult => { | ||
| if (providedId) { | ||
| assertValid(providedId, '`id` in `createType`'); | ||
| } | ||
| const id = providedId ?? generate(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aaand same question |
||
| const ops: Op[] = []; | ||
|
|
||
| ops.push(...createDefaultProperties({ entityId: id, name, description, cover })); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.