xanthous-tech / dgraph-orm Goto Github PK
View Code? Open in Web Editor NEWdGraph ORM written in TypeScript
License: MIT License
dGraph ORM written in TypeScript
License: MIT License
Currently we don't support mapping facet alias in the ObjectMapper
. However, if this feature is important, we can try to find a way to support it.
I used the 0.1.15
version and the new 0.1.17-alpha3
add test and found that the generated code is different, when the facetvalue is 0
or undefined
, the 0.1.17-alpha3
will go wrong
it('should add the correct facet', function() {
class OrderFacet {
constructor({ order, width }: { order?: number; width?: number } = {}) {
if (order != null) {
this.order = order;
}
if (width != null) {
this.width = width;
}
}
@Facet()
order?: number;
@Facet()
width?: number;
}
@Node()
class Person {
@Uid()
id: string;
@Property()
name: string;
@Predicate({ type: () => Person, facet: OrderFacet })
friends: IPredicate<Person, OrderFacet>;
}
const lim = new Person();
lim.name = 'Lim';
const nick = new Person();
nick.name = 'Nick';
lim.friends.withFacet(new OrderFacet({ order: 0 })).add(nick);
nick.friends.withFacet(new OrderFacet({ order: 1, width: 200 })).add(lim);
console.log(MutationBuilder.getSetNQuadsString(lim).quads);
// _:b83db4fc12819b2a <dgraph.type> "Person" .
// _:b83db4fc13060b01 <dgraph.type> "Person" .
// _:b83db4fc12819b2a <Person.friends> _:b83db4fc13060b01 (order=0) .
// _:b83db4fc13060b01 <Person.friends> _:b83db4fc12819b2a (order=1,width=200) .
});
it('should add the correct facet', function() {
class OrderFacet {
constructor({ order, width }: { order?: number; width?: number } = {}) {
if (order != null) {
this.order = order;
}
if (width != null) {
this.width = width;
}
}
@Facet()
order?: number;
@Facet()
width?: number;
}
@Node()
class Person {
@Uid()
id: string;
@Property()
name: string;
@Predicate({ type: () => Person, facet: OrderFacet })
friends: IPredicate<Person, OrderFacet>;
}
const transaction = TransactionBuilder.build();
const lim = transaction.nodeFor(Person);
lim.name = 'Lim';
const nick = transaction.nodeFor(Person);
nick.name = 'Nick';
lim.friends.withFacet(new OrderFacet({ order: 0 })).add(nick);
nick.friends.withFacet(new OrderFacet({ order: 1, width: 200 })).add(lim);
console.log(transaction.getSetNQuadsString());
// _:b83db8df3d812bd0 <dgraph.type> "Person" .
// _:b83db8df3d812bd0 <Person.name> "Lim"^^<xs:string> .
// _:b83db8df3d8c3f6b <dgraph.type> "Person" .
// _:b83db8df3d8c3f6b <Person.name> "Nick"^^<xs:string> .
// _:b83db8df3d812bd0 <Person.friends> _:b83db8df3d8c3f6b (order=undefined,width=undefined) . <- Facet generation error
// _:b83db8df3d8c3f6b <Person.friends> _:b83db8df3d812bd0 (order=1,width=200) .
});
The supported RDF datatypes and the corresponding internal type in which the data is stored are as follows.
Storage Type | Dgraph type |
---|---|
xs:string | string |
xs:dateTime | dateTime |
xs:date | datetime |
xs:int | int |
xs:boolean | bool |
xs:double | float |
xs:float | float |
geo:geojson | geo |
xs:password | password |
http://www.w3.org/2001/XMLSchema#string | string |
http://www.w3.org/2001/XMLSchema#dateTime | dateTime |
http://www.w3.org/2001/XMLSchema#date | dateTime |
http://www.w3.org/2001/XMLSchema#int | int |
http://www.w3.org/2001/XMLSchema#boolean | bool |
http://www.w3.org/2001/XMLSchema#double | float |
http://www.w3.org/2001/XMLSchema#float | float |
Needs to be able to override dgraph.type
to fit in existing initialized data.
it('performance Testing', () => {
@Node()
class Cell {
@Uid()
uid: string;
@Predicate({ name: 'from_row', type: () => Row })
fromRow: IPredicate<Row>;
@Predicate({ name: 'from_column', type: () => Column })
fromColumn: IPredicate<Column>;
}
@Node()
class Row {
@Uid()
uid: string;
@Predicate({ name: 'has_cell', type: () => Cell })
hasCell: IPredicate<Cell>;
}
@Node()
class Column {
@Uid()
uid: string;
@Predicate({ name: 'has_cell', type: () => Cell })
hasCell: IPredicate<Cell>;
}
const column = new Column();
let i = 0;
let first20 = 0;
let last20 = 0;
let time = 0;
const times = 5000;
while (i++ < times) {
const cell = new Cell();
const row = new Row();
cell.fromRow.add(row);
cell.fromColumn.add(column);
row.hasCell.add(cell);
column.hasCell.add(cell);
const _time1 = Date.now();
MutationBuilder.getSetNQuadsString(cell);
const _time2 = Date.now();
time += _time2 - _time1;
if (i < 21) {
first20 += _time2 - _time1;
}
if (i > 4980) {
last20 += _time2 - _time1;
}
}
console.log('duration: ', time);
console.log('avg: ', time / times);
console.log(`first 20: ${first20}, avg: ${first20 / 20}`);
console.log(`last 20: ${last20}, avg: ${last20 / 20}`);
});
deleteAll
APIafter calling IPredicate.delete(node: T)
, the generated mutation also removes all node data and edges as seen here:
this should be optional: there should be a way to configure whether you want just the predicate edge deleted or the entire node as well.
Sample data:
const data = [
{
uid: '0x1',
'Person.name': 'John'
},
{
uid: '0x2',
'Person.name': 'Jane',
'Person.friends': [
{
uid: '0x1',
}
]
}
];
How do I delete only the friends
edge between Jane and John?
I would just like to generate the following nquad: <0x2> <Person.friends> <0x1>
, but if I call anything like jane.friends.delete(john);
I get more than just that edge deleted:
<0x1> * * .
<0x2> <Person.friends> <0x1> .
This line of code here should be checking if the value is undefined
rather than falsey. It's causing facets with 0
value to be ignored by the transaction builder.
dgraph-orm/src/transaction/transaction.ts
Lines 264 to 266 in 80a1e45
Created pull request to complement this issue report: #39
When we use an existing object to associate a new object, the edge association is not initialized, so it is undefined, which makes it impossible to add an association.
it('should initialize new object', function() {
@Node()
class Person {
@Uid()
id: string;
@Property()
name: string;
@Predicate({ type: () => Person })
friends: IPredicate<Person>;
}
const transaction = TransactionBuilder.build();
// This is a new data
const nick = transaction.nodeFor(Person);
nick.name = 'Nick';
// This is an existing data
const c = new Person();
c.id = '0x12';
c.name = 'c';
nick.friends.add(c);
c.friends.add(nick); // <-Cannot read property 'add' of undefined
console.log(transaction.getSetNQuadsString());
});
it('should support the same recursive ID', ()=>{
//#region class
@Node()
class Cell{
@Uid()
uid: string;
@Predicate({ name: 'from_row', type: () => Row })
fromRow: Predicate<Row>;
@Predicate({ name: 'from_column', type: () => Column })
fromColumn: Predicate<Column>;
}
@Node()
class Row{
@Uid()
uid: string;
@Predicate({ name: 'has_cell', type: () => Cell })
hasCell: Predicate<Cell>;
}
@Node()
class Column{
@Uid()
uid: string;
@Predicate({ name: 'has_cell', type: () => Cell })
hasCell: Predicate<Cell>;
}
//#endregion
const cell = new Cell();
const row = new Row();
const column = new Column();
cell.fromRow.add(row);
cell.fromColumn.add(column);
row.hasCell.add(cell);
const rq = MutationBuilder.getSetNQuads(row);
const rowId = rq.nodeMap.get(row)?.value;
const fromRowId = (rq.quads as Quad[]).find(r=>r.predicate.id === 'from_row')?.object.value;
expect(rowId).toEqual(fromRowId);
});
When I delete the relationship of an edge, check that the relationship still exists after using the remove function
const recordReferenceColumn = column.hasRecordReferenceColumn.get();
if (recordReferenceColumn.length) {
column.hasForeignLookupColumn.remove(recordReferenceColumn[0]);
}
schema define:
@Node({
dgraphType: 'Post'
})
export class PostEntity {
@Property({
type: PropertyType.String
})
title: string;
}
schema build output:
type Post {
PostEntity.title: string // prefix is classname and type restrict is unnecessary.
}
PostEntity.title: string .
expect output:
type Post {
Post.title
}
Post.title: string .
It works well, but the result is not expected.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.