1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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();
}
|