-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
158 lines (124 loc) · 3.68 KB
/
index.js
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
const express = require('express')
const querystring = require('querystring')
const parseAlgoliaSQL = require('./src/parseAlgoliaSQL')
const { getIndex, existIndex } = require('./src/indexes')
const { v4 } = require('uuid')
const createServer = (options) => {
const path = options.path || process.cwd()
const app = express()
app.use(express.json())
app.post('/1/indexes/:indexName/query', async (req, res) => {
const { body, params: { indexName } } = req
const { params: queryParams } = body
const db = getIndex(indexName, path)
const { query, filters } = querystring.parse(queryParams)
const searchExp = []
if (query !== undefined) {
searchExp.push(!query ? '*' : query)
}
if (filters) {
searchExp.push(parseAlgoliaSQL(db, filters))
}
const result = await db.SEARCH(...searchExp)
const hits = result.map((item) => {
const { obj } = item
obj.objectID = obj._id
delete obj._id
return obj
})
return res.json({
hits,
params: queryParams || '',
query: query || ''
})
})
app.post('/1/indexes/:indexName', async (req, res) => {
const { body, params: { indexName } } = req
const _id = v4()
const db = getIndex(indexName, path)
await db.PUT([{
_id,
...body
}])
return res.status(201).json({
createdAt: (new Date()).toISOString(),
taskID: 'algolite-task-id',
objectID: _id
})
})
app.put('/1/indexes/:indexName/:objectID', async (req, res) => {
const { body, params: { indexName } } = req
const { objectID } = req.params
const db = getIndex(indexName, path)
try {
await db.DELETE([objectID])
} catch (error) {
if (!error.notFound) {
return res.status(500).end()
}
}
await db.PUT([{
_id: objectID,
...body
}])
return res.status(201).json({
updatedAt: (new Date()).toISOString(),
taskID: 'algolite-task-id',
objectID
})
})
app.delete('/1/indexes/:indexName/:objectID', async (req, res) => {
const { objectID, indexName } = req.params
const db = getIndex(indexName, path)
try {
await db.DELETE([objectID])
} catch (error) {
if (!error.notFound) {
res.status(500).end()
}
}
return res.status(200).json({
deletedAt: (new Date()).toISOString(),
taskID: 'algolite-task-id',
objectID
})
})
app.post('/1/indexes/:indexName/deleteByQuery', async (req, res) => {
const { body, params: { indexName } } = req
const { params: queryParams } = body
const { facetFilters } = querystring.parse(queryParams)
const db = getIndex(indexName, path)
const searchExp = []
if (facetFilters) {
searchExp.push(parseAlgoliaSQL(db, facetFilters))
}
if (searchExp.length === 0) {
return res.status(400).json({
message: 'DeleteByQuery endpoint only supports tagFilters, facetFilters, numericFilters and geoQuery condition',
status: 400
})
}
const result = await db.SEARCH(...searchExp)
const ids = result.map(obj => obj._id)
await db.INDEX.DELETE(ids)
return res.status(201).json({
updatedAt: (new Date()).toISOString(),
taskID: 'algolite-task-id'
})
})
app.post('/1/indexes/:indexName/clear', async (req, res) => {
const { indexName } = req.params
if (!existIndex(indexName, path)) {
return res.status(400).end()
}
const db = getIndex(indexName, path)
const result = await db.INDEX.GET('')
const ids = result.map(obj => obj._id)
await db.INDEX.DELETE(ids)
return res.status(200).json({
taskID: 'algolite-task-id'
})
})
return app
}
module.exports = createServer