Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add leoric_bone meta data to Bone #324

Merged
merged 1 commit into from
Jul 21, 2022
Merged
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: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Realm = require('./src/realm');
const Decorators = require('./src/decorators');
const Raw = require('./src/raw');
const { MysqlDriver, PostgresDriver, SqliteDriver, AbstractDriver } = require('./src/drivers');
const { isBone } = require('./src/utils');

/**
* @typedef {Object} RawSql
Expand Down Expand Up @@ -64,6 +65,7 @@ Object.assign(Realm, {
SqliteDriver,
AbstractDriver,
Raw,
isBone,
});

module.exports = Realm;
3 changes: 3 additions & 0 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
LEGACY_TIMESTAMP_COLUMN_MAP,
ASSOCIATE_METADATA_MAP,
TIMESTAMP_ATTRIBUTE_NAMES,
IS_LEORIC_BONE,
} = require('./constants');

const columnAttributesKey = Symbol('leoric#columns');
Expand Down Expand Up @@ -1715,4 +1716,6 @@ for (const getter of Spell_getters) {
});
}

Reflect.defineMetadata(IS_LEORIC_BONE, true, Bone);

module.exports = Bone;
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const ASSOCIATE_METADATA_MAP = {
belongsTo: Symbol('belongsTo'),
};

const IS_LEORIC_BONE = Symbol('leoric#bone');

module.exports = {
AGGREGATOR_MAP,
LEGACY_TIMESTAMP_MAP,
Expand All @@ -44,4 +46,5 @@ module.exports = {
ASSOCIATE_METADATA_MAP,
TIMESTAMP_ATTRIBUTE_NAMES,
AGGREGATORS,
IS_LEORIC_BONE,
};
3 changes: 2 additions & 1 deletion src/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path');
const Bone = require('./bone');
const { findDriver, AbstractDriver } = require('./drivers');
const { camelCase } = require('./utils/string');
const { isBone } = require('./utils');
const sequelize = require('./adapters/sequelize');
const Raw = require('./raw');
const { LEGACY_TIMESTAMP_MAP } = require('./constants');
Expand Down Expand Up @@ -33,7 +34,7 @@ async function findModels(dir) {
const extname = path.extname(entry.name);
if (entry.isFile() && ['.js', '.mjs'].includes(extname)) {
const model = require(path.join(dir, entry.name));
if (model.prototype instanceof Bone) models.push(model);
if (isBone(model)) models.push(model);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { performance } = require('perf_hooks');
const { IS_LEORIC_BONE } = require('../constants');

function isPlainObject(value) {
return Object.prototype.toString.call(value) === '[object Object]';
Expand Down Expand Up @@ -43,10 +44,17 @@ const logger = {};
};
});

function isBone(bone) {
if (!bone || (typeof bone !== 'object' && typeof bone !== 'function')) return false;
const metaValue = Reflect.getMetadata(IS_LEORIC_BONE, bone);
return metaValue === true;
}

module.exports = {
isPlainObject,
compose,
getPropertyNames,
calculateDuration,
logger,
isBone,
};
21 changes: 19 additions & 2 deletions test/unit/utils/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const assert = require('assert').strict;
const { Bone } = require('../../..');
const { compose, getPropertyNames, logger } = require('../../../src/utils');
const { Bone, sequelize } = require('../../..');
const { compose, getPropertyNames, logger, isBone } = require('../../../src/utils');

describe('=> compose', function() {
it('should return a default function if nothing to compose', function() {
Expand Down Expand Up @@ -71,3 +71,20 @@ describe('=> logger', function() {
logger.log('foo');
});
});

describe('=> isBone', () => {
it('should work', () => {
assert.equal(isBone(), false);
assert.equal(isBone(null), false);
assert.equal(isBone(1), false);
assert.equal(isBone(() => {}), false);
assert.equal(isBone({}), false);
assert.equal(isBone(Bone), true);
class Note {}
assert.equal(isBone(Note), false);
class Note1 extends Bone {}
assert.equal(isBone(Note1), true);
assert.equal(isBone(sequelize(Note1)), true);
assert.equal(isBone(sequelize(Note)), false);
});
});