From b8505ee656bd46164a80822d3f47922fb337adbf Mon Sep 17 00:00:00 2001 From: Amit Meshbey Date: Sat, 10 Apr 2021 22:35:57 +0300 Subject: [PATCH] formatJson options --- doc/api/model/instance-methods.md | 11 ++++++----- lib/model/Model.js | 2 +- lib/model/modelToJson.js | 3 ++- typings/objection/index.d.ts | 3 ++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/doc/api/model/instance-methods.md b/doc/api/model/instance-methods.md index cc63bebfc..64604f39d 100644 --- a/doc/api/model/instance-methods.md +++ b/doc/api/model/instance-methods.md @@ -747,9 +747,9 @@ There are a couple of requirements for the implementation: ```js class Person extends Model { - $formatJson(json) { + $formatJson(json, options) { // Remember to call the super class's implementation. - json = super.$formatJson(json); + json = super.$formatJson(json, options); // Do your conversion here. return json; } @@ -768,9 +768,10 @@ There are a couple of requirements for the implementation: ##### Arguments -| Argument | Type | Description | -| -------- | ------ | -------------------------------- | -| json | Object | The JSON POJO in internal format | +| Argument | Type | Description | +| -------- | ------ | -------------------------------------------------------- | +| json | Object | The JSON POJO in internal format | +| options | Object | Formatting options from toJSON's options.format property | ##### Return value diff --git a/lib/model/Model.js b/lib/model/Model.js index 336957a9e..4b01ff846 100644 --- a/lib/model/Model.js +++ b/lib/model/Model.js @@ -127,7 +127,7 @@ class Model { return json; } - $formatJson(json) { + $formatJson(json, options) { return json; } diff --git a/lib/model/modelToJson.js b/lib/model/modelToJson.js index 1de996e3d..73956459f 100644 --- a/lib/model/modelToJson.js +++ b/lib/model/modelToJson.js @@ -15,10 +15,11 @@ function toJson(model, optIn) { pick: null, omitFromJson: model.$omitFromJson() || null, cloneObjects: modelClass.cloneObjectAttributes, + format: optIn && optIn.format }; let json = toExternalJsonImpl(model, opt); - json = model.$formatJson(json); + json = model.$formatJson(json, opt.format); return json; } diff --git a/typings/objection/index.d.ts b/typings/objection/index.d.ts index dc45ac9a6..faf6f1c29 100644 --- a/typings/objection/index.d.ts +++ b/typings/objection/index.d.ts @@ -1267,6 +1267,7 @@ declare namespace Objection { export interface ToJsonOptions extends CloneOptions { virtuals?: boolean | string[]; + format?: Pojo } export interface ValidatorContext { @@ -1695,7 +1696,7 @@ declare namespace Objection { $formatDatabaseJson(json: Pojo): Pojo; $parseDatabaseJson(json: Pojo): Pojo; - $formatJson(json: Pojo): Pojo; + $formatJson(json: Pojo, opt?: Pojo): Pojo; $parseJson(json: Pojo, opt?: ModelOptions): Pojo; $beforeValidate(jsonSchema: JSONSchema, json: Pojo, opt: ModelOptions): JSONSchema;