Skip to content

Commit

Permalink
fix(client): pass TGLink to .put()
Browse files Browse the repository at this point in the history
  • Loading branch information
ivkan committed Aug 19, 2023
1 parent 7a5a9bb commit 272849c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/client/graph/graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,45 @@ export function getPathData(
};
}

const isReferenceLink = (data: TGValue) => Object.keys(data).length === 1 && typeof data['#'] === 'string';

export function flattenGraphData(data: TGValue, fullPath: string[]): {
graphData: TGGraphData,
soul: string
}
{
if (isObject(data))
{
if (isReferenceLink(data))
{
const propertyName = fullPath.pop();
const soul = fullPath.join('/').trim();

return {
graphData: {
[soul]: {
[propertyName]: data
}
} as TGGraphData,
soul
}
}
else
{
const soul = fullPath.join('/');
return {
graphData: flattenGraphByPath(data, [soul]),
soul
};
}
/*console.log({
data, fullPath
});
const soul = fullPath.join('/');
return {
graphData: flattenGraphByPath(data, [soul]),
soul
};
};*/
}
else if (fullPath.length === 1 && data === null)
{
Expand Down
12 changes: 10 additions & 2 deletions src/client/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class TGLink
* You do not need to re-save the entire object every time, TopGun will automatically
* merge your data into what already exists as a "partial" update.
**/
put(value: TGValue, opt?: TGOptionsPut): Promise<TGMessage>
put(value: TGValue|TGLink, opt?: TGOptionsPut): Promise<TGMessage>
{
return new Promise<TGMessage>((resolve) =>
{
Expand All @@ -176,12 +176,20 @@ export class TGLink
'You cannot save data to user space if the user is not authorized.',
);
}
if (!this._parent && (!isObject(value) && value !== null))
else if (!this._parent && (!isObject(value) && value !== null))
{
throw new Error(
'Data at root of graph must be a node (an object) or null.',
);
}
else if (value instanceof TGLink)
{
if (value.getPath().length > 2)
{
throw new Error('Data at root of graph must be a node (an object) or null.');
}
value = { '#': value.getPath().join('/') };
}

this._client.graph.putPath(
this.getPath(),
Expand Down

0 comments on commit 272849c

Please sign in to comment.