@objectstack/plugin-pinyin-search 15.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ /**
4
+ * #2486 — companion projection: before-save stamping, no-write-amplification
5
+ * guard, backfill and rebuild reconcile. Uses a minimal fake engine (the
6
+ * boot-backfill.test.ts pattern) with a stub registry.
7
+ */
8
+
9
+ import { describe, it, expect, beforeEach } from 'vitest';
10
+ import {
11
+ bindSearchCompanionHooks,
12
+ backfillSearchCompanion,
13
+ rebuildSearchCompanion,
14
+ PINYIN_SEARCH_HOOK_PACKAGE,
15
+ } from './companion-projection.js';
16
+
17
+ interface Row { [k: string]: any }
18
+
19
+ const contactSchema = {
20
+ name: 'crm_contact',
21
+ nameField: 'name',
22
+ fields: {
23
+ name: { type: 'text' },
24
+ email: { type: 'email' },
25
+ __search: { type: 'text', hidden: true, readonly: true, system: true },
26
+ },
27
+ };
28
+
29
+ const plainSchema = {
30
+ name: 'crm_note',
31
+ fields: { title: { type: 'text' } }, // no companion provisioned
32
+ };
33
+
34
+ function makeEngine(schemas: any[]) {
35
+ const tables: Record<string, Row[]> = {};
36
+ const hooks: Record<string, Array<{ handler: (ctx: any) => any; opts: any }>> = {};
37
+ const byName = new Map(schemas.map((s) => [s.name, s]));
38
+ const engine = {
39
+ _tables: tables,
40
+ registry: {
41
+ getObject: (n: string) => byName.get(n),
42
+ getAllObjects: () => [...byName.values()],
43
+ },
44
+ registerHook(event: string, handler: any, opts?: any) {
45
+ (hooks[event] ??= []).push({ handler, opts });
46
+ },
47
+ unregisterHooksByPackage(packageId: string) {
48
+ let removed = 0;
49
+ for (const [event, entries] of Object.entries(hooks)) {
50
+ const kept = entries.filter((e) => e.opts?.packageId !== packageId);
51
+ removed += entries.length - kept.length;
52
+ hooks[event] = kept;
53
+ }
54
+ return removed;
55
+ },
56
+ async trigger(event: string, ctx: any) {
57
+ for (const { handler } of hooks[event] ?? []) await handler(ctx);
58
+ },
59
+ async find(o: string, opts?: any) {
60
+ const rows = tables[o] ?? [];
61
+ const offset = opts?.offset ?? 0;
62
+ return rows.slice(offset, offset + (opts?.limit ?? rows.length));
63
+ },
64
+ async insert(o: string, data: Row) {
65
+ const ctx = { object: o, event: 'beforeInsert', input: { data } };
66
+ await engine.trigger('beforeInsert', ctx);
67
+ (tables[o] ??= []).push({ ...ctx.input.data });
68
+ return ctx.input.data;
69
+ },
70
+ async update(o: string, data: Row) {
71
+ const ctx = { object: o, event: 'beforeUpdate', input: { id: data.id, data } };
72
+ await engine.trigger('beforeUpdate', ctx);
73
+ const t = tables[o] ?? [];
74
+ const i = t.findIndex((r) => r.id === data.id);
75
+ if (i >= 0) t[i] = { ...t[i], ...ctx.input.data };
76
+ return t[i];
77
+ },
78
+ _hooks: hooks,
79
+ };
80
+ return engine;
81
+ }
82
+
83
+ describe('bindSearchCompanionHooks', () => {
84
+ let engine: ReturnType<typeof makeEngine>;
85
+
86
+ beforeEach(() => {
87
+ engine = makeEngine([contactSchema, plainSchema]);
88
+ bindSearchCompanionHooks(engine as any);
89
+ });
90
+
91
+ it('binds beforeInsert + beforeUpdate globally and is idempotent (rebind-safe)', () => {
92
+ bindSearchCompanionHooks(engine as any); // rebind
93
+ expect(engine._hooks.beforeInsert).toHaveLength(1);
94
+ expect(engine._hooks.beforeUpdate).toHaveLength(1);
95
+ expect(engine._hooks.beforeInsert[0].opts.packageId).toBe(PINYIN_SEARCH_HOOK_PACKAGE);
96
+ expect(engine._hooks.beforeInsert[0].opts.object).toBeUndefined(); // global
97
+ });
98
+
99
+ it('stamps __search on insert when the name field carries CJK', async () => {
100
+ const row = await engine.insert('crm_contact', { id: 'c1', name: '张伟' });
101
+ expect(row.__search).toBe('zhangwei zw');
102
+ });
103
+
104
+ it('leaves __search null for latin names (source column already matches)', async () => {
105
+ const row = await engine.insert('crm_contact', { id: 'c2', name: 'Ada Lovelace' });
106
+ expect(row.__search).toBe(null);
107
+ });
108
+
109
+ it('recomputes on update ONLY when the source field is in the patch', async () => {
110
+ await engine.insert('crm_contact', { id: 'c3', name: '张伟' });
111
+ // email-only patch: no recompute, no companion key added
112
+ const patch: Row = { id: 'c3', email: 'zw@example.com' };
113
+ await engine.update('crm_contact', patch);
114
+ expect('__search' in patch).toBe(false);
115
+ // name patch: recompute
116
+ const updated = await engine.update('crm_contact', { id: 'c3', name: '王芳' });
117
+ expect(updated.__search).toBe('wangfang wf');
118
+ });
119
+
120
+ it('clears the companion when a CJK name is renamed to latin (no stale recall)', async () => {
121
+ await engine.insert('crm_contact', { id: 'c4', name: '张伟' });
122
+ const updated = await engine.update('crm_contact', { id: 'c4', name: 'Victor Zhang' });
123
+ expect(updated.__search).toBe(null);
124
+ });
125
+
126
+ it('ignores objects without a provisioned companion column', async () => {
127
+ const row = await engine.insert('crm_note', { id: 'n1', title: '会议纪要' });
128
+ expect('__search' in row).toBe(false);
129
+ });
130
+ });
131
+
132
+ describe('backfillSearchCompanion / rebuildSearchCompanion', () => {
133
+ it('fills rows missing a blob, in pages, and skips rows that need none', async () => {
134
+ const engine = makeEngine([contactSchema]);
135
+ engine._tables.crm_contact = [
136
+ { id: 'a', name: '张伟', __search: null }, // hook-bypassing write → fill
137
+ { id: 'b', name: 'Ada', __search: null }, // latin → skip
138
+ { id: 'c', name: '王芳', __search: 'wangfang wf' }, // already filled → skip
139
+ { id: 'd', name: '李雷', __search: null }, // fill (second page)
140
+ ];
141
+ const result = await backfillSearchCompanion(engine as any, undefined, { batchSize: 2 });
142
+ expect(result).toEqual({ objects: 1, scanned: 4, updated: 2 });
143
+ const rows = engine._tables.crm_contact;
144
+ expect(rows.find((r) => r.id === 'a')!.__search).toBe('zhangwei zw');
145
+ expect(rows.find((r) => r.id === 'b')!.__search).toBe(null);
146
+ expect(rows.find((r) => r.id === 'd')!.__search).toBe('lilei ll');
147
+ });
148
+
149
+ it('is idempotent — a second pass updates nothing', async () => {
150
+ const engine = makeEngine([contactSchema]);
151
+ engine._tables.crm_contact = [{ id: 'a', name: '张伟', __search: null }];
152
+ await backfillSearchCompanion(engine as any);
153
+ const second = await backfillSearchCompanion(engine as any);
154
+ expect(second.updated).toBe(0);
155
+ });
156
+
157
+ it('rebuild recomputes everything, clearing stale blobs (reconcile entry)', async () => {
158
+ const engine = makeEngine([contactSchema]);
159
+ engine._tables.crm_contact = [
160
+ { id: 'a', name: 'Renamed To Latin', __search: 'zhangwei zw' }, // stale → cleared
161
+ { id: 'b', name: '王芳', __search: 'wrongblob' }, // wrong → recomputed
162
+ ];
163
+ const result = await rebuildSearchCompanion(engine as any);
164
+ expect(result.updated).toBe(2);
165
+ expect(engine._tables.crm_contact[0].__search).toBe(null);
166
+ expect(engine._tables.crm_contact[1].__search).toBe('wangfang wf');
167
+ });
168
+ });
@@ -0,0 +1,214 @@
1
+ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ /**
4
+ * `__search` companion-column projection (#2486).
5
+ *
6
+ * The column itself is DECLARED at object compile time by the SchemaRegistry
7
+ * (`provisionSearchCompanion`, gated on `OS_SEARCH_PINYIN_ENABLED`); this
8
+ * module only FILLS the value — the `plugin-sharing` primary-BU projection
9
+ * pattern (column on the object, plugin maintains it via hooks).
10
+ *
11
+ * Write path: global `beforeInsert`/`beforeUpdate` hooks stamp
12
+ * `data.__search` whenever a companion source field (the object's
13
+ * display/name field) is present in the write — i.e. only when the source
14
+ * actually changed, avoiding write amplification. Writes that bypass hooks
15
+ * (bulk import, direct migration) leave the companion empty; the boot
16
+ * backfill and the `rebuildSearchCompanion` reconcile entry cover that.
17
+ */
18
+
19
+ import {
20
+ SEARCH_COMPANION_FIELD,
21
+ resolveSearchCompanionSources,
22
+ containsCJK,
23
+ } from '@objectstack/objectql';
24
+ import { computeSearchCompanionValue } from './pinyin.js';
25
+
26
+ const SYSTEM_CTX = { isSystem: true, positions: [], permissions: [] } as const;
27
+
28
+ export const PINYIN_SEARCH_HOOK_PACKAGE = 'plugin-pinyin-search:companion';
29
+
30
+ interface MinimalEngine {
31
+ registerHook(
32
+ event: string,
33
+ handler: (ctx: any) => any | Promise<any>,
34
+ options?: { object?: string | string[]; priority?: number; packageId?: string },
35
+ ): void;
36
+ unregisterHooksByPackage(packageId: string): number;
37
+ find(object: string, query?: any, options?: any): Promise<any[]>;
38
+ update(object: string, data: any, options?: any): Promise<any>;
39
+ registry?: {
40
+ getObject(name: string): any;
41
+ getAllObjects?(packageId?: string): any[];
42
+ };
43
+ }
44
+
45
+ interface MinimalLogger {
46
+ info?: (msg: any, ...rest: any[]) => void;
47
+ warn?: (msg: any, ...rest: any[]) => void;
48
+ debug?: (msg: any, ...rest: any[]) => void;
49
+ }
50
+
51
+ /**
52
+ * Stamp `data.__search` on a before-save hook context when a companion source
53
+ * field is part of the write. Recomputes from the NEW value; a non-CJK new
54
+ * value clears the companion (null) so stale pinyin never recalls a renamed
55
+ * record. Never throws — a normalization failure must not fail the write.
56
+ */
57
+ async function stampCompanion(engine: MinimalEngine, ctx: any, logger?: MinimalLogger): Promise<void> {
58
+ const object = ctx?.object;
59
+ if (!object) return;
60
+ const schema = engine.registry?.getObject?.(object);
61
+ if (!schema?.fields?.[SEARCH_COMPANION_FIELD]) return;
62
+
63
+ const data = ctx?.input?.data;
64
+ if (!data || typeof data !== 'object' || Array.isArray(data)) return;
65
+
66
+ const sources = resolveSearchCompanionSources(schema);
67
+ if (sources.length === 0) return;
68
+ const touched = sources.filter((s) => Object.prototype.hasOwnProperty.call(data, s));
69
+ if (touched.length === 0) return; // source unchanged → no recompute (no write amplification)
70
+
71
+ try {
72
+ data[SEARCH_COMPANION_FIELD] = await computeSearchCompanionValue(sources.map((s) => data[s]));
73
+ } catch (err: any) {
74
+ logger?.warn?.('[pinyin-search] companion normalization failed — write proceeds without it', {
75
+ object,
76
+ error: err?.message,
77
+ });
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Bind the global before-save hooks that keep `__search` in step. Idempotent
83
+ * (unbinds the package first). Hooks are global (no object filter) with a
84
+ * cheap early-out: objects without a provisioned companion column return
85
+ * immediately. They run for system-context writes too — the projection must
86
+ * stay correct regardless of who writes (seeds, imports, admin UI).
87
+ */
88
+ export function bindSearchCompanionHooks(engine: MinimalEngine, logger?: MinimalLogger): void {
89
+ if (typeof engine.registerHook !== 'function') return;
90
+ if (typeof engine.unregisterHooksByPackage === 'function') {
91
+ engine.unregisterHooksByPackage(PINYIN_SEARCH_HOOK_PACKAGE);
92
+ }
93
+ const opts = { packageId: PINYIN_SEARCH_HOOK_PACKAGE, priority: 150 };
94
+ const handler = (ctx: any) => stampCompanion(engine, ctx, logger);
95
+ engine.registerHook('beforeInsert', handler, opts);
96
+ engine.registerHook('beforeUpdate', handler, opts);
97
+ logger?.info?.('[pinyin-search] companion hooks bound (beforeInsert/beforeUpdate, all objects)');
98
+ }
99
+
100
+ export interface CompanionBackfillOptions {
101
+ /** Rows fetched per page during the scan. Default 1000. */
102
+ batchSize?: number;
103
+ /** Restrict to one object (reconcile entry); default: every provisioned object. */
104
+ object?: string;
105
+ /**
106
+ * Recompute EVERY row's companion, not just missing ones — the periodic
107
+ * reconcile/rebuild mode. Default false (backfill: only rows whose
108
+ * companion is empty but whose source has CJK content).
109
+ */
110
+ force?: boolean;
111
+ }
112
+
113
+ export interface CompanionBackfillResult {
114
+ objects: number;
115
+ scanned: number;
116
+ updated: number;
117
+ }
118
+
119
+ /**
120
+ * Backfill / reconcile the companion column.
121
+ *
122
+ * Denormalized-on-write columns go stale when writes bypass hooks (bulk
123
+ * import, direct migration) and are empty for rows that predate the switch
124
+ * being enabled. This scans every object that carries the companion column
125
+ * (paged, system context) and fills the gaps; with `force: true` it
126
+ * recomputes unconditionally (the periodic reconcile / rebuild entry).
127
+ * Idempotent; per-row failures are skipped so one bad row never aborts the
128
+ * pass.
129
+ */
130
+ export async function backfillSearchCompanion(
131
+ engine: MinimalEngine,
132
+ logger?: MinimalLogger,
133
+ options?: CompanionBackfillOptions,
134
+ ): Promise<CompanionBackfillResult> {
135
+ const batchSize = Math.max(1, options?.batchSize ?? 1000);
136
+ const all = options?.object
137
+ ? [engine.registry?.getObject?.(options.object)].filter(Boolean)
138
+ : engine.registry?.getAllObjects?.() ?? [];
139
+
140
+ const result: CompanionBackfillResult = { objects: 0, scanned: 0, updated: 0 };
141
+
142
+ for (const schema of all) {
143
+ if (!schema?.name || !schema?.fields?.[SEARCH_COMPANION_FIELD]) continue;
144
+ const sources = resolveSearchCompanionSources(schema);
145
+ if (sources.length === 0) continue;
146
+ result.objects++;
147
+
148
+ let offset = 0;
149
+ for (;;) {
150
+ let rows: any[] = [];
151
+ try {
152
+ rows = await engine.find(schema.name, {
153
+ fields: ['id', ...sources, SEARCH_COMPANION_FIELD],
154
+ limit: batchSize,
155
+ offset,
156
+ context: SYSTEM_CTX,
157
+ });
158
+ } catch (err: any) {
159
+ logger?.warn?.('[pinyin-search] backfill scan failed', { object: schema.name, error: err?.message });
160
+ break;
161
+ }
162
+ if (!rows?.length) break;
163
+ result.scanned += rows.length;
164
+
165
+ for (const row of rows) {
166
+ if (row?.id == null) continue;
167
+ const hasBlob = typeof row[SEARCH_COMPANION_FIELD] === 'string' && row[SEARCH_COMPANION_FIELD] !== '';
168
+ const hasCjkSource = sources.some((s) => containsCJK(row[s]));
169
+ // Backfill mode: touch only rows missing a blob they should have.
170
+ // Force mode: recompute everything (also clears stale blobs).
171
+ if (!options?.force && (hasBlob || !hasCjkSource)) continue;
172
+ try {
173
+ const value = await computeSearchCompanionValue(sources.map((s) => row[s]));
174
+ if (!options?.force && value == null) continue;
175
+ if (value === row[SEARCH_COMPANION_FIELD]) continue;
176
+ await engine.update(
177
+ schema.name,
178
+ { id: row.id, [SEARCH_COMPANION_FIELD]: value },
179
+ { context: SYSTEM_CTX },
180
+ );
181
+ result.updated++;
182
+ } catch (err: any) {
183
+ logger?.warn?.('[pinyin-search] backfill row skipped', {
184
+ object: schema.name,
185
+ id: row.id,
186
+ error: err?.message,
187
+ });
188
+ }
189
+ }
190
+
191
+ if (rows.length < batchSize) break;
192
+ offset += batchSize;
193
+ }
194
+ }
195
+
196
+ if (result.updated > 0) {
197
+ logger?.info?.('[pinyin-search] companion backfill complete', result);
198
+ }
199
+ return result;
200
+ }
201
+
202
+ /**
203
+ * Periodic reconcile / rebuild entry: recompute the companion for every row
204
+ * (optionally one object). Alias for `backfillSearchCompanion` with
205
+ * `force: true` — exposed under its own name so operators/jobs have an
206
+ * explicit "rebuild the pinyin index" handle.
207
+ */
208
+ export function rebuildSearchCompanion(
209
+ engine: MinimalEngine,
210
+ logger?: MinimalLogger,
211
+ options?: Omit<CompanionBackfillOptions, 'force'>,
212
+ ): Promise<CompanionBackfillResult> {
213
+ return backfillSearchCompanion(engine, logger, { ...options, force: true });
214
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ export { PinyinSearchPlugin } from './pinyin-search-plugin.js';
4
+ export type { PinyinSearchPluginOptions } from './pinyin-search-plugin.js';
5
+ export {
6
+ bindSearchCompanionHooks,
7
+ backfillSearchCompanion,
8
+ rebuildSearchCompanion,
9
+ PINYIN_SEARCH_HOOK_PACKAGE,
10
+ } from './companion-projection.js';
11
+ export type {
12
+ CompanionBackfillOptions,
13
+ CompanionBackfillResult,
14
+ } from './companion-projection.js';
15
+ export { computeSearchCompanionValue } from './pinyin.js';
@@ -0,0 +1,105 @@
1
+ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ /**
4
+ * PinyinSearchPlugin (#2486) — pinyin recall for `$search`.
5
+ *
6
+ * Pure hook plugin: the `__search` companion column is declared at object
7
+ * compile time by the SchemaRegistry (gated on the SAME
8
+ * `OS_SEARCH_PINYIN_ENABLED` decision point), the engine ORs it into the
9
+ * `$search` filter, and this plugin fills the value:
10
+ *
11
+ * - before-save hooks: recompute full pinyin + initials of the
12
+ * display/name field when it changes;
13
+ * - boot backfill (`kernel:bootstrapped`): fill rows that predate the
14
+ * switch or arrived via hook-bypassing writes;
15
+ * - `rebuildSearchCompanion` (exported): explicit reconcile/rebuild entry.
16
+ *
17
+ * When the flag is off the plugin is inert: no hooks, no backfill, and
18
+ * `pinyin-pro` is never imported.
19
+ */
20
+
21
+ import type { Plugin, PluginContext } from '@objectstack/core';
22
+ import { resolveSearchPinyinEnabled } from '@objectstack/types';
23
+ import {
24
+ bindSearchCompanionHooks,
25
+ backfillSearchCompanion,
26
+ } from './companion-projection.js';
27
+
28
+ export interface PinyinSearchPluginOptions {
29
+ /**
30
+ * Force-enable/disable regardless of `OS_SEARCH_PINYIN_ENABLED` (tests /
31
+ * embedders). Default: `resolveSearchPinyinEnabled()`.
32
+ */
33
+ enabled?: boolean;
34
+ /** Skip the boot backfill (default: run it once per boot). */
35
+ backfill?: boolean;
36
+ }
37
+
38
+ export class PinyinSearchPlugin implements Plugin {
39
+ name = 'com.objectstack.plugin.pinyin-search';
40
+ version = '1.0.0';
41
+ type = 'standard';
42
+ dependencies = ['com.objectstack.engine.objectql'];
43
+
44
+ private readonly options: PinyinSearchPluginOptions;
45
+
46
+ constructor(options: PinyinSearchPluginOptions = {}) {
47
+ this.options = options;
48
+ }
49
+
50
+ private get enabled(): boolean {
51
+ return this.options.enabled ?? resolveSearchPinyinEnabled();
52
+ }
53
+
54
+ async init(_ctx: PluginContext): Promise<void> {
55
+ // Nothing to register: the companion column is provisioned by the
56
+ // SchemaRegistry's compile-time seam, not injected at runtime.
57
+ }
58
+
59
+ async start(ctx: PluginContext): Promise<void> {
60
+ if (!this.enabled) {
61
+ ctx.logger.debug?.('PinyinSearchPlugin: OS_SEARCH_PINYIN_ENABLED is off — inert');
62
+ return;
63
+ }
64
+
65
+ ctx.hook('kernel:ready', async () => {
66
+ const engine = this.resolveEngine(ctx);
67
+ if (!engine) {
68
+ ctx.logger.warn('PinyinSearchPlugin: no ObjectQL engine — companion hooks NOT bound');
69
+ return;
70
+ }
71
+ try {
72
+ bindSearchCompanionHooks(engine, ctx.logger as any);
73
+ } catch (err: any) {
74
+ ctx.logger.warn('PinyinSearchPlugin: companion hooks not bound', { error: err?.message });
75
+ }
76
+ });
77
+
78
+ // Backfill AFTER boot settles (`kernel:bootstrapped` fires once every
79
+ // `kernel:ready` hook — including seed loading — has completed), so
80
+ // seeded rows written before/around hook binding are reconciled too.
81
+ if (this.options.backfill !== false) {
82
+ ctx.hook('kernel:bootstrapped', async () => {
83
+ const engine = this.resolveEngine(ctx);
84
+ if (!engine) return;
85
+ try {
86
+ await backfillSearchCompanion(engine, ctx.logger as any);
87
+ } catch (err: any) {
88
+ ctx.logger.warn('PinyinSearchPlugin: companion backfill failed', { error: err?.message });
89
+ }
90
+ });
91
+ }
92
+ }
93
+
94
+ private resolveEngine(ctx: PluginContext): any {
95
+ try {
96
+ return ctx.getService<any>('objectql');
97
+ } catch {
98
+ try {
99
+ return ctx.getService<any>('data');
100
+ } catch {
101
+ return null;
102
+ }
103
+ }
104
+ }
105
+ }
@@ -0,0 +1,39 @@
1
+ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ import { describe, it, expect } from 'vitest';
4
+ import { computeSearchCompanionValue } from './pinyin.js';
5
+
6
+ describe('computeSearchCompanionValue (#2486)', () => {
7
+ it('stores full pinyin + initials in one blob ("张伟" → "zhangwei zw")', async () => {
8
+ expect(await computeSearchCompanionValue(['张伟'])).toBe('zhangwei zw');
9
+ });
10
+
11
+ it('recalls every documented input shape as a substring of the blob', async () => {
12
+ const blob = (await computeSearchCompanionValue(['张伟']))!;
13
+ for (const typed of ['zhang', 'wei', 'zhangwei', 'zw']) {
14
+ expect(blob.includes(typed)).toBe(true);
15
+ }
16
+ });
17
+
18
+ it('handles multi-word and mixed CJK/latin values', async () => {
19
+ const blob = (await computeSearchCompanionValue(['上海分公司']))!;
20
+ expect(blob).toBe('shanghaifengongsi shfgs');
21
+
22
+ const mixed = (await computeSearchCompanionValue(['张伟2号']))!;
23
+ expect(mixed.includes('zhangwei2hao')).toBe(true);
24
+ });
25
+
26
+ it('returns null for non-CJK / empty / non-string values (companion cleared)', async () => {
27
+ expect(await computeSearchCompanionValue(['Zhang Wei'])).toBe(null);
28
+ expect(await computeSearchCompanionValue([''])).toBe(null);
29
+ expect(await computeSearchCompanionValue([null, undefined, 42])).toBe(null);
30
+ expect(await computeSearchCompanionValue([])).toBe(null);
31
+ });
32
+
33
+ it('deduplicates when initials equal the full form (single-char name)', async () => {
34
+ const blob = (await computeSearchCompanionValue(['张']))!;
35
+ expect(blob).toBe('zhang z');
36
+ // no duplicated tokens
37
+ expect(new Set(blob.split(' ')).size).toBe(blob.split(' ').length);
38
+ });
39
+ });
package/src/pinyin.ts ADDED
@@ -0,0 +1,63 @@
1
+ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ /**
4
+ * Pinyin normalization for the `__search` companion column (#2486).
5
+ *
6
+ * One normalized blob per record — full pinyin AND initials in the same
7
+ * column — so a single `$contains` recalls every latin input shape:
8
+ *
9
+ * "张伟" → "zhangwei zw"
10
+ * `zhang` / `wei` / `zhangwei` → substring of the full form
11
+ * `zw` → substring of the initials form
12
+ *
13
+ * `pinyin-pro` is loaded lazily on first use: non-Chinese deployments (flag
14
+ * off → hooks never bound) never import it and pay zero cost.
15
+ *
16
+ * Polyphones: pinyin-pro's default heuristics are accepted (issue #2486
17
+ * "待定" — surname polyphone dictionaries are a P2 follow-up).
18
+ */
19
+
20
+ import { containsCJK } from '@objectstack/objectql';
21
+
22
+ type PinyinFn = (text: string, options?: Record<string, unknown>) => string | string[];
23
+
24
+ let _pinyin: Promise<PinyinFn> | null = null;
25
+
26
+ /** Lazy-load `pinyin-pro` (cached module-wide). */
27
+ function loadPinyin(): Promise<PinyinFn> {
28
+ _pinyin ??= import('pinyin-pro').then((m: any) => (m.pinyin ?? m.default?.pinyin) as PinyinFn);
29
+ return _pinyin;
30
+ }
31
+
32
+ /** Lowercase and strip everything that is not a latin letter or digit. */
33
+ function squash(syllables: string | string[]): string {
34
+ const joined = Array.isArray(syllables) ? syllables.join('') : String(syllables ?? '');
35
+ return joined.toLowerCase().replace(/[^a-z0-9]+/g, '');
36
+ }
37
+
38
+ /**
39
+ * Compute the companion value for the given source-field values.
40
+ *
41
+ * Returns the normalized blob (`"<full-pinyin> <initials>"`, deduplicated)
42
+ * when at least one value contains CJK characters, else `null` — a `null`
43
+ * companion means "nothing pinyin-searchable here" and clears any stale blob
44
+ * when a name is edited away from CJK. Non-CJK values need no companion:
45
+ * their source column already matches latin input directly.
46
+ */
47
+ export async function computeSearchCompanionValue(values: ReadonlyArray<unknown>): Promise<string | null> {
48
+ const cjkValues = values.filter((v): v is string => containsCJK(v));
49
+ if (cjkValues.length === 0) return null;
50
+
51
+ const pinyin = await loadPinyin();
52
+ const parts: string[] = [];
53
+ for (const value of cjkValues) {
54
+ // `nonZh: 'consecutive'` keeps latin/digit runs intact inside mixed
55
+ // values ("张伟2号" → "zhangwei2hao"), so mixed names stay one token.
56
+ const full = squash(pinyin(value, { toneType: 'none', type: 'array', nonZh: 'consecutive' }));
57
+ const initials = squash(pinyin(value, { pattern: 'first', toneType: 'none', type: 'array', nonZh: 'consecutive' }));
58
+ if (full) parts.push(full);
59
+ if (initials && initials !== full) parts.push(initials);
60
+ }
61
+ if (parts.length === 0) return null;
62
+ return [...new Set(parts)].join(' ');
63
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "types": ["node"]
7
+ },
8
+ "include": ["src/**/*"],
9
+ "exclude": ["dist", "node_modules", "**/*.test.ts"]
10
+ }