Skip to content

Commit 2e34401

Browse files
Merge pull request #26 from xanthous-tech/delete-all
Delete all
2 parents 6ff5760 + 52af6f4 commit 2e34401

12 files changed

Lines changed: 70 additions & 58 deletions

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xanthous/dgraph-orm",
3-
"version": "0.2.0-alpha5",
3+
"version": "0.3.2",
44
"description": "dgraph ORM in TypeScript",
55
"main": "dist/index.js",
66
"author": "Xanthous Tech Developers <hi@x-tech.io>",
@@ -19,8 +19,7 @@
1919
"scripts": {
2020
"clean": "rimraf dist",
2121
"build": "tsc -p tsconfig.build.json",
22-
"postinstall": "npm run clean && npm run build",
23-
"prestart": "npm run postinstall",
22+
"prepublish": "npm run test && npm run clean && npm run build",
2423
"lint": "eslint ./src --ext .ts",
2524
"lint:fix": "eslint --fix",
2625
"format": "prettier --write",
@@ -40,6 +39,7 @@
4039
"@babel/preset-env": "^7.7.1",
4140
"@babel/preset-typescript": "^7.7.2",
4241
"@types/jest": "^24.0.23",
42+
"@types/uniqid": "^4.1.3",
4343
"@typescript-eslint/eslint-plugin": "^2.16.0",
4444
"@typescript-eslint/parser": "^2.16.0",
4545
"eslint": "^6.8.0",
@@ -63,9 +63,9 @@
6363
"debug": "^4.1.1",
6464
"dgraph-js": "^2.0.2",
6565
"grpc": "^1.24.2",
66-
"instauuid": "^1.0.4",
6766
"lodash": "^4.17.15",
68-
"reflect-metadata": "^0.1.13"
67+
"reflect-metadata": "^0.1.13",
68+
"uniqid": "^5.2.0"
6969
},
7070
"volta": {
7171
"node": "10.18.1",

src/decorator/predicate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,9 @@ export interface IPredicate<T, U = void> {
118118
* Remove a list of nodes.
119119
*/
120120
delete(nodes: T[]): IPredicate<T, U>;
121+
122+
/**
123+
* Removes all nodes from the predicate.
124+
*/
125+
deleteAll(): IPredicate<T, U>;
121126
}

src/transaction/mutation-builder.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ export class MutationBuilder {
9595

9696
public getDeleteNQuadsString(): string {
9797
const quads = this.getDeleteNQuads();
98-
return new Writer({ format: 'N-Quads' }).quadsToString(quads);
98+
const quadsString = new Writer({ format: 'N-Quads' }).quadsToString(quads);
99+
100+
const lines = quadsString.split('\n');
101+
102+
return Array.from(new Set(lines)).join('\n');
99103
}
100104

101105
public getDeleteNQuads(): Quad[] {

src/transaction/predicate-impl.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,28 @@ export class PredicateImpl<T = any, U = any> implements IPredicate<T, U> {
7272

7373
delete(node: T): IPredicate<T, U>;
7474
delete(nodes: T[]): IPredicate<T, U>;
75-
delete(nodeOrNodes: T | T[]) {
75+
delete(nodeOrNodes: T | T[]): IPredicate<T, U> {
7676
if (!Array.isArray(nodeOrNodes)) {
7777
nodeOrNodes = [nodeOrNodes];
7878
}
7979

8080
const deleteDiff = this._diff.deletes.get(this)!;
8181
for (const node of nodeOrNodes) {
8282
deleteDiff.add(node);
83+
this._data.splice(
84+
this._data.findIndex(n => n === node),
85+
1
86+
);
8387
}
8488

8589
return this;
8690
}
8791

92+
deleteAll(): IPredicate<T, U> {
93+
this.delete(this._data);
94+
return this;
95+
}
96+
8897
private trackFacetValues(facet: Object): void {
8998
const metadata = MetadataStorage.Instance.facets.get(facet.constructor.name);
9099
if (!metadata) {

src/transaction/transaction.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Quad } from 'n3';
2-
import * as UUID from 'instauuid';
2+
import uniqid from 'uniqid';
33
import { plainToClass } from 'class-transformer';
44

55
import { IPredicate } from '..';
@@ -76,9 +76,9 @@ export class Transaction<T extends Object, V> implements ITransaction<T> {
7676
return new MutationBuilder(this.diff, this.tempIDS).getSetNQuadsString();
7777
}
7878

79-
public delete(node: T): void;
80-
public delete(nodes: T[]): void;
81-
public delete(nodeOrNodes: T | T[]) {
79+
public delete(node: T): ITransaction<T>;
80+
public delete(nodes: T[]): ITransaction<T>;
81+
public delete(nodeOrNodes: T | T[]): ITransaction<T> {
8282
if (!Array.isArray(nodeOrNodes)) {
8383
nodeOrNodes = [nodeOrNodes];
8484
}
@@ -109,7 +109,7 @@ export class Transaction<T extends Object, V> implements ITransaction<T> {
109109
// new created node instance when assigning data to it.
110110
delete data.uid;
111111
} else {
112-
id = UUID('hex').toString();
112+
id = uniqid();
113113
this.tempIDS.set(nodeInstance, id);
114114
}
115115

@@ -256,7 +256,13 @@ export class Transaction<T extends Object, V> implements ITransaction<T> {
256256
facetDataIndices.forEach(idx => {
257257
const plain = facets.reduce<IObjectLiteral>((acc, f) => {
258258
const facetPropertyName = `${name}|${f.args.propertyName}`;
259-
const facetValue = Private.accessUnsafe(value._owner, facetPropertyName)[idx];
259+
const facetDataMap = Private.accessUnsafe(value._owner, facetPropertyName);
260+
261+
if (!facetDataMap) {
262+
return acc;
263+
}
264+
265+
const facetValue = facetDataMap[idx];
260266

261267
if (facetValue) {
262268
acc[f.args.propertyName] = facetValue;

tests/delete.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,24 @@ describe('Delete handling', function() {
4545
transaction.delete(kamil);
4646
transaction.tree[0].friends.delete(jane);
4747

48+
// XXX: here kamil is not deleted in the friends predicate
49+
expect(transaction.tree[0].friends.get().length).toBe(1);
50+
51+
expect(transaction.getDeleteNQuadsString()).toEqual(
52+
`<0x3> * * .
53+
<0x2> * * .
54+
<0x1> <Person.friends> <0x2> .
55+
`
56+
);
57+
58+
transaction.tree[0].friends.deleteAll();
59+
expect(transaction.tree[0].friends.get().length).toBe(0);
60+
4861
expect(transaction.getDeleteNQuadsString()).toEqual(
4962
`<0x3> * * .
5063
<0x2> * * .
5164
<0x1> <Person.friends> <0x2> .
65+
<0x1> <Person.friends> <0x3> .
5266
`
5367
);
5468
});

tests/mutation.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ describe('Mutation handling', () => {
101101
class PersonKnows {
102102
@Facet()
103103
familiarity: number;
104+
105+
@Facet()
106+
years: number;
104107
}
105108

106109
@Node()
@@ -120,6 +123,7 @@ describe('Mutation handling', () => {
120123
uid: '0x1',
121124
'Person.name': 'John',
122125
'Person.friends|familiarity': { '0': 999 },
126+
'Person.friends|years': { '0': 3 },
123127
'Person.friends': [
124128
{
125129
uid: '0x2',
@@ -152,7 +156,7 @@ describe('Mutation handling', () => {
152156
`<0x3> <Person.name> "New Kamil"^^<xs:string> .
153157
<0x2> <Person.name> "New Jane"^^<xs:string> .
154158
<0x1> <Person.name> "New John"^^<xs:string> .
155-
<0x1> <Person.friends> <0x2> (familiarity=666) .
159+
<0x1> <Person.friends> <0x2> (familiarity=666,years=3) .
156160
`
157161
);
158162
});

tests/perf.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-ignore
2-
import * as util from 'util';
31

42
import { IPredicate, Node, Predicate, Property, TransactionBuilder, Uid } from '../src';
53

@@ -77,15 +75,14 @@ describe('Performance testing', () => {
7775
console.timeEnd('Second');
7876
});
7977

80-
it.only('Fuzz with random data', () => {
78+
it('Fuzz with random data', () => {
8179
interface IStation {
8280
uid: string;
8381
name: string;
8482
connects: IStation[];
8583
}
8684

8785
@Node()
88-
// @ts-ignore
8986
class Station {
9087
@Uid()
9188
uid: string;

tsconfig.build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "commonjs",
77
"declaration": true,
88
"removeComments": true,
9+
"esModuleInterop": true,
910
"emitDecoratorMetadata": true,
1011
"experimentalDecorators": true,
1112
"target": "es2015",

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "commonjs",
77
"declaration": true,
88
"removeComments": true,
9+
"esModuleInterop": true,
910
"emitDecoratorMetadata": true,
1011
"experimentalDecorators": true,
1112
"target": "es2015",

0 commit comments

Comments
 (0)