Skip to content

Commit

Permalink
feat: adding static find methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 30, 2019
1 parent be37cfb commit c7a62f8
Show file tree
Hide file tree
Showing 10 changed files with 888 additions and 52 deletions.
46 changes: 29 additions & 17 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ declare module '@ioc:Adonis/Lucid/Model' {
profiler?: ProfilerContract | ProfilerRowContract,
}

/**
* Adapter and also accept a client directly
*/
export type ModelAdapterOptions = ModelOptions & {
client?: QueryClientContract,
}

type DecoratorFn = (target, property) => void

/**
Expand Down Expand Up @@ -116,10 +123,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
> extends ChainableContract<Model['$refs']> {
model: Model

/**
* A copy of options based to the query builder
*/
options?: ModelOptions
client: QueryClientContract,

/**
* A custom set of sideloaded properties defined on the query
Expand Down Expand Up @@ -189,6 +193,12 @@ 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 Expand Up @@ -218,8 +228,7 @@ declare module '@ioc:Adonis/Lucid/Model' {

/**
* Shape of the model static properties. The `$` prefix is to denote
* special properties from the base model with exception to `create`
* `findAll`, `findAll`.
* special properties from the base model
*/
export interface ModelConstructorContract {
/**
Expand Down Expand Up @@ -285,7 +294,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
this: T,
result?: ModelObject,
sideloadAttributes?: ModelObject,
options?: any,
options?: ModelOptions,
): null | InstanceType<T>

/**
Expand All @@ -296,7 +305,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
this: T,
results: ModelObject[],
sideloadAttributes?: ModelObject,
options?: any,
options?: ModelOptions,
): InstanceType<T>[]

/**
Expand Down Expand Up @@ -326,7 +335,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
create<T extends ModelConstructorContract> (
this: T,
values: ModelObject,
options?: ModelOptions,
options?: ModelAdapterOptions,
): InstanceType<T>

/**
Expand All @@ -335,7 +344,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
find<T extends ModelConstructorContract> (
this: T,
value: any,
options?: ModelOptions,
options?: ModelAdapterOptions,
): Promise<null | InstanceType<T>>

/**
Expand All @@ -344,7 +353,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
findOrFail<T extends ModelConstructorContract> (
this: T,
value: any,
options?: ModelOptions,
options?: ModelAdapterOptions,
): Promise<InstanceType<T>>

/**
Expand All @@ -353,7 +362,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
findMany<T extends ModelConstructorContract> (
this: T,
value: any[],
options?: ModelOptions,
options?: ModelAdapterOptions,
): Promise<InstanceType<T>[]>

/**
Expand All @@ -363,7 +372,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
this: T,
search: any,
savePayload?: any,
options?: ModelOptions,
options?: ModelAdapterOptions,
): Promise<InstanceType<T>>

/**
Expand All @@ -374,13 +383,16 @@ declare module '@ioc:Adonis/Lucid/Model' {
this: T,
search: any,
savePayload?: any,
options?: ModelOptions,
options?: ModelAdapterOptions,
): Promise<InstanceType<T>>

/**
* Fetch all rows
*/
all<T extends ModelConstructorContract> (this: T, options?: ModelOptions): Promise<InstanceType<T>[]>
all<T extends ModelConstructorContract> (
this: T,
options?: ModelAdapterOptions,
): Promise<InstanceType<T>[]>

/**
* Returns the query for fetching a model instance
Expand All @@ -389,7 +401,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
Model extends ModelConstructorContract,
> (
this: Model,
options?: ModelOptions,
options?: ModelAdapterOptions,
): ModelQueryBuilderContract<Model> & ExcutableQueryBuilderContract<InstanceType<Model>[]>

new (): ModelContract
Expand Down Expand Up @@ -419,7 +431,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
*/
query (
modelConstructor: ModelConstructorContract,
options?: ModelOptions,
options?: ModelAdapterOptions,
): ModelQueryBuilderContract<ModelConstructorContract> & ExcutableQueryBuilderContract<ModelContract[]>
}
}
6 changes: 6 additions & 0 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
import * as knex from 'knex'
import { Dictionary } from 'ts-essentials'
import { ProfilerRowContract, ProfilerContract } from '@ioc:Adonis/Core/Profiler'
import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'

/**
* Get one or many of a generic
Expand Down Expand Up @@ -817,6 +818,7 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
* other queries
*/
interface RawContract {
client: QueryClientContract,
wrap (before: string, after: string): this
}

Expand All @@ -830,6 +832,8 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
> extends ChainableContract<Record> {
del (): this

client: QueryClientContract,

/**
* Clone current query
*/
Expand Down Expand Up @@ -870,6 +874,8 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
Record extends Dictionary<any, string> = Dictionary<StrictValues, string>,
ReturnColumns extends any = any[]
> {
client: QueryClientContract,

/**
* Table for the insert query
*/
Expand Down
10 changes: 7 additions & 3 deletions src/Orm/Adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

import { DatabaseContract } from '@ioc:Adonis/Lucid/Database'
import {
ModelOptions,
ModelContract,
AdapterContract,
ModelAdapterOptions,
ModelConstructorContract,
} from '@ioc:Adonis/Lucid/Model'

Expand All @@ -28,7 +28,11 @@ export class Adapter implements AdapterContract {
/**
* Returns the query client based upon the model instance
*/
private _getModelClient (modelConstructor: ModelConstructorContract, options?: ModelOptions) {
private _getModelClient (modelConstructor: ModelConstructorContract, options?: ModelAdapterOptions) {
if (options && options.client) {
return options.client
}

const connection = options && options.connection || modelConstructor.$connection
const profiler = options && options.profiler
return this._db.connection(connection, { profiler })
Expand All @@ -37,7 +41,7 @@ export class Adapter implements AdapterContract {
/**
* Returns the model query builder instance for a given model
*/
public query (modelConstructor: ModelConstructorContract, options?: ModelOptions): any {
public query (modelConstructor: ModelConstructorContract, options?: ModelAdapterOptions): any {
const client = this._getModelClient(modelConstructor, options)
return client.modelQuery(modelConstructor)
}
Expand Down
65 changes: 50 additions & 15 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
BaseRelationNode,
RelationContract,
AvailableRelations,
ModelAdapterOptions,
ThroughRelationNode,
ModelConstructorContract,
} from '@ioc:Adonis/Lucid/Model'
Expand Down Expand Up @@ -131,13 +132,10 @@ export class BaseModel implements ModelContract {
const instance = new this()
instance.$consumeAdapterResult(adapterResult, sideloadAttributes)
instance.$hydrateOriginals()
instance.$setOptions(options)
instance.$persisted = true
instance.$isLocal = false

if (options) {
instance.$options = options
}

return instance
}

Expand All @@ -152,7 +150,7 @@ export class BaseModel implements ModelContract {
this: T,
adapterResults: ModelObject[],
sideloadAttributes?: ModelObject,
options?: any,
options?: ModelOptions,
): InstanceType<T>[] {
if (!Array.isArray(adapterResults)) {
return []
Expand Down Expand Up @@ -302,7 +300,7 @@ export class BaseModel implements ModelContract {
public static async find<T extends ModelConstructorContract> (
this: T,
value: any,
options?: any,
options?: ModelAdapterOptions,
) {
return this.query(options).where(this.$primaryKey, value).first()
}
Expand All @@ -313,7 +311,7 @@ export class BaseModel implements ModelContract {
public static async findOrFail<T extends ModelConstructorContract> (
this: T,
value: any,
options?: any,
options?: ModelAdapterOptions,
) {
return this.query(options).where(this.$primaryKey, value).firstOrFail()
}
Expand All @@ -324,9 +322,13 @@ export class BaseModel implements ModelContract {
public static async findMany<T extends ModelConstructorContract> (
this: T,
value: any[],
options?: any,
options?: ModelAdapterOptions,
) {
return this.query(options).whereIn(this.$primaryKey, value).exec()
return this
.query(options)
.whereIn(this.$primaryKey, value)
.orderBy(this.$primaryKey, 'desc')
.exec()
}

/**
Expand All @@ -336,7 +338,7 @@ export class BaseModel implements ModelContract {
this: T,
search: any,
savePayload?: any,
options?: ModelOptions,
options?: ModelAdapterOptions,
) {
const row = await this.firstOrNew(search, savePayload, options)
if (!row.$persisted) {
Expand All @@ -353,13 +355,14 @@ export class BaseModel implements ModelContract {
this: T,
search: any,
savePayload?: any,
options?: ModelOptions,
options?: ModelAdapterOptions,
) {
let row = await this.query(options).where(search).first()
const query = this.query(options)
let row = await query.where(search).first()

if (!row) {
row = new this() as InstanceType<T>
row.$options = options
row.$setOptions({ client: query.client })
row.fill(Object.assign({}, search, savePayload))
return row
}
Expand All @@ -371,9 +374,9 @@ export class BaseModel implements ModelContract {
*/
public static async all <T extends ModelConstructorContract> (
this: T,
options?: any,
options?: ModelAdapterOptions,
) {
return this.query(options).exec()
return this.query(options).orderBy(this.$primaryKey, 'desc').exec()
}

constructor () {
Expand Down Expand Up @@ -552,6 +555,38 @@ export class BaseModel implements ModelContract {
return Object.keys(this.$dirty).length > 0
}

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

this.$options = this.$options || {}

if (options.client) {
this.$options.connection = options.client.connectionName
this.$options.profiler = options.client.profiler
return
}

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

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

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

/**
* Set attribute
*/
Expand Down
Loading

0 comments on commit c7a62f8

Please sign in to comment.