pqb 0.18.32 → 0.18.34

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
@@ -591,6 +591,73 @@ type DbResult<ColumnTypes> = Db<string, Record<string, never>, EmptyObject, Colu
591
591
  adapter: Adapter;
592
592
  close: Adapter['close'];
593
593
  };
594
+ /**
595
+ * For the case of using the query builder as a standalone tool, use `createDb` from `pqb` package.
596
+ *
597
+ * As `Orchid ORM` focuses on ORM usage, docs examples mostly demonstrates how to work with ORM-defined tables,
598
+ * but everything that's not related to table relations should also work with `pqb` query builder on its own.
599
+ *
600
+ * It is accepting the same options as `orchidORM` + options of `createBaseTable`:
601
+ *
602
+ * ```ts
603
+ * import { createDb } from 'orchid-orm';
604
+ *
605
+ * const db = createDb({
606
+ * // db connection options
607
+ * databaseURL: process.env.DATABASE_URL,
608
+ * log: true,
609
+ *
610
+ * // columns in db are in snake case:
611
+ * snakeCase: true,
612
+ *
613
+ * // override default SQL for timestamp, see `nowSQL` above
614
+ * nowSQL: `now() AT TIME ZONE 'UTC'`,
615
+ *
616
+ * // override column types:
617
+ * columnTypes: (t) => ({
618
+ * // by default timestamp is returned as a string, override to a number
619
+ * timestamp: () => t.timestamp().asNumber(),
620
+ * }),
621
+ * });
622
+ * ```
623
+ *
624
+ * After `db` is defined, construct queryable tables in such way:
625
+ *
626
+ * ```ts
627
+ * export const User = db('user', (t) => ({
628
+ * id: t.identity().primaryKey(),
629
+ * name: t.text(3, 100),
630
+ * password: t.text(8, 200),
631
+ * age: t.integer().nullable(),
632
+ * ...t.timestamps(),
633
+ * }));
634
+ * ```
635
+ *
636
+ * Now the `User` can be used for making type-safe queries:
637
+ *
638
+ * ```ts
639
+ * const users = await User.select('id', 'name') // only known columns are allowed
640
+ * .where({ age: { gte: 20 } }) // gte is available only on the numeric field, and the only number is allowed
641
+ * .order({ createdAt: 'DESC' }) // type safe as well
642
+ * .limit(10);
643
+ *
644
+ * // users array has a proper type of Array<{ id: number, name: string }>
645
+ * ```
646
+ *
647
+ * The optional third argument is for table options:
648
+ *
649
+ * ```ts
650
+ * const Table = db('table', (t) => ({ ...columns }), {
651
+ * // provide this value if the table belongs to a specific database schema:
652
+ * schema: 'customTableSchema',
653
+ * // override `log` option of `createDb`:
654
+ * log: true, // boolean or object described `createdDb` section
655
+ * logger: { ... }, // override logger
656
+ * noPrimaryKey: 'ignore', // override noPrimaryKey
657
+ * snakeCase: true, // override snakeCase
658
+ * })
659
+ * ```
660
+ */
594
661
  declare const createDb: <ColumnTypes extends Record<string, orchid_core.AnyColumnTypeCreator> = DefaultColumnTypes>({ log, logger, columnTypes: ctOrFn, snakeCase, nowSQL, ...options }: DbOptions<ColumnTypes>) => DbResult<ColumnTypes>;
595
662
 
596
663
  type ToSQLCtx = {
@@ -1891,7 +1958,7 @@ declare class QueryGet {
1891
1958
  * It will throw `NotFoundError` when not found.
1892
1959
  *
1893
1960
  * ```ts
1894
- * import { NumberColumn } from 'pqb';
1961
+ * import { NumberColumn } from 'orchid-orm';
1895
1962
  *
1896
1963
  * const firstName: string = await db.table.get('name');
1897
1964
  *
@@ -3754,7 +3821,7 @@ declare abstract class JsonModifiers extends QueryBase {
3754
3821
  * Selects a value from JSON data using a JSON path.
3755
3822
  *
3756
3823
  * ```ts
3757
- * import { columnTypes } from 'pqb';
3824
+ * import { columnTypes } from 'orchid-orm';
3758
3825
  *
3759
3826
  * db.table.jsonPathQuery(
3760
3827
  * columnTypes.text(3, 100), // type of the value
@@ -4001,7 +4068,7 @@ declare class With {
4001
4068
  * Add Common Table Expression (CTE) to the query.
4002
4069
  *
4003
4070
  * ```ts
4004
- * import { columnTypes } from 'pqb';
4071
+ * import { columnTypes } from 'orchid-orm';
4005
4072
  * import { NumberColumn } from './number';
4006
4073
  *
4007
4074
  * // .with optionally accepts such options:
@@ -4345,7 +4412,7 @@ declare class Update {
4345
4412
  * To make sure that at least one row was updated use `updateOrThrow`:
4346
4413
  *
4347
4414
  * ```ts
4348
- * import { NotFoundError } from 'pqb';
4415
+ * import { NotFoundError } from 'orchid-orm';
4349
4416
  *
4350
4417
  * try {
4351
4418
  * // updatedCount is guaranteed to be greater than 0
@@ -5635,6 +5702,20 @@ declare class QueryMethods<ColumnTypes> {
5635
5702
  * await selectFollowing(db.user.select('id', 'name'), currentUser);
5636
5703
  * ```
5637
5704
  *
5705
+ * To get the result type of query helper, use `QueryHelperResult` type:
5706
+ *
5707
+ * ```ts
5708
+ * import { QueryHelperResult } from 'orchid-orm';
5709
+ *
5710
+ * const selectHelper = db.table.makeHelper((q) => q.select('id', 'name'));
5711
+ *
5712
+ * // This type is identical to `db.table.select('id', 'name')`
5713
+ * type SelectQuery = QueryHelperResult<typeof selectHelper>;
5714
+ *
5715
+ * // Await to get result, the type is `{ id: number, name: string }[]`
5716
+ * type Result = Awaited<QueryHelperResult<typeof selectHelper>>;
5717
+ * ```
5718
+ *
5638
5719
  * @param fn - helper function
5639
5720
  */
5640
5721
  makeHelper<T extends Query, Args extends unknown[], Result>(this: T, fn: (q: T, ...args: Args) => Result): QueryHelper<T, Args, Result>;
@@ -5975,7 +6056,6 @@ declare class BigSerialColumn extends NumberAsStringBaseColumn {
5975
6056
  toCode(t: string): Code;
5976
6057
  }
5977
6058
 
5978
- type StringColumn = ColumnType<string, typeof Operators.text>;
5979
6059
  type TextColumnData = StringTypeData;
5980
6060
  type TextMethods = typeof stringTypeMethods;
5981
6061
  interface TextBaseColumn extends ColumnType<string, typeof Operators.text>, TextMethods {
@@ -6007,6 +6087,10 @@ declare class VarCharColumn<Limit extends number | undefined = undefined> extend
6007
6087
  dataType: "varchar";
6008
6088
  toCode(t: string): Code;
6009
6089
  }
6090
+ declare class StringColumn<Limit extends number = 255> extends VarCharColumn<Limit> {
6091
+ constructor(limit?: Limit);
6092
+ toCode(t: string): Code;
6093
+ }
6010
6094
  declare class CharColumn<Limit extends number | undefined = undefined> extends LimitedTextBaseColumn<Limit> {
6011
6095
  dataType: "char";
6012
6096
  toCode(t: string): Code;
@@ -6645,7 +6729,7 @@ type DefaultColumnTypes = TimestampHelpers & {
6645
6729
  varchar<Limit extends number | undefined = undefined>(limit?: Limit): VarCharColumn<Limit>;
6646
6730
  char<Limit extends number | undefined = undefined>(limit?: Limit): CharColumn<Limit>;
6647
6731
  text(min: number, max: number): TextColumn;
6648
- string<Limit extends number = 255>(limit?: Limit): VarCharColumn<Limit>;
6732
+ string<Limit extends number = 255>(limit?: Limit): StringColumn<Limit>;
6649
6733
  citext(min: number, max: number): CitextColumn;
6650
6734
  bytea(): ByteaColumn;
6651
6735
  date(): DateColumn;
package/dist/index.js CHANGED
@@ -2859,6 +2859,21 @@ class VarCharColumn extends LimitedTextBaseColumn {
2859
2859
  );
2860
2860
  }
2861
2861
  }
2862
+ class StringColumn extends VarCharColumn {
2863
+ constructor(limit = 255) {
2864
+ super(limit);
2865
+ }
2866
+ toCode(t) {
2867
+ let max = this.data.maxChars;
2868
+ if (max === 255)
2869
+ max = void 0;
2870
+ return columnCode(
2871
+ this,
2872
+ t,
2873
+ `string(${max != null ? max : ""})${orchidCore.stringDataToCode(this.data)}`
2874
+ );
2875
+ }
2876
+ }
2862
2877
  class CharColumn extends LimitedTextBaseColumn {
2863
2878
  constructor() {
2864
2879
  super(...arguments);
@@ -3616,7 +3631,7 @@ const columnTypes = __spreadValues$9({
3616
3631
  return new TextColumn(min, max);
3617
3632
  },
3618
3633
  string(limit = 255) {
3619
- return new VarCharColumn(limit);
3634
+ return new StringColumn(limit);
3620
3635
  },
3621
3636
  citext(min, max) {
3622
3637
  return new CitextColumn(min, max);
@@ -6170,7 +6185,7 @@ class QueryGet {
6170
6185
  * It will throw `NotFoundError` when not found.
6171
6186
  *
6172
6187
  * ```ts
6173
- * import { NumberColumn } from 'pqb';
6188
+ * import { NumberColumn } from 'orchid-orm';
6174
6189
  *
6175
6190
  * const firstName: string = await db.table.get('name');
6176
6191
  *
@@ -7454,7 +7469,7 @@ class JsonModifiers extends QueryBase {
7454
7469
  * Selects a value from JSON data using a JSON path.
7455
7470
  *
7456
7471
  * ```ts
7457
- * import { columnTypes } from 'pqb';
7472
+ * import { columnTypes } from 'orchid-orm';
7458
7473
  *
7459
7474
  * db.table.jsonPathQuery(
7460
7475
  * columnTypes.text(3, 100), // type of the value
@@ -8041,7 +8056,7 @@ class With {
8041
8056
  * Add Common Table Expression (CTE) to the query.
8042
8057
  *
8043
8058
  * ```ts
8044
- * import { columnTypes } from 'pqb';
8059
+ * import { columnTypes } from 'orchid-orm';
8045
8060
  * import { NumberColumn } from './number';
8046
8061
  *
8047
8062
  * // .with optionally accepts such options:
@@ -8546,7 +8561,7 @@ class Update {
8546
8561
  * To make sure that at least one row was updated use `updateOrThrow`:
8547
8562
  *
8548
8563
  * ```ts
8549
- * import { NotFoundError } from 'pqb';
8564
+ * import { NotFoundError } from 'orchid-orm';
8550
8565
  *
8551
8566
  * try {
8552
8567
  * // updatedCount is guaranteed to be greater than 0
@@ -9882,6 +9897,20 @@ class QueryMethods {
9882
9897
  * await selectFollowing(db.user.select('id', 'name'), currentUser);
9883
9898
  * ```
9884
9899
  *
9900
+ * To get the result type of query helper, use `QueryHelperResult` type:
9901
+ *
9902
+ * ```ts
9903
+ * import { QueryHelperResult } from 'orchid-orm';
9904
+ *
9905
+ * const selectHelper = db.table.makeHelper((q) => q.select('id', 'name'));
9906
+ *
9907
+ * // This type is identical to `db.table.select('id', 'name')`
9908
+ * type SelectQuery = QueryHelperResult<typeof selectHelper>;
9909
+ *
9910
+ * // Await to get result, the type is `{ id: number, name: string }[]`
9911
+ * type Result = Awaited<QueryHelperResult<typeof selectHelper>>;
9912
+ * ```
9913
+ *
9885
9914
  * @param fn - helper function
9886
9915
  */
9887
9916
  makeHelper(fn) {
@@ -10426,6 +10455,7 @@ exports.Select = Select;
10426
10455
  exports.SerialColumn = SerialColumn;
10427
10456
  exports.SmallIntColumn = SmallIntColumn;
10428
10457
  exports.SmallSerialColumn = SmallSerialColumn;
10458
+ exports.StringColumn = StringColumn;
10429
10459
  exports.TextBaseColumn = TextBaseColumn;
10430
10460
  exports.TextColumn = TextColumn;
10431
10461
  exports.Then = Then;