Skip to content

Commit

Permalink
Convert to/from Neo4j date to native JS date.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasperblues committed Mar 14, 2024
1 parent 1718c9a commit d494e52
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 161 deletions.
155 changes: 77 additions & 78 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"@types/express": "^4.17.20",
"@types/istanbul-lib-report": "^3.0.2",
"@types/jest": "^29.5.6",
"@types/moment": "^2.13.0",
"@types/node": "^20.8.9",
"@types/pg": "^8.10.7",
"@types/shortid": "^0.0.31",
Expand Down
4 changes: 3 additions & 1 deletion src/manager/FinderOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export class FinderOperations {

async getOne<T>(spec: QuerySpecification<T>): Promise<T> {
const results = await this.persistenceManager.query(spec);
assert(results.length === 1, `Expected exactly one result`);
if (results.length != 1) {
throw new DrivineError(`Expected exactly one result`, undefined, spec)
}
return results[0];
}

Expand Down
9 changes: 7 additions & 2 deletions src/mapper/Neo4jResultMapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Integer } from 'neo4j-driver';
import { GraphResultMapper } from '@/mapper/GraphResultMapper';

const neo4j = require('neo4j-driver');
import neo4j from "neo4j-driver";

export class Neo4jResultMapper extends GraphResultMapper {
keys(record: any): string[] {
Expand Down Expand Up @@ -34,6 +33,12 @@ export class Neo4jResultMapper extends GraphResultMapper {
if (this.isRecord(val)) {
return this.toNative(this.recordToNative(val));
}
if (val instanceof neo4j.types.Date) {
return val.toStandardDate();
}
if (val instanceof neo4j.types.DateTime) {
return val.toStandardDate();
}
if (typeof val === 'object') {
return this.mapObj(this.toNative.bind(this), val);
}
Expand Down
13 changes: 10 additions & 3 deletions src/utils/ObjectUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { classToPlain } from 'class-transformer';
import { instanceToPlain } from 'class-transformer';
import neo4j from "neo4j-driver";

export interface PrimitivePropsOptions {
classToPlain: boolean;
neo4jDateToDateTime: boolean;
}

export class ObjectUtils {
Expand All @@ -10,10 +12,13 @@ export class ObjectUtils {
* @param object
* @returns {{}}
*/
static primitiveProps = (object: any, options: PrimitivePropsOptions = { classToPlain: true }): any => {
static primitiveProps = (
object: any,
options: PrimitivePropsOptions = { classToPlain: true, neo4jDateToDateTime: true }
): any => {
const props = {};
if (object) {
const source = options.classToPlain ? classToPlain(object) : object;
const source = options.classToPlain ? instanceToPlain(object) : object;
const strings = Object.keys(source);
strings.forEach((key: string) => {
const candidate = source[key];
Expand All @@ -24,6 +29,8 @@ export class ObjectUtils {
candidate.filter((it: any) => typeof it === 'object').length === 0
) {
props[key] = candidate;
} else if (candidate != undefined && candidate instanceof Date && options.neo4jDateToDateTime) {
props[key] = neo4j.types.DateTime.fromStandardDate(candidate);
} else if (candidate != undefined && typeof candidate !== 'object') {
props[key] = candidate;
}
Expand Down
12 changes: 10 additions & 2 deletions test/1.Unit/utils/ObjectUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Transform } from 'class-transformer';
import { ObjectUtils } from '@/utils';
const uuid = require('uuid').v4;
import neo4j from "neo4j-driver"

class Foobar {

}

class Urbanite {
readonly id: string;
Expand All @@ -13,7 +18,8 @@ class Urbanite {
@Transform((params) => new Date(params.value), { toClassOnly: true })
readonly dateOfBirth: Date;

nonPrimitiveProperty: Date;
nonPrimitiveProperty: Foobar;
citizenShipDate: Date;

constructor(id: string, firstName: string, lastName: string, dateOfBirth: Date) {
this.id = id;
Expand All @@ -26,12 +32,14 @@ class Urbanite {
describe('ObjectUtils::primitiveProps', () => {
it('should strip the non-primitive properties from an object', () => {
const mg = new Urbanite(uuid(), 'Brutus', 'Paramdal', new Date('1984-06-22'));
mg.nonPrimitiveProperty = new Date();
mg.nonPrimitiveProperty = new Foobar();
mg.citizenShipDate = new Date();
const props = ObjectUtils.primitiveProps(mg);
expect(props.id).not.toBeNull();
expect(props.firstName).toEqual('Brutus');
expect(props.lastName).toEqual('Paramdal');
expect(props.dateOfBirth).toEqual(456710400000);
expect(props.nonPrimitiveProperty).toBeUndefined();
expect(props.citizenShipDate).toBeInstanceOf(neo4j.types.DateTime)
});
});
Loading

0 comments on commit d494e52

Please sign in to comment.