-
-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
146 additions
and
44 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
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,37 @@ | ||
import { Relation } from '../../../interface' | ||
import { PGCatalog, PGCatalogForeignKeyConstraint } from '../../introspection' | ||
import PGObjectType from '../type/PGObjectType' | ||
import PGCollection from './PGCollection' | ||
import PGCollectionKey from './PGCollectionKey' | ||
|
||
// TODO: This implementation is sketchy. Implement it better! | ||
class PGRelation implements Relation<PGObjectType.Value> { | ||
constructor ( | ||
public tailCollection: PGCollection, | ||
public headCollectionKey: PGCollectionKey, | ||
private _pgConstraint: PGCatalogForeignKeyConstraint, | ||
) {} | ||
|
||
private _pgCatalog = this.tailCollection._pgCatalog | ||
private _pgTailAttributes = this._pgCatalog.getClassAttributes(this._pgConstraint.classId, this._pgConstraint.keyAttributeNums) | ||
private _tailFieldNames = this._pgTailAttributes.map(pgAttribute => this.tailCollection.type.getPGAttributeFieldName(pgAttribute)!) | ||
private _headFieldNames = Array.from(this.headCollectionKey.keyType.fields.keys()) | ||
|
||
/** | ||
* Construct the name for this relation using the native Postgres foreign key | ||
* constraint attributes. | ||
*/ | ||
public readonly name = this._tailFieldNames.join('_and_') | ||
|
||
/** | ||
* Gets an instance of the head collection’s key type by just extracting some keys from | ||
*/ | ||
public getHeadKeyFromTailValue (value: PGObjectType.Value): PGObjectType.Value { | ||
return this._tailFieldNames.reduce((headKey, tailFieldName, i) => { | ||
headKey.set(this._headFieldNames[i], value.get(tailFieldName)) | ||
return headKey | ||
}, new Map()) | ||
} | ||
} | ||
|
||
export default PGRelation |
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