Created
May 16, 2020 18:58
-
-
Save bbangert/cfef65bdd48e28c24e19588535a18e2b to your computer and use it in GitHub Desktop.
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
import { AuthBaseModel } from './auth-base'; | |
import { Emails } from './emails'; | |
export class Account extends AuthBaseModel { | |
public static tableName = 'accounts'; | |
public static idColumn = 'uid'; | |
protected uuidFields = ['uid']; | |
public uid!: string; | |
public createdAt!: number; | |
public locale!: string; | |
public emails!: Emails[]; | |
public static relationMappings = { | |
emails: { | |
join: { | |
from: 'accounts.uid', | |
to: 'emails.uid', | |
}, | |
modelClass: Emails, | |
relation: AuthBaseModel.HasManyRelation, | |
}, | |
}; | |
} |
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
import { BaseModel } from '../base'; | |
export class AuthBaseModel extends BaseModel {} |
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
import { Model } from 'objection'; | |
import { intBoolTransformer, uuidTransformer } from '../transformers'; | |
/** | |
* Base Model for helpers that should be present on all models. | |
*/ | |
export class BaseModel extends Model { | |
protected uuidFields: string[] = []; | |
protected intBoolFields: string[] = []; | |
public $parseDatabaseJson(json: any) { | |
json = super.$parseDatabaseJson(json); | |
for (const field of this.uuidFields) { | |
if (json[field]) { | |
json[field] = uuidTransformer.from(json[field]); | |
} | |
} | |
for (const field of this.intBoolFields) { | |
if (json[field]) { | |
json[field] = intBoolTransformer.from(json[field]); | |
} | |
} | |
return json; | |
} | |
public $formatDatabaseJson(json: any) { | |
json = super.$formatDatabaseJson(json); | |
for (const field of this.uuidFields) { | |
if (json[field]) { | |
json[field] = uuidTransformer.to(json[field]); | |
} | |
} | |
for (const field of this.intBoolFields) { | |
if (json[field]) { | |
json[field] = intBoolTransformer.to(json[field]); | |
} | |
} | |
return json; | |
} | |
} |
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
import { AuthBaseModel } from './auth-base'; | |
export class Emails extends AuthBaseModel { | |
public static tableName = 'emails'; | |
protected uuidFields = ['uid', 'emailCode']; | |
protected intBoolFields = ['isVerified', 'isPrimary']; | |
public normalizedEmail!: string; | |
public email!: string; | |
public uid!: string; | |
public isVerified!: boolean; | |
public isPrimary!: boolean; | |
public emailCode!: string; | |
public verifiedAt?: number; | |
public createdAt!: number; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment