Skip to content

Commit

Permalink
Merge pull request #28 from tim-s-ccs/add-request-as-an-attribute
Browse files Browse the repository at this point in the history
Make request an attribute on active model
  • Loading branch information
tim-s-ccs authored Feb 21, 2022
2 parents 4e49431 + f359601 commit f03409c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/models/activeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { utils } from '..'
import { ValidationSchema, ValidationScheme } from '../types/validation/validationSchema'

abstract class ActiveModel extends Model implements ActiveModelInterface {
req: Request
data: ActiveModelData = this.data

abstract tableName: string
Expand All @@ -19,8 +20,9 @@ abstract class ActiveModel extends Model implements ActiveModelInterface {

errors: ActiveModelErrors = {}

constructor(data: ActiveModelData) {
constructor(req: Request, data: ActiveModelData) {
super(data)
this.req = req
}

// TODO: Possilby static init model
Expand Down Expand Up @@ -164,23 +166,23 @@ abstract class ActiveModel extends Model implements ActiveModelInterface {
}
}

private _save = (req: Request, dataInterface: DataInterfaceFunction): void => {
private _save = (dataInterface: DataInterfaceFunction): void => {
const activeModelAttributes: Array<string> = Object.keys(this.data).filter((attribute) => this.data[attribute] instanceof ActiveModel)

activeModelAttributes.forEach(activeModelAttribute => (this.data[activeModelAttribute] as ActiveModel)._save(req, dataInterface))
activeModelAttributes.forEach(activeModelAttribute => (this.data[activeModelAttribute] as ActiveModel)._save(dataInterface))

if ('updatedAt' in this.modelSchema) this.data['updatedAt'] = utils.dateHelpers.getCurrentDateTimeString()

dataInterface(req, this.tableName, this.data.id, this.attributes())
dataInterface(this.req, this.tableName, this.data.id, this.attributes())
}

protected beforeSave?(): void

save = (req: Request, call: string = ''): boolean => {
save = (call: string = ''): boolean => {
if (this.validate(call)) {
this.beforeSave && this.beforeSave()

this._save(req, setActiveRow)
this._save(setActiveRow)

return true
} else {
Expand All @@ -190,13 +192,13 @@ abstract class ActiveModel extends Model implements ActiveModelInterface {

protected beforeCreate?(): void

create = (req: Request): boolean => {
create = (): boolean => {
this.beforeCreate && this.beforeCreate()

if (this.validate('new')) {
this.beforeSave && this.beforeSave()

this._save(req, addActiveRow)
this._save(addActiveRow)

return true
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/types/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ModelInterface {
}

export interface ActiveModelInterface {
req: Request
data: ActiveModelData
tableName: string
modelSchema: ModelSchema
Expand All @@ -18,8 +19,8 @@ export interface ActiveModelInterface {
errorList(): Array<ListError>
attributes(): TableRow
assignAttributes(date: ActiveModelData): void
save(req: Request, call: string): boolean
create(req: Request): boolean
save(call: string): boolean
create(): boolean
}

export interface StaticModelInterface {
Expand Down

0 comments on commit f03409c

Please sign in to comment.