quickpostgres
Version:
An easy, beginner-friendly PostgreSQL database wrapper similar to quick.db.
33 lines • 1.22 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const unset_1 = __importDefault(require("lodash/unset"));
exports.default = async (db, params, options) => {
// Fetch entry
const fetched = await db.query(`SELECT * FROM ${options.table} WHERE ID = ($1)`, [params.id]);
// If empty, return false
let json;
if (!fetched.rows[0])
return false;
else
json = JSON.parse(fetched.rows[0].json);
// Check if user wants to delete a prop inside an object
if (typeof json === "object" && params.ops.target) {
(0, unset_1.default)(json, params.ops.target);
json = JSON.stringify(json);
db.query(`UPDATE ${options.table} SET json = ($1) WHERE ID = ($2)`, [
json,
params.id,
]);
return true;
}
else if (params.ops.target)
throw new TypeError("Fetched value is not an object.");
else
await db.query(`DELETE FROM ${options.table} WHERE ID = ($1)`, [params.id]);
// Resolve
return true;
};
//# sourceMappingURL=delete.js.map
;