summaryrefslogtreecommitdiff
path: root/src/app/db/query.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2021-10-17 02:52:05 +0200
committerJules Laplace <julescarbon@gmail.com>2021-10-17 02:52:05 +0200
commit06ecdf2af182034496e2123852deee4a58de1043 (patch)
treec8d4eb9664dd368bee5a4bf73dd1e02015ecaf39 /src/app/db/query.js
making a shoebox
Diffstat (limited to 'src/app/db/query.js')
-rw-r--r--src/app/db/query.js148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/app/db/query.js b/src/app/db/query.js
new file mode 100644
index 0000000..65a9157
--- /dev/null
+++ b/src/app/db/query.js
@@ -0,0 +1,148 @@
+/**
+ * Define standard database queries
+ * @module app/db/query
+ */
+
+import {
+ buildIndexQuery,
+ buildCountQuery,
+ buildPaginationResponse,
+} from "app/db/helpers";
+
+/**
+ * Fetch a set of database objects matching a query
+ * @param {bookshelf.Model} Model the model to query
+ * @param {Object} query query options
+ * @param {Object} paginate optional pagination options
+ * @param {User} user the current `request.user`
+ * @param {function} queryBuilder a custom function to build the query
+ * @param {Object} columns valid columns on the model
+ * @param {Array} withRelated a custom function to build the query
+ * @returns {Promise} a query which resolves to the desired objects
+ */
+export function index({
+ Model,
+ query,
+ paginate,
+ user,
+ columns,
+ queryBuilder,
+ withRelated,
+}) {
+ return Model.query((builder) =>
+ buildIndexQuery({
+ builder,
+ Model,
+ query,
+ paginate,
+ user,
+ columns,
+ queryBuilder,
+ })
+ ).fetchAll({ withRelated });
+}
+
+/**
+ * Count database objects matching a query
+ * @param {bookshelf.Model} Model the model to query
+ * @param {Object} query query options
+ * @param {Object} paginate optional pagination options
+ * @param {User} user the current `request.user`
+ * @param {function} queryBuilder a custom function to build the query
+ * @param {Object} columns valid columns on the model
+ * @returns {Promise} a query which resolves to the count
+ */
+export function count({ Model, query, paginate, user, queryBuilder, columns }) {
+ return Model.query((builder) =>
+ buildCountQuery({
+ builder,
+ Model,
+ query,
+ paginate,
+ user,
+ queryBuilder,
+ columns,
+ })
+ )
+ .count()
+ .then((rowCount) => {
+ return buildPaginationResponse({ rowCount, query, paginate });
+ });
+}
+
+/**
+ * Fetch single object by ID
+ * @param {bookshelf.Model} Model the model to query
+ * @param {number} objectID the primary key of the object
+ * @param {string} field optional, the field to query
+ * @param {Object} criteria additional criteria for the show query (when specifying a pivot table by its relations)
+ * @returns {Promise} a query which resolves to the desired object
+ */
+export function show({ Model, objectID, field, criteria, withRelated }) {
+ return new Model({
+ ...criteria,
+ [field || Model.prototype.idAttribute]: objectID,
+ }).fetch({ withRelated });
+}
+
+/**
+ * Fetch several objects by ID
+ * @param {bookshelf.Model} Model the model to query
+ * @param {Array} ids a list of keys to query
+ * @param {string} field optional, the field to query
+ * @returns {Promise} a query which resolves to the desired objects
+ */
+export function showIDs({ Model, ids, field, queryBuilder, withRelated }) {
+ return Model.query((builder) => {
+ builder.whereIn([field || Model.prototype.idAttribute], ids);
+ if (queryBuilder) {
+ queryBuilder(builder);
+ }
+ return builder;
+ }).fetchAll({ withRelated });
+}
+
+/**
+ * Create a record
+ * @param {bookshelf.Model} Model the model to create
+ * @param {Object} data the data to insert
+ * @param {bookshelf.transaction} transaction optional transaction
+ * @returns {Promise} a query which resolves to the new object
+ */
+export function create({ Model, data, transaction }) {
+ return new Model(data).save({}, { transacting: transaction });
+}
+
+/**
+ * Update a complete record
+ * @param {Object} instance an instance of a bookshelf model
+ * @param {Object} data the complete updated record.
+ * @param {bookshelf.transaction} transaction optional transaction
+ * @returns {Promise} a query which resolves to the updated object
+ */
+export function update({ instance, data, transaction }) {
+ return instance.save(data, { transacting: transaction });
+}
+
+/**
+ * Partially update a record by ID
+ * @param {bookshelf.Model} Model the model to affect
+ * @param {number} objectID the primary key of the object
+ * @param {Object} data the fields to update.
+ * @returns {Promise} a query which resolves to the updated object
+ */
+export function patch({ Model, objectID, data }) {
+ return Model.forge({ [Model.prototype.idAttribute]: objectID }).save(data, {
+ patch: true,
+ });
+}
+
+/**
+ * Destroy a record by ID
+ * @param {bookshelf.Model} Model the model to affect
+ * @param {number} objectID the primary key of the object
+ * @returns {Promise} a query which resolves when the object is destroyed
+ */
+export function destroy({ Model, objectID }) {
+ return new Model({ [Model.prototype.idAttribute]: objectID }).destroy();
+}