pqb 0.7.7 → 0.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pqb",
3
- "version": "0.7.7",
3
+ "version": "0.7.9",
4
4
  "description": "Postgres query builder",
5
5
  "homepage": "https://orchid-orm.netlify.app/guide/query-builder-setup.html",
6
6
  "repository": {
package/src/adapter.ts CHANGED
@@ -41,8 +41,9 @@ for (const key in types.builtins) {
41
41
 
42
42
  const returnArg = (arg: unknown) => arg;
43
43
 
44
- export type AdapterOptions = Omit<PoolConfig, 'types'> & {
44
+ export type AdapterOptions = Omit<PoolConfig, 'types' | 'connectionString'> & {
45
45
  types?: TypeParsers;
46
+ databaseURL?: string;
46
47
  };
47
48
 
48
49
  export class Adapter {
@@ -51,6 +52,13 @@ export class Adapter {
51
52
 
52
53
  constructor({ types = defaultTypeParsers, ...config }: AdapterOptions) {
53
54
  this.types = types;
55
+ if (config.databaseURL) {
56
+ (config as PoolConfig).connectionString = config.databaseURL;
57
+ const url = new URL(config.databaseURL);
58
+ if (url.searchParams.get('ssl') === 'true') {
59
+ config.ssl = true;
60
+ }
61
+ }
54
62
  this.pool = new Pool(config);
55
63
  }
56
64
 
package/src/db.test.ts CHANGED
@@ -2,6 +2,7 @@ import {
2
2
  adapter,
3
3
  assertType,
4
4
  db,
5
+ dbOptions,
5
6
  expectSql,
6
7
  User,
7
8
  userData,
@@ -153,4 +154,16 @@ describe('db', () => {
153
154
  expect(logger.warn).not.toBeCalled();
154
155
  });
155
156
  });
157
+
158
+ it('should use ssl when ssl=true query parameter provided on a databaseUrl option', () => {
159
+ const db = createDb({
160
+ ...dbOptions,
161
+ databaseURL: dbOptions.databaseURL + '?ssl=true',
162
+ });
163
+
164
+ expect(
165
+ (db.adapter.pool as unknown as { options: Record<string, unknown> })
166
+ .options.ssl,
167
+ ).toBe(true);
168
+ });
156
169
  });
@@ -14,10 +14,22 @@ describe('then', () => {
14
14
  column: db.raw((t) => t.boolean(), 'koko'),
15
15
  }).catch((err) => {
16
16
  expect(err.message).toBe(`column "koko" does not exist`);
17
+ expect(err.stack).toContain('then.test.ts');
17
18
  done();
18
19
  });
19
20
 
20
21
  assertType<Awaited<typeof query>, { column: boolean }[] | void>();
21
22
  });
22
23
  });
24
+
25
+ it('should throw NotFoundError with proper stack trace', async () => {
26
+ let error: unknown | undefined;
27
+ try {
28
+ await User.take();
29
+ } catch (err) {
30
+ error = err;
31
+ }
32
+
33
+ expect((error as { stack: string }).stack).toContain('then.test.ts');
34
+ });
23
35
  });
@@ -133,7 +133,15 @@ const then = async (
133
133
 
134
134
  resolve?.(result);
135
135
  } catch (err) {
136
- const error = err instanceof DatabaseError ? assignError(q, err) : err;
136
+ let error;
137
+ if (err instanceof DatabaseError) {
138
+ error = new (q.error as unknown as new () => QueryError)();
139
+ assignError(error, err);
140
+ } else {
141
+ error = err;
142
+ }
143
+
144
+ (error as Error).stack = queryError.stack;
137
145
 
138
146
  if (q.query.log && sql && logData) {
139
147
  q.query.log.onError(error as Error, sql, logData);
@@ -142,9 +150,7 @@ const then = async (
142
150
  }
143
151
  };
144
152
 
145
- const assignError = (q: Query, from: DatabaseError) => {
146
- const to = new (q.error as unknown as new () => QueryError)();
147
- to.stack = queryError.stack;
153
+ const assignError = (to: QueryError, from: DatabaseError) => {
148
154
  to.message = from.message;
149
155
  (to as { length?: number }).length = from.length;
150
156
  (to as { name?: string }).name = from.name;
@@ -12,11 +12,11 @@ import { MaybeArray, toArray } from '../utils';
12
12
  import { Adapter } from '../adapter';
13
13
 
14
14
  export const dbOptions = {
15
- connectionString: process.env.DATABASE_URL,
15
+ databaseURL: process.env.DATABASE_URL,
16
16
  columnTypes,
17
17
  };
18
18
 
19
- export const dbClient = new Client(dbOptions);
19
+ export const dbClient = new Client({ connectionString: dbOptions.databaseURL });
20
20
 
21
21
  export const adapter = new Adapter(dbOptions);
22
22