-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support disconnect and fix timestamps init (#313)
* feat: close #309 support disconnect * fix: close #311 Model.timestamps didn't init correct while use static attributes to define a model Co-authored-by: JimmyDaddy <[email protected]>
- Loading branch information
1 parent
a9a0eda
commit 2ae1e31
Showing
8 changed files
with
183 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert').strict; | ||
const { connect, Bone, DataTypes } = require('../..'); | ||
|
||
describe('connect', function() { | ||
beforeEach(() => { | ||
Bone.driver = null; | ||
}); | ||
|
||
it('should work without define attributes clearly', async function() { | ||
class Book extends Bone {} | ||
await connect({ | ||
port: process.env.MYSQL_PORT, | ||
user: 'root', | ||
database: 'leoric', | ||
Bone: Bone, | ||
models: [ Book ], | ||
}); | ||
assert.equal(Book.timestamps.createdAt, 'createdAt'); | ||
assert.equal(Book.attributes.createdAt.columnName, 'gmt_create'); | ||
assert.equal(Book.timestamps.updatedAt, 'updatedAt'); | ||
assert.equal(Book.attributes.updatedAt.columnName, 'gmt_modified'); | ||
assert.equal(Book.timestamps.deletedAt, 'deletedAt'); | ||
assert.equal(Book.attributes.deletedAt.columnName, 'gmt_deleted'); | ||
|
||
}); | ||
|
||
it('should work with define attributes clearly', async function() { | ||
const { STRING, BIGINT, DECIMAL, DATE } = DataTypes; | ||
class Book extends Bone { | ||
static attributes = { | ||
isbn: { type: BIGINT, primaryKey: true }, | ||
name: { type: STRING, allowNull: false }, | ||
price: { type: DECIMAL(10, 3), allowNull: false }, | ||
createdAt: { type: DATE }, | ||
updatedAt: { type: DATE }, | ||
deletedAt: { type: DATE }, | ||
} | ||
} | ||
await connect({ | ||
port: process.env.MYSQL_PORT, | ||
user: 'root', | ||
database: 'leoric', | ||
models: [ Book ], | ||
}); | ||
assert.equal(Book.timestamps.createdAt, 'createdAt'); | ||
assert.equal(Book.attributes.createdAt.columnName, 'gmt_create'); | ||
assert.equal(Book.timestamps.updatedAt, 'updatedAt'); | ||
assert.equal(Book.attributes.updatedAt.columnName, 'gmt_modified'); | ||
assert.equal(Book.timestamps.deletedAt, 'deletedAt'); | ||
assert.equal(Book.attributes.deletedAt.columnName, 'gmt_deleted'); | ||
}); | ||
|
||
it('should work with snake case', async function() { | ||
const { STRING, BIGINT, DECIMAL, DATE } = DataTypes; | ||
class Book extends Bone { | ||
static attributes = { | ||
isbn: { type: BIGINT, primaryKey: true }, | ||
name: { type: STRING, allowNull: false }, | ||
price: { type: DECIMAL(10, 3), allowNull: false }, | ||
created_at: { type: DATE }, | ||
updated_at: { type: DATE }, | ||
deleted_at: { type: DATE }, | ||
} | ||
} | ||
await connect({ | ||
port: process.env.MYSQL_PORT, | ||
user: 'root', | ||
database: 'leoric', | ||
models: [ Book ], | ||
}); | ||
assert.equal(Book.timestamps.createdAt, 'created_at'); | ||
assert.equal(Book.attributes.created_at.columnName, 'gmt_create'); | ||
assert.equal(Book.timestamps.updatedAt, 'updated_at'); | ||
assert.equal(Book.attributes.updated_at.columnName, 'gmt_modified'); | ||
assert.equal(Book.timestamps.deletedAt, 'deleted_at'); | ||
assert.equal(Book.attributes.deleted_at.columnName, 'gmt_deleted'); | ||
}); | ||
|
||
|
||
it('should work with mixed case', async function() { | ||
const { STRING, BIGINT, DECIMAL, DATE } = DataTypes; | ||
class Book extends Bone { | ||
static attributes = { | ||
isbn: { type: BIGINT, primaryKey: true }, | ||
name: { type: STRING, allowNull: false }, | ||
price: { type: DECIMAL(10, 3), allowNull: false }, | ||
created_at: { type: DATE }, | ||
updatedAt: { type: DATE }, | ||
deleted_at: { type: DATE }, | ||
} | ||
} | ||
await connect({ | ||
port: process.env.MYSQL_PORT, | ||
user: 'root', | ||
database: 'leoric', | ||
models: [ Book ], | ||
}); | ||
assert.equal(Book.timestamps.createdAt, 'created_at'); | ||
assert.equal(Book.attributes.created_at.columnName, 'gmt_create'); | ||
assert.equal(Book.timestamps.updatedAt, 'updatedAt'); | ||
assert.equal(Book.attributes.updatedAt.columnName, 'gmt_modified'); | ||
assert.equal(Book.timestamps.deletedAt, 'deleted_at'); | ||
assert.equal(Book.attributes.deleted_at.columnName, 'gmt_deleted'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters