Skip to content

Commit

Permalink
feat: support maps
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <danscode@selman.org>
  • Loading branch information
dselman committed Dec 12, 2023
1 parent e0532e1 commit 7e543c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
"version": "1.0.0",
"description": "Concerto Graph",
"main": "dist/graphmodel.js",
"type": "module",
"license": "Apache-2.0",
"scripts": {
"prebuild": "npx eslint src/**/*.ts",
"build": "tsc",
"prestart": "npm run build",
"start": "node dist/demo/index.js",
"start": "node dist/src/demo/index.js",
"test": "jest",
"coverage": "npx jest --coverage"
},
Expand Down
15 changes: 13 additions & 2 deletions src/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ concept Address {
o String country
}
scalar PersonName extends String
scalar PersonEmail extends String
map AddressBook {
o PersonName
o PersonEmail
}
concept Person extends GraphNode {
o Address address optional
@label("ACTED_IN")
--> Movie[] actedIn optional
@label("DIRECTED")
Expand All @@ -31,6 +37,8 @@ concept Director extends Person {
}
concept User extends Person {
o Address address
o AddressBook addressBook
@label("RATED")
--> Movie[] ratedMovies optional
}
Expand Down Expand Up @@ -73,6 +81,9 @@ async function run() {
state: 'CO',
country: 'USA'
};
const addressBook = {
'Dan' : 'dan@example.com'
};
await graphModel.mergeNode(transaction, `${NS}.Movie`, {identifier: 'Brazil', summary: 'The film centres on Sam Lowry, a low-ranking bureaucrat trying to find a woman who appears in his dreams while he is working in a mind-numbing job and living in a small apartment, set in a dystopian world in which there is an over-reliance on poorly maintained (and rather whimsical) machines'} );
await graphModel.mergeNode(transaction, `${NS}.Movie`, {identifier: 'The Man Who Killed Don Quixote', summary: 'Instead of a literal adaptation, Gilliam\'s film was about "an old, retired, and slightly kooky nobleman named Alonso Quixano".'} );
await graphModel.mergeNode(transaction, `${NS}.Movie`, {identifier: 'Fear and Loathing in Las Vegas', summary: 'Duke, under the influence of mescaline, complains of a swarm of giant bats, and inventories their drug stash. They pick up a young hitchhiker and explain their mission: Duke has been assigned by a magazine to cover the Mint 400 motorcycle race in Las Vegas. They bought excessive drugs for the trip, and rented a red Chevrolet Impala convertible.'} );
Expand All @@ -90,7 +101,7 @@ async function run() {
await graphModel.mergeRelationship(transaction, `${NS}.Director`, 'Terry Gilliam', `${NS}.Movie`, 'The Man Who Killed Don Quixote', 'directed' );
await graphModel.mergeRelationship(transaction, `${NS}.Director`, 'Terry Gilliam', `${NS}.Movie`, 'Fear and Loathing in Las Vegas', 'directed' );

await graphModel.mergeNode(transaction, `${NS}.User`, {identifier: 'Dan', address} );
await graphModel.mergeNode(transaction, `${NS}.User`, {identifier: 'Dan', address, addressBook} );
await graphModel.mergeRelationship(transaction, `${NS}.User`, 'Dan', `${NS}.Movie`, 'Brazil', 'ratedMovies' );

await graphModel.mergeNode(transaction, `${NS}.Actor`, {identifier: 'Jonathan Pryce'} );
Expand Down
17 changes: 13 additions & 4 deletions src/graphmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,19 @@ export class GraphModel {
newProperties[key] = DateTime.fromStandardDate(new Date(value as string))
} else if (value !== null && !Array.isArray(value) && typeof value === 'object') {
const propertyDecl = this.modelManager.getType(property.getFullyQualifiedTypeName());
const childValue:PropertyBag = await this.validateAndTransformProperties(transaction, propertyDecl, value as PropertyBag);
Object.keys(childValue).forEach( childKey => {
newProperties[`${key}_${childKey}`] = childValue[childKey];
});
if(!propertyDecl.isMapDeclaration()) {
const childValue:PropertyBag = await this.validateAndTransformProperties(transaction, propertyDecl, value as PropertyBag);
Object.keys(childValue).forEach( childKey => {
newProperties[`${key}_${childKey}`] = childValue[childKey];
});
}
else {
Object.keys(value).forEach( childKey => {
const childValue = value[childKey];
// TODO DCS - support map values that are objects...
newProperties[`${key}_${childKey}`] = childValue;
});
}
}
else {
newProperties[key] = value;
Expand Down

0 comments on commit 7e543c7

Please sign in to comment.