@budibase/backend-core 3.2.11 → 3.2.16

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
@@ -14084,7 +14084,7 @@ var DDInstrumentedDatabase = class {
14084
14084
  }
14085
14085
  exists(docId) {
14086
14086
  return import_dd_trace.default.trace("db.exists", (span) => {
14087
- span?.addTags({ db_name: this.name, doc_id: docId });
14087
+ span.addTags({ db_name: this.name, doc_id: docId });
14088
14088
  if (docId) {
14089
14089
  return this.db.exists(docId);
14090
14090
  }
@@ -14093,122 +14093,167 @@ var DDInstrumentedDatabase = class {
14093
14093
  }
14094
14094
  get(id) {
14095
14095
  return import_dd_trace.default.trace("db.get", (span) => {
14096
- span?.addTags({ db_name: this.name, doc_id: id });
14096
+ span.addTags({ db_name: this.name, doc_id: id });
14097
14097
  return this.db.get(id);
14098
14098
  });
14099
14099
  }
14100
14100
  tryGet(id) {
14101
- return import_dd_trace.default.trace("db.tryGet", (span) => {
14102
- span?.addTags({ db_name: this.name, doc_id: id });
14103
- return this.db.tryGet(id);
14101
+ return import_dd_trace.default.trace("db.tryGet", async (span) => {
14102
+ span.addTags({ db_name: this.name, doc_id: id });
14103
+ const doc = await this.db.tryGet(id);
14104
+ span.addTags({ doc_found: doc !== void 0 });
14105
+ return doc;
14104
14106
  });
14105
14107
  }
14106
14108
  getMultiple(ids, opts) {
14107
- return import_dd_trace.default.trace("db.getMultiple", (span) => {
14108
- span?.addTags({
14109
+ return import_dd_trace.default.trace("db.getMultiple", async (span) => {
14110
+ span.addTags({
14109
14111
  db_name: this.name,
14110
14112
  num_docs: ids.length,
14111
14113
  allow_missing: opts?.allowMissing
14112
14114
  });
14113
- return this.db.getMultiple(ids, opts);
14115
+ const docs = await this.db.getMultiple(ids, opts);
14116
+ span.addTags({ num_docs_found: docs.length });
14117
+ return docs;
14114
14118
  });
14115
14119
  }
14116
14120
  remove(idOrDoc, rev) {
14117
- return import_dd_trace.default.trace("db.remove", (span) => {
14118
- span?.addTags({ db_name: this.name, doc_id: idOrDoc });
14121
+ return import_dd_trace.default.trace("db.remove", async (span) => {
14122
+ span.addTags({ db_name: this.name, doc_id: idOrDoc, rev });
14119
14123
  const isDocument2 = typeof idOrDoc === "object";
14120
14124
  const id = isDocument2 ? idOrDoc._id : idOrDoc;
14121
14125
  rev = isDocument2 ? idOrDoc._rev : rev;
14122
- return this.db.remove(id, rev);
14126
+ const resp = await this.db.remove(id, rev);
14127
+ span.addTags({ ok: resp.ok });
14128
+ return resp;
14123
14129
  });
14124
14130
  }
14125
14131
  bulkRemove(documents, opts) {
14126
14132
  return import_dd_trace.default.trace("db.bulkRemove", (span) => {
14127
- span?.addTags({ db_name: this.name, num_docs: documents.length });
14133
+ span.addTags({
14134
+ db_name: this.name,
14135
+ num_docs: documents.length,
14136
+ silence_errors: opts?.silenceErrors
14137
+ });
14128
14138
  return this.db.bulkRemove(documents, opts);
14129
14139
  });
14130
14140
  }
14131
14141
  put(document, opts) {
14132
- return import_dd_trace.default.trace("db.put", (span) => {
14133
- span?.addTags({ db_name: this.name, doc_id: document._id });
14134
- return this.db.put(document, opts);
14142
+ return import_dd_trace.default.trace("db.put", async (span) => {
14143
+ span.addTags({
14144
+ db_name: this.name,
14145
+ doc_id: document._id,
14146
+ force: opts?.force
14147
+ });
14148
+ const resp = await this.db.put(document, opts);
14149
+ span.addTags({ ok: resp.ok });
14150
+ return resp;
14135
14151
  });
14136
14152
  }
14137
14153
  bulkDocs(documents) {
14138
14154
  return import_dd_trace.default.trace("db.bulkDocs", (span) => {
14139
- span?.addTags({ db_name: this.name, num_docs: documents.length });
14155
+ span.addTags({ db_name: this.name, num_docs: documents.length });
14140
14156
  return this.db.bulkDocs(documents);
14141
14157
  });
14142
14158
  }
14143
14159
  allDocs(params2) {
14144
- return import_dd_trace.default.trace("db.allDocs", (span) => {
14145
- span?.addTags({ db_name: this.name });
14146
- return this.db.allDocs(params2);
14160
+ return import_dd_trace.default.trace("db.allDocs", async (span) => {
14161
+ span.addTags({ db_name: this.name, ...params2 });
14162
+ const resp = await this.db.allDocs(params2);
14163
+ span.addTags({
14164
+ total_rows: resp.total_rows,
14165
+ rows_length: resp.rows.length,
14166
+ offset: resp.offset
14167
+ });
14168
+ return resp;
14147
14169
  });
14148
14170
  }
14149
14171
  query(viewName, params2) {
14150
- return import_dd_trace.default.trace("db.query", (span) => {
14151
- span?.addTags({ db_name: this.name, view_name: viewName });
14152
- return this.db.query(viewName, params2);
14172
+ return import_dd_trace.default.trace("db.query", async (span) => {
14173
+ span.addTags({ db_name: this.name, view_name: viewName, ...params2 });
14174
+ const resp = await this.db.query(viewName, params2);
14175
+ span.addTags({
14176
+ total_rows: resp.total_rows,
14177
+ rows_length: resp.rows.length,
14178
+ offset: resp.offset
14179
+ });
14180
+ return resp;
14153
14181
  });
14154
14182
  }
14155
14183
  destroy() {
14156
- return import_dd_trace.default.trace("db.destroy", (span) => {
14157
- span?.addTags({ db_name: this.name });
14158
- return this.db.destroy();
14184
+ return import_dd_trace.default.trace("db.destroy", async (span) => {
14185
+ span.addTags({ db_name: this.name });
14186
+ const resp = await this.db.destroy();
14187
+ span.addTags({ ok: resp.ok });
14188
+ return resp;
14159
14189
  });
14160
14190
  }
14161
14191
  compact() {
14162
- return import_dd_trace.default.trace("db.compact", (span) => {
14163
- span?.addTags({ db_name: this.name });
14164
- return this.db.compact();
14192
+ return import_dd_trace.default.trace("db.compact", async (span) => {
14193
+ span.addTags({ db_name: this.name });
14194
+ const resp = await this.db.compact();
14195
+ span.addTags({ ok: resp.ok });
14196
+ return resp;
14165
14197
  });
14166
14198
  }
14167
14199
  dump(stream3, opts) {
14168
14200
  return import_dd_trace.default.trace("db.dump", (span) => {
14169
- span?.addTags({ db_name: this.name });
14201
+ span.addTags({
14202
+ db_name: this.name,
14203
+ batch_limit: opts?.batch_limit,
14204
+ batch_size: opts?.batch_size,
14205
+ style: opts?.style,
14206
+ timeout: opts?.timeout,
14207
+ num_doc_ids: opts?.doc_ids?.length,
14208
+ view: opts?.view
14209
+ });
14170
14210
  return this.db.dump(stream3, opts);
14171
14211
  });
14172
14212
  }
14173
14213
  load(...args) {
14174
14214
  return import_dd_trace.default.trace("db.load", (span) => {
14175
- span?.addTags({ db_name: this.name });
14215
+ span.addTags({ db_name: this.name, num_args: args.length });
14176
14216
  return this.db.load(...args);
14177
14217
  });
14178
14218
  }
14179
14219
  createIndex(...args) {
14180
14220
  return import_dd_trace.default.trace("db.createIndex", (span) => {
14181
- span?.addTags({ db_name: this.name });
14221
+ span.addTags({ db_name: this.name, num_args: args.length });
14182
14222
  return this.db.createIndex(...args);
14183
14223
  });
14184
14224
  }
14185
14225
  deleteIndex(...args) {
14186
14226
  return import_dd_trace.default.trace("db.deleteIndex", (span) => {
14187
- span?.addTags({ db_name: this.name });
14227
+ span.addTags({ db_name: this.name, num_args: args.length });
14188
14228
  return this.db.deleteIndex(...args);
14189
14229
  });
14190
14230
  }
14191
14231
  getIndexes(...args) {
14192
14232
  return import_dd_trace.default.trace("db.getIndexes", (span) => {
14193
- span?.addTags({ db_name: this.name });
14233
+ span.addTags({ db_name: this.name, num_args: args.length });
14194
14234
  return this.db.getIndexes(...args);
14195
14235
  });
14196
14236
  }
14197
14237
  sql(sql, parameters) {
14198
- return import_dd_trace.default.trace("db.sql", (span) => {
14199
- span?.addTags({ db_name: this.name });
14200
- return this.db.sql(sql, parameters);
14238
+ return import_dd_trace.default.trace("db.sql", async (span) => {
14239
+ span.addTags({ db_name: this.name, num_bindings: parameters?.length });
14240
+ const resp = await this.db.sql(sql, parameters);
14241
+ span.addTags({ num_rows: resp.length });
14242
+ return resp;
14201
14243
  });
14202
14244
  }
14203
14245
  sqlPurgeDocument(docIds) {
14204
14246
  return import_dd_trace.default.trace("db.sqlPurgeDocument", (span) => {
14205
- span?.addTags({ db_name: this.name });
14247
+ span.addTags({
14248
+ db_name: this.name,
14249
+ num_docs: Array.isArray(docIds) ? docIds.length : 1
14250
+ });
14206
14251
  return this.db.sqlPurgeDocument(docIds);
14207
14252
  });
14208
14253
  }
14209
14254
  sqlDiskCleanup() {
14210
14255
  return import_dd_trace.default.trace("db.sqlDiskCleanup", (span) => {
14211
- span?.addTags({ db_name: this.name });
14256
+ span.addTags({ db_name: this.name });
14212
14257
  return this.db.sqlDiskCleanup();
14213
14258
  });
14214
14259
  }
@@ -17563,7 +17608,7 @@ var DatabaseImpl = class _DatabaseImpl {
17563
17608
  return await this.nano().db.destroy(this.name);
17564
17609
  } catch (err) {
17565
17610
  if (err.statusCode === 404) {
17566
- return;
17611
+ return { ok: true };
17567
17612
  } else {
17568
17613
  throw new CouchDBError(err.message, err);
17569
17614
  }
@@ -25624,7 +25669,7 @@ function isIgnoredType(type) {
25624
25669
  const ignored = ["link" /* LINK */, "formula" /* FORMULA */, "ai" /* AI */];
25625
25670
  return ignored.indexOf(type) !== -1;
25626
25671
  }
25627
- function generateSchema(schema, table, tables, oldTable = null, renamed) {
25672
+ function generateSchema(schema, table, tables, oldTable, renamed) {
25628
25673
  let primaryKeys = table && table.primary ? table.primary : [];
25629
25674
  const columns = Object.values(table.schema);
25630
25675
  let metaCols = columns.filter((col) => col.meta);
@@ -25644,7 +25689,7 @@ function generateSchema(schema, table, tables, oldTable = null, renamed) {
25644
25689
  (col) => col.foreignKey
25645
25690
  );
25646
25691
  for (let [key, column] of Object.entries(table.schema)) {
25647
- const oldColumn = oldTable ? oldTable.schema[key] : null;
25692
+ const oldColumn = oldTable?.schema[key];
25648
25693
  if (oldColumn && oldColumn.type || columnTypeSet.includes(key) || renamed?.updated === key) {
25649
25694
  continue;
25650
25695
  }
@@ -25784,16 +25829,15 @@ var SqlTableQueryBuilder = class {
25784
25829
  * @return the operation that was found in the JSON.
25785
25830
  */
25786
25831
  _operation(json) {
25787
- return json.endpoint.operation;
25832
+ return json.operation;
25788
25833
  }
25789
25834
  _tableQuery(json) {
25790
25835
  let client = (0, import_knex.knex)({ client: this.sqlClient }).schema;
25791
- let schemaName = json?.endpoint?.schema;
25792
- if (schemaName) {
25793
- client = client.withSchema(schemaName);
25836
+ if (json?.schema) {
25837
+ client = client.withSchema(json.schema);
25794
25838
  }
25795
25839
  let query;
25796
- if (!json.table || !json.meta || !json.meta.tables) {
25840
+ if (!json.table || !json.tables) {
25797
25841
  throw new Error("Cannot execute without table being specified");
25798
25842
  }
25799
25843
  if (json.table.sourceType === "internal" /* INTERNAL */) {
@@ -25801,15 +25845,15 @@ var SqlTableQueryBuilder = class {
25801
25845
  }
25802
25846
  switch (this._operation(json)) {
25803
25847
  case "CREATE_TABLE" /* CREATE_TABLE */:
25804
- query = buildCreateTable(client, json.table, json.meta.tables);
25848
+ query = buildCreateTable(client, json.table, json.tables);
25805
25849
  break;
25806
25850
  case "UPDATE_TABLE" /* UPDATE_TABLE */:
25807
- if (!json.meta || !json.meta.table) {
25851
+ if (!json.table) {
25808
25852
  throw new Error("Must specify old table for update");
25809
25853
  }
25810
- if (this.sqlClient === "mysql2" /* MY_SQL */ && json.meta.renamed) {
25854
+ if (this.sqlClient === "mysql2" /* MY_SQL */ && json.meta?.renamed) {
25811
25855
  const updatedColumn = json.meta.renamed.updated;
25812
- const tableName = schemaName ? `\`${schemaName}\`.\`${json.table.name}\`` : `\`${json.table.name}\``;
25856
+ const tableName = json?.schema ? `\`${json.schema}\`.\`${json.table.name}\`` : `\`${json.table.name}\``;
25813
25857
  return {
25814
25858
  sql: `alter table ${tableName} rename column \`${json.meta.renamed.old}\` to \`${updatedColumn}\`;`,
25815
25859
  bindings: []
@@ -25818,14 +25862,14 @@ var SqlTableQueryBuilder = class {
25818
25862
  query = buildUpdateTable(
25819
25863
  client,
25820
25864
  json.table,
25821
- json.meta.tables,
25822
- json.meta.table,
25823
- json.meta.renamed
25865
+ json.tables,
25866
+ json.meta?.oldTable,
25867
+ json.meta?.renamed
25824
25868
  );
25825
- if (this.sqlClient === "mssql" /* MS_SQL */ && json.meta.renamed) {
25869
+ if (this.sqlClient === "mssql" /* MS_SQL */ && json.meta?.renamed) {
25826
25870
  const oldColumn = json.meta.renamed.old;
25827
25871
  const updatedColumn = json.meta.renamed.updated;
25828
- const tableName = schemaName ? `${schemaName}.${json.table.name}` : `${json.table.name}`;
25872
+ const tableName = json?.schema ? `${json.schema}.${json.table.name}` : `${json.table.name}`;
25829
25873
  const sql = getNativeSql(query);
25830
25874
  if (Array.isArray(sql)) {
25831
25875
  for (const query2 of sql) {
@@ -25936,11 +25980,11 @@ var InternalBuilder = class {
25936
25980
  this.knex = knex3;
25937
25981
  this.splitter = new filters_exports.ColumnSplitter([this.table], {
25938
25982
  aliases: this.query.tableAliases,
25939
- columnPrefix: this.query.meta.columnPrefix
25983
+ columnPrefix: this.query.meta?.columnPrefix
25940
25984
  });
25941
25985
  }
25942
25986
  get table() {
25943
- return this.query.meta.table;
25987
+ return this.query.table;
25944
25988
  }
25945
25989
  get knexClient() {
25946
25990
  return this.knex.client;
@@ -26029,8 +26073,7 @@ var InternalBuilder = class {
26029
26073
  return parts.join(".");
26030
26074
  }
26031
26075
  isFullSelectStatementRequired() {
26032
- const { meta } = this.query;
26033
- for (let column of Object.values(meta.table.schema)) {
26076
+ for (let column of Object.values(this.table.schema)) {
26034
26077
  if (this.SPECIAL_SELECT_CASES.POSTGRES_MONEY(column)) {
26035
26078
  return true;
26036
26079
  } else if (this.SPECIAL_SELECT_CASES.MSSQL_DATES(column)) {
@@ -26040,29 +26083,29 @@ var InternalBuilder = class {
26040
26083
  return false;
26041
26084
  }
26042
26085
  generateSelectStatement() {
26043
- const { meta, endpoint, resource } = this.query;
26086
+ const { table, resource } = this.query;
26044
26087
  if (!resource || !resource.fields || resource.fields.length === 0) {
26045
26088
  return "*";
26046
26089
  }
26047
- const alias = this.getTableName(endpoint.entityId);
26048
- const schema = meta.table.schema;
26090
+ const alias = this.getTableName(table);
26091
+ const schema = this.table.schema;
26049
26092
  if (!this.isFullSelectStatementRequired()) {
26050
26093
  return [this.knex.raw("??", [`${alias}.*`])];
26051
26094
  }
26052
26095
  return resource.fields.map((field) => {
26053
26096
  const parts = field.split(/\./g);
26054
- let table = void 0;
26097
+ let table2 = void 0;
26055
26098
  let column = parts[0];
26056
26099
  if (parts.length > 1) {
26057
- table = parts[0];
26100
+ table2 = parts[0];
26058
26101
  column = parts.slice(1).join(".");
26059
26102
  }
26060
- return { table, column, field };
26061
- }).filter(({ table }) => !table || table === alias).map(({ table, column, field }) => {
26103
+ return { table: table2, column, field };
26104
+ }).filter(({ table: table2 }) => !table2 || table2 === alias).map(({ table: table2, column, field }) => {
26062
26105
  const columnSchema = schema[column];
26063
26106
  if (this.SPECIAL_SELECT_CASES.POSTGRES_MONEY(columnSchema)) {
26064
26107
  return this.knex.raw(`??::money::numeric as ??`, [
26065
- this.rawQuotedIdentifier([table, column].join(".")),
26108
+ this.rawQuotedIdentifier([table2, column].join(".")),
26066
26109
  this.knex.raw(this.quote(field))
26067
26110
  ]);
26068
26111
  }
@@ -26072,8 +26115,8 @@ var InternalBuilder = class {
26072
26115
  this.knex.raw(this.quote(field))
26073
26116
  ]);
26074
26117
  }
26075
- if (table) {
26076
- return this.rawQuotedIdentifier(`${table}.${column}`);
26118
+ if (table2) {
26119
+ return this.rawQuotedIdentifier(`${table2}.${column}`);
26077
26120
  } else {
26078
26121
  return this.rawQuotedIdentifier(field);
26079
26122
  }
@@ -26204,9 +26247,8 @@ var InternalBuilder = class {
26204
26247
  return query.andWhere(`${document}.fieldName`, "=", relationship.column);
26205
26248
  }
26206
26249
  addRelationshipForFilter(query, allowEmptyRelationships2, filterKey, whereCb) {
26207
- const { relationships, endpoint, tableAliases: aliases } = this.query;
26208
- const tableName = endpoint.entityId;
26209
- const fromAlias = aliases?.[tableName] || tableName;
26250
+ const { relationships, schema, tableAliases: aliases, table } = this.query;
26251
+ const fromAlias = aliases?.[table.name] || table.name;
26210
26252
  const matches2 = (value) => filterKey.match(new RegExp(`^${value}\\.`));
26211
26253
  if (!relationships) {
26212
26254
  return query;
@@ -26233,7 +26275,7 @@ var InternalBuilder = class {
26233
26275
  const throughAlias = aliases?.[manyToMany.through] || relationship.through;
26234
26276
  let throughTable = this.tableNameWithSchema(manyToMany.through, {
26235
26277
  alias: throughAlias,
26236
- schema: endpoint.schema
26278
+ schema
26237
26279
  });
26238
26280
  subQuery = subQuery.innerJoin(throughTable, function() {
26239
26281
  this.on(
@@ -26614,22 +26656,8 @@ var InternalBuilder = class {
26614
26656
  isSqs() {
26615
26657
  return isSqs(this.table);
26616
26658
  }
26617
- getTableName(tableOrName) {
26618
- let table;
26619
- if (typeof tableOrName === "string") {
26620
- const name2 = tableOrName;
26621
- if (this.query.table?.name === name2) {
26622
- table = this.query.table;
26623
- } else if (this.query.meta.table?.name === name2) {
26624
- table = this.query.meta.table;
26625
- } else if (!this.query.meta.tables?.[name2]) {
26626
- return name2;
26627
- } else {
26628
- table = this.query.meta.tables[name2];
26629
- }
26630
- } else if (tableOrName) {
26631
- table = tableOrName;
26632
- } else {
26659
+ getTableName(table) {
26660
+ if (!table) {
26633
26661
  table = this.table;
26634
26662
  }
26635
26663
  let name = table.name;
@@ -26761,8 +26789,9 @@ var InternalBuilder = class {
26761
26789
  }
26762
26790
  return withSchema;
26763
26791
  }
26764
- buildJsonField(field) {
26792
+ buildJsonField(table, field) {
26765
26793
  const parts = field.split(".");
26794
+ let baseName = parts[parts.length - 1];
26766
26795
  let unaliased;
26767
26796
  let tableField;
26768
26797
  if (parts.length > 1) {
@@ -26773,8 +26802,15 @@ var InternalBuilder = class {
26773
26802
  unaliased = parts.join(".");
26774
26803
  tableField = unaliased;
26775
26804
  }
26776
- const separator = this.client === "oracledb" /* ORACLE */ ? " VALUE " : ",";
26777
- return this.knex.raw(`?${separator}??`, [unaliased, this.rawQuotedIdentifier(tableField)]).toString();
26805
+ if (this.query.meta?.columnPrefix) {
26806
+ baseName = baseName.replace(this.query.meta.columnPrefix, "");
26807
+ }
26808
+ let identifier = this.rawQuotedIdentifier(tableField);
26809
+ const schema = table.schema[baseName];
26810
+ if (schema && schema.type === "bigint" /* BIGINT */) {
26811
+ identifier = this.castIntToString(identifier);
26812
+ }
26813
+ return [unaliased, identifier];
26778
26814
  }
26779
26815
  maxFunctionParameters() {
26780
26816
  switch (this.client) {
@@ -26789,7 +26825,7 @@ var InternalBuilder = class {
26789
26825
  addJsonRelationships(query, fromTable, relationships) {
26790
26826
  const sqlClient = this.client;
26791
26827
  const knex3 = this.knex;
26792
- const { resource, tableAliases: aliases, endpoint, meta } = this.query;
26828
+ const { resource, tableAliases: aliases, schema, tables } = this.query;
26793
26829
  const fields = resource?.fields || [];
26794
26830
  for (let relationship of relationships) {
26795
26831
  const {
@@ -26803,11 +26839,14 @@ var InternalBuilder = class {
26803
26839
  if (!toTable || !fromTable) {
26804
26840
  continue;
26805
26841
  }
26806
- const relatedTable = meta.tables?.[toTable];
26842
+ const relatedTable = tables[toTable];
26843
+ if (!relatedTable) {
26844
+ throw new Error(`related table "${toTable}" not found in datasource`);
26845
+ }
26807
26846
  const toAlias = aliases?.[toTable] || toTable, fromAlias = aliases?.[fromTable] || fromTable, throughAlias = throughTable && aliases?.[throughTable] || throughTable;
26808
26847
  let toTableWithSchema = this.tableNameWithSchema(toTable, {
26809
26848
  alias: toAlias,
26810
- schema: endpoint.schema
26849
+ schema
26811
26850
  });
26812
26851
  const requiredFields = [
26813
26852
  ...relatedTable?.primary || [],
@@ -26821,7 +26860,13 @@ var InternalBuilder = class {
26821
26860
  0,
26822
26861
  Math.floor(this.maxFunctionParameters() / 2)
26823
26862
  );
26824
- const fieldList = relationshipFields.map((field) => this.buildJsonField(field)).join(",");
26863
+ const fieldList = relationshipFields.map(
26864
+ (field) => this.buildJsonField(relatedTable, field)
26865
+ );
26866
+ const fieldListFormatted = fieldList.map((f) => {
26867
+ const separator = this.client === "oracledb" /* ORACLE */ ? " VALUE " : ",";
26868
+ return this.knex.raw(`?${separator}??`, [f[0], f[1]]).toString();
26869
+ }).join(",");
26825
26870
  const primaryKey = `${toAlias}.${toPrimary || toKey}`;
26826
26871
  let subQuery = knex3.from(toTableWithSchema).orderBy(primaryKey);
26827
26872
  const isManyToMany = throughTable && toPrimary && fromPrimary;
@@ -26829,7 +26874,7 @@ var InternalBuilder = class {
26829
26874
  if (isManyToMany) {
26830
26875
  let throughTableWithSchema = this.tableNameWithSchema(throughTable, {
26831
26876
  alias: throughAlias,
26832
- schema: endpoint.schema
26877
+ schema
26833
26878
  });
26834
26879
  subQuery = subQuery.join(throughTableWithSchema, function() {
26835
26880
  this.on(`${toAlias}.${toPrimary}`, "=", `${throughAlias}.${toKey}`);
@@ -26851,30 +26896,36 @@ var InternalBuilder = class {
26851
26896
  case "sqlite3" /* SQL_LITE */:
26852
26897
  subQuery = this.addJoinFieldCheck(subQuery, relationship);
26853
26898
  wrapperQuery = standardWrap(
26854
- this.knex.raw(`json_group_array(json_object(${fieldList}))`)
26899
+ this.knex.raw(
26900
+ `json_group_array(json_object(${fieldListFormatted}))`
26901
+ )
26855
26902
  );
26856
26903
  break;
26857
26904
  case "pg" /* POSTGRES */:
26858
26905
  wrapperQuery = standardWrap(
26859
- this.knex.raw(`json_agg(json_build_object(${fieldList}))`)
26906
+ this.knex.raw(`json_agg(json_build_object(${fieldListFormatted}))`)
26860
26907
  );
26861
26908
  break;
26862
26909
  case "mariadb" /* MARIADB */:
26863
26910
  wrapperQuery = subQuery.select(
26864
26911
  knex3.raw(
26865
- `json_arrayagg(json_object(${fieldList}) LIMIT ${getRelationshipLimit()})`
26912
+ `json_arrayagg(json_object(${fieldListFormatted}) LIMIT ${getRelationshipLimit()})`
26866
26913
  )
26867
26914
  );
26868
26915
  break;
26869
26916
  case "mysql2" /* MY_SQL */:
26870
26917
  case "oracledb" /* ORACLE */:
26871
26918
  wrapperQuery = standardWrap(
26872
- this.knex.raw(`json_arrayagg(json_object(${fieldList}))`)
26919
+ this.knex.raw(`json_arrayagg(json_object(${fieldListFormatted}))`)
26873
26920
  );
26874
26921
  break;
26875
26922
  case "mssql" /* MS_SQL */: {
26876
- const comparatorQuery = knex3.select(`${fromAlias}.*`).from({
26877
- [fromAlias]: subQuery.select(`${toAlias}.*`).limit(getRelationshipLimit())
26923
+ const comparatorQuery = knex3.select(`*`).from({
26924
+ [fromAlias]: subQuery.select(
26925
+ fieldList.map((f) => {
26926
+ return knex3.ref(f[1]).as(f[0]);
26927
+ })
26928
+ ).limit(getRelationshipLimit())
26878
26929
  });
26879
26930
  wrapperQuery = knex3.raw(
26880
26931
  `(SELECT ?? = (${comparatorQuery} FOR JSON PATH))`,
@@ -26890,8 +26941,7 @@ var InternalBuilder = class {
26890
26941
  return query;
26891
26942
  }
26892
26943
  addJoin(query, tables, columns) {
26893
- const { tableAliases: aliases, endpoint } = this.query;
26894
- const schema = endpoint.schema;
26944
+ const { tableAliases: aliases, schema } = this.query;
26895
26945
  const toTable = tables.to, fromTable = tables.from, throughTable = tables.through;
26896
26946
  const toAlias = aliases?.[toTable] || toTable, throughAlias = throughTable && aliases?.[throughTable] || throughTable, fromAlias = aliases?.[fromTable] || fromTable;
26897
26947
  let toTableWithSchema = this.tableNameWithSchema(toTable, {
@@ -26931,16 +26981,16 @@ var InternalBuilder = class {
26931
26981
  return query;
26932
26982
  }
26933
26983
  qualifiedKnex(opts) {
26934
- let alias = this.query.tableAliases?.[this.query.endpoint.entityId];
26984
+ let alias = this.query.tableAliases?.[this.query.table.name];
26935
26985
  if (opts?.alias === false) {
26936
26986
  alias = void 0;
26937
26987
  } else if (typeof opts?.alias === "string") {
26938
26988
  alias = opts.alias;
26939
26989
  }
26940
26990
  return this.knex(
26941
- this.tableNameWithSchema(this.query.endpoint.entityId, {
26991
+ this.tableNameWithSchema(this.query.table.name, {
26942
26992
  alias,
26943
- schema: this.query.endpoint.schema
26993
+ schema: this.query.schema
26944
26994
  })
26945
26995
  );
26946
26996
  }
@@ -26952,9 +27002,7 @@ var InternalBuilder = class {
26952
27002
  let query = this.qualifiedKnex({ alias: false });
26953
27003
  const parsedBody = this.parseBody(body2);
26954
27004
  if (this.client === "oracledb" /* ORACLE */) {
26955
- for (const [column, schema] of Object.entries(
26956
- this.query.meta.table.schema
26957
- )) {
27005
+ for (const [column, schema] of Object.entries(this.query.table.schema)) {
26958
27006
  if (schema.constraints?.presence === true || schema.type === "formula" /* FORMULA */ || schema.type === "auto" /* AUTO */ || schema.type === "link" /* LINK */ || schema.type === "ai" /* AI */) {
26959
27007
  continue;
26960
27008
  }
@@ -27004,10 +27052,8 @@ var InternalBuilder = class {
27004
27052
  return query.upsert(parsedBody);
27005
27053
  }
27006
27054
  read(opts = {}) {
27007
- let { endpoint, filters, paginate, relationships } = this.query;
27055
+ let { operation, filters, paginate, relationships, table } = this.query;
27008
27056
  const { limits } = opts;
27009
- const counting = endpoint.operation === "COUNT" /* COUNT */;
27010
- const tableName = endpoint.entityId;
27011
27057
  let query = this.qualifiedKnex();
27012
27058
  let foundOffset = null;
27013
27059
  let foundLimit = limits?.query || limits?.base;
@@ -27022,7 +27068,7 @@ var InternalBuilder = class {
27022
27068
  } else if (paginate && paginate.limit) {
27023
27069
  foundLimit = paginate.limit;
27024
27070
  }
27025
- if (!counting) {
27071
+ if (operation !== "COUNT" /* COUNT */) {
27026
27072
  if (foundLimit != null) {
27027
27073
  query = query.limit(foundLimit);
27028
27074
  }
@@ -27031,25 +27077,25 @@ var InternalBuilder = class {
27031
27077
  }
27032
27078
  }
27033
27079
  const aggregations = this.query.resource?.aggregations || [];
27034
- if (counting) {
27080
+ if (operation === "COUNT" /* COUNT */) {
27035
27081
  query = this.addDistinctCount(query);
27036
27082
  } else if (aggregations.length > 0) {
27037
27083
  query = this.addAggregations(query, aggregations);
27038
27084
  } else {
27039
27085
  query = query.select(this.generateSelectStatement());
27040
27086
  }
27041
- if (!counting) {
27087
+ if (operation !== "COUNT" /* COUNT */) {
27042
27088
  query = this.addSorting(query);
27043
27089
  }
27044
27090
  query = this.addFilters(query, filters, { relationship: true });
27045
27091
  if (relationships?.length && aggregations.length === 0) {
27046
- const mainTable = this.query.tableAliases?.[this.query.endpoint.entityId] || this.query.endpoint.entityId;
27092
+ const mainTable = this.query.tableAliases?.[table.name] || table.name;
27047
27093
  const cte = this.addSorting(
27048
27094
  this.knex.with("paginated", query).select(this.generateSelectStatement()).from({
27049
27095
  [mainTable]: "paginated"
27050
27096
  })
27051
27097
  );
27052
- return this.addJsonRelationships(cte, tableName, relationships);
27098
+ return this.addJsonRelationships(cte, table.name, relationships);
27053
27099
  }
27054
27100
  return query;
27055
27101
  }
@@ -27154,28 +27200,24 @@ var SqlQueryBuilder = class extends sqlTable_default {
27154
27200
  return {};
27155
27201
  }
27156
27202
  const input = this._query({
27157
- endpoint: {
27158
- ...json.endpoint,
27159
- operation: "READ" /* READ */
27160
- },
27161
- resource: {
27162
- fields: []
27163
- },
27203
+ operation: "READ" /* READ */,
27204
+ datasource: json.datasource,
27205
+ schema: json.schema,
27206
+ table: json.table,
27207
+ tables: json.tables,
27208
+ resource: { fields: [] },
27164
27209
  filters: json.extra?.idFilter,
27165
- paginate: {
27166
- limit: 1
27167
- },
27168
- meta: json.meta
27210
+ paginate: { limit: 1 }
27169
27211
  });
27170
27212
  return queryFn(input, "READ" /* READ */);
27171
27213
  }
27172
27214
  // when creating if an ID has been inserted need to make sure
27173
27215
  // the id filter is enriched with it before trying to retrieve the row
27174
27216
  checkLookupKeys(id, json) {
27175
- if (!id || !json.meta.table || !json.meta.table.primary) {
27217
+ if (!id || !json.table.primary) {
27176
27218
  return json;
27177
27219
  }
27178
- const primaryKey = json.meta.table.primary?.[0];
27220
+ const primaryKey = json.table.primary[0];
27179
27221
  json.extra = {
27180
27222
  idFilter: {
27181
27223
  equal: {