-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement insert query builder
- Loading branch information
1 parent
e99b202
commit 447fda7
Showing
8 changed files
with
246 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: '3.4' | ||
services: | ||
test: | ||
build: | ||
context: . | ||
target: build-deps | ||
links: | ||
- mysql | ||
- pg | ||
command: ["npm", "run", "test:docker"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../../adonis-typings/database.ts" /> | ||
|
||
import * as knex from 'knex' | ||
import { InsertQueryBuilderContract } from '@ioc:Adonis/Addons/DatabaseQueryBuilder' | ||
|
||
/** | ||
* Exposes the API for performing SQL inserts | ||
*/ | ||
export class InsertQueryBuilder implements InsertQueryBuilderContract { | ||
constructor (protected $knexBuilder: knex.QueryBuilder) { | ||
} | ||
|
||
/** | ||
* Define table for performing the insert query | ||
*/ | ||
public table (table: any): this { | ||
this.$knexBuilder.table(table) | ||
return this | ||
} | ||
|
||
/** | ||
* Define returning columns for the insert query | ||
*/ | ||
public returning (column: any): any { | ||
this.$knexBuilder.returning(column) | ||
return this | ||
} | ||
|
||
/** | ||
* Perform insert query | ||
*/ | ||
public insert (columns: any, defer = false): Promise<any> | any { | ||
this.$knexBuilder.insert(columns) | ||
|
||
if (!defer) { | ||
return this.exec() | ||
} | ||
|
||
return this | ||
} | ||
|
||
/** | ||
* Insert multiple rows in a single query | ||
*/ | ||
public multiInsert (columns: any, defer = false): Promise<any> | any { | ||
return this.insert(columns, defer) | ||
} | ||
|
||
/** | ||
* Executes the insert query | ||
*/ | ||
public async exec (): Promise<any> { | ||
const returnValue = await this.$knexBuilder | ||
return returnValue | ||
} | ||
|
||
/** | ||
* Get SQL representation of the constructed query | ||
*/ | ||
public toSQL () { | ||
return this.$knexBuilder.toSQL() | ||
} | ||
|
||
/** | ||
* Returns string representation of the query | ||
*/ | ||
public toString () { | ||
return this.$knexBuilder.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../adonis-typings/database.ts" /> | ||
|
||
import * as test from 'japa' | ||
|
||
import { Connection } from '../src/Connection' | ||
import { getConfig, setup, cleanup, getInsertBuilder } from '../test-helpers' | ||
|
||
test.group('Query Builder | from', (group) => { | ||
group.before(async () => { | ||
await setup() | ||
}) | ||
|
||
group.after(async () => { | ||
await cleanup() | ||
}) | ||
|
||
test('perform insert', (assert) => { | ||
const connection = new Connection('primary', getConfig()) | ||
connection.connect() | ||
|
||
const db = getInsertBuilder(connection) | ||
const { sql, bindings } = db.table('users').insert({ username: 'virk' }, true).toSQL() | ||
|
||
const { sql: knexSql, bindings: knexBindings } = connection.client! | ||
.from('users') | ||
.insert({ username: 'virk' }) | ||
.toSQL() | ||
|
||
assert.equal(sql, knexSql) | ||
assert.deepEqual(bindings, knexBindings) | ||
}) | ||
|
||
test('perform multi insert', (assert) => { | ||
const connection = new Connection('primary', getConfig()) | ||
connection.connect() | ||
|
||
const db = getInsertBuilder(connection) | ||
const { sql, bindings } = db | ||
.table('users') | ||
.multiInsert([{ username: 'virk' }, { username: 'nikk' }], true) | ||
.toSQL() | ||
|
||
const { sql: knexSql, bindings: knexBindings } = connection.client! | ||
.from('users') | ||
.insert([{ username: 'virk' }, { username: 'nikk' }]) | ||
.toSQL() | ||
|
||
assert.equal(sql, knexSql) | ||
assert.deepEqual(bindings, knexBindings) | ||
}) | ||
|
||
test('define returning columns', (assert) => { | ||
const connection = new Connection('primary', getConfig()) | ||
connection.connect() | ||
|
||
const db = getInsertBuilder(connection) | ||
const { sql, bindings } = db | ||
.table('users') | ||
.returning(['id', 'username']) | ||
.multiInsert([{ username: 'virk' }, { username: 'nikk' }], true) | ||
.toSQL() | ||
|
||
const { sql: knexSql, bindings: knexBindings } = connection.client! | ||
.from('users') | ||
.returning(['id', 'username']) | ||
.insert([{ username: 'virk' }, { username: 'nikk' }]) | ||
.toSQL() | ||
|
||
assert.equal(sql, knexSql) | ||
assert.deepEqual(bindings, knexBindings) | ||
}) | ||
}) |