-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add polymorphic serializer #66
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { DataDocument } from '../interfaces/json-api.interface'; | ||
import { SerializerOptions } from '../interfaces/serializer.interface'; | ||
import ResourceIdentifier from '../models/resource-identifier.model'; | ||
import Resource from '../models/resource.model'; | ||
import { Dictionary, nullish, SingleOrArray } from '../types/global.types'; | ||
import { Helpers } from '../utils/serializer.utils'; | ||
import Relator from './relator'; | ||
import Serializer from './serializer'; | ||
|
||
export default class PolymorphicSerializer< | ||
PrimaryType extends Dictionary<any> | ||
> extends Serializer<PrimaryType> { | ||
private serialisers: Record<string, Serializer>; | ||
|
||
private key: keyof PrimaryType; | ||
|
||
public constructor( | ||
commonName: string, | ||
key: keyof PrimaryType, | ||
serializers: Record<string, Serializer> | ||
) { | ||
super(commonName); | ||
this.serialisers = serializers; | ||
this.key = key; | ||
} | ||
|
||
public async serialize( | ||
data: SingleOrArray<PrimaryType> | nullish, | ||
options?: Partial<SerializerOptions<PrimaryType>> | ||
): Promise<Partial<DataDocument<PrimaryType>>> { | ||
if (Array.isArray(data)) { | ||
data.map((d) => { | ||
return this.serializeSingle(d, options); | ||
}); | ||
} else if (data) { | ||
return this.serializeSingle(data, options); | ||
} | ||
|
||
return Object.values(this.serialisers)[0].serialize(data, options); | ||
} | ||
|
||
public createIdentifier( | ||
data: PrimaryType, | ||
options?: SerializerOptions<PrimaryType> | ||
): ResourceIdentifier { | ||
const serializer = this.getSerializerForData(data); | ||
if (serializer) { | ||
return serializer.createIdentifier(data, options); | ||
} | ||
return super.createIdentifier(data, options); | ||
} | ||
|
||
public async createResource( | ||
data: PrimaryType, | ||
options?: Partial<SerializerOptions<PrimaryType>>, | ||
helpers?: Helpers<PrimaryType>, | ||
relatorDataCache?: Map<Relator<any>, Dictionary<any>[]> | ||
): Promise<Resource<PrimaryType>> { | ||
const serializer = this.getSerializerForData(data); | ||
if (serializer) { | ||
return serializer.createResource(data, options, helpers, relatorDataCache); | ||
} | ||
return super.createResource(data, options, helpers, relatorDataCache); | ||
} | ||
|
||
private async serializeSingle( | ||
data: PrimaryType, | ||
options?: Partial<SerializerOptions<PrimaryType>> | ||
) { | ||
const serializer = this.getSerializerForData(data); | ||
if (serializer) { | ||
return serializer.serialize(data, options); | ||
} | ||
return super.serialize(data, options); | ||
} | ||
|
||
private getSerializerForData(data: PrimaryType): Serializer | null { | ||
if (this.serialisers[data[this.key]]) { | ||
return this.serialisers[data[this.key]]; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { PolymorphicSerializer, Relator, Serializer } from '../lib'; | ||
import ResourceIdentifier from '../lib/models/resource-identifier.model'; | ||
import Resource from '../lib/models/resource.model'; | ||
|
||
describe('Issue #65 - Polymorphic relator', () => { | ||
class Model { | ||
id: string; | ||
|
||
children: Child[]; | ||
} | ||
|
||
abstract class Child { | ||
public type: string; | ||
|
||
constructor(public id: string) {} | ||
} | ||
|
||
class Child1 extends Child { | ||
constructor(id: string, public child1: string) { | ||
super(id); | ||
this.type = 'type:Child1'; | ||
} | ||
} | ||
|
||
class Child2 extends Child { | ||
constructor(id: string, public child2: string) { | ||
super(id); | ||
this.type = 'type:Child2'; | ||
} | ||
} | ||
|
||
class Child3 extends Child { | ||
constructor(id: string, public child2: string) { | ||
super(id); | ||
this.type = 'type:Child3'; | ||
} | ||
} | ||
|
||
it('should work non-polymorphicly', async () => { | ||
const model: Model = new Model(); | ||
const child1: Child1 = new Child1('1', 'child1'); | ||
const child2: Child2 = new Child2('2', 'child2'); | ||
|
||
model.id = '1'; | ||
model.children = [child1, child2]; | ||
|
||
const Child1Serializer = new Serializer<Child>('Child'); | ||
|
||
const relator = new Relator<Model, Child>( | ||
async (obj) => obj.children, | ||
() => Child1Serializer, | ||
{ relatedName: 'children' } | ||
); | ||
|
||
const ModelSerializer = new Serializer<Model>('Model', { | ||
relators: [relator], | ||
projection: { | ||
children: 0, | ||
}, | ||
}); | ||
|
||
const data = (await ModelSerializer.serialize(model)) as { data: Resource<Model> }; | ||
|
||
expect(data.data).toBeInstanceOf(Resource); | ||
expect(data.data.relationships?.children.data).toHaveLength(2); | ||
expect(data.data.relationships?.children.data?.[0].id).toEqual('1'); | ||
expect(data.data.relationships?.children.data?.[1].id).toEqual('2'); | ||
|
||
expect(data.data.relationships?.children.data?.[0].type).toEqual('Child'); | ||
expect(data.data.relationships?.children.data?.[1].type).toEqual('Child'); | ||
}); | ||
|
||
it('should work polymorphicly', async () => { | ||
const model: Model = new Model(); | ||
const child1: Child1 = new Child1('1', 'child1'); | ||
const child2: Child2 = new Child2('2', 'child2'); | ||
const child3: Child2 = new Child3('3', 'child3'); | ||
|
||
model.id = '1'; | ||
model.children = [child1, child2, child3]; | ||
|
||
const Child1Serializer = new Serializer<Child1>('Child1'); | ||
const Child2Serializer = new Serializer<Child2>('Child2'); | ||
|
||
const PolySerializer = new PolymorphicSerializer<Child>('Child', 'type', { | ||
'type:Child1': Child1Serializer, | ||
'type:Child2': Child2Serializer, | ||
}); | ||
|
||
const relator = new Relator<Model, Child>(async (obj) => obj.children, PolySerializer, { | ||
relatedName: 'children', | ||
}); | ||
|
||
const ModelSerializer = new Serializer<Model>('Model', { | ||
relators: [relator], | ||
projection: { | ||
children: 0, | ||
}, | ||
}); | ||
|
||
const data = (await ModelSerializer.serialize(model)) as { data: Resource<Model> }; | ||
|
||
expect(data.data).toBeInstanceOf(Resource); | ||
expect(data.data.relationships?.children.data).toHaveLength(3); | ||
expect(data.data.relationships?.children.data?.[0].id).toEqual('1'); | ||
expect(data.data.relationships?.children.data?.[1].id).toEqual('2'); | ||
expect(data.data.relationships?.children.data?.[2].id).toEqual('3'); | ||
|
||
expect(data.data.relationships?.children.data?.[0].type).toEqual('Child1'); | ||
expect(data.data.relationships?.children.data?.[1].type).toEqual('Child2'); | ||
expect(data.data.relationships?.children.data?.[2].type).toEqual('Child'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, congratulations on your first child! I saw you replied on one of the issues and I wanted to congratulate you :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Still getting used to the change in sleep patterns, but loving it :D