Skip to content
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

add options to load() to resolve recursiveDelete() issue #12

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SolidLogic {

async findAclDocUrl (url: string) {
const doc = this.store.sym(url)
await this.load(doc)
await this.load(doc, { force: true })
const docNode = this.store.any(doc, ACL_LINK)
if (!docNode) {
throw new Error(`No ACL link discovered for ${url}`)
Expand Down Expand Up @@ -147,11 +147,18 @@ export class SolidLogic {
})
}

load (doc: NamedNode | NamedNode[] | string) {
load (doc: NamedNode | NamedNode[] | string, options?: any) {
if (!this.store.fetcher) {
throw new Error('Cannot load doc(s), have no fetcher')
}
return this.store.fetcher.load(doc)
// force = true to reload the doc
// but first remove doc: this is to avoid just adding quads
if (options && options.force) {
let removeDoc: any[] = [doc].flat()
if (typeof doc === 'string') removeDoc = [this.store.sym(doc as string)]
removeDoc.forEach(item => this.store.removeDocument(item as NamedNode))
}
return this.store.fetcher.load(doc, options)
}

async loadIndexes (
Expand Down Expand Up @@ -232,15 +239,13 @@ export class SolidLogic {
}

async getContainerMembers(containerUrl) {
await this.load(this.store.sym(containerUrl));
await this.load(this.store.sym(containerUrl), { force: true })
return this.store.statementsMatching(this.store.sym(containerUrl), this.store.sym('http://www.w3.org/ns/ldp#contains')).map((st: Statement) => st.object.value);
}

async recursiveDelete (url: string) {
try {
if (this.isContainer(url)) {
const aclDocUrl = await this.findAclDocUrl(url);
await this.fetcher.fetch(aclDocUrl, { method: 'DELETE' });
const containerMembers = await this.getContainerMembers(url);
await Promise.all(containerMembers.map(url => this.recursiveDelete(url)));
}
Expand Down