From f7e81256bbc5974bc2fd80baffadb31cf37baaaf Mon Sep 17 00:00:00 2001 From: JimmyDaddy Date: Wed, 20 Jul 2022 22:41:05 +0800 Subject: [PATCH] feat: add leoric_bone meta data to Bone --- index.js | 2 ++ src/bone.js | 3 +++ src/constants.js | 3 +++ src/realm.js | 3 ++- src/utils/index.js | 8 ++++++++ test/unit/utils/index.test.js | 21 +++++++++++++++++++-- 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 00ddf428..e3c70439 100644 --- a/index.js +++ b/index.js @@ -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 @@ -64,6 +65,7 @@ Object.assign(Realm, { SqliteDriver, AbstractDriver, Raw, + isBone, }); module.exports = Realm; diff --git a/src/bone.js b/src/bone.js index 11cde84d..9b12dd24 100644 --- a/src/bone.js +++ b/src/bone.js @@ -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'); @@ -1715,4 +1716,6 @@ for (const getter of Spell_getters) { }); } +Reflect.defineMetadata(IS_LEORIC_BONE, true, Bone); + module.exports = Bone; diff --git a/src/constants.js b/src/constants.js index 55dc2960..32ce7966 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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, @@ -44,4 +46,5 @@ module.exports = { ASSOCIATE_METADATA_MAP, TIMESTAMP_ATTRIBUTE_NAMES, AGGREGATORS, + IS_LEORIC_BONE, }; diff --git a/src/realm.js b/src/realm.js index b18f60f4..d055fdb0 100644 --- a/src/realm.js +++ b/src/realm.js @@ -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'); @@ -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); } } diff --git a/src/utils/index.js b/src/utils/index.js index 19bd491e..8e6549a3 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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]'; @@ -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, }; diff --git a/test/unit/utils/index.test.js b/test/unit/utils/index.test.js index d06b447c..52df76f7 100644 --- a/test/unit/utils/index.test.js +++ b/test/unit/utils/index.test.js @@ -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() { @@ -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); + }); +});