/*
dbConfig format
{
"username": "oracle",
"port": "1521",
"password": "yourpassword",
"host": "yourhost/orcl"
}
*/
let dbConfig = require("./dbconfig-eduardo.json");
let db = require("oracle-db-util")(dbConfig).on("ready", function () {
console.log("it works");
});
(async () => {
try {
const rows = await db.findOne(
"USERS",//The table name
["ID", "NAME"], //optional, columns name
{
//optional where clause
name: "Eduardo Carrillo"
}
);
console.log("find one result");
console.log(rows);
} catch (e) {
throw e;
}
})();
(async () => {
try {
const { rows } = await db.find("USERS");
console.log("getAllResults");
console.log(rows);
} catch (e) {
throw e;
}
})();
// For unhandled promise rejection error
process.on("unhandledRejection", function (reason, p) {
console.log(
"Possibly Unhandled Rejection at: Promise ",
p,
" reason: ",
reason
);
// application specific logging here
});
// Prevent the server crashes with uncaught exception
process.on("uncaughtException", function (err) {
console.log("Caught exception: " + err.stack);
});
let dbConfig = require("./dbconfig-eduardo.json");
let db = require("oracle-db-util")(dbConfig).on("ready", function () {
console.log("it works");
});
//Insert row with returning values
(async () => {
const id = Math.floor(Math.random() * 10000);
try {
const data = {
id,
name: "Awesome user " + id
};
const row = await db.save("users", data, ["ID", "NAME"]);//save user and return the id and name
console.log("row saved");
console.log(row);
} catch (e) {
throw e;
}
})();
//common insert
(async () => {
const id = Math.floor(Math.random() * 10000);
try {
const data = {
id,
name: "Awesome user " + id
};
const {rowsAffected}= await db.save("users", data);
console.log("rows affected %d ",rowsAffected);
} catch (e) {
throw e;
}
})();
/*
require db file
*/
(async () => {
try {
const { rowsAffected } = await db.update({
table: "users", data: {
name: "Ecarrillo"
}, where: {
id: 1
}
});
console.log(rowsAffected);
} catch (e) {
throw e;
}
})();
let db = require("./db");
(async () => {
try {
const r = await db.deleteRow({
table: "users", where: {
id: 1575
}
});
console.log(r);
} catch (e) {
throw e;
}
})();