Skip to content

Commit

Permalink
fix(BaseModel): ensure we are saving when creating
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Dec 7, 2019
1 parent f8c0feb commit 21f884b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
this: T,
values: ModelObject,
options?: ModelAdapterOptions,
): InstanceType<T>
): Promise<InstanceType<T>>

/**
* Find one using the primary key
Expand Down
5 changes: 3 additions & 2 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,13 @@ export class BaseModel implements ModelContract {
* Returns a fresh instance of model by applying attributes
* to the model instance
*/
public static create<T extends ModelConstructorContract> (
public static async create<T extends ModelConstructorContract> (
this: T,
values: ModelObject,
): InstanceType<T> {
): Promise<InstanceType<T>> {
const instance = new this()
instance.fill(values)
await instance.save()
return instance as InstanceType<T>
}

Expand Down
18 changes: 12 additions & 6 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,8 @@ test.group('Base Model | relations', (group) => {
await db.manager.closeAll()
})

test('set hasOne relation', (assert) => {
test('set hasOne relation', async (assert) => {
const adapter = new FakeAdapter()
class Profile extends BaseModel {
@column()
public username: string
Expand All @@ -1192,8 +1193,9 @@ test.group('Base Model | relations', (group) => {
}

const user = new User()
Profile.$adapter = adapter
user.$consumeAdapterResult({ id: 1 })
user.$setRelated('profile', Profile.create({ username: 'virk' }))
user.$setRelated('profile', await Profile.create({ username: 'virk' }))

assert.deepEqual(user.profile.username, 'virk')
assert.instanceOf(user.$preloaded.profile, Profile)
Expand Down Expand Up @@ -1225,7 +1227,8 @@ test.group('Base Model | relations', (group) => {
assert.deepEqual(user.$preloaded, {})
})

test('serialize relation toJSON', (assert) => {
test('serialize relation toJSON', async (assert) => {
const adapter = new FakeAdapter()
class Profile extends BaseModel {
@column()
public username: string
Expand All @@ -1243,8 +1246,9 @@ test.group('Base Model | relations', (group) => {
}

const user = new User()
Profile.$adapter = adapter
user.$consumeAdapterResult({ id: 1 })
user.$setRelated('profile', Profile.create({ username: 'virk' }))
user.$setRelated('profile', await Profile.create({ username: 'virk' }))

assert.deepEqual(user.toJSON(), {
id: 1,
Expand All @@ -1254,7 +1258,8 @@ test.group('Base Model | relations', (group) => {
})
})

test('serialize relation toJSON with custom serializeAs key', (assert) => {
test('serialize relation toJSON with custom serializeAs key', async (assert) => {
const adapter = new FakeAdapter()
class Profile extends BaseModel {
@column()
public username: string
Expand All @@ -1272,8 +1277,9 @@ test.group('Base Model | relations', (group) => {
}

const user = new User()
Profile.$adapter = adapter
user.$consumeAdapterResult({ id: 1 })
user.$setRelated('profile', Profile.create({ username: 'virk' }))
user.$setRelated('profile', await Profile.create({ username: 'virk' }))

assert.deepEqual(user.toJSON(), {
id: 1,
Expand Down

0 comments on commit 21f884b

Please sign in to comment.