orchid-orm 1.34.7 → 1.34.9

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
@@ -1,6 +1,6 @@
1
- import { Query, SelectableFromShape, CreateMethodsNames, CreateData, AddQueryDefaults, RelationConfigBase, UpdateData, WhereArg, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, Adapter, FromArg, FromResult, AdapterOptions, DbSharedOptions, RelationJoinQuery, RelationQuery, RelationsBase, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, QueryData, QueryBase, TableDataFn, DbTableOptionScopes, SqlMethod, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery } from 'pqb';
1
+ import { Query, SelectableFromShape, CreateMethodsNames, CreateData, AddQueryDefaults, RelationConfigBase, UpdateData, WhereArg, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, Adapter, FromArg, FromResult, AdapterOptions, DbSharedOptions, RelationJoinQuery, RelationQuery, RelationsBase, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, QueryData, QueryBase, TableDataFn, DbTableOptionScopes, RawSQL, DynamicRawSQL, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery } from 'pqb';
2
2
  export * from 'pqb';
3
- import { ColumnsShapeBase, EmptyObject, MaybeArray, RecordUnknown, Simplify, ColumnShapeOutput, ColumnShapeInput, ColumnShapeInputPartial, CoreQueryScopes, ColumnSchemaConfig, QueryColumns, QueryReturnType } from 'orchid-core';
3
+ import { ColumnsShapeBase, EmptyObject, MaybeArray, RecordUnknown, Simplify, ColumnShapeOutput, ColumnShapeInput, ColumnShapeInputPartial, CoreQueryScopes, ColumnSchemaConfig, StaticSQLArgs, QueryColumn, DynamicSQLArg, QueryColumns, QueryReturnType } from 'orchid-core';
4
4
  export * from 'orchid-core';
5
5
 
6
6
  interface RelationCommonOptions<Related extends TableClass = TableClass, Scope extends Query = Query> {
@@ -461,7 +461,7 @@ interface BaseTableInstance<ColumnTypes> {
461
461
  * Or you can add a computed column on the ORM level, without adding it to the database, in such a way:
462
462
  *
463
463
  * ```ts
464
- * import { BaseTable } from './baseTable';
464
+ * import { BaseTable, sql } from './baseTable';
465
465
  *
466
466
  * export class UserTable extends BaseTable {
467
467
  * readonly table = 'user';
@@ -473,7 +473,7 @@ interface BaseTableInstance<ColumnTypes> {
473
473
  *
474
474
  * computed = this.setComputed({
475
475
  * fullName: (q) =>
476
- * q.sql`${q.column('firstName')} || ' ' || ${q.column('lastName')}`.type(
476
+ * sql`${q.column('firstName')} || ' ' || ${q.column('lastName')}`.type(
477
477
  * (t) => t.string(),
478
478
  * ),
479
479
  * });
@@ -510,6 +510,8 @@ interface BaseTableInstance<ColumnTypes> {
510
510
  * We can define a computed `title` by passing a function into `sql` method:
511
511
  *
512
512
  * ```ts
513
+ * import { sql } from './baseTable';
514
+ *
513
515
  * type Locale = 'en' | 'uk' | 'be';
514
516
  * const asyncLanguageStorage = new AsyncLocalStorage<Locale>();
515
517
  * const defaultLocale: Locale = 'en';
@@ -524,20 +526,18 @@ interface BaseTableInstance<ColumnTypes> {
524
526
  * }));
525
527
  *
526
528
  * computed = this.setComputed({
527
- * title: (q) =>
528
- * q
529
- * // .sql can take a function that accepts `sql` argument and must return SQL
530
- * .sql((sql) => {
531
- * // get locale dynamically based on current storage value
532
- * const locale = asyncLanguageStorage.getStore() || defaultLocale;
533
- *
534
- * // use COALESCE in case when localized title is NULL, use title_en
535
- * return sql`COALESCE(
529
+ * title: () =>
530
+ * // `sql` accepts a callback to generate a new query on every run
531
+ * sql(() => {
532
+ * // get locale dynamically based on current storage value
533
+ * const locale = asyncLanguageStorage.getStore() || defaultLocale;
534
+ *
535
+ * // use COALESCE in case when localized title is NULL, use title_en
536
+ * return sql`COALESCE(
536
537
  * ${q.column(`title_${locale}`)},
537
538
  * ${q.column(`title_${defaultLocale}`)}
538
539
  * )`;
539
- * })
540
- * .type((t) => t.text()),
540
+ * }).type((t) => t.text()),
541
541
  * });
542
542
  * }
543
543
  * ```
@@ -609,11 +609,13 @@ interface BaseTableInstance<ColumnTypes> {
609
609
  afterDelete: AfterSelectableHookMethod;
610
610
  afterDeleteCommit: AfterSelectableHookMethod;
611
611
  }
612
- interface BaseTableClass<SchemaConfig extends ColumnSchemaConfig, ColumnTypes> extends SqlMethod<ColumnTypes> {
612
+ interface BaseTableClass<SchemaConfig extends ColumnSchemaConfig, ColumnTypes> {
613
613
  nowSQL: string | undefined;
614
614
  exportAs: string;
615
615
  columnTypes: ColumnTypes;
616
616
  getFilePath(): string;
617
+ sql<T>(...args: StaticSQLArgs): RawSQL<QueryColumn<T>, ColumnTypes>;
618
+ sql<T>(...args: [DynamicSQLArg<QueryColumn<T>>]): DynamicRawSQL<QueryColumn<T>, ColumnTypes>;
617
619
  new (): BaseTableInstance<ColumnTypes>;
618
620
  instance(): BaseTableInstance<ColumnTypes>;
619
621
  /**
package/dist/index.js CHANGED
@@ -29,6 +29,11 @@ function createBaseTable({
29
29
  this.q = {};
30
30
  this.language = language;
31
31
  }
32
+ static sql(...args) {
33
+ const sql = pqb.raw(...args);
34
+ sql.columnTypes = columnTypes;
35
+ return sql;
36
+ }
32
37
  static inputSchema() {
33
38
  this.instance();
34
39
  return this._inputSchema === void 0 ? this._inputSchema = schemaConfig.inputSchema.call(this) : this._inputSchema;
@@ -139,7 +144,7 @@ function createBaseTable({
139
144
  options
140
145
  };
141
146
  }
142
- }, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a.columnTypes = columnTypes, _a.sql = pqb.SqlMethod.prototype.sql, _a);
147
+ }, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a.columnTypes = columnTypes, _a);
143
148
  orchidCore.applyMixins(base, [pqb.QueryHooks]);
144
149
  base.prototype.types = columnTypes;
145
150
  base.prototype.snakeCase = snakeCase;