imodel 0.16.2 → 0.17.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/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.16.2
2
+ * imodel v0.17.0
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
@@ -13,6 +13,15 @@ function setDevelopment(d) {
13
13
  isDevelopment = Boolean(d);
14
14
  }
15
15
 
16
+ /** @import { SelectItem } from './types' */
17
+ class Subquery {
18
+ /** @returns {SelectItem[]} */
19
+ toSubquery() {
20
+ throw new Error('toSubquery 方法未在子类中实现');
21
+ }
22
+ }
23
+
24
+ /** @import { SelectItem } from './types' */
16
25
  /** @typedef {{toWhereValue(): WhereValue[][]}} WhereLike */
17
26
  /** @typedef {WhereLike | Record<string, any> | (WhereLike | Record<string, any>)[]} Wheres */
18
27
  /**
@@ -21,6 +30,7 @@ function setDevelopment(d) {
21
30
  * @prop {null} [or]
22
31
  * @prop {string | {table?: string; field: string} | (string | {table?: string; field: string})[]} field
23
32
  * @prop {any} value
33
+ * @prop {SelectItem[]?} [subquery]
24
34
  * @prop {boolean} [not]
25
35
  * @prop {string?} [operator]
26
36
  */
@@ -111,12 +121,14 @@ function parse(items) {
111
121
  return null;
112
122
  }
113
123
  const [field, operator, value, not] = items;
124
+ const subquery = value instanceof Subquery ? value.toSubquery() : null;
114
125
  // @ts-ignore
115
126
  return [{
116
127
  field,
117
128
  operator,
118
129
  value,
119
- not: Boolean(not)
130
+ not: Boolean(not),
131
+ subquery
120
132
  }];
121
133
  }
122
134
  /**
@@ -658,9 +670,9 @@ function isPseudo(model) {
658
670
  }
659
671
 
660
672
  const now = Symbol();
661
- /** @type {typeof now} now */
673
+ /** @typeof {typeof now} now */
662
674
  const uuid = Symbol();
663
- /** @type {typeof uuid} uuid */
675
+ /** @typeof {typeof uuid} uuid */
664
676
  const increment$1 = Symbol();
665
677
  const decrement$1 = Symbol();
666
678
  const multiply$1 = Symbol();
@@ -929,7 +941,7 @@ function toFieldList(list, fieldColumns) {
929
941
  return list.map(it => Object.fromEntries(map.map(([k, v]) => [k, it[v]])));
930
942
  }
931
943
 
932
- /** @import { Environment, FieldDefine, Fields, IConnection, Skip, TableDefine } from '../types' */
944
+ /** @import { Environment, FieldDefine, Fields, Skip, TableConnection, TableDefine } from '../types' */
933
945
  /**
934
946
  *
935
947
  * @param {any} p
@@ -960,7 +972,7 @@ function defaultMap(value, others) {
960
972
  }
961
973
  /**
962
974
  *
963
- * @param {IConnection} conn
975
+ * @param {TableConnection} conn
964
976
  * @param {Environment} env
965
977
  * @param {[string, FieldDefine<TableDefine>][]} fields
966
978
  * @param {Record<string, any>[]} newList
@@ -1062,7 +1074,7 @@ async function createSub(conn, env, fields, newList, list, skip) {
1062
1074
  // TODO: 嵌套
1063
1075
  /**
1064
1076
  *
1065
- * @param {IConnection} conn
1077
+ * @param {TableConnection} conn
1066
1078
  * @param {Environment} env
1067
1079
  * @param {TableDefine} model
1068
1080
  * @param {any} p
@@ -1320,10 +1332,10 @@ function* getFixedValueWhere(columns, ignore) {
1320
1332
  }
1321
1333
  }
1322
1334
 
1323
- /** @import { Environment, FieldDefine, IConnection, Queryable, TableDefine } from '../types' */
1335
+ /** @import { Environment, FieldDefine, Queryable, TableConnection, TableDefine } from '../types' */
1324
1336
  /**
1325
1337
  *
1326
- * @param {IConnection} conn
1338
+ * @param {TableConnection} conn
1327
1339
  * @param {any[]} list
1328
1340
  * @param {[string, FieldDefine<TableDefine>][]} fields
1329
1341
  * @param {Set<string>} baseFieldSet
@@ -1381,7 +1393,7 @@ async function deleteSub(conn, list, fields, baseFieldSet, env, hasResult) {
1381
1393
  }
1382
1394
  // TODO: 嵌套
1383
1395
  /**
1384
- * @param {IConnection} conn
1396
+ * @param {TableConnection} conn
1385
1397
  * @param {Queryable} queryable
1386
1398
  * @param {Environment} env
1387
1399
  * @returns {Promise<number>}
@@ -1486,7 +1498,7 @@ async function getSetValue$1(columns, fieldColumns, update, skip) {
1486
1498
 
1487
1499
  /* eslint-disable max-lines */
1488
1500
  /* eslint-disable no-labels */
1489
- /** @import { Constraint, Environment, FieldDefine, IConnection, Skip, TableDefine } from '../types' */
1501
+ /** @import { Constraint, Environment, FieldDefine, Skip, TableConnection, TableDefine } from '../types' */
1490
1502
  /** @import { WhereValue } from '../Where.mjs' */
1491
1503
  /**
1492
1504
  *
@@ -1604,7 +1616,7 @@ function separation(newList, oldList, primaryKeys, commonFilterKeys, noField) {
1604
1616
  }
1605
1617
  /**
1606
1618
  *
1607
- * @param {IConnection} conn
1619
+ * @param {TableConnection} conn
1608
1620
  * @param {Environment} env
1609
1621
  * @param {[Record<string, any>, Record<string, any>, Record<string, any>][]} updatedOldNewList
1610
1622
  * @param {string} key
@@ -1810,7 +1822,7 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
1810
1822
  }
1811
1823
  /**
1812
1824
  *
1813
- * @param {IConnection} conn
1825
+ * @param {TableConnection} conn
1814
1826
  * @param {[string, FieldDefine<TableDefine>][]} fields
1815
1827
  * @param {[Record<string, any>, Record<string, any>, Record<string, any>][]} list
1816
1828
  * @param {Environment} env
@@ -1838,7 +1850,7 @@ async function updateSub(conn, fields, list, env, skip) {
1838
1850
  }
1839
1851
  /**
1840
1852
  *
1841
- * @param {IConnection} conn
1853
+ * @param {TableConnection} conn
1842
1854
  * @param {TableDefine} model
1843
1855
  * @param {Record<string, any>} data
1844
1856
  * @param {Record<string, any>} newData
@@ -1920,10 +1932,10 @@ async function setDeleteTimestamp(columns, update) {
1920
1932
 
1921
1933
  /** @import { WhereValue } from '../Where.mjs' */
1922
1934
  /** @import { SetValue } from '../set.mjs' */
1923
- /** @import { Environment, IConnection, TableDefine } from '../types' */
1935
+ /** @import { Environment, TableDefine, TableConnection } from '../types' */
1924
1936
  /**
1925
1937
  *
1926
- * @param {IConnection} conn
1938
+ * @param {TableConnection} conn
1927
1939
  * @param {TableDefine} tableDef
1928
1940
  * @param {any} data
1929
1941
  * @param {Record<string, SetValue>} update
@@ -1961,10 +1973,10 @@ async function pseudoDestroy (conn, tableDef, data, update, env) {
1961
1973
  return toFieldList(dataUpdated, fieldColumns)[0] || null;
1962
1974
  }
1963
1975
 
1964
- /** @import { IConnection, TableDefine, Environment } from '../types' */
1976
+ /** @import { TableDefine, Environment, TableConnection } from '../types' */
1965
1977
  /**
1966
1978
  *
1967
- * @param {IConnection} conn
1979
+ * @param {TableConnection} conn
1968
1980
  * @param {TableDefine} tableDef
1969
1981
  * @param {any} data
1970
1982
  * @param {Environment} env
@@ -2068,7 +2080,7 @@ function sort2column(sort, fieldColumns) {
2068
2080
  }
2069
2081
 
2070
2082
  /** @import Connection from './index.mjs' */
2071
- /** @import { Environment, FieldDefine, Fields, IConnection, Skip, TableDefine, FieldType } from '../types' */
2083
+ /** @import { Environment, FieldDefine, Fields, Skip, TableDefine, FieldType, TableConnection, SelectItem } from '../types' */
2072
2084
  /**
2073
2085
  * @param {string[] | boolean | null} [items]
2074
2086
  */
@@ -2121,7 +2133,7 @@ function getSelectFields(select, columns, tableFields, baseFieldSet) {
2121
2133
  }
2122
2134
  /**
2123
2135
  *
2124
- * @param {IConnection} conn
2136
+ * @param {TableConnection} conn
2125
2137
  * @param {any[]} list
2126
2138
  * @param {[string, FieldDefine<TableDefine>][]} fields
2127
2139
  * @param {Set<string>} baseFieldSet
@@ -2162,16 +2174,18 @@ async function findSub(conn, list, fields, baseFieldSet, env, returnFields) {
2162
2174
  const where = [subList2where(list, mapList, fieldColumns), ...getValueWhere(constraints, columns, fieldColumns)];
2163
2175
  const selected = fieldMap && getSelectFields(fieldMap[''] || [], columns, tableFields, baseFieldSet) || null;
2164
2176
  const select = select2column(columns, fieldColumns, selected && [...selected, ...mapList.map(v => v[0])]);
2165
- /** @type {PromiseLike<object[]>} */
2166
- const result = table && typeof table === 'object' ? table.find(env, conn, columns, {
2167
- where,
2168
- sort: getDefaultSort(columns, noColumn),
2169
- select
2170
- }) : conn.find(env, table, columns, {
2177
+ /** @type {SelectItem[]} */
2178
+ const selects = [{
2179
+ table: typeof table === 'string' ? table : '',
2180
+ columns,
2181
+ select,
2171
2182
  where,
2172
- sort: getDefaultSort(columns, noColumn),
2173
- select
2174
- });
2183
+ sort: getDefaultSort(columns, noColumn) || [],
2184
+ group: [],
2185
+ having: []
2186
+ }];
2187
+ /** @type {PromiseLike<object[]>} */
2188
+ const result = table && typeof table === 'object' ? table.select(env, conn, selects) : conn.select(env, selects);
2175
2189
  let promise = result.then(list => findSub(conn, list, tableFields, new Set(Object.keys(fieldColumns || columns)), env, fieldMap));
2176
2190
  promises.push(array ? promise.then(subList => {
2177
2191
  for (const item of list) {
@@ -2188,7 +2202,7 @@ async function findSub(conn, list, fields, baseFieldSet, env, returnFields) {
2188
2202
  /**
2189
2203
  *
2190
2204
  * @template {TableDefine} T
2191
- * @param {IConnection} conn
2205
+ * @param {TableConnection} conn
2192
2206
  * @param {Connection} that
2193
2207
  * @param {T} p
2194
2208
  * @param {Environment} env
@@ -2220,19 +2234,19 @@ async function search(conn, that, p, env, returnFields, skip) {
2220
2234
  const selected = fieldMap && getSelectFields(fieldMap[''] || [], columns, tableFields, baseFieldSet) || null;
2221
2235
  const select = select2column(columns, fieldColumns, selected);
2222
2236
  const sorted = sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns);
2223
- const list = table && typeof table === 'object' ? await table.find(env, conn, columns, {
2224
- where,
2225
- offset,
2226
- limit,
2237
+ /** @type {SelectItem[]} */
2238
+ const selects = [{
2239
+ table: typeof table === 'string' ? table : '',
2240
+ columns,
2227
2241
  select,
2228
- sort: sorted
2229
- }) : await conn.find(env, table || '', columns, {
2230
2242
  where,
2231
2243
  offset,
2232
2244
  limit,
2233
- select,
2234
- sort: sorted
2235
- });
2245
+ sort: sorted || [],
2246
+ group: [],
2247
+ having: []
2248
+ }];
2249
+ const list = table && typeof table === 'object' ? await table.select(env, conn, selects) : await conn.select(env, selects);
2236
2250
  if (!list.length) {
2237
2251
  return [];
2238
2252
  }
@@ -2243,11 +2257,11 @@ async function search(conn, that, p, env, returnFields, skip) {
2243
2257
  }
2244
2258
 
2245
2259
  /** @import Connection from './index.mjs' */
2246
- /** @import { Environment, TableDefine, IConnection, Skip } from '../types' */
2260
+ /** @import { Environment, TableDefine, Skip, TableConnection, SelectItem } from '../types' */
2247
2261
  /**
2248
2262
  *
2249
2263
  * @template {TableDefine} T
2250
- * @param {IConnection} conn
2264
+ * @param {TableConnection} conn
2251
2265
  * @param {Connection} that
2252
2266
  * @param {T} p
2253
2267
  * @param {Environment} env
@@ -2271,17 +2285,18 @@ async function first(conn, that, p, env, skip) {
2271
2285
  if (whereHook) {
2272
2286
  where.push(...where2column(Where.parse(whereHook), fieldColumns));
2273
2287
  }
2274
- const list = table && typeof table === 'object' ? await table.find(env, conn, columns, {
2275
- where,
2276
- limit: 1,
2277
- sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
2278
- select: select2column(columns, fieldColumns)
2279
- }) : await conn.find(env, table || '', columns, {
2288
+ /** @type {SelectItem[]} */
2289
+ const selects = [{
2290
+ table: typeof table === 'string' ? table : '',
2291
+ columns,
2280
2292
  where,
2281
2293
  limit: 1,
2282
- sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
2283
- select: select2column(columns, fieldColumns)
2284
- });
2294
+ sort: sort?.length ? sort2column(sort, fieldColumns) || [] : getDefaultSort(columns) || [],
2295
+ select: select2column(columns, fieldColumns),
2296
+ group: [],
2297
+ having: []
2298
+ }];
2299
+ const list = table && typeof table === 'object' ? await table.select(env, conn, selects) : await conn.select(env, selects);
2285
2300
  const baseFieldSet = new Set(Object.keys(fieldColumns || columns));
2286
2301
  const [item] = await findSub(conn, list, tableFields, baseFieldSet, env);
2287
2302
  if (!item) {
@@ -2293,9 +2308,9 @@ async function first(conn, that, p, env, skip) {
2293
2308
  return p[Build](item, true, env);
2294
2309
  }
2295
2310
 
2296
- /** @import { Environment, IConnection, Queryable } from '../types' */
2311
+ /** @import { Environment, Queryable, TableConnection } from '../types' */
2297
2312
  /**
2298
- * @param {IConnection} conn
2313
+ * @param {TableConnection} conn
2299
2314
  * @param {Queryable} queryable
2300
2315
  * @param {Record<string, any> | boolean | undefined} update
2301
2316
  * @param {Environment} env
@@ -2451,6 +2466,43 @@ function table2db(tables) {
2451
2466
  return allTables;
2452
2467
  }
2453
2468
 
2469
+ /** @import { Fields, Queryable, SelectItem } from '../types' */
2470
+ /**
2471
+ * @template {Fields} T
2472
+ * @param {Queryable<T>} queryable
2473
+ * @returns {SelectItem[]}
2474
+ */
2475
+ function toSelect(queryable) {
2476
+ const [columns, fieldColumns, tableFields] = toColumns(queryable.fields);
2477
+ const {
2478
+ table
2479
+ } = queryable;
2480
+ const options = getOptions(queryable);
2481
+ const {
2482
+ offset,
2483
+ limit
2484
+ } = options;
2485
+ const where = where2column(options.where, fieldColumns);
2486
+ where.push(...getFixedValueWhere(columns));
2487
+ const select = select2column(columns, fieldColumns, options.select);
2488
+ const {
2489
+ sort
2490
+ } = options;
2491
+ /** @type {SelectItem[]} */
2492
+ const result = [{
2493
+ table: typeof table === 'string' ? table : '',
2494
+ columns,
2495
+ offset,
2496
+ limit,
2497
+ where,
2498
+ select,
2499
+ sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
2500
+ group: options.group || [],
2501
+ having: []
2502
+ }];
2503
+ return result;
2504
+ }
2505
+
2454
2506
  /* eslint-disable max-lines */
2455
2507
  /* eslint-disable max-len */
2456
2508
  /**
@@ -2495,6 +2547,8 @@ class Connection {
2495
2547
  #env;
2496
2548
  /** @type {() => Promise<IConnection<E>>} */
2497
2549
  #getConnection;
2550
+ /** @type {(t: TableConnect<E> | null | undefined) => Promise<TableConnection<E>>} */
2551
+ #getTableConnection;
2498
2552
  /** @type {E?} */
2499
2553
  #baseTransaction;
2500
2554
  /**
@@ -2505,6 +2559,7 @@ class Connection {
2505
2559
  constructor(connection, transaction = null) {
2506
2560
  this.#baseTransaction = transaction;
2507
2561
  this.#getConnection = async () => connection;
2562
+ this.#getTableConnection = async t => typeof t === 'function' ? t(await connection) : connection;
2508
2563
  this.#env = {
2509
2564
  isDevelopment,
2510
2565
  values,
@@ -2635,7 +2690,7 @@ class Connection {
2635
2690
  if (list.length !== 1) {
2636
2691
  const modelBuild = model[Build];
2637
2692
  const md = typeof modelBuild === 'function' ? modelBuild.bind(model) : undefined;
2638
- const conn = await this.#getConnection();
2693
+ const conn = await this.#getTableConnection(model.connect);
2639
2694
  let setData = list;
2640
2695
  if (typeof beforeCreateMany === 'function') {
2641
2696
  // @ts-ignore
@@ -2651,7 +2706,7 @@ class Connection {
2651
2706
  if (typeof model[Create] !== 'function') {
2652
2707
  const modelBuild = model[Build];
2653
2708
  const md = typeof modelBuild === 'function' ? modelBuild.bind(model) : undefined;
2654
- const conn = await this.#getConnection();
2709
+ const conn = await this.#getTableConnection(model.connect);
2655
2710
  let setData = data;
2656
2711
  if (typeof beforeCreate === 'function') {
2657
2712
  setData = (await beforeCreate.call(setData, this, model, setData)) || setData;
@@ -2666,7 +2721,7 @@ class Connection {
2666
2721
  const m = /** @type {Create} */model;
2667
2722
  const result = m[Create](this, data, async (data, that) => {
2668
2723
  let setData = data;
2669
- const conn = await this.#getConnection();
2724
+ const conn = await this.#getTableConnection(model.connect);
2670
2725
  if (typeof beforeCreate === 'function') {
2671
2726
  setData = (await beforeCreate.call(that ?? setData, this, model, setData)) || setData;
2672
2727
  }
@@ -2688,9 +2743,10 @@ class Connection {
2688
2743
  */
2689
2744
  async search(sql, fields, skip) {
2690
2745
  const returnFields = Array.isArray(fields) ? fields : [];
2691
- const conn = await this.#getConnection();
2746
+ const conn = await this.#getTableConnection(sql.connect);
2747
+ const result = search(conn, this, sql, this.#env, returnFields, skip);
2692
2748
  // @ts-ignore
2693
- return search(conn, this, sql, this.#env, returnFields, skip);
2749
+ return result;
2694
2750
  }
2695
2751
  /**
2696
2752
  *
@@ -2700,7 +2756,7 @@ class Connection {
2700
2756
  * @returns {Promise<T extends Build<infer R > ? R[] : T extends TableDefine<infer F> ? FieldValue<F>[] : never>}
2701
2757
  */
2702
2758
  async find(sql, skip) {
2703
- const conn = await this.#getConnection();
2759
+ const conn = await this.#getTableConnection(sql.connect);
2704
2760
  const env = this.#env;
2705
2761
  /** @type {any} */
2706
2762
  const result = await search(conn, this, sql, env, true, skip);
@@ -2719,7 +2775,7 @@ class Connection {
2719
2775
  * @returns {Promise<T extends Build<infer R > ? R : T extends TableDefine<infer F> ? FieldValue<F> : never>}
2720
2776
  */
2721
2777
  async first(sql, skip) {
2722
- const conn = await this.#getConnection();
2778
+ const conn = await this.#getTableConnection(sql.connect);
2723
2779
  return first(conn, this, sql, this.#env, skip);
2724
2780
  }
2725
2781
  /**
@@ -2738,12 +2794,13 @@ class Connection {
2738
2794
  afterUpdate
2739
2795
  } = model.hooks || {};
2740
2796
  if (typeof data[Save] !== 'function') {
2741
- const conn = await this.#getConnection();
2797
+ const conn = await this.#getTableConnection(model.connect);
2742
2798
  if (newData) {
2743
2799
  let setData = newData;
2744
2800
  if (typeof beforeUpdate === 'function') {
2801
+ const result = (await beforeUpdate.call(data, this, model, data, setData)) || setData;
2745
2802
  // @ts-ignore
2746
- setData = (await beforeUpdate.call(data, this, model, data, setData)) || setData;
2803
+ setData = result;
2747
2804
  }
2748
2805
  // @ts-ignore
2749
2806
  const r = await updateData(conn, model, data, setData, this.#env, skip);
@@ -2771,7 +2828,7 @@ class Connection {
2771
2828
  const that = /** @type {Save}*/data;
2772
2829
  // @ts-ignore
2773
2830
  return that[Save](this, async (data, newData) => {
2774
- const conn = await this.#getConnection();
2831
+ const conn = await this.#getTableConnection(model.connect);
2775
2832
  if (newData) {
2776
2833
  let setData = newData;
2777
2834
  if (typeof beforeUpdate === 'function') {
@@ -2809,7 +2866,7 @@ class Connection {
2809
2866
  afterDelete
2810
2867
  } = tableDef.hooks || {};
2811
2868
  if (typeof data[Destroy] !== 'function') {
2812
- const conn = await this.#getConnection();
2869
+ const conn = await this.#getTableConnection(tableDef.connect);
2813
2870
  if (typeof beforeDelete === 'function') {
2814
2871
  // @ts-ignore
2815
2872
  await beforeDelete.call(data, this, tableDef, data, null);
@@ -2824,7 +2881,7 @@ class Connection {
2824
2881
  const that = /** @type {Destroy} */data;
2825
2882
  // @ts-ignore
2826
2883
  return that[Destroy](this, async data => {
2827
- const conn = await this.#getConnection();
2884
+ const conn = await this.#getTableConnection(tableDef.connect);
2828
2885
  if (typeof beforeDelete === 'function') {
2829
2886
  await beforeDelete.call(that, this, tableDef, data, null);
2830
2887
  }
@@ -2855,7 +2912,7 @@ class Connection {
2855
2912
  ...value
2856
2913
  };
2857
2914
  if (typeof data[PseudoDestroy] !== 'function') {
2858
- const conn = await this.#getConnection();
2915
+ const conn = await this.#getTableConnection(tableDef.connect);
2859
2916
  if (typeof beforeDelete === 'function') {
2860
2917
  // @ts-ignore
2861
2918
  await beforeDelete.call(data, this, tableDef, data, update);
@@ -2871,7 +2928,7 @@ class Connection {
2871
2928
  const that = /** @type {PseudoDestroy} */data;
2872
2929
  // @ts-ignore
2873
2930
  return that[PseudoDestroy](this, update, async data => {
2874
- const conn = await this.#getConnection();
2931
+ const conn = await this.#getTableConnection(tableDef.connect);
2875
2932
  if (typeof beforeDelete === 'function') {
2876
2933
  await beforeDelete.call(that, this, tableDef, data, update);
2877
2934
  }
@@ -2927,7 +2984,7 @@ class Connection {
2927
2984
  * @returns {Promise<number>}
2928
2985
  */
2929
2986
  async destroyMany(queryable, update) {
2930
- const conn = await this.#getConnection();
2987
+ const conn = await this.#getTableConnection(queryable.connect);
2931
2988
  if (update === false || !isPseudo(queryable)) {
2932
2989
  return deleteMany(conn, queryable, this.#env);
2933
2990
  }
@@ -3003,6 +3060,7 @@ class Connection {
3003
3060
  * @returns {Promise<T | T[]>}
3004
3061
  */
3005
3062
  async insert({
3063
+ connect,
3006
3064
  table,
3007
3065
  fields
3008
3066
  }, data, keys, ignoreConflict) {
@@ -3025,7 +3083,7 @@ class Connection {
3025
3083
  insertKeys = Object.entries(columns).map(v => v[0]);
3026
3084
  }
3027
3085
  const ignore = Boolean(Array.isArray(keys) ? ignoreConflict : keys);
3028
- const conn = await this.#getConnection();
3086
+ const conn = await this.#getTableConnection(connect);
3029
3087
  const result = table && typeof table === 'object' ? await table.insert(this.#env, conn, columns, insertValues, insertKeys, ignore) : await conn.insert(this.#env, table || '', columns, insertValues, insertKeys, ignore);
3030
3088
  const list = toFieldList(result, fieldColumns);
3031
3089
  // @ts-ignore
@@ -3125,6 +3183,7 @@ class Connection {
3125
3183
  * @returns {Promise<T | T[]>}
3126
3184
  */
3127
3185
  async upsert({
3186
+ connect,
3128
3187
  table,
3129
3188
  fields
3130
3189
  }, data, keys, conflictKeys, conflictSet) {
@@ -3148,7 +3207,7 @@ class Connection {
3148
3207
  insertKeys = Object.entries(columns).map(v => v[0]);
3149
3208
  }
3150
3209
  const conflict = Array.isArray(conflictKeys) || conflictKeys === true ? conflictKeys : baseFields.filter(([, v]) => v.primary).sort(([, a], [, b]) => Number(a.primary) - Number(b.primary)).map(([v]) => v);
3151
- const conn = await this.#getConnection();
3210
+ const conn = await this.#getTableConnection(connect);
3152
3211
  const result = table && typeof table === 'object' ? await table.upsert(this.#env, conn, columns, insertValues, insertKeys, conflict, set) : await conn.upsert(this.#env, table || '', columns, insertValues, insertKeys, conflict, set);
3153
3212
  const list = toFieldList(result, fieldColumns);
3154
3213
  // @ts-ignore
@@ -3162,11 +3221,12 @@ class Connection {
3162
3221
  * @returns {Promise<number>}
3163
3222
  */
3164
3223
  async update({
3224
+ connect,
3165
3225
  table,
3166
3226
  fields
3167
3227
  }, update, where, skip) {
3168
3228
  const [columns, fieldColumns, tableFields] = toColumns(fields);
3169
- const conn = await this.#getConnection();
3229
+ const conn = await this.#getTableConnection(connect);
3170
3230
  const whereColumns = where2column(where ? Where.parse(where) : [], fieldColumns);
3171
3231
  whereColumns.push(...getFixedValueWhere(columns));
3172
3232
  return table && typeof table === 'object' ? table.update(this.#env, conn, columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns) : conn.update(this.#env, table || '', columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns);
@@ -3181,11 +3241,12 @@ class Connection {
3181
3241
  * @returns {Promise<any[]>}
3182
3242
  */
3183
3243
  async updateReturn({
3244
+ connect,
3184
3245
  table,
3185
3246
  fields
3186
3247
  }, update, returning, where, skip) {
3187
3248
  const [columns, fieldColumns, tableFields] = toColumns(fields);
3188
- const conn = await this.#getConnection();
3249
+ const conn = await this.#getTableConnection(connect);
3189
3250
  const whereColumns = where2column(where ? Where.parse(where) : [], fieldColumns);
3190
3251
  whereColumns.push(...getFixedValueWhere(columns));
3191
3252
  const result = table && typeof table === 'object' ? await table.updateReturn(this.#env, conn, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns) : await conn.updateReturn(this.#env, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), table || '', columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns);
@@ -3200,13 +3261,14 @@ class Connection {
3200
3261
  * @returns {Promise<number>}
3201
3262
  */
3202
3263
  async updateMany({
3264
+ connect,
3203
3265
  table,
3204
3266
  fields
3205
3267
  }, pKeys, setKeys, list, skip) {
3206
3268
  const [columns, fieldColumns, tableFields] = toColumns(fields);
3207
3269
  const update = await getSetValue$1(columns, fieldColumns, null, skip);
3208
3270
  const [whereColumns, setColumns] = getKeys(columns, update, field2column(pKeys, fieldColumns), field2column(setKeys, fieldColumns));
3209
- const conn = await this.#getConnection();
3271
+ const conn = await this.#getTableConnection(connect);
3210
3272
  return table && typeof table === 'object' ? table.updateMany(this.#env, conn, columns, update, whereColumns, setColumns, toColumnList(list, fieldColumns), []) : conn.updateMany(this.#env, table || '', columns, update, whereColumns, setColumns, list, []);
3211
3273
  }
3212
3274
  /**
@@ -3220,13 +3282,14 @@ class Connection {
3220
3282
  * @returns {Promise<any[]>}
3221
3283
  */
3222
3284
  async updateManyReturn({
3285
+ connect,
3223
3286
  table,
3224
3287
  fields
3225
3288
  }, pKeys, setKeys, list, returning, skip) {
3226
3289
  const [columns, fieldColumns, tableFields] = toColumns(fields);
3227
3290
  const update = getSetValue$1(columns, fieldColumns, null, skip);
3228
3291
  const [whereColumns, setColumns] = getKeys(columns, update, field2column(pKeys, fieldColumns), field2column(setKeys, fieldColumns));
3229
- const conn = await this.#getConnection();
3292
+ const conn = await this.#getTableConnection(connect);
3230
3293
  const result = table && typeof table === 'object' ? await table.updateManyReturn(this.#env, conn, columns, update, whereColumns, setColumns, toColumnList(list, fieldColumns), [], returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns)) : await conn.updateManyReturn(this.#env, table || '', columns, update, whereColumns, setColumns, toColumnList(list, fieldColumns), [], returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns));
3231
3294
  return toFieldList(result, fieldColumns);
3232
3295
  }
@@ -3236,11 +3299,12 @@ class Connection {
3236
3299
  * @returns {Promise<number>}
3237
3300
  */
3238
3301
  async delete({
3302
+ connect,
3239
3303
  table,
3240
3304
  fields
3241
3305
  }, where) {
3242
3306
  const [columns, fieldColumns, tableFields] = toColumns(fields);
3243
- const conn = await this.#getConnection();
3307
+ const conn = await this.#getTableConnection(connect);
3244
3308
  const whereColumns = where2column(where ? Where.parse(where) : [], fieldColumns);
3245
3309
  whereColumns.push(...getFixedValueWhere(columns));
3246
3310
  return table && typeof table === 'object' ? table.delete(this.#env, conn, columns, whereColumns) : conn.delete(this.#env, table || '', columns, whereColumns);
@@ -3253,11 +3317,12 @@ class Connection {
3253
3317
  * @returns {Promise<any[]>}
3254
3318
  */
3255
3319
  async deleteReturn({
3320
+ connect,
3256
3321
  table,
3257
3322
  fields
3258
3323
  }, returning, where) {
3259
3324
  const [columns, fieldColumns, tableFields] = toColumns(fields);
3260
- const conn = await this.#getConnection();
3325
+ const conn = await this.#getTableConnection(connect);
3261
3326
  const whereColumns = where2column(where ? Where.parse(where) : [], fieldColumns);
3262
3327
  whereColumns.push(...getFixedValueWhere(columns));
3263
3328
  const result = table && typeof table === 'object' ? await table.deleteReturn(this.#env, conn, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), columns, whereColumns) : await conn.deleteReturn(this.#env, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), table || '', columns, whereColumns);
@@ -3271,12 +3336,13 @@ class Connection {
3271
3336
  async count(queryable) {
3272
3337
  const [columns, fieldColumns, tableFields] = toColumns(queryable.fields);
3273
3338
  const {
3339
+ connect,
3274
3340
  table
3275
3341
  } = queryable;
3276
3342
  const options = getOptions(queryable);
3277
3343
  const where = where2column(options.where, fieldColumns);
3278
3344
  where.push(...getFixedValueWhere(columns));
3279
- const conn = await this.#getConnection();
3345
+ const conn = await this.#getTableConnection(connect);
3280
3346
  return table && typeof table === 'object' ? table.count(this.#env, conn, columns, where) : conn.count(this.#env, table || '', columns, where);
3281
3347
  }
3282
3348
  /**
@@ -3284,43 +3350,12 @@ class Connection {
3284
3350
  * @returns {Promise<any[]>}
3285
3351
  */
3286
3352
  async select(queryable) {
3287
- const [columns, fieldColumns, tableFields] = toColumns(queryable.fields);
3288
3353
  const {
3289
3354
  table
3290
3355
  } = queryable;
3291
- const options = getOptions(queryable);
3292
- const {
3293
- offset,
3294
- limit
3295
- } = options;
3296
- const where = where2column(options.where, fieldColumns);
3297
- where.push(...getFixedValueWhere(columns));
3298
- const conn = await this.#getConnection();
3299
- const select = select2column(columns, fieldColumns, options.select);
3300
- const {
3301
- sort
3302
- } = options;
3303
- const result = table && typeof table === 'object' ? await table.select(this.#env, conn, [{
3304
- table: '',
3305
- columns,
3306
- offset,
3307
- limit,
3308
- where,
3309
- select,
3310
- sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
3311
- group: options.group || [],
3312
- having: []
3313
- }]) : await conn.select(this.#env, [{
3314
- table: table || '',
3315
- columns,
3316
- offset,
3317
- limit,
3318
- where,
3319
- select,
3320
- sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
3321
- group: options.group || [],
3322
- having: []
3323
- }]);
3356
+ const conn = await this.#getTableConnection(queryable.connect);
3357
+ const selects = toSelect(queryable);
3358
+ const result = table && typeof table === 'object' ? await table.select(this.#env, conn, selects) : await conn.select(this.#env, selects);
3324
3359
  return result;
3325
3360
  }
3326
3361
  /**
@@ -4747,7 +4782,7 @@ function coefficient(...value) {
4747
4782
  };
4748
4783
  }
4749
4784
 
4750
- /** @import { Fields, Options, Queryable, Select, VirtualTable } from './types' */
4785
+ /** @import { Fields, Options, Queryable, Select, TableConnect, VirtualTable } from './types' */
4751
4786
  /** @import { WhereValue, Wheres } from './Where.mjs' */
4752
4787
  /**
4753
4788
  *
@@ -4802,6 +4837,7 @@ function selectFn(fn, select, newSelect) {
4802
4837
  * @template {Fields} T
4803
4838
  * @template {object} [TB=object]
4804
4839
  * @typedef {object} QueryOptions
4840
+ * @property {TableConnect<any>?} [connect]
4805
4841
  * @property {string | VirtualTable} [table]
4806
4842
  * @property {T} fields
4807
4843
  * @property {string} [pseudo]
@@ -4812,7 +4848,7 @@ function selectFn(fn, select, newSelect) {
4812
4848
  * @template {object} [TB=object]
4813
4849
  * @implements {Queryable<T>}
4814
4850
  */
4815
- class Query {
4851
+ class Query extends Subquery {
4816
4852
  /**
4817
4853
  * @template {Fields} F
4818
4854
  * @template {new (o: QueryOptions<F>) => Query<F>} T
@@ -4830,6 +4866,8 @@ class Query {
4830
4866
  pseudo
4831
4867
  });
4832
4868
  }
4869
+ /** @readonly @type {TableConnect | null | undefined} */
4870
+
4833
4871
  /** @readonly @type {string | VirtualTable | undefined} */
4834
4872
 
4835
4873
  /** @readonly @type {T} */
@@ -4847,12 +4885,17 @@ class Query {
4847
4885
  get options() {
4848
4886
  return this.#options;
4849
4887
  }
4888
+ toSubquery() {
4889
+ return toSelect(this);
4890
+ }
4850
4891
  /**
4851
4892
  * @param {QueryOptions<T, TB> | Query<T, TB>} options
4852
4893
  * @param {boolean} [noBuild]
4853
4894
  */
4854
4895
  constructor(options, noBuild) {
4896
+ super();
4855
4897
  if (options instanceof Query) {
4898
+ this.connect = options.connect;
4856
4899
  this.table = options.table;
4857
4900
  this.fields = options.fields;
4858
4901
  this.pseudo = options.pseudo;
@@ -4864,11 +4907,13 @@ class Query {
4864
4907
  this.#primary = options.#primary;
4865
4908
  } else {
4866
4909
  const {
4910
+ connect,
4867
4911
  table,
4868
4912
  fields,
4869
4913
  pseudo,
4870
4914
  build
4871
4915
  } = options;
4916
+ this.connect = connect;
4872
4917
  this.table = table;
4873
4918
  this.fields = fields;
4874
4919
  this.pseudo = typeof pseudo === 'string' ? pseudo : '';