From 10257b82948594f9e106ad49734fb6eec1db9e1b Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 27 Jun 2020 14:58:39 +0200 Subject: [PATCH] ManyModelRelation: add cloneReduceToOne method --- package.json | 2 +- src/auth/models/User.ts | 2 +- src/db/ModelRelation.ts | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c97d56e..72ccdc6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.10.1", + "version": "0.10.2", "description": "Node web framework", "repository": "git@gitlab.com:ArisuOngaku/wms-core.git", "author": "Alice Gaudon ", diff --git a/src/auth/models/User.ts b/src/auth/models/User.ts index 1e41ea9..19a7b87 100644 --- a/src/auth/models/User.ts +++ b/src/auth/models/User.ts @@ -22,7 +22,7 @@ export default class User extends Model { foreignKey: 'user_id' }); - public readonly mainEmail = this.emails.clone().constraint(q => q.where('main', true)); + public readonly mainEmail = this.emails.cloneReduceToOne().constraint(q => q.where('main', true)); protected init(): void { this.addProperty('name', new Validator().acceptUndefined().between(3, 64)); diff --git a/src/db/ModelRelation.ts b/src/db/ModelRelation.ts index 7cec698..dff0c31 100644 --- a/src/db/ModelRelation.ts +++ b/src/db/ModelRelation.ts @@ -86,6 +86,10 @@ export class ManyModelRelation extends ModelRe return new ManyModelRelation(this.model, this.foreignModelClass, this.dbProperties); } + public cloneReduceToOne(): OneModelRelation { + return new OneModelRelation(this.model, this.foreignModelClass, this.dbProperties); + } + public getModelID(): any { return this.model[this.dbProperties.localKey]; }