Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BaseModel): ensure we are saving when creating #504

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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