Skip to content

Commit

Permalink
feat: introduce resolve methods and builder separation
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukanOO authored and YuukanOO committed Sep 3, 2020
1 parent bb74563 commit d419651
Show file tree
Hide file tree
Showing 28 changed files with 9,237 additions and 1,203 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
coverage
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line no-undef
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
};
30 changes: 29 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
name: CI/CD
on: push
jobs:
test:
name: Lint & Test
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm test
deploy:
name: Deploy on npm
runs-on: ubuntu-20.04
# needs: test in the future... when a test job will be defined
needs: test
if: github.ref == 'refs/heads/master' # Deploy only on the master branch
steps:
- name: Checkout
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
coverage
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,127 @@
# @solideal/storage

Making access to Solid Pod's data a breeze! (or at least, trying to 😁)

## Motivation

There's already a lot of librairies out there to read and write Linked Data quads but for developers coming from a relatively mainstream background, getting up & running and develop for Solid is really a huge pain.

This tiny library abstracts the [inrupt/solid-client-js](https://github.com/inrupt/solid-client-js) and provides a simpler alternative to create, update, delete and find stuff on a Pod by taking care of the boring and repetitive stuff.

## Installation

```console
$ npm install @solideal/storage
```

## Usage

Let's say you are a new developer trying to get its head around that Linked Data stuff. You now understand that in order to persist stuff to a Pod, you need to define **what** you are persisting and **which** vocabulary to use for each of your properties.

Now, a simple example. You want to persist user generated bookmarks to its pod and you already have a javascript structure to hold that data.

```ts
const aBookmark = {
id: "some-unique-identifier",
title: "My super duber local bookmark",
url: "http://localhost:3000",
};
```

By navigating on Internet, you found that the vocabulary https://www.w3.org/2002/01/bookmark#Bookmark makes sense and that you can describe the title with http://purl.org/dc/elements/1.1/title and the url with https://www.w3.org/2002/01/bookmark#recalls.

Everything looks good, let's try to save our `aBookmark` to a Pod with `@solideal/storage`.

```ts
import { Repository, is } from "@solideal/storage";

const repository = new Repository<typeof aBookmark>({
source: "https://yuukanoo.solid.community/public/bookmarks.ttl", // where to store and read data
type: "https://www.w3.org/2002/01/bookmark#Bookmark",
schema: {
id: is.key(), // This one is mandatory and will contains the resource location
title: is.string("http://purl.org/dc/elements/1.1/title"),
url: is.url("https://www.w3.org/2002/01/bookmark#recalls"),
},
});

await repository.save(aBookmark); // Easy right?

// Let's find every bookmarks
const bookmarks = await repository.find();

/**
* Will returns every bookmarks in https://yuukanoo.solid.community/public/bookmarks.ttl
* [
* {
* id: "https://yuukanoo.solid.community/public/bookmarks.ttl#anid",
* title: "My super duber local bookmark",
* url: "http://localhost:3000",
* },
* {
* id: "https://yuukanoo.solid.community/public/bookmarks.ttl#anotherone",
* title: "Another bookmark",
* url: "http://localhost:5000",
* },
* ]
*/

// And delete the previously added one
await repository.remove(aBookmark);
```

## Determining the source of a repository

⚠ For now, the Repository only works with **Resources**, not **Containers**, but this is already planned 😎

Commonly you don't know where a kind of data exactly is stored on a user pod. Lucky for us, Solid has types index which makes it easy to resolve a location. For this usage, `@solideal/storage` provides helper functions which returns the first available location of a specific type.

```ts
import {
Repository,
is,
configure,
resolveTypeLocation,
resolveOrCreateTypeLocation,
} from "@solideal/storage";

// Let's configure the global webid to use as a default if not provided
// everywhere else, you can also pass the `fetch` used by every network calls here.
configure({ webid: "https://yuukanoo.solid.community/profile/card#me" });

// Let's resolve the type location from a user webid by looking at public/private
// type indexes.
const source = await resolveTypeLocation(
"https://www.w3.org/2002/01/bookmark#Bookmark"
);

// Sometimes you also want to create the resource and register the type if
// it does not exists yet. For this purpose, you can use the function below
const createIfNotFoundSource = await resolveOrCreateTypeLocation(
"https://www.w3.org/2002/01/bookmark#Bookmark",
{
path: "/public/my-bookmarks.ttl",
index: "public", // or private, but you need write access to the index
}
);

const repository = new Repository<typeof aBookmark>({
source,
type: "https://www.w3.org/2002/01/bookmark#Bookmark",
schema: {
id: is.key(),
title: is.string("http://purl.org/dc/elements/1.1/title"),
url: is.url("https://www.w3.org/2002/01/bookmark#recalls"),
},
});

// To make things easier, you can also use the following shortcut
const repo = await Repository.resolve<typeof aBookmark>({
type: "https://www.w3.org/2002/01/bookmark#Bookmark",
schema: {
id: is.key(),
title: is.string("http://purl.org/dc/elements/1.1/title"),
url: is.url("https://www.w3.org/2002/01/bookmark#recalls"),
},
});
```
23 changes: 0 additions & 23 deletions example/index.ts

This file was deleted.

Loading

0 comments on commit d419651

Please sign in to comment.