@objectstack/driver-sql 9.3.0 → 9.4.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.
package/dist/index.js CHANGED
@@ -285,33 +285,47 @@ var SqlDriver = class {
285
285
  // CRUD — DriverInterface core
286
286
  // ===================================
287
287
  async find(object, query, options) {
288
- const builder = this.getBuilder(object, options);
289
- this.applyTenantScope(builder, object, options);
288
+ const buildBase = () => {
289
+ const b = this.getBuilder(object, options);
290
+ this.applyTenantScope(b, object, options);
291
+ if (query.where) {
292
+ this.applyFilters(b, query.where);
293
+ }
294
+ if (query.orderBy && Array.isArray(query.orderBy)) {
295
+ for (const item of query.orderBy) {
296
+ if (item.field) {
297
+ b.orderBy(this.mapSortField(item.field), item.order || "asc");
298
+ }
299
+ }
300
+ }
301
+ if (query.offset !== void 0) b.offset(query.offset);
302
+ if (query.limit !== void 0) b.limit(query.limit);
303
+ return b;
304
+ };
305
+ const builder = buildBase();
290
306
  if (query.fields) {
291
307
  builder.select(query.fields.map((f) => this.mapSortField(f)));
292
308
  } else {
293
309
  builder.select("*");
294
310
  }
295
- if (query.where) {
296
- this.applyFilters(builder, query.where);
297
- }
298
- if (query.orderBy && Array.isArray(query.orderBy)) {
299
- for (const item of query.orderBy) {
300
- if (item.field) {
301
- builder.orderBy(this.mapSortField(item.field), item.order || "asc");
302
- }
303
- }
304
- }
305
- if (query.offset !== void 0) builder.offset(query.offset);
306
- if (query.limit !== void 0) builder.limit(query.limit);
307
311
  let results;
308
312
  try {
309
313
  results = await builder;
310
314
  } catch (error) {
311
- if (error.message && (error.message.includes("no such column") || error.message.includes("column") && error.message.includes("does not exist"))) {
312
- return [];
315
+ const isUnknownColumn = error.message && (error.message.includes("no such column") || error.message.includes("column") && error.message.includes("does not exist"));
316
+ if (isUnknownColumn) {
317
+ if (query.fields) {
318
+ try {
319
+ results = await buildBase().select("*");
320
+ } catch {
321
+ return [];
322
+ }
323
+ } else {
324
+ return [];
325
+ }
326
+ } else {
327
+ throw error;
313
328
  }
314
- throw error;
315
329
  }
316
330
  if (!Array.isArray(results)) {
317
331
  return [];