Skip to content

Commit

Permalink
feat: store package
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Sep 14, 2023
1 parent 64f2d76 commit 840f859
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-lemons-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/resource-store": minor
---

Package introduced as copy of `@hydrofoil/knossos/lib/store.js`
21 changes: 21 additions & 0 deletions packages/store/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 hypermedia.app

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
71 changes: 71 additions & 0 deletions packages/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @packageDocumentation
* @module @hydrofoil/resource-store
*/

import { DatasetCore, NamedNode, Term } from 'rdf-js'
import type { GraphPointer } from 'clownface'
import { StreamClient } from 'sparql-http-client/StreamClient'
import { ASK, CONSTRUCT, INSERT } from '@tpluscode/sparql-builder'
import $rdf from '@zazuko/env'
import { sparql } from '@tpluscode/rdf-string'

/**
* Provides functions to work with individual resources.
*
* Implementors should take care to retrieve and manipulate only resource's "own" triples,
* that is, avoid retrieving inferred statements or statements from other resources' representations.
*/
export interface ResourceStore {
exists(term: Term): Promise<boolean>

load(term: Term): Promise<GraphPointer<NamedNode, DatasetCore>>

save(resource: GraphPointer<NamedNode>): Promise<void>

delete(term: Term): Promise<void>
}

function assertNamedNode(term: Term): asserts term is NamedNode {
if (term.termType !== 'NamedNode') {
throw new Error('Term must be a named node')
}
}

/**
* Default implementation of {@see ResourceStore}, which keeps each resource
* is its own named graph.
*/
export class ResourcePerGraphStore implements ResourceStore {
constructor(private client: StreamClient) {
}

exists(term: Term): Promise<boolean> {
assertNamedNode(term)

return ASK`?s ?p ?o`.FROM(term).execute(this.client.query)
}

async load(term: Term): Promise<GraphPointer<NamedNode>> {
assertNamedNode(term)

const dataset = await $rdf.dataset().import(await CONSTRUCT`?s ?p ?o`
.FROM(term)
.WHERE`?s ?p ?o`
.execute(this.client.query))

return $rdf.clownface({ dataset, term })
}

async save(resource: GraphPointer<NamedNode>): Promise<void> {
const insert = INSERT.DATA`GRAPH ${resource.term} { ${resource.dataset} }`
const query = sparql`DROP SILENT GRAPH ${resource.term}; ${insert}`

return this.client.query.update(query.toString())
}

async delete(term: Term): Promise<void> {
const query = sparql`DROP SILENT GRAPH ${term}`
return this.client.query.update(query.toString())
}
}
20 changes: 20 additions & 0 deletions packages/store/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@hydrofoil/resource-store",
"version": "0.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"prepare": "tsc"
},
"repository": {
"type": "git",
"url": "https://github.com/hypermedia-app/creta",
"directory": "packages/store"
},
"dependencies": {
"@tpluscode/rdf-string": "^1.0.3",
"@tpluscode/sparql-builder": "^1.1.0",
"@zazuko/env": "^1.3.1",
"sparql-http-client": "^2.4.2"
}
}
2 changes: 1 addition & 1 deletion typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"entryPoints": [
"packages/knossos/lib/resource.ts",
"packages/knossos/lib/settings.ts",
"packages/knossos/lib/store.ts",
"packages/store/index.ts",
"packages/knossos/server.ts",
"packages/knossos/index.ts",
"packages/knossos/shacl.ts",
Expand Down
Loading

0 comments on commit 840f859

Please sign in to comment.