ADD: TsApiFeatures
ADD: Typesense ADD: Index all belongings router, controller & service
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
const catchAsync = require('../../globalErrorHandler/catchAsync')
|
||||
const belongingService = require('./belongingService')
|
||||
|
||||
exports.indexAllBelongings = catchAsync(async (req, res, next) => {
|
||||
const { indexedCount } = await belongingService.indexAllBelongings()
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
indexedCount
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
exports.getAllBelongings = catchAsync(async (req, res, next) => {
|
||||
const belongings = await belongingService.getAllBelongings()
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ router.get('/admin/get-all', perm.requires('getallbelongings'), belongingControl
|
||||
|
||||
router.post('/admin/create', perm.requires('createabelonging'), belongingController.createBelonging)
|
||||
|
||||
router.post('/admin/index-all', perm.requires('indexbelongings', belongingController.indexAllBelongings))
|
||||
|
||||
router.get('/admin/get-one/:belongingId', perm.requires('getabelonging'), belongingController.getBelonging)
|
||||
|
||||
router.put('/admin/update/:belongingId', perm.requires('updateabelonging'), belongingController.updateBelonging)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const Belonging = require('./belongingModel')
|
||||
const AppError = require('../../globalErrorHandler/AppError')
|
||||
const { client } = require('../../setup/typesense')
|
||||
|
||||
module.exports.getAllBelongings = async () => {
|
||||
return Belonging.find({}, null, null)
|
||||
@@ -38,3 +39,72 @@ module.exports.deleteBelonging = async (belongingId) => {
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const ensureCollectionExists = async () => {
|
||||
const collections = await client.collections().retrieve()
|
||||
const collectionExists = collections.some((collection) => collection.name === 'belongings')
|
||||
|
||||
if (collectionExists) {
|
||||
await client.collections('belongings').delete()
|
||||
}
|
||||
|
||||
const schema = getTypeSenseSchema()
|
||||
|
||||
await client.collections().create(schema)
|
||||
console.log('Collection "belongings" created successfully')
|
||||
}
|
||||
const indexAllBelongings = async () => {
|
||||
const batchSize = 20
|
||||
const count = await Product.countDocuments({isTrash: false, isActive: true, isHidden: false}, null)
|
||||
const rounds = Math.ceil(count / batchSize)
|
||||
|
||||
await ensureCollectionExists()
|
||||
|
||||
for (let i = 1; i <= rounds; i++) {
|
||||
console.log(`Indexing ${batchSize} products, Round ${i}`)
|
||||
|
||||
const belongings = await Belonging.find({}, null, null)
|
||||
.populate('category')
|
||||
.skip((i - 1) * batchSize)
|
||||
.limit(batchSize)
|
||||
|
||||
let documents = []
|
||||
|
||||
for (const belonging of belongings) {
|
||||
|
||||
const data = {
|
||||
}
|
||||
|
||||
for (let feature of product.features) {
|
||||
if (feature.definition && feature.value) {
|
||||
data[feature.definition.englishName] = feature.value
|
||||
}
|
||||
}
|
||||
|
||||
documents.push(data)
|
||||
}
|
||||
|
||||
await client.collections('belonging').documents().import(documents, {action: 'emplace'})
|
||||
}
|
||||
|
||||
return {totalCount: count}
|
||||
}
|
||||
|
||||
|
||||
const getTypeSenseSchema = () => {
|
||||
return {
|
||||
name: 'belongings',
|
||||
enable_nested_fields: true,
|
||||
fields: [
|
||||
{ name: '_id', type: 'string', facet: false, infix: false },
|
||||
{ name: 'projectCode', type: 'string', facet: false, infix: true },
|
||||
{ name: 'belongingCode', type: 'string', facet: true },
|
||||
{ name: 'category', type: 'string[]', facet: true },
|
||||
{ name: 'nativeCategory', type: 'string[]', facet: false },
|
||||
{ name: 'documents', type: 'string[]', facet: false },
|
||||
{ name: 'description', type: 'string', facet: false },
|
||||
{ name: 'coordinates', type: 'geopoint' },
|
||||
],
|
||||
default_sorting_field: 'createdAt',
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user