Skip to content

Commit

Permalink
serialization of JSON looks much nicer now
Browse files Browse the repository at this point in the history
  • Loading branch information
guyroyse committed Dec 23, 2021
1 parent e885644 commit ca862b2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
21 changes: 19 additions & 2 deletions lib/entity/entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SchemaDefinition } from "..";

/**
* A JavaScript object containing the underlying data of an {@link Entity}.
*/
Expand All @@ -7,7 +9,10 @@ export type EntityData = Record<string, number | boolean | string | string[]>;
* A constructor that creates an {@link Entity} of type TEntity.
* @template TEntity The {@link Entity} type.
*/
export type EntityConstructor<TEntity> = new (id: string, data?: EntityData) => TEntity;
export type EntityConstructor<TEntity> = new (
schemaDef: SchemaDefinition,
id: string,
data?: EntityData) => TEntity;

/**
* An Entity is the class from which objects that Redis OM maps to are made. You need
Expand All @@ -27,12 +32,24 @@ export default abstract class Entity {
*/
readonly entityData: EntityData;

private schemaDef: SchemaDefinition;

/**
* Creates an new Entity.
* @internal
*/
constructor(id: string, data: EntityData = {}) {
constructor(schemaDef: SchemaDefinition, id: string, data: EntityData = {}) {
this.schemaDef = schemaDef;
this.entityId = id;
this.entityData = data;
}

toJSON() {
let json: Record<string, any> = { entityId: this.entityId }
console.log(Object.getOwnPropertyDescriptors(this));
for (let key in this.schemaDef) {
json[key] = (this as Record<string, any>)[key];
}
return json;
}
}
4 changes: 2 additions & 2 deletions lib/repository/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class Repository<TEntity extends Entity> {
*/
createEntity(data: EntityCreationData = {}): TEntity {
let id = this.schema.generateId();
let entity = new this.schema.entityCtor(id);
let entity = new this.schema.entityCtor(this.schema.definition, id);
for (let key in data) {
if (this.schema.entityCtor.prototype.hasOwnProperty(key)) {
(entity as Record<string, any>)[key] = data[key]
Expand Down Expand Up @@ -178,7 +178,7 @@ export default class Repository<TEntity extends Entity> {
entityData = this.hashConverter.toEntityData(hashData);
}

let entity = new this.schema.entityCtor(id, entityData);
let entity = new this.schema.entityCtor(this.schema.definition, id, entityData);
return entity;
}

Expand Down
8 changes: 1 addition & 7 deletions lib/schema/schema-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ export type FieldDefinition = NumericField | StringField | BooleanField | ArrayF
/**
* Group of {@link FieldDefinition}s that define the schema for an {@link Entity}.
*/
export type SchemaDefinition = {
/**
* The key determines the propery name that is added to the {@link Entity}. The property
* contains a {@link FieldDefinition} that tell Redis OM how to map the property to Redis.
*/
[key: string]: FieldDefinition
}
export type SchemaDefinition = Record<string, FieldDefinition>;

/** A function that generates random {@link Entity.entityId | Entity IDs}. */
export type IdStrategy = () => string;
Expand Down
4 changes: 2 additions & 2 deletions lib/search/results-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class HashSearchResultsConverter<TEntity extends Entity> extends SearchRe

let converter = new HashConverter(this.schema.definition);
let entityData: EntityData = converter.toEntityData(hashData);
return new this.schema.entityCtor(id, entityData);
return new this.schema.entityCtor(this.schema.definition, id, entityData);
}
}

Expand All @@ -66,6 +66,6 @@ export class JsonSearchResultsConverter<TEntity extends Entity> extends SearchRe
let jsonData: JsonData = JSON.parse(jsonString);
let converter = new JsonConverter(this.schema.definition);
let entityData: EntityData = converter.toEntityData(jsonData);
return new this.schema.entityCtor(id, entityData);
return new this.schema.entityCtor(this.schema.definition, id, entityData);
}
}
14 changes: 10 additions & 4 deletions spec/unit/entity/entity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AliasedEntity, SimpleEntity } from '../helpers/test-entity-and-schema';
import { AliasedEntity, aliasedSchema, SimpleEntity, simpleSchema } from '../helpers/test-entity-and-schema';

describe("Entity", () => {

Expand All @@ -8,27 +8,31 @@ describe("Entity", () => {

let entity: SimpleEntity;

beforeEach(() => entity = new SimpleEntity(entityId));
beforeEach(() => entity = new SimpleEntity(simpleSchema.definition, entityId));

it("has the passed in Redis ID", () => expect(entity.entityId).toBe(entityId));
it("returns null for the number property", () => expect(entity.aNumber).toBeNull());
it("returns null for the string property", () => expect(entity.aString).toBeNull());
it("returns null for the boolean property", () => expect(entity.aBoolean).toBeNull());
it("returns null for the array property", () => expect(entity.anArray).toBeNull());
it("serializes to the expected JSON", () => expect(JSON.stringify(entity))
.toBe('{"entityId":"foo","aString":null,"aNumber":null,"aBoolean":null,"anArray":null}'));
});

describe("with data", () => {

let entity: SimpleEntity;

beforeEach(() => entity = new SimpleEntity(entityId,
beforeEach(() => entity = new SimpleEntity(simpleSchema.definition, entityId,
{ aNumber: 42, aString: 'foo', aBoolean: false, anArray: [ "foo", "bar", "baz"] }));

it("has the passed in Redis ID", () => expect(entity.entityId).toBe(entityId));
it("returns a number for the number property", () => expect(entity.aNumber).toBe(42));
it("returns a string for the string property", () => expect(entity.aString).toBe('foo'));
it("returns a boolean for the boolean property", () => expect(entity.aBoolean).toBe(false));
it("returns an array for the array property", () => expect(entity.anArray).toEqual([ 'foo', 'bar', 'baz' ]));
it("serializes to the expected JSON", () => expect(JSON.stringify(entity))
.toBe('{"entityId":"foo","aString":"foo","aNumber":42,"aBoolean":false,"anArray":["foo","bar","baz"]}'));

describe("changing the data", () => {
it("stores a number when the number property is changed", () => {
Expand Down Expand Up @@ -145,14 +149,16 @@ describe("Entity", () => {

let entity: AliasedEntity;

beforeEach(() => entity = new AliasedEntity(entityId,
beforeEach(() => entity = new AliasedEntity(aliasedSchema.definition, entityId,
{ anotherNumber: 23, anotherString: 'bar', anotherBoolean: true, anotherArray: [ "bar", "baz", "qux" ] }));

it("has the passed in Redis ID", () => expect(entity.entityId).toBe(entityId));
it("returns a number for the number property", () => expect(entity.aNumber).toBe(23));
it("returns a string for the string property", () => expect(entity.aString).toBe('bar'));
it("returns a boolean for the boolean property", () => expect(entity.aBoolean).toBe(true));
it("returns an array for the array property", () => expect(entity.anArray).toEqual([ 'bar', 'baz', 'qux' ]));
it("serializes to the expected JSON", () => expect(JSON.stringify(entity))
.toBe('{"entityId":"foo","aString":"bar","aNumber":23,"aBoolean":true,"anArray":["bar","baz","qux"]}'));

describe("changing the data", () => {
it("stores a number when the number property is changed", () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/helpers/test-entity-and-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface AliasedEntity {
aNumber?: number | null;
aBoolean?: boolean | null;
anArray?: string[] | null;
}
}

export const simpleSchema = new Schema(SimpleEntity, {
aString: { type: 'string' },
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/schema/schema-fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe("Schema", () => {
beforeEach(() => {
let entityData: EntityData = {};
entityData[data.providedEntityFieldName] = data.providedEntityFieldValue;
entity = new TestEntity('foo', entityData)
entity = new TestEntity(data.schemaDef, 'foo', entityData)
});

it("adds the getter and setter for the field from the schema definition to the entity", () => {
Expand Down

0 comments on commit ca862b2

Please sign in to comment.