1- import { Delete , Insert , Join , SelectAll , SelectOne , Update } from './interfaces'
1+ import { ConflictUpsert , Delete , Insert , Join , SelectAll , SelectOne , Update } from './interfaces'
22import { ConflictTypes , FetchTypes , OrderTypes } from './enums'
33import { Query , Raw } from './tools'
44
@@ -60,6 +60,19 @@ export class QueryBuilder<GenericResult, GenericResultOne> {
6060 insert ( params : Insert ) : Query {
6161 let args : any [ ] = [ ]
6262
63+ if ( typeof params . onConflict === 'object' ) {
64+ if ( params . onConflict . where ?. params ) {
65+ // 1 - on conflict where parameters
66+ args = args . concat ( params . onConflict . where . params )
67+ }
68+
69+ if ( params . onConflict . data ) {
70+ // 2 - on conflict data parameters
71+ args = args . concat ( this . _parse_arguments ( params . onConflict . data ) )
72+ }
73+ }
74+
75+ // 3 - insert data parameters
6376 if ( Array . isArray ( params . data ) ) {
6477 for ( const row of params . data ) {
6578 args = args . concat ( this . _parse_arguments ( row ) )
@@ -116,8 +129,22 @@ export class QueryBuilder<GenericResult, GenericResultOne> {
116129 } )
117130 }
118131
119- _onConflict ( resolution ?: string | ConflictTypes ) : string {
132+ _onConflict ( resolution ?: string | ConflictTypes | ConflictUpsert ) : string {
120133 if ( resolution ) {
134+ if ( typeof resolution === 'object' ) {
135+ if ( ! Array . isArray ( resolution . column ) ) {
136+ resolution . column = [ resolution . column ]
137+ }
138+
139+ const _update_query = this . update ( {
140+ tableName : '_REPLACE_' ,
141+ data : resolution . data ,
142+ where : resolution . where ,
143+ } ) . query . replace ( ' _REPLACE_' , '' ) // Replace here is to lint the query
144+
145+ return ` ON CONFLICT (${ resolution . column . join ( ', ' ) } ) DO ${ _update_query } `
146+ }
147+
121148 return `OR ${ resolution } `
122149 }
123150 return ''
@@ -131,8 +158,24 @@ export class QueryBuilder<GenericResult, GenericResultOne> {
131158 }
132159
133160 const columns = Object . keys ( params . data [ 0 ] ) . join ( ', ' )
134-
135161 let index = 1
162+
163+ let orConflict = '' ,
164+ onConflict = ''
165+ if ( params . onConflict && typeof params . onConflict === 'object' ) {
166+ onConflict = this . _onConflict ( params . onConflict )
167+
168+ if ( params . onConflict . where ?. params ) {
169+ index += params . onConflict . where ?. params . length
170+ }
171+
172+ if ( params . onConflict . data ) {
173+ index += this . _parse_arguments ( params . onConflict . data ) . length
174+ }
175+ } else {
176+ orConflict = this . _onConflict ( params . onConflict )
177+ }
178+
136179 for ( const row of params . data ) {
137180 const values : Array < string > = [ ]
138181 Object . values ( row ) . forEach ( ( value ) => {
@@ -149,8 +192,9 @@ export class QueryBuilder<GenericResult, GenericResultOne> {
149192 }
150193
151194 return (
152- `INSERT ${ this . _onConflict ( params . onConflict ) } INTO ${ params . tableName } (${ columns } )` +
195+ `INSERT ${ orConflict } INTO ${ params . tableName } (${ columns } )` +
153196 ` VALUES ${ rows . join ( ', ' ) } ` +
197+ onConflict +
154198 this . _returning ( params . returning )
155199 )
156200 }
0 commit comments