@gzl10/baserow 1.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 (58) hide show
  1. package/CHANGELOG.md +435 -0
  2. package/README.md +847 -0
  3. package/dist/index.d.ts +8749 -0
  4. package/dist/index.js +11167 -0
  5. package/dist/index.js.map +1 -0
  6. package/package.json +91 -0
  7. package/src/BaserowClient.ts +501 -0
  8. package/src/ClientWithCreds.ts +545 -0
  9. package/src/ClientWithCredsWs.ts +852 -0
  10. package/src/ClientWithToken.ts +171 -0
  11. package/src/contexts/DatabaseClientContext.ts +114 -0
  12. package/src/contexts/DatabaseContext.ts +870 -0
  13. package/src/contexts/DatabaseTokenContext.ts +331 -0
  14. package/src/contexts/FieldContext.ts +399 -0
  15. package/src/contexts/RowContext.ts +99 -0
  16. package/src/contexts/TableClientContext.ts +291 -0
  17. package/src/contexts/TableContext.ts +1247 -0
  18. package/src/contexts/TableOnlyContext.ts +74 -0
  19. package/src/contexts/WorkspaceContext.ts +490 -0
  20. package/src/express/errors.ts +260 -0
  21. package/src/express/index.ts +69 -0
  22. package/src/express/middleware.ts +225 -0
  23. package/src/express/serializers.ts +314 -0
  24. package/src/index.ts +247 -0
  25. package/src/presets/performance.ts +262 -0
  26. package/src/services/AuthService.ts +472 -0
  27. package/src/services/DatabaseService.ts +246 -0
  28. package/src/services/DatabaseTokenService.ts +186 -0
  29. package/src/services/FieldService.ts +1543 -0
  30. package/src/services/RowService.ts +982 -0
  31. package/src/services/SchemaControlService.ts +420 -0
  32. package/src/services/TableService.ts +781 -0
  33. package/src/services/WorkspaceService.ts +113 -0
  34. package/src/services/core/BaseAuthClient.ts +111 -0
  35. package/src/services/core/BaseClient.ts +107 -0
  36. package/src/services/core/BaseService.ts +71 -0
  37. package/src/services/core/HttpService.ts +115 -0
  38. package/src/services/core/ValidationService.ts +149 -0
  39. package/src/types/auth.ts +177 -0
  40. package/src/types/core.ts +91 -0
  41. package/src/types/errors.ts +105 -0
  42. package/src/types/fields.ts +456 -0
  43. package/src/types/index.ts +222 -0
  44. package/src/types/requests.ts +333 -0
  45. package/src/types/responses.ts +50 -0
  46. package/src/types/schema.ts +446 -0
  47. package/src/types/tokens.ts +36 -0
  48. package/src/types.ts +11 -0
  49. package/src/utils/auth.ts +174 -0
  50. package/src/utils/axios.ts +647 -0
  51. package/src/utils/field-cache.ts +164 -0
  52. package/src/utils/httpFactory.ts +66 -0
  53. package/src/utils/jwt-decoder.ts +188 -0
  54. package/src/utils/jwtTokens.ts +50 -0
  55. package/src/utils/performance.ts +105 -0
  56. package/src/utils/prisma-mapper.ts +961 -0
  57. package/src/utils/validation.ts +463 -0
  58. package/src/validators/schema.ts +419 -0
@@ -0,0 +1,291 @@
1
+ import { Logger, Table, Row, PrismaLikeQueryOptions } from '../types'
2
+ import { TableService } from '../services/TableService'
3
+ import { FieldService } from '../services/FieldService'
4
+ import { RowService } from '../services/RowService'
5
+ import { PrismaBaserowMapper } from '../utils/prisma-mapper'
6
+ import { FieldCache } from '../utils/field-cache'
7
+
8
+ /**
9
+ * Context para operaciones en una tabla específica con BaserowClient
10
+ * Proporciona API jerárquica para Database Token operations a nivel table
11
+ */
12
+ export class TableClientContext {
13
+ private databaseId: number
14
+ private tableId: number
15
+ private resolvedTable?: Table
16
+ private logger?: Logger
17
+ private fieldCache: FieldCache
18
+
19
+ constructor(
20
+ databaseId: number,
21
+ tableId: number,
22
+ private tableService: TableService,
23
+ private fieldService: FieldService,
24
+ private rowService: RowService,
25
+ logger?: Logger
26
+ ) {
27
+ this.databaseId = databaseId
28
+ this.tableId = tableId
29
+ this.logger = logger
30
+ this.fieldCache = new FieldCache(logger)
31
+ }
32
+
33
+ /**
34
+ * Obtener metadata completa de campos con cache
35
+ *
36
+ * @private
37
+ * @returns FieldMetadata con tipos y opciones select
38
+ */
39
+ private async getFieldMetadata() {
40
+ return await this.fieldCache.getFieldMetadata(this.fieldService, this.tableId)
41
+ }
42
+
43
+ /**
44
+ * Operaciones de rows en esta table (API Prisma-like)
45
+ */
46
+ get rows() {
47
+ return {
48
+ // === MÉTODOS ESTILO PRISMA ===
49
+
50
+ /**
51
+ * Buscar múltiples filas con opciones avanzadas estilo Prisma
52
+ *
53
+ * Equivalente a Prisma findMany(), proporciona filtrado avanzado,
54
+ * ordenamiento, paginación y selección de campos con sintaxis familiar.
55
+ *
56
+ * @param options - Opciones de consulta estilo Prisma
57
+ * @returns Promise con array de filas que cumplen los criterios
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const users = await table.rows.findMany({
62
+ * where: {
63
+ * status: 'active',
64
+ * age: { gte: 18 }
65
+ * },
66
+ * select: {
67
+ * id: true,
68
+ * name: true,
69
+ * email: true
70
+ * },
71
+ * orderBy: { created_at: 'desc' },
72
+ * take: 20
73
+ * })
74
+ * ```
75
+ */
76
+ findMany: async <T = Row>(options?: PrismaLikeQueryOptions<T>): Promise<T[]> => {
77
+ const fieldMetadata = await this.getFieldMetadata()
78
+ const baserowOptions = PrismaBaserowMapper.transformPrismaToBaserow(options, fieldMetadata)
79
+ const response = await this.rowService.list(this.tableId, baserowOptions)
80
+ return response.rows as T[]
81
+ },
82
+
83
+ /**
84
+ * Buscar TODAS las filas de la tabla con paginación automática
85
+ *
86
+ * Similar a findMany() pero sin límites de paginación. Descarga
87
+ * automáticamente todas las filas de la tabla aplicando los filtros
88
+ * especificados. Ideal para operaciones de exportación o análisis completo.
89
+ *
90
+ * @param options - Opciones de consulta (sin take/skip ya que obtiene todas)
91
+ * @returns Promise con TODAS las filas que cumplen los criterios
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * // Descargar tabla completa
96
+ * const allUsers = await client.table(123).rows.findAll()
97
+ *
98
+ * // Con filtros aplicados
99
+ * const allActiveUsers = await client.table(123).rows.findAll({
100
+ * where: { status: 'active' },
101
+ * orderBy: { created_at: 'desc' }
102
+ * })
103
+ * ```
104
+ *
105
+ * @since 1.1.0
106
+ */
107
+ findAll: async <T = Row>(options?: Omit<PrismaLikeQueryOptions<T>, 'take' | 'skip'>): Promise<T[]> => {
108
+ const fieldMetadata = await this.getFieldMetadata()
109
+ const baserowOptions = PrismaBaserowMapper.transformPrismaToBaserow(options, fieldMetadata)
110
+ return (await this.rowService.listAll(this.tableId, baserowOptions)) as T[]
111
+ },
112
+
113
+ /**
114
+ * Buscar la primera fila que cumpla los criterios
115
+ *
116
+ * @param options - Opciones de consulta estilo Prisma
117
+ * @returns Promise con la primera fila encontrada o null
118
+ */
119
+ findFirst: async <T = Row>(options?: PrismaLikeQueryOptions<T>): Promise<T | null> => {
120
+ const fieldMetadata = await this.getFieldMetadata()
121
+ const baserowOptions = PrismaBaserowMapper.transformPrismaToBaserow({ ...options, take: 1 }, fieldMetadata)
122
+ const response = await this.rowService.list(this.tableId, baserowOptions)
123
+ return response.rows.length > 0 ? (response.rows[0] as T) : null
124
+ },
125
+
126
+ /**
127
+ * Buscar fila específica por ID o criterios únicos
128
+ *
129
+ * @param idOrOptions - ID de la fila o opciones de búsqueda
130
+ * @returns Promise con la fila encontrada o null
131
+ */
132
+ findUnique: async <T = Row>(idOrOptions: number | PrismaLikeQueryOptions<T>): Promise<T | null> => {
133
+ if (typeof idOrOptions === 'number') {
134
+ try {
135
+ const row = await this.rowService.get(this.tableId, idOrOptions)
136
+ return row as T
137
+ } catch {
138
+ return null
139
+ }
140
+ } else {
141
+ const fieldMetadata = await this.getFieldMetadata()
142
+ const baserowOptions = PrismaBaserowMapper.transformPrismaToBaserow(
143
+ { ...idOrOptions, take: 1 },
144
+ fieldMetadata
145
+ )
146
+ const response = await this.rowService.list(this.tableId, baserowOptions)
147
+ return response.rows.length > 0 ? (response.rows[0] as T) : null
148
+ }
149
+ },
150
+
151
+ /**
152
+ * Crear una fila individual
153
+ *
154
+ * @param data - Datos para crear la fila
155
+ * @returns Promise con la fila creada
156
+ */
157
+ create: async <T = Row>(data: Partial<T> | Record<string, any>): Promise<T> => {
158
+ return (await this.rowService.createRow(this.tableId, data as Record<string, any>)) as T
159
+ },
160
+
161
+ /**
162
+ * Crear múltiples filas
163
+ *
164
+ * @param data - Array de datos para crear filas
165
+ * @param options - Opciones de creación
166
+ * @returns Promise con las filas creadas
167
+ */
168
+ createMany: async <T = Row>(data: Partial<T>[] | Record<string, any>[], options?: any): Promise<T[]> => {
169
+ return (await this.rowService.createBulk(this.tableId, data as Record<string, any>[], options)) as T[]
170
+ },
171
+
172
+ /**
173
+ * Actualizar múltiples filas
174
+ *
175
+ * @param updates - Array de actualizaciones con ID
176
+ * @param options - Opciones de actualización
177
+ * @returns Promise con las filas actualizadas
178
+ */
179
+ updateMany: async <T = Row>(
180
+ updates: Array<{ id: number } & Partial<T>> | Array<{ id: number } & Record<string, any>>,
181
+ options?: any
182
+ ): Promise<T[]> => {
183
+ return (await this.rowService.updateBulk(
184
+ this.tableId,
185
+ updates as Array<{ id: number } & Record<string, any>>,
186
+ options
187
+ )) as T[]
188
+ },
189
+
190
+ /**
191
+ * Eliminar múltiples filas
192
+ *
193
+ * @param ids - Array de IDs a eliminar
194
+ * @param options - Opciones de eliminación
195
+ * @returns Promise que resuelve cuando se completa
196
+ */
197
+ deleteMany: async (ids: number[], options?: any): Promise<void> => {
198
+ await this.rowService.deleteBulk(this.tableId, ids, options)
199
+ },
200
+
201
+ // === MÉTODOS ESPECÍFICOS (sin equivalente Prisma directo) ===
202
+
203
+ /**
204
+ * Buscar rows por texto
205
+ */
206
+ search: async (query: string, options?: any) => {
207
+ return await this.rowService.search(this.tableId, query, options)
208
+ },
209
+
210
+ /**
211
+ * Contar rows con filtros opcionales (sintaxis Prisma)
212
+ */
213
+ count: async (options?: { where?: Record<string, any> }): Promise<number> => {
214
+ const fieldMetadata = await this.getFieldMetadata()
215
+ const filters = PrismaBaserowMapper.transformWhereToFilters(options?.where, fieldMetadata)
216
+ return await this.rowService.count(this.tableId, filters)
217
+ },
218
+
219
+ /**
220
+ * Listar todas las rows de la tabla (con paginación automática)
221
+ */
222
+ listAll: async (options?: any) => {
223
+ return await this.rowService.listAll(this.tableId, options)
224
+ }
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Acceder a una fila específica en esta table
230
+ */
231
+ row(rowId: number) {
232
+ return {
233
+ /**
234
+ * Obtener fila por ID
235
+ */
236
+ get: async (options?: any) => {
237
+ return await this.rowService.get(this.tableId, rowId, options)
238
+ },
239
+
240
+ /**
241
+ * Actualizar fila
242
+ */
243
+ update: async (data: Record<string, any>) => {
244
+ return await this.rowService.updateRow(this.tableId, rowId, data)
245
+ },
246
+
247
+ /**
248
+ * Eliminar fila
249
+ */
250
+ delete: async () => {
251
+ return await this.rowService.delete(this.tableId, rowId)
252
+ },
253
+
254
+ /**
255
+ * Verificar si fila existe
256
+ */
257
+ exists: async () => {
258
+ return await this.rowService.exists(this.tableId, rowId)
259
+ }
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Obtener información de la table (lazy loading)
265
+ */
266
+ async get(): Promise<Table> {
267
+ if (this.resolvedTable) {
268
+ return this.resolvedTable
269
+ }
270
+
271
+ this.resolvedTable = await this.tableService.get(this.tableId)
272
+
273
+ if (this.logger) {
274
+ this.logger.debug?.(`Resolved table: "${this.resolvedTable.name}" (ID: ${this.resolvedTable.id})`)
275
+ }
276
+
277
+ return this.resolvedTable
278
+ }
279
+
280
+ /**
281
+ * Verificar si la table existe
282
+ */
283
+ async exists(): Promise<boolean> {
284
+ try {
285
+ await this.get()
286
+ return true
287
+ } catch {
288
+ return false
289
+ }
290
+ }
291
+ }