Skip to content

Commit ab89dcc

Browse files
committed
wip
1 parent 5ec1843 commit ab89dcc

File tree

4 files changed

+118
-41
lines changed

4 files changed

+118
-41
lines changed

src/graph/create-relation-type.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import { Triple } from '../triple.js';
55
import type { CreateRelationOp, Op, SetTripleOp } from '../types.js';
66

77
type Params = {
8-
from: string;
9-
to: string;
108
name: string;
119
};
1210

13-
export const createRelationType = async ({ from, to, name }: Params) => {
11+
export const createRelationType = async ({ name }: Params) => {
1412
const id = generate();
1513
const ops: Op[] = [];
1614

src/graph/create-type.ts

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,67 @@
1-
import { SCHEMA_TYPE, TYPES_ATTRIBUTE } from '../core/ids/system.js';
1+
import { ATTRIBUTE, SCHEMA_TYPE, TYPES_ATTRIBUTE } from '../core/ids/system.js';
22
import { generate } from '../id.js';
33
import { Relation } from '../relation.js';
44
import { Triple } from '../triple.js';
55
import type { CreateRelationOp, Op, SetTripleOp, ValueType } from '../types.js';
66

77
type Params = {
8-
attributes: {
9-
[filedName: string]: { id?: string; type: ValueType; name: string };
8+
existingAttributes?: { [filedName: string]: { id: string; type: ValueType } };
9+
newAttributes?: {
10+
[filedName: string]: { type: ValueType; name: string };
1011
};
1112
relations?: Array<{
1213
relationTypeId: string;
1314
toId: string;
1415
}>;
1516
};
1617

17-
export const createType = async ({ attributes }: Params) => {
18+
export const createType = async ({ newAttributes, existingAttributes }: Params) => {
1819
const id = generate();
1920
const ops: Op[] = [];
2021
const mapping: Record<string, { id: string; type: ValueType }> = {};
2122

22-
for (const [fieldName, item] of Object.entries(attributes)) {
23-
const attributeId = item.id || generate();
24-
const setTripleOp: SetTripleOp = Triple.make({
25-
entityId: id,
26-
attributeId,
27-
value: {
28-
type: item.type,
29-
value: fieldName,
30-
},
31-
});
32-
33-
ops.push(setTripleOp);
34-
mapping[fieldName] = { id: attributeId, type: item.type };
35-
}
36-
3723
// add the type relation
38-
const setRelationOp: CreateRelationOp = Relation.make({
24+
const relationOp: CreateRelationOp = Relation.make({
3925
fromId: id,
4026
relationTypeId: TYPES_ATTRIBUTE,
4127
toId: SCHEMA_TYPE,
4228
});
43-
ops.push(setRelationOp);
29+
ops.push(relationOp);
30+
31+
if (newAttributes) {
32+
for (const [fieldName, item] of Object.entries(newAttributes)) {
33+
const attributeId = generate();
34+
const setTripleOp: SetTripleOp = Triple.make({
35+
entityId: id,
36+
attributeId,
37+
value: {
38+
type: item.type,
39+
value: fieldName,
40+
},
41+
});
42+
43+
const relationOp: CreateRelationOp = Relation.make({
44+
fromId: id,
45+
relationTypeId: ATTRIBUTE,
46+
toId: attributeId,
47+
});
48+
49+
ops.push(setTripleOp, relationOp);
50+
mapping[fieldName] = { id: attributeId, type: item.type };
51+
}
52+
}
53+
54+
if (existingAttributes) {
55+
for (const [fieldName, item] of Object.entries(existingAttributes)) {
56+
const relationOp: CreateRelationOp = Relation.make({
57+
fromId: id,
58+
relationTypeId: ATTRIBUTE,
59+
toId: item.id,
60+
});
61+
ops.push(relationOp);
62+
mapping[fieldName] = { id: item.id, type: item.type };
63+
}
64+
}
4465

4566
// TODO relations
4667

src/graph/graph.test.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,51 @@ import { createType } from './create-type.js';
77

88
describe('Graph', () => {
99
it('creates multiple types, relation types, entities, relations', async () => {
10-
const operations: Array<Op> = [];
10+
const ops: Array<Op> = [];
1111

1212
// create person type
1313
const {
1414
id: personTypeId,
1515
ops: createPersonTypeOps,
1616
mapping: personMapping,
1717
} = await createType({
18-
attributes: {
19-
name: {
20-
id: NAME_ATTRIBUTE,
21-
type: 'TEXT',
22-
name: 'Name',
23-
},
18+
newAttributes: {
2419
age: {
25-
// ID is optional and will be generated automatically
2620
type: 'NUMBER',
2721
name: 'Age',
2822
},
2923
},
24+
existingAttributes: {
25+
name: {
26+
id: NAME_ATTRIBUTE,
27+
type: 'TEXT',
28+
},
29+
},
3030
});
31-
operations.push(...createPersonTypeOps);
31+
ops.push(...createPersonTypeOps);
3232

3333
// create restaurant type
3434
const {
3535
id: restaurantTypeId,
3636
ops: createRestaurantTypeOps,
3737
mapping: restaurantMapping,
3838
} = await createType({
39-
attributes: {
39+
existingAttributes: {
4040
name: {
4141
id: NAME_ATTRIBUTE,
4242
type: 'TEXT',
43-
name: 'Name',
4443
},
4544
},
4645
});
47-
operations.push(...createRestaurantTypeOps);
46+
ops.push(...createRestaurantTypeOps);
4847

4948
// create loves relation type
5049
const { id: relationTypeId, ops: createRelationTypeOps } = await createRelationType({
5150
from: personTypeId,
5251
to: restaurantTypeId,
5352
name: 'Loves',
5453
});
55-
operations.push(...createRelationTypeOps);
54+
ops.push(...createRelationTypeOps);
5655

5756
// create restaurant entity
5857
const { id: restaurantId, ops: createRestaurantOps } = await createEntity({
@@ -62,7 +61,7 @@ describe('Graph', () => {
6261
},
6362
mapping: restaurantMapping,
6463
});
65-
operations.push(...createRestaurantOps);
64+
ops.push(...createRestaurantOps);
6665

6766
// create person entity
6867
const { id: personId, ops: createPersonOps } = await createEntity({
@@ -73,17 +72,17 @@ describe('Graph', () => {
7372
mapping: personMapping,
7473
relations: [{ relationTypeId, toId: restaurantId }],
7574
});
76-
operations.push(...createPersonOps);
75+
ops.push(...createPersonOps);
7776

78-
expect(operations.length).toBe(12);
77+
expect(ops.length).toBe(12);
7978

8079
from: Person
8180
to: Name
8281
relation type: PROPERTIES
8382

8483
// publish all changes
8584
// const { proposalId } = await publish({
86-
// operations,
85+
// ops,
8786
// editName: 'Create person, restaurant, and loves relation',
8887
// spaceId: '0x1',
8988
// accountId: '0x2',

src/graph/graph2.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const schema = {
2+
event: {
3+
types: ['AaGd6UMXfNtL6U6Xx7K8Cv'], // Event type
4+
},
5+
};
6+
7+
const { ops, id: ageAttributeId } = createAttributeEntity({
8+
type: 'NUMBER',
9+
name: 'Age',
10+
});
11+
12+
const { ops, id: metFirstAttributeId } = createAttributeEntity({
13+
type: 'TIME',
14+
name: 'Met First',
15+
});
16+
17+
const { ops, mapping } = createType({
18+
attributes: {
19+
name: {
20+
id: NAME_ATTRIBUTE,
21+
type: 'TEXT',
22+
},
23+
age: {
24+
id: ageAttributeId,
25+
type: 'NUMBER',
26+
},
27+
},
28+
relations: {
29+
friend: {
30+
relationTypeId: 'AaGd6UMXfNtL6U6Xx7K8Cv',
31+
attributes: {
32+
metFirst: {
33+
id: metFirstAttributeId,
34+
type: 'TIME',
35+
},
36+
},
37+
},
38+
},
39+
});
40+
41+
schema.person = mapping;
42+
43+
const createEntity = createEntityFactory(schema);
44+
45+
const { ops: personOps, mapping: personMapping } = createEntity({
46+
attributes: {
47+
name: 'John Doe',
48+
},
49+
relations: {
50+
friend: [
51+
{
52+
id: 'ABCD', // other person ID
53+
attributes: {
54+
metFirst: 'Jane Doe',
55+
},
56+
},
57+
],
58+
},
59+
});

0 commit comments

Comments
 (0)