Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 6x 6x 143x 56x 87x 143x 6x 39x 39x 37x 37x 6x 2x 2x 6x 41x 41x 2x 39x 39x 39x 6x 4x 4x 4x 4x 4x 4x 6x | import {db} from "./database";
import {Model} from "./model";
import {query} from "./query.builder";
import {logQuery} from "./yago.logger";
export abstract class Base {
protected model: any;
public tableName(): string {
let name;
if(this.model) {
name = this.model.name;
} else {
name = this.constructor.name;
}
return name.toLowerCase() + 's';
}
protected insert(model?: any): number | string {
model = model || this;
query.insert(model).into(this.tableName()).run();
model.id = db.getIdLastRecordInserted();
return model.id;
}
protected update(model?: any): number | string {
query.table(this.tableName()).update(model).where('id', model.id).run();
return model.id;
}
public save(model?: any): number | string {
model = model || this;
let idModelSaved: number | string;
if (model.isSaved()) {
idModelSaved = this.update(model);
} else {
idModelSaved = this.insert(model);
}
(idModelSaved > 0) && logQuery(`Saved ${model.constructor.name} with id: ${idModelSaved}`, 'result');
return idModelSaved;
}
public remove(model?: any): number {
model = model || this;
const deleteQuery = `delete from ${this.tableName()} where id=${model.id}`;
db.run(deleteQuery);
logQuery(deleteQuery, 'query');
logQuery(`Deleted ${this.constructor.name} with id: ${model.id}`, 'result');
return model.id;
}
}
|