Skip to content

Instantly share code, notes, and snippets.

@omatrot
Last active September 22, 2021 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omatrot/1a8f5bc467594fa664c00f1de5d7a3e2 to your computer and use it in GitHub Desktop.
Save omatrot/1a8f5bc467594fa664c00f1de5d7a3e2 to your computer and use it in GitHub Desktop.
Dcerialize (Typescript) Trackable Entities Class template
{{> imports}}
export class {{class}} extends TrackableEntity {
{{{> properties}}}
{{{> constructor}}}
}
{{spaces 2}}constructor() {
{{spaces 4}}super();
{{spaces 4}}return super.proxify(this);
{{spaces 1}} }
{{spaces 2}}static onSerialized(json: IJsonObject, instance: {{class}}) {
{{spaces 4}}// Add ITrackable properties that cannot be decorated
{{spaces 4}}json["trackingState"] = instance.trackingState;
{{spaces 4}}json["modifiedProperties"] = SerializeSet<String>
{{spaces 4}}(
{{spaces 6}}Array.from(instance.modifiedProperties.values()),
{{spaces 6}}() => String
{{spaces 4}});
{{spaces 4}}// Custom processing of dates (May be empty)
{{#each properties}}
{{#ifCond property-type '==' 'Date'}}
{{spaces 4}}if (instance.{{property-name}}) {
{{spaces 6}}json["{{property-name}}"] = instance.{{property-name}}.toISOString();
{{spaces 4}} }
{{/ifCond}}
{{/each}}
{{spaces 2}} }
import { TrackableEntity } from 'trackable-entities';
{{#each imports}}
import { {{import}} } from './{{import}}';
{{/each}}
import {
autoserializeAs,
autoserializeAsArray,
SerializeSet,
IJsonObject
} from "dcerialize";
{{#each properties}}
{{#ifCond property-type '==' 'number'}}
{{spaces 2}}@autoserializeAs(() => Number)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'string'}}
{{spaces 2}}@autoserializeAs(() => String)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'boolean'}}
{{spaces 2}}@autoserializeAs(() => Boolean)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'Date'}}
{{spaces 2}}@autoserializeAs(() => Date)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'any'}}
{{spaces 2}}@autoserializeAs(() => String)
{{else}}
{{/ifCond}}
{{spaces 2}}{{property-name}}: {{#ifCond property-type '==' 'any'}}string{{else}}{{property-type}}{{/ifCond}};
{{/each}}
{{#if nav-properties}}
{{#each nav-properties}}
{{#if nav-property-collection}}
{{spaces 2}}@autoserializeAsArray(() => {{nav-property-type}})
{{spaces 2}}{{nav-property-name}}: {{nav-property-type}}[];
{{else}}
{{spaces 2}}@autoserializeAs(() => {{nav-property-type}})
{{spaces 2}}{{nav-property-name}}: {{nav-property-type}};
{{/if}}
{{/each}}
{{/if}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment