ai-database 0.1.0 → 0.2.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.
Files changed (72) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/.turbo/turbo-test.log +102 -0
  3. package/README.md +381 -68
  4. package/TESTING.md +410 -0
  5. package/TEST_SUMMARY.md +250 -0
  6. package/TODO.md +128 -0
  7. package/dist/ai-promise-db.d.ts +370 -0
  8. package/dist/ai-promise-db.d.ts.map +1 -0
  9. package/dist/ai-promise-db.js +839 -0
  10. package/dist/ai-promise-db.js.map +1 -0
  11. package/dist/authorization.d.ts +531 -0
  12. package/dist/authorization.d.ts.map +1 -0
  13. package/dist/authorization.js +632 -0
  14. package/dist/authorization.js.map +1 -0
  15. package/dist/durable-clickhouse.d.ts +193 -0
  16. package/dist/durable-clickhouse.d.ts.map +1 -0
  17. package/dist/durable-clickhouse.js +422 -0
  18. package/dist/durable-clickhouse.js.map +1 -0
  19. package/dist/durable-promise.d.ts +182 -0
  20. package/dist/durable-promise.d.ts.map +1 -0
  21. package/dist/durable-promise.js +409 -0
  22. package/dist/durable-promise.js.map +1 -0
  23. package/dist/execution-queue.d.ts +239 -0
  24. package/dist/execution-queue.d.ts.map +1 -0
  25. package/dist/execution-queue.js +400 -0
  26. package/dist/execution-queue.js.map +1 -0
  27. package/dist/index.d.ts +50 -191
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +79 -462
  30. package/dist/index.js.map +1 -0
  31. package/dist/linguistic.d.ts +115 -0
  32. package/dist/linguistic.d.ts.map +1 -0
  33. package/dist/linguistic.js +379 -0
  34. package/dist/linguistic.js.map +1 -0
  35. package/dist/memory-provider.d.ts +304 -0
  36. package/dist/memory-provider.d.ts.map +1 -0
  37. package/dist/memory-provider.js +785 -0
  38. package/dist/memory-provider.js.map +1 -0
  39. package/dist/schema.d.ts +899 -0
  40. package/dist/schema.d.ts.map +1 -0
  41. package/dist/schema.js +1165 -0
  42. package/dist/schema.js.map +1 -0
  43. package/dist/tests.d.ts +107 -0
  44. package/dist/tests.d.ts.map +1 -0
  45. package/dist/tests.js +568 -0
  46. package/dist/tests.js.map +1 -0
  47. package/dist/types.d.ts +972 -0
  48. package/dist/types.d.ts.map +1 -0
  49. package/dist/types.js +126 -0
  50. package/dist/types.js.map +1 -0
  51. package/package.json +37 -37
  52. package/src/ai-promise-db.ts +1243 -0
  53. package/src/authorization.ts +1102 -0
  54. package/src/durable-clickhouse.ts +596 -0
  55. package/src/durable-promise.ts +582 -0
  56. package/src/execution-queue.ts +608 -0
  57. package/src/index.test.ts +868 -0
  58. package/src/index.ts +337 -0
  59. package/src/linguistic.ts +404 -0
  60. package/src/memory-provider.test.ts +1036 -0
  61. package/src/memory-provider.ts +1119 -0
  62. package/src/schema.test.ts +1254 -0
  63. package/src/schema.ts +2296 -0
  64. package/src/tests.ts +725 -0
  65. package/src/types.ts +1177 -0
  66. package/test/README.md +153 -0
  67. package/test/edge-cases.test.ts +646 -0
  68. package/test/provider-resolution.test.ts +402 -0
  69. package/tsconfig.json +9 -0
  70. package/vitest.config.ts +19 -0
  71. package/dist/index.d.mts +0 -195
  72. package/dist/index.mjs +0 -430
package/dist/index.mjs DELETED
@@ -1,430 +0,0 @@
1
- // src/utils.ts
2
- var handleError = (error, operation, collection) => {
3
- const errorMessage = error.message || "Unknown error";
4
- const statusCode = error.status || error.statusCode || 500;
5
- const enhancedError = new Error(`${operation} operation failed on collection '${collection}': ${errorMessage}`);
6
- Object.assign(enhancedError, {
7
- statusCode,
8
- operation,
9
- collection,
10
- originalError: error
11
- });
12
- throw enhancedError;
13
- };
14
- var transformQueryOptions = (options = {}) => {
15
- const result = {};
16
- if (options.where) {
17
- result.where = options.where;
18
- }
19
- if (options.sort) {
20
- if (Array.isArray(options.sort)) {
21
- result.sort = options.sort.join(",");
22
- } else {
23
- result.sort = options.sort;
24
- }
25
- }
26
- if (options.limit) {
27
- result.limit = options.limit;
28
- }
29
- if (options.page) {
30
- result.page = options.page;
31
- }
32
- if (options.select) {
33
- if (Array.isArray(options.select)) {
34
- result.fields = options.select;
35
- } else {
36
- result.fields = [options.select];
37
- }
38
- }
39
- if (options.populate) {
40
- if (typeof options.populate === "string") {
41
- result.depth = 1;
42
- } else if (Array.isArray(options.populate) && options.populate.length > 0) {
43
- result.depth = 1;
44
- }
45
- }
46
- return result;
47
- };
48
- var getPayloadInstance = (options) => {
49
- if (options.payload) {
50
- return options.payload;
51
- }
52
- if (options.apiUrl) {
53
- const restConfig = {
54
- apiUrl: options.apiUrl,
55
- apiKey: options.apiKey,
56
- headers: options.headers,
57
- fetchOptions: options.fetchOptions
58
- };
59
- return createRestClient(restConfig);
60
- }
61
- try {
62
- if (typeof global !== "undefined" && global.payload) {
63
- return global.payload;
64
- }
65
- } catch (e) {
66
- }
67
- throw new Error("No Payload instance provided. Please provide a payload instance or apiUrl.");
68
- };
69
- var createRestClient = (config) => {
70
- const { apiUrl, apiKey, headers = {}, fetchOptions = {} } = config;
71
- const requestHeaders = {
72
- "Content-Type": "application/json",
73
- ...headers
74
- };
75
- if (apiKey) {
76
- requestHeaders["Authorization"] = `Bearer ${apiKey}`;
77
- }
78
- return {
79
- find: async (options) => {
80
- const { collection, ...params } = options;
81
- const searchParams = new URLSearchParams();
82
- Object.entries(params).forEach(([key, value]) => {
83
- if (typeof value === "object") {
84
- searchParams.append(key, JSON.stringify(value));
85
- } else {
86
- searchParams.append(key, String(value));
87
- }
88
- });
89
- const url = `${apiUrl}/${collection}?${searchParams.toString()}`;
90
- const response = await fetch(url, {
91
- method: "GET",
92
- headers: requestHeaders,
93
- ...fetchOptions
94
- });
95
- if (!response.ok) {
96
- const error = await response.json();
97
- throw new Error(error.message || `Failed to fetch from ${collection}`);
98
- }
99
- return response.json();
100
- },
101
- findByID: async (options) => {
102
- const { collection, id, ...params } = options;
103
- const searchParams = new URLSearchParams();
104
- Object.entries(params).forEach(([key, value]) => {
105
- if (typeof value === "object") {
106
- searchParams.append(key, JSON.stringify(value));
107
- } else {
108
- searchParams.append(key, String(value));
109
- }
110
- });
111
- const url = `${apiUrl}/${collection}/${id}?${searchParams.toString()}`;
112
- const response = await fetch(url, {
113
- method: "GET",
114
- headers: requestHeaders,
115
- ...fetchOptions
116
- });
117
- if (!response.ok) {
118
- const error = await response.json();
119
- throw new Error(error.message || `Failed to fetch ${id} from ${collection}`);
120
- }
121
- return response.json();
122
- },
123
- create: async (options) => {
124
- const { collection, data } = options;
125
- const url = `${apiUrl}/${collection}`;
126
- const response = await fetch(url, {
127
- method: "POST",
128
- headers: requestHeaders,
129
- body: JSON.stringify(data),
130
- ...fetchOptions
131
- });
132
- if (!response.ok) {
133
- const error = await response.json();
134
- throw new Error(error.message || `Failed to create document in ${collection}`);
135
- }
136
- return response.json();
137
- },
138
- update: async (options) => {
139
- const { collection, id, data } = options;
140
- const url = `${apiUrl}/${collection}/${id}`;
141
- const response = await fetch(url, {
142
- method: "PATCH",
143
- headers: requestHeaders,
144
- body: JSON.stringify(data),
145
- ...fetchOptions
146
- });
147
- if (!response.ok) {
148
- const error = await response.json();
149
- throw new Error(error.message || `Failed to update document ${id} in ${collection}`);
150
- }
151
- return response.json();
152
- },
153
- delete: async (options) => {
154
- const { collection, id } = options;
155
- const url = `${apiUrl}/${collection}/${id}`;
156
- const response = await fetch(url, {
157
- method: "DELETE",
158
- headers: requestHeaders,
159
- ...fetchOptions
160
- });
161
- if (!response.ok) {
162
- const error = await response.json();
163
- throw new Error(error.message || `Failed to delete document ${id} from ${collection}`);
164
- }
165
- return response.json();
166
- }
167
- };
168
- };
169
-
170
- // src/collectionHandler.ts
171
- var CollectionHandler = class {
172
- payload;
173
- collection;
174
- /**
175
- * Creates a new collection handler
176
- * @param options Collection handler options
177
- */
178
- constructor(options) {
179
- this.payload = options.payload;
180
- this.collection = options.collectionName;
181
- }
182
- /**
183
- * Find documents in the collection
184
- * @template T Type of documents to return
185
- * @param options Query options including filtering, sorting, and pagination
186
- * @returns Promise resolving to a list of documents
187
- */
188
- async find(options = {}) {
189
- try {
190
- const transformedOptions = transformQueryOptions(options);
191
- const result = await this.payload.find({
192
- collection: this.collection,
193
- ...transformedOptions
194
- });
195
- return {
196
- docs: result.docs || [],
197
- totalDocs: result.totalDocs || 0,
198
- page: result.page || 1,
199
- totalPages: result.totalPages || 1,
200
- limit: result.limit || 10,
201
- hasPrevPage: result.hasPrevPage || false,
202
- hasNextPage: result.hasNextPage || false,
203
- prevPage: result.prevPage || null,
204
- nextPage: result.nextPage || null
205
- };
206
- } catch (error) {
207
- return handleError(error, "find", this.collection);
208
- }
209
- }
210
- /**
211
- * Find a single document by ID
212
- * @template T Type of document to return
213
- * @param id Document ID
214
- * @returns Promise resolving to a single document
215
- */
216
- async findOne(id) {
217
- try {
218
- return await this.payload.findByID({
219
- collection: this.collection,
220
- id
221
- });
222
- } catch (error) {
223
- return handleError(error, "findOne", this.collection);
224
- }
225
- }
226
- /**
227
- * Create a new document in the collection
228
- * @template T Type of document to create
229
- * @param data Document data
230
- * @returns Promise resolving to the created document
231
- */
232
- async create(data) {
233
- try {
234
- return await this.payload.create({
235
- collection: this.collection,
236
- data
237
- });
238
- } catch (error) {
239
- return handleError(error, "create", this.collection);
240
- }
241
- }
242
- /**
243
- * Update an existing document
244
- * @template T Type of document to update
245
- * @param id Document ID
246
- * @param data Updated document data
247
- * @returns Promise resolving to the updated document
248
- */
249
- async update(id, data) {
250
- try {
251
- return await this.payload.update({
252
- collection: this.collection,
253
- id,
254
- data
255
- });
256
- } catch (error) {
257
- return handleError(error, "update", this.collection);
258
- }
259
- }
260
- /**
261
- * Delete a document
262
- * @template T Type of deleted document
263
- * @param id Document ID
264
- * @returns Promise resolving to the deleted document
265
- */
266
- async delete(id) {
267
- try {
268
- return await this.payload.delete({
269
- collection: this.collection,
270
- id
271
- });
272
- } catch (error) {
273
- return handleError(error, "delete", this.collection);
274
- }
275
- }
276
- /**
277
- * Search for documents
278
- * @template T Type of documents to search
279
- * @param query Search query string
280
- * @param options Query options
281
- * @returns Promise resolving to a list of matching documents
282
- */
283
- async search(query, options = {}) {
284
- try {
285
- const transformedOptions = transformQueryOptions(options);
286
- if (typeof this.payload.search === "function") {
287
- const result2 = await this.payload.search({
288
- collection: this.collection,
289
- query,
290
- ...transformedOptions
291
- });
292
- return {
293
- docs: result2.docs || [],
294
- totalDocs: result2.totalDocs || 0,
295
- page: result2.page || 1,
296
- totalPages: result2.totalPages || 1,
297
- limit: result2.limit || 10,
298
- hasPrevPage: result2.hasPrevPage || false,
299
- hasNextPage: result2.hasNextPage || false,
300
- prevPage: result2.prevPage || null,
301
- nextPage: result2.nextPage || null
302
- };
303
- }
304
- const result = await this.payload.find({
305
- collection: this.collection,
306
- search: query,
307
- ...transformedOptions
308
- });
309
- return {
310
- docs: result.docs || [],
311
- totalDocs: result.totalDocs || 0,
312
- page: result.page || 1,
313
- totalPages: result.totalPages || 1,
314
- limit: result.limit || 10,
315
- hasPrevPage: result.hasPrevPage || false,
316
- hasNextPage: result.hasNextPage || false,
317
- prevPage: result.prevPage || null,
318
- nextPage: result.nextPage || null
319
- };
320
- } catch (error) {
321
- return handleError(error, "search", this.collection);
322
- }
323
- }
324
- };
325
-
326
- // src/adapters/node.ts
327
- var createNodeClient = (options = {}) => {
328
- return DB(options);
329
- };
330
-
331
- // src/adapters/edge.ts
332
- var createEdgeClient = (options = {}) => {
333
- if (!options.apiUrl) {
334
- throw new Error("apiUrl is required for Edge environments");
335
- }
336
- return DB(options);
337
- };
338
-
339
- // src/embedding.ts
340
- async function generateEmbedding(text, options = {}) {
341
- try {
342
- const modelName = options.model || "text-embedding-3-small";
343
- const apiKey = options.apiKey || typeof globalThis !== "undefined" && globalThis.process?.env?.OPENAI_API_KEY;
344
- if (!apiKey) {
345
- throw new Error("apiKey option or OPENAI_API_KEY environment variable is required for embedding generation");
346
- }
347
- const input = Array.isArray(text) ? text : [text];
348
- const response = await fetch("https://api.openai.com/v1/embeddings", {
349
- method: "POST",
350
- headers: {
351
- "Content-Type": "application/json",
352
- Authorization: `Bearer ${apiKey}`
353
- },
354
- body: JSON.stringify({
355
- input,
356
- model: modelName
357
- })
358
- });
359
- if (!response.ok) {
360
- const errorData = await response.json().catch(() => ({}));
361
- throw new Error(`OpenAI API error: ${errorData.error?.message || response.statusText}`);
362
- }
363
- const data = await response.json();
364
- const embeddings = [];
365
- if (data && data.data && Array.isArray(data.data)) {
366
- for (const item of data.data) {
367
- if (item && item.embedding && Array.isArray(item.embedding)) {
368
- embeddings.push(item.embedding);
369
- }
370
- }
371
- }
372
- return {
373
- embedding: embeddings.length > 0 ? embeddings : null,
374
- model: modelName,
375
- success: embeddings.length > 0
376
- };
377
- } catch (error) {
378
- console.error("Error generating embedding:", error);
379
- return {
380
- embedding: null,
381
- model: options.model || "text-embedding-3-small",
382
- success: false,
383
- error: error instanceof Error ? error.message : String(error)
384
- };
385
- }
386
- }
387
- function calculateSimilarity(embedding1, embedding2) {
388
- if (embedding1.length !== embedding2.length) {
389
- throw new Error("Embeddings must have the same dimensions");
390
- }
391
- let dotProduct = 0;
392
- let magnitude1 = 0;
393
- let magnitude2 = 0;
394
- for (let i = 0; i < embedding1.length; i++) {
395
- dotProduct += embedding1[i] * embedding2[i];
396
- magnitude1 += embedding1[i] * embedding1[i];
397
- magnitude2 += embedding2[i] * embedding2[i];
398
- }
399
- magnitude1 = Math.sqrt(magnitude1);
400
- magnitude2 = Math.sqrt(magnitude2);
401
- if (magnitude1 === 0 || magnitude2 === 0) {
402
- return 0;
403
- }
404
- return dotProduct / (magnitude1 * magnitude2);
405
- }
406
-
407
- // src/index.ts
408
- var DB = (options = {}) => {
409
- const payload = getPayloadInstance(options);
410
- return new Proxy({}, {
411
- get: (target, prop) => {
412
- if (typeof prop === "string" && prop !== "then") {
413
- const collectionName = prop === "resources" ? "things" : String(prop);
414
- return new CollectionHandler({ payload, collectionName });
415
- }
416
- return target[prop];
417
- }
418
- });
419
- };
420
- var db = DB();
421
- var index_default = DB;
422
- export {
423
- DB,
424
- calculateSimilarity,
425
- createEdgeClient,
426
- createNodeClient,
427
- db,
428
- index_default as default,
429
- generateEmbedding
430
- };