Skip to content

Commit

Permalink
fix: meta attributes should still be available if subclass-ed (#427)
Browse files Browse the repository at this point in the history
* fix: meta attributes should still be available if subclass-ed

* build: nodejs v22.x

* test: bump test coverage
  • Loading branch information
cyjake authored Nov 18, 2024
1 parent 11fd930 commit 8795b82
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

- uses: actions/setup-node@v2
with:
node-version: 16
node-version: 22

- name: npm install, build, and test
run: |
Expand Down
41 changes: 20 additions & 21 deletions src/realm/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/raw.test.js
Original file line number Diff line number Diff line change
@@ -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'; } }));
});
});
});
24 changes: 24 additions & 0 deletions test/unit/realm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8795b82

Please sign in to comment.