Skip to content

Commit

Permalink
refactor: remove primaryAdapterKey in favor of using $refs for lookin…
Browse files Browse the repository at this point in the history
…g up cast keys
  • Loading branch information
thetutlage committed Nov 9, 2019
1 parent aaf7d38 commit 3334888
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 49 deletions.
5 changes: 0 additions & 5 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,6 @@ declare module '@ioc:Adonis/Lucid/Model' {
*/
$primaryKey: string

/**
* The primary key for executing queries
*/
$primaryAdapterKey: string

/**
* Custom database connection to use
*/
Expand Down
29 changes: 11 additions & 18 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ export class BaseModel implements ModelContract {
*/
public static $primaryKey: string

/**
* Primary key for executing database queries. The adapter keys are
* the column names
*/
public static $primaryAdapterKey: string

/**
* Whether or not the model has been booted. Booting the model initializes it's
* static properties. Base models must not be initialized.
Expand Down Expand Up @@ -230,7 +224,6 @@ export class BaseModel implements ModelContract {

this.$booted = true
this.$primaryKey = this.$primaryKey || 'id'
this.$primaryAdapterKey = snakeCase(this.$primaryKey)

Object.defineProperty(this, '$refs', { value: {} })
Object.defineProperty(this, '$columns', { value: new Map() })
Expand Down Expand Up @@ -265,7 +258,6 @@ export class BaseModel implements ModelContract {
*/
if (column.primary) {
this.$primaryKey = name
this.$primaryAdapterKey = column.castAs
}

this.$columns.set(name, column)
Expand Down Expand Up @@ -375,7 +367,7 @@ export class BaseModel implements ModelContract {
value: any,
options?: ModelAdapterOptions,
) {
return this.query(options).where(this.$primaryAdapterKey, value).first()
return this.query(options).where(this.$refs[this.$primaryKey], value).first()
}

/**
Expand All @@ -386,7 +378,7 @@ export class BaseModel implements ModelContract {
value: any,
options?: ModelAdapterOptions,
) {
return this.query(options).where(this.$primaryAdapterKey, value).firstOrFail()
return this.query(options).where(this.$refs[this.$primaryKey], value).firstOrFail()
}

/**
Expand All @@ -399,8 +391,8 @@ export class BaseModel implements ModelContract {
) {
return this
.query(options)
.whereIn(this.$primaryAdapterKey, value)
.orderBy(this.$primaryAdapterKey, 'desc')
.whereIn(this.$refs[this.$primaryKey], value)
.orderBy(this.$refs[this.$primaryKey], 'desc')
.exec()
}

Expand Down Expand Up @@ -480,7 +472,7 @@ export class BaseModel implements ModelContract {
this: T,
options?: ModelAdapterOptions,
) {
return this.query(options).orderBy(this.$primaryAdapterKey, 'desc').exec()
return this.query(options).orderBy(this.$refs[this.$primaryKey], 'desc').exec()
}

constructor () {
Expand Down Expand Up @@ -1054,7 +1046,7 @@ export class BaseModel implements ModelContract {
const insertQuery = client.insertQuery().table(modelConstructor.$table)

if (modelConstructor.$increments) {
insertQuery.returning(modelConstructor.$primaryAdapterKey)
insertQuery.returning(modelConstructor.$refs[modelConstructor.$primaryKey])
}
return insertQuery
}
Expand All @@ -1065,7 +1057,7 @@ export class BaseModel implements ModelContract {
return client
.query()
.from(modelConstructor.$table)
.where(modelConstructor.$primaryAdapterKey, this.$primaryKeyValue)
.where(modelConstructor.$refs[modelConstructor.$primaryKey], this.$primaryKeyValue)
}

/**
Expand All @@ -1086,7 +1078,8 @@ export class BaseModel implements ModelContract {
public async refresh () {
this._ensureIsntDeleted()
const modelConstructor = this.constructor as typeof BaseModel
const { $table, $primaryAdapterKey } = modelConstructor
const { $table } = modelConstructor
const primaryAdapterKey = modelConstructor.$refs[modelConstructor.$primaryKey]

/**
* Noop when model instance is not persisted
Expand All @@ -1104,8 +1097,8 @@ export class BaseModel implements ModelContract {
throw new Exception(
[
'Model.reload failed. ',
`Unable to lookup ${$table} table where ${$primaryAdapterKey} = ${this.$primaryKeyValue}`,
].join('')
`Unable to lookup ${$table} table where ${primaryAdapterKey} = ${this.$primaryKeyValue}`,
].join(''),
)
}

Expand Down
26 changes: 0 additions & 26 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,32 +116,6 @@ test.group('Base model | boot', (group) => {
User.$boot()
assert.deepEqual(User.$refs, { id: 'id', userName: 'user_name' })
})

test('compute primary adapter key from primary key column', async (assert) => {
class User extends BaseModel {
@column({ primary: true })
public id: number

@column()
public username: string
}

User.$boot()
assert.equal(User.$primaryAdapterKey, 'id')
})

test('use cast as key when defined on primary column', async (assert) => {
class User extends BaseModel {
@column({ primary: true, castAs: 'user_id' })
public id: number

@column()
public username: string
}

User.$boot()
assert.equal(User.$primaryAdapterKey, 'user_id')
})
})

test.group('Base Model | getter-setters', (group) => {
Expand Down

0 comments on commit 3334888

Please sign in to comment.