orchid-orm 1.50.4 → 1.51.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.d.ts CHANGED
@@ -188,19 +188,19 @@ interface BelongsToInfo<T extends RelationConfigSelf, Name extends string, Rel e
188
188
  }
189
189
 
190
190
  declare function transaction<Result>(this: {
191
- $queryBuilder: Db;
191
+ $qb: Db;
192
192
  }, fn: () => Promise<Result>): Promise<Result>;
193
193
  declare function transaction<Result>(this: {
194
- $queryBuilder: Db;
194
+ $qb: Db;
195
195
  }, options: IsolationLevel | TransactionOptions, fn: () => Promise<Result>): Promise<Result>;
196
196
  declare function ensureTransaction<Result>(this: {
197
- $queryBuilder: Db;
197
+ $qb: Db;
198
198
  }, cb: () => Promise<Result>): Promise<Result>;
199
199
  declare function isInTransaction(this: {
200
- $queryBuilder: Db;
200
+ $qb: Db;
201
201
  }): boolean;
202
202
  declare function afterCommit(this: {
203
- $queryBuilder: Db;
203
+ $qb: Db;
204
204
  }, hook: AfterCommitStandaloneHook): void;
205
205
 
206
206
  interface FromQuery extends Query {
@@ -226,7 +226,7 @@ type OrchidORM<T extends TableClasses = TableClasses> = {
226
226
  */
227
227
  $afterCommit: typeof afterCommit;
228
228
  $adapter: Adapter;
229
- $queryBuilder: Db;
229
+ $qb: Db;
230
230
  /**
231
231
  * Use `$query` to perform raw SQL queries.
232
232
  *
@@ -417,18 +417,6 @@ interface RelationConfigSelf {
417
417
  relations: RelationThunks;
418
418
  }
419
419
  type RelationConfigParams<T extends RelationConfigSelf, Relation extends RelationThunk> = Relation extends BelongsTo ? BelongsToParams<T, Relation> : Relation extends HasOne | HasMany ? HasOneParams<T, Relation> : Relation extends HasAndBelongsToMany ? HasAndBelongsToManyParams<T, Relation> : never;
420
- type MapRelation<T extends RelationConfigSelf, K extends keyof T['relations'] & string> = T['relations'][K] extends BelongsTo ? {
421
- relationConfig: BelongsToInfo<T, K, T['relations'][K], T['relations'][K]['options']['columns'][number] & string, T['relations'][K]['options']['required'], BelongsToQuery<RelationTableToQuery<T['relations'][K]>, K>>;
422
- } : T['relations'][K] extends HasOne ? {
423
- relationConfig: HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>>;
424
- } : T['relations'][K] extends HasMany ? {
425
- relationConfig: HasManyInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>>;
426
- } : T['relations'][K] extends HasAndBelongsToMany ? {
427
- relationConfig: HasAndBelongsToManyInfo<T, K, T['relations'][K], HasAndBelongsToManyQuery<K, RelationTableToQuery<T['relations'][K]>>>;
428
- } : never;
429
- type MapRelations<T> = T extends RelationConfigSelf ? {
430
- [K in keyof T['relations'] & string]: MapRelation<T, K>;
431
- } : EmptyObject;
432
420
 
433
421
  interface TableClass<T extends ORMTableInput = ORMTableInput> {
434
422
  new (): T;
@@ -446,11 +434,11 @@ interface TableInfo {
446
434
  interface Table extends Query, TableInfo {
447
435
  }
448
436
  interface TableToDb<T extends ORMTableInput, Relations extends RelationsBase> extends TableInfo, Db<T['table'], T['columns']['shape'], keyof ShapeColumnPrimaryKeys<T['columns']['shape']> extends never ? never : ShapeColumnPrimaryKeys<T['columns']['shape']>, ShapeUniqueColumns<T['columns']['shape']> | TableDataItemsUniqueColumns<T['columns']['shape'], T['columns']['data']>, TableDataItemsUniqueColumnTuples<T['columns']['shape'], T['columns']['data']>, UniqueConstraints<T['columns']['shape']> | TableDataItemsUniqueConstraints<T['columns']['data']>, T['types'], T['columns']['shape'] & ComputedColumnsFromOptions<T['computed']>, MapTableScopesOption<T>> {
449
- relations: {
450
- [K in keyof Relations]: Relations[K]['relationConfig']['query'] & Relations[K];
451
- };
437
+ relations: Relations;
452
438
  }
453
- type ORMTableInputToQueryBuilder<T extends ORMTableInput> = T extends RelationConfigSelf ? TableToDb<T, MapRelations<T>> : TableToDb<T, EmptyObject>;
439
+ type ORMTableInputToQueryBuilder<T extends ORMTableInput> = T extends RelationConfigSelf ? TableToDb<T, T extends RelationConfigSelf ? {
440
+ [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? BelongsToInfo<T, K, T['relations'][K], T['relations'][K]['options']['columns'][number] & string, T['relations'][K]['options']['required'], BelongsToQuery<RelationTableToQuery<T['relations'][K]>, K>> : T['relations'][K] extends HasOne ? HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasMany ? HasManyInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, K, T['relations'][K], HasAndBelongsToManyQuery<K, RelationTableToQuery<T['relations'][K]>>> : never;
441
+ } : EmptyObject> : TableToDb<T, EmptyObject>;
454
442
  interface ORMTableInput {
455
443
  table: string;
456
444
  columns: {
package/dist/index.js CHANGED
@@ -89,7 +89,7 @@ function createBaseTable({
89
89
  setColumns(fn, dataFn) {
90
90
  columnTypes[orchidCore.snakeCaseKey] = this.snakeCase;
91
91
  const shape = pqb.getColumnTypes(columnTypes, fn, nowSQL, this.language);
92
- const tableData = this.constructor.prototype.tableData = pqb.parseTableData(dataFn);
92
+ const tableData = pqb.parseTableData(dataFn);
93
93
  if (this.snakeCase) {
94
94
  for (const key in shape) {
95
95
  const column = shape[key];
@@ -148,10 +148,10 @@ function createBaseTable({
148
148
  }
149
149
 
150
150
  const getThroughRelation = (table, through) => {
151
- return table.relations[through]?.relationConfig;
151
+ return table.relations[through];
152
152
  };
153
153
  const getSourceRelation = (throughRelation, source) => {
154
- return throughRelation.query.relations[source]?.relationConfig;
154
+ return throughRelation.query.relations[source];
155
155
  };
156
156
  const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKeys, nestedInsert) => {
157
157
  const value = item[key];
@@ -297,7 +297,8 @@ const joinQueryChainHOF = (relPKeys, reverseJoin, joinQuery) => (joiningQuery, b
297
297
  return joinQuery(jq, baseQuery);
298
298
  }
299
299
  const last = chain[chain.length - 1];
300
- const query = "relationConfig" in last ? last.relationConfig.joinQuery(last, baseQuery) : last;
300
+ const prev = chain[chain.length - 2];
301
+ const query = prev.rel.joinQuery(last.query, baseQuery);
301
302
  let useWhereExist = true;
302
303
  if (jq.q.returnType !== "value" && jq.q.returnType !== "valueOrThrow") {
303
304
  let tablePrefix;
@@ -1958,7 +1959,7 @@ const applyRelations = (qb, tables, result) => {
1958
1959
  let message = `Cannot define a \`${item.relationName}\` relation on \`${as}\``;
1959
1960
  const table = result[as];
1960
1961
  const { through, source } = relation.options;
1961
- const throughRel = table.relations[through]?.relationConfig;
1962
+ const throughRel = table.relations[through];
1962
1963
  if (through && !throughRel) {
1963
1964
  message += `: cannot find \`${through}\` relation required by the \`through\` option`;
1964
1965
  } else if (source && throughRel && !throughRel.table.relations[source]) {
@@ -2033,7 +2034,7 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
2033
2034
  return q;
2034
2035
  }
2035
2036
  };
2036
- baseQuery.relationConfig = {
2037
+ dbTable.relations[relationName] = {
2037
2038
  table: otherDbTable,
2038
2039
  query,
2039
2040
  queryRelated: data.queryRelated,
@@ -2041,7 +2042,7 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
2041
2042
  reverseJoin: data.reverseJoin,
2042
2043
  modifyRelatedQuery: data.modifyRelatedQuery
2043
2044
  };
2044
- dbTable.relations[relationName] = query;
2045
+ (dbTable.relationQueries ?? (dbTable.relationQueries = {}))[relationName] = query;
2045
2046
  const tableRelations = delayedRelations.get(dbTable);
2046
2047
  if (!tableRelations) return;
2047
2048
  tableRelations[relationName]?.forEach((data2) => {
@@ -2050,19 +2051,19 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
2050
2051
  };
2051
2052
 
2052
2053
  function transaction(fnOrOptions, fn) {
2053
- return this.$queryBuilder.transaction(
2054
+ return this.$qb.transaction(
2054
2055
  fnOrOptions,
2055
2056
  fn
2056
2057
  );
2057
2058
  }
2058
2059
  function ensureTransaction(cb) {
2059
- return this.$queryBuilder.ensureTransaction(cb);
2060
+ return this.$qb.ensureTransaction(cb);
2060
2061
  }
2061
2062
  function isInTransaction() {
2062
- return this.$queryBuilder.isInTransaction();
2063
+ return this.$qb.isInTransaction();
2063
2064
  }
2064
2065
  function afterCommit(hook) {
2065
- this.$queryBuilder.afterCommit(hook);
2066
+ this.$qb.afterCommit(hook);
2066
2067
  }
2067
2068
 
2068
2069
  const orchidORM = ({
@@ -2084,7 +2085,7 @@ const orchidORM = ({
2084
2085
  if ("db" in options) {
2085
2086
  adapter = options.db.q.adapter;
2086
2087
  transactionStorage = options.db.internal.transactionStorage;
2087
- qb = options.db.queryBuilder;
2088
+ qb = options.db.qb;
2088
2089
  } else {
2089
2090
  adapter = "adapter" in options ? options.adapter : new pqb.Adapter(options);
2090
2091
  transactionStorage = new node_async_hooks.AsyncLocalStorage();
@@ -2102,8 +2103,10 @@ const orchidORM = ({
2102
2103
  $isInTransaction: isInTransaction,
2103
2104
  $afterCommit: afterCommit,
2104
2105
  $adapter: adapter,
2105
- $queryBuilder: qb,
2106
- $query: (...args) => qb.query(...args),
2106
+ $qb: qb,
2107
+ get $query() {
2108
+ return qb.query;
2109
+ },
2107
2110
  $queryArrays: (...args) => qb.queryArrays(...args),
2108
2111
  $with: qb.with.bind(qb),
2109
2112
  $withRecursive: qb.withRecursive.bind(qb),
@@ -2140,7 +2143,7 @@ const orchidORM = ({
2140
2143
  table.types,
2141
2144
  transactionStorage,
2142
2145
  options2,
2143
- table.constructor.prototype.tableData ?? {}
2146
+ table.constructor.prototype.columns?.data ?? {}
2144
2147
  );
2145
2148
  dbTable.definedAs = key;
2146
2149
  dbTable.db = result;