Skip to content

Commit c710562

Browse files
authored
Merge pull request #10 from SecJS/refactor/len-primary-key-insert-update
refactor: add the returnKey for insert and update operations
2 parents 441f8a4 + 6324056 commit c710562

10 files changed

Lines changed: 186 additions & 76 deletions

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,26 @@ const productIds = await database
297297
.buildTable('products')
298298
.insert([{ name: 'iPhone 10' }, { name: 'iPhone 11' }, { name: 'iPhone 12' }])
299299

300+
// WARN - ONLY FOR SQL
301+
{
302+
// When Database insert the data he will always return an array of ID.
303+
// But you can change this behavior setting the returnKey value
304+
const returnKey = 'name'
305+
const productNames = await database
306+
.buildTable('products')
307+
.insert([{ name: 'iPhone 13' }], returnKey)
308+
309+
// The returnKey can be defined only in SQL Drivers because Mongo always returns the _id by default
310+
}
311+
312+
const returnKeyId = 'id'
300313
// Insert and return an array of products objects
301314
const products = await database
302315
.buildTable('products')
303-
.insertAndGet({ name: 'iPhone 13' })
316+
// You can set returnKey here too, this way it will insert the product
317+
// and then find the products by the returnKey value, in this case, the id
318+
// WARN - Remember, this is a SQL feature only and will not take effect in MongoDriver
319+
.insertAndGet({ name: 'iPhone 13' }, returnKeyId)
304320

305321
// WARN - buildTable method needs to be called only one time before the connection is stabilished.
306322
// when you call buildTable it saves in the Driver instance the table that you are working on.
@@ -336,10 +352,26 @@ const productsPaginated = await database.forPage(page, limit)
336352
```ts
337353
const productIds = await database.insert([{ name: 'iPhone 10' }, { name: 'iPhone 11' }])
338354

339-
// Be carefull with update, remember to always use where
355+
// WARN - ONLY FOR SQL
356+
{
357+
// When Database update the data he will always return an array of ID.
358+
// But you can change this behavior setting the returnKey value
359+
const returnKey = 'name'
360+
const productNames = await database
361+
.buildTable('products')
362+
.update([{ name: 'iPhone 13' }], returnKey)
363+
364+
// The returnKey can be defined only in SQL Drivers because Mongo always returns the _id by default
365+
}
366+
367+
const returnKeyId = 'id'
368+
// WARN - Be carefull with update, remember to always use where
340369
const productsUpdated = await database
341370
.buildWhereIn('id', productIds)
342-
.updateAndGet({ name: 'iPhone X' }) // or updateAngGet('name', 'iPhone X')
371+
// You can set returnKey here too, this way it will update the product
372+
// and then find the products by the returnKey value, in this case, the id
373+
// WARN - Remember, this is a SQL feature only and will not take effect in MongoDriver
374+
.updateAndGet({ name: 'iPhone X' }, returnKeyId) // or updateAngGet('name', 'iPhone X', returnKeyId)
343375

344376
console.log(productsUpdated) // [{ id: 1, name: 'iPhone X', quantity: 0 }, { id: 2, name: 'iPhone X', quantity: 0 }]
345377
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/database",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Handle your application database with factories, seeders and query builder in Node.js",
55
"license": "MIT",
66
"author": "João Lenon <lenon@secjs.com.br>",

src/Contracts/DatabaseContract.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,23 @@ export interface DatabaseContract {
150150
* In case of bulk inserts only the last inserted id will be returned.
151151
*
152152
* @param values The values that are going to be inserted.
153+
* @param returnKey The returned value from query default is id
153154
* @return The newly created id or ids inside array.
154155
*
155156
*/
156-
insert(values: any | any[]): Promise<string[]>
157+
insert(values: any | any[], returnKey?: string): Promise<string[]>
157158

158159
/**
159160
* InsertAndGet method
160161
*
161162
* Same as insert but return an array with the values inserted
162163
*
163164
* @param values The values that are going to be inserted.
165+
* @param returnKey The returned value from query default is id
164166
* @return The array with the values inserted.
165167
*
166168
*/
167-
insertAndGet(values: any | any[]): Promise<any[]>
169+
insertAndGet(values: any | any[], returnKey?: string): Promise<any[]>
168170

169171
/**
170172
* Update method
@@ -173,10 +175,11 @@ export interface DatabaseContract {
173175
*
174176
* @param key The key to be updated.
175177
* @param value The value of the key.
178+
* @param returnKey The returned value from query default is id
176179
* @return The number of affected rows.
177180
*
178181
*/
179-
update(key: any | string, value?: any): Promise<string[]>
182+
update(key: any | string, value?: any, returnKey?: string): Promise<string[]>
180183

181184
/**
182185
* UpdateAndGet method
@@ -185,10 +188,15 @@ export interface DatabaseContract {
185188
*
186189
* @param key The key to be updated.
187190
* @param value The value of the key.
191+
* @param returnKey The returned value from query default is id
188192
* @return The payload updated.
189193
*
190194
*/
191-
updateAndGet(key: any | string, value?: any): Promise<any[]>
195+
updateAndGet(
196+
key: any | string,
197+
value?: any,
198+
returnKey?: string,
199+
): Promise<any[]>
192200

193201
/**
194202
* delete method

src/Contracts/DriverContract.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,23 @@ export interface DriverContract {
409409
* In case of bulk inserts only the last inserted id will be returned.
410410
*
411411
* @param values The values that are going to be inserted.
412+
* @param returnKey The returned value from query default is id
412413
* @return The newly created id or ids inside array.
413414
*
414415
*/
415-
insert(values: any | any[]): Promise<string[]>
416+
insert(values: any | any[], returnKey?: string): Promise<string[]>
416417

417418
/**
418419
* InsertAndGet method
419420
*
420421
* Same as insert but return an array with the values inserted
421422
*
422423
* @param values The values that are going to be inserted.
424+
* @param returnKey The returned value from query default is id
423425
* @return The array with the values inserted.
424426
*
425427
*/
426-
insertAndGet(values: any | any[]): Promise<any[]>
428+
insertAndGet(values: any | any[], returnKey?: string): Promise<any[]>
427429

428430
/**
429431
* Update method
@@ -432,10 +434,11 @@ export interface DriverContract {
432434
*
433435
* @param key The key to be updated.
434436
* @param value The value of the key.
437+
* @param returnKey The returned value from query default is id
435438
* @return The number of affected rows.
436439
*
437440
*/
438-
update(key: any | string, value?: any): Promise<string[]>
441+
update(key: any | string, value?: any, returnKey?: string): Promise<string[]>
439442

440443
/**
441444
* UpdateAndGet method
@@ -444,10 +447,15 @@ export interface DriverContract {
444447
*
445448
* @param key The key to be updated.
446449
* @param value The value of the key.
450+
* @param returnKey The returned value from query default is id
447451
* @return The payload updated.
448452
*
449453
*/
450-
updateAndGet(key: any | string, value?: any): Promise<any[]>
454+
updateAndGet(
455+
key: any | string,
456+
value?: any,
457+
returnKey?: string,
458+
): Promise<any[]>
451459

452460
/**
453461
* delete method

src/Database.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,28 @@ export class Database implements DatabaseContract {
143143
return this.driver.findMany()
144144
}
145145

146-
async insert(values: any | any[]): Promise<string[]> {
147-
return this.driver.insert(values)
146+
async insert(values: any | any[], returnKey = 'id'): Promise<string[]> {
147+
return this.driver.insert(values, returnKey)
148148
}
149149

150-
async insertAndGet(values: any | any[]): Promise<any[]> {
151-
return this.driver.insertAndGet(values)
150+
async insertAndGet(values: any | any[], returnKey = 'id'): Promise<any[]> {
151+
return this.driver.insertAndGet(values, returnKey)
152152
}
153153

154-
async update(key: any | string, value?: any): Promise<string[]> {
155-
return this.driver.update(key, value)
154+
async update(
155+
key: any | string,
156+
value?: any,
157+
returnKey = 'id',
158+
): Promise<string[]> {
159+
return this.driver.update(key, value, returnKey)
156160
}
157161

158-
async updateAndGet(key: any | string, value?: any): Promise<any[]> {
159-
return this.driver.updateAndGet(key, value)
162+
async updateAndGet(
163+
key: any | string,
164+
value?: any,
165+
returnKey = 'id',
166+
): Promise<any[]> {
167+
return this.driver.updateAndGet(key, value, returnKey)
160168
}
161169

162170
async delete(): Promise<number> {

src/Drivers/MySqlDriver.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,16 @@ export class MySqlDriver implements DriverContract {
239239
return this.buildSkip(page).buildLimit(limit).findMany()
240240
}
241241

242-
async insert(values: any | any[]): Promise<string[]> {
243-
const insert: any[] = await this.queryBuilder.insert(values, 'id')
242+
async insert(values: any | any[], returnKey = 'id'): Promise<string[]> {
243+
const insert: any[] = await this.queryBuilder.insert(values, returnKey)
244244

245-
return insert.map(i => `${i.id}`)
245+
return insert.map(i => `${i[returnKey]}`)
246246
}
247247

248-
async insertAndGet(values: any | any[]): Promise<any[]> {
249-
const arrayOfId = await this.insert(values)
248+
async insertAndGet(values: any | any[], returnKey = 'id'): Promise<any[]> {
249+
const arrayOfId = await this.insert(values, returnKey)
250250

251-
return this.query().whereIn('id', arrayOfId)
251+
return this.query().whereIn(returnKey, arrayOfId)
252252
}
253253

254254
async max(column: string): Promise<number> {
@@ -299,22 +299,28 @@ export class MySqlDriver implements DriverContract {
299299
await this.queryBuilder.table(tableName).truncate()
300300
}
301301

302-
async update(key: any, value?: any): Promise<string[]> {
302+
async update(key: any, value?: any, returnKey = 'id'): Promise<string[]> {
303303
if (typeof key === 'object') {
304-
const data: any[] = await this.queryBuilder.update(key)
304+
const _returnKey = value
305305

306-
return data.map(i => `${i.id}`)
306+
const data: any[] = await this.queryBuilder.update(key, _returnKey)
307+
308+
return data.map(i => `${i[_returnKey]}`)
307309
}
308310

309-
const data: any[] = await this.queryBuilder.update(key, value, 'id')
311+
const data: any[] = await this.queryBuilder.update(key, value, returnKey)
310312

311-
return data.map(i => `${i.id}`)
313+
return data.map(i => `${i[returnKey]}`)
312314
}
313315

314-
async updateAndGet(key: any, value?: any): Promise<any[]> {
315-
const arrayOfId = await this.update(key, value)
316+
async updateAndGet(key: any, value?: any, returnKey = 'id'): Promise<any[]> {
317+
let _returnKey = returnKey
318+
319+
if (!returnKey) _returnKey = value
320+
321+
const arrayOfId = await this.update(key, value, returnKey)
316322

317-
return this.query().whereIn('id', arrayOfId)
323+
return this.query().whereIn(_returnKey, arrayOfId)
318324
}
319325

320326
async increment(column: string, value: number): Promise<void> {

src/Drivers/PostgresDriver.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,16 @@ export class PostgresDriver implements DriverContract {
237237
return this.buildSkip(page).buildLimit(limit).findMany()
238238
}
239239

240-
async insert(values: any | any[]): Promise<string[]> {
241-
const insert: any[] = await this.queryBuilder.insert(values, 'id')
240+
async insert(values: any | any[], returnKey = 'id'): Promise<string[]> {
241+
const insert: any[] = await this.queryBuilder.insert(values, returnKey)
242242

243-
return insert.map(i => `${i.id}`)
243+
return insert.map(i => `${i[returnKey]}`)
244244
}
245245

246-
async insertAndGet(values: any | any[]): Promise<any[]> {
247-
const arrayOfId = await this.insert(values)
246+
async insertAndGet(values: any | any[], returnKey = 'id'): Promise<any[]> {
247+
const arrayOfId = await this.insert(values, returnKey)
248248

249-
return this.query().whereIn('id', arrayOfId)
249+
return this.query().whereIn(returnKey, arrayOfId)
250250
}
251251

252252
async max(column: string): Promise<number> {
@@ -297,22 +297,28 @@ export class PostgresDriver implements DriverContract {
297297
await this.queryBuilder.table(tableName).truncate()
298298
}
299299

300-
async update(key: any, value?: any): Promise<string[]> {
300+
async update(key: any, value?: any, returnKey = 'id'): Promise<string[]> {
301301
if (typeof key === 'object') {
302-
const data: any[] = await this.queryBuilder.update(key)
302+
const _returnKey = value
303303

304-
return data.map(i => `${i.id}`)
304+
const data: any[] = await this.queryBuilder.update(key, _returnKey)
305+
306+
return data.map(i => `${i[_returnKey]}`)
305307
}
306308

307-
const data: any[] = await this.queryBuilder.update(key, value, 'id')
309+
const data: any[] = await this.queryBuilder.update(key, value, returnKey)
308310

309-
return data.map(i => `${i.id}`)
311+
return data.map(i => `${i[returnKey]}`)
310312
}
311313

312-
async updateAndGet(key: any, value?: any): Promise<any[]> {
313-
const arrayOfId = await this.update(key, value)
314+
async updateAndGet(key: any, value?: any, returnKey = 'id'): Promise<any[]> {
315+
let _returnKey = returnKey
316+
317+
if (!returnKey) _returnKey = value
318+
319+
const arrayOfId = await this.update(key, value, returnKey)
314320

315-
return this.query().whereIn('id', arrayOfId)
321+
return this.query().whereIn(_returnKey, arrayOfId)
316322
}
317323

318324
async increment(column: string, value: number): Promise<void> {

src/Drivers/SqlServerDriver.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,16 @@ export class SqlServerDriver implements DriverContract {
240240
return this.buildSkip(page).buildLimit(limit).findMany()
241241
}
242242

243-
async insert(values: any | any[]): Promise<string[]> {
244-
const insert: any[] = await this.queryBuilder.insert(values, 'id')
243+
async insert(values: any | any[], returnKey = 'id'): Promise<string[]> {
244+
const insert: any[] = await this.queryBuilder.insert(values, returnKey)
245245

246-
return insert.map(i => `${i.id}`)
246+
return insert.map(i => `${i[returnKey]}`)
247247
}
248248

249-
async insertAndGet(values: any | any[]): Promise<any[]> {
250-
const arrayOfId = await this.insert(values)
249+
async insertAndGet(values: any | any[], returnKey = 'id'): Promise<any[]> {
250+
const arrayOfId = await this.insert(values, returnKey)
251251

252-
return this.query().whereIn('id', arrayOfId)
252+
return this.query().whereIn(returnKey, arrayOfId)
253253
}
254254

255255
async max(column: string): Promise<number> {
@@ -300,22 +300,28 @@ export class SqlServerDriver implements DriverContract {
300300
await this.queryBuilder.table(tableName).truncate()
301301
}
302302

303-
async update(key: any, value?: any): Promise<string[]> {
303+
async update(key: any, value?: any, returnKey = 'id'): Promise<string[]> {
304304
if (typeof key === 'object') {
305-
const data: any[] = await this.queryBuilder.update(key)
305+
const _returnKey = value
306306

307-
return data.map(i => `${i.id}`)
307+
const data: any[] = await this.queryBuilder.update(key, _returnKey)
308+
309+
return data.map(i => `${i[_returnKey]}`)
308310
}
309311

310-
const data: any[] = await this.queryBuilder.update(key, value, 'id')
312+
const data: any[] = await this.queryBuilder.update(key, value, returnKey)
311313

312-
return data.map(i => `${i.id}`)
314+
return data.map(i => `${i[returnKey]}`)
313315
}
314316

315-
async updateAndGet(key: any, value?: any): Promise<any[]> {
316-
const arrayOfId = await this.update(key, value)
317+
async updateAndGet(key: any, value?: any, returnKey = 'id'): Promise<any[]> {
318+
let _returnKey = returnKey
319+
320+
if (!returnKey) _returnKey = value
321+
322+
const arrayOfId = await this.update(key, value, returnKey)
317323

318-
return this.query().whereIn('id', arrayOfId)
324+
return this.query().whereIn(_returnKey, arrayOfId)
319325
}
320326

321327
async increment(column: string, value: number): Promise<void> {

0 commit comments

Comments
 (0)