Skip to content

Instantly share code, notes, and snippets.

@bbangert
Created May 16, 2020 18:58
Show Gist options
  • Save bbangert/cfef65bdd48e28c24e19588535a18e2b to your computer and use it in GitHub Desktop.
Save bbangert/cfef65bdd48e28c24e19588535a18e2b to your computer and use it in GitHub Desktop.
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,
},
};
}
import { BaseModel } from '../base';
export class AuthBaseModel extends BaseModel {}
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;
}
}
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