-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDatabase.imba
More file actions
214 lines (161 loc) · 6.07 KB
/
Database.imba
File metadata and controls
214 lines (161 loc) · 6.07 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import type { Knex } from 'knex'
import { attachPaginate } from 'knex-paginate'
import querystring from 'querystring'
import location from '../Support/Helpers/location'
import isString from '../Support/Helpers/isString'
import Config from './Config'
import knex from 'knex'
let Database\Knex = null
try
let config = Object.assign({}, Config.make())
delete config.connection.driver
Database\Knex = knex(config)
knex.QueryBuilder.extend 'create', do(data)
const returning = Config.isReturningDriver ? ['*', { includeTriggerModifications: true }] : null
let results
if Array.isArray(returning)
results = await this.insert(data).returning(...returning)
else
results = await this.insert(data)
return !Array.isArray(data) && Array.isArray(results) ? results[0] : results
knex.QueryBuilder.extend 'get', do(columns)
let results\object[] = await this
if columns && Array.isArray(columns) && columns.length > 0
let mappedResults = []
for result in results
const object = {}
for column in columns
object[column] = result[column]
mappedResults.push(object)
return mappedResults
if this._hidden && Array.isArray(this._hidden) && this._hidden.length > 0
for result in results
for column in this._hidden
delete result[column]
results
knex.QueryBuilder.extend 'autoPaginate', do(pageSize = 20)
const locationContext = await location!
let page = 1
let query = {}
let url = ''
if locationContext
const search = querystring.parse(locationContext.search.replace(/^\?/, ''))
page = search.page || 1
query = search
url = locationContext.origin + locationContext.pathname
return this.pagination({ page, query, url, pageSize })
knex.QueryBuilder.extend 'pagination', do({page = 1, pageSize = 20, query = {}, url = ''})
page = parseInt(page)
if !isString(url)
throw new TypeError('url must be a string')
if isNaN(page)
page = 1
if page < 1
page = 1
if pageSize < 1
pageSize = 20
def getPagesArray currentPage, totalPages
const maxPagesToShow = 5
const halfMaxPagesToShow = Math.floor(maxPagesToShow / 2)
let startPage = Math.max(1, currentPage - halfMaxPagesToShow)
let endPage = Math.min(totalPages, startPage + maxPagesToShow - 1)
if endPage < totalPages
startPage = Math.max(1, currentPage - halfMaxPagesToShow)
else
startPage = Math.max(1, totalPages - maxPagesToShow + 1)
endPage = Math.min(startPage + maxPagesToShow - 1, totalPages)
Array.from({ length: endPage - startPage + 1 }, do(_, i) startPage + i)
const countQuery = this.clone!.clearSelect!.count('* as total')
return Promise.all([this.clone!.limit(pageSize).offset((page - 1) * pageSize), countQuery])
.then do([data, [{ total }]])
const totalPages = Math.ceil(total / pageSize)
const pages = getPagesArray(page, totalPages)
const prevPage = page > 1 ? page - 1 : null
const nextPage = page < totalPages ? page + 1 : null
const firstPage = 1
const lastPage = totalPages
if this._hidden && Array.isArray(this._hidden) && this._hidden.length > 0
for result in data
for column in this._hidden
delete result[column]
const results = {
data,
pagination: {
total,
pageSize,
currentPage: page,
totalPages,
pages,
firstPage,
lastPage,
prevPage,
nextPage,
}
}
if query
const links = {}
url = url.length > 1 ? url.replace(/\/+$/, '') + '/' : ''
params = []
keys = Object.keys(query)
for key in keys
params.push(key + '=' + encodeURIComponent(key == 'page' ? (results.pagination.firstPage) : query[key]))
links.firstPage = {
label: 'First',
active: results.pagination.firstPage === results.pagination.currentPage,
url: url + '?' + params.join('&')
}
params = []
keys = Object.keys(query)
for key in keys
params.push(key + '=' + encodeURIComponent(key == 'page' ? (results.pagination.prevPage) : query[key]))
links.prevPage = results.pagination.prevPage ? {
label: 'Previous',
active: results.pagination.prevPage === results.pagination.currentPage,
url: url + '?' + params.join('&')
} : null
for page in pages
query.page = page
params = []
keys = Object.keys(query)
for key in keys
params.push(key + '=' + encodeURIComponent(query[key]))
links[page] = {
label: page,
active: page === results.pagination.currentPage,
url: url + '?' + params.join('&')
}
params = []
keys = Object.keys(query)
for key in keys
params.push(key + '=' + encodeURIComponent(key == 'page' ? (results.pagination.nextPage) : query[key]))
links.nextPage = results.pagination.nextPage ? {
label: 'Next',
active: results.pagination.nextPage === results.pagination.currentPage,
url: url + '?' + params.join('&')
} : null
params = []
keys = Object.keys(query)
for key in keys
params.push(key + '=' + encodeURIComponent(key == 'page' ? (results.pagination.lastPage) : query[key]))
links.lastPage = {
label: 'Last',
active: results.pagination.lastPage === results.pagination.currentPage,
url: url + '?' + params.join('&')
}
results.pagination.links = links
return results
knex.QueryBuilder.extend 'hidden', do(columns)
this._hidden = columns
return this
knex.QueryBuilder.extend 'softDelete', do this.update({ deleted_at: Database.fn.now! })
knex.QueryBuilder.extend 'restore', do this.update({ deleted_at: null })
knex.QueryBuilder.extend 'withTrashed', do this.where do this.whereNull('deleted_at').orWhereNotNull('deleted_at')
knex.QueryBuilder.extend 'withoutTrashed', do this.whereNull('deleted_at')
knex.QueryBuilder.extend 'onlyTrashed', do this.whereNotNull('deleted_at')
knex.TableBuilder.extend 'softDeletes', do this.timestamp('deleted_at').nullable!
knex.TableBuilder.extend 'dropSoftDeletes', do this.dropColumn('deleted_at')
knex.TableBuilder.extend 'rememberToken', do this.string('remember_token').nullable!
attachPaginate!
catch
Database\Knex = null
export default Database