ModelRelation: add count(), has() methods
This commit is contained in:
parent
74ffdf8325
commit
4b9b62517b
@ -53,6 +53,18 @@ export default abstract class ModelRelation<S extends Model, O extends Model, R
|
|||||||
public abstract async eagerLoad(relations: ModelRelation<S, O, R>[]): Promise<ModelQueryResult<O>>;
|
public abstract async eagerLoad(relations: ModelRelation<S, O, R>[]): Promise<ModelQueryResult<O>>;
|
||||||
|
|
||||||
public abstract async populate(models: ModelQueryResult<O>): Promise<void>;
|
public abstract async populate(models: ModelQueryResult<O>): Promise<void>;
|
||||||
|
|
||||||
|
public async count(): Promise<number> {
|
||||||
|
const models = await this.get();
|
||||||
|
if (Array.isArray(models)) return models.length;
|
||||||
|
else return models !== null ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async has(model: O): Promise<boolean> {
|
||||||
|
const models = await this.get();
|
||||||
|
if (Array.isArray(models)) return models.find(m => m.equals(model)) !== undefined;
|
||||||
|
else return models && models.equals(model);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class OneModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O | null> {
|
export class OneModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O | null> {
|
||||||
@ -154,7 +166,6 @@ export class ManyModelRelation<S extends Model, O extends Model> extends ModelRe
|
|||||||
public async populate(models: ModelQueryResult<O>): Promise<void> {
|
public async populate(models: ModelQueryResult<O>): Promise<void> {
|
||||||
this.cachedModels = models.filter(m => m[this.dbProperties.foreignKey] === this.getModelID());
|
this.cachedModels = models.filter(m => m[this.dbProperties.foreignKey] === this.getModelID());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ManyThroughModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> {
|
export class ManyThroughModelRelation<S extends Model, O extends Model> extends ModelRelation<S, O, O[]> {
|
||||||
@ -212,10 +223,8 @@ export class ManyThroughModelRelation<S extends Model, O extends Model> extends
|
|||||||
.map(p => p[`pivot.${this.dbProperties.foreignPivotKey}`]);
|
.map(p => p[`pivot.${this.dbProperties.foreignPivotKey}`]);
|
||||||
this.cachedModels = models.filter(m => ids.indexOf(m[this.dbProperties.foreignKey]) >= 0);
|
this.cachedModels = models.filter(m => ids.indexOf(m[this.dbProperties.foreignKey]) >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export type QueryModifier<M extends Model> = (query: ModelQuery<M>) => ModelQuery<M>;
|
export type QueryModifier<M extends Model> = (query: ModelQuery<M>) => ModelQuery<M>;
|
||||||
|
|
||||||
export type ModelFilter<O extends Model> = (model: O) => boolean | Promise<boolean>;
|
export type ModelFilter<O extends Model> = (model: O) => boolean | Promise<boolean>;
|
||||||
|
Loading…
Reference in New Issue
Block a user