@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.mjs CHANGED
@@ -248,33 +248,47 @@ var SqlDriver = class {
248
248
  // CRUD — DriverInterface core
249
249
  // ===================================
250
250
  async find(object, query, options) {
251
- const builder = this.getBuilder(object, options);
252
- this.applyTenantScope(builder, object, options);
251
+ const buildBase = () => {
252
+ const b = this.getBuilder(object, options);
253
+ this.applyTenantScope(b, object, options);
254
+ if (query.where) {
255
+ this.applyFilters(b, query.where);
256
+ }
257
+ if (query.orderBy && Array.isArray(query.orderBy)) {
258
+ for (const item of query.orderBy) {
259
+ if (item.field) {
260
+ b.orderBy(this.mapSortField(item.field), item.order || "asc");
261
+ }
262
+ }
263
+ }
264
+ if (query.offset !== void 0) b.offset(query.offset);
265
+ if (query.limit !== void 0) b.limit(query.limit);
266
+ return b;
267
+ };
268
+ const builder = buildBase();
253
269
  if (query.fields) {
254
270
  builder.select(query.fields.map((f) => this.mapSortField(f)));
255
271
  } else {
256
272
  builder.select("*");
257
273
  }
258
- if (query.where) {
259
- this.applyFilters(builder, query.where);
260
- }
261
- if (query.orderBy && Array.isArray(query.orderBy)) {
262
- for (const item of query.orderBy) {
263
- if (item.field) {
264
- builder.orderBy(this.mapSortField(item.field), item.order || "asc");
265
- }
266
- }
267
- }
268
- if (query.offset !== void 0) builder.offset(query.offset);
269
- if (query.limit !== void 0) builder.limit(query.limit);
270
274
  let results;
271
275
  try {
272
276
  results = await builder;
273
277
  } catch (error) {
274
- if (error.message && (error.message.includes("no such column") || error.message.includes("column") && error.message.includes("does not exist"))) {
275
- return [];
278
+ const isUnknownColumn = error.message && (error.message.includes("no such column") || error.message.includes("column") && error.message.includes("does not exist"));
279
+ if (isUnknownColumn) {
280
+ if (query.fields) {
281
+ try {
282
+ results = await buildBase().select("*");
283
+ } catch {
284
+ return [];
285
+ }
286
+ } else {
287
+ return [];
288
+ }
289
+ } else {
290
+ throw error;
276
291
  }
277
- throw error;
278
292
  }
279
293
  if (!Array.isArray(results)) {
280
294
  return [];