@graphprotocol/hypergraph 0.3.0 → 0.4.0
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/dist/cli/Cli.d.ts +2 -0
- package/dist/cli/Cli.d.ts.map +1 -0
- package/dist/cli/Cli.js +8 -0
- package/dist/cli/Cli.js.map +1 -0
- package/dist/cli/Logger.d.ts +3 -0
- package/dist/cli/Logger.d.ts.map +1 -0
- package/dist/cli/Logger.js +20 -0
- package/dist/cli/Logger.js.map +1 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +20 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/bun.d.ts +3 -0
- package/dist/cli/bun.d.ts.map +1 -0
- package/dist/cli/bun.js +3 -0
- package/dist/cli/bun.js.map +1 -0
- package/dist/cli/services/Typesync.d.ts +21 -0
- package/dist/cli/services/Typesync.d.ts.map +1 -0
- package/dist/cli/services/Typesync.js +137 -0
- package/dist/cli/services/Typesync.js.map +1 -0
- package/dist/cli/services/Utils.d.ts +10 -0
- package/dist/cli/services/Utils.d.ts.map +1 -0
- package/dist/cli/services/Utils.js +154 -0
- package/dist/cli/services/Utils.js.map +1 -0
- package/dist/cli/subcommands/typesync.d.ts +7 -0
- package/dist/cli/subcommands/typesync.d.ts.map +1 -0
- package/dist/cli/subcommands/typesync.js +38 -0
- package/dist/cli/subcommands/typesync.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/mapping/Mapping.d.ts +43 -31
- package/dist/mapping/Mapping.d.ts.map +1 -1
- package/dist/mapping/Mapping.js +45 -23
- package/dist/mapping/Mapping.js.map +1 -1
- package/package.json +7 -3
- package/src/cli/Cli.ts +15 -0
- package/src/cli/Logger.ts +20 -0
- package/src/cli/bin.ts +33 -0
- package/src/cli/bun.ts +3 -0
- package/src/cli/services/Typesync.ts +189 -0
- package/src/cli/services/Utils.ts +187 -0
- package/src/cli/subcommands/typesync.ts +93 -0
- package/src/index.ts +1 -0
- package/src/mapping/Mapping.ts +61 -39
package/src/mapping/Mapping.ts
CHANGED
|
@@ -10,30 +10,30 @@ import { namesAreUnique, toCamelCase, toPascalCase } from './Utils.js';
|
|
|
10
10
|
*/
|
|
11
11
|
export type MappingEntry = {
|
|
12
12
|
/**
|
|
13
|
-
* Array of the `Id
|
|
13
|
+
* Array of the `Id` of the type in the Knowledge Graph.
|
|
14
14
|
* Is an array because a type can belong to multiple spaces/extend multiple types.
|
|
15
15
|
*
|
|
16
16
|
* @since 0.2.0
|
|
17
17
|
*/
|
|
18
|
-
typeIds: Array<Grc20Id
|
|
18
|
+
typeIds: Array<Grc20Id>;
|
|
19
19
|
/**
|
|
20
|
-
* Record of property names to the `Id
|
|
20
|
+
* Record of property names to the `Id` of the type in the Knowledge Graph
|
|
21
21
|
*
|
|
22
22
|
* @since 0.2.0
|
|
23
23
|
*/
|
|
24
24
|
properties?:
|
|
25
25
|
| {
|
|
26
|
-
[key: string]: Grc20Id
|
|
26
|
+
[key: string]: Grc20Id;
|
|
27
27
|
}
|
|
28
28
|
| undefined;
|
|
29
29
|
/**
|
|
30
|
-
* Record of relation properties to the `Id
|
|
30
|
+
* Record of relation properties to the `Id` of the type in the Knowledge Graph
|
|
31
31
|
*
|
|
32
32
|
* @since 0.2.0
|
|
33
33
|
*/
|
|
34
34
|
relations?:
|
|
35
35
|
| {
|
|
36
|
-
[key: string]: Grc20Id
|
|
36
|
+
[key: string]: Grc20Id;
|
|
37
37
|
}
|
|
38
38
|
| undefined;
|
|
39
39
|
};
|
|
@@ -46,20 +46,20 @@ export type MappingEntry = {
|
|
|
46
46
|
*
|
|
47
47
|
* const mapping: Mapping = {
|
|
48
48
|
* Account: {
|
|
49
|
-
* typeIds: [Id
|
|
49
|
+
* typeIds: [Id('a5fd07b1-120f-46c6-b46f-387ef98396a6')],
|
|
50
50
|
* properties: {
|
|
51
|
-
* username: Id
|
|
52
|
-
* createdAt: Id
|
|
51
|
+
* username: Id('994edcff-6996-4a77-9797-a13e5e3efad8'),
|
|
52
|
+
* createdAt: Id('64bfba51-a69b-4746-be4b-213214a879fe')
|
|
53
53
|
* }
|
|
54
54
|
* },
|
|
55
55
|
* Event: {
|
|
56
|
-
* typeIds: [Id
|
|
56
|
+
* typeIds: [Id('0349187b-526f-435f-b2bb-9e9caf23127a')],
|
|
57
57
|
* properties: {
|
|
58
|
-
* name: Id
|
|
59
|
-
* description: Id
|
|
58
|
+
* name: Id('3808e060-fb4a-4d08-8069-35b8c8a1902b'),
|
|
59
|
+
* description: Id('1f0d9007-8da2-4b28-ab9f-3bc0709f4837'),
|
|
60
60
|
* },
|
|
61
61
|
* relations: {
|
|
62
|
-
* speaker: Id
|
|
62
|
+
* speaker: Id('a5fd07b1-120f-46c6-b46f-387ef98396a6')
|
|
63
63
|
* }
|
|
64
64
|
* }
|
|
65
65
|
* }
|
|
@@ -99,6 +99,12 @@ export const SchemaDataTypePrimitive = EffectSchema.Literal('String', 'Number',
|
|
|
99
99
|
* @since 0.2.0
|
|
100
100
|
*/
|
|
101
101
|
export type SchemaDataTypePrimitive = typeof SchemaDataTypePrimitive.Type;
|
|
102
|
+
/**
|
|
103
|
+
* @since 0.4.0
|
|
104
|
+
*/
|
|
105
|
+
export function isDataTypePrimitive(val: string): val is SchemaDataTypePrimitive {
|
|
106
|
+
return ['String', 'Number', 'Boolean', 'Date', 'Point'].includes(val);
|
|
107
|
+
}
|
|
102
108
|
/**
|
|
103
109
|
* @since 0.2.0
|
|
104
110
|
*/
|
|
@@ -107,6 +113,22 @@ export const SchemaDataType = EffectSchema.Union(SchemaDataTypePrimitive, Schema
|
|
|
107
113
|
* @since 0.2.0
|
|
108
114
|
*/
|
|
109
115
|
export type SchemaDataType = typeof SchemaDataType.Type;
|
|
116
|
+
/**
|
|
117
|
+
* @since 0.4.0
|
|
118
|
+
*/
|
|
119
|
+
export function isDataType(val: string): val is SchemaDataType {
|
|
120
|
+
return isDataTypePrimitive(val) || isDataTypeRelation(val);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @since 0.4.0
|
|
124
|
+
*/
|
|
125
|
+
export function getDataType(val: string): SchemaDataType {
|
|
126
|
+
const dataType = isDataTypePrimitive(val) || isDataTypeRelation(val);
|
|
127
|
+
if (dataType) {
|
|
128
|
+
return val;
|
|
129
|
+
}
|
|
130
|
+
throw new Error(`Passed dataType ${val} is not supported`);
|
|
131
|
+
}
|
|
110
132
|
/**
|
|
111
133
|
* @since 0.2.0
|
|
112
134
|
*/
|
|
@@ -238,7 +260,7 @@ export const SchemaUnknownDecoder = EffectSchema.decodeUnknownSync(Schema);
|
|
|
238
260
|
* properties: [
|
|
239
261
|
* {
|
|
240
262
|
* name: "username",
|
|
241
|
-
* dataType: "
|
|
263
|
+
* dataType: "String",
|
|
242
264
|
* knowledgeGraphId: null
|
|
243
265
|
* }
|
|
244
266
|
* ]
|
|
@@ -297,8 +319,8 @@ export function allRelationPropertyTypesExist(types: ReadonlyArray<SchemaType>):
|
|
|
297
319
|
export type GenerateMappingResult = [mapping: Mapping, ops: ReadonlyArray<Op>];
|
|
298
320
|
|
|
299
321
|
// Helper types for internal processing
|
|
300
|
-
type PropertyIdMapping = { propName: string; id: Grc20Id
|
|
301
|
-
type TypeIdMapping = Map<string, Grc20Id
|
|
322
|
+
type PropertyIdMapping = { propName: string; id: Grc20Id };
|
|
323
|
+
type TypeIdMapping = Map<string, Grc20Id | null>;
|
|
302
324
|
type ProcessedProperty =
|
|
303
325
|
| { type: 'resolved'; mapping: PropertyIdMapping; ops: Array<Op> }
|
|
304
326
|
| { type: 'deferred'; property: SchemaTypePropertyRelation };
|
|
@@ -342,7 +364,7 @@ function createPropertyWithOps(
|
|
|
342
364
|
if (property.knowledgeGraphId) {
|
|
343
365
|
return {
|
|
344
366
|
type: 'resolved',
|
|
345
|
-
mapping: { propName: property.name, id: Grc20Id
|
|
367
|
+
mapping: { propName: property.name, id: Grc20Id(property.knowledgeGraphId) },
|
|
346
368
|
ops: [],
|
|
347
369
|
};
|
|
348
370
|
}
|
|
@@ -421,7 +443,7 @@ function processType(type: SchemaType, typeIdMap: TypeIdMapping): ProcessedType
|
|
|
421
443
|
if (type.knowledgeGraphId) {
|
|
422
444
|
const entry: MappingEntry & { typeName: string } = {
|
|
423
445
|
typeName: toPascalCase(type.name),
|
|
424
|
-
typeIds: [Grc20Id
|
|
446
|
+
typeIds: [Grc20Id(type.knowledgeGraphId)],
|
|
425
447
|
};
|
|
426
448
|
|
|
427
449
|
if (EffectArray.isNonEmptyArray(primitiveProperties)) {
|
|
@@ -487,7 +509,7 @@ function processType(type: SchemaType, typeIdMap: TypeIdMapping): ProcessedType
|
|
|
487
509
|
* @example
|
|
488
510
|
* ```ts
|
|
489
511
|
* import { Id } from "@graphprotocol/grc-20"
|
|
490
|
-
* import { generateMapping } from "@graphprotocol/
|
|
512
|
+
* import { generateMapping } from "@graphprotocol/hypergraph"
|
|
491
513
|
*
|
|
492
514
|
* const schema: Schema = {
|
|
493
515
|
* types: [
|
|
@@ -497,7 +519,7 @@ function processType(type: SchemaType, typeIdMap: TypeIdMapping): ProcessedType
|
|
|
497
519
|
* properties: [
|
|
498
520
|
* {
|
|
499
521
|
* name: "username",
|
|
500
|
-
* dataType: "
|
|
522
|
+
* dataType: "String",
|
|
501
523
|
* knowledgeGraphId: "994edcff-6996-4a77-9797-a13e5e3efad8"
|
|
502
524
|
* },
|
|
503
525
|
* {
|
|
@@ -513,12 +535,12 @@ function processType(type: SchemaType, typeIdMap: TypeIdMapping): ProcessedType
|
|
|
513
535
|
* properties: [
|
|
514
536
|
* {
|
|
515
537
|
* name: "name",
|
|
516
|
-
* dataType: "
|
|
538
|
+
* dataType: "String",
|
|
517
539
|
* knowledgeGraphId: "3808e060-fb4a-4d08-8069-35b8c8a1902b"
|
|
518
540
|
* },
|
|
519
541
|
* {
|
|
520
542
|
* name: "description",
|
|
521
|
-
* dataType: "
|
|
543
|
+
* dataType: "String",
|
|
522
544
|
* knowledgeGraphId: null
|
|
523
545
|
* },
|
|
524
546
|
* {
|
|
@@ -535,20 +557,20 @@ function processType(type: SchemaType, typeIdMap: TypeIdMapping): ProcessedType
|
|
|
535
557
|
*
|
|
536
558
|
* expect(mapping).toEqual({
|
|
537
559
|
* Account: {
|
|
538
|
-
* typeIds: [Id
|
|
560
|
+
* typeIds: [Id("a5fd07b1-120f-46c6-b46f-387ef98396a6")], // comes from input schema
|
|
539
561
|
* properties: {
|
|
540
|
-
* username: Id
|
|
541
|
-
* createdAt: Id
|
|
562
|
+
* username: Id("994edcff-6996-4a77-9797-a13e5e3efad8"), // comes from input schema
|
|
563
|
+
* createdAt: Id("8cd7d9ac-a878-4287-8000-e71e6f853117"), // generated from Graph.createProperty Op
|
|
542
564
|
* }
|
|
543
565
|
* },
|
|
544
566
|
* Event: {
|
|
545
|
-
* typeIds: [Id
|
|
567
|
+
* typeIds: [Id("20b3fe39-8e62-41a0-b9cb-92743fd760da")], // generated from Graph.createType Op
|
|
546
568
|
* properties: {
|
|
547
|
-
* name: Id
|
|
548
|
-
* description: Id
|
|
569
|
+
* name: Id("3808e060-fb4a-4d08-8069-35b8c8a1902b"), // comes from input schema
|
|
570
|
+
* description: Id("8fc4e17c-7581-4d6c-a712-943385afc7b5"), // generated from Graph.createProperty Op
|
|
549
571
|
* },
|
|
550
572
|
* relations: {
|
|
551
|
-
* speaker: Id
|
|
573
|
+
* speaker: Id("651ce59f-643b-4931-bf7a-5dc0ca0f5a47"), // generated from Graph.createProperty Op
|
|
552
574
|
* }
|
|
553
575
|
* }
|
|
554
576
|
* })
|
|
@@ -557,23 +579,23 @@ function processType(type: SchemaType, typeIdMap: TypeIdMapping): ProcessedType
|
|
|
557
579
|
* {
|
|
558
580
|
* type: "CREATE_PROPERTY",
|
|
559
581
|
* property: {
|
|
560
|
-
* id: Id
|
|
561
|
-
* dataType: "
|
|
582
|
+
* id: Id("8cd7d9ac-a878-4287-8000-e71e6f853117"),
|
|
583
|
+
* dataType: "String"
|
|
562
584
|
* }
|
|
563
585
|
* },
|
|
564
586
|
* // Graph.createProperty Op for Event.description property
|
|
565
587
|
* {
|
|
566
588
|
* type: "CREATE_PROPERTY",
|
|
567
589
|
* property: {
|
|
568
|
-
* id: Id
|
|
569
|
-
* dataType: "
|
|
590
|
+
* id: Id("8fc4e17c-7581-4d6c-a712-943385afc7b5"),
|
|
591
|
+
* dataType: "String"
|
|
570
592
|
* }
|
|
571
593
|
* },
|
|
572
594
|
* // Graph.createProperty Op for Event.speaker property
|
|
573
595
|
* {
|
|
574
596
|
* type: "CREATE_PROPERTY",
|
|
575
597
|
* property: {
|
|
576
|
-
* id: Id
|
|
598
|
+
* id: Id("651ce59f-643b-4931-bf7a-5dc0ca0f5a47"),
|
|
577
599
|
* dataType: "RELATION"
|
|
578
600
|
* }
|
|
579
601
|
* },
|
|
@@ -581,7 +603,7 @@ function processType(type: SchemaType, typeIdMap: TypeIdMapping): ProcessedType
|
|
|
581
603
|
* {
|
|
582
604
|
* type: "CREATE_PROPERTY",
|
|
583
605
|
* property: {
|
|
584
|
-
* id: Id
|
|
606
|
+
* id: Id("651ce59f-643b-4931-bf7a-5dc0ca0f5a47"),
|
|
585
607
|
* dataType: "RELATION"
|
|
586
608
|
* }
|
|
587
609
|
* },
|
|
@@ -600,8 +622,8 @@ export function generateMapping(input: Schema): GenerateMappingResult {
|
|
|
600
622
|
// Build initial type ID map
|
|
601
623
|
const typeIdMap: TypeIdMapping = pipe(
|
|
602
624
|
schema.types,
|
|
603
|
-
EffectArray.reduce(new Map<string, Grc20Id
|
|
604
|
-
map.set(type.name, type.knowledgeGraphId != null ? Grc20Id
|
|
625
|
+
EffectArray.reduce(new Map<string, Grc20Id | null>(), (map, type) =>
|
|
626
|
+
map.set(type.name, type.knowledgeGraphId != null ? Grc20Id(type.knowledgeGraphId) : null),
|
|
605
627
|
),
|
|
606
628
|
);
|
|
607
629
|
|
|
@@ -750,7 +772,7 @@ export class RelationValueTypeDoesNotExistError extends Data.TaggedError(
|
|
|
750
772
|
export function mapSchemaDataTypeToGRC20PropDataType(dataType: SchemaDataType): CreatePropertyParams['dataType'] {
|
|
751
773
|
switch (true) {
|
|
752
774
|
case dataType === 'Boolean': {
|
|
753
|
-
return '
|
|
775
|
+
return 'BOOLEAN';
|
|
754
776
|
}
|
|
755
777
|
case dataType === 'Date': {
|
|
756
778
|
return 'TIME';
|
|
@@ -765,7 +787,7 @@ export function mapSchemaDataTypeToGRC20PropDataType(dataType: SchemaDataType):
|
|
|
765
787
|
return 'RELATION';
|
|
766
788
|
}
|
|
767
789
|
default: {
|
|
768
|
-
return '
|
|
790
|
+
return 'STRING';
|
|
769
791
|
}
|
|
770
792
|
}
|
|
771
793
|
}
|