Skip to content

Commit

Permalink
refactor: cleanup API and make it consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent cb8067c commit 6a67a23
Show file tree
Hide file tree
Showing 39 changed files with 1,654 additions and 1,736 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ MYSQL_PORT=3306
MYSQL_USER=virk
MYSQL_PASSWORD=password

PG_HOST=0.0.0.0
PG_HOST=pg
PG_PORT=5432
PG_USER=relay
PG_USER=virk
PG_PASSWORD=password
122 changes: 72 additions & 50 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
*/
export type ColumnOptions = {
castAs: string,
serializeAs: string | null,
serializeAs: string | null, // null means do not serialize column
isPrimary: boolean,
hasGetter: boolean,
hasSetter: boolean,
Expand All @@ -101,8 +101,11 @@ declare module '@ioc:Adonis/Lucid/Model' {
*/
export type ColumnDecorator = (
options?: Partial<Omit<ColumnOptions, 'hasGetter' | 'hasSetter'>>,
) => (target, property) => void
export type ComputedDecorator = (options?: Partial<ComputedOptions>) => (target, property) => void
) => (target: any, property: any) => void

export type ComputedDecorator = (
options?: Partial<ComputedOptions>,
) => (target: any, property: any) => void

/**
* ------------------------------------------------------
Expand Down Expand Up @@ -160,7 +163,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
* Reference to query client used for making queries
*/
client: QueryClientContract
knexQuery: knex.QueryBuilder,
knexQuery: knex.QueryBuilder

/**
* A custom set of sideloaded properties defined on the query
Expand All @@ -169,11 +172,6 @@ declare module '@ioc:Adonis/Lucid/Model' {
*/
sideload (value: ModelObject): this

/**
* The connection name used by the model query builder
*/
connection: string

/**
* Execute and get first result
*/
Expand Down Expand Up @@ -214,18 +212,20 @@ declare module '@ioc:Adonis/Lucid/Model' {
$attributes: ModelObject
$extras: ModelObject
$original: ModelObject
$dirty: ModelObject
$isPersisted: boolean
$isNew: boolean
$isLocal: boolean
$isDirty: boolean
$isDeleted: boolean
$preloaded: { [relation: string]: ModelContract | ModelContract[] }
$sideloaded: ModelObject
$primaryKeyValue?: number | string
$options?: ModelOptions
$trx?: TransactionClientContract,

sideloaded: ModelObject

primaryKeyValue?: number | string
isPersisted: boolean
isNew: boolean
isLocal: boolean
dirty: ModelObject
isDirty: boolean
isDeleted: boolean

options?: ModelOptions
trx?: TransactionClientContract,
$setOptionsAndTrx (options?: ModelAdapterOptions): void

/**
Expand Down Expand Up @@ -290,16 +290,15 @@ declare module '@ioc:Adonis/Lucid/Model' {
*/
$consumeAdapterResult (adapterResult: ModelObject, sideloadAttributes?: ModelObject): void

hydrateOriginals(): void
fill (value: ModelObject): void
merge (value: ModelObject): void
save (): Promise<void>
delete (): Promise<void>
serialize (): ModelObject
toJSON (): ModelObject
refresh (): Promise<void>

preload: ModelBuilderPreloadFn<this>

related<
Name extends keyof ExtractRelations<this>,
RelationType extends TypedRelations = this[Name] extends TypedRelations ? this[Name] : never
Expand All @@ -323,7 +322,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
* Whether or not model has been booted. After this model configurations
* are ignored
*/
$booted: boolean
readonly booted: boolean

/**
* A map of defined columns
Expand All @@ -344,12 +343,12 @@ declare module '@ioc:Adonis/Lucid/Model' {
* The primary key for finding unique referencing to a
* model
*/
$primaryKey: string
readonly primaryKey: string

/**
* Custom database connection to use
*/
$connection?: string
readonly connection?: string

/**
* Adapter to work as a bridge between query builder and the model
Expand All @@ -365,38 +364,18 @@ declare module '@ioc:Adonis/Lucid/Model' {
* Whether primary key is auto incrementing or not. If not, then
* end user must provide the value for the primary key
*/
$increments: boolean
readonly increments: boolean

/**
* Database table to use
*/
$table: string
readonly table: string

/**
* Refs are named value pair on model used mainly for autocompleting
* the query constraints
*/
$refs: { [key: string]: string }

$boot (): void

/**
* Register a before hook
*/
$before<T extends ModelConstructorContract> (
this: T,
event: EventsList,
handler: HooksHandler<InstanceType<T>>,
): void

/**
* Register an after hook
*/
$after<T extends ModelConstructorContract> (
this: T,
event: EventsList,
handler: HooksHandler<InstanceType<T>>,
): void
readonly $refs: { [key: string]: string }

/**
* Creating model from adapter results
Expand Down Expand Up @@ -463,6 +442,26 @@ declare module '@ioc:Adonis/Lucid/Model' {
$resolveCastKey (key: string): string
$mapKeysToCastKeys (values: ModelObject): ModelObject

boot (): void

/**
* Register a before hook
*/
before<T extends ModelConstructorContract> (
this: T,
event: EventsList,
handler: HooksHandler<InstanceType<T>>,
): void

/**
* Register an after hook
*/
after<T extends ModelConstructorContract> (
this: T,
event: EventsList,
handler: HooksHandler<InstanceType<T>>,
): void

/**
* Creating model
*/
Expand Down Expand Up @@ -640,32 +639,55 @@ declare module '@ioc:Adonis/Lucid/Model' {
* defaults
*/
export type OrmConfigContract = {
/**
* Return the default table name for a given model
*/
getTableName (model: ModelConstructorContract): string

/**
* Return the `castAs` key (database column name) for a given model
* property.
*/
getCastAsKey (model: ModelConstructorContract, key: string): string

/**
* Return the `serializeAs` key for a given model property
*/
getSerializeAsKey (model: ModelConstructorContract, key: string): string
serialize (model: ModelConstructorContract, key: string): boolean

/**
* Return the local key property name for a given relationship
*/
getLocalKey (
relation: TypedRelations['type'],
model: ModelConstructorContract,
relatedModel: ModelConstructorContract,
): string

/**
* Return the foreign key property name for a given relationship
*/
getForeignKey (
relation: TypedRelations['type'],
model: ModelConstructorContract,
relatedModel: ModelConstructorContract,
): string

/**
* Return the pivot table name for many to many relationship
*/
getPivotTableName (
relation: TypedRelations['type'],
relation: 'manyToMany',
model: ModelConstructorContract,
relatedModel: ModelConstructorContract,
relationName: string,
): string

/**
* Return the pivot foreign key for many to many relationship
*/
getPivotForeignKey (
relation: TypedRelations['type'],
relation: 'manyToMany',
model: ModelConstructorContract,
relatedModel: ModelConstructorContract,
relationName: string,
Expand Down
Loading

0 comments on commit 6a67a23

Please sign in to comment.