mirror of
https://github.com/nocodb/nocodb.git
synced 2026-05-02 22:57:08 +00:00
Feature: Bulk insert, delete, read - npm v0.2.5
This commit is contained in:
71
lib/xsql.js
71
lib/xsql.js
@@ -173,6 +173,65 @@ class Xsql {
|
||||
|
||||
}
|
||||
|
||||
getBulkInsertStatement(tableName, objectArray, queryParamsObj) {
|
||||
|
||||
if (tableName in this.metaDb.tables && objectArray) {
|
||||
|
||||
let insObj = objectArray[0];
|
||||
|
||||
// goal => insert into ?? (?,?..?) values ? [tablName, col1,col2...coln,[[ObjValues_1],[ObjValues_2],...[ObjValues_N]]
|
||||
queryParamsObj.query = ' INSERT INTO ?? ( '
|
||||
queryParamsObj.params.push(tableName)
|
||||
|
||||
let cols = [];
|
||||
let colPresent = false;
|
||||
|
||||
/**************** START : prepare column names to be inserted ****************/
|
||||
// iterate over all column in table and have only ones existing in objects to be inserted
|
||||
for (var i = 0; i < this.metaDb.tables[tableName]['columns'].length; ++i) {
|
||||
|
||||
let colName = this.metaDb.tables[tableName]['columns'][i]['column_name']
|
||||
|
||||
|
||||
if (colName in insObj) {
|
||||
|
||||
if (colPresent) {
|
||||
queryParamsObj.query += ','
|
||||
}
|
||||
|
||||
queryParamsObj.query += colName
|
||||
|
||||
colPresent = true;
|
||||
|
||||
}
|
||||
|
||||
cols.push(colName)
|
||||
|
||||
//console.log('> > ', queryParamsObj.query);
|
||||
|
||||
}
|
||||
|
||||
queryParamsObj.query += ' ) values ?'
|
||||
/**************** END : prepare column names to be inserted ****************/
|
||||
|
||||
|
||||
/**************** START : prepare value object in prepared statement ****************/
|
||||
// iterate over sent object array
|
||||
let arrOfArr = []
|
||||
for (var i = 0; i < objectArray.length; ++i) {
|
||||
let arrValues = []
|
||||
for (var j = 0; j < cols.length; ++j) {
|
||||
if (cols[j] in objectArray[i])
|
||||
arrValues.push(objectArray[i][cols[j]])
|
||||
}
|
||||
arrOfArr.push(arrValues);
|
||||
}
|
||||
queryParamsObj.params.push(arrOfArr)
|
||||
/**************** END : prepare value object in prepared statement ****************/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
getGroupByClause(_groupby, tableName, queryParamsObj) {
|
||||
|
||||
if (_groupby) {
|
||||
@@ -350,6 +409,14 @@ class Xsql {
|
||||
|
||||
}
|
||||
|
||||
getPrimaryKeyName(tableName) {
|
||||
let pk = null
|
||||
if (tableName in this.metaDb.tables) {
|
||||
pk = this.metaDb.tables[tableName].primaryKeys[0]['column_name']
|
||||
}
|
||||
return pk
|
||||
}
|
||||
|
||||
getPrimaryKeyWhereClause(tableName, pksValues) {
|
||||
|
||||
let whereClause = '';
|
||||
@@ -462,6 +529,9 @@ class Xsql {
|
||||
routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/findOne', 'findOne'))
|
||||
routes.push(this.prepareRoute(internal, 'post', apiPrefix, tableName, 'create'))
|
||||
routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName, 'list'))
|
||||
routes.push(this.prepareRoute(internal, 'post', apiPrefix, tableName + '/bulk', 'bulkInsert'))
|
||||
routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/bulk', 'bulkRead'))
|
||||
routes.push(this.prepareRoute(internal, 'delete', apiPrefix, tableName + '/bulk', 'bulkDelete'))
|
||||
routes.push(this.prepareRoute(internal, 'put', apiPrefix, tableName, 'update'))
|
||||
routes.push(this.prepareRoute(internal, 'get', apiPrefix, tableName + '/:id', 'read'))
|
||||
routes.push(this.prepareRoute(internal, 'patch', apiPrefix, tableName + '/:id', 'patch'))
|
||||
@@ -570,7 +640,6 @@ class Xsql {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user