Skip to content

Commit

Permalink
Merge branch 'main' of github.com:solid-contrib/data-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
soltanireza65 committed Dec 13, 2023
2 parents 794f9cb + 37b4e07 commit eab0bf3
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ Apart from this "vanilla" version, we will also package each data module for use
* [Soukai-Solid](https://github.com/NoelDeMartin/soukai-solid)
* [rdflib](https://github.com/linkeddata/rdflib.js)
* [LDO](https://github.com/o-development/ldo)

Create a new module [Link to tutorial](./new-module.md)
111 changes: 111 additions & 0 deletions new-module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Ading a new dada-module to the repo:
the criteria is to match previus packages while adding a new package, e.g. they all need to have a `getAll` method.
- use one of the existing modules as an inspiration.
- Create a directory as the `module` name.
- Create a directory as the `lib/framework` name.
- Init a npm package.
- It should export a modules that has all CRUD operations inside, e.g. `get`, `getAll`, `create`, `delete`, `update`.

## new data-module tutorial (bookmarks with inrupt):
```bash
git clone git@github.com:solid-contrib/data-modules.git
cd data-modules
mkdir bookmarks/inrupt
cd bookmarks/inrupt
npm init -y
npm i typescript @types/node -D
npm i @inrupt/solid-client solid-typeindex-support
npx tsc --init
mkdir src/
mkdir src/
touch src/index.ts
```

tsconfig content:
```json
{
"compilerOptions": {
"outDir": "./dist",
"module": "ESNext",
"target": "es6",
"allowJs": true,
"declaration": true,
"moduleResolution": "Node",
"esModuleInterop": true,
"strict": true,
"rootDir": "./",
"baseUrl": "src",
"stripInternal": true,

},
"types": ["node"],
"include": ["src", "test", "__test__"],
"exclude": ["node_modules", "**/test/*"]
}
```

```typescript
import { TypeIndexHelper } from "solid-typeindex-support";

export class Bookmark {
private static async getIndexUrls(session): Promise<string[]> {
const registeries = await TypeIndexHelper.getFromTypeIndex(session.info.webId!, session.fetch, true)

if (!!registeries?.length) {
return registeries
} else {
const pods = (await getPodUrlAll(session.info.webId!, { fetch: session.fetch }))[0];

const baseURL = pods ? pods : session.info.webId?.split("/profile")[0]

const defaultIndexUrl = `${baseURL}/bookmarks/index.ttl`;

const defaultIndexDataset = await getSolidDataset(defaultIndexUrl, { fetch: session.fetch });

if (!defaultIndexDataset) {
await saveSolidDatasetAt(defaultIndexUrl, createSolidDataset(), { fetch: session.fetch });
}

await TypeIndexHelper.registerInTypeIndex(session.info.webId!, session.fetch, defaultIndexUrl, true)

return [defaultIndexUrl];
}
}

public static async create(payload: ICreateBookmark, session: Session): Promise<boolean> {

const { title, link, creator, topic } = payload

if (!isValidUrl(link)) throw new Error("link is not a valid URL")
if (creator && !isValidUrl(creator)) throw new Error("creator is not a valid URL")


const [indexUrl] = await this.getIndexUrls(session);

const ds = await getSolidDataset(indexUrl, { fetch: session.fetch });

let newBookmarkThing = createThing()

newBookmarkThing = addStringNoLocale(newBookmarkThing, DCTERMS.title, title)
newBookmarkThing = addNamedNode(newBookmarkThing, BOOKMARK.recalls, namedNode(link))

if (creator) newBookmarkThing = addNamedNode(newBookmarkThing, DCTERMS.creator, namedNode(creator))

if (topic && isValidUrl(topic)) newBookmarkThing = addNamedNode(newBookmarkThing, BOOKMARK.hasTopic, namedNode(topic))
if (topic && !isValidUrl(topic)) newBookmarkThing = addStringNoLocale(newBookmarkThing, BOOKMARK.hasTopic, topic)

newBookmarkThing = addStringNoLocale(newBookmarkThing, DCTERMS.created, new Date().toISOString())
newBookmarkThing = addStringNoLocale(newBookmarkThing, __DC_UPDATED, new Date().toISOString())

newBookmarkThing = addUrl(newBookmarkThing, RDF.type, BOOKMARK.Bookmark)

const updatedBookmarkList = setThing(ds, newBookmarkThing);
const updatedDataset = await saveSolidDatasetAt(indexUrl, updatedBookmarkList, { fetch: session.fetch });

return updatedDataset ? true : false
};

// The rest of methods
}
```

0 comments on commit eab0bf3

Please sign in to comment.