Skip to content

Commit

Permalink
fix: export Logger and Spell (#126)
Browse files Browse the repository at this point in the history
... to let users intercept lower level api calls
  • Loading branch information
cyjake authored Jun 30, 2021
1 parent 7b7c6f7 commit 529d73e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 9 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.5.1 / 2021-06-30
==================

* fix: export Logger and Spell to let users intercept lower level api calls

1.5.0 / 2021-06-30
==================

Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const fs = require('fs').promises;
const path = require('path');

const Logger = require('./src/drivers/abstract/logger');
const Spell = require('./src/spell');
const Bone = require('./src/bone');
const Collection = require('./src/collection');
const DataTypes = require('./src/data_types');
Expand Down Expand Up @@ -242,6 +244,15 @@ const connect = async function connect(opts = {}) {
};

Object.assign(Realm.prototype, migrations, { DataTypes });
Object.assign(Realm, { connect, Bone, Collection, DataTypes, sequelize, ...Hint });
Object.assign(Realm, {
connect,
Bone,
Collection,
DataTypes,
Logger,
Spell,
sequelize,
...Hint,
});

module.exports = Realm;
3 changes: 2 additions & 1 deletion src/drivers/abstract/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const Logger = require('./logger');

module.exports = class AbstractDriver {
constructor(opts = {}) {
this.logger = new Logger(opts.logger);
const { logger } = opts;
this.logger = logger instanceof Logger ? logger : new Logger(logger);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/drivers/mysql/spellbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@ module.exports = {

return `ON DUPLICATE KEY UPDATE ${sets.join(', ')}`;
},
formatReturning() { return ''; },

formatReturning() {
return '';
},
};
8 changes: 4 additions & 4 deletions src/spell.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function parseLogicalObjectCondition(name, value) {
}

/**
* parse conditions in MongoDB style, which is quite polular in ORMs for JavaScript. See {@link module:lib/spell~OPERATOR_MAP} for supported `$op`s.
* parse conditions in MongoDB style, which is quite polular in ORMs for JavaScript. See {@link module:src/spell~OPERATOR_MAP} for supported `$op`s.
* @example
* { foo: null }
* { foo: { $gt: new Date(2012, 4, 15) } }
Expand Down Expand Up @@ -280,10 +280,10 @@ function parseSelect(spell, ...names) {

/**
* Translate key-value pairs of attributes into key-value pairs of columns. Get ready for the SET part when generating SQL.
* @param {Spell} spell
* @param {Spell} spell
* @param {Object} obj - key-value pairs of attributes
* @param {boolean} strict - check attribute exist or not
* @returns
* @returns
*/
function formatValueSet(spell, obj, strict = true) {
const { Model } = spell;
Expand All @@ -296,7 +296,7 @@ function formatValueSet(spell, obj, strict = true) {
continue;
}
}

// raw sql don't need to uncast
if (obj[name] && obj[name].__raw) {
sets[name] = obj[name];
Expand Down
16 changes: 16 additions & 0 deletions test/unit/drivers/abstract/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert').strict;
const dayjs = require('dayjs');
const { Logger } = require('../../../..');
const AbstractDriver = require('../../../../src/drivers/abstract');

describe('=> AbstractDriver', function() {
Expand Down Expand Up @@ -33,3 +34,18 @@ describe('=> AbstractDriver', function() {
assert.ok(date instanceof Date);
});
});

describe('=> AbstractDriver#logger', function() {
it('should create logger by default', async function() {
const driver = new AbstractDriver();
assert.ok(driver.logger);
assert.ok(driver.logger instanceof Logger);
});

it('should accept custom logger', async function() {
class CustomLogger extends Logger {};
const driver = new AbstractDriver({ logger: new CustomLogger });
assert.ok(driver.logger);
assert.ok(driver.logger instanceof CustomLogger);
});
});
6 changes: 4 additions & 2 deletions test/unit/realm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert').strict;
const Realm = require('../..');
const { connect, Bone, DataTypes } = Realm;
const { connect, Bone, DataTypes, Logger, Spell } = Realm;

const attributes = {
id: DataTypes.BIGINT,
Expand Down Expand Up @@ -35,6 +35,8 @@ describe('=> Realm', () => {
assert.ok(connect);
assert.ok(Bone);
assert.ok(DataTypes);
assert.ok(Logger);
assert.ok(Spell);
});

it('should not subclass Bone unless asked specifically', async () => {
Expand Down Expand Up @@ -242,7 +244,7 @@ describe('=> Realm', () => {
});
});

describe('transaction', () => {
describe('realm.transaction', () => {
it('realm.transaction generator callback should work', async () => {
const queries = [];
const email = 'lighting@valhalla.ne';
Expand Down

0 comments on commit 529d73e

Please sign in to comment.