awSQL/lib/Insert.js
2024-11-22 04:36:31 +01:00

55 lines
1.5 KiB
JavaScript

class Insert {
#instance;
#database;
#into;
#data;
constructor(instance, defaultDatabase, into){
this.#instance = instance;
this.#database = defaultDatabase;
this.#into = into;
}
/**
* Selects a database for this query
* @param {String} database - Name of the database
* @returns {this}
*/
selectDatabase = (database) => {
this.#database = database;
return this;
}
/**
* The data (rows) to insert
* @param {Array} objects - Array containing objects to insert, where the key represents the column-name. All objects must have the same structure!
* @returns
*/
data = (objects) => {
this.#data = objects;
return this;
}
execute = async () => {
if (!this.#data) throw new Error("Insert: tried to insert without data");
if (!this.#instance.isConnected()) throw new Error(`Can't execute query: Instance has no connection`);
if (!this.#database) throw new Error(`Can't execute query: Database not selected`);
const columns = Object.keys(this.#data[0]);
const queryString = `INSERT INTO ${this.#database}.${this.#into} (${columns.toString()}) VALUES ?`;
const values = [];
for (let obj of this.#data){
let thisRow = [];
for (let key of columns){
thisRow.push(obj[key]);
}
values.push(thisRow);
}
return await this.#instance.queryRaw(queryString, [values]);
}
}
module.exports = Insert;