@an5/adapters 0.2.1 → 0.2.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.
Files changed (52) hide show
  1. package/README.md +283 -263
  2. package/dotnet/An5Adapter.cs +275 -275
  3. package/package.json +56 -53
  4. package/python/base/metadata.py +10 -10
  5. package/python/table_client.py +216 -216
  6. package/typescript/an5Adapter.d.ts +4 -2
  7. package/typescript/an5Adapter.js +60 -23
  8. package/typescript/base/uuid.d.ts +5 -0
  9. package/typescript/base/uuid.js +17 -0
  10. package/typescript/browser.d.ts +5 -0
  11. package/typescript/browser.js +12 -0
  12. package/typescript/googlesheets/adapter.d.ts +1 -1
  13. package/typescript/googlesheets/adapter.js +22 -11
  14. package/typescript/googlesheets/an5SheetsAdapter.d.ts +1 -1
  15. package/typescript/googlesheets/config.d.ts +5 -1
  16. package/typescript/googlesheets/config.js +2 -1
  17. package/typescript/googlesheets/helpers.js +28 -9
  18. package/typescript/googlesheets/index.d.ts +2 -1
  19. package/typescript/googlesheets/parseConnectionString.js +4 -0
  20. package/typescript/googlesheets/retry.d.ts +23 -1
  21. package/typescript/googlesheets/retry.js +87 -11
  22. package/typescript/googlesheets/tableClient.d.ts +1 -0
  23. package/typescript/googlesheets/tableClient.js +59 -42
  24. package/typescript/index.d.ts +4 -2
  25. package/typescript/{an5Adapter.ts → src/an5Adapter.ts} +505 -474
  26. package/typescript/{base → src/base}/index.ts +3 -3
  27. package/typescript/{base → src/base}/metadata.ts +28 -28
  28. package/typescript/{base → src/base}/sql.ts +98 -98
  29. package/typescript/{base → src/base}/types.ts +24 -24
  30. package/typescript/src/base/uuid.ts +14 -0
  31. package/typescript/src/browser.ts +6 -0
  32. package/typescript/{config.ts → src/config.ts} +95 -95
  33. package/typescript/{googlesheets → src/googlesheets}/adapter.ts +259 -241
  34. package/typescript/{googlesheets → src/googlesheets}/an5SheetsAdapter.ts +3 -3
  35. package/typescript/{googlesheets → src/googlesheets}/config.ts +47 -44
  36. package/typescript/{googlesheets → src/googlesheets}/helpers.ts +143 -124
  37. package/typescript/{googlesheets → src/googlesheets}/index.ts +3 -2
  38. package/typescript/{googlesheets → src/googlesheets}/parseConnectionString.ts +46 -44
  39. package/typescript/src/googlesheets/retry.ts +126 -0
  40. package/typescript/{googlesheets → src/googlesheets}/sqlExecutor.ts +301 -301
  41. package/typescript/{googlesheets → src/googlesheets}/tableClient.ts +455 -435
  42. package/typescript/{googlesheets → src/googlesheets}/types.ts +6 -6
  43. package/typescript/{index.ts → src/index.ts} +19 -15
  44. package/typescript/{mssql → src/mssql}/engine.ts +132 -132
  45. package/typescript/{mysql → src/mssql}/index.ts +1 -1
  46. package/typescript/{mysql → src/mysql}/engine.ts +80 -80
  47. package/typescript/{postgres → src/mysql}/index.ts +1 -1
  48. package/typescript/{postgres → src/postgres}/engine.ts +87 -87
  49. package/typescript/{sqlite → src/postgres}/index.ts +1 -1
  50. package/typescript/{sqlite → src/sqlite}/engine.ts +55 -55
  51. package/typescript/{mssql → src/sqlite}/index.ts +1 -1
  52. package/typescript/googlesheets/retry.ts +0 -32
@@ -1,10 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SheetsTableClient = void 0;
4
- const crypto_1 = require("crypto");
4
+ const uuid_1 = require("../base/uuid");
5
5
  const metadata_1 = require("../base/metadata");
6
6
  const helpers_1 = require("./helpers");
7
7
  const retry_1 = require("./retry");
8
+ // ─── Utility ──────────────────────────────────────────────────────────────────
9
+ function resolveIdField(fields) {
10
+ if (Object.prototype.hasOwnProperty.call(fields, 'id'))
11
+ return 'id';
12
+ return Object.keys(fields).find(name => name.endsWith('_id') || name.endsWith('Id') || name.toLowerCase() === 'id');
13
+ }
8
14
  // ─── Table Client ─────────────────────────────────────────────────────────────
9
15
  class SheetsTableClient {
10
16
  constructor(adapter, modelName, sheetMapping) {
@@ -146,20 +152,20 @@ class SheetsTableClient {
146
152
  const rows = await this.findMany({ where: args?.where });
147
153
  return rows.length;
148
154
  }
155
+ assignId(data, fields) {
156
+ const idFieldName = resolveIdField(fields);
157
+ if (!idFieldName)
158
+ return;
159
+ const fieldDef = fields[idFieldName];
160
+ const rawType = typeof fieldDef === 'string' ? fieldDef : (fieldDef?.ts || fieldDef?.sql || '');
161
+ const isStringType = ['string', 'uuid', 'uniqueidentifier', 'nvarchar', 'varchar', 'text'].includes(rawType.toLowerCase());
162
+ if (isStringType && !data[idFieldName]) {
163
+ data[idFieldName] = (0, uuid_1.generateUUID)();
164
+ }
165
+ }
149
166
  async create(args) {
150
- const fields = this.fields;
151
- const idFieldName = Object.prototype.hasOwnProperty.call(fields, 'id')
152
- ? 'id'
153
- : Object.keys(fields).find(name => name.endsWith('_id') || name.endsWith('Id') || name.toLowerCase() === 'id');
154
167
  const data = { ...args.data };
155
- if (idFieldName) {
156
- const fieldDef = fields[idFieldName];
157
- const rawType = typeof fieldDef === 'string' ? fieldDef : (fieldDef?.ts || fieldDef?.sql || '');
158
- const isStringType = ['string', 'uuid', 'uniqueidentifier', 'nvarchar', 'varchar', 'text'].includes(rawType.toLowerCase());
159
- if (isStringType && !data[idFieldName]) {
160
- data[idFieldName] = (0, crypto_1.randomUUID)();
161
- }
162
- }
168
+ this.assignId(data, this.fields);
163
169
  const cols = Object.keys(data).filter(k => data[k] !== undefined);
164
170
  const headers = await this.getOrCreateHeaders(cols);
165
171
  for (const h of headers) {
@@ -174,28 +180,16 @@ class SheetsTableClient {
174
180
  async createMany(args) {
175
181
  if (args.data.length === 0)
176
182
  return { count: 0 };
183
+ const fields = this.fields;
177
184
  const allKeys = new Set();
178
- for (const row of args.data) {
185
+ for (const row of args.data)
179
186
  Object.keys(row).forEach(k => allKeys.add(k));
180
- }
181
187
  const cols = [...allKeys];
182
- const fields = this.fields;
183
- const idFieldName = Object.prototype.hasOwnProperty.call(fields, 'id')
184
- ? 'id'
185
- : Object.keys(fields).find(name => name.endsWith('_id') || name.endsWith('Id') || name.toLowerCase() === 'id');
186
- const prepared = [];
187
- for (const item of args.data) {
188
+ const prepared = args.data.map(item => {
188
189
  const data = { ...item };
189
- if (idFieldName) {
190
- const fieldDef = fields[idFieldName];
191
- const rawType = typeof fieldDef === 'string' ? fieldDef : (fieldDef?.ts || fieldDef?.sql || '');
192
- const isStringType = ['string', 'uuid', 'uniqueidentifier', 'nvarchar', 'varchar', 'text'].includes(rawType.toLowerCase());
193
- if (isStringType && !data[idFieldName]) {
194
- data[idFieldName] = (0, crypto_1.randomUUID)();
195
- }
196
- }
197
- prepared.push(data);
198
- }
190
+ this.assignId(data, fields);
191
+ return data;
192
+ });
199
193
  const headers = await this.getOrCreateHeaders(cols);
200
194
  const batchValues = prepared.map(data => {
201
195
  for (const h of headers) {
@@ -238,16 +232,26 @@ class SheetsTableClient {
238
232
  async updateMany(args) {
239
233
  const { headers, rows } = await this.readAllRows();
240
234
  const matching = rows.filter(r => (0, helpers_1.matchWhere)(r, args.where));
241
- for (const target of matching) {
242
- const updated = { ...target, ...args.data, __row: target.__row };
243
- await this.adapter.writeRange(`${this.escSheetName}!A${target.__row}`, [
244
- this.rowToValues(updated, headers),
245
- ]);
246
- }
235
+ if (matching.length === 0)
236
+ return { count: 0 };
237
+ // Batch all writes into a single batchUpdate call
238
+ const api = await this.adapter.getSheets();
239
+ const spreadsheetId = this.adapter.config.spreadsheetId;
240
+ const valueRanges = matching.map(target => {
241
+ const updated = { ...target, ...args.data };
242
+ return {
243
+ range: `${this.escSheetName}!A${target.__row}`,
244
+ values: [this.rowToValues(updated, headers)],
245
+ };
246
+ });
247
+ await (0, retry_1.withRetry)(() => api.spreadsheets.values.batchUpdate({
248
+ spreadsheetId,
249
+ requestBody: { valueInputOption: 'RAW', data: valueRanges },
250
+ }));
247
251
  return { count: matching.length };
248
252
  }
249
253
  async delete(args) {
250
- const { headers, rows } = await this.readAllRows();
254
+ const { rows } = await this.readAllRows();
251
255
  const matching = rows.filter(r => (0, helpers_1.matchWhere)(r, args.where));
252
256
  if (matching.length === 0)
253
257
  throw new Error('No record found matching where clause');
@@ -260,17 +264,30 @@ class SheetsTableClient {
260
264
  return rest;
261
265
  }
262
266
  async deleteMany(args) {
263
- const { headers, rows } = await this.readAllRows();
267
+ const { rows } = await this.readAllRows();
264
268
  const matching = rows.filter(r => (0, helpers_1.matchWhere)(r, args?.where));
265
269
  if (matching.length === 0)
266
270
  return { count: 0 };
267
271
  const meta = await this.adapter.getSheetMeta(this.sheetName);
268
272
  if (!meta)
269
273
  throw new Error(`Sheet "${this.sheetName}" not found`);
274
+ // Batch all deletes into a single batchUpdate (sorted DESC to keep row indices stable)
275
+ const api = await this.adapter.getSheets();
270
276
  const sorted = [...matching].sort((a, b) => b.__row - a.__row);
271
- for (const target of sorted) {
272
- await this.deleteRowByIndex(target.__row, meta.sheetId);
273
- }
277
+ const requests = sorted.map(target => ({
278
+ deleteDimension: {
279
+ range: {
280
+ sheetId: meta.sheetId,
281
+ dimension: 'ROWS',
282
+ startIndex: target.__row - 1,
283
+ endIndex: target.__row,
284
+ },
285
+ },
286
+ }));
287
+ await (0, retry_1.withRetry)(() => api.spreadsheets.batchUpdate({
288
+ spreadsheetId: this.adapter.config.spreadsheetId,
289
+ requestBody: { requests },
290
+ }));
274
291
  return { count: matching.length };
275
292
  }
276
293
  async deleteAll() {
@@ -1,2 +1,4 @@
1
- export { An5Adapter, AdapterTableClient, createAn5Adapter, setAdapterMetadata, An5AdapterConfig, AdapterMetadata, Dialect, } from './an5Adapter';
2
- export { getLlmConfig, setLlmConfig, getEmbeddingConfig, setEmbeddingConfig, resetAdapter, LlmConfigData, EmbeddingConfigData, } from './config';
1
+ export { An5Adapter, AdapterTableClient, createAn5Adapter, setAdapterMetadata, } from './an5Adapter';
2
+ export type { An5AdapterConfig, AdapterMetadata, Dialect, } from './an5Adapter';
3
+ export { getLlmConfig, setLlmConfig, getEmbeddingConfig, setEmbeddingConfig, resetAdapter, } from './config';
4
+ export type { LlmConfigData, EmbeddingConfigData, } from './config';