start relations (has one or many; has one; has many)

This commit is contained in:
garrettmills
2020-08-16 09:32:22 -05:00
parent 2fd3c6c22b
commit 81906b02bc
15 changed files with 259 additions and 21 deletions

View File

@@ -18,3 +18,17 @@ await scaffolding.up()
*/
const app = make(Application, units)
await app.run()
import UserModel from './models/User.model.ts'
import ObjectResultOperator from "../orm/src/builder/type/result/ObjectResultOperator.ts";
// const users = await UserModel.find_one({ username: 'garrettmills' })
const sel = UserModel.select('*').with('login_attempts').where('username', '=', 'garrettmills')
console.log(sel)
console.log(sel.clone())
const gm = await sel.results().first()
console.log(gm)
console.log(await gm.login_attempts())
// console.log(await las.fetchRelated())

View File

@@ -0,0 +1,20 @@
import {Model} from '../../orm/src/model/Model.ts'
import {Field} from '../../orm/src/model/Field.ts'
import {Type} from '../../orm/src/db/types.ts'
export default class LoginAttemptModel extends Model<LoginAttemptModel> {
protected static table = 'daton_login_attempts'
protected static key = 'daton_login_attempt_id'
protected static readonly CREATED_AT = 'attempt_date'
protected static readonly UPDATED_AT = null
@Field(Type.int)
protected daton_login_attempt_id?: number
@Field(Type.int)
protected user_id!: number
@Field(Type.timestamp)
protected attempt_date!: Date
}

30
app/models/User.model.ts Normal file
View File

@@ -0,0 +1,30 @@
import {Model} from '../../orm/src/model/Model.ts'
import {Type} from '../../orm/src/db/types.ts'
import {Field} from '../../orm/src/model/Field.ts'
import LoginAttemptModel from './LoginAttempt.model.ts'
import {Relation} from '../../orm/src/model/relation/decorators.ts'
export default class UserModel extends Model<UserModel> {
protected static table = 'daton_users'
protected static key = 'user_id'
protected static readonly CREATED_AT = 'created_at'
protected static readonly UPDATED_AT = 'updated_at'
@Field(Type.int)
protected user_id?: number
@Field(Type.varchar)
protected first_name!: String
@Field(Type.varchar)
protected last_name!: String
@Field(Type.bool)
protected active!: Boolean
@Relation()
public login_attempts() {
return this.has_many(LoginAttemptModel)
}
}

View File

@@ -23,5 +23,5 @@ export default [
ViewEngineUnit,
RoutesUnit,
RoutingUnit,
HttpServerUnit,
// HttpServerUnit,
]