Skip to content

Commit 82cdefc

Browse files
committed
add support for surround query
1 parent a4dbc6d commit 82cdefc

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/runtime/composables/useDB.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Query {
6262
limit: null,
6363
skip: null,
6464
search: null,
65+
surround: null,
6566
config: null
6667
}
6768
}
@@ -105,10 +106,18 @@ class Query {
105106
return this
106107
}
107108

109+
surround(slugOrPath, options) {
110+
const opts = { before: 1, after: 1, ...options }
111+
const path = `/${slugOrPath}`.replace(/\/+/g, '/')
112+
113+
this.query.surround = { ...opts, path, slug: slugOrPath }
114+
return this
115+
}
116+
108117
async resolve(data, options) {
109118
if (options.find) { this.limit(1) }
110119

111-
const { where, order, limit, skip, search, only, without, config } = this.query
120+
const { where, order, limit, skip, search, surround, only, without, config } = this.query
112121

113122
let records = data.filter(compile(where))
114123

@@ -120,6 +129,24 @@ class Query {
120129
records = sortOn(records, order)
121130
}
122131

132+
if (surround) {
133+
const index = records.findIndex(
134+
item => item.slug === surround.slug || item.path === surround.path
135+
)
136+
137+
const items = []
138+
139+
for (let i = 1; i <= surround.before; i++) {
140+
items.push(records[index - i])
141+
}
142+
143+
for (let i = 1; i <= surround.after; i++) {
144+
items.push(records[index + i])
145+
}
146+
147+
records = items
148+
}
149+
123150
if (skip) {
124151
records = records.slice(skip)
125152
}

0 commit comments

Comments
 (0)