ai-database 0.1.0 → 2.0.1
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/.turbo/turbo-build.log +5 -0
- package/CHANGELOG.md +9 -0
- package/README.md +381 -68
- package/TESTING.md +410 -0
- package/TEST_SUMMARY.md +250 -0
- package/TODO.md +128 -0
- package/dist/ai-promise-db.d.ts +370 -0
- package/dist/ai-promise-db.d.ts.map +1 -0
- package/dist/ai-promise-db.js +839 -0
- package/dist/ai-promise-db.js.map +1 -0
- package/dist/authorization.d.ts +531 -0
- package/dist/authorization.d.ts.map +1 -0
- package/dist/authorization.js +632 -0
- package/dist/authorization.js.map +1 -0
- package/dist/durable-clickhouse.d.ts +193 -0
- package/dist/durable-clickhouse.d.ts.map +1 -0
- package/dist/durable-clickhouse.js +422 -0
- package/dist/durable-clickhouse.js.map +1 -0
- package/dist/durable-promise.d.ts +182 -0
- package/dist/durable-promise.d.ts.map +1 -0
- package/dist/durable-promise.js +409 -0
- package/dist/durable-promise.js.map +1 -0
- package/dist/execution-queue.d.ts +239 -0
- package/dist/execution-queue.d.ts.map +1 -0
- package/dist/execution-queue.js +400 -0
- package/dist/execution-queue.js.map +1 -0
- package/dist/index.d.ts +50 -191
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +79 -462
- package/dist/index.js.map +1 -0
- package/dist/linguistic.d.ts +115 -0
- package/dist/linguistic.d.ts.map +1 -0
- package/dist/linguistic.js +379 -0
- package/dist/linguistic.js.map +1 -0
- package/dist/memory-provider.d.ts +304 -0
- package/dist/memory-provider.d.ts.map +1 -0
- package/dist/memory-provider.js +785 -0
- package/dist/memory-provider.js.map +1 -0
- package/dist/schema.d.ts +899 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +1165 -0
- package/dist/schema.js.map +1 -0
- package/dist/tests.d.ts +107 -0
- package/dist/tests.d.ts.map +1 -0
- package/dist/tests.js +568 -0
- package/dist/tests.js.map +1 -0
- package/dist/types.d.ts +972 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +126 -0
- package/dist/types.js.map +1 -0
- package/package.json +37 -37
- package/src/ai-promise-db.ts +1243 -0
- package/src/authorization.ts +1102 -0
- package/src/durable-clickhouse.ts +596 -0
- package/src/durable-promise.ts +582 -0
- package/src/execution-queue.ts +608 -0
- package/src/index.test.ts +868 -0
- package/src/index.ts +337 -0
- package/src/linguistic.ts +404 -0
- package/src/memory-provider.test.ts +1036 -0
- package/src/memory-provider.ts +1119 -0
- package/src/schema.test.ts +1254 -0
- package/src/schema.ts +2296 -0
- package/src/tests.ts +725 -0
- package/src/types.ts +1177 -0
- package/test/README.md +153 -0
- package/test/edge-cases.test.ts +646 -0
- package/test/provider-resolution.test.ts +402 -0
- package/tsconfig.json +9 -0
- package/vitest.config.ts +19 -0
- package/dist/index.d.mts +0 -195
- package/dist/index.mjs +0 -430
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for provider resolution from DATABASE_URL
|
|
3
|
+
*
|
|
4
|
+
* Tests how the DB factory resolves different DATABASE_URL formats.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
8
|
+
import { setProvider, createMemoryProvider } from '../src/index.js'
|
|
9
|
+
|
|
10
|
+
describe('DATABASE_URL parsing', () => {
|
|
11
|
+
let originalEnv: NodeJS.ProcessEnv
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
// Save original environment
|
|
15
|
+
originalEnv = { ...process.env }
|
|
16
|
+
// Reset provider
|
|
17
|
+
setProvider(createMemoryProvider())
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
// Restore original environment
|
|
22
|
+
process.env = originalEnv
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe('URL format detection', () => {
|
|
26
|
+
it('detects in-memory provider', () => {
|
|
27
|
+
const url = ':memory:'
|
|
28
|
+
|
|
29
|
+
// The parseDatabaseUrl function is internal, but we can test the behavior
|
|
30
|
+
// by checking that it doesn't throw and returns expected structure
|
|
31
|
+
expect(url).toBe(':memory:')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('detects filesystem provider (default)', () => {
|
|
35
|
+
const urls = [
|
|
36
|
+
'./content',
|
|
37
|
+
'/absolute/path/to/content',
|
|
38
|
+
'relative/path',
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
urls.forEach(url => {
|
|
42
|
+
expect(url).not.toContain('://')
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('detects SQLite provider', () => {
|
|
47
|
+
const urls = [
|
|
48
|
+
'sqlite://./content',
|
|
49
|
+
'sqlite:///absolute/path',
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
urls.forEach(url => {
|
|
53
|
+
expect(url.startsWith('sqlite://')).toBe(true)
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('detects Turso provider', () => {
|
|
58
|
+
const urls = [
|
|
59
|
+
'libsql://my-db.turso.io',
|
|
60
|
+
'libsql://my-db.turso.io/db',
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
urls.forEach(url => {
|
|
64
|
+
expect(
|
|
65
|
+
url.startsWith('libsql://') || url.includes('.turso.io')
|
|
66
|
+
).toBe(true)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('detects ClickHouse provider (local)', () => {
|
|
71
|
+
const urls = [
|
|
72
|
+
'chdb://./content',
|
|
73
|
+
'chdb:///absolute/path',
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
urls.forEach(url => {
|
|
77
|
+
expect(url.startsWith('chdb://')).toBe(true)
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('detects ClickHouse provider (remote)', () => {
|
|
82
|
+
const urls = [
|
|
83
|
+
'clickhouse://localhost:8123',
|
|
84
|
+
'clickhouse://clickhouse.example.com:8123/db',
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
urls.forEach(url => {
|
|
88
|
+
expect(url.startsWith('clickhouse://') && url.includes(':')).toBe(true)
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe('provider selection', () => {
|
|
94
|
+
it('uses default provider when DATABASE_URL not set', () => {
|
|
95
|
+
delete process.env.DATABASE_URL
|
|
96
|
+
|
|
97
|
+
// Should default to filesystem or memory
|
|
98
|
+
expect(process.env.DATABASE_URL).toBeUndefined()
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('respects DATABASE_URL environment variable', () => {
|
|
102
|
+
process.env.DATABASE_URL = 'sqlite://./test.db'
|
|
103
|
+
|
|
104
|
+
expect(process.env.DATABASE_URL).toBe('sqlite://./test.db')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('handles empty DATABASE_URL', () => {
|
|
108
|
+
process.env.DATABASE_URL = ''
|
|
109
|
+
|
|
110
|
+
// Should use default
|
|
111
|
+
expect(process.env.DATABASE_URL).toBe('')
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
describe('URL parsing logic', () => {
|
|
116
|
+
it('extracts root path from filesystem URL', () => {
|
|
117
|
+
const url = './content'
|
|
118
|
+
const root = url
|
|
119
|
+
|
|
120
|
+
expect(root).toBe('./content')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('extracts root path from SQLite URL', () => {
|
|
124
|
+
const url = 'sqlite://./content'
|
|
125
|
+
const root = url.replace('sqlite://', '')
|
|
126
|
+
|
|
127
|
+
expect(root).toBe('./content')
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('extracts root path from chDB URL', () => {
|
|
131
|
+
const url = 'chdb://./data'
|
|
132
|
+
const root = url.replace('chdb://', '')
|
|
133
|
+
|
|
134
|
+
expect(root).toBe('./data')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('preserves remote URLs', () => {
|
|
138
|
+
const url = 'libsql://my-db.turso.io'
|
|
139
|
+
|
|
140
|
+
expect(url).toContain('turso.io')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('converts clickhouse:// to https:// for remote', () => {
|
|
144
|
+
const url = 'clickhouse://localhost:8123'
|
|
145
|
+
const httpUrl = url.replace('clickhouse://', 'https://')
|
|
146
|
+
|
|
147
|
+
expect(httpUrl).toBe('https://localhost:8123')
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
describe('provider paths', () => {
|
|
152
|
+
it('generates correct .db folder path for SQLite', () => {
|
|
153
|
+
const root = './content'
|
|
154
|
+
const dbPath = `${root}/.db/index.sqlite`
|
|
155
|
+
|
|
156
|
+
expect(dbPath).toBe('./content/.db/index.sqlite')
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('generates correct .db folder path for ClickHouse', () => {
|
|
160
|
+
const root = './content'
|
|
161
|
+
const dbPath = `${root}/.db/clickhouse`
|
|
162
|
+
|
|
163
|
+
expect(dbPath).toBe('./content/.db/clickhouse')
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('handles absolute paths', () => {
|
|
167
|
+
const root = '/var/data/content'
|
|
168
|
+
const dbPath = `${root}/.db/index.sqlite`
|
|
169
|
+
|
|
170
|
+
expect(dbPath).toBe('/var/data/content/.db/index.sqlite')
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
describe('special cases', () => {
|
|
175
|
+
it('handles URLs with query parameters', () => {
|
|
176
|
+
const url = 'libsql://my-db.turso.io?auth=token'
|
|
177
|
+
|
|
178
|
+
expect(url).toContain('?auth=token')
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('handles URLs with database names', () => {
|
|
182
|
+
const url = 'clickhouse://localhost:8123/mydb'
|
|
183
|
+
|
|
184
|
+
expect(url).toContain('/mydb')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('handles file:// protocol', () => {
|
|
188
|
+
const url = 'sqlite:///absolute/path/to/db.sqlite'
|
|
189
|
+
const path = url.replace('sqlite://', '')
|
|
190
|
+
|
|
191
|
+
expect(path).toBe('/absolute/path/to/db.sqlite')
|
|
192
|
+
})
|
|
193
|
+
})
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
describe('provider initialization', () => {
|
|
197
|
+
beforeEach(() => {
|
|
198
|
+
setProvider(createMemoryProvider())
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('initializes memory provider synchronously', () => {
|
|
202
|
+
const provider = createMemoryProvider()
|
|
203
|
+
|
|
204
|
+
expect(provider).toBeDefined()
|
|
205
|
+
expect(typeof provider.get).toBe('function')
|
|
206
|
+
expect(typeof provider.create).toBe('function')
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it('memory provider is immediately usable', async () => {
|
|
210
|
+
const provider = createMemoryProvider()
|
|
211
|
+
|
|
212
|
+
const result = await provider.create('Test', 'test1', { value: 'test' })
|
|
213
|
+
|
|
214
|
+
expect(result.$id).toBe('test1')
|
|
215
|
+
expect(result.value).toBe('test')
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
it('setProvider allows custom provider', () => {
|
|
219
|
+
const customProvider = createMemoryProvider()
|
|
220
|
+
|
|
221
|
+
// Should not throw
|
|
222
|
+
expect(() => setProvider(customProvider)).not.toThrow()
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('multiple setProvider calls replace provider', async () => {
|
|
226
|
+
const provider1 = createMemoryProvider()
|
|
227
|
+
const provider2 = createMemoryProvider()
|
|
228
|
+
|
|
229
|
+
setProvider(provider1)
|
|
230
|
+
await provider1.create('Test', 'test1', { value: 'first' })
|
|
231
|
+
|
|
232
|
+
setProvider(provider2)
|
|
233
|
+
|
|
234
|
+
// provider2 should not have test1
|
|
235
|
+
const result = await provider2.get('Test', 'test1')
|
|
236
|
+
expect(result).toBeNull()
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
describe('provider interface compliance', () => {
|
|
241
|
+
it('memory provider implements all required methods', () => {
|
|
242
|
+
const provider = createMemoryProvider()
|
|
243
|
+
|
|
244
|
+
const requiredMethods = [
|
|
245
|
+
'get',
|
|
246
|
+
'list',
|
|
247
|
+
'search',
|
|
248
|
+
'create',
|
|
249
|
+
'update',
|
|
250
|
+
'delete',
|
|
251
|
+
'related',
|
|
252
|
+
'relate',
|
|
253
|
+
'unrelate',
|
|
254
|
+
]
|
|
255
|
+
|
|
256
|
+
requiredMethods.forEach(method => {
|
|
257
|
+
expect(typeof (provider as any)[method]).toBe('function')
|
|
258
|
+
})
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
it('memory provider has utility methods', () => {
|
|
262
|
+
const provider = createMemoryProvider()
|
|
263
|
+
|
|
264
|
+
expect(typeof provider.clear).toBe('function')
|
|
265
|
+
expect(typeof provider.stats).toBe('function')
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
it('provider methods return expected types', async () => {
|
|
269
|
+
const provider = createMemoryProvider()
|
|
270
|
+
|
|
271
|
+
// get returns Record<string, unknown> | null
|
|
272
|
+
const getResult = await provider.get('Test', 'id1')
|
|
273
|
+
expect(getResult === null || typeof getResult === 'object').toBe(true)
|
|
274
|
+
|
|
275
|
+
// create returns Record<string, unknown>
|
|
276
|
+
const createResult = await provider.create('Test', 'id1', { value: 'test' })
|
|
277
|
+
expect(typeof createResult).toBe('object')
|
|
278
|
+
|
|
279
|
+
// delete returns boolean
|
|
280
|
+
const deleteResult = await provider.delete('Test', 'id1')
|
|
281
|
+
expect(typeof deleteResult).toBe('boolean')
|
|
282
|
+
|
|
283
|
+
// list returns array
|
|
284
|
+
const listResult = await provider.list('Test')
|
|
285
|
+
expect(Array.isArray(listResult)).toBe(true)
|
|
286
|
+
|
|
287
|
+
// search returns array
|
|
288
|
+
const searchResult = await provider.search('Test', 'query')
|
|
289
|
+
expect(Array.isArray(searchResult)).toBe(true)
|
|
290
|
+
|
|
291
|
+
// related returns array
|
|
292
|
+
const relatedResult = await provider.related('Test', 'id1', 'relation')
|
|
293
|
+
expect(Array.isArray(relatedResult)).toBe(true)
|
|
294
|
+
})
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
describe('error handling', () => {
|
|
298
|
+
it('handles invalid DATABASE_URL gracefully', () => {
|
|
299
|
+
const invalidUrls = [
|
|
300
|
+
'invalid://something',
|
|
301
|
+
'ftp://not-supported',
|
|
302
|
+
'://malformed',
|
|
303
|
+
]
|
|
304
|
+
|
|
305
|
+
// These should not crash the parsing logic
|
|
306
|
+
invalidUrls.forEach(url => {
|
|
307
|
+
expect(typeof url).toBe('string')
|
|
308
|
+
})
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
it('handles missing provider dependencies gracefully', async () => {
|
|
312
|
+
// When using a provider that requires external packages,
|
|
313
|
+
// it should fail gracefully if packages aren't installed
|
|
314
|
+
|
|
315
|
+
// This is more of a documentation test - the actual behavior
|
|
316
|
+
// depends on dynamic imports in resolveProvider()
|
|
317
|
+
expect(true).toBe(true)
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
describe('performance considerations', () => {
|
|
322
|
+
it('provider stats track counts correctly', async () => {
|
|
323
|
+
const provider = createMemoryProvider()
|
|
324
|
+
|
|
325
|
+
await provider.create('User', 'user1', { name: 'User 1' })
|
|
326
|
+
await provider.create('User', 'user2', { name: 'User 2' })
|
|
327
|
+
await provider.create('Post', 'post1', { title: 'Post 1' })
|
|
328
|
+
|
|
329
|
+
const stats = provider.stats()
|
|
330
|
+
|
|
331
|
+
expect(stats.entities).toBe(3)
|
|
332
|
+
expect(stats.relations).toBe(0)
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it('tracks relation counts', async () => {
|
|
336
|
+
const provider = createMemoryProvider()
|
|
337
|
+
|
|
338
|
+
await provider.create('User', 'user1', { name: 'User 1' })
|
|
339
|
+
await provider.create('Post', 'post1', { title: 'Post 1' })
|
|
340
|
+
await provider.create('Post', 'post2', { title: 'Post 2' })
|
|
341
|
+
|
|
342
|
+
await provider.relate('User', 'user1', 'posts', 'Post', 'post1')
|
|
343
|
+
await provider.relate('User', 'user1', 'posts', 'Post', 'post2')
|
|
344
|
+
|
|
345
|
+
const stats = provider.stats()
|
|
346
|
+
|
|
347
|
+
expect(stats.entities).toBe(3)
|
|
348
|
+
expect(stats.relations).toBe(2)
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
it('clear resets all counts', async () => {
|
|
352
|
+
const provider = createMemoryProvider()
|
|
353
|
+
|
|
354
|
+
await provider.create('User', 'user1', { name: 'User 1' })
|
|
355
|
+
await provider.create('Post', 'post1', { title: 'Post 1' })
|
|
356
|
+
await provider.relate('User', 'user1', 'posts', 'Post', 'post1')
|
|
357
|
+
|
|
358
|
+
provider.clear()
|
|
359
|
+
|
|
360
|
+
const stats = provider.stats()
|
|
361
|
+
expect(stats.entities).toBe(0)
|
|
362
|
+
expect(stats.relations).toBe(0)
|
|
363
|
+
})
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
describe('documentation examples', () => {
|
|
367
|
+
it('matches README filesystem example', () => {
|
|
368
|
+
const url = './content'
|
|
369
|
+
|
|
370
|
+
expect(url).toBe('./content')
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
it('matches README SQLite example', () => {
|
|
374
|
+
const url = 'sqlite://./content'
|
|
375
|
+
|
|
376
|
+
expect(url.startsWith('sqlite://')).toBe(true)
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
it('matches README Turso example', () => {
|
|
380
|
+
const url = 'libsql://your-db.turso.io'
|
|
381
|
+
|
|
382
|
+
expect(url.includes('.turso.io')).toBe(true)
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
it('matches README chDB example', () => {
|
|
386
|
+
const url = 'chdb://./content'
|
|
387
|
+
|
|
388
|
+
expect(url.startsWith('chdb://')).toBe(true)
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
it('matches README ClickHouse example', () => {
|
|
392
|
+
const url = 'clickhouse://localhost:8123'
|
|
393
|
+
|
|
394
|
+
expect(url.startsWith('clickhouse://')).toBe(true)
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
it('matches README memory example', () => {
|
|
398
|
+
const url = ':memory:'
|
|
399
|
+
|
|
400
|
+
expect(url).toBe(':memory:')
|
|
401
|
+
})
|
|
402
|
+
})
|
package/tsconfig.json
ADDED
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config'
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: false,
|
|
6
|
+
environment: 'node',
|
|
7
|
+
include: ['src/**/*.test.ts', 'test/**/*.test.ts'],
|
|
8
|
+
exclude: ['node_modules/**', 'dist/**'],
|
|
9
|
+
testTimeout: 30000,
|
|
10
|
+
hookTimeout: 15000,
|
|
11
|
+
// Run tests sequentially for database operations
|
|
12
|
+
pool: 'forks',
|
|
13
|
+
poolOptions: {
|
|
14
|
+
forks: {
|
|
15
|
+
singleFork: true,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
})
|
package/dist/index.d.mts
DELETED
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import { ListResponse } from 'apis.do/types';
|
|
2
|
-
export { ClientOptions, ErrorResponse, ListResponse, QueryParams } from 'apis.do/types';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Types for ai-database package
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Options for embedding generation
|
|
10
|
-
*/
|
|
11
|
-
interface EmbeddingOptions {
|
|
12
|
-
/** Embedding model to use (defaults to openai:text-embedding-3-small) */
|
|
13
|
-
model?: string;
|
|
14
|
-
/** Additional options for the embedding model */
|
|
15
|
-
[key: string]: any;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Result of embedding generation
|
|
19
|
-
*/
|
|
20
|
-
interface EmbeddingResult {
|
|
21
|
-
/** Generated embedding vectors */
|
|
22
|
-
embedding: number[][] | null;
|
|
23
|
-
/** Model used for embedding generation */
|
|
24
|
-
model: string;
|
|
25
|
-
/** Whether the embedding generation was successful */
|
|
26
|
-
success: boolean;
|
|
27
|
-
/** Error message if embedding generation failed */
|
|
28
|
-
error?: string;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Generic collection data type
|
|
32
|
-
*/
|
|
33
|
-
type CollectionData = Record<string, any>;
|
|
34
|
-
/**
|
|
35
|
-
* Query options for database operations
|
|
36
|
-
*/
|
|
37
|
-
interface QueryOptions {
|
|
38
|
-
/** Filter criteria */
|
|
39
|
-
where?: Record<string, any>;
|
|
40
|
-
/** Sorting options (field:direction) */
|
|
41
|
-
sort?: string | string[];
|
|
42
|
-
/** Number of results per page */
|
|
43
|
-
limit?: number;
|
|
44
|
-
/** Page number for pagination */
|
|
45
|
-
page?: number;
|
|
46
|
-
/** Fields to include in the result */
|
|
47
|
-
select?: string | string[];
|
|
48
|
-
/** Relations to populate */
|
|
49
|
-
populate?: string | string[];
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Methods available for each collection
|
|
53
|
-
* @template T Document type for the collection
|
|
54
|
-
*/
|
|
55
|
-
interface CollectionMethods<T = CollectionData> {
|
|
56
|
-
/** Find documents in the collection */
|
|
57
|
-
find: (options?: QueryOptions) => Promise<ListResponse<T>>;
|
|
58
|
-
/** Find a single document by ID */
|
|
59
|
-
findOne: (id: string) => Promise<T>;
|
|
60
|
-
/** Create a new document */
|
|
61
|
-
create: (data: Partial<T>) => Promise<T>;
|
|
62
|
-
/** Update an existing document */
|
|
63
|
-
update: (id: string, data: Partial<T>) => Promise<T>;
|
|
64
|
-
/** Delete a document */
|
|
65
|
-
delete: (id: string) => Promise<T>;
|
|
66
|
-
/** Search for documents */
|
|
67
|
-
search: (query: string, options?: QueryOptions) => Promise<ListResponse<T>>;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Database client interface
|
|
71
|
-
*/
|
|
72
|
-
interface DatabaseClientType {
|
|
73
|
-
/** Dynamic access to any collection */
|
|
74
|
-
[collection: string]: CollectionMethods;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Payload instance interface defining the minimum required methods
|
|
78
|
-
*/
|
|
79
|
-
interface PayloadInstance {
|
|
80
|
-
find?: (options: any) => Promise<any>;
|
|
81
|
-
findByID?: (options: any) => Promise<any>;
|
|
82
|
-
create?: (options: any) => Promise<any>;
|
|
83
|
-
update?: (options: any) => Promise<any>;
|
|
84
|
-
delete?: (options: any) => Promise<any>;
|
|
85
|
-
db?: any;
|
|
86
|
-
[key: string]: any;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Configuration for REST API-based Payload client
|
|
90
|
-
*/
|
|
91
|
-
interface RestClientConfig {
|
|
92
|
-
apiUrl: string;
|
|
93
|
-
apiKey?: string;
|
|
94
|
-
headers?: Record<string, string>;
|
|
95
|
-
fetchOptions?: RequestInit;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Options for DB initialization
|
|
99
|
-
*/
|
|
100
|
-
interface DBOptions {
|
|
101
|
-
baseUrl?: string;
|
|
102
|
-
apiKey?: string;
|
|
103
|
-
payload?: PayloadInstance;
|
|
104
|
-
apiUrl?: string;
|
|
105
|
-
headers?: Record<string, string>;
|
|
106
|
-
fetchOptions?: RequestInit;
|
|
107
|
-
[collection: string]: any;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Collection handler options
|
|
111
|
-
*/
|
|
112
|
-
interface CollectionHandlerOptions {
|
|
113
|
-
payload: PayloadInstance;
|
|
114
|
-
collectionName: string;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Node.js adapter for ai-database
|
|
119
|
-
*/
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Creates a database client for Node.js environments
|
|
123
|
-
* @param options Configuration options
|
|
124
|
-
* @returns Database client instance
|
|
125
|
-
* @example
|
|
126
|
-
* import { getPayload } from 'payload'
|
|
127
|
-
* import config from '@payload-config'
|
|
128
|
-
* import { createNodeClient } from 'ai-database/adapters'
|
|
129
|
-
*
|
|
130
|
-
* const payload = await getPayload({ config })
|
|
131
|
-
* const db = createNodeClient({ payload })
|
|
132
|
-
*/
|
|
133
|
-
declare const createNodeClient: (options?: DBOptions) => DatabaseClientType;
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Edge runtime adapter for ai-database
|
|
137
|
-
*/
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Creates a database client for Edge runtime environments
|
|
141
|
-
* @param options Configuration options (must include apiUrl)
|
|
142
|
-
* @returns Database client instance
|
|
143
|
-
* @example
|
|
144
|
-
* import { createEdgeClient } from 'ai-database/adapters'
|
|
145
|
-
*
|
|
146
|
-
* const db = createEdgeClient({
|
|
147
|
-
* apiUrl: 'https://your-payload-api.com/api',
|
|
148
|
-
* apiKey: 'your-api-key'
|
|
149
|
-
* })
|
|
150
|
-
*/
|
|
151
|
-
declare const createEdgeClient: (options?: DBOptions) => DatabaseClientType;
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Embedding functionality for ai-database
|
|
155
|
-
* Uses OpenAI API directly for generating embeddings
|
|
156
|
-
*/
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Generate embeddings for text using OpenAI API
|
|
160
|
-
* @param text Text to generate embeddings for
|
|
161
|
-
* @param options Embedding options
|
|
162
|
-
* @returns Promise resolving to embedding result
|
|
163
|
-
*/
|
|
164
|
-
declare function generateEmbedding(text: string | string[], options?: EmbeddingOptions): Promise<EmbeddingResult>;
|
|
165
|
-
/**
|
|
166
|
-
* Calculate cosine similarity between two embeddings
|
|
167
|
-
* @param embedding1 First embedding vector
|
|
168
|
-
* @param embedding2 Second embedding vector
|
|
169
|
-
* @returns Cosine similarity score (0-1)
|
|
170
|
-
*/
|
|
171
|
-
declare function calculateSimilarity(embedding1: number[], embedding2: number[]): number;
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* ai-database
|
|
175
|
-
* Direct interface to Payload CMS with database.do compatibility
|
|
176
|
-
*/
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Creates a database client with the provided options
|
|
180
|
-
* @param options Configuration options for the database client
|
|
181
|
-
* @returns A proxy-based database client that provides collection-based access
|
|
182
|
-
* @example
|
|
183
|
-
* const db = DB({ payload }) // Node.js with Payload instance
|
|
184
|
-
* const db = DB({ apiUrl: 'https://api.example.com' }) // Edge with REST API
|
|
185
|
-
*/
|
|
186
|
-
declare const DB: (options?: DBOptions) => DatabaseClientType;
|
|
187
|
-
/**
|
|
188
|
-
* Default database client instance
|
|
189
|
-
* Note: This will attempt to use a global Payload instance if available,
|
|
190
|
-
* otherwise it will throw an error. In most cases, you should use the DB
|
|
191
|
-
* function directly with explicit options.
|
|
192
|
-
*/
|
|
193
|
-
declare const db: DatabaseClientType;
|
|
194
|
-
|
|
195
|
-
export { type CollectionData, type CollectionHandlerOptions, type CollectionMethods, DB, type DBOptions, type DatabaseClientType, type EmbeddingOptions, type EmbeddingResult, type PayloadInstance, type QueryOptions, type RestClientConfig, calculateSimilarity, createEdgeClient, createNodeClient, db, DB as default, generateEmbedding };
|