mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added property name function to composite index decorator
This commit is contained in:
parent
8902402acc
commit
27b9dabefc
@ -4,8 +4,10 @@ import {Index} from "../../../src/decorator/indices/Index";
|
||||
import {CompositeIndex} from "../../../src/decorator/indices/CompositeIndex";
|
||||
|
||||
@Table("sample16_post")
|
||||
@CompositeIndex(["title", "text"])
|
||||
@CompositeIndex("my_index_with_id_and_text", ["id", "text"])
|
||||
@CompositeIndex(["title", "likesCount"])
|
||||
@CompositeIndex((post: Post) => [post.title, post.text])
|
||||
@CompositeIndex("my_index_with_id_and_title", (post: Post) => [post.id, post.title])
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
|
||||
@ -6,9 +6,11 @@ import {defaultMetadataStorage} from "../../typeorm";
|
||||
*/
|
||||
export function CompositeIndex(name: string, fields: string[]): Function;
|
||||
export function CompositeIndex(fields: string[]): Function;
|
||||
export function CompositeIndex(nameOrFields: string|string[], maybeFields?: string[]): Function {
|
||||
export function CompositeIndex(fields: (object: any) => any[]): Function;
|
||||
export function CompositeIndex(name: string, fields: (object: any) => any[]): Function;
|
||||
export function CompositeIndex(nameOrFields: string|string[]|((object: any) => any[]), maybeFields?: ((object: any) => any[])|string[]): Function {
|
||||
const name = typeof nameOrFields === "string" ? nameOrFields : undefined;
|
||||
const fields = typeof nameOrFields === "string" ? <string[]> maybeFields : nameOrFields;
|
||||
const fields = typeof nameOrFields === "string" ? <((object: any) => any[])|string[]> maybeFields : nameOrFields;
|
||||
|
||||
return function (cls: Function) {
|
||||
defaultMetadataStorage().compositeIndexMetadatas.add(new CompositeIndexMetadata(cls, name, fields));
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import {TargetMetadata} from "./TargetMetadata";
|
||||
import {NamingStrategyInterface} from "../naming-strategy/NamingStrategy";
|
||||
import {EntityMetadata} from "./EntityMetadata";
|
||||
|
||||
/**
|
||||
* This metadata interface contains all information about table's composite index.
|
||||
@ -15,15 +16,15 @@ export class CompositeIndexMetadata extends TargetMetadata {
|
||||
*/
|
||||
namingStrategy: NamingStrategyInterface;
|
||||
|
||||
/**
|
||||
* Entity metadata of the class to which this index is applied.
|
||||
*/
|
||||
entityMetadata: EntityMetadata;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Columns combination to be used as index.
|
||||
*/
|
||||
readonly columns: string[];
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// ---------------------------------------------------------------------
|
||||
@ -33,13 +34,18 @@ export class CompositeIndexMetadata extends TargetMetadata {
|
||||
*/
|
||||
private readonly _name: string;
|
||||
|
||||
/**
|
||||
* Columns combination to be used as index.
|
||||
*/
|
||||
private readonly _columns: ((object: any) => any[])|string[];
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
constructor(target: Function, name: string|undefined, columns: string[]) {
|
||||
constructor(target: Function, name: string|undefined, columns: ((object: any) => any[])|string[]) {
|
||||
super(target);
|
||||
this.columns = columns;
|
||||
this._columns = columns;
|
||||
if (name)
|
||||
this._name = name;
|
||||
}
|
||||
@ -52,4 +58,15 @@ export class CompositeIndexMetadata extends TargetMetadata {
|
||||
return this.namingStrategy.indexName(this.target, this._name, this.columns);
|
||||
}
|
||||
|
||||
get columns() {
|
||||
|
||||
// if columns already an array of string then simply return it
|
||||
if (this._columns instanceof Array)
|
||||
return this._columns;
|
||||
|
||||
// if columns is a function that returns array of field names then execute it and get columns names from it
|
||||
const propertiesMap = this.entityMetadata.createPropertiesMap();
|
||||
return this._columns(propertiesMap).map((i: any) => String(i));
|
||||
}
|
||||
|
||||
}
|
||||
@ -38,6 +38,9 @@ export class EntityMetadata {
|
||||
// this.indices = indices;
|
||||
this.compositeIndices = compositeIndices;
|
||||
this.foreignKeys = foreignKeys;
|
||||
|
||||
// this.relations.forEach(relation => relation.entityMetadata = this);
|
||||
this.compositeIndices.forEach(index => index.entityMetadata = this);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user