From 8795b82a1465f72828cb50dda1e854d51f322fbc Mon Sep 17 00:00:00 2001 From: Chen Yangjian <252317+cyjake@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:49:08 +0800 Subject: [PATCH] fix: meta attributes should still be available if subclass-ed (#427) * fix: meta attributes should still be available if subclass-ed * build: nodejs v22.x * test: bump test coverage --- .github/workflows/nodejs.yml | 2 +- .github/workflows/npmpublish.yml | 2 +- src/realm/base.js | 41 ++++++++++++++++---------------- test/unit/raw.test.js | 12 ++++++++++ test/unit/realm.test.js | 24 +++++++++++++++++++ 5 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 test/unit/raw.test.js diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index e539bc90..a6f0b0be 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -36,7 +36,7 @@ jobs: strategy: matrix: - node-version: [16.x, 18.x, 20.x] + node-version: [16.x, 18.x, 20.x, 22.x] steps: - uses: actions/checkout@v2 diff --git a/.github/workflows/npmpublish.yml b/.github/workflows/npmpublish.yml index fc13b8ce..1c30fcbc 100644 --- a/.github/workflows/npmpublish.yml +++ b/.github/workflows/npmpublish.yml @@ -37,7 +37,7 @@ jobs: - uses: actions/setup-node@v2 with: - node-version: 16 + node-version: 22 - name: npm install, build, and test run: | diff --git a/src/realm/base.js b/src/realm/base.js index ce583a09..8f5266ba 100644 --- a/src/realm/base.js +++ b/src/realm/base.js @@ -46,26 +46,6 @@ function initAttributes(model, columns) { model.init(attributes, { timestamps: false }); } -async function loadModels(Spine, models, opts) { - const { database } = opts; - const tables = models.map(model => model.physicTable); - const schemaInfo = await Spine.driver.querySchemaInfo(database, tables); - - for (const model of models) { - // assign driver if model's driver not exist - if (!model.driver) model.driver = Spine.driver; - // assign options if model's options not exist - if (!model.options) model.options = Spine.options; - const columns = schemaInfo[model.physicTable] || schemaInfo[model.table]; - if (!model.attributes) initAttributes(model, columns); - model.load(columns); - } - - for (const model of models) { - model.initialize(); - } -} - function createSpine(opts) { let Model = Bone; if (opts.Bone && opts.Bone.prototype instanceof Bone) { @@ -142,6 +122,25 @@ class BaseRealm { return Object.values(this.models); } + async loadModels(models, opts) { + const { database } = opts; + const tables = models.map(model => model.physicTable); + const schemaInfo = await this.driver.querySchemaInfo(database, tables); + + for (const model of models) { + if (!model.driver) model.driver = this.driver; + if (!model.options) model.options = this.options; + if (!model.models) model.models = this.models; + const columns = schemaInfo[model.physicTable] || schemaInfo[model.table]; + if (!model.attributes) initAttributes(model, columns); + model.load(columns); + } + + for (const model of models) { + model.initialize(); + } + } + async connect() { let models = await this.getModels(); @@ -150,7 +149,7 @@ class BaseRealm { models = models.filter(model => model.synchronized == null); if (models.length > 0) { - await loadModels(this.Bone, models, this.options); + await this.loadModels(models, this.options); } this.connected = true; return this.Bone; diff --git a/test/unit/raw.test.js b/test/unit/raw.test.js new file mode 100644 index 00000000..9eda297d --- /dev/null +++ b/test/unit/raw.test.js @@ -0,0 +1,12 @@ +'use strict'; + +const { Raw } = require('../../src'); +const assert = require('assert').strict; + +describe('=> Raw', () => { + describe('constructor()', () => { + it('should throw if not called with string', async () => { + assert.throws(() => new Raw({ toSqlString() { return 'foo'; } })); + }); + }); +}); diff --git a/test/unit/realm.test.js b/test/unit/realm.test.js index 4a672be0..655eac08 100644 --- a/test/unit/realm.test.js +++ b/test/unit/realm.test.js @@ -181,6 +181,30 @@ describe('=> Realm', () => { }); }); + describe('realm.connect', async () => { + class Post extends Bone { + static table = 'articles'; + } + + afterEach(async () => { + await Post.truncate(); + }); + + it('realm.connect should set necessary static attributes on models', async () => { + const realm = new Realm({ + port: process.env.MYSQL_PORT, + user: 'root', + database: 'leoric', + models: [ Post ], + subclass: true, + }); + await realm.connect(); + assert.ok(Post.driver); + assert.ok(Post.models); + assert.ok(Post.options); + }); + }); + describe('realm.query', async () => { class Post extends Bone {