Skip to content

Commit

Permalink
feat: add defineProperty method on the model itself
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 30, 2021
1 parent 2923085 commit 78e7a53
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
14 changes: 14 additions & 0 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,20 @@ declare module '@ioc:Adonis/Lucid/Orm' {
: RelationshipsContract
$getRelation<Model extends LucidModel>(this: Model, name: string): RelationshipsContract

/**
* Define a static property on the model using the inherit or
* define strategy.
*
* Inherit strategy will clone the property from the parent model
* and will set it on the current model
*/
$defineProperty<Model extends LucidModel, Prop extends keyof Model>(
this: Model,
propertyName: Prop,
defaultValue: Model[Prop],
strategy: 'inherit' | 'define' | ((value: Model[Prop]) => Model[Prop])
): void

/**
* Boot model
*/
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"homepage": "https://github.com/adonisjs/lucid#readme",
"dependencies": {
"@poppinss/hooks": "^3.0.5",
"@poppinss/utils": "^3.1.5",
"@poppinss/utils": "^3.2.1",
"@types/faker": "^5.5.7",
"faker": "^5.5.3",
"fast-deep-equal": "^3.1.3",
Expand All @@ -70,7 +70,7 @@
"@adonisjs/sink": "^5.1.6",
"@poppinss/dev-utils": "^1.1.5",
"@types/luxon": "^2.0.4",
"@types/node": "^16.10.1",
"@types/node": "^16.10.2",
"@types/pluralize": "0.0.29",
"@types/qs": "^6.9.7",
"chance": "^1.1.8",
Expand All @@ -92,7 +92,7 @@
"prettier": "^2.4.1",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2",
"tedious": "^12.2.0",
"tedious": "^13.0.4",
"typescript": "^4.4.3"
},
"publishConfig": {
Expand Down
90 changes: 42 additions & 48 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class BaseModel implements LucidRow {

return adapterResults.reduce((models, row) => {
if (isObject(row)) {
models.push(this['$createFromAdapterResult'](row, sideloadAttributes, options))
models.push(this.$createFromAdapterResult(row, sideloadAttributes, options))
}
return models
}, []) as InstanceType<T>[]
Expand Down Expand Up @@ -469,6 +469,26 @@ export class BaseModel implements LucidRow {
return this.$relationsDefinitions.get(name)!
}

/**
* Define a static property on the model using the inherit or
* define strategy.
*
* Inherit strategy will clone the property from the parent model
* and will set it on the current model
*/
public static $defineProperty<Model extends LucidModel, Prop extends keyof Model>(
this: Model,
propertyName: Prop,
defaultValue: Model[Prop],
strategy: 'inherit' | 'define' | ((value: Model[Prop]) => Model[Prop])
) {
defineStaticProperty(this, BaseModel, {
propertyName: propertyName,
defaultValue: defaultValue,
strategy: strategy,
})
}

/**
* Boot the model
*/
Expand All @@ -492,45 +512,33 @@ export class BaseModel implements LucidRow {
/**
* Table name is never inherited from the base model
*/
defineStaticProperty(this, BaseModel, {
propertyName: 'table',
defaultValue: this.namingStrategy.tableName(this),
strategy: 'define',
})
this.$defineProperty('table', this.namingStrategy.tableName(this), 'define')

/**
* Inherit primary key or default to "id"
*/
defineStaticProperty(this, BaseModel, {
propertyName: 'primaryKey',
defaultValue: 'id',
strategy: 'inherit',
})
this.$defineProperty('primaryKey', 'id', 'inherit')

/**
* Inherit selfAssignPrimaryKey or default to "false"
*/
defineStaticProperty(this, BaseModel, {
propertyName: 'selfAssignPrimaryKey',
defaultValue: false,
strategy: 'inherit',
})
this.$defineProperty('selfAssignPrimaryKey', false, 'inherit')

/**
* Define the keys property. This allows looking up variations
* for model keys
*/
defineStaticProperty(this, BaseModel, {
propertyName: '$keys',
defaultValue: {
this.$defineProperty(
'$keys',
{
attributesToColumns: new ModelKeys(),
attributesToSerialized: new ModelKeys(),
columnsToAttributes: new ModelKeys(),
columnsToSerialized: new ModelKeys(),
serializedToColumns: new ModelKeys(),
serializedToAttributes: new ModelKeys(),
},
strategy: (value) => {
(value) => {
return {
attributesToColumns: new ModelKeys(Object.assign({}, value.attributesToColumns.all())),
attributesToSerialized: new ModelKeys(
Expand All @@ -543,54 +551,40 @@ export class BaseModel implements LucidRow {
Object.assign({}, value.serializedToAttributes.all())
),
}
},
})
}
)

/**
* Define columns
*/
defineStaticProperty(this, BaseModel, {
propertyName: '$columnsDefinitions',
defaultValue: new Map(),
strategy: 'inherit',
})
this.$defineProperty('$columnsDefinitions', new Map(), 'inherit')

/**
* Define computed properties
*/
defineStaticProperty(this, BaseModel, {
propertyName: '$computedDefinitions',
defaultValue: new Map(),
strategy: 'inherit',
})
this.$defineProperty('$computedDefinitions', new Map(), 'inherit')

/**
* Define relationships
*/
defineStaticProperty(this, BaseModel, {
propertyName: '$relationsDefinitions',
defaultValue: new Map(),
strategy: (value) => {
const relations = new Map()
value.forEach((relation, key) => relations.set(key, relation))
return relations
},
this.$defineProperty('$relationsDefinitions', new Map(), (value) => {
const relations = new Map<string, RelationshipsContract>()
value.forEach((relation, key) => relations.set(key, relation))
return relations
})

/**
* Define hooks.
*/
defineStaticProperty(this, BaseModel, {
propertyName: '$hooks',
defaultValue: new Hooks(
this.$container.getResolver(undefined, 'modelHooks', 'App/Models/Hooks')
),
strategy: (value: Hooks) => {
this.$defineProperty(
'$hooks',
new Hooks(this.$container.getResolver(undefined, 'modelHooks', 'App/Models/Hooks')),
(value: Hooks) => {
const hooks = new Hooks()
hooks.merge(value)
return hooks
},
})
}
)
}

/**
Expand Down

0 comments on commit 78e7a53

Please sign in to comment.