Skip to content

Commit

Permalink
Update 3.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhorin committed Mar 1, 2023
1 parent b77e746 commit a93151c
Show file tree
Hide file tree
Showing 49 changed files with 279 additions and 152 deletions.
12 changes: 6 additions & 6 deletions HISTORY.md
Expand Up @@ -2,8 +2,8 @@

* db/MongoDatabase
- remove deprecated reindexing
* upgrade dependencies
* upgrade dependencies

## 3.4.0

* helper/CommonHelper
Expand All @@ -15,14 +15,14 @@
* validator/Validator
- fix checking of active scenarios
* web/asset/AssetBundle
- fix style asset render
- fix style asset render

## 3.3.0

* web/session
- refactor session stores
* upgrade dependencies
* upgrade dependencies

## 3.2.0

* base/Configuration
Expand Down
5 changes: 3 additions & 2 deletions base/Base.js
Expand Up @@ -10,8 +10,9 @@ module.exports = class Base {

static init (nodeModule) {
if (nodeModule) {
ClassHelper.defineClassProperty(this, 'CLASS_FILE', nodeModule.filename);
ClassHelper.defineClassProperty(this, 'CLASS_DIRECTORY', path.dirname(nodeModule.filename));
const file = nodeModule.filename;
ClassHelper.defineClassProperty(this, 'CLASS_FILE', file);
ClassHelper.defineClassProperty(this, 'CLASS_DIRECTORY', path.dirname(file));
}
ClassHelper.defineConstantClassProperties(this);
return this;
Expand Down
10 changes: 8 additions & 2 deletions base/Controller.js
Expand Up @@ -64,7 +64,10 @@ module.exports = class Controller extends Base {

static getModelClass () {
if (!this.hasOwnProperty('_MODEL_CLASS')) {
const closest = FileHelper.getClosestDirectory(this.CONTROLLER_DIRECTORY, this.CLASS_DIRECTORY);
const closest = FileHelper.getClosestDirectory(
this.CONTROLLER_DIRECTORY,
this.CLASS_DIRECTORY
);
const nested = this.getNestedDirectory();
const className = this.getModelClassName();
const dir = path.join(this.MODEL_DIRECTORY, nested, className);
Expand All @@ -90,7 +93,10 @@ module.exports = class Controller extends Base {

static getNestedDirectory () {
if (!this.hasOwnProperty('_NESTED_DIRECTORY')) {
this._NESTED_DIRECTORY = FileHelper.getRelativePathByDirectory(this.CONTROLLER_DIRECTORY, this.CLASS_DIRECTORY);
this._NESTED_DIRECTORY = FileHelper.getRelativePathByDirectory(
this.CONTROLLER_DIRECTORY,
this.CLASS_DIRECTORY
);
}
return this._NESTED_DIRECTORY;
}
Expand Down
12 changes: 8 additions & 4 deletions base/FileMap.js
Expand Up @@ -41,20 +41,24 @@ module.exports = class FileMap extends Base {
}

indexDirectoryFiles (dir) {
for (const name of fs.readdirSync(dir)) {
const names = fs.readdirSync(dir);
for (const name of names) {
const file = path.join(dir, name);
if (fs.lstatSync(file).isDirectory()) {
const stat = fs.lstatSync(file);
if (stat.isDirectory()) {
this.indexDirectoryFiles(file);
} else {
this._files[this.getKey(file)] = file;
const key = this.getKey(file);
this._files[key] = file;
}
}
}

getKey (file) {
const relative = file.substring(this.directory.length + 1);
const parts = relative.split(path.sep);
parts.push(FileHelper.trimExtension(parts.pop()));
const name = parts.pop();
parts.push(FileHelper.trimExtension(name));
return parts.join('/'); // normalize separator
}

Expand Down
6 changes: 4 additions & 2 deletions base/Module.js
Expand Up @@ -289,11 +289,13 @@ module.exports = class Module extends Base {
}

beforeAction (action) {
return this.trigger(this.EVENT_BEFORE_ACTION, new ActionEvent(action));
const event = new ActionEvent(action);
return this.trigger(this.EVENT_BEFORE_ACTION, event);
}

afterAction (action) {
return this.trigger(this.EVENT_AFTER_ACTION, new ActionEvent(action));
const event = new ActionEvent(action);
return this.trigger(this.EVENT_AFTER_ACTION, event);
}

// INIT
Expand Down
3 changes: 2 additions & 1 deletion behavior/DataHistoryBehavior.js
Expand Up @@ -26,7 +26,8 @@ module.exports = class DataHistoryBehavior extends Base {
}

async beforeUpdate () {
for (const name of this.getAttrNames()) {
const names = this.getAttrNames();
for (const name of names) {
if (this.owner.isAttrChanged(name)) {
await this.createHistory(name);
}
Expand Down
9 changes: 6 additions & 3 deletions behavior/SortOrderBehavior.js
Expand Up @@ -19,8 +19,10 @@ module.exports = class SortOrderBehavior extends Base {
}

async beforeInsert () {
if (CommonHelper.isEmpty(this.owner.get(this.orderAttr))) {
this.owner.set(this.orderAttr, await this.getNextNumber());
const order = this.owner.get(this.orderAttr);
if (CommonHelper.isEmpty(order)) {
const next = await this.getNextNumber();
this.owner.set(this.orderAttr, next);
}
}

Expand All @@ -29,7 +31,8 @@ module.exports = class SortOrderBehavior extends Base {
if (typeof this.filter === 'function') {
this.filter(query, this.owner);
} else if (typeof this.filter === 'string') {
query.and({[this.filter]: this.owner.get(this.filter)});
const value = this.owner.get(this.filter);
query.and({[this.filter]: value});
}
const direction = this.step > 0 ? -1 : 1;
const order = {[this.orderAttr]: direction};
Expand Down
5 changes: 4 additions & 1 deletion cache/DatabaseCache.js
Expand Up @@ -25,7 +25,10 @@ module.exports = class DatabaseCache extends Base {
const query = this.getQuery().and({key});
const data = await query.one();
if (data) {
if (!data.expiredAt || data.expiredAt > this.constructor.getNow()) {
if (!data.expiredAt) {
return data.value;
}
if (data.expiredAt > this.constructor.getNow()) {
return data.value;
}
}
Expand Down
7 changes: 5 additions & 2 deletions data/Pagination.js
Expand Up @@ -43,7 +43,8 @@ module.exports = class Pagination extends Base {
}
this.setPageSize(this.pageSize);
if (this.page === null) {
this.page = parseInt(this.getQueryParam(this.pageParam, 1)) - 1;
const page = this.getQueryParam(this.pageParam, 1);
this.page = parseInt(page) - 1;
}
this.setPage(this.page);
}
Expand Down Expand Up @@ -128,7 +129,9 @@ module.exports = class Pagination extends Base {
} else {
delete params[this.pageParam];
}
pageSize = pageSize || this.pageSize;
if (!pageSize) {
pageSize = this.pageSize;
}
if (pageSize !== this.defaultPageSize) {
params[this.pageSizeParam] = pageSize;
} else {
Expand Down
23 changes: 15 additions & 8 deletions db/ActiveLinker.js
Expand Up @@ -93,7 +93,9 @@ module.exports = class ActiveLinker extends Base {
async unlinkAll (name, deletion) {
const relation = this.owner.getRelation(name);
if (relation) {
const method = relation.isOuterLink() ? 'unlinkViaAll' : 'unlinkInternalAll';
const method = relation.isOuterLink()
? 'unlinkViaAll'
: 'unlinkInternalAll';
await this[method](relation, deletion);
this.owner.unsetRelated(name);
}
Expand All @@ -115,7 +117,8 @@ module.exports = class ActiveLinker extends Base {
deletion ? await db.delete(via.getTable(), condition)
: await db.update(via.getTable(), condition, nulls);
} else if (deletion) {
for (const model of await via.model.find(condition).all()) {
const models = await via.model.find(condition).all();
for (const model of models) {
await model.delete();
}
} else {
Expand All @@ -125,16 +128,20 @@ module.exports = class ActiveLinker extends Base {

async unlinkInternalAll (relation) {
// relation via array valued attribute
if (Array.isArray(this.owner.get(relation.linkKey))) {
const link = this.owner.get(relation.linkKey);
if (Array.isArray(link)) {
return this.owner.directUpdate({[relation.linkKey]: []});
}
let condition = {[relation.refKey]: this.owner.get(relation.linkKey)};
let condition = {[relation.refKey]: link};
if (relation.getWhere()) {
condition = ['and', condition, relation.getWhere()];
}
relation.getViaArray()
? await relation.model.getDb().updateAllPull(relation.model.getTable(), {}, condition)
: await relation.model.find(condition).updateAll({[relation.refKey]: null});
if (relation.getViaArray()) {
const table = relation.model.getTable();
await relation.model.getDb().updateAllPull(table, {}, condition);
} else {
await relation.model.find(condition).updateAll({[relation.refKey]: null});
}
}

unsetUnlinked (name, model, relation) {
Expand All @@ -144,7 +151,7 @@ module.exports = class ActiveLinker extends Base {
const models = this.owner.getRelated(name);
if (Array.isArray(models)) {
const id = model.getId();
const result = models.filter(target => !CommonHelper.isEqual(id, target.getId()));
const result = models.filter(m => !CommonHelper.isEqual(id, m.getId()));
this.owner.populateRelation(name, result);
}
}
Expand Down
7 changes: 4 additions & 3 deletions db/ActiveQuery.js
Expand Up @@ -272,9 +272,10 @@ module.exports = class ActiveQuery extends Base {
return true;
}
const link = this.primaryModel.get(this.linkKey);
return link !== null
&& link !== undefined
&& (!Array.isArray(link) || link.length);
if (link === null || link === undefined) {
return false;
}
return !Array.isArray(link) || link.length;
}

// POPULATE
Expand Down
22 changes: 15 additions & 7 deletions db/ActiveRecord.js
Expand Up @@ -202,23 +202,27 @@ module.exports = class ActiveRecord extends Base {

async insert () {
await this.beforeSave(true);
this.set(this.PK, await this.createQuery().insert(this.filterAttrs()));
const data = this.filterAttrs();
const id = await this.createQuery().insert(data);
this.set(this.PK, id);
this._isNew = false;
await this.afterSave(true);
this.assignOldAttrs();
}

async update () {
await this.beforeSave(false);
await this.findSelf().update(this.filterAttrs());
const data = this.filterAttrs();
await this.findSelf().update(data);
await this.afterSave(false);
this.assignOldAttrs();
}

// skip before and after save triggers
async directUpdate (data) {
this.assign(data);
await this.findSelf().update(this.filterAttrs());
data = this.filterAttrs();
await this.findSelf().update(data);
this.assignOldAttrs();
}

Expand All @@ -244,15 +248,17 @@ module.exports = class ActiveRecord extends Base {
static async resolveRelation (name, models) {
const relations = [];
for (const model of models) {
relations.push(await model.resolveRelation(name));
const result = await model.resolveRelation(name);
relations.push(result);
}
return relations;
}

static async resolveRelations (names, models) {
const relations = [];
for (const model of models) {
relations.push(await model.resolveRelations(names));
const result = await model.resolveRelations(names);
relations.push(result);
}
return relations;
}
Expand Down Expand Up @@ -372,7 +378,8 @@ module.exports = class ActiveRecord extends Base {
const models = [];
for (const model of result) {
if (typeof model?.resolveRelation === 'function') {
models.push(await model.resolveRelation(nestedName));
const items = await model.resolveRelation(nestedName);
models.push(items);
}
}
return ArrayHelper.concat(models);
Expand All @@ -397,7 +404,8 @@ module.exports = class ActiveRecord extends Base {
async resolveRelations (names) {
const result = [];
for (const name of names) {
result.push(await this.resolveRelation(name));
const data = await this.resolveRelation(name);
result.push(data);
}
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion db/Expression.js
Expand Up @@ -16,7 +16,8 @@ module.exports = class Expression {
if (this.params) {
for (const key of Object.keys(this.params)) {
const regex = new RegExp(`:${key}`, 'g');
this._value = this._value.replace(regex, db.escape(this.params[key]));
const value = db.escape(this.params[key]);
this._value = this._value.replace(regex, value);
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions db/MongoDatabase.js
Expand Up @@ -39,7 +39,8 @@ module.exports = class MongoDatabase extends Base {
}

async isTableExists (name) {
for (const {collectionName} of await this._connection.collections()) {
const collections = await this._connection.collections();
for (const {collectionName} of collections) {
if (name === collectionName) {
return true;
}
Expand All @@ -48,7 +49,8 @@ module.exports = class MongoDatabase extends Base {

async getTableNames () {
const names = [];
for (const {collectionName} of await this._connection.collections()) {
const collections = await this._connection.collections();
for (const {collectionName} of collections) {
names.push(collectionName);
}
return names;
Expand Down Expand Up @@ -100,7 +102,10 @@ module.exports = class MongoDatabase extends Base {

upsert (table, query, data, options) {
this.traceCommand('upsert', {table, query, data});
return this.getTable(table).updateOne(query, {$set: data}, {upsert: true, ...options});
return this.getTable(table).updateOne(query, {$set: data}, {
upsert: true,
...options
});
}

update (table, query, data, options) {
Expand Down Expand Up @@ -147,7 +152,8 @@ module.exports = class MongoDatabase extends Base {

async dropAll () {
this.traceCommand('dropAll');
for (const name of await this.getTableNames()) {
const names = await this.getTableNames();
for (const name of names) {
await this.drop(name);
}
}
Expand Down
4 changes: 3 additions & 1 deletion filter/AccessControl.js
Expand Up @@ -29,7 +29,9 @@ module.exports = class AccessControl extends Base {
createRules () {
const rules = [];
for (const config of this.rules) {
config.Class = config.Class || this.AccessRule;
if (!config.Class) {
config.Class = this.AccessRule;
}
rules.push(this.spawn(config));
}
return rules;
Expand Down
6 changes: 4 additions & 2 deletions filter/AccessRule.js
Expand Up @@ -30,12 +30,14 @@ module.exports = class AccessRule extends Base {
}
}
if (this.methods) {
if (!this.methods.includes(action.controller.req.method?.toLowerCase())) {
const name = action.controller.req.method?.toLowerCase();
if (!this.methods.includes(name)) {
return;
}
}
if (this.controllers) {
if (!this.controllers.includes(action.controller.getBaseName())) {
const name = action.controller.getBaseName();
if (!this.controllers.includes(name)) {
return;
}
}
Expand Down

0 comments on commit a93151c

Please sign in to comment.