Skip to content

Commit

Permalink
refactor: add the returnKey for insert and update operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Mar 17, 2022
1 parent 441f8a4 commit 6324056
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 76 deletions.
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,26 @@ const productIds = await database
.buildTable('products')
.insert([{ name: 'iPhone 10' }, { name: 'iPhone 11' }, { name: 'iPhone 12' }])

// WARN - ONLY FOR SQL
{
// When Database insert the data he will always return an array of ID.
// But you can change this behavior setting the returnKey value
const returnKey = 'name'
const productNames = await database
.buildTable('products')
.insert([{ name: 'iPhone 13' }], returnKey)

// The returnKey can be defined only in SQL Drivers because Mongo always returns the _id by default
}

const returnKeyId = 'id'
// Insert and return an array of products objects
const products = await database
.buildTable('products')
.insertAndGet({ name: 'iPhone 13' })
// You can set returnKey here too, this way it will insert the product
// and then find the products by the returnKey value, in this case, the id
// WARN - Remember, this is a SQL feature only and will not take effect in MongoDriver
.insertAndGet({ name: 'iPhone 13' }, returnKeyId)

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

// Be carefull with update, remember to always use where
// WARN - ONLY FOR SQL
{
// When Database update the data he will always return an array of ID.
// But you can change this behavior setting the returnKey value
const returnKey = 'name'
const productNames = await database
.buildTable('products')
.update([{ name: 'iPhone 13' }], returnKey)

// The returnKey can be defined only in SQL Drivers because Mongo always returns the _id by default
}

const returnKeyId = 'id'
// WARN - Be carefull with update, remember to always use where
const productsUpdated = await database
.buildWhereIn('id', productIds)
.updateAndGet({ name: 'iPhone X' }) // or updateAngGet('name', 'iPhone X')
// You can set returnKey here too, this way it will update the product
// and then find the products by the returnKey value, in this case, the id
// WARN - Remember, this is a SQL feature only and will not take effect in MongoDriver
.updateAndGet({ name: 'iPhone X' }, returnKeyId) // or updateAngGet('name', 'iPhone X', returnKeyId)

console.log(productsUpdated) // [{ id: 1, name: 'iPhone X', quantity: 0 }, { id: 2, name: 'iPhone X', quantity: 0 }]
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/database",
"version": "1.0.5",
"version": "1.0.6",
"description": "Handle your application database with factories, seeders and query builder in Node.js",
"license": "MIT",
"author": "João Lenon <lenon@secjs.com.br>",
Expand Down
16 changes: 12 additions & 4 deletions src/Contracts/DatabaseContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,23 @@ export interface DatabaseContract {
* In case of bulk inserts only the last inserted id will be returned.
*
* @param values The values that are going to be inserted.
* @param returnKey The returned value from query default is id
* @return The newly created id or ids inside array.
*
*/
insert(values: any | any[]): Promise<string[]>
insert(values: any | any[], returnKey?: string): Promise<string[]>

/**
* InsertAndGet method
*
* Same as insert but return an array with the values inserted
*
* @param values The values that are going to be inserted.
* @param returnKey The returned value from query default is id
* @return The array with the values inserted.
*
*/
insertAndGet(values: any | any[]): Promise<any[]>
insertAndGet(values: any | any[], returnKey?: string): Promise<any[]>

/**
* Update method
Expand All @@ -173,10 +175,11 @@ export interface DatabaseContract {
*
* @param key The key to be updated.
* @param value The value of the key.
* @param returnKey The returned value from query default is id
* @return The number of affected rows.
*
*/
update(key: any | string, value?: any): Promise<string[]>
update(key: any | string, value?: any, returnKey?: string): Promise<string[]>

/**
* UpdateAndGet method
Expand All @@ -185,10 +188,15 @@ export interface DatabaseContract {
*
* @param key The key to be updated.
* @param value The value of the key.
* @param returnKey The returned value from query default is id
* @return The payload updated.
*
*/
updateAndGet(key: any | string, value?: any): Promise<any[]>
updateAndGet(
key: any | string,
value?: any,
returnKey?: string,
): Promise<any[]>

/**
* delete method
Expand Down
16 changes: 12 additions & 4 deletions src/Contracts/DriverContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,23 @@ export interface DriverContract {
* In case of bulk inserts only the last inserted id will be returned.
*
* @param values The values that are going to be inserted.
* @param returnKey The returned value from query default is id
* @return The newly created id or ids inside array.
*
*/
insert(values: any | any[]): Promise<string[]>
insert(values: any | any[], returnKey?: string): Promise<string[]>

/**
* InsertAndGet method
*
* Same as insert but return an array with the values inserted
*
* @param values The values that are going to be inserted.
* @param returnKey The returned value from query default is id
* @return The array with the values inserted.
*
*/
insertAndGet(values: any | any[]): Promise<any[]>
insertAndGet(values: any | any[], returnKey?: string): Promise<any[]>

/**
* Update method
Expand All @@ -432,10 +434,11 @@ export interface DriverContract {
*
* @param key The key to be updated.
* @param value The value of the key.
* @param returnKey The returned value from query default is id
* @return The number of affected rows.
*
*/
update(key: any | string, value?: any): Promise<string[]>
update(key: any | string, value?: any, returnKey?: string): Promise<string[]>

/**
* UpdateAndGet method
Expand All @@ -444,10 +447,15 @@ export interface DriverContract {
*
* @param key The key to be updated.
* @param value The value of the key.
* @param returnKey The returned value from query default is id
* @return The payload updated.
*
*/
updateAndGet(key: any | string, value?: any): Promise<any[]>
updateAndGet(
key: any | string,
value?: any,
returnKey?: string,
): Promise<any[]>

/**
* delete method
Expand Down
24 changes: 16 additions & 8 deletions src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,28 @@ export class Database implements DatabaseContract {
return this.driver.findMany()
}

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

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

async update(key: any | string, value?: any): Promise<string[]> {
return this.driver.update(key, value)
async update(
key: any | string,
value?: any,
returnKey = 'id',
): Promise<string[]> {
return this.driver.update(key, value, returnKey)
}

async updateAndGet(key: any | string, value?: any): Promise<any[]> {
return this.driver.updateAndGet(key, value)
async updateAndGet(
key: any | string,
value?: any,
returnKey = 'id',
): Promise<any[]> {
return this.driver.updateAndGet(key, value, returnKey)
}

async delete(): Promise<number> {
Expand Down
34 changes: 20 additions & 14 deletions src/Drivers/MySqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,16 @@ export class MySqlDriver implements DriverContract {
return this.buildSkip(page).buildLimit(limit).findMany()
}

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

return insert.map(i => `${i.id}`)
return insert.map(i => `${i[returnKey]}`)
}

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

return this.query().whereIn('id', arrayOfId)
return this.query().whereIn(returnKey, arrayOfId)
}

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

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

return data.map(i => `${i.id}`)
const data: any[] = await this.queryBuilder.update(key, _returnKey)

return data.map(i => `${i[_returnKey]}`)
}

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

return data.map(i => `${i.id}`)
return data.map(i => `${i[returnKey]}`)
}

async updateAndGet(key: any, value?: any): Promise<any[]> {
const arrayOfId = await this.update(key, value)
async updateAndGet(key: any, value?: any, returnKey = 'id'): Promise<any[]> {
let _returnKey = returnKey

if (!returnKey) _returnKey = value

const arrayOfId = await this.update(key, value, returnKey)

return this.query().whereIn('id', arrayOfId)
return this.query().whereIn(_returnKey, arrayOfId)
}

async increment(column: string, value: number): Promise<void> {
Expand Down
34 changes: 20 additions & 14 deletions src/Drivers/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,16 @@ export class PostgresDriver implements DriverContract {
return this.buildSkip(page).buildLimit(limit).findMany()
}

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

return insert.map(i => `${i.id}`)
return insert.map(i => `${i[returnKey]}`)
}

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

return this.query().whereIn('id', arrayOfId)
return this.query().whereIn(returnKey, arrayOfId)
}

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

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

return data.map(i => `${i.id}`)
const data: any[] = await this.queryBuilder.update(key, _returnKey)

return data.map(i => `${i[_returnKey]}`)
}

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

return data.map(i => `${i.id}`)
return data.map(i => `${i[returnKey]}`)
}

async updateAndGet(key: any, value?: any): Promise<any[]> {
const arrayOfId = await this.update(key, value)
async updateAndGet(key: any, value?: any, returnKey = 'id'): Promise<any[]> {
let _returnKey = returnKey

if (!returnKey) _returnKey = value

const arrayOfId = await this.update(key, value, returnKey)

return this.query().whereIn('id', arrayOfId)
return this.query().whereIn(_returnKey, arrayOfId)
}

async increment(column: string, value: number): Promise<void> {
Expand Down
34 changes: 20 additions & 14 deletions src/Drivers/SqlServerDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,16 @@ export class SqlServerDriver implements DriverContract {
return this.buildSkip(page).buildLimit(limit).findMany()
}

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

return insert.map(i => `${i.id}`)
return insert.map(i => `${i[returnKey]}`)
}

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

return this.query().whereIn('id', arrayOfId)
return this.query().whereIn(returnKey, arrayOfId)
}

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

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

return data.map(i => `${i.id}`)
const data: any[] = await this.queryBuilder.update(key, _returnKey)

return data.map(i => `${i[_returnKey]}`)
}

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

return data.map(i => `${i.id}`)
return data.map(i => `${i[returnKey]}`)
}

async updateAndGet(key: any, value?: any): Promise<any[]> {
const arrayOfId = await this.update(key, value)
async updateAndGet(key: any, value?: any, returnKey = 'id'): Promise<any[]> {
let _returnKey = returnKey

if (!returnKey) _returnKey = value

const arrayOfId = await this.update(key, value, returnKey)

return this.query().whereIn('id', arrayOfId)
return this.query().whereIn(_returnKey, arrayOfId)
}

async increment(column: string, value: number): Promise<void> {
Expand Down
Loading

0 comments on commit 6324056

Please sign in to comment.