Skip to content

Commit 01272dd

Browse files
Merge pull request #32 from osmanmesutozcan/remove-class-transformer-internal
remove class transformer
2 parents 4650dd2 + 8f8244b commit 01272dd

File tree

12 files changed

+134
-64
lines changed

12 files changed

+134
-64
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "@xanthous/dgraph-orm",
3-
"version": "0.3.3",
3+
"version": "0.4.0",
44
"description": "dgraph ORM in TypeScript",
5+
"types": "dist/index.d.ts",
56
"main": "dist/index.js",
67
"author": "Xanthous Tech Developers <hi@x-tech.io>",
78
"contributors": [
@@ -58,7 +59,6 @@
5859
"@types/debug": "^4.1.5",
5960
"@types/n3": "^1.1.1",
6061
"@xanthous/n3": "^1.3.5",
61-
"class-transformer": "^0.2.3",
6262
"class-validator": "^0.10.2",
6363
"debug": "^4.1.1",
6464
"dgraph-js": "^2.0.2",

src/decorator/predicate.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Exclude } from 'class-transformer';
2-
31
import { MetadataStorage } from '../metadata/storage';
42
import { Constructor } from '../utils/class';
53

@@ -16,9 +14,6 @@ export function Predicate(options: Predicate.IOptions): PropertyDecorator {
1614
name = `${target.constructor.name}.${propertyName}`;
1715
}
1816

19-
// Exclude the predicates to prevent class-transformer from doing unnecessary stuff..
20-
Exclude()(target, name);
21-
2217
MetadataStorage.Instance.addPredicateMetadata({
2318
facet: options.facet,
2419
count: options.count !== undefined ? options.count : true,

src/decorator/property.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Expose } from 'class-transformer';
2-
31
import { MetadataStorage } from '../metadata/storage';
42
import { PropertyType, PropertyTypeUtils } from '../types/property';
53

@@ -58,17 +56,13 @@ export function Property(options: Property.IOptions = {}): PropertyDecorator {
5856
name = `${target.constructor.name}.${propertyName}`;
5957
}
6058

61-
// When we load data into the class, we will have a new property
62-
// defined as the auto-generated name, we need to make sure property with predicate
63-
// decorator returns the correct value.
64-
Expose({ name, toClassOnly: true })(target, propertyName);
65-
6659
MetadataStorage.Instance.addPropertyMetadata({
6760
type,
6861
name,
6962
isArray,
7063
target,
71-
propertyName
64+
propertyName,
65+
default: options.default
7266
});
7367
};
7468
}
@@ -93,6 +87,11 @@ export namespace Property {
9387
* property lets user to reuse a global predicate between different nodes.
9488
*/
9589
name?: string;
90+
91+
/**
92+
* Default value to fallback to.
93+
*/
94+
default?: number | boolean | string;
9695
}
9796
}
9897

src/decorator/uid.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Expose } from 'class-transformer';
21
import { MetadataStorage } from '../metadata/storage';
32

43
/**
@@ -9,7 +8,6 @@ import { MetadataStorage } from '../metadata/storage';
98
export function Uid() {
109
return function(target: Object, propertyName: string): void {
1110
// Expose the uid json property to class.
12-
Expose({ name: 'uid', toClassOnly: true })(target, propertyName);
1311
MetadataStorage.Instance.addUidMetadata({ target, propertyName });
1412
};
1513
}

src/metadata/property.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export namespace PropertyMetadata {
3636
* Is the predicate an array type.
3737
*/
3838
isArray: boolean;
39+
40+
/**
41+
* Default value for the predicate.
42+
*/
43+
default?: any;
3944
}
4045
}
4146

src/transaction/diff-tracker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ export class DiffTracker {
1919
* Attaching a tracker on a property will make that property enumerable.
2020
*/
2121
public trackProperty(target: Object, propertyName: string, diffKey?: string): Object {
22-
this.ensureInstance(target, propertyName, diffKey || propertyName);
23-
2422
const initialValue = Reflect.get(target, propertyName);
2523
if (initialValue !== undefined) {
24+
this.ensureInstance(target, propertyName, diffKey || propertyName);
2625
this._instances.get(target)![propertyName].set(initialValue);
2726
}
2827

@@ -33,9 +32,11 @@ export class DiffTracker {
3332
configurable: true,
3433
enumerable: true,
3534
set: function(value: any) {
35+
tracker.ensureInstance(target, propertyName, diffKey || propertyName);
3636
tracker._instances.get(target)![propertyName].set(value);
3737
},
3838
get: function() {
39+
tracker.ensureInstance(target, propertyName, diffKey || propertyName);
3940
return tracker._instances.get(target)![propertyName].get();
4041
}
4142
});

src/transaction/mutation-builder.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import quad = DataFactory.quad;
1111
import namedNode = DataFactory.namedNode;
1212
import literal = DataFactory.literal;
1313
import variable = DataFactory.variable;
14+
import { DiffValue } from './diff-tracker';
1415

1516
/**
1617
* Dgraph type prefix to add on the new nodes.
@@ -132,24 +133,29 @@ export class MutationBuilder {
132133
}
133134

134135
const quads: Quad[] = [];
135-
const changes = this.diff.properties.getSets(target);
136-
if (changes.length > 0) {
137-
changes.forEach(c => {
138-
const propertyMetadata = metadata.find(pm => pm.args.propertyName === c.key || pm.args.name === c.key);
136+
const changes = this.diff.properties.getSets(target).reduce((acc, c) => {
137+
acc.set(c.key, c);
138+
return acc;
139+
}, new Map<string, DiffValue<any>>());
139140

140-
if (!propertyMetadata) {
141-
return;
142-
}
141+
metadata.forEach(pm => {
142+
const change = changes.get(pm.args.propertyName) || changes.get(pm.args.name);
143+
144+
if (!change && !pm.args.default) {
145+
return;
146+
}
143147

144-
quads.push(
145-
DataFactory.quad(
146-
targetNode,
147-
DataFactory.namedNode(c.key),
148-
DataFactory.literal(c.get(), PropertyTypeUtils.getLiteralTypeNamedNode(propertyMetadata.args.type))
148+
quads.push(
149+
DataFactory.quad(
150+
targetNode,
151+
DataFactory.namedNode(change ? change.key : pm.args.name),
152+
DataFactory.literal(
153+
change ? change.get() : pm.args.default,
154+
PropertyTypeUtils.getLiteralTypeNamedNode(pm.args.type)
149155
)
150-
);
151-
});
152-
}
156+
)
157+
);
158+
});
153159

154160
return quads;
155161
}

src/transaction/transaction.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { Quad } from 'n3';
22
import uniqid from 'uniqid';
3-
import { plainToClass } from 'class-transformer';
3+
4+
import { DiffTracker } from './diff-tracker';
5+
import { FacetStorage } from './facet-storage';
6+
import { PredicateImpl } from './predicate-impl';
7+
import { MutationBuilder } from './mutation-builder';
48

59
import { IPredicate } from '..';
610
import { MetadataStorage } from '../metadata/storage';
711
import { PredicateMetadata } from '../metadata/predicate';
812
import { INode, IPlainPredicates } from '../types/data';
913
import { Constructor } from '../utils/class';
1014
import { IObjectLiteral } from '../utils/type';
11-
12-
import { DiffTracker } from './diff-tracker';
13-
import { FacetStorage } from './facet-storage';
14-
import { PredicateImpl } from './predicate-impl';
15-
import { MutationBuilder } from './mutation-builder';
1615
import { IterableWeakMap } from '../utils/iterator';
16+
import { transformer } from '../utils/transformer';
1717

1818
/**
1919
* Create an environment for a mapped tree.
@@ -143,10 +143,7 @@ export class Transaction<T extends Object, V> implements ITransaction<T> {
143143
}
144144

145145
// Build the entry class
146-
const ins: T = plainToClass(cls, pln, {
147-
enableCircularCheck: true,
148-
strategy: 'exposeAll'
149-
});
146+
const ins: T = transformer(cls, pln);
150147

151148
// Keep reference to the instance so in case of circular we can simply get it from storage and complete the circle.
152149
storage.set(pln, ins);
@@ -271,7 +268,7 @@ export class Transaction<T extends Object, V> implements ITransaction<T> {
271268
return acc;
272269
}, {});
273270

274-
const facetInstance = plainToClass(facet!, plain);
271+
const facetInstance = Object.assign(new facet!(), plain);
275272
FacetStorage.attach(propertyName, instance, children[Number(idx)], facetInstance);
276273

277274
// Track each facet property in facet instance and reset it..
@@ -335,7 +332,7 @@ export interface ITransaction<T> {
335332
/**
336333
* Initialize a node object from data.
337334
*/
338-
nodeFor<N extends Object, V extends Object>(nodeCls: Constructor<N>, data: V): N;
335+
nodeFor<N extends Object, V extends Object>(nodeCls: Constructor<N>, data: V & { uid?: string }): N;
339336

340337
/**
341338
* Get set nQuads for transaction.

src/utils/class.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
import { Transform } from 'class-transformer';
2-
31
/**
42
* A class constructor accepting arbitrary arguments.
53
*/
64
export type Constructor<T = {}> = new (...args: any[]) => T;
7-
8-
/**
9-
* Default Value Transform Decorator, need to use with `@Expose`
10-
* @param defaultValue default value to provide
11-
*/
12-
export function DefaultValue(defaultValue: any): PropertyDecorator {
13-
return Transform((value: any) => value || defaultValue, { toClassOnly: true });
14-
}

src/utils/transformer.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Constructor } from './class';
2+
import { IObjectLiteral } from './type';
3+
import { MetadataStorage } from '../metadata/storage';
4+
5+
export function transformer<T>(cls: Constructor<T>, plain: IObjectLiteral<T>): T {
6+
const className = cls.name;
7+
const instance = new cls();
8+
Object.assign(instance, plain);
9+
10+
const properties = MetadataStorage.Instance.properties.get(className);
11+
if (properties) {
12+
for (const property of properties) {
13+
(instance as any)[property.args.propertyName] = plain[property.args.name];
14+
}
15+
}
16+
17+
const uids = MetadataStorage.Instance.uids.get(className);
18+
if (uids && uids.length > 0) {
19+
for (const uid of uids) {
20+
(instance as any)[uid.args.propertyName] = plain['uid'];
21+
}
22+
}
23+
24+
return instance;
25+
}

0 commit comments

Comments
 (0)