-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow models be extended from Bone directly (#12)
* fix: allow models be extended from Bone directly ... to make codebases that use v0.4.x a bit easier to upgrade * release: v1.1.4
- Loading branch information
Showing
13 changed files
with
188 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
1.1.4 / 2021-06-25 | ||
================== | ||
|
||
**fixes** | ||
* [#12](https://github.com/eggjs/egg-orm/pull/12) - fix: allow models be extended from Bone directly to make codebases that use v0.4.x a bit easier to upgrade (Chen Yangjian <<[email protected]>>) | ||
|
||
1.1.3 / 2021-06-16 | ||
================== | ||
|
||
**fixes** | ||
* [#9](https://github.com/eggjs/egg-orm/pull/9) - fix: ctx.model.ctx injection | ||
* [#9](https://github.com/eggjs/egg-orm/pull/9) - fix: ctx.model.ctx injection (Chen Yangjian <<[email protected]>>) | ||
|
||
1.1.2 / 2021-03-04 | ||
================== | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "egg-orm", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Object relational mapping for Egg applications", | ||
"eggPlugin": { | ||
"name": "orm" | ||
|
@@ -29,7 +29,7 @@ | |
"author": "Chen Yangjian <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"leoric": "^1.4.0-alpha.3" | ||
"leoric": "^1.4.1" | ||
}, | ||
"devDependencies": { | ||
"egg": "^2.25.0", | ||
|
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 |
---|---|---|
|
@@ -49,7 +49,6 @@ describe('test/delegate.test.js', () => { | |
const user2 = new ctx.orm.User({ | ||
nickname: 'bar nickname', | ||
}); | ||
console.log(user2.toObject()); | ||
assert(user2.nickname === 'bar nickname'); | ||
}); | ||
|
||
|
@@ -69,30 +68,34 @@ describe('test/delegate.test.js', () => { | |
assert.notEqual(ctx.orm, ctx2.orm); | ||
assert.notEqual(ctx.orm.User, ctx2.orm.User); | ||
}); | ||
}); | ||
|
||
describe('GET /users/:id, POST /users', () => { | ||
it('should create and get user successfully', async () => { | ||
const res = await app.httpRequest() | ||
.post('/users') | ||
.send({ | ||
nickname: 'rose', | ||
email: '[email protected]', | ||
}); | ||
assert(res.status === 200); | ||
assert(res.body.id); | ||
assert(res.body.nickname === 'rose'); | ||
assert(res.body.email === '[email protected]'); | ||
assert(res.body.createdAt); | ||
|
||
const res2 = await app.httpRequest() | ||
.get(`/users/${res.body.id}`) | ||
.send({ | ||
nickname: 'rose', | ||
email: '[email protected]', | ||
}); | ||
assert(res2.status === 200); | ||
assert.deepEqual(res2.body, res.body); | ||
}); | ||
describe('GET /users/:id, POST /users', () => { | ||
beforeEach(async function() { | ||
await app.orm.User.truncate(); | ||
}); | ||
|
||
it('should create and get user successfully', async () => { | ||
const res = await app.httpRequest() | ||
.post('/users') | ||
.send({ | ||
nickname: 'rose', | ||
email: '[email protected]', | ||
}); | ||
assert(res.status === 200); | ||
assert(res.body.id); | ||
assert(res.body.nickname === 'rose'); | ||
assert(res.body.email === '[email protected]'); | ||
assert(res.body.createdAt); | ||
|
||
const res2 = await app.httpRequest() | ||
.get(`/users/${res.body.id}`) | ||
.send({ | ||
nickname: 'rose', | ||
email: '[email protected]', | ||
}); | ||
assert(res2.status === 200); | ||
assert.deepEqual(res2.body, res.body); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
'use strict'; | ||
|
||
module.exports = (app) => { | ||
module.exports = function(app) { | ||
const { DataTypes: { BIGINT, TEXT, STRING } } = app.model; | ||
const Post = app.model.define('Post', { | ||
id: { type: BIGINT, autoIncrement: true }, | ||
content: TEXT, | ||
description: STRING, | ||
}) | ||
}); | ||
return Post; | ||
}; |
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,18 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
return class UsersController extends app.Controller { | ||
async show() { | ||
const user = await this.ctx.model.User.findOne(this.ctx.params.id); | ||
this.ctx.body = user; | ||
} | ||
|
||
async create() { | ||
const user = await app.model.User.create({ | ||
nickname: this.ctx.request.body.nickname, | ||
email: this.ctx.request.body.email, | ||
}); | ||
this.ctx.body = user; | ||
} | ||
}; | ||
}; |
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,5 @@ | ||
'use strict'; | ||
|
||
const { Bone } = require('leoric'); | ||
|
||
module.exports = class User extends Bone {}; |
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,5 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
app.resources('users', '/users', 'users'); | ||
}; |
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,16 @@ | ||
'use strict'; | ||
|
||
const { Bone } = require('leoric'); | ||
|
||
exports.orm = { | ||
database: 'egg-orm', | ||
port: process.env.MYSQL_PORT, | ||
// connect to Bone directly | ||
Bone, | ||
}; | ||
|
||
exports.keys = 'hello'; | ||
|
||
exports.security = { | ||
csrf: false, | ||
}; |
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,3 @@ | ||
{ | ||
"name": "legacy" | ||
} |
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,53 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert').strict; | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/legacy.test.js', () => { | ||
let app; | ||
|
||
before(() => { | ||
app = mm.app({ | ||
baseDir: 'apps/legacy', | ||
}); | ||
return app.ready(); | ||
}); | ||
|
||
after(mm.restore); | ||
|
||
describe('app.model', function() { | ||
it('should be accessible via app.model', function() { | ||
assert(app.model); | ||
assert(app.model.User); | ||
}); | ||
}); | ||
|
||
describe('GET /users/:id, POST /users', () => { | ||
beforeEach(async function() { | ||
await app.model.User.truncate(); | ||
}); | ||
|
||
it('should create and get user successfully', async () => { | ||
const res = await app.httpRequest() | ||
.post('/users') | ||
.send({ | ||
nickname: 'jack', | ||
email: '[email protected]', | ||
}); | ||
assert(res.status === 200); | ||
assert(res.body.id); | ||
assert(res.body.nickname === 'jack'); | ||
assert(res.body.email === '[email protected]'); | ||
assert(res.body.createdAt); | ||
|
||
const res2 = await app.httpRequest() | ||
.get(`/users/${res.body.id}`) | ||
.send({ | ||
nickname: 'jack', | ||
email: '[email protected]', | ||
}); | ||
assert(res2.status === 200); | ||
assert.deepEqual(res2.body, res.body); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -47,7 +47,6 @@ describe('test/plugin.test.js', () => { | |
const user2 = new ctx.model.User({ | ||
nickname: 'bar nickname', | ||
}); | ||
console.log(user2.toObject()); | ||
assert(user2.nickname === 'bar nickname'); | ||
}); | ||
|
||
|
@@ -81,7 +80,6 @@ describe('test/plugin.test.js', () => { | |
const post2 = new ctx.model.Post({ | ||
description: 'bar nickname', | ||
}); | ||
console.log(post2.toObject()); | ||
assert(post2.description === 'bar nickname'); | ||
}); | ||
|
||
|
@@ -101,30 +99,34 @@ describe('test/plugin.test.js', () => { | |
assert.notEqual(ctx.model, ctx2.model); | ||
assert.notEqual(ctx.model.User, ctx2.model.User); | ||
}); | ||
}); | ||
|
||
describe('GET /users/:id, POST /users', () => { | ||
it('should create and get user successfully', async () => { | ||
const res = await app.httpRequest() | ||
.post('/users') | ||
.send({ | ||
nickname: 'jack', | ||
email: '[email protected]', | ||
}); | ||
assert(res.status === 200); | ||
assert(res.body.id); | ||
assert(res.body.nickname === 'jack'); | ||
assert(res.body.email === '[email protected]'); | ||
assert(res.body.createdAt); | ||
|
||
const res2 = await app.httpRequest() | ||
.get(`/users/${res.body.id}`) | ||
.send({ | ||
nickname: 'jack', | ||
email: '[email protected]', | ||
}); | ||
assert(res2.status === 200); | ||
assert.deepEqual(res2.body, res.body); | ||
}); | ||
describe('GET /users/:id, POST /users', () => { | ||
beforeEach(async function() { | ||
await app.model.User.truncate(); | ||
}); | ||
|
||
it('should create and get user successfully', async () => { | ||
const res = await app.httpRequest() | ||
.post('/users') | ||
.send({ | ||
nickname: 'jack', | ||
email: '[email protected]', | ||
}); | ||
assert(res.status === 200); | ||
assert(res.body.id); | ||
assert(res.body.nickname === 'jack'); | ||
assert(res.body.email === '[email protected]'); | ||
assert(res.body.createdAt); | ||
|
||
const res2 = await app.httpRequest() | ||
.get(`/users/${res.body.id}`) | ||
.send({ | ||
nickname: 'jack', | ||
email: '[email protected]', | ||
}); | ||
assert(res2.status === 200); | ||
assert.deepEqual(res2.body, res.body); | ||
}); | ||
}); | ||
}); |