Skip to content

Commit

Permalink
upgrade context finalize process (#201)
Browse files Browse the repository at this point in the history
* upgrade data context finalize process

* 2.18.2
  • Loading branch information
kbarbounakis authored Feb 3, 2025
1 parent 919e0f3 commit 2389e94
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 51 deletions.
98 changes: 56 additions & 42 deletions data-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ var { DataModel } = require('./data-model');
function DefaultDataContext()
{
/**
* @type {DataAdapter|*}
* @type {import('./types').DataAdapter}
*/
var db_= null;
var _db= null;
/**
* @name DataAdapter#hasConfiguration
* @type {Function}
Expand All @@ -51,10 +51,15 @@ function DefaultDataContext()
/**
* @private
*/
this.finalize_ = function() {
if (db_)
db_.close();
db_=null;
this._finalize = function(cb) {
if (_db) {
return _db.close(function(err) {
// destroy db context
_db = null;
return cb(err);
});
}
return cb();
};
var self = this;
// set data context name with respect to DataContext implementation
Expand All @@ -69,8 +74,8 @@ function DefaultDataContext()

self.getDb = function() {

if (db_)
return db_;
if (_db)
return _db;
var er;
//otherwise load database options from configuration
var strategy = self.getConfiguration().getStrategy(DataConfigurationStrategy);
Expand All @@ -96,31 +101,31 @@ function DefaultDataContext()
var AdapterTypeCtor = adapterType.type;
// create adapter instance
if (typeof AdapterTypeCtor === 'function') {
db_ = new AdapterTypeCtor(adapter.options);
_db = new AdapterTypeCtor(adapter.options);
} else if (typeof createInstance === 'function') {
db_ = createInstance(adapter.options);
_db = createInstance(adapter.options);
} else {
// throw error
var err = new Error('The given adapter type is invalid. Adapter type constructor is undefined.');
err.code = 'ERR_ADAPTER_TYPE';
throw err;
}
if (typeof db_.hasConfiguration === 'function') {
db_.hasConfiguration(function() {
if (typeof _db.hasConfiguration === 'function') {
_db.hasConfiguration(function() {
return self.getConfiguration();
});
}
return db_;
return _db;
};

self.setDb = function(value) {
/**
* @type {DataAdapter|*}
*/
db_ = value;
if (db_) {
if (typeof db_.hasConfiguration === 'function') {
db_.hasConfiguration(function() {
_db = value;
if (_db) {
if (typeof _db.hasConfiguration === 'function') {
_db.hasConfiguration(function() {
return self.getConfiguration();
});
}
Expand Down Expand Up @@ -192,8 +197,13 @@ DefaultDataContext.prototype.model = function(name) {
*/
DefaultDataContext.prototype.finalize = function(cb) {
cb = cb || function () {};
this.finalize_();
cb.call(this);
void this._finalize(function(err) {
if (err) {
TraceUtils.error('An error occurred while finalizing the underlying database context.');
TraceUtils.error(err);
}
return cb();
});
};


Expand All @@ -212,27 +222,26 @@ function NamedDataContext(name)
* @type {DataAdapter}
* @private
*/
var db_;
var _db;
/**
* @private
*/
this.finalize_ = function() {
try {
if (db_)
db_.close();
}
catch(err) {
TraceUtils.debug('An error occurred while closing the underlying database context.');
TraceUtils.debug(err);
this._finalize = function(cb) {
if (_db) {
return _db.close(function(err) {
// destroy db context
_db = null;
return cb(err);
});
}
db_ = null;
return cb();
};
var self = this;
self[nameProperty] = name;

self.getDb = function() {
if (db_)
return db_;
if (_db)
return _db;
var strategy = self.getConfiguration().getStrategy(DataConfigurationStrategy);
//otherwise load database options from configuration
var adapter = strategy.adapters.find(function(x) {
Expand All @@ -251,31 +260,31 @@ function NamedDataContext(name)
var AdapterTypeCtor = adapterType.type;
// create adapter instance
if (typeof AdapterTypeCtor === 'function') {
db_ = new AdapterTypeCtor(adapter.options);
_db = new AdapterTypeCtor(adapter.options);
} else if (typeof createInstance === 'function') {
db_ = createInstance(adapter.options);
_db = createInstance(adapter.options);
} else {
// throw error
var err = new Error('The given adapter type is invalid. Adapter type constructor is undefined.');
err.code = 'ERR_ADAPTER_TYPE';
throw err;
}
if (typeof db_.hasConfiguration === 'function') {
db_.hasConfiguration(function() {
if (typeof _db.hasConfiguration === 'function') {
_db.hasConfiguration(function() {
return self.getConfiguration();
});
}
return db_;
return _db;
};

/**
* @param {DataAdapter|*} value
*/
self.setDb = function(value) {
db_ = value;
if (db_) {
if (typeof db_.hasConfiguration === 'function') {
db_.hasConfiguration(function() {
_db = value;
if (_db) {
if (typeof _db.hasConfiguration === 'function') {
_db.hasConfiguration(function() {
return self.getConfiguration();
});
}
Expand Down Expand Up @@ -364,8 +373,13 @@ NamedDataContext.prototype.model = function(name) {

NamedDataContext.prototype.finalize = function(cb) {
cb = cb || function () {};
this.finalize_();
cb.call(this);
this._finalize(function(err) {
if (err) {
TraceUtils.error('An error occurred while finalizing the underlying database context.');
TraceUtils.error(err);
}
cb();
});
};

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/data",
"version": "2.18.1",
"version": "2.18.2",
"description": "MOST Web Framework Codename Blueshift - Data module",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions spec/DataAssociationMapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('DataAssociationMapping', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
})
it('should get item children (for one-to-many association)', async () => {
Expand Down
1 change: 1 addition & 0 deletions spec/DataAttributeResolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('DataAttributeResolver', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
})
it('should resolve child nested attributes', async () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/DataNestedObjectListener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('DataNestedObjectListener', () => {
return done();
});
afterAll(async () => {
await context.finalize();
await context.finalizeAsync();
await app.finalize();
});
it('should use zero-or-one multiplicity', async () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/DataObjectAssociationListener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('DataObjectAssociationListener', () => {
return done();
});
afterAll(async () => {
await context.finalize();
await context.finalizeAsync();
await app.finalize();
});
it('should validate foreign-key association', async ()=> {
Expand Down
1 change: 1 addition & 0 deletions spec/DataObjectJunction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('DataObjectJunction', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
});

Expand Down
1 change: 1 addition & 0 deletions spec/DataObjectTag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('DataObjectTag', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
});

Expand Down
1 change: 1 addition & 0 deletions spec/DataValueResolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('DataValueResolver', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
})
it('should resolve value from an associated parent object', async () => {
Expand Down
1 change: 1 addition & 0 deletions spec/FunctionContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('FunctionContext', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
});

Expand Down
1 change: 1 addition & 0 deletions spec/HasParentJunction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('HasParentJunction', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
});

Expand Down
8 changes: 4 additions & 4 deletions spec/OnExecuteNestedQueryable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import { DataConfigurationStrategy } from '../data-configuration';
import { OnExecuteNestedQueryable } from '../OnExecuteNestedQueryable';
import { TestUtils } from './adapter/TestUtils';
import { TestApplication2 } from './TestApplication';
import { DataContext } from 'types';

describe('OnExecuteNestedQueryable', () => {
let app: TestApplication2;
let context: DataContext;
beforeAll((done) => {
app = new TestApplication2();
context = app.createContext();
return done();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
})
it('should find listener', () => {
expect(app).toBeTruthy();
const context = app.createContext();
const model = context.model('Product');
expect(model).toBeTruthy();
let listeners = model.rawListeners('before.execute');
Expand All @@ -35,7 +38,6 @@ describe('OnExecuteNestedQueryable', () => {
});

it('should use nested query', async () => {
const context = app.createContext();
let Actions = context.model('Action');
const beforeExecute = OnExecuteNestedQueryable.prototype.beforeExecute;
Actions.removeListener('before.execute', beforeExecute);
Expand All @@ -47,14 +49,12 @@ describe('OnExecuteNestedQueryable', () => {
});

it('should use nested many-to-one query', async () => {
const context = app.createContext();
let Orders = context.model('Order');
await expect(Orders.where('customer/gender/alternateName')
.equal('Female').silent().getItems()).resolves.toBeTruthy();
});

it('should use nested one-to-many query', async () => {
const context = app.createContext();
let People = context.model('Person');
await expect(People.where('orders/orderStatus/alternateName')
.equal('OrderProblem').silent().getItems()).resolves.toBeTruthy();
Expand Down
1 change: 1 addition & 0 deletions spec/ParentChildRelationship.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('ParentChildRelationship', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
});

Expand Down
1 change: 1 addition & 0 deletions spec/ZeroOneMultiplicity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('ZeroOrOneMultiplicity', () => {
context = app.createContext();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
})
it('should use zero or one multiplicity', async () => {
Expand Down

0 comments on commit 2389e94

Please sign in to comment.