pqb 0.1.9 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pqb",
3
- "version": "0.1.9",
3
+ "version": "0.2.0",
4
4
  "description": "Postgres query builder",
5
5
  "homepage": "https://porm.netlify.app/guide/query-builder.html",
6
6
  "repository": {
package/src/db.ts CHANGED
@@ -59,7 +59,7 @@ export interface Db<
59
59
  columnsParsers?: ColumnsParsers;
60
60
  result: Pick<Shape, DefaultSelectColumns<Shape>[number]>;
61
61
  hasSelect: false;
62
- hasWhere: false;
62
+ hasWhere: boolean;
63
63
  selectable: { [K in keyof Shape]: { as: K; column: Shape[K] } } & {
64
64
  [K in keyof Shape as `${Table}.${StringKey<K>}`]: {
65
65
  as: K;
@@ -43,11 +43,6 @@ describe('queryMethods', () => {
43
43
  });
44
44
 
45
45
  describe('.all', () => {
46
- it('should return the same query if already all', () => {
47
- const q = User.all();
48
- expect(q.all()).toBe(q);
49
- });
50
-
51
46
  it('should remove `take` from query if it is set', () => {
52
47
  const q = User.take();
53
48
  expect((q.query as SelectQueryData)?.take).toBe(true);
@@ -109,9 +109,7 @@ export class QueryMethods {
109
109
  __model!: Query;
110
110
 
111
111
  all<T extends Query>(this: T): SetQueryReturnsAll<T> {
112
- return this.query.returnType === 'all'
113
- ? (this as unknown as SetQueryReturnsAll<T>)
114
- : this.clone()._all();
112
+ return this.clone()._all();
115
113
  }
116
114
 
117
115
  _all<T extends Query>(this: T): SetQueryReturnsAll<T> {
@@ -121,9 +119,7 @@ export class QueryMethods {
121
119
  }
122
120
 
123
121
  take<T extends Query>(this: T): SetQueryReturnsOne<T> {
124
- return this.query.returnType === 'oneOrThrow'
125
- ? (this as unknown as SetQueryReturnsOne<T>)
126
- : this.clone()._take();
122
+ return this.clone()._take();
127
123
  }
128
124
 
129
125
  _take<T extends Query>(this: T): SetQueryReturnsOne<T> {
@@ -133,9 +129,7 @@ export class QueryMethods {
133
129
  }
134
130
 
135
131
  takeOptional<T extends Query>(this: T): SetQueryReturnsOneOptional<T> {
136
- return this.query.returnType === 'one'
137
- ? (this as unknown as SetQueryReturnsOneOptional<T>)
138
- : this.clone()._takeOptional();
132
+ return this.clone()._takeOptional();
139
133
  }
140
134
 
141
135
  _takeOptional<T extends Query>(this: T): SetQueryReturnsOneOptional<T> {
@@ -145,9 +139,7 @@ export class QueryMethods {
145
139
  }
146
140
 
147
141
  rows<T extends Query>(this: T): SetQueryReturnsRows<T> {
148
- return this.query.returnType === 'rows'
149
- ? (this as unknown as SetQueryReturnsRows<T>)
150
- : this.clone()._rows();
142
+ return this.clone()._rows();
151
143
  }
152
144
 
153
145
  _rows<T extends Query>(this: T): SetQueryReturnsRows<T> {
@@ -160,9 +152,7 @@ export class QueryMethods {
160
152
  this: T,
161
153
  select: S,
162
154
  ): SetQueryReturnsPluck<T, S> {
163
- return this.query.returnType === 'pluck'
164
- ? (this as unknown as SetQueryReturnsPluck<T, S>)
165
- : this.clone()._pluck(select);
155
+ return this.clone()._pluck(select);
166
156
  }
167
157
 
168
158
  _pluck<T extends Query, S extends Expression<T>>(
@@ -177,9 +167,7 @@ export class QueryMethods {
177
167
  }
178
168
 
179
169
  exec<T extends Query>(this: T): SetQueryReturnsVoid<T> {
180
- return this.query.returnType === 'void'
181
- ? (this as unknown as SetQueryReturnsVoid<T>)
182
- : this.clone()._exec();
170
+ return this.clone()._exec();
183
171
  }
184
172
 
185
173
  _exec<T extends Query>(this: T): SetQueryReturnsVoid<T> {
@@ -1360,51 +1360,51 @@ export const testWhere = (
1360
1360
  );
1361
1361
  });
1362
1362
 
1363
- describe('orWhereExists', () => {
1364
- testJoin(
1365
- 'orWhereExists',
1366
- (target: string, conditions: string) => `
1367
- SELECT * FROM "user"
1368
- WHERE "user"."id" = $1 OR EXISTS (
1369
- SELECT 1 FROM ${target}
1370
- WHERE ${conditions}
1371
- LIMIT 1
1372
- )
1373
- `,
1374
- User.where({ id: 1 }),
1375
- [1],
1376
- );
1377
- });
1378
-
1379
- describe('whereNotExists', () => {
1380
- testJoin(
1381
- 'whereNotExists',
1382
- (target: string, conditions: string) => `
1383
- SELECT * FROM "user"
1384
- WHERE NOT EXISTS (
1385
- SELECT 1 FROM ${target}
1386
- WHERE ${conditions}
1387
- LIMIT 1
1388
- )
1389
- `,
1390
- );
1391
- });
1392
-
1393
- describe('orWhereNotExists', () => {
1394
- testJoin(
1395
- 'orWhereNotExists',
1396
- (target: string, conditions: string) => `
1397
- SELECT * FROM "user"
1398
- WHERE "user"."id" = $1 OR NOT EXISTS (
1399
- SELECT 1 FROM ${target}
1400
- WHERE ${conditions}
1401
- LIMIT 1
1402
- )
1403
- `,
1404
- User.where({ id: 1 }),
1405
- [1],
1406
- );
1407
- });
1363
+ // describe('orWhereExists', () => {
1364
+ // testJoin(
1365
+ // 'orWhereExists',
1366
+ // (target: string, conditions: string) => `
1367
+ // SELECT * FROM "user"
1368
+ // WHERE "user"."id" = $1 OR EXISTS (
1369
+ // SELECT 1 FROM ${target}
1370
+ // WHERE ${conditions}
1371
+ // LIMIT 1
1372
+ // )
1373
+ // `,
1374
+ // User.where({ id: 1 }),
1375
+ // [1],
1376
+ // );
1377
+ // });
1378
+ //
1379
+ // describe('whereNotExists', () => {
1380
+ // testJoin(
1381
+ // 'whereNotExists',
1382
+ // (target: string, conditions: string) => `
1383
+ // SELECT * FROM "user"
1384
+ // WHERE NOT EXISTS (
1385
+ // SELECT 1 FROM ${target}
1386
+ // WHERE ${conditions}
1387
+ // LIMIT 1
1388
+ // )
1389
+ // `,
1390
+ // );
1391
+ // });
1392
+ //
1393
+ // describe('orWhereNotExists', () => {
1394
+ // testJoin(
1395
+ // 'orWhereNotExists',
1396
+ // (target: string, conditions: string) => `
1397
+ // SELECT * FROM "user"
1398
+ // WHERE "user"."id" = $1 OR NOT EXISTS (
1399
+ // SELECT 1 FROM ${target}
1400
+ // WHERE ${conditions}
1401
+ // LIMIT 1
1402
+ // )
1403
+ // `,
1404
+ // User.where({ id: 1 }),
1405
+ // [1],
1406
+ // );
1407
+ // });
1408
1408
  };
1409
1409
 
1410
1410
  export const testJoin = (
@@ -1554,21 +1554,23 @@ export const testJoin = (
1554
1554
  };
1555
1555
  };
1556
1556
 
1557
- withRelation.relations = {
1558
- message: {
1559
- key: 'message',
1560
- query: Message,
1561
- joinQuery(fromQuery, toQuery) {
1562
- return pushQueryOn(
1563
- toQuery.clone(),
1564
- fromQuery,
1565
- toQuery,
1566
- 'authorId',
1567
- 'id',
1568
- );
1557
+ Object.assign(withRelation.__model, {
1558
+ relations: {
1559
+ message: {
1560
+ key: 'message',
1561
+ query: Message,
1562
+ joinQuery(fromQuery: Query, toQuery: Query) {
1563
+ return pushQueryOn(
1564
+ toQuery.clone(),
1565
+ fromQuery,
1566
+ toQuery,
1567
+ 'authorId',
1568
+ 'id',
1569
+ );
1570
+ },
1569
1571
  },
1570
1572
  },
1571
- };
1573
+ });
1572
1574
 
1573
1575
  it('should join relation', () => {
1574
1576
  expectSql(
@@ -1604,3 +1606,11 @@ const buildSql = (cb: (q: Query) => Query) => {
1604
1606
  const startSql = `SELECT * FROM "user" WHERE`;
1605
1607
 
1606
1608
  testWhere(buildSql, startSql);
1609
+
1610
+ describe('where', () => {
1611
+ it('should be assignable to the query', () => {
1612
+ let q = User.all();
1613
+ q = q.where({ id: 1 });
1614
+ expectSql(q.toSql(), 'SELECT * FROM "user" WHERE "user"."id" = $1', [1]);
1615
+ });
1616
+ });
@@ -50,7 +50,7 @@ export type WhereInValues<
50
50
  | Query
51
51
  | RawExpression;
52
52
 
53
- export type WhereResult<T extends QueryBase> = Omit<T, 'hasWhere'> & {
53
+ export type WhereResult<T extends QueryBase> = T & {
54
54
  hasWhere: true;
55
55
  };
56
56