Models unit; session model; generalize session classes/interfaces
This commit is contained in:
20
orm/src/ModelsUnit.ts
Normal file
20
orm/src/ModelsUnit.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import {CanonicalDefinition} from '../../lib/src/unit/Canonical.ts'
|
||||
import {Model} from './model/Model.ts'
|
||||
import {Unit} from '../../lib/src/lifecycle/decorators.ts'
|
||||
import {StaticCanonical} from '../../lib/src/unit/StaticCanonical.ts'
|
||||
|
||||
@Unit()
|
||||
export default class ModelsUnit extends StaticCanonical<Model<any>, typeof Model> {
|
||||
protected base_path = './app/models'
|
||||
protected canonical_item = 'model'
|
||||
protected suffix = '.model.ts'
|
||||
|
||||
public async init_canonical_item(def: CanonicalDefinition) {
|
||||
const item = await super.init_canonical_item(def)
|
||||
if ( !(item.prototype instanceof Model) ) {
|
||||
throw new TypeError(`Invalid model definition: ${def.original_name}. Models must extend from Daton ORM's base Model class.`)
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,10 @@ export default abstract class ConnectionExecutable<T> {
|
||||
return 0
|
||||
}
|
||||
|
||||
async exists(): Promise<boolean> {
|
||||
return (await this.count()) > 0
|
||||
}
|
||||
|
||||
async execute_in_connection(connection: string | Connection): Promise<QueryResult> {
|
||||
const conn = typeof connection === 'string' ? make(Database).connection(connection) : connection
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ export default class PostgresConnection extends Connection {
|
||||
const result = await this._client.query(query)
|
||||
|
||||
let base_i = 0
|
||||
const cols = collect(result?.rowDescription?.columns || []).sortBy('index').map(col => {
|
||||
const cols = collect(result?.rowDescription?.columns || []).map(col => {
|
||||
col.index = base_i
|
||||
base_i += 1
|
||||
return col
|
||||
|
||||
@@ -24,7 +24,7 @@ export abstract class Model<T extends Model<T>> extends Builder<T> {
|
||||
* The name of the connection this model should run through.
|
||||
* @type string
|
||||
*/
|
||||
protected static connection: string
|
||||
protected static connection: string = 'default'
|
||||
|
||||
/**
|
||||
* The name of the table this model is stored in.
|
||||
@@ -42,13 +42,13 @@ export abstract class Model<T extends Model<T>> extends Builder<T> {
|
||||
* Optionally, the timestamp field set on creation.
|
||||
* @type string
|
||||
*/
|
||||
protected static readonly CREATED_AT = 'created_at'
|
||||
protected static readonly CREATED_AT: string | null = 'created_at'
|
||||
|
||||
/**
|
||||
* Optionally, the timestamp field set op update.
|
||||
* @type string
|
||||
*/
|
||||
protected static readonly UPDATED_AT = 'updated_at'
|
||||
protected static readonly UPDATED_AT: string | null = 'updated_at'
|
||||
|
||||
/**
|
||||
* If true, the CREATED_AT and UPDATED_AT columns will be automatically set.
|
||||
@@ -569,7 +569,7 @@ export abstract class Model<T extends Model<T>> extends Builder<T> {
|
||||
* @param without_timestamps - if true, the UPDATED_AT/CREATED_AT timestamps will not be touched
|
||||
* @return Promise<Model>
|
||||
*/
|
||||
public async save({ without_timestamps = false }): Promise<Model<T>> {
|
||||
public async save({ without_timestamps = false } = {}): Promise<Model<T>> {
|
||||
await this.saving$.next(this)
|
||||
const constructor = (this.constructor as typeof Model)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user