Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
feat(joins): default local and foreign keys (#12)
Browse files Browse the repository at this point in the history
* feat: automatic local and foreign keys

* chore: version bump
  • Loading branch information
gjuchault authored Mar 20, 2017
1 parent b500670 commit 0fb3455
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,41 @@ user
### hasOne

```js
User.hasOne('Role', 'role', 'user_id')
User.hasOne('Role', 'role', 'User_id')
```

Arguments are:

1. Relation Model name (the first argument of `requelize.model`)
2. The virutal field on the instance (so that you have `user.role.id`)
3. The foreign key to use (usually: `model_id` or `modelId` or `Model_id`)
3. The foreign key to use. requelize defaults to `SelfModel_id`

### hasMany

```js
User.hasMany('Post', 'posts', 'user_id')
User.hasMany('Post', 'posts', 'User_id')
```

Arguments are:

1. Relation Model name (the first argument of `requelize.model`)
2. The virutal field on the instance (so that you have `user.posts[0].id`)
3. The foreign key to use (usually: `model_id` or `modelId` or `Model_id`)
3. The foreign key to use. requelize defaults to `SelfModel_id`

The foreign key have to be equal to the local key used on `belongsTo` method

### belongsTo

```js
Role.belongsTo('User', 'user', 'user_id')
Post.belongsTo('User', 'user', 'user_id')
Role.belongsTo('User', 'user', 'User_id')
Post.belongsTo('User', 'user', 'User_id')
```

Arguments are:

1. Relation Model name (the first argument of `requelize.model`)
2. The virutal field on the instance (so that you have `role.user.id` or `post.user.id`)
3. The local key to use (usually: `model_id` or `modelId` or `Model_id`)
3. The local key to use (usually: `model_id` or `modelId` or `Model_id`). requelize defaults to `Model_id`

### belongsToMany

Expand Down
12 changes: 6 additions & 6 deletions lib/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ function createModel (requelize) {

/**
* Generate an hasOne relation
* @param {string} model Model name
* @param {string} field Field name
* @param {string} foreignKey Foreign key
* @param {string} model Model name
* @param {string} field Field name
* @param {string} [foreignKey] Foreign key. Defaults to {thismodel}_id
*/
static hasOne (model, field, foreignKey) {
static hasOne (model, field, foreignKey = `${Model._name}_id`) {
Model._relModel(field)
Model._joins[field] = ({ type: 'hasOne', model, field, foreignKey })
}
Expand All @@ -139,7 +139,7 @@ function createModel (requelize) {
* @param {string} field Field name
* @param {string} foreignKey Foreign key
*/
static hasMany (model, field, foreignKey) {
static hasMany (model, field, foreignKey = `${Model._name}_id`) {
Model._relModel(field)
Model._joins[field] = ({ type: 'hasMany', model, field, foreignKey })
}
Expand All @@ -150,7 +150,7 @@ function createModel (requelize) {
* @param {string} field Field name
* @param {string} localKey Foreign key
*/
static belongsTo (model, field, localKey) {
static belongsTo (model, field, localKey = `${model}_id`) {
Model._relModel(field)
Model._relField(localKey)
Model._joins[field] = ({ type: 'belongsTo', model, field, localKey })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "requelize",
"version": "0.4.0",
"version": "0.5.0",
"description": "RethinkDB ORM",
"main": "index.js",
"repository": "https://github.com/buckless/requelize.git",
Expand Down
51 changes: 51 additions & 0 deletions test/test.default-join-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Joi = require('joi')
const { test, requelize, dropDb } = require('./utils')

test('relationships - belongsTo', (t) => {
t.plan(2)

let Foo
let Bar

let foo
let bar

dropDb()
.then(() => {
Foo = requelize.model('foo', { name: Joi.string() })
Bar = requelize.model('bar', { name: Joi.string() })

Foo.hasOne('bar', 'bar')
Bar.belongsTo('foo', 'foo')

return requelize.sync()
})
.then(() => {
foo = new Foo({ name: 'foo1' })

return foo.save()
})
.then(() => {
bar = new Bar({ name: 'bar1', foo_id: foo.id })

return bar.save()
})
.then(() => {
foo.bar_id = bar.id

return foo.save()
})
.then(() => {
return Bar.get(bar.id).embed({ foo: true })
})
.then((res) => {
t.ok(res && res.foo, 'embedded model')
t.equal(res.foo.id, foo.id, 'relation B-A')
})
.catch((err) => {
t.fail(err)
})
.then(() => {
t.end()
})
})

0 comments on commit 0fb3455

Please sign in to comment.