Skip to content

Commit 816a669

Browse files
fixed modify document functions in WOQL (#111)
1 parent 40fff27 commit 816a669

File tree

6 files changed

+73
-25
lines changed

6 files changed

+73
-25
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
- [literal](api/woql.js?id=literal)
149149
- [iri](api/woql.js?id=iri)
150150
- [vars](api/woql.js?id=vars)
151+
- [doc](api/woql.js?id=doc)
151152
- [client](api/woql.js?id=client)
152153
- [emerge](api/woql.js?id=emerge)
153154
- [update_triple](api/woql.js?id=update_triple)

docs/api/woql.js.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,24 @@ Use [read_document](#read_document) instead.
100100

101101

102102
## read_document
103-
##### WOQL.read\_document(IRI, output, formatObj) ⇒ <code>object</code>
103+
##### WOQL.read\_document(IRI, output) ⇒ <code>object</code>
104104
Read a node identified by an IRI as a JSON-LD document
105105

106106
**Returns**: <code>object</code> - WOQLQuery
107107

108108
| Param | Type | Description |
109109
| --- | --- | --- |
110-
| IRI | <code>string</code> | The document id or a variable |
111-
| output | <code>string</code> | variable name |
112-
| formatObj | <code>object</code> | |
110+
| IRI | <code>string</code> | The document id or a variable to read |
111+
| output | <code>string</code> | Variable which will be bound to the document. |
113112

113+
**Example**
114+
```js
115+
const query = WOQL.read_document(
116+
"Person/0b4feda109d9d13c9da809090b342ad9e4d8185545ce05f7cd20b97fe458f547",
117+
"v:Person"
118+
);
119+
const res = await client.query(query);
120+
```
114121

115122
## insert_document
116123
##### WOQL.insert\_document(docjson, [IRI]) ⇒ <code>object</code>
@@ -123,6 +130,12 @@ Insert a document in the graph.
123130
| docjson | <code>object</code> | The document to insert. Must either have an '@id' or have a class specified key. |
124131
| [IRI] | <code>string</code> | An optional identifier specifying the document location. |
125132

133+
**Example**
134+
```js
135+
const res = await client.query(
136+
WOQL.insert_document(WOQL.doc({ "@type" : "Person", "label": "John" }))
137+
)
138+
```
126139

127140
## update_document
128141
##### WOQL.update\_document(docjson, [IRI]) ⇒ <code>object</code>
@@ -1367,6 +1380,22 @@ const [a, b, c] = WOQL.vars("a", "b", "c")
13671380
//a, b, c are javascript variables which can be used as WOQL variables in subsequent queries
13681381
```
13691382

1383+
## doc
1384+
##### WOQL.doc(object) ⇒ <code>object</code>
1385+
Produces an encoded form of a document that can be used by a WOQL operation
1386+
such as `WOQL.insert_document`.
1387+
1388+
**Returns**: <code>object</code> - The encoded document
1389+
1390+
| Param | Type | Description |
1391+
| --- | --- | --- |
1392+
| object | <code>object</code> | Document to encode |
1393+
1394+
**Example**
1395+
```js
1396+
const doc = WOQL.doc({ "@type": "Person", name: "Newperson" })
1397+
```
1398+
13701399
## client
13711400
##### WOQL.client(client) ⇒ <code>WOQLClient</code>
13721401
Gets/Sets woqlClient

lib/query/woqlCore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function WOQLQuery(query) {
3737
'DeleteTriple',
3838
'AddQuad',
3939
'DeleteQuad',
40+
'InsertDocument',
4041
'DeleteDocument',
4142
'UpdateDocument',
4243
];

lib/query/woqlDoc.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-restricted-syntax */
21
// eslint-disable-next-line consistent-return
32
function convert(obj) {
43
if (obj == null) {
@@ -29,6 +28,7 @@ function convert(obj) {
2928
};
3029
} if (typeof (obj) === 'object') {
3130
const pairs = [];
31+
// eslint-disable-next-line no-restricted-syntax
3232
for (const [key, value] of Object.entries(obj)) {
3333
pairs.push({
3434
'@type': 'FieldValuePair',
@@ -37,8 +37,11 @@ function convert(obj) {
3737
});
3838
}
3939
return {
40-
'@type': 'DictionaryTemplate',
41-
data: pairs,
40+
'@type': 'Value',
41+
dictionary: {
42+
'@type': 'DictionaryTemplate',
43+
data: pairs,
44+
},
4245
};
4346
}
4447
}

lib/query/woqlQuery.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,20 @@ const WOQLQuery = require('./woqlCore');
2020
}
2121
} */
2222

23-
WOQLQuery.prototype.read_document = function (IRI, OutputVar, Format) {
23+
WOQLQuery.prototype.read_document = function (IRI, OutputVar) {
2424
if (this.cursor['@type']) this.wrapCursorWithAnd();
2525
this.cursor['@type'] = 'ReadDocument';
2626
this.cursor.identifier = this.cleanNodeValue(IRI);
2727
this.cursor.document = this.expandValueVariable(OutputVar);
28-
return this.wform(Format);
28+
return this;
2929
};
3030

3131
WOQLQuery.prototype.insert_document = function (docjson, IRI) {
3232
if (this.cursor['@type']) this.wrapCursorWithAnd();
3333
this.cursor['@type'] = 'InsertDocument';
3434
if (typeof IRI !== 'undefined') this.cursor.identifier = this.cleanNodeValue(IRI);
3535

36-
if (typeof docjson === 'string') {
37-
this.cursor.document = this.expandValueVariable(docjson);
38-
} else {
39-
this.cursor.document = docjson;
40-
}
36+
this.cursor.document = this.cleanObject(docjson);
4137

4238
return this.updated();
4339
};
@@ -47,11 +43,7 @@ WOQLQuery.prototype.update_document = function (docjson, IRI) {
4743
this.cursor['@type'] = 'UpdateDocument';
4844
if (typeof IRI !== 'undefined') this.cursor.identifier = this.cleanNodeValue(IRI);
4945

50-
if (typeof docjson === 'string') {
51-
this.cursor.document = this.expandValueVariable(docjson);
52-
} else {
53-
this.cursor.document = docjson;
54-
}
46+
this.cursor.document = this.cleanObject(docjson);
5547

5648
return this.updated();
5749
};

lib/woql.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// /@ts-check
44
// I HAVE TO REVIEW THE Inheritance and the prototype chain
55
const WOQLQuery = require('./query/woqlBuilder');
6-
const { Var } = require('./query/woqlDoc');
6+
const { Var, Doc } = require('./query/woqlDoc');
77
/**
88
* @license Apache Version 2
99
* @module WOQL
@@ -102,14 +102,19 @@ WOQL.read_object = function (IRI, output, formatObj) {
102102

103103
/**
104104
* Read a node identified by an IRI as a JSON-LD document
105-
* @param {string} IRI - The document id or a variable
106-
* @param {string} output - variable name
107-
* @param {object} formatObj
105+
* @param {string} IRI - The document id or a variable to read
106+
* @param {string} output - Variable which will be bound to the document.
108107
* @return {object} WOQLQuery
108+
* @example
109+
* const query = WOQL.read_document(
110+
* "Person/0b4feda109d9d13c9da809090b342ad9e4d8185545ce05f7cd20b97fe458f547",
111+
* "v:Person"
112+
* );
113+
* const res = await client.query(query);
109114
*/
110115

111-
WOQL.read_document = function (IRI, output, formatObj) {
112-
return new WOQLQuery().read_document(IRI, output, formatObj);
116+
WOQL.read_document = function (IRI, output) {
117+
return new WOQLQuery().read_document(IRI, output);
113118
};
114119

115120
/**
@@ -118,6 +123,10 @@ WOQL.read_document = function (IRI, output, formatObj) {
118123
* have a class specified key.
119124
* @param {string} [IRI] - An optional identifier specifying the document location.
120125
* @return {object} WOQLQuery
126+
* @example
127+
* const res = await client.query(
128+
* WOQL.insert_document(WOQL.doc({ "@type" : "Person", "label": "John" }))
129+
* )
121130
*/
122131

123132
WOQL.insert_document = function (docjson, IRI) {
@@ -1264,6 +1273,19 @@ WOQL.vars = function (...varNames) {
12641273
return varNames.map((item) => new Var(item));
12651274
};
12661275

1276+
/**
1277+
* Produces an encoded form of a document that can be used by a WOQL operation
1278+
* such as `WOQL.insert_document`.
1279+
* @param {object} object - Document to encode
1280+
* @returns {object} The encoded document
1281+
* @example
1282+
* const doc = WOQL.doc({ "@type": "Person", name: "Newperson" })
1283+
*/
1284+
1285+
WOQL.doc = function (object) {
1286+
return new Doc(object);
1287+
};
1288+
12671289
/**
12681290
* Gets/Sets woqlClient
12691291
* @param {WOQLClient}

0 commit comments

Comments
 (0)