Comparing version
@@ -1,8 +0,10 @@ | ||
import { IJsonPropertiesMapper, TJsonaModel, TJsonaRelationships, TJsonApiBody, TJsonApiData, IJsonaModelBuilder } from '../JsonaTypes'; | ||
import { IJsonPropertiesMapper, TJsonaModel, TJsonaRelationships, TJsonApiBody, TJsonApiData, IJsonaModelBuilder, IDeserializeCache } from '../JsonaTypes'; | ||
declare class JsonDeserializer implements IJsonaModelBuilder { | ||
protected pm: IJsonPropertiesMapper; | ||
protected dc: IDeserializeCache; | ||
protected body: any; | ||
protected includedInObject: any; | ||
protected cachedModels: {}; | ||
constructor(propertiesMapper: any); | ||
constructor(propertiesMapper: any, deserializeCache: any); | ||
setDeserializeCache(dc: any): void; | ||
setPropertiesMapper(pm: any): void; | ||
@@ -9,0 +11,0 @@ setJsonParsedObject(body: TJsonApiBody): void; |
@@ -10,6 +10,10 @@ "use strict"; | ||
var JsonDeserializer = /** @class */ (function () { | ||
function JsonDeserializer(propertiesMapper) { | ||
function JsonDeserializer(propertiesMapper, deserializeCache) { | ||
this.cachedModels = {}; | ||
this.setPropertiesMapper(propertiesMapper); | ||
this.setDeserializeCache(deserializeCache); | ||
} | ||
JsonDeserializer.prototype.setDeserializeCache = function (dc) { | ||
this.dc = dc; | ||
}; | ||
JsonDeserializer.prototype.setPropertiesMapper = function (pm) { | ||
@@ -42,19 +46,9 @@ this.pm = pm; | ||
JsonDeserializer.prototype.buildModelByData = function (data) { | ||
var entityKey = createEntityKey(data); | ||
var model; | ||
var onlyTypeIdInData = Object.keys(data).length === 2 && data.type && data.id; | ||
if (entityKey && onlyTypeIdInData) { | ||
// checks for built model in cachedModels is a protection from creating models on recursive relationships | ||
// NOTE: onlyTypeIdInData need for prevent return empty, cached model (for collections with recursive relations) | ||
// https://github.com/olosegres/jsona/issues/17 | ||
model = this.cachedModels[entityKey]; | ||
if (model) { | ||
return model; | ||
} | ||
var cachedModel = this.dc.getCachedModel(data); | ||
if (cachedModel) { | ||
return cachedModel; | ||
} | ||
model = this.pm.createModel(data.type); | ||
var model = this.pm.createModel(data.type); | ||
this.dc.handleModel(model, data); // should be called before this.pm.setRelationships(model, relationships); | ||
if (model) { | ||
if (entityKey) { | ||
this.cachedModels[entityKey] = model; | ||
} | ||
this.pm.setId(model, data.id); | ||
@@ -61,0 +55,0 @@ if (data.attributes) { |
@@ -1,8 +0,10 @@ | ||
import { IModelPropertiesMapper, IJsonPropertiesMapper, TJsonaDenormalizedIncludeNames, TJsonaNormalizedIncludeNamesTree, TJsonaModel, TJsonApiBody, TReduxObject } from './JsonaTypes'; | ||
import { IModelPropertiesMapper, IJsonPropertiesMapper, TJsonaDenormalizedIncludeNames, TJsonaNormalizedIncludeNamesTree, TJsonaModel, TJsonApiBody, TReduxObject, IDeserializeCacheConstructor } from './JsonaTypes'; | ||
declare class Jsona { | ||
modelPropertiesMapper: IModelPropertiesMapper; | ||
jsonPropertiesMapper: IJsonPropertiesMapper; | ||
DeserializeCache: IDeserializeCacheConstructor; | ||
constructor(params?: { | ||
modelPropertiesMapper: IModelPropertiesMapper; | ||
jsonPropertiesMapper: IJsonPropertiesMapper; | ||
modelPropertiesMapper?: IModelPropertiesMapper; | ||
jsonPropertiesMapper?: IJsonPropertiesMapper; | ||
DeserializeCache?: IDeserializeCacheConstructor; | ||
}); | ||
@@ -9,0 +11,0 @@ /** |
@@ -8,2 +8,3 @@ "use strict"; | ||
var simplePropertyMappers_1 = require("./simplePropertyMappers"); | ||
var cache_1 = require("./cache"); | ||
var Jsona = /** @class */ (function () { | ||
@@ -13,2 +14,3 @@ function Jsona(params) { | ||
this.jsonPropertiesMapper = new simplePropertyMappers_1.JsonPropertiesMapper(); | ||
this.DeserializeCache = cache_1.DeserializeCache; | ||
if (params && params.modelPropertiesMapper) { | ||
@@ -20,2 +22,5 @@ this.modelPropertiesMapper = params.modelPropertiesMapper; | ||
} | ||
if (params && params.DeserializeCache) { | ||
this.DeserializeCache = params.DeserializeCache; | ||
} | ||
} | ||
@@ -46,3 +51,4 @@ /** | ||
} | ||
var modelBuilder = new JsonDeserializer_1.default(this.jsonPropertiesMapper); | ||
var deserializeCache = new this.DeserializeCache(); | ||
var modelBuilder = new JsonDeserializer_1.default(this.jsonPropertiesMapper, deserializeCache); | ||
if (typeof body === 'string') { | ||
@@ -49,0 +55,0 @@ modelBuilder.setJsonParsedObject(utils_1.jsonParse(body)); |
@@ -20,2 +20,10 @@ export interface IModelPropertiesMapper { | ||
} | ||
export interface IDeserializeCache { | ||
getCachedModel(data: TJsonApiData): TJsonaModel | null; | ||
handleModel(model: TJsonaModel, data: TJsonApiData): void; | ||
createCacheKey(data: TJsonApiData): string; | ||
} | ||
export interface IDeserializeCacheConstructor { | ||
new (): IDeserializeCache; | ||
} | ||
export declare type TAnyKeyValueObject = { | ||
@@ -22,0 +30,0 @@ [key: string]: any; |
{ | ||
"name": "jsona", | ||
"description": "Provide data formatters (data model builder & json builder) to work with JSON API specification v1.0 in your JavaScript / TypeScript code", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "json-api", |
@@ -8,2 +8,3 @@ import { | ||
IJsonaModelBuilder, | ||
IDeserializeCache, | ||
} from '../JsonaTypes'; | ||
@@ -22,2 +23,3 @@ | ||
protected pm: IJsonPropertiesMapper; | ||
protected dc: IDeserializeCache; | ||
protected body; | ||
@@ -27,6 +29,11 @@ protected includedInObject; | ||
constructor(propertiesMapper) { | ||
constructor(propertiesMapper, deserializeCache) { | ||
this.setPropertiesMapper(propertiesMapper); | ||
this.setDeserializeCache(deserializeCache); | ||
} | ||
setDeserializeCache(dc): void { | ||
this.dc = dc; | ||
} | ||
setPropertiesMapper(pm): void { | ||
@@ -65,26 +72,13 @@ this.pm = pm; | ||
buildModelByData(data: TJsonApiData): TJsonaModel { | ||
const entityKey = createEntityKey(data); | ||
const cachedModel = this.dc.getCachedModel(data); | ||
let model; | ||
if (cachedModel) { | ||
return cachedModel; | ||
} | ||
const onlyTypeIdInData = Object.keys(data).length === 2 && data.type && data.id; | ||
const model = this.pm.createModel(data.type); | ||
if (entityKey && onlyTypeIdInData) { | ||
// checks for built model in cachedModels is a protection from creating models on recursive relationships | ||
// NOTE: onlyTypeIdInData need for prevent return empty, cached model (for collections with recursive relations) | ||
// https://github.com/olosegres/jsona/issues/17 | ||
model = this.cachedModels[entityKey]; | ||
this.dc.handleModel(model, data); // should be called before this.pm.setRelationships(model, relationships); | ||
if (model) { | ||
return model; | ||
} | ||
} | ||
model = this.pm.createModel(data.type); | ||
if (model) { | ||
if (entityKey) { | ||
this.cachedModels[entityKey] = model; | ||
} | ||
this.pm.setId(model, data.id); | ||
@@ -91,0 +85,0 @@ |
@@ -8,3 +8,4 @@ import { | ||
TJsonApiBody, | ||
TReduxObject | ||
TReduxObject, | ||
IDeserializeCacheConstructor, | ||
} from './JsonaTypes'; | ||
@@ -22,2 +23,4 @@ | ||
import { DeserializeCache } from './cache'; | ||
class Jsona { | ||
@@ -27,6 +30,8 @@ | ||
public jsonPropertiesMapper: IJsonPropertiesMapper = new JsonPropertiesMapper(); | ||
public DeserializeCache: IDeserializeCacheConstructor = DeserializeCache; | ||
constructor(params?: { | ||
modelPropertiesMapper: IModelPropertiesMapper, | ||
jsonPropertiesMapper: IJsonPropertiesMapper | ||
modelPropertiesMapper?: IModelPropertiesMapper, | ||
jsonPropertiesMapper?: IJsonPropertiesMapper, | ||
DeserializeCache?: IDeserializeCacheConstructor, | ||
}) { | ||
@@ -39,2 +44,5 @@ if (params && params.modelPropertiesMapper) { | ||
} | ||
if (params && params.DeserializeCache) { | ||
this.DeserializeCache = params.DeserializeCache; | ||
} | ||
} | ||
@@ -76,3 +84,4 @@ | ||
const modelBuilder = new JsonDeserializer(this.jsonPropertiesMapper); | ||
const deserializeCache = new this.DeserializeCache(); | ||
const modelBuilder = new JsonDeserializer(this.jsonPropertiesMapper, deserializeCache); | ||
@@ -79,0 +88,0 @@ if (typeof body === 'string') { |
@@ -24,2 +24,12 @@ | ||
export interface IDeserializeCache { | ||
getCachedModel(data: TJsonApiData): TJsonaModel | null; | ||
handleModel(model: TJsonaModel, data: TJsonApiData): void; | ||
createCacheKey(data: TJsonApiData): string; | ||
} | ||
export interface IDeserializeCacheConstructor { | ||
new(): IDeserializeCache; | ||
} | ||
export type TAnyKeyValueObject = { | ||
@@ -26,0 +36,0 @@ [key: string]: any |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
43
7.5%2101
5.58%0
-100%123445
-2.29%