Skip to content

Commit 8bdb7d4

Browse files
committed
feat: add truncate method to base model
1 parent 5c678af commit 8bdb7d4

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

adonis-typings/model.ts

+5
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ declare module '@ioc:Adonis/Lucid/Model' {
680680
Result extends any = InstanceType<Model>,
681681
> (this: Model, options?: ModelAdapterOptions): ModelQueryBuilderContract<Model, Result>
682682

683+
/**
684+
* Truncate model table
685+
*/
686+
truncate (cascade?: boolean): Promise<void>
687+
683688
new (): Model
684689
}
685690

src/Orm/BaseModel/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,13 @@ export class BaseModel implements ModelContract {
648648
return this.query(options).orderBy(this.primaryKey, 'desc')
649649
}
650650

651+
/**
652+
* Truncate model table
653+
*/
654+
public static truncate (cascade: boolean = false) {
655+
return (this as ModelConstructorContract).query().client.truncate(this.table, cascade)
656+
}
657+
651658
constructor () {
652659
return new Proxy(this, proxyHandler)
653660
}

test/database/query-client.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ test.group('Query client', (group) => {
8080

8181
const client = new QueryClient('write', connection)
8282
const column = await client.columnsInfo('users', 'id')
83-
assert.equal(column.type, 'integer')
83+
assert.oneOf(column.type, ['integer', 'int'])
8484
})
8585

8686
test('truncate table with cascade', async (_assert) => {
@@ -96,7 +96,7 @@ test.group('Query client', (group) => {
9696
})
9797
await connection.client?.schema.createTableIfNotExists('test_profiles', (table) => {
9898
table.increments('id').primary()
99-
table.integer('user_id').references('test_users.id').onDelete('CASCADE')
99+
table.integer('user_id').unsigned().references('test_users.id').onDelete('CASCADE')
100100
})
101101

102102
/**

0 commit comments

Comments
 (0)