servcraft 0.1.0 → 0.1.3
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.
- package/.claude/settings.local.json +30 -0
- package/.github/CODEOWNERS +18 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +46 -0
- package/.github/dependabot.yml +59 -0
- package/.github/workflows/ci.yml +188 -0
- package/.github/workflows/release.yml +195 -0
- package/AUDIT.md +602 -0
- package/LICENSE +21 -0
- package/README.md +1102 -1
- package/dist/cli/index.cjs +2026 -2168
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +2026 -2168
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +595 -616
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +114 -52
- package/dist/index.d.ts +114 -52
- package/dist/index.js +595 -616
- package/dist/index.js.map +1 -1
- package/docs/CLI-001_MULTI_DB_PLAN.md +546 -0
- package/docs/DATABASE_MULTI_ORM.md +399 -0
- package/docs/PHASE1_BREAKDOWN.md +346 -0
- package/docs/PROGRESS.md +550 -0
- package/docs/modules/ANALYTICS.md +226 -0
- package/docs/modules/API-VERSIONING.md +252 -0
- package/docs/modules/AUDIT.md +192 -0
- package/docs/modules/AUTH.md +431 -0
- package/docs/modules/CACHE.md +346 -0
- package/docs/modules/EMAIL.md +254 -0
- package/docs/modules/FEATURE-FLAG.md +291 -0
- package/docs/modules/I18N.md +294 -0
- package/docs/modules/MEDIA-PROCESSING.md +281 -0
- package/docs/modules/MFA.md +266 -0
- package/docs/modules/NOTIFICATION.md +311 -0
- package/docs/modules/OAUTH.md +237 -0
- package/docs/modules/PAYMENT.md +804 -0
- package/docs/modules/QUEUE.md +540 -0
- package/docs/modules/RATE-LIMIT.md +339 -0
- package/docs/modules/SEARCH.md +288 -0
- package/docs/modules/SECURITY.md +327 -0
- package/docs/modules/SESSION.md +382 -0
- package/docs/modules/SWAGGER.md +305 -0
- package/docs/modules/UPLOAD.md +296 -0
- package/docs/modules/USER.md +505 -0
- package/docs/modules/VALIDATION.md +294 -0
- package/docs/modules/WEBHOOK.md +270 -0
- package/docs/modules/WEBSOCKET.md +691 -0
- package/package.json +53 -38
- package/prisma/schema.prisma +395 -1
- package/src/cli/commands/add-module.ts +520 -87
- package/src/cli/commands/db.ts +3 -4
- package/src/cli/commands/docs.ts +256 -6
- package/src/cli/commands/generate.ts +12 -19
- package/src/cli/commands/init.ts +384 -214
- package/src/cli/index.ts +0 -4
- package/src/cli/templates/repository.ts +6 -1
- package/src/cli/templates/routes.ts +6 -21
- package/src/cli/utils/docs-generator.ts +6 -7
- package/src/cli/utils/env-manager.ts +717 -0
- package/src/cli/utils/field-parser.ts +16 -7
- package/src/cli/utils/interactive-prompt.ts +223 -0
- package/src/cli/utils/template-manager.ts +346 -0
- package/src/config/database.config.ts +183 -0
- package/src/config/env.ts +0 -10
- package/src/config/index.ts +0 -14
- package/src/core/server.ts +1 -1
- package/src/database/adapters/mongoose.adapter.ts +132 -0
- package/src/database/adapters/prisma.adapter.ts +118 -0
- package/src/database/connection.ts +190 -0
- package/src/database/interfaces/database.interface.ts +85 -0
- package/src/database/interfaces/index.ts +7 -0
- package/src/database/interfaces/repository.interface.ts +129 -0
- package/src/database/models/mongoose/index.ts +7 -0
- package/src/database/models/mongoose/payment.schema.ts +347 -0
- package/src/database/models/mongoose/user.schema.ts +154 -0
- package/src/database/prisma.ts +1 -4
- package/src/database/redis.ts +101 -0
- package/src/database/repositories/mongoose/index.ts +7 -0
- package/src/database/repositories/mongoose/payment.repository.ts +380 -0
- package/src/database/repositories/mongoose/user.repository.ts +255 -0
- package/src/database/seed.ts +6 -1
- package/src/index.ts +9 -20
- package/src/middleware/security.ts +2 -6
- package/src/modules/analytics/analytics.routes.ts +80 -0
- package/src/modules/analytics/analytics.service.ts +364 -0
- package/src/modules/analytics/index.ts +18 -0
- package/src/modules/analytics/types.ts +180 -0
- package/src/modules/api-versioning/index.ts +15 -0
- package/src/modules/api-versioning/types.ts +86 -0
- package/src/modules/api-versioning/versioning.middleware.ts +120 -0
- package/src/modules/api-versioning/versioning.routes.ts +54 -0
- package/src/modules/api-versioning/versioning.service.ts +189 -0
- package/src/modules/audit/audit.repository.ts +206 -0
- package/src/modules/audit/audit.service.ts +27 -59
- package/src/modules/auth/auth.controller.ts +2 -2
- package/src/modules/auth/auth.middleware.ts +3 -9
- package/src/modules/auth/auth.routes.ts +10 -107
- package/src/modules/auth/auth.service.ts +126 -23
- package/src/modules/auth/index.ts +3 -4
- package/src/modules/cache/cache.service.ts +367 -0
- package/src/modules/cache/index.ts +10 -0
- package/src/modules/cache/types.ts +44 -0
- package/src/modules/email/email.service.ts +3 -10
- package/src/modules/email/templates.ts +2 -8
- package/src/modules/feature-flag/feature-flag.repository.ts +303 -0
- package/src/modules/feature-flag/feature-flag.routes.ts +247 -0
- package/src/modules/feature-flag/feature-flag.service.ts +566 -0
- package/src/modules/feature-flag/index.ts +20 -0
- package/src/modules/feature-flag/types.ts +192 -0
- package/src/modules/i18n/i18n.middleware.ts +186 -0
- package/src/modules/i18n/i18n.routes.ts +191 -0
- package/src/modules/i18n/i18n.service.ts +456 -0
- package/src/modules/i18n/index.ts +18 -0
- package/src/modules/i18n/types.ts +118 -0
- package/src/modules/media-processing/index.ts +17 -0
- package/src/modules/media-processing/media-processing.routes.ts +111 -0
- package/src/modules/media-processing/media-processing.service.ts +245 -0
- package/src/modules/media-processing/types.ts +156 -0
- package/src/modules/mfa/index.ts +20 -0
- package/src/modules/mfa/mfa.repository.ts +206 -0
- package/src/modules/mfa/mfa.routes.ts +595 -0
- package/src/modules/mfa/mfa.service.ts +572 -0
- package/src/modules/mfa/totp.ts +150 -0
- package/src/modules/mfa/types.ts +57 -0
- package/src/modules/notification/index.ts +20 -0
- package/src/modules/notification/notification.repository.ts +356 -0
- package/src/modules/notification/notification.service.ts +483 -0
- package/src/modules/notification/types.ts +119 -0
- package/src/modules/oauth/index.ts +20 -0
- package/src/modules/oauth/oauth.repository.ts +219 -0
- package/src/modules/oauth/oauth.routes.ts +446 -0
- package/src/modules/oauth/oauth.service.ts +293 -0
- package/src/modules/oauth/providers/apple.provider.ts +250 -0
- package/src/modules/oauth/providers/facebook.provider.ts +181 -0
- package/src/modules/oauth/providers/github.provider.ts +248 -0
- package/src/modules/oauth/providers/google.provider.ts +189 -0
- package/src/modules/oauth/providers/twitter.provider.ts +214 -0
- package/src/modules/oauth/types.ts +94 -0
- package/src/modules/payment/index.ts +19 -0
- package/src/modules/payment/payment.repository.ts +733 -0
- package/src/modules/payment/payment.routes.ts +390 -0
- package/src/modules/payment/payment.service.ts +354 -0
- package/src/modules/payment/providers/mobile-money.provider.ts +274 -0
- package/src/modules/payment/providers/paypal.provider.ts +190 -0
- package/src/modules/payment/providers/stripe.provider.ts +215 -0
- package/src/modules/payment/types.ts +140 -0
- package/src/modules/queue/cron.ts +438 -0
- package/src/modules/queue/index.ts +87 -0
- package/src/modules/queue/queue.routes.ts +600 -0
- package/src/modules/queue/queue.service.ts +842 -0
- package/src/modules/queue/types.ts +222 -0
- package/src/modules/queue/workers.ts +366 -0
- package/src/modules/rate-limit/index.ts +59 -0
- package/src/modules/rate-limit/rate-limit.middleware.ts +134 -0
- package/src/modules/rate-limit/rate-limit.routes.ts +269 -0
- package/src/modules/rate-limit/rate-limit.service.ts +348 -0
- package/src/modules/rate-limit/stores/memory.store.ts +165 -0
- package/src/modules/rate-limit/stores/redis.store.ts +322 -0
- package/src/modules/rate-limit/types.ts +153 -0
- package/src/modules/search/adapters/elasticsearch.adapter.ts +326 -0
- package/src/modules/search/adapters/meilisearch.adapter.ts +261 -0
- package/src/modules/search/adapters/memory.adapter.ts +278 -0
- package/src/modules/search/index.ts +21 -0
- package/src/modules/search/search.service.ts +234 -0
- package/src/modules/search/types.ts +214 -0
- package/src/modules/security/index.ts +40 -0
- package/src/modules/security/sanitize.ts +223 -0
- package/src/modules/security/security-audit.service.ts +388 -0
- package/src/modules/security/security.middleware.ts +398 -0
- package/src/modules/session/index.ts +3 -0
- package/src/modules/session/session.repository.ts +159 -0
- package/src/modules/session/session.service.ts +340 -0
- package/src/modules/session/types.ts +38 -0
- package/src/modules/swagger/index.ts +7 -1
- package/src/modules/swagger/schema-builder.ts +16 -4
- package/src/modules/swagger/swagger.service.ts +9 -10
- package/src/modules/swagger/types.ts +0 -2
- package/src/modules/upload/index.ts +14 -0
- package/src/modules/upload/types.ts +83 -0
- package/src/modules/upload/upload.repository.ts +199 -0
- package/src/modules/upload/upload.routes.ts +311 -0
- package/src/modules/upload/upload.service.ts +448 -0
- package/src/modules/user/index.ts +3 -3
- package/src/modules/user/user.controller.ts +15 -9
- package/src/modules/user/user.repository.ts +237 -113
- package/src/modules/user/user.routes.ts +39 -164
- package/src/modules/user/user.service.ts +4 -3
- package/src/modules/validation/validator.ts +12 -17
- package/src/modules/webhook/index.ts +91 -0
- package/src/modules/webhook/retry.ts +196 -0
- package/src/modules/webhook/signature.ts +135 -0
- package/src/modules/webhook/types.ts +181 -0
- package/src/modules/webhook/webhook.repository.ts +358 -0
- package/src/modules/webhook/webhook.routes.ts +442 -0
- package/src/modules/webhook/webhook.service.ts +457 -0
- package/src/modules/websocket/features.ts +504 -0
- package/src/modules/websocket/index.ts +106 -0
- package/src/modules/websocket/middlewares.ts +298 -0
- package/src/modules/websocket/types.ts +181 -0
- package/src/modules/websocket/websocket.service.ts +692 -0
- package/src/utils/errors.ts +7 -0
- package/src/utils/pagination.ts +4 -1
- package/tests/helpers/db-check.ts +79 -0
- package/tests/integration/auth-redis.test.ts +94 -0
- package/tests/integration/cache-redis.test.ts +387 -0
- package/tests/integration/mongoose-repositories.test.ts +410 -0
- package/tests/integration/payment-prisma.test.ts +637 -0
- package/tests/integration/queue-bullmq.test.ts +417 -0
- package/tests/integration/user-prisma.test.ts +441 -0
- package/tests/integration/websocket-socketio.test.ts +552 -0
- package/tests/setup.ts +11 -9
- package/vitest.config.ts +3 -8
- package/npm-cache/_cacache/content-v2/sha512/1c/d0/03440d500a0487621aad1d6402978340698976602046db8e24fa03c01ee6c022c69b0582f969042d9442ee876ac35c038e960dd427d1e622fa24b8eb7dba +0 -0
- package/npm-cache/_cacache/content-v2/sha512/42/55/28b493ca491833e5aab0e9c3108d29ab3f36c248ca88f45d4630674fce9130959e56ae308797ac2b6328fa7f09a610b9550ed09cb971d039876d293fc69d +0 -0
- package/npm-cache/_cacache/content-v2/sha512/e0/12/f360dc9315ee5f17844a0c8c233ee6bf7c30837c4a02ea0d56c61c7f7ab21c0e958e50ed2c57c59f983c762b93056778c9009b2398ffc26def0183999b13 +0 -0
- package/npm-cache/_cacache/content-v2/sha512/ed/b0/fae1161902898f4c913c67d7f6cdf6be0665aec3b389b9c4f4f0a101ca1da59badf1b59c4e0030f5223023b8d63cfe501c46a32c20c895d4fb3f11ca2232 +0 -0
- package/npm-cache/_cacache/index-v5/58/94/c2cba79e0f16b4c10e95a87e32255741149e8222cc314a476aab67c39cc0 +0 -5
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SearchEngine,
|
|
3
|
+
SearchDocument,
|
|
4
|
+
SearchQuery,
|
|
5
|
+
SearchResult,
|
|
6
|
+
IndexSettings,
|
|
7
|
+
BulkIndexOperation,
|
|
8
|
+
BulkIndexResult,
|
|
9
|
+
IndexStats,
|
|
10
|
+
AutocompleteResult,
|
|
11
|
+
} from '../types.js';
|
|
12
|
+
|
|
13
|
+
interface ElasticsearchClient {
|
|
14
|
+
index: (params: unknown) => Promise<unknown>;
|
|
15
|
+
bulk: (params: unknown) => Promise<unknown>;
|
|
16
|
+
search: (params: unknown) => Promise<unknown>;
|
|
17
|
+
delete: (params: unknown) => Promise<unknown>;
|
|
18
|
+
update: (params: unknown) => Promise<unknown>;
|
|
19
|
+
get: (params: unknown) => Promise<unknown>;
|
|
20
|
+
indices: {
|
|
21
|
+
create: (params: unknown) => Promise<unknown>;
|
|
22
|
+
delete: (params: unknown) => Promise<unknown>;
|
|
23
|
+
putSettings: (params: unknown) => Promise<unknown>;
|
|
24
|
+
stats: (params: unknown) => Promise<unknown>;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Elasticsearch Adapter
|
|
30
|
+
* Production-ready search with Elasticsearch
|
|
31
|
+
*/
|
|
32
|
+
export class ElasticsearchAdapter implements SearchEngine {
|
|
33
|
+
private client: ElasticsearchClient;
|
|
34
|
+
|
|
35
|
+
constructor(client: ElasticsearchClient) {
|
|
36
|
+
this.client = client;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async createIndex(indexName: string, settings?: IndexSettings): Promise<void> {
|
|
40
|
+
const mappings: Record<string, unknown> = {};
|
|
41
|
+
|
|
42
|
+
if (settings?.searchableAttributes || settings?.filterableAttributes) {
|
|
43
|
+
mappings.properties = {};
|
|
44
|
+
|
|
45
|
+
(settings.searchableAttributes || []).forEach((attr) => {
|
|
46
|
+
(mappings.properties as Record<string, unknown>)[attr] = { type: 'text' };
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
(settings.filterableAttributes || []).forEach((attr) => {
|
|
50
|
+
(mappings.properties as Record<string, unknown>)[attr] = { type: 'keyword' };
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
await this.client.indices.create({
|
|
55
|
+
index: indexName,
|
|
56
|
+
body: {
|
|
57
|
+
settings: {
|
|
58
|
+
number_of_shards: settings?.numberOfShards || 1,
|
|
59
|
+
number_of_replicas: settings?.numberOfReplicas || 1,
|
|
60
|
+
},
|
|
61
|
+
mappings,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async deleteIndex(indexName: string): Promise<void> {
|
|
67
|
+
await this.client.indices.delete({ index: indexName });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async index(indexName: string, id: string, document: SearchDocument): Promise<void> {
|
|
71
|
+
await this.client.index({
|
|
72
|
+
index: indexName,
|
|
73
|
+
id,
|
|
74
|
+
body: document,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async bulkIndex(indexName: string, operations: BulkIndexOperation[]): Promise<BulkIndexResult> {
|
|
79
|
+
const startTime = Date.now();
|
|
80
|
+
const body: unknown[] = [];
|
|
81
|
+
|
|
82
|
+
for (const op of operations) {
|
|
83
|
+
if (op.operation === 'index') {
|
|
84
|
+
body.push({ index: { _index: indexName, _id: op.id } });
|
|
85
|
+
body.push(op.document);
|
|
86
|
+
} else if (op.operation === 'update') {
|
|
87
|
+
body.push({ update: { _index: indexName, _id: op.id } });
|
|
88
|
+
body.push({ doc: op.document });
|
|
89
|
+
} else if (op.operation === 'delete') {
|
|
90
|
+
body.push({ delete: { _index: indexName, _id: op.id } });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const result = (await this.client.bulk({ body })) as {
|
|
95
|
+
items: Array<Record<string, { error?: unknown }>>;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
let success = 0;
|
|
99
|
+
let failed = 0;
|
|
100
|
+
const errors: Array<{ id: string; error: string }> = [];
|
|
101
|
+
|
|
102
|
+
result.items.forEach((item, idx) => {
|
|
103
|
+
const operation = Object.values(item)[0];
|
|
104
|
+
if (operation && operation.error) {
|
|
105
|
+
failed++;
|
|
106
|
+
errors.push({
|
|
107
|
+
id: operations[idx]?.id ?? 'unknown',
|
|
108
|
+
error: JSON.stringify(operation.error),
|
|
109
|
+
});
|
|
110
|
+
} else {
|
|
111
|
+
success++;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
success,
|
|
117
|
+
failed,
|
|
118
|
+
errors: errors.length > 0 ? errors : undefined,
|
|
119
|
+
processingTime: Date.now() - startTime,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async search<T = SearchDocument>(
|
|
124
|
+
indexName: string,
|
|
125
|
+
query: SearchQuery
|
|
126
|
+
): Promise<SearchResult<T>> {
|
|
127
|
+
const body: Record<string, unknown> = {
|
|
128
|
+
query: {
|
|
129
|
+
multi_match: {
|
|
130
|
+
query: query.query,
|
|
131
|
+
fields: query.fields || ['*'],
|
|
132
|
+
fuzziness: query.fuzziness || 'AUTO',
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
from: query.offset || 0,
|
|
136
|
+
size: query.limit || 20,
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
if (query.sort) {
|
|
140
|
+
body.sort = query.sort.map((s) => ({
|
|
141
|
+
[s.field]: { order: s.direction },
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (query.filters) {
|
|
146
|
+
const must: unknown[] = [];
|
|
147
|
+
|
|
148
|
+
for (const filter of query.filters) {
|
|
149
|
+
switch (filter.operator) {
|
|
150
|
+
case 'eq':
|
|
151
|
+
must.push({ term: { [filter.field]: filter.value } });
|
|
152
|
+
break;
|
|
153
|
+
case 'in':
|
|
154
|
+
must.push({ terms: { [filter.field]: filter.value } });
|
|
155
|
+
break;
|
|
156
|
+
case 'exists':
|
|
157
|
+
must.push({ exists: { field: filter.field } });
|
|
158
|
+
break;
|
|
159
|
+
case 'gt':
|
|
160
|
+
case 'gte':
|
|
161
|
+
case 'lt':
|
|
162
|
+
case 'lte':
|
|
163
|
+
must.push({
|
|
164
|
+
range: { [filter.field]: { [filter.operator]: filter.value } },
|
|
165
|
+
});
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
body.query = {
|
|
171
|
+
bool: {
|
|
172
|
+
must: [body.query, ...must],
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (query.highlight) {
|
|
178
|
+
body.highlight = {
|
|
179
|
+
fields: query.fields ? Object.fromEntries(query.fields.map((f) => [f, {}])) : { '*': {} },
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const result = (await this.client.search({
|
|
184
|
+
index: indexName,
|
|
185
|
+
body,
|
|
186
|
+
})) as {
|
|
187
|
+
hits: {
|
|
188
|
+
total: { value: number };
|
|
189
|
+
hits: Array<{
|
|
190
|
+
_source: T;
|
|
191
|
+
_score: number;
|
|
192
|
+
highlight?: Record<string, string[]>;
|
|
193
|
+
}>;
|
|
194
|
+
};
|
|
195
|
+
took: number;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const hits = result.hits.hits.map((hit) => ({
|
|
199
|
+
document: hit._source,
|
|
200
|
+
score: hit._score,
|
|
201
|
+
highlights: hit.highlight,
|
|
202
|
+
}));
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
hits,
|
|
206
|
+
total: result.hits.total.value,
|
|
207
|
+
processingTime: result.took,
|
|
208
|
+
page: Math.floor((query.offset || 0) / (query.limit || 20)) + 1,
|
|
209
|
+
totalPages: Math.ceil(result.hits.total.value / (query.limit || 20)),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async delete(indexName: string, id: string): Promise<void> {
|
|
214
|
+
await this.client.delete({
|
|
215
|
+
index: indexName,
|
|
216
|
+
id,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async update(indexName: string, id: string, document: Partial<SearchDocument>): Promise<void> {
|
|
221
|
+
await this.client.update({
|
|
222
|
+
index: indexName,
|
|
223
|
+
id,
|
|
224
|
+
body: {
|
|
225
|
+
doc: document,
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async get(indexName: string, id: string): Promise<SearchDocument | null> {
|
|
231
|
+
try {
|
|
232
|
+
const result = (await this.client.get({
|
|
233
|
+
index: indexName,
|
|
234
|
+
id,
|
|
235
|
+
})) as { _source: SearchDocument };
|
|
236
|
+
|
|
237
|
+
return result._source;
|
|
238
|
+
} catch {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async updateSettings(indexName: string, settings: IndexSettings): Promise<void> {
|
|
244
|
+
const esSettings: Record<string, unknown> = {};
|
|
245
|
+
|
|
246
|
+
if (settings.stopWords) {
|
|
247
|
+
esSettings.analysis = {
|
|
248
|
+
filter: {
|
|
249
|
+
custom_stop: {
|
|
250
|
+
type: 'stop',
|
|
251
|
+
stopwords: settings.stopWords,
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
await this.client.indices.putSettings({
|
|
258
|
+
index: indexName,
|
|
259
|
+
body: esSettings,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async getStats(indexName: string): Promise<IndexStats> {
|
|
264
|
+
const stats = (await this.client.indices.stats({
|
|
265
|
+
index: indexName,
|
|
266
|
+
})) as {
|
|
267
|
+
indices: Record<
|
|
268
|
+
string,
|
|
269
|
+
{
|
|
270
|
+
total: {
|
|
271
|
+
docs: { count: number };
|
|
272
|
+
store: { size_in_bytes: number };
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
>;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const indexStats = stats.indices[indexName];
|
|
279
|
+
|
|
280
|
+
if (!indexStats) {
|
|
281
|
+
throw new Error(`Index ${indexName} not found`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
name: indexName,
|
|
286
|
+
documentCount: indexStats.total.docs.count,
|
|
287
|
+
size: indexStats.total.store.size_in_bytes,
|
|
288
|
+
isIndexing: false,
|
|
289
|
+
health: 'green',
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async autocomplete(indexName: string, query: string, limit = 10): Promise<AutocompleteResult> {
|
|
294
|
+
const startTime = Date.now();
|
|
295
|
+
|
|
296
|
+
const result = (await this.client.search({
|
|
297
|
+
index: indexName,
|
|
298
|
+
body: {
|
|
299
|
+
suggest: {
|
|
300
|
+
autocomplete: {
|
|
301
|
+
prefix: query,
|
|
302
|
+
completion: {
|
|
303
|
+
field: 'suggest',
|
|
304
|
+
size: limit,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
})) as {
|
|
310
|
+
suggest: {
|
|
311
|
+
autocomplete: Array<{
|
|
312
|
+
options: Array<{ text: string; _source: SearchDocument }>;
|
|
313
|
+
}>;
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const suggestions = result.suggest.autocomplete[0]?.options.map((opt) => opt.text) || [];
|
|
318
|
+
const documents = result.suggest.autocomplete[0]?.options.map((opt) => opt._source) || [];
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
suggestions,
|
|
322
|
+
documents,
|
|
323
|
+
processingTime: Date.now() - startTime,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SearchEngine,
|
|
3
|
+
SearchDocument,
|
|
4
|
+
SearchQuery,
|
|
5
|
+
SearchResult,
|
|
6
|
+
IndexSettings,
|
|
7
|
+
BulkIndexOperation,
|
|
8
|
+
BulkIndexResult,
|
|
9
|
+
IndexStats,
|
|
10
|
+
AutocompleteResult,
|
|
11
|
+
} from '../types.js';
|
|
12
|
+
|
|
13
|
+
interface MeilisearchClient {
|
|
14
|
+
index: (indexName: string) => MeilisearchIndex;
|
|
15
|
+
getIndex: (indexName: string) => Promise<MeilisearchIndex>;
|
|
16
|
+
createIndex: (indexName: string, options?: { primaryKey?: string }) => Promise<unknown>;
|
|
17
|
+
deleteIndex: (indexName: string) => Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface MeilisearchIndex {
|
|
21
|
+
addDocuments: (documents: unknown[]) => Promise<{ taskUid: number }>;
|
|
22
|
+
updateDocuments: (documents: unknown[]) => Promise<{ taskUid: number }>;
|
|
23
|
+
deleteDocument: (id: string) => Promise<{ taskUid: number }>;
|
|
24
|
+
deleteDocuments: (ids: string[]) => Promise<{ taskUid: number }>;
|
|
25
|
+
getDocument: (id: string) => Promise<unknown>;
|
|
26
|
+
search: (query: string, options?: unknown) => Promise<unknown>;
|
|
27
|
+
updateSettings: (settings: unknown) => Promise<{ taskUid: number }>;
|
|
28
|
+
getSettings: () => Promise<unknown>;
|
|
29
|
+
getStats: () => Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Meilisearch Adapter
|
|
34
|
+
* Production-ready search with Meilisearch
|
|
35
|
+
*/
|
|
36
|
+
export class MeilisearchAdapter implements SearchEngine {
|
|
37
|
+
private client: MeilisearchClient;
|
|
38
|
+
|
|
39
|
+
constructor(client: MeilisearchClient) {
|
|
40
|
+
this.client = client;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async createIndex(indexName: string, settings?: IndexSettings): Promise<void> {
|
|
44
|
+
await this.client.createIndex(indexName);
|
|
45
|
+
|
|
46
|
+
if (settings) {
|
|
47
|
+
await this.updateSettings(indexName, settings);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async deleteIndex(indexName: string): Promise<void> {
|
|
52
|
+
await this.client.deleteIndex(indexName);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async index(indexName: string, id: string, document: SearchDocument): Promise<void> {
|
|
56
|
+
const index = this.client.index(indexName);
|
|
57
|
+
await index.addDocuments([{ ...document, id }]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async bulkIndex(indexName: string, operations: BulkIndexOperation[]): Promise<BulkIndexResult> {
|
|
61
|
+
const startTime = Date.now();
|
|
62
|
+
const index = this.client.index(indexName);
|
|
63
|
+
|
|
64
|
+
const toIndex = operations
|
|
65
|
+
.filter((op) => op.operation === 'index' && op.document)
|
|
66
|
+
.map((op) => ({ ...op.document, id: op.id }));
|
|
67
|
+
|
|
68
|
+
const toUpdate = operations
|
|
69
|
+
.filter((op) => op.operation === 'update' && op.document)
|
|
70
|
+
.map((op) => ({ ...op.document, id: op.id }));
|
|
71
|
+
|
|
72
|
+
const toDelete = operations.filter((op) => op.operation === 'delete').map((op) => op.id);
|
|
73
|
+
|
|
74
|
+
let success = 0;
|
|
75
|
+
let failed = 0;
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
if (toIndex.length > 0) {
|
|
79
|
+
await index.addDocuments(toIndex);
|
|
80
|
+
success += toIndex.length;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (toUpdate.length > 0) {
|
|
84
|
+
await index.updateDocuments(toUpdate);
|
|
85
|
+
success += toUpdate.length;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (toDelete.length > 0) {
|
|
89
|
+
await index.deleteDocuments(toDelete);
|
|
90
|
+
success += toDelete.length;
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
93
|
+
failed = operations.length - success;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
success,
|
|
98
|
+
failed,
|
|
99
|
+
processingTime: Date.now() - startTime,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async search<T = SearchDocument>(
|
|
104
|
+
indexName: string,
|
|
105
|
+
query: SearchQuery
|
|
106
|
+
): Promise<SearchResult<T>> {
|
|
107
|
+
const index = this.client.index(indexName);
|
|
108
|
+
|
|
109
|
+
const options: Record<string, unknown> = {
|
|
110
|
+
limit: query.limit || 20,
|
|
111
|
+
offset: query.offset || 0,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (query.fields) {
|
|
115
|
+
options.attributesToSearchOn = query.fields;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (query.filters) {
|
|
119
|
+
const filterStrings = query.filters.map((f) => {
|
|
120
|
+
switch (f.operator) {
|
|
121
|
+
case 'eq':
|
|
122
|
+
return `${f.field} = ${JSON.stringify(f.value)}`;
|
|
123
|
+
case 'ne':
|
|
124
|
+
return `${f.field} != ${JSON.stringify(f.value)}`;
|
|
125
|
+
case 'gt':
|
|
126
|
+
return `${f.field} > ${f.value}`;
|
|
127
|
+
case 'gte':
|
|
128
|
+
return `${f.field} >= ${f.value}`;
|
|
129
|
+
case 'lt':
|
|
130
|
+
return `${f.field} < ${f.value}`;
|
|
131
|
+
case 'lte':
|
|
132
|
+
return `${f.field} <= ${f.value}`;
|
|
133
|
+
case 'in':
|
|
134
|
+
return `${f.field} IN ${JSON.stringify(f.value)}`;
|
|
135
|
+
case 'exists':
|
|
136
|
+
return `${f.field} EXISTS`;
|
|
137
|
+
default:
|
|
138
|
+
return '';
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
options.filter = filterStrings.filter((f) => f).join(' AND ');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (query.sort) {
|
|
146
|
+
options.sort = query.sort.map((s) => `${s.field}:${s.direction}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (query.facets) {
|
|
150
|
+
options.facets = query.facets;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const result = (await index.search(query.query, options)) as {
|
|
154
|
+
hits: T[];
|
|
155
|
+
estimatedTotalHits: number;
|
|
156
|
+
processingTimeMs: number;
|
|
157
|
+
facetDistribution?: Record<string, Record<string, number>>;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const hits = result.hits.map((doc) => ({
|
|
161
|
+
document: doc,
|
|
162
|
+
score: 1.0,
|
|
163
|
+
}));
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
hits,
|
|
167
|
+
total: result.estimatedTotalHits,
|
|
168
|
+
processingTime: result.processingTimeMs,
|
|
169
|
+
page: Math.floor((query.offset || 0) / (query.limit || 20)) + 1,
|
|
170
|
+
totalPages: Math.ceil(result.estimatedTotalHits / (query.limit || 20)),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async delete(indexName: string, id: string): Promise<void> {
|
|
175
|
+
const index = this.client.index(indexName);
|
|
176
|
+
await index.deleteDocument(id);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async update(indexName: string, id: string, document: Partial<SearchDocument>): Promise<void> {
|
|
180
|
+
const index = this.client.index(indexName);
|
|
181
|
+
await index.updateDocuments([{ ...document, id }]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async get(indexName: string, id: string): Promise<SearchDocument | null> {
|
|
185
|
+
try {
|
|
186
|
+
const index = this.client.index(indexName);
|
|
187
|
+
const doc = await index.getDocument(id);
|
|
188
|
+
return doc as SearchDocument;
|
|
189
|
+
} catch {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async updateSettings(indexName: string, settings: IndexSettings): Promise<void> {
|
|
195
|
+
const index = this.client.index(indexName);
|
|
196
|
+
|
|
197
|
+
const meilisearchSettings: Record<string, unknown> = {};
|
|
198
|
+
|
|
199
|
+
if (settings.searchableAttributes) {
|
|
200
|
+
meilisearchSettings.searchableAttributes = settings.searchableAttributes;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (settings.filterableAttributes) {
|
|
204
|
+
meilisearchSettings.filterableAttributes = settings.filterableAttributes;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (settings.sortableAttributes) {
|
|
208
|
+
meilisearchSettings.sortableAttributes = settings.sortableAttributes;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (settings.displayedAttributes) {
|
|
212
|
+
meilisearchSettings.displayedAttributes = settings.displayedAttributes;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (settings.stopWords) {
|
|
216
|
+
meilisearchSettings.stopWords = settings.stopWords;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (settings.synonyms) {
|
|
220
|
+
meilisearchSettings.synonyms = settings.synonyms;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
await index.updateSettings(meilisearchSettings);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async getStats(indexName: string): Promise<IndexStats> {
|
|
227
|
+
const index = this.client.index(indexName);
|
|
228
|
+
const stats = (await index.getStats()) as {
|
|
229
|
+
numberOfDocuments: number;
|
|
230
|
+
isIndexing: boolean;
|
|
231
|
+
fieldDistribution: Record<string, number>;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
name: indexName,
|
|
236
|
+
documentCount: stats.numberOfDocuments,
|
|
237
|
+
size: 0,
|
|
238
|
+
isIndexing: stats.isIndexing,
|
|
239
|
+
health: 'green',
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async autocomplete(indexName: string, query: string, limit = 10): Promise<AutocompleteResult> {
|
|
244
|
+
const startTime = Date.now();
|
|
245
|
+
const result = await this.search(indexName, {
|
|
246
|
+
query,
|
|
247
|
+
limit,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const suggestions = result.hits.map((hit) => {
|
|
251
|
+
const doc = hit.document as SearchDocument;
|
|
252
|
+
return String(doc.title || doc.name || doc.id || '');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
suggestions,
|
|
257
|
+
documents: result.hits.map((h) => h.document as SearchDocument),
|
|
258
|
+
processingTime: Date.now() - startTime,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}
|