Skip to content

Commit

Permalink
refactor: simply API to get-set model $options
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent 156073a commit 29c3d6e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
16 changes: 9 additions & 7 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,15 @@ declare module '@ioc:Adonis/Lucid/Model' {
> extends ChainableContract<Model['$refs']> {
model: Model

client: QueryClientContract,
/**
* A copy of client options. They can be set on any model instance
*/
readonly clientOptions: ModelOptions

/**
* Reference to query client used for making queries
*/
client: QueryClientContract

/**
* A custom set of sideloaded properties defined on the query
Expand Down Expand Up @@ -408,12 +416,6 @@ declare module '@ioc:Adonis/Lucid/Model' {
client: QueryClientContract,
): ReturnType<QueryClientContract['query']> | ReturnType<QueryClientContract['insertQuery']>

/**
* Read/write options
*/
$setOptions (options?: ModelAdapterOptions): void
$getOptions (): ModelOptions | undefined

/**
* Read/write attributes
*/
Expand Down
49 changes: 21 additions & 28 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class BaseModel implements ModelContract {
const instance = new this()
instance.$consumeAdapterResult(adapterResult, sideloadAttributes)
instance.$hydrateOriginals()
instance.$setOptions(options)
instance.$options = options
instance.$persisted = true
instance.$isLocal = false

Expand Down Expand Up @@ -383,7 +383,7 @@ export class BaseModel implements ModelContract {

if (!row) {
row = new this() as InstanceType<T>
row.$setOptions({ client: query.client })
row.$options = query.clientOptions
row.fill(Object.assign({}, search, savePayload))
return row
}
Expand All @@ -404,6 +404,12 @@ export class BaseModel implements ModelContract {
return new Proxy(this, proxyHandler)
}

/**
* Custom options defined on the model instance that are
* passed to the adapter
*/
private _options?: ModelOptions

/**
* Reference to transaction that will be used for performing queries on a given
* model instance.
Expand Down Expand Up @@ -505,12 +511,6 @@ export class BaseModel implements ModelContract {
*/
public $isDeleted: boolean = false

/**
* Custom options defined on the model instance that are
* passed to the adapter
*/
public $options: ModelOptions

/**
* Returns the value of primary key. The value must be
* set inside attributes object
Expand Down Expand Up @@ -623,37 +623,30 @@ export class BaseModel implements ModelContract {
}

/**
* Sets the options on the model instance
* Get options
*/
public $setOptions (options?: ModelAdapterOptions) {
if (!options) {
return
}

this.$options = this.$options || {}
public get $options (): ModelOptions | undefined {
return this._options
}

if (options.client) {
this.$options.connection = options.client.connectionName
this.$options.profiler = options.client.profiler
/**
* Set options
*/
public set $options (options: ModelOptions | undefined) {
if (!options) {
return
}

this._options = this._options || {}
if (options.connection) {
this.$options.connection = options.connection
this._options.connection = options.connection
}

if (options.profiler) {
this.$options.profiler = options.profiler
this._options.profiler = options.profiler
}
}

/**
* Returns $options
*/
public $getOptions (): ModelOptions | undefined {
return this.$options
}

/**
* Set attribute
*/
Expand Down Expand Up @@ -860,7 +853,7 @@ export class BaseModel implements ModelContract {
preloader.preload(relationName, callback)
}

await preloader.processAllForOne(this, constructor.query(this.$getOptions()).client)
await preloader.processAllForOne(this, constructor.query(this.$options).client)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
/**
* Options that must be passed to all new model instances
*/
private _options: ModelOptions = {
public clientOptions: ModelOptions = {
connection: this.client.connectionName,
profiler: this.client.profiler,
}
Expand All @@ -73,7 +73,7 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
const modelInstances = this.model.$createMultipleFromAdapterResult(
rows,
this._sideloaded,
this._options,
this.clientOptions,
)

await this._preloader.processAllForMany(modelInstances, this.client)
Expand Down

0 comments on commit 29c3d6e

Please sign in to comment.