orchid-orm 1.12.1 → 1.12.3

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
@@ -69,6 +69,63 @@ type OrchidORM<T extends TableClasses = TableClasses> = {
69
69
  $transaction: typeof transaction;
70
70
  $adapter: Adapter;
71
71
  $queryBuilder: Db;
72
+ /**
73
+ * Use `$query` to perform raw SQL queries.
74
+ *
75
+ * ```ts
76
+ * const value = 1;
77
+ *
78
+ * // it is safe to interpolate inside the backticks (``):
79
+ * const result = await db.$query<{ one: number }>`SELECT ${value} AS one`;
80
+ * // data is inside `rows` array:
81
+ * result.rows[0].one;
82
+ * ```
83
+ *
84
+ * If the query is executing inside a transaction, it will use the transaction connection automatically.
85
+ *
86
+ * ```ts
87
+ * await db.transaction(async () => {
88
+ * // both queries will execute in the same transaction
89
+ * await db.$query`SELECT 1`;
90
+ * await db.$query`SELECT 2`;
91
+ * });
92
+ * ```
93
+ *
94
+ * Alternatively, support a simple SQL string, with optional `values`:
95
+ *
96
+ * Note that the values is a simple array, and the SQL is referring to the values with `$1`, `$2` and so on.
97
+ *
98
+ * ```ts
99
+ * const value = 1;
100
+ *
101
+ * // it is NOT safe to interpolate inside a simple string, use `values` to pass the values.
102
+ * const result = await db.$query<{ one: number }>({
103
+ * raw: 'SELECT $1 AS one',
104
+ * values: [value],
105
+ * });
106
+ * // data is inside `rows` array:
107
+ * result.rows[0].one;
108
+ * ```
109
+ *
110
+ * @param args - SQL template literal, or an object { raw: string, values?: unknown[] }
111
+ */
112
+ $query: Db['query'];
113
+ /**
114
+ * The same as the {@link $query}, but returns an array of arrays instead of objects:
115
+ *
116
+ * ```ts
117
+ * const value = 1;
118
+ *
119
+ * // it is safe to interpolate inside the backticks (``):
120
+ * const result = await db.$queryArrays<[number]>`SELECT ${value} AS one`;
121
+ * // `rows` is an array of arrays:
122
+ * const row = result.rows[0];
123
+ * row[0]; // our value
124
+ * ```
125
+ *
126
+ * @param args - SQL template literal, or an object { raw: string, values?: unknown[] }
127
+ */
128
+ $queryArrays: Db['queryArrays'];
72
129
  $from<Args extends FromArgs<Query>>(...args: Args): FromResult<Query, Args>;
73
130
  $close(): Promise<void>;
74
131
  };
@@ -315,7 +372,7 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
315
372
  }) | undefined): {};
316
373
  check(check: orchid_core.RawSQLBase<orchid_core.ColumnTypeBase<unknown, orchid_core.BaseOperators, unknown, orchid_core.ColumnDataBase>, {}>): {};
317
374
  } : CT;
318
- query: QueryData;
375
+ q: QueryData;
319
376
  filePath: string;
320
377
  result: ColumnsShapeBase;
321
378
  clone<T_20 extends QueryBase>(this: T_20): T_20;
package/dist/index.js CHANGED
@@ -28,7 +28,7 @@ const create = (columnTypes, filePathOrStack, snakeCase, nowSQL, exportAs = "Bas
28
28
  constructor() {
29
29
  this.snakeCase = snakeCase;
30
30
  this.columnTypes = columnTypes;
31
- this.query = {};
31
+ this.q = {};
32
32
  }
33
33
  static getFilePath() {
34
34
  if (filePath)
@@ -141,20 +141,20 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
141
141
  }
142
142
  const relationData = [values];
143
143
  store.belongsTo[key] = relationData;
144
- q.query.wrapInTransaction = true;
144
+ q.q.wrapInTransaction = true;
145
145
  pqb.pushQueryValue(q, "beforeCreate", async (q2) => {
146
146
  const inserted = await this.nestedInsert(
147
147
  q2,
148
148
  relationData.map(([, , data]) => data)
149
149
  );
150
- const { values: values2 } = q2.query;
150
+ const { values: values2 } = q2.q;
151
151
  relationData.forEach(([rowIndex2, columnIndex2], index) => {
152
152
  values2[rowIndex2][columnIndex2] = inserted[index][primaryKey];
153
153
  });
154
154
  });
155
155
  }
156
156
  update(q, ctx, set) {
157
- q.query.wrapInTransaction = true;
157
+ q.q.wrapInTransaction = true;
158
158
  const data = set[this.key];
159
159
  if (this.nestedUpdate(q, set, data, ctx)) {
160
160
  ctx.willSetKeys = true;
@@ -254,14 +254,14 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
254
254
  update[foreignKey] = await query.get(primaryKey)._create(params.create);
255
255
  } else if (params.delete) {
256
256
  const selectQuery = q2.clone();
257
- selectQuery.query.type = void 0;
257
+ selectQuery.q.type = void 0;
258
258
  idsForDelete = await selectQuery._pluck(foreignKey);
259
259
  update[foreignKey] = null;
260
260
  }
261
261
  });
262
262
  const { upsert } = params;
263
263
  if (upsert || params.update || params.delete) {
264
- if (!((_a = q.query.select) == null ? void 0 : _a.includes("*")) && !((_b = q.query.select) == null ? void 0 : _b.includes(foreignKey))) {
264
+ if (!((_a = q.q.select) == null ? void 0 : _a.includes("*")) && !((_b = q.q.select) == null ? void 0 : _b.includes(foreignKey))) {
265
265
  q._select(foreignKey);
266
266
  }
267
267
  }
@@ -316,7 +316,7 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKey, nested
316
316
  store.hasRelation[key].push(values);
317
317
  return;
318
318
  }
319
- q.query.wrapInTransaction = true;
319
+ q.q.wrapInTransaction = true;
320
320
  const relationData = [values];
321
321
  store.hasRelation[key] = relationData;
322
322
  q._afterCreate(
@@ -335,10 +335,10 @@ const hasRelationHandleUpdate = (q, set, key, primaryKey, nestedUpdate) => {
335
335
  const value = set[key];
336
336
  if (!value.set && !("upsert" in value) && (!value.disconnect || Array.isArray(value.disconnect) && value.disconnect.length === 0) && (!value.delete || Array.isArray(value.delete) && value.delete.length === 0) && (!value.update || Array.isArray(value.update.where) && value.update.where.length === 0) && (!value.create || Array.isArray(value.create) && value.create.length === 0))
337
337
  return;
338
- if (!((_a = q.query.select) == null ? void 0 : _a.includes("*")) && !((_b = q.query.select) == null ? void 0 : _b.includes(primaryKey))) {
338
+ if (!((_a = q.q.select) == null ? void 0 : _a.includes("*")) && !((_b = q.q.select) == null ? void 0 : _b.includes(primaryKey))) {
339
339
  q._select(primaryKey);
340
340
  }
341
- q.query.wrapInTransaction = true;
341
+ q.q.wrapInTransaction = true;
342
342
  q._afterUpdate(q.primaryKeys, (rows, q2) => {
343
343
  return nestedUpdate(
344
344
  q2,
@@ -464,8 +464,10 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
464
464
  modifyRelatedQuery(relationQuery) {
465
465
  return (query2) => {
466
466
  const fromQuery = query2.clone();
467
- fromQuery.query.select = fromQuerySelect;
468
- relationQuery.query.values = { from: fromQuery };
467
+ fromQuery.q.select = fromQuerySelect;
468
+ const q = relationQuery.q;
469
+ q.kind = "from";
470
+ q.values = { from: fromQuery };
469
471
  };
470
472
  }
471
473
  };
@@ -661,8 +663,10 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
661
663
  modifyRelatedQuery(relationQuery) {
662
664
  return (query2) => {
663
665
  const fromQuery = query2.clone();
664
- fromQuery.query.select = fromQuerySelect;
665
- relationQuery.query.values = { from: fromQuery };
666
+ fromQuery.q.select = fromQuerySelect;
667
+ const q = relationQuery.q;
668
+ q.kind = "from";
669
+ q.values = { from: fromQuery };
666
670
  };
667
671
  }
668
672
  };
@@ -751,7 +755,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
751
755
  [foreignKey]: data[0][primaryKey]
752
756
  }))
753
757
  );
754
- delete t.query[pqb.toSqlCacheKey];
758
+ delete t.q[pqb.toSqlCacheKey];
755
759
  }
756
760
  if (params.disconnect || params.set) {
757
761
  await t.where(
@@ -763,7 +767,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
763
767
  )
764
768
  )._update({ [foreignKey]: null });
765
769
  if (params.set) {
766
- delete t.query[pqb.toSqlCacheKey];
770
+ delete t.q[pqb.toSqlCacheKey];
767
771
  await t.where(
768
772
  Array.isArray(params.set) ? {
769
773
  OR: params.set
@@ -772,7 +776,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
772
776
  }
773
777
  }
774
778
  if (params.delete || params.update) {
775
- delete t.query[pqb.toSqlCacheKey];
779
+ delete t.q[pqb.toSqlCacheKey];
776
780
  const q = t._where(
777
781
  getWhereForNestedUpdate(
778
782
  data,
@@ -863,7 +867,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
863
867
  [fk]: removeColumnName(table.shape[pk]),
864
868
  [afk]: removeColumnName(query.shape[apk])
865
869
  };
866
- baseQuery.query = __spreadProps$1(__spreadValues$3({}, baseQuery.query), {
870
+ baseQuery.q = __spreadProps$1(__spreadValues$3({}, baseQuery.q), {
867
871
  shape: baseQuery.shape
868
872
  });
869
873
  const subQuery = Object.create(baseQuery);
@@ -896,8 +900,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
896
900
  subQuery,
897
901
  (q) => q._on(associationForeignKeyFull, `${pqb.getQueryAs(toQuery)}.${apk}`)._on(foreignKeyFull, `${pqb.getQueryAs(fromQuery)}.${pk}`)
898
902
  );
899
- join.query.joinedShapes = __spreadProps$1(__spreadValues$3({}, join.query.joinedShapes), {
900
- [fromQuery.query.as || fromQuery.table]: fromQuery.query.shape
903
+ join.q.joinedShapes = __spreadProps$1(__spreadValues$3({}, join.q.joinedShapes), {
904
+ [fromQuery.q.as || fromQuery.table]: fromQuery.q.shape
901
905
  });
902
906
  return join;
903
907
  },
@@ -917,8 +921,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
917
921
  "Creating multiple `hasAndBelongsToMany` records is not yet supported"
918
922
  );
919
923
  }
920
- const fromQuery = ref.query.clone();
921
- fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
924
+ const fromQuery = ref.q.clone();
925
+ fromQuery.q.select = [{ selectAs: { [fk]: pk } }];
922
926
  const createdCount = await subQuery.count()._createFrom(
923
927
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
924
928
  fromQuery,
@@ -931,7 +935,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
931
935
  }
932
936
  });
933
937
  return (q) => {
934
- ref.query = q;
938
+ ref.q = q;
935
939
  };
936
940
  }
937
941
  };
@@ -1118,7 +1122,7 @@ const nestedUpdate = (state) => {
1118
1122
  if (params.set) {
1119
1123
  const j = queryJoinTable(state, data);
1120
1124
  await j._delete();
1121
- delete j.query[pqb.toSqlCacheKey];
1125
+ delete j.q[pqb.toSqlCacheKey];
1122
1126
  const ids = await queryRelatedTable(
1123
1127
  state.relatedTableQuery,
1124
1128
  params.set
@@ -1267,10 +1271,10 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
1267
1271
  } else {
1268
1272
  query._takeOptional();
1269
1273
  }
1270
- query.query.returnsOne = true;
1274
+ query.q.returnsOne = true;
1271
1275
  }
1272
1276
  if (data.virtualColumn) {
1273
- dbTable.shape[relationName] = dbTable.query.shape[relationName] = data.virtualColumn;
1277
+ dbTable.shape[relationName] = dbTable.q.shape[relationName] = data.virtualColumn;
1274
1278
  }
1275
1279
  makeRelationQuery(dbTable, relationName, data, query);
1276
1280
  baseQuery.joinQuery = data.joinQuery;
@@ -1280,7 +1284,7 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
1280
1284
  return originalJoin.apply(this, args);
1281
1285
  } else {
1282
1286
  const q = this.clone();
1283
- q.query.innerJoinLateral = true;
1287
+ q.q.innerJoinLateral = true;
1284
1288
  return q;
1285
1289
  }
1286
1290
  };
@@ -1310,10 +1314,10 @@ const makeRelationQuery = (table, relationName, data, q) => {
1310
1314
  this.baseQuery,
1311
1315
  (q2) => data.reverseJoin(this, toTable)
1312
1316
  );
1313
- query.query.joinedShapes = __spreadValues$2({
1314
- [pqb.getQueryAs(this)]: this.query.shape
1315
- }, this.query.joinedShapes);
1316
- query.query[pqb.relationQueryKey] = {
1317
+ query.q.joinedShapes = __spreadValues$2({
1318
+ [pqb.getQueryAs(this)]: this.q.shape
1319
+ }, this.q.joinedShapes);
1320
+ query.q[pqb.relationQueryKey] = {
1317
1321
  relationName,
1318
1322
  sourceQuery: this,
1319
1323
  relationQuery: toTable,
@@ -1390,7 +1394,7 @@ const orchidORM = (_a, tables) => {
1390
1394
  let transactionStorage;
1391
1395
  let qb;
1392
1396
  if ("db" in options) {
1393
- adapter = options.db.query.adapter;
1397
+ adapter = options.db.q.adapter;
1394
1398
  transactionStorage = options.db.internal.transactionStorage;
1395
1399
  qb = options.db.queryBuilder;
1396
1400
  } else {
@@ -1411,6 +1415,8 @@ const orchidORM = (_a, tables) => {
1411
1415
  $transaction: transaction,
1412
1416
  $adapter: adapter,
1413
1417
  $queryBuilder: qb,
1418
+ $query: (...args) => qb.query(...args),
1419
+ $queryArrays: (...args) => qb.queryArrays(...args),
1414
1420
  $from: (...args) => qb.from(...args),
1415
1421
  $close: () => adapter.close()
1416
1422
  };
@@ -1447,7 +1453,7 @@ const orchidORM = (_a, tables) => {
1447
1453
  const table = tableInstances[key];
1448
1454
  if (table.init) {
1449
1455
  table.init(result);
1450
- Object.assign(result[key].baseQuery.query, table.query);
1456
+ Object.assign(result[key].baseQuery.q, table.q);
1451
1457
  }
1452
1458
  }
1453
1459
  return result;
@@ -1476,7 +1482,7 @@ const createRepo = (table, methods) => {
1476
1482
  const proto = Object.create(q2.baseQuery);
1477
1483
  proto.baseQuery = proto;
1478
1484
  const result = Object.create(proto);
1479
- result.query = pqb.getClonedQueryData(q2.query);
1485
+ result.q = pqb.getClonedQueryData(q2.q);
1480
1486
  if (plainMethods) {
1481
1487
  Object.assign(proto.baseQuery, plainMethods);
1482
1488
  }