Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"plugin:node/recommended"
],
"parserOptions": {
"ecmaVersion": 2015,
"ecmaVersion": 2018,
"sourceType": "module"
},
"env": {
Expand Down
42 changes: 40 additions & 2 deletions OnJsonAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,52 @@ class OnJsonAttribute {
* @returns void
*/
static afterSelect(event, callback) {
const jsonAttributes = event.model.attributes.filter((attr) => {
return attr.type === 'Json' && attr.additionalType != null && attr.model === event.model.name;
const anyJsonAttributes = event.model.attributes.filter((attr) => {
return attr.type === 'Json' && attr.model === event.model.name;
});
if (anyJsonAttributes.length === 0) {
return callback();
}
const jsonAttributes = anyJsonAttributes.filter((attr) => {
return attr.additionalType != null;
}).map((attr) => {
return attr.name
});
// if there are no json attributes with additional type
if (jsonAttributes.length === 0) {
// get json attributes with no additional type
const unknownJsonAttributes = anyJsonAttributes.filter((attr) => {
return attr.additionalType == null;
}).map((attr) => {
return attr.name
});
// parse json for each item
if (unknownJsonAttributes.length > 0) {
const parseUnknownJson = (item) => {
unknownJsonAttributes.forEach((name) => {
if (Object.prototype.hasOwnProperty.call(item, name)) {
const value = item[name];
if (typeof value === 'string') {
item[name] = JSON.parse(value);
}
}
});
};
// iterate over result
const {result} = event;
if (result == null) {
return callback();
}
if (Array.isArray(result)) {
result.forEach((item) => parseUnknownJson(item));
} else {
// or parse json for single item
parseUnknownJson(result)
}
}
return callback();
}

let select = [];
const { viewAdapter: entity } = event.model;
if (event.emitter && event.emitter.query && event.emitter.query.$select) {
Expand Down
4 changes: 2 additions & 2 deletions data-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var {DataConfigurationStrategy} = require('./data-configuration');
var cfg = require('./data-configuration');
var Symbol = require('symbol');
var nameProperty = Symbol('name');
var { DataModel } = require('./data-model');

/**
* @classdesc Represents the default data context of MOST Data Applications.
Expand Down Expand Up @@ -171,8 +172,7 @@ DefaultDataContext.prototype.model = function(name) {
var obj = self.getConfiguration().getStrategy(DataConfigurationStrategy).model(modelName);
if (_.isNil(obj))
return null;
var DataModel = require('./data-model').DataModel,
model = new DataModel(obj);
var model = new DataModel(obj);
//set model context
model.context = self;
//return model
Expand Down
6 changes: 3 additions & 3 deletions data-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var {TraceUtils} = require('@themost/common');
var {TextUtils} = require('@themost/common');
var {DataCacheStrategy} = require('./data-cache');
var {DataFieldQueryResolver} = require('./data-field-query-resolver');
var functions = require('./functions');

/**
* @classdesc Represents an event listener for validating not nullable fields. This listener is automatically registered in all data models.
Expand Down Expand Up @@ -222,8 +223,7 @@ function CalculatedValueListener() {
*/
CalculatedValueListener.prototype.beforeSave = function(event, callback) {
//get function context
var functions = require('./functions'),
functionContext = functions.createContext();
var functionContext = functions.createContext();
_.assign(functionContext, event);
functionContext.context = event.model.context;
//find all attributes that have a default value
Expand Down Expand Up @@ -555,7 +555,7 @@ DefaultValueListener.prototype.beforeSave = function(event, callback) {
}
else {
//get function context
var functions = require('./functions'), functionContext = functions.createContext();
var functionContext = functions.createContext();
_.assign(functionContext, event);
//find all attributes that have a default value
var attrs = event.model.attributes.filter(function(x) { return (typeof x.value!== 'undefined'); });
Expand Down
18 changes: 12 additions & 6 deletions data-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require('@themost/promise-sequence');
var DataObjectState = types.DataObjectState;
var { OnJsonAttribute } = require('./OnJsonAttribute');
var { isObjectDeep } = require('./is-object');
var { DataStateValidatorListener } = require('./data-state-validator');
/**
* @this DataModel
* @param {DataField} field
Expand Down Expand Up @@ -616,8 +617,7 @@ function unregisterContextListeners() {
var DataCachingListener = dataListeners.DataCachingListener;
var DataModelCreateViewListener = dataListeners.DataModelCreateViewListener;
var DataModelSeedListener = dataListeners.DataModelSeedListener;
var DataStateValidatorListener = require('./data-state-validator').DataStateValidatorListener;


//1. State validator listener
this.on('before.save', DataStateValidatorListener.prototype.beforeSave);
this.on('before.remove', DataStateValidatorListener.prototype.beforeRemove);
Expand Down Expand Up @@ -710,9 +710,10 @@ DataModel.prototype.join = function(model) {
* @param {String|*} attr - A string that represents the name of a field
* @returns DataQueryable
*/
// eslint-disable-next-line no-unused-vars
DataModel.prototype.where = function(attr) {
var result = new DataQueryable(this);
return result.where(attr);
return result.where.apply(result, Array.from(arguments));
};

/**
Expand Down Expand Up @@ -1508,7 +1509,13 @@ function cast_(obj, state) {
if (mapping == null) {
var {[name]: value} = obj;
if (x.type === 'Json') {
result[x.name] = isObjectDeep(value) ? JSON.stringify(value) : null;
if (value != null) {
// set json value
result[x.name] = typeof value === 'string' ? value : JSON.stringify(value);
} else {
// set null value
result[x.name] = value;
}
} else {
result[x.name] = value;
}
Expand Down Expand Up @@ -1758,8 +1765,7 @@ DataModel.prototype.save = function(obj, callback)
* @see DataObjectState
*/
DataModel.prototype.inferState = function(obj, callback) {
var self = this,
DataStateValidatorListener = require('./data-state-validator').DataStateValidatorListener;
var self = this;
var e = { model:self, target:obj };
DataStateValidatorListener.prototype.beforeSave(e, function(err) {
//if error return error
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ module.exports = {

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
'^@themost/data/platform-server$': [
'<rootDir>/platform-server/index'
],
'^@themost/data$': [
'<rootDir>/index'
]
Expand Down
55 changes: 54 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@themost/promise-sequence": "^1.0.1",
"ajv": "^8.17.1",
"async": "^2.6.4",
"crypto-js": "^4.2.0",
"lodash": "^4.17.21",
"node-cache": "^5.1.2",
"pluralize": "^7.0.0",
Expand All @@ -44,6 +45,7 @@
"@themost/common": "^2.5.11",
"@themost/json-logger": "^1.1.0",
"@themost/peers": "^1.0.2",
"@themost/pool": "^2.10.1",
"@themost/query": "^2.6.73",
"@themost/sqlite": "^2.8.4",
"@themost/xml": "^2.5.2",
Expand Down
Loading