-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return a promise from relationships (#190)
- Loading branch information
1 parent
38d7a9b
commit 5aeb903
Showing
7 changed files
with
266 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// @flow | ||
import { camelize } from 'inflection'; | ||
|
||
import relatedFor from './utils/related-for'; | ||
|
||
import type Model from '../model'; | ||
|
||
export async function get( | ||
owner: Model, | ||
key: string | ||
): Array<Model> | ?Model { | ||
const options = owner.constructor.relationshipFor(key); | ||
let relationship; | ||
|
||
const { | ||
type, | ||
model | ||
} = options; | ||
|
||
if (model) { | ||
const related = relatedFor(owner); | ||
let { foreignKey } = options; | ||
|
||
foreignKey = camelize(foreignKey, true); | ||
relationship = related.get(key); | ||
|
||
if (!relationship) { | ||
switch (type) { | ||
case 'hasOne': | ||
case 'hasMany': | ||
relationship = model.where({ | ||
[foreignKey]: owner[owner.constructor.primaryKey] | ||
}); | ||
|
||
if (type === 'hasOne') { | ||
relationship = relationship.first(); | ||
} | ||
|
||
relationship = await relationship; | ||
break; | ||
|
||
case 'belongsTo': | ||
const foreignVal = owner[foreignKey]; | ||
|
||
if (foreignVal) { | ||
relationship = await model.find(foreignVal); | ||
} | ||
break; | ||
} | ||
|
||
set(owner, key, relationship); | ||
} | ||
} | ||
|
||
return relationship; | ||
} | ||
|
||
export function set( | ||
owner: Model, | ||
key: string, | ||
val: Array<Model> | ?Model | ||
): void { | ||
const { type, model } = owner.constructor.relationshipFor(key); | ||
const related = relatedFor(owner); | ||
|
||
switch (type) { | ||
case 'hasMany': | ||
if (Array.isArray(val)) { | ||
related.set(key, val); | ||
} | ||
break; | ||
|
||
case 'hasOne': | ||
case 'belongsTo': | ||
if (val && typeof val === 'object' && !model.isInstance(val)) { | ||
val = new model(val); | ||
} | ||
|
||
related.set(key, val); | ||
break; | ||
} | ||
} |
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,9 @@ | ||
// @flow | ||
import type Model from '../model'; | ||
|
||
export type options = { | ||
type: 'hasOne' | 'hasMany' | 'belongsTo'; | ||
model: Model; | ||
inverse: string; | ||
foreignKey: string; | ||
}; |
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,15 @@ | ||
// @flow | ||
import type Model from '../../model'; | ||
|
||
const REFS: WeakMap<Model, Map<string, Model>> = new WeakMap(); | ||
|
||
export default function relatedFor(owner: Model): Map<string, Model> { | ||
let related = REFS.get(owner); | ||
|
||
if (!related) { | ||
related = new Map(); | ||
REFS.set(owner, related); | ||
} | ||
|
||
return related; | ||
} |
Oops, something went wrong.