Skip to content

Commit

Permalink
breaking: drop the deprecated Model.describe() (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored Sep 3, 2021
1 parent 03d5145 commit e1501e9
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 51 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ async function loadModels(Spine, models, opts) {
}

for (const model of models) {
model.describe();
model.initialize();
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/adapters/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,8 @@ module.exports = Bone => {
return spell;
}

static async describe() {
const { driver, physicTable: table } = this;
return await driver.describeTable(table);
}
// EXISTS
// static async describe() {}

static async destroy(options = {}) {
const { where, individualHooks, paranoid } = options;
Expand Down
26 changes: 9 additions & 17 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function copyValues(values) {
* The base class that provides Object-relational mapping. This class is never intended to be used directly. We need to create models that extends from Bone. Most of the query features of Bone is implemented by {@link Spell} such as {@link Spell#$group} and {@link Spell#$join}. With Bone, you can create models like this:
*
* class Post extends Bone {
* static describe() {
* static initialize() {
* this.hasMany('comments')
* this.belongsTo('author', { className: 'User' })
* this.attribute('extra', { type: JSON })
Expand Down Expand Up @@ -873,7 +873,7 @@ class Bone {
* @param {Object} meta
* @example
* class Post extends Bone {
* static describe() {
* static initialize() {
* Post.attribute('extra', { type: JSON })
* }
* }
Expand Down Expand Up @@ -953,19 +953,6 @@ class Bone {
}
}

/**
* Override this method to setup associations, rename attributes, etc.
* @deprecated use {@link Bone.initialize} instead
* @example
* class Post extends Bone {
* static describe() {
* this.belongsTo('author', { className: 'User' })
* this.renameAttribute('content', 'body')
* }
* }
*/
static describe() {}

/**
* Override this method to setup associations, rename attributes, etc.
* @example
Expand Down Expand Up @@ -1251,14 +1238,14 @@ class Bone {
* Start a join query by including associations by name. The associations should be predefined in model's static `describe()` method. See {@link Bone.belongsTo}, {@link Bone.hasMany}, and {@link Bone.hasOne} for more information.
* @example
* class Post extends Bone {
* static describe() {
* static initialize() {
* this.hasMany('comments')
* this.belongsTo('author')
* }
* }
* Post.include('comments')
* Post.include('author', 'comments')
* @param {...string} names - association names defined in {@link Bone.describe}
* @param {...string} names - association names defined in {@link Bone.initialize}
*/
static include(...names) {
return this._find().$with(...names);
Expand Down Expand Up @@ -1516,6 +1503,11 @@ class Bone {
this.load(schemaInfo[table]);
}

static async describe() {
const { driver, physicTable: table } = this;
return await driver.describeTable(table);
}

static async drop() {
return await this.driver.dropTable(this.table);
}
Expand Down
8 changes: 6 additions & 2 deletions src/drivers/abstract/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ module.exports = {
},

async describeTable(table) {
const { escapeId } = this;
await this.query(`DESCRIBE ${escapeId(table)}`);
const { database } = this.options;
const schemaInfo = await this.querySchemaInfo(database, table);
return schemaInfo[table].reduce(function(result, column) {
result[column.columnName] = column;
return result;
}, {});
},

async addColumn(table, name, params) {
Expand Down
16 changes: 16 additions & 0 deletions src/drivers/mysql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,20 @@ module.exports = {
`);
await this.query(sql);
},

async describeTable(table) {
const { escapeId } = this;
const { rows } = await this.query(`DESCRIBE ${escapeId(table)}`);
const result = {};
for (const row of rows) {
result[row.Field] = {
columnName: row.Field,
columnType: row.Type,
allowNull: row.Null === 'YES',
defaultValue: row.Default,
autoIncrement: row.Extra === 'auto_increment',
};
}
return result;
},
};
4 changes: 0 additions & 4 deletions src/drivers/sqlite/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ module.exports = {
}
},

async describeTable(table) {
return await this.query(`PRAGMA table_info(${this.escapeId(table)})`);
},

async addColumn(table, name, params) {
const { escapeId } = this;
const attribute = new Attribute(name, params);
Expand Down
18 changes: 18 additions & 0 deletions test/integration/suite/definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,21 @@ describe('=> Bone.truncate()', () => {
assert.equal(await Temp.count(), 0);
});
});

describe('=> Bone.describe()', function() {
beforeEach(async function() {
await Bone.driver.dropTable('temp');
});

it('should be able to get table description', async function() {
class Temp extends Bone {};
Temp.init({
id: INTEGER,
foo: STRING,
}, { tableName: 'temp' });

await Temp.sync();
const result = await Temp.describe();
assert.deepEqual(Object.keys(result), [ 'id', 'foo' ]);
});
});
9 changes: 2 additions & 7 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,6 @@ describe('=> Sequelize adapter', () => {
assert.equal(book.price, 7);
});

it('Model.describe()', async () => {

});

it('Model.destroy()', async () => {
await Promise.all([
Post.create({ title: 'By three they come' }),
Expand Down Expand Up @@ -861,9 +857,8 @@ describe('=> Sequelize adapter', () => {
});

it('Model.describe()', async function() {
const { rows } = await Book.describe();
assert(rows);
assert(rows.find(row => row.name === 'isbn'));
const result = await Book.describe();
assert(result.hasOwnProperty('isbn'));
});

it('model.decrement()', async () => {
Expand Down
32 changes: 16 additions & 16 deletions test/unit/drivers/sqlite/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ describe('=> SQLite driver', () => {
await driver.alterTable('notes', {
params: { type: JSONB },
});
const { rows } = await driver.describeTable('notes');
assert.deepEqual(rows.pop(), {
cid: 2,
name: 'params',
type: 'JSON',
notnull: 0,
dflt_value: null,
pk: 0
const result = await driver.describeTable('notes');
assert.deepEqual(result.params, {
columnName: 'params',
columnType: 'json',
dataType: 'json',
allowNull: true,
defaultValue: null,
primaryKey: false
});
});

Expand All @@ -113,14 +113,14 @@ describe('=> SQLite driver', () => {
});
}, /NOT NULL/);
// should rollback if failed to alter table
const { rows } = await driver.describeTable('notes');
assert.deepEqual(rows.pop(), {
cid: 1,
name: 'title',
type: 'VARCHAR(255)',
notnull: 0,
dflt_value: null,
pk: 0
const tableInfo = await driver.describeTable('notes');
assert.deepEqual(tableInfo.title, {
columnName: 'title',
columnType: 'varchar(255)',
dataType: 'varchar',
allowNull: true,
defaultValue: null,
primaryKey: false,
});
const result = await driver.query('SELECT * FROM notes');
assert.equal(result.rows.length, 1);
Expand Down

0 comments on commit e1501e9

Please sign in to comment.