Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"./commands/*": "./build/commands/*.js",
"./factories": "./build/src/factories/main.js",
"./database": "./build/src/database/main.js",
"./query_runner": "./build/src/query_runner/main.js",
"./orm": "./build/src/orm/main.js",
"./orm/relations": "./build/src/orm/relations/main.js",
"./seeders": "./build/src/seeders/main.js",
Expand Down
63 changes: 37 additions & 26 deletions src/orm/query_builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,15 @@ export class ModelQueryBuilder
}
}

public isWriteQuery() {
return ['update', 'del', 'insert'].includes((this.knexQuery as any)['_method'])
}

/**
* Executes the current query
* Convert fetched results to an array of model instances
*/
private async execQuery() {
this.applyWhere()

const isWriteQuery = ['update', 'del', 'insert'].includes((this.knexQuery as any)['_method'])
const queryData = Object.assign(this.getQueryData(), this.customReporterData)
const rows = await new QueryRunner(this.client, this.debugQueries, queryData).run(
this.knexQuery
)

/**
* Return the rows as it is when query is a write query
*/
if (isWriteQuery || !this.wrapResultsToModelInstances) {
return Array.isArray(rows) ? rows : [rows]
}

/**
* Convert fetched results to an array of model instances
*/
const modelInstances = rows.reduce((models: LucidRow[], row: ModelObject) => {
public convertRowsToModelInstances(rows: any): LucidRow[] {
return rows.reduce((models: LucidRow[], row: ModelObject) => {
if (isObject(row)) {
const modelInstance = this.model.$createFromAdapterResult(
row,
Expand All @@ -209,14 +195,39 @@ export class ModelQueryBuilder
}
return models
}, [])
}

/**
* Preload for model instances
*/
await this.preloader
/**
* Preload for model instances
*/
public async preloadFromModels(models: LucidRow[]): Promise<void> {
return this.preloader
.sideload(this.sideloaded)
.debug(this.debugQueries)
.processAllForMany(modelInstances, this.client)
.processAllForMany(models, this.client)
}

/**
* Executes the current query
*/
private async execQuery() {
this.applyWhere()

const queryData = Object.assign(this.getQueryData(), this.customReporterData)
const rows = await new QueryRunner(this.client, this.debugQueries, queryData).run(
this.knexQuery
)

/**
* Return the rows as it is when query is a write query
*/
if (this.isWriteQuery() || !this.wrapResultsToModelInstances) {
return Array.isArray(rows) ? rows : [rows]
}

const modelInstances = this.convertRowsToModelInstances(rows)

await this.preloadFromModels(modelInstances)

return modelInstances
}
Expand Down
Loading