Skip to content

Commit

Permalink
Fix url minting for contains relations
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Jul 7, 2024
1 parent 2714a33 commit d508566
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
46 changes: 23 additions & 23 deletions src/models/SolidModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,29 @@ export class SolidModel extends SolidModelBase {
.map(([resourceId]) => baseUrl ? urlResolve(baseUrl, resourceId) : resourceId);
}

public static async withCollection<Result>(
collection: string | undefined | (() => Result | Promise<Result>) = '',
operation?: () => Result | Promise<Result>,
): Promise<Result> {
const oldCollection = this.collection;

if (typeof collection !== 'string') {
operation = collection;
collection = '';
}

if (!operation)
throw new SoukaiError('Invalid method given to withCollection (SolidModel internals)');

this.collection = collection || oldCollection;

const result = await operation();

this.collection = oldCollection;

return result;
}

protected static bootRdfContexts(
initialClass?: typeof SolidModel,
initialRdfContexts: Record<string, string> = {},
Expand Down Expand Up @@ -577,29 +600,6 @@ export class SolidModel extends SolidModelBase {
return this.rdfContexts.default ?? '';
}

protected static async withCollection<Result>(
collection: string | undefined | (() => Result | Promise<Result>) = '',
operation?: () => Result | Promise<Result>,
): Promise<Result> {
const oldCollection = this.collection;

if (typeof collection !== 'string') {
operation = collection;
collection = '';
}

if (!operation)
throw new SoukaiError('Invalid method given to withCollection (SolidModel internals)');

this.collection = collection || oldCollection;

const result = await operation();

this.collection = oldCollection;

return result;
}

// TODO this should be optional
public url!: string;

Expand Down
16 changes: 12 additions & 4 deletions src/models/relations/SolidContainsRelation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ export default class SolidContainsRelation<
}

public async create(attributes: Attributes = {}): Promise<Related> {
this.assertParentExists();

const related = this.relatedClass.newInstance(attributes);

if (!related.url && related.static('mintsUrls')) {
related.mintUrl();
this.relatedClass.withCollection(this.parent.url, () => related.mintUrl());
}

return tap(this.attach(related), model => this.save(model));
}

public async save(model: Related): Promise<Related> {
if (!this.parent.exists()) {
throw new SoukaiError('Cannot save a model because the container doesn\'t exist');
}
this.assertParentExists();

return tap(model, async () => {
await model.save(this.parent.url);
Expand All @@ -71,4 +71,12 @@ export default class SolidContainsRelation<
});
}

protected assertParentExists(): void {
if (this.parent.exists()) {
return;
}

throw new SoukaiError('Cannot save a model because the container doesn\'t exist');
}

}

0 comments on commit d508566

Please sign in to comment.