Skip to content

Commit a68679f

Browse files
chore: wip
1 parent 278cb26 commit a68679f

File tree

7 files changed

+80
-7
lines changed

7 files changed

+80
-7
lines changed

app/Models/User.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default {
1717
},
1818
useTimestamps: true, // defaults to true, `timestampable` used as an alias
1919
useSearch: {
20-
// defaults to true, `searchable` used as an alias
21-
displayable: ['id', 'job_title', 'name', 'email'], // the fields to become searchable (defaults to all fields)
20+
21+
displayable: ['id', 'job_title', 'name', 'email'], // the fields to become d (defaults to all fields)
2222
searchable: ['job_title', 'name', 'email'], // the fields to become searchable (defaults to all fields)
2323
sortable: ['created_at', 'updated_at'], // the fields to become sortable (defaults to all fields)
2424
filterable: ['job_title'], // the fields to become filterable (defaults to all fields)
@@ -110,8 +110,8 @@ export default {
110110
},
111111
},
112112
get: {
113-
formalName: (user: UserModel) => {
114-
const name = user?.name as string
113+
formalName: (attributes) => {
114+
const name = attributes.user?.name as string
115115
const nameParts = name.split(' ')
116116

117117
return `${capitalize(nameParts.pop()!)}, ${capitalize(nameParts.join(' '))}`
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import process from 'node:process'
2+
import { log } from '@stacksjs/logging'
3+
import { flushModelDocuments } from '@stacksjs/search-engine'
4+
5+
const result = await flushModelDocuments()
6+
7+
if (result?.isErr()) {
8+
console.error(result.error)
9+
log.error('generateMigrations failed', result.error)
10+
}
11+
12+
process.exit(0)

storage/framework/core/buddy/src/commands/search.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { ExitCode } from '@stacksjs/types'
77

88
export function search(buddy: CLI): void {
99
const descriptions = {
10-
search: 'Indexes database data to search engine',
10+
import: 'Indexes database data to search engine',
11+
flush: 'Flushes all data from search engine',
1112
settings: 'Update index settings',
1213
model: 'Target a specific model',
1314
verbose: 'Enable verbose output',
1415
}
1516

1617
buddy
17-
.command('search-engine:update', descriptions.search)
18+
.command('search-engine:import', descriptions.import)
1819
.option('-m, --model [model]', descriptions.model, { default: false })
1920
.option('--verbose', descriptions.verbose, { default: false })
2021
.action(async (options: SearchOptions) => {
@@ -40,6 +41,33 @@ export function search(buddy: CLI): void {
4041
process.exit(ExitCode.Success)
4142
})
4243

44+
buddy
45+
.command('search-engine:flush', descriptions.flush)
46+
.option('-m, --model [model]', descriptions.model, { default: false })
47+
.option('--verbose', descriptions.verbose, { default: false })
48+
.action(async (options: SearchOptions) => {
49+
log.debug('Running `search-engine:flush` ...', options)
50+
51+
const perf = await intro('search-engine:flush')
52+
const result = await runAction(Action.SearchEngineFlush, options)
53+
54+
if (result.isErr()) {
55+
await outro(
56+
'While running the stripe:setup command, there was an issue',
57+
{ startTime: perf, useSeconds: true },
58+
result.error,
59+
)
60+
process.exit()
61+
}
62+
63+
await outro(`Successfully imported model data to search engine.`, {
64+
startTime: perf,
65+
useSeconds: true,
66+
})
67+
68+
process.exit(ExitCode.Success)
69+
})
70+
4371
buddy
4472
.command('search-engine:sync-settings', descriptions.settings)
4573
.option('-m, --model [model]', descriptions.model, { default: false })

storage/framework/core/enums/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export enum Action {
8888
RouteList = 'route/list', // ✅
8989
StripeSetup = 'saas/setup',
9090
SearchEngineImport = 'search/import',
91+
SearchEngineFlush = 'search/flush',
9192
SearchEngineSyncSettings = 'search/settings',
9293
Test = 'test/index',
9394
TestUi = 'test/ui',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Model } from '@stacksjs/types'
2+
import { type Err, ok, type Ok } from '@stacksjs/error-handling'
3+
import { log } from '@stacksjs/logging'
4+
import { getModelName, getTableName } from '@stacksjs/orm'
5+
import { path } from '@stacksjs/path'
6+
import { useSearchEngine } from '@stacksjs/search-engine'
7+
import { globSync } from '@stacksjs/storage'
8+
9+
export async function flushModelDocuments(): Promise<Ok<string, never> | Err<string, any>> {
10+
try {
11+
const userModelFiles = globSync([path.userModelsPath('*.ts')], { absolute: true })
12+
const { deleteIndex } = useSearchEngine()
13+
14+
for (const model of userModelFiles) {
15+
const modelInstance = (await import(model)).default as Model
16+
const searchable = modelInstance.traits?.useSearch
17+
18+
const tableName = getTableName(modelInstance, model)
19+
20+
if (searchable && (typeof searchable === 'boolean' || typeof searchable === 'object'))
21+
await deleteIndex(tableName)
22+
}
23+
24+
return ok('Successfully flushed all model data from search engine!')
25+
}
26+
catch (err: any) {
27+
log.error(err)
28+
29+
return err(err)
30+
}
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './add'
2+
export * from './flush'
23
export * from './settings'

storage/framework/core/search-engine/src/documents/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Model } from '@stacksjs/types'
22
import { type Err, ok, type Ok } from '@stacksjs/error-handling'
33
import { log } from '@stacksjs/logging'
4-
import { getModelName, getTableName } from '@stacksjs/orm'
4+
import { getTableName } from '@stacksjs/orm'
55
import { path } from '@stacksjs/path'
66
import { useSearchEngine } from '@stacksjs/search-engine'
77
import { globSync } from '@stacksjs/storage'

0 commit comments

Comments
 (0)