@ng-org/shex-orm 0.1.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +236 -0
- package/dist/ShexJTypes.d.ts +542 -0
- package/dist/ShexJTypes.d.ts.map +1 -0
- package/dist/ShexJTypes.js +10 -0
- package/dist/build.d.ts +8 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +72 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +15 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/schema-converter/__tests__/typingTransformer.test.d.ts +2 -0
- package/dist/schema-converter/__tests__/typingTransformer.test.d.ts.map +1 -0
- package/dist/schema-converter/__tests__/typingTransformer.test.js +76 -0
- package/dist/schema-converter/converter.d.ts +12 -0
- package/dist/schema-converter/converter.d.ts.map +1 -0
- package/dist/schema-converter/converter.js +79 -0
- package/dist/schema-converter/templates/schema.ejs +8 -0
- package/dist/schema-converter/templates/shapeTypes.ejs +14 -0
- package/dist/schema-converter/templates/typings.ejs +14 -0
- package/dist/schema-converter/transformers/ShexJSchemaTransformer.d.ts +348 -0
- package/dist/schema-converter/transformers/ShexJSchemaTransformer.d.ts.map +1 -0
- package/dist/schema-converter/transformers/ShexJSchemaTransformer.js +239 -0
- package/dist/schema-converter/transformers/ShexJTypingTransformer.d.ts +366 -0
- package/dist/schema-converter/transformers/ShexJTypingTransformer.d.ts.map +1 -0
- package/dist/schema-converter/transformers/ShexJTypingTransformer.js +623 -0
- package/dist/schema-converter/util/ShapeInterfaceDeclaration.d.ts +5 -0
- package/dist/schema-converter/util/ShapeInterfaceDeclaration.d.ts.map +1 -0
- package/dist/schema-converter/util/ShapeInterfaceDeclaration.js +1 -0
- package/dist/schema-converter/util/annotateReadablePredicates.d.ts +8 -0
- package/dist/schema-converter/util/annotateReadablePredicates.d.ts.map +1 -0
- package/dist/schema-converter/util/annotateReadablePredicates.js +148 -0
- package/dist/schema-converter/util/dedupeObjectTypeMembers.d.ts +3 -0
- package/dist/schema-converter/util/dedupeObjectTypeMembers.d.ts.map +1 -0
- package/dist/schema-converter/util/dedupeObjectTypeMembers.js +47 -0
- package/dist/schema-converter/util/getRdfTypesForTripleConstraint.d.ts +4 -0
- package/dist/schema-converter/util/getRdfTypesForTripleConstraint.d.ts.map +1 -0
- package/dist/schema-converter/util/getRdfTypesForTripleConstraint.js +98 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +10 -0
- package/dist/util/forAllShapes.d.ts +2 -0
- package/dist/util/forAllShapes.d.ts.map +1 -0
- package/dist/util/forAllShapes.js +25 -0
- package/package.json +61 -0
package/README.md
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# Schema Converter SHEX > TypeScript
|
|
2
|
+
|
|
3
|
+
CLI tool to convert SHEX shapes to schemas and TypeScript definitions ("shape types") that can be used for creating ORM objects.
|
|
4
|
+
|
|
5
|
+
## How to Use
|
|
6
|
+
|
|
7
|
+
Install `@ng-org/shex-orm` as dev dependency or globally (`--global`).
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --save-dev @ng-org/shex-orm
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Then run
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx rdf-orm build --input ./src/shapes/shex --output ./src/shapes/orm
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The input directory needs to contain shex files with one or more shape definitions each.
|
|
20
|
+
The output directory will contain the typescript files with type definitions and the converted schema.
|
|
21
|
+
|
|
22
|
+
You will then pass the shape type of a shape definition to the ng sdk:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { useShape } from "@ng-org/orm/react";
|
|
26
|
+
import { TestObjectShapeType } from "../shapes/orm/testShape.shapeTypes";
|
|
27
|
+
|
|
28
|
+
export function TestComponent() {
|
|
29
|
+
const testObjects = useShape(TestObjectShapeType);
|
|
30
|
+
...
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
For an example, see the [multi-framework-signals example application](../examples/multi-framework-signals/README.md).
|
|
35
|
+
|
|
36
|
+
## Generated Output
|
|
37
|
+
|
|
38
|
+
For each SHEX file, the tool creates three TypeScript files:
|
|
39
|
+
|
|
40
|
+
- A schema file like `person.schema.ts`
|
|
41
|
+
- A typings file like `person.typings.ts`
|
|
42
|
+
- A shape type file like `person.shapeTypes.ts` which contains a `ShapeType` that consists of the schema, the type, and the IRI of the main shape
|
|
43
|
+
|
|
44
|
+
The transformers for converting SHEX to schema and typings files are based on `@ldo/traverser-shexj`.
|
|
45
|
+
|
|
46
|
+
### ShapeType File
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
export const PersonShapeType: ShapeType<Person> = {
|
|
50
|
+
schema: personSchema,
|
|
51
|
+
shape: "http://example.org/PersonShape",
|
|
52
|
+
};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Schema File
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import type { Schema } from "@ng-org/shex-orm";
|
|
59
|
+
|
|
60
|
+
export const personSchema: Schema = {
|
|
61
|
+
"http://example.org/PersonShape": {
|
|
62
|
+
iri: "http://example.org/PersonShape",
|
|
63
|
+
predicates: [
|
|
64
|
+
{
|
|
65
|
+
dataTypes: [
|
|
66
|
+
{
|
|
67
|
+
valType: "literal",
|
|
68
|
+
literals: ["http://example.org/Person"],
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
maxCardinality: -1,
|
|
72
|
+
minCardinality: 1,
|
|
73
|
+
iri: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
|
|
74
|
+
readablePredicate: "@type",
|
|
75
|
+
// `extra` here allows additional type values along with `http://example.org/Person`.
|
|
76
|
+
extra: true,
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
dataTypes: [{ valType: "string" }],
|
|
80
|
+
maxCardinality: 1,
|
|
81
|
+
minCardinality: 1,
|
|
82
|
+
iri: "http://example.org/name",
|
|
83
|
+
readablePredicate: "name",
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
dataTypes: [{ valType: "string" }],
|
|
87
|
+
maxCardinality: -1,
|
|
88
|
+
minCardinality: 0,
|
|
89
|
+
iri: "http://example.org/email",
|
|
90
|
+
readablePredicate: "email",
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
dataTypes: [
|
|
94
|
+
{
|
|
95
|
+
valType: "shape",
|
|
96
|
+
shape: "http://example.org/PersonShape||http://example.org/address",
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
maxCardinality: 1,
|
|
100
|
+
minCardinality: 0,
|
|
101
|
+
iri: "http://example.org/address",
|
|
102
|
+
readablePredicate: "address",
|
|
103
|
+
// `extra` here enables that if multiple children are present but only one is valid, the shape is still considered valid.
|
|
104
|
+
extra: true,
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
"http://example.org/PersonShape||http://example.org/address": {
|
|
109
|
+
iri: "http://example.org/PersonShape||http://example.org/address",
|
|
110
|
+
predicates: [
|
|
111
|
+
{
|
|
112
|
+
dataTypes: [{ valType: "string" }],
|
|
113
|
+
maxCardinality: 1,
|
|
114
|
+
minCardinality: 1,
|
|
115
|
+
iri: "http://example.org/city",
|
|
116
|
+
readablePredicate: "city",
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Readable Predicate Names
|
|
124
|
+
|
|
125
|
+
The `readablePredicate` field is automatically generated from the predicate IRI and becomes the property name in the TypeScript interface.
|
|
126
|
+
|
|
127
|
+
**Generation Rules:**
|
|
128
|
+
|
|
129
|
+
1. **Special case**: `rdf:type` (`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`) always becomes `@type`
|
|
130
|
+
|
|
131
|
+
2. **No conflicts**: If the last segment of the IRI is unique within the shape, it's used as-is:
|
|
132
|
+
- `http://example.org/name` → `name`
|
|
133
|
+
- `http://schema.org/email` → `email`
|
|
134
|
+
|
|
135
|
+
3. **Conflict resolution**: When multiple predicates in the same shape share the same last segment (local name), **all** predicates in that collision group are renamed using prefixes:
|
|
136
|
+
- The algorithm walks backward through IRI segments (right to left)
|
|
137
|
+
- For each predicate, it tries `{prefix}_{localName}` combinations until finding an unused name
|
|
138
|
+
- Example: Both `http://foaf.org/name` and `http://schema.org/name` would become `foaf_name` and `schema_name`
|
|
139
|
+
|
|
140
|
+
4. **Fallback**: If prefix combinations are exhausted, a composite name is generated from all IRI segments (excluding protocol) with incrementing numbers for uniqueness:
|
|
141
|
+
- Pattern: `{composite}_{localName}` or `{composite}_{localName}_1`, `{composite}_{localName}_2`, etc.
|
|
142
|
+
|
|
143
|
+
**Character sanitization**: Special characters (except dots and dashes) are replaced with underscores to ensure valid JavaScript identifiers.
|
|
144
|
+
|
|
145
|
+
**Note**: You can **manually edit** the `readablePredicate` values in the generated schema files if you prefer different property names. The schema acts as the single source of truth for property naming.
|
|
146
|
+
|
|
147
|
+
### Typings File
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
export type IRI = string;
|
|
151
|
+
|
|
152
|
+
export interface Person {
|
|
153
|
+
readonly "@id": IRI;
|
|
154
|
+
readonly "@graph": IRI;
|
|
155
|
+
/**
|
|
156
|
+
* Original IRI: http://www.w3.org/1999/02/22-rdf-syntax-ns#type
|
|
157
|
+
*/
|
|
158
|
+
"@type": "http://example.org/Person";
|
|
159
|
+
/**
|
|
160
|
+
* Original IRI: http://example.org/name
|
|
161
|
+
*/
|
|
162
|
+
name: string;
|
|
163
|
+
/**
|
|
164
|
+
* Original IRI: http://example.org/email
|
|
165
|
+
*/
|
|
166
|
+
email?: Set<string>;
|
|
167
|
+
/**
|
|
168
|
+
* Original IRI: http://example.org/address
|
|
169
|
+
*/
|
|
170
|
+
address?: {
|
|
171
|
+
readonly "@id": IRI;
|
|
172
|
+
readonly "@graph": IRI;
|
|
173
|
+
/**
|
|
174
|
+
* Original IRI: http://example.org/city
|
|
175
|
+
*/
|
|
176
|
+
city: string;
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### Standard Properties
|
|
182
|
+
|
|
183
|
+
- **`@type`**: The RDF type IRI (from `rdf:type`) is always converted to the property name `@type` by default
|
|
184
|
+
- **`@id` and `@graph`**: These properties are automatically added to all typed objects as readonly properties
|
|
185
|
+
|
|
186
|
+
### Cardinality Handling
|
|
187
|
+
|
|
188
|
+
Predicates with a cardinality higher than 1 (i.e., `maxCardinality > 1` or `maxCardinality === -1` for unlimited) are represented as TypeScript `Set<T>` types.
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## NextGraph
|
|
193
|
+
|
|
194
|
+
> NextGraph brings about the convergence of P2P and Semantic Web technologies, towards a decentralized, secure and privacy-preserving cloud, based on CRDTs.
|
|
195
|
+
>
|
|
196
|
+
> This open source ecosystem provides solutions for end-users (a platform) and software developers (a framework), wishing to use or create **decentralized** apps featuring: **live collaboration** on rich-text documents, peer to peer communication with **end-to-end encryption**, offline-first, **local-first**, portable and interoperable data, total ownership of data and software, security and privacy. Centered on repositories containing **semantic data** (RDF), **rich text**, and structured data formats like **JSON**, synced between peers belonging to permissioned groups of users, it offers strong eventual consistency, thanks to the use of **CRDTs**. Documents can be linked together, signed, shared securely, queried using the **SPARQL** language and organized into sites and containers.
|
|
197
|
+
>
|
|
198
|
+
> More info here [https://nextgraph.org](https://nextgraph.org)
|
|
199
|
+
|
|
200
|
+
## Support
|
|
201
|
+
|
|
202
|
+
Documentation can be found here [https://docs.nextgraph.org](https://docs.nextgraph.org)
|
|
203
|
+
|
|
204
|
+
And our community forum where you can ask questions is here [https://forum.nextgraph.org](https://forum.nextgraph.org)
|
|
205
|
+
|
|
206
|
+
## License
|
|
207
|
+
|
|
208
|
+
3 files have been taken from LDO project and modified by us. 1 file has been taken from LDO project without modifications.
|
|
209
|
+
|
|
210
|
+
All from repository
|
|
211
|
+
https://github.com/o-development/ldo/tree/main/packages/schema-converter-shex
|
|
212
|
+
at commit c461beb5a5acf379d3069f0734dfa5d57fd20eaa (Aug 23, 2025) licensed under MIT License with copyright attribution to : Copyright (c) 2023 Jackson Morgan.
|
|
213
|
+
Those 4 files are here relicensed under Apache 2.0 and MIT.
|
|
214
|
+
|
|
215
|
+
All subsequent commits on those files, and any other file in this package are licensed under either of
|
|
216
|
+
|
|
217
|
+
- Apache License, Version 2.0 ([LICENSE-APACHE2](LICENSE-APACHE2) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
218
|
+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
219
|
+
at your option.
|
|
220
|
+
|
|
221
|
+
`SPDX-License-Identifier: Apache-2.0 OR MIT`
|
|
222
|
+
|
|
223
|
+
### Contributions license
|
|
224
|
+
|
|
225
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted
|
|
226
|
+
for inclusion in the work by you shall be dual licensed as below, without any
|
|
227
|
+
additional terms or conditions.
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
NextGraph received funding through the [NGI Assure Fund](https://nlnet.nl/assure) and the [NGI Zero Commons Fund](https://nlnet.nl/commonsfund/), both funds established by [NLnet](https://nlnet.nl/) Foundation with financial support from the European Commission's [Next Generation Internet](https://ngi.eu/) programme, under the aegis of DG Communications Networks, Content and Technology under grant agreements No 957073 and No 101092990, respectively.
|
|
232
|
+
|
|
233
|
+
[license-image]: https://img.shields.io/badge/license-Apache2.0-blue.svg
|
|
234
|
+
[license-link]: https://git.nextgraph.org/NextGraph/nextgraph-rs/raw/branch/master/LICENSE-APACHE2
|
|
235
|
+
[license-image2]: https://img.shields.io/badge/license-MIT-blue.svg
|
|
236
|
+
[license-link2]: https://git.nextgraph.org/NextGraph/nextgraph-rs/src/branch/master/LICENSE-MIT
|