@juit/pgproxy-persister 1.4.1 → 1.4.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 +1 -0
- package/dist/model.cjs.map +1 -1
- package/dist/model.d.ts +1 -38
- package/dist/model.mjs.map +1 -1
- package/dist/persister.d.ts +1 -1
- package/dist/search.cjs +16 -16
- package/dist/search.cjs.map +1 -1
- package/dist/search.d.ts +36 -159
- package/dist/search.mjs +16 -16
- package/dist/search.mjs.map +1 -1
- package/package.json +4 -13
- package/src/index.ts +3 -0
- package/src/model.ts +8 -81
- package/src/persister.ts +1 -1
- package/src/search.ts +81 -254
package/dist/index.d.ts
CHANGED
package/dist/model.cjs.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/model.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAuB;AAEvB,mBAAuE;
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAuB;AAEvB,mBAAuE;AAuIvE,SAAS,MACL,OACA,QAC4B;AAC9B,QAAM,aAAa,CAAC;AAEpB,MAAI,QAAQ;AACZ,aAAW,CAAE,QAAQ,KAAM,KAAK,OAAO,QAAQ,KAAK,GAAG;AACrD,QAAI,UAAU,OAAW;AACzB,QAAI,UAAU,MAAM;AAClB,iBAAW,KAAK,OAAG,8BAAO,MAAM,CAAC,UAAU;AAAA,IAC7C,OAAO;AACL,YAAM,QAAQ,OAAO,KAAK,KAAK;AAC/B,iBAAW,KAAK,OAAG,8BAAO,MAAM,CAAC,KAAK,KAAK,EAAE;AAAA,IAC/C;AACA;AAAA,EACF;AAEA,SAAO;AAAA,IACL,WAAW,SAAS,UAAU,WAAW,KAAK,OAAO,CAAC,KAAK;AAAA,IA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
|
|
6
6
|
}
|
package/dist/model.d.ts
CHANGED
|
@@ -1,41 +1,5 @@
|
|
|
1
1
|
import type { PGQueryable } from '@juit/pgproxy-client';
|
|
2
|
-
type
|
|
3
|
-
[K in keyof T]: T[K];
|
|
4
|
-
};
|
|
5
|
-
type OnlyStrings<T> = T extends string ? T : never;
|
|
6
|
-
/** The definition of a column */
|
|
7
|
-
export interface ColumnDefinition<T = any> {
|
|
8
|
-
/** The TypeScript type of the column (from the type parser) */
|
|
9
|
-
type: T;
|
|
10
|
-
/** Whether the column is _generated_ or not */
|
|
11
|
-
isGenerated?: boolean;
|
|
12
|
-
/** Whether the column is _nullable_ or not */
|
|
13
|
-
isNullable?: boolean;
|
|
14
|
-
/** Whether the column _specifies a default value_ or not */
|
|
15
|
-
hasDefault?: boolean;
|
|
16
|
-
}
|
|
17
|
-
/** Infer the TypeScript type suitable for an `INSERT` in a table */
|
|
18
|
-
export type InferInsertType<Table extends Record<string, ColumnDefinition>> = SimplifyIntersection<{
|
|
19
|
-
[Column in keyof Table as Column extends string ? Table[Column]['isGenerated'] extends true ? never : Table[Column]['isNullable'] extends true ? Column : Table[Column]['hasDefault'] extends true ? Column : never : never]?: Table[Column]['isNullable'] extends true ? Table[Column]['type'] | null : Table[Column]['type'];
|
|
20
|
-
} & {
|
|
21
|
-
[Column in keyof Table as Column extends string ? Table[Column]['isGenerated'] extends true ? never : Table[Column]['isNullable'] extends true ? never : Table[Column]['hasDefault'] extends true ? never : Column : never]-?: Table[Column]['isNullable'] extends true ? Table[Column]['type'] | null : Table[Column]['type'];
|
|
22
|
-
}>;
|
|
23
|
-
/** Infer the TypeScript type suitable for a `SELECT` from a table */
|
|
24
|
-
export type InferSelectType<Table extends Record<string, ColumnDefinition>> = {
|
|
25
|
-
[Column in keyof Table as Column extends string ? Column : never]-?: (Table[Column]['isNullable'] extends true ? Table[Column]['type'] | null : Table[Column]['type']) & (Table[Column] extends {
|
|
26
|
-
branding: infer Brand;
|
|
27
|
-
} ? Brand : unknown);
|
|
28
|
-
};
|
|
29
|
-
/** Infer the TypeScript type suitable for a `UPDATE` in a table */
|
|
30
|
-
export type InferUpdateType<Table extends Record<string, ColumnDefinition>> = {
|
|
31
|
-
[Column in keyof Table as Column extends string ? Table[Column]['isGenerated'] extends true ? never : Column : never]?: Table[Column]['isNullable'] extends true ? Table[Column]['type'] | null : Table[Column]['type'];
|
|
32
|
-
};
|
|
33
|
-
/** Infer the TypeScript type used for querying records */
|
|
34
|
-
export type InferQueryType<Table extends Record<string, ColumnDefinition>> = {
|
|
35
|
-
[Column in keyof Table as Column extends string ? Column : never]?: Table[Column]['isNullable'] extends true ? Table[Column]['type'] | null : Table[Column]['type'];
|
|
36
|
-
};
|
|
37
|
-
/** Infer the available sort values for a table (as required by `ORDER BY`) */
|
|
38
|
-
export type InferSort<Table extends Record<string, ColumnDefinition>> = `${OnlyStrings<keyof Table>}${' ASC' | ' asc' | ' DESC' | ' desc' | ''}`;
|
|
2
|
+
import type { ColumnDefinition, InferInsertType, InferQueryType, InferSelectType, InferSort, InferUpdateType } from '@juit/pgproxy-model';
|
|
39
3
|
/** The model interface defines a CRUD interface to PosgreSQL tables */
|
|
40
4
|
export interface Model<Table extends Record<string, ColumnDefinition>> {
|
|
41
5
|
/**
|
|
@@ -115,4 +79,3 @@ export interface ModelConstructor {
|
|
|
115
79
|
new <Schema extends Record<string, ColumnDefinition>>(queryable: PGQueryable, table: string): Model<Schema>;
|
|
116
80
|
}
|
|
117
81
|
export declare const Model: ModelConstructor;
|
|
118
|
-
export {};
|
package/dist/model.mjs.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/model.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,cAAc;AAEvB,SAAS,QAAQ,aAAa,cAAc,2BAA2B;
|
|
4
|
+
"mappings": ";AAAA,SAAS,cAAc;AAEvB,SAAS,QAAQ,aAAa,cAAc,2BAA2B;AAuI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,WAAO,OAAO,CAAC;AAAA,EACjB;AAAA,EAEA,MAAM,OACF,OACA,OACiC;AACnC,UAAM,CAAE,KAAK,MAAO,IAAI,OAAO,KAAK,SAAS,OAAO,KAAK;AACzD,UAAM,SAAS,MAAM,KAAK,YAAY,MAA8B,KAAK,MAAM;AAC/E,WAAO,OAAO;AAAA,EAChB;AAAA,EAEA,MAAM,OACF,OACe;AACjB,UAAM,CAAE,KAAK,MAAO,IAAI,IAAI,KAAK,SAAS,KAAK;AAC/C,UAAM,SAAS,MAAM,KAAK,YAAY,MAAM,KAAK,MAAM;AACvD,WAAO,OAAO;AAAA,EAChB;AACF;AAMO,IAAM,QAA0B;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/dist/persister.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PGClient } from '@juit/pgproxy-client';
|
|
2
2
|
import { Model } from './model';
|
|
3
3
|
import type { PGClientOptions, PGTransactionable } from '@juit/pgproxy-client';
|
|
4
|
-
import type { ColumnDefinition } from '
|
|
4
|
+
import type { ColumnDefinition } from '@juit/pgproxy-model';
|
|
5
5
|
export type InferModelType<Schema, Table extends string & keyof Schema> = Schema[Table] extends Record<string, ColumnDefinition> ? Model<Schema[Table]> : never;
|
|
6
6
|
export interface ModelProvider<Schema> {
|
|
7
7
|
in<Table extends string>(table: Table & keyof Schema): InferModelType<Schema, Table & keyof Schema>;
|
package/dist/search.cjs
CHANGED
|
@@ -32,16 +32,16 @@ function reviver(_key, data) {
|
|
|
32
32
|
return data;
|
|
33
33
|
}
|
|
34
34
|
var SearchImpl = class {
|
|
35
|
-
/** Our
|
|
36
|
-
#
|
|
35
|
+
/** Our search provider instance */
|
|
36
|
+
#provider;
|
|
37
37
|
/** The escaped table name */
|
|
38
38
|
#eTable;
|
|
39
39
|
/** The escaped joins */
|
|
40
40
|
#eJoins;
|
|
41
41
|
/** The full-text search column (if any) */
|
|
42
42
|
#fullTextSearchColumn;
|
|
43
|
-
constructor(
|
|
44
|
-
this.#
|
|
43
|
+
constructor(provider, table, joinsOrFullTextSearchColumn, maybeFullTextSearchColumn) {
|
|
44
|
+
this.#provider = provider;
|
|
45
45
|
this.#eTable = (0, import_utils.encodeSchemaAndName)(table);
|
|
46
46
|
let joins = {};
|
|
47
47
|
let fullTextSearchColumn = void 0;
|
|
@@ -77,8 +77,8 @@ var SearchImpl = class {
|
|
|
77
77
|
const orderby = [];
|
|
78
78
|
const params = [];
|
|
79
79
|
if (extra) {
|
|
80
|
-
where.push(extra.
|
|
81
|
-
params.push(...extra.params);
|
|
80
|
+
where.push(extra.query);
|
|
81
|
+
if (extra.params) params.push(...extra.params);
|
|
82
82
|
}
|
|
83
83
|
let esearch = "";
|
|
84
84
|
if (count === "only") {
|
|
@@ -180,23 +180,23 @@ var SearchImpl = class {
|
|
|
180
180
|
if (limit) sql += ` LIMIT $${params.push(limit)}`;
|
|
181
181
|
return [sql, params];
|
|
182
182
|
}
|
|
183
|
-
query(options,
|
|
184
|
-
return this.#query(false, options,
|
|
183
|
+
query(options, where) {
|
|
184
|
+
return this.#query(false, options, where);
|
|
185
185
|
}
|
|
186
|
-
async find(options,
|
|
187
|
-
const [sql, params] = this.#query(false, { ...options, offset: 0, limit: 1 },
|
|
188
|
-
const result = await this.#
|
|
186
|
+
async find(options, where) {
|
|
187
|
+
const [sql, params] = this.#query(false, { ...options, offset: 0, limit: 1 }, where);
|
|
188
|
+
const result = await this.#provider.query(sql, params);
|
|
189
189
|
if (result.rows[0]) return JSON.parse(result.rows[0].result, reviver);
|
|
190
190
|
return void 0;
|
|
191
191
|
}
|
|
192
|
-
async search(options,
|
|
193
|
-
const [sql, params] = this.#query(true, options,
|
|
194
|
-
const result = await this.#
|
|
192
|
+
async search(options, where) {
|
|
193
|
+
const [sql, params] = this.#query(true, options, where);
|
|
194
|
+
const result = await this.#provider.query(sql, params).catch((error) => {
|
|
195
195
|
throw new Error(`Error executing search query: ${error.message}`, { cause: { sql, params, error } });
|
|
196
196
|
});
|
|
197
197
|
if (result.rows.length === 0 && (options.offset || 0) > 0) {
|
|
198
|
-
const [sql2, params2] = this.#query("only", { ...options, offset: 0, limit: void 0 },
|
|
199
|
-
const result2 = await this.#
|
|
198
|
+
const [sql2, params2] = this.#query("only", { ...options, offset: 0, limit: void 0 }, where);
|
|
199
|
+
const result2 = await this.#provider.query(sql2, params2);
|
|
200
200
|
(0, import_utils.assert)(result2.rows[0], "Expected total row in count query");
|
|
201
201
|
const total2 = Number(result2.rows[0].total);
|
|
202
202
|
return { total: total2, rows: [] };
|
package/dist/search.cjs.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/search.ts"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAuB;AAEvB,mBAA4C;
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAuB;AAEvB,mBAA4C;AAmJ5C,IAAM,SAAS;AAGR,SAAS,QAAQ,MAAc,MAAgB;AACpD,MAAK,OAAO,SAAS,YAAa,OAAO,KAAK,IAAI,EAAG,QAAO,IAAI,KAAK,IAAI;AACzE,SAAO;AACT;AAEA,IAAM,aAAN,MAIgD;AAAA;AAAA,EAE9C;AAAA;AAAA,EAEA;AAAA;AAAA,EAEA;AAAA;AAAA,EAEA;AAAA,EAOA,YACI,UACA,OACA,6BACA,2BACF;AACA,SAAK,YAAY;AACjB,SAAK,cAAU,kCAAoB,KAAK;AAExC,QAAI,QAAe,CAAC;AACpB,QAAI,uBAA2C;AAE/C,QAAI,OAAO,gCAAgC,UAAU;AACnD,6BAAuB;AAAA,IACzB,WAAW,6BAA6B;AACtC,cAAQ;AACR,6BAAuB;AAAA,IACzB;AAEA,SAAK,wBAAwB,wBAAwB;AAErD,SAAK,UAAU,OAAO,YAAY,OAAO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,UAAM,SAAS,MAAM,KAAK,UAAU,MAAyC,KAAK,MAAM,EAAE,MAAM,CAAC,UAAU;AACzG,YAAM,IAAI,MAAM,iCAAiC,MAAM,OAAO,IAAI,EAAE,OAAO,EAAE,KAAK,QAAQ,MAAM,EAAE,CAAC;AAAA,IACrG,CAAC;AAED,QAAK,OAAO,KAAK,WAAW,MAAQ,QAAQ,UAAU,KAAK,GAAI;AAC7D,YAAM,CAAEC,MAAKC,OAAO,IAAI,KAAK,OAAO,QAAQ,EAAE,GAAG,SAAS,QAAQ,GAAG,OAAO,OAAU,GAAG,KAAK;AAC9F,YAAMC,UAAS,MAAM,KAAK,UAAU,MAAyBF,MAAKC,OAAM;AACxE,+BAAOC,QAAO,KAAK,CAAC,GAAG,mCAAmC;AAC1D,YAAMC,SAAQ,OAAOD,QAAO,KAAK,CAAC,EAAE,KAAK;AACzC,aAAO,EAAE,OAAAC,QAAO,MAAM,CAAC,EAAE;AAAA,IAC3B;AAEA,UAAM,OAAO,OAAO,KAAK,IAAI,CAAC,QAAQ,KAAK,MAAM,IAAI,QAAQ,OAAO,CAAC;AACrE,UAAM,QAAQ,OAAO,OAAO,KAAK,CAAC,GAAG,KAAK,KAAK;AAE/C,WAAO,EAAE,OAAO,KAAK;AAAA,EACvB;AACF;AAOO,IAAM,SAA4B;",
|
|
5
5
|
"names": ["evalue", "sql", "params", "result", "total"]
|
|
6
6
|
}
|
package/dist/search.d.ts
CHANGED
|
@@ -1,211 +1,88 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { PGQuery } from '@juit/pgproxy-client';
|
|
2
|
+
import type { SearchJoins, SearchOptions, SearchQuery, SearchResult, SearchResults } from '@juit/pgproxy-model';
|
|
3
|
+
import type { Connection, Persister } from './persister';
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* An object to perform searches on a given table.
|
|
5
6
|
*/
|
|
6
|
-
export interface
|
|
7
|
-
/**
|
|
8
|
-
* The column in _the search table_ (passed to the constructor of
|
|
9
|
-
* {@link Search}) referencing the specified `refTable` (defined here).
|
|
10
|
-
*
|
|
11
|
-
* ```sql
|
|
12
|
-
* ... LEFT JOIN "refTable" ON "table"."column" = "refTable"."refColumn"
|
|
13
|
-
* ^^^^^^
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
column: string;
|
|
17
|
-
/**
|
|
18
|
-
* The name of the table to _left join_.
|
|
19
|
-
*
|
|
20
|
-
* ```sql
|
|
21
|
-
* ... LEFT JOIN "refTable" ON "table"."column" = "refTable"."refColumn"
|
|
22
|
-
* ^^^^^^^^ ^^^^^^^^
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
refTable: string & keyof Schema;
|
|
26
|
-
/**
|
|
27
|
-
* The column in the `refTable` referenced by the _the search table_.
|
|
28
|
-
*
|
|
29
|
-
* ```sql
|
|
30
|
-
* ... LEFT JOIN "refTable" ON "table"."column" = "refTable"."refColumn"
|
|
31
|
-
* ^^^^^^^^^
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
refColumn: string;
|
|
35
|
-
/**
|
|
36
|
-
* The column in the referenced table to use as default sort column, when
|
|
37
|
-
* sorting by this join.
|
|
38
|
-
*/
|
|
39
|
-
sortColumn?: string;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Definition for joins in a {@link Search}
|
|
43
|
-
*
|
|
44
|
-
* Each key is the name of the join as it will appear in the results, and the
|
|
45
|
-
* value defines how to perform the join.
|
|
46
|
-
*
|
|
47
|
-
* See {@link StraightJoin} and {@link LinkedJoin} for details on the fields.
|
|
48
|
-
*/
|
|
49
|
-
export interface SearchJoins<Schema> {
|
|
50
|
-
[key: string]: SearchJoin<Schema>;
|
|
51
|
-
}
|
|
52
|
-
/** Internal interface defining operators available to *single values* */
|
|
53
|
-
interface ValueSearchFilter<Schema, Table extends string & keyof Schema> {
|
|
54
|
-
name: string & keyof Schema[Table];
|
|
55
|
-
field?: string;
|
|
56
|
-
op?: '=' | '!=' | '>' | '>=' | '<' | '<=' | '~' | 'like' | 'ilike';
|
|
57
|
-
value: string | number | Date | boolean | null;
|
|
58
|
-
}
|
|
59
|
-
/** Internal interface defining operators available to *array values* */
|
|
60
|
-
interface ArraySearchFilter<Schema, Table extends string & keyof Schema> {
|
|
61
|
-
name: string & keyof Schema[Table];
|
|
62
|
-
field?: string;
|
|
63
|
-
op: 'in' | 'not in';
|
|
64
|
-
value: (string | number | Date | boolean | null)[];
|
|
65
|
-
}
|
|
66
|
-
/** Internal interface defining operators available to *json values* */
|
|
67
|
-
interface JsonSearchFilter<Schema, Table extends string & keyof Schema> {
|
|
68
|
-
name: string & keyof Schema[Table];
|
|
69
|
-
field?: never;
|
|
70
|
-
op: '@>' | '<@';
|
|
71
|
-
value: any;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* A filter for a search that matches a single value
|
|
75
|
-
*
|
|
76
|
-
* - `name` is the column name to filter on
|
|
77
|
-
* - `field` is a field to filter on when the column is a complex type (JSONB)
|
|
78
|
-
* - `op` is the operator to use for the filter (default: `=`)
|
|
79
|
-
* - `value` is the value to filter for
|
|
80
|
-
*
|
|
81
|
-
* All operators are defined as per PostgreSQL documentation, with few notable
|
|
82
|
-
* exceptions:
|
|
83
|
-
*
|
|
84
|
-
* - `~` is an alias to the `ilike` operator
|
|
85
|
-
* - `in` and `not in` are used to match a value against an array of possible
|
|
86
|
-
* values using the `... = ANY(...)` or `... != ALL(...)` constructs
|
|
87
|
-
* - `@>` and `<@` will accept single values as well as arrays.
|
|
88
|
-
* - `!=` and `=` will use the PostgreSQL `IS (NOT) DISTINCT FROM` semantics
|
|
89
|
-
* to properly handle `NULL` comparisons.
|
|
90
|
-
*/
|
|
91
|
-
export type SearchFilter<Schema, Table extends string & keyof Schema> = ValueSearchFilter<Schema, Table> | ArraySearchFilter<Schema, Table> | JsonSearchFilter<Schema, Table>;
|
|
92
|
-
/**
|
|
93
|
-
* Base interface for querying results via our {@link Search}.
|
|
94
|
-
*/
|
|
95
|
-
export interface SearchQuery<Schema, Table extends string & keyof Schema, Joins extends SearchJoins<Schema> = {}> {
|
|
96
|
-
/** An optional set of filters to apply */
|
|
97
|
-
filters?: SearchFilter<Schema, Table>[];
|
|
98
|
-
/** An optional column to sort by */
|
|
99
|
-
sort?: string & (keyof Schema[Table] | keyof Joins);
|
|
100
|
-
/** The order to sort by (if `sort` is specified, default: 'asc') */
|
|
101
|
-
order?: 'asc' | 'desc';
|
|
102
|
-
/** An optional full-text search query, available for full-text search */
|
|
103
|
-
q?: string;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Full options for querying a limited set of results via our {@link Search}.
|
|
107
|
-
*/
|
|
108
|
-
export interface SearchOptions<Schema, Table extends string & keyof Schema, Joins extends SearchJoins<Schema> = {}> extends SearchQuery<Schema, Table, Joins> {
|
|
109
|
-
/** Offset to start returning rows from (default: 0) */
|
|
110
|
-
offset?: number;
|
|
111
|
-
/** Maximum number of rows to return (default: 20, unlimited if 0) */
|
|
112
|
-
limit?: number;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Extra (manual) SQL to further customize our {@link Search} queries.
|
|
116
|
-
*/
|
|
117
|
-
export interface SearchExtra {
|
|
118
|
-
/** Extra `WHERE` clause to add to our search */
|
|
119
|
-
where: string;
|
|
120
|
-
/** Parameters for the extra `WHERE` clause */
|
|
121
|
-
params: any[];
|
|
122
|
-
}
|
|
123
|
-
/** A single search result row (with joins) */
|
|
124
|
-
export type SearchResult<Schema, Table extends string & keyof Schema, Joins extends SearchJoins<Schema> = {}> = Schema[Table] extends Record<string, ColumnDefinition> ? InferSelectType<Schema[Table]> & {
|
|
125
|
-
[key in keyof Joins]: Joins[key]['refTable'] extends keyof Schema ? Schema[Joins[key]['refTable']] extends Record<string, ColumnDefinition> ? Schema[Table][Joins[key]['column']]['isNullable'] extends true ? InferSelectType<Schema[Joins[key]['refTable']]> | null : InferSelectType<Schema[Joins[key]['refTable']]> : unknown : unknown;
|
|
126
|
-
} : never;
|
|
127
|
-
/** What's being returned by our `search` */
|
|
128
|
-
export interface SearchResults<Schema, Table extends string & keyof Schema, Joins extends SearchJoins<Schema> = {}> {
|
|
129
|
-
/** The total length of all available results (without offset or limit) */
|
|
130
|
-
total: number;
|
|
131
|
-
/** The lines queried (truncated by offset and limit) */
|
|
132
|
-
rows: SearchResult<Schema, Table, Joins>[];
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* An object to perform searches on a given table in our {@link Persister}
|
|
136
|
-
*/
|
|
137
|
-
export interface Search<Schema, Table extends string & keyof Schema, Joins extends SearchJoins<Schema>> {
|
|
7
|
+
export interface Search<Schema, Table extends string & keyof Schema, Joins extends SearchJoins<Schema>, TextSearch extends boolean> {
|
|
138
8
|
/**
|
|
139
9
|
* Return the first result (if any) matching the specified query.
|
|
140
10
|
*
|
|
141
11
|
* This will intrinsically limit the search to 1 result.
|
|
142
12
|
*
|
|
143
13
|
* @param query The query to filter results by
|
|
144
|
-
* @param
|
|
14
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
145
15
|
* @returns The first matching result, or `undefined` if no results matched
|
|
146
16
|
*/
|
|
147
|
-
find(query: SearchQuery<Schema, Table, Joins>,
|
|
17
|
+
find(query: SearchQuery<Schema, Table, Joins, TextSearch>, where?: PGQuery): Promise<SearchResult<Schema, Table, Joins> | undefined>;
|
|
148
18
|
/**
|
|
149
19
|
* Return the raw SQL query and parameters for the specified options.
|
|
150
20
|
*
|
|
151
21
|
* @param options The search options to generate SQL for
|
|
152
|
-
* @param
|
|
22
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
153
23
|
* @returns A tuple containing the SQL string and its parameters
|
|
154
24
|
*/
|
|
155
|
-
query(options: SearchOptions<Schema, Table, Joins>,
|
|
25
|
+
query(options: SearchOptions<Schema, Table, Joins, TextSearch>, where?: PGQuery): [sql: string, params: any[]];
|
|
156
26
|
/**
|
|
157
27
|
* Perform a search with the specified options.
|
|
158
28
|
*
|
|
159
29
|
* @param options The search options to use
|
|
160
|
-
* @param
|
|
30
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
161
31
|
* @returns The search results
|
|
162
32
|
*/
|
|
163
|
-
search(options: SearchOptions<Schema, Table, Joins>,
|
|
33
|
+
search(options: SearchOptions<Schema, Table, Joins, TextSearch>, where?: PGQuery): Promise<SearchResults<Schema, Table, Joins>>;
|
|
164
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* A query provider for models
|
|
37
|
+
*/
|
|
38
|
+
export type SearchProvider<Schema = any> = Persister<Schema> | Connection<Schema>;
|
|
165
39
|
/**
|
|
166
40
|
* A constructor for our {@link Search} object
|
|
167
41
|
*/
|
|
168
42
|
export interface SearchConstructor {
|
|
169
43
|
/**
|
|
170
|
-
* Construct a {@link Search} object using the specified
|
|
171
|
-
* operating on the
|
|
44
|
+
* Construct a {@link Search} object using the specified
|
|
45
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
46
|
+
* specified table.
|
|
172
47
|
*
|
|
173
|
-
* @param
|
|
48
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
174
49
|
* @param table The table to perform searches on
|
|
175
50
|
*/
|
|
176
|
-
new <P extends
|
|
51
|
+
new <P extends SearchProvider, T extends string & (P extends SearchProvider<infer S> ? keyof S : never)>(provider: P, table: T): Search<P extends SearchProvider<infer S> ? S : never, T, {}, false>;
|
|
177
52
|
/**
|
|
178
|
-
* Construct a {@link Search} object using the specified
|
|
179
|
-
*
|
|
53
|
+
* Construct a {@link Search} object using the specified
|
|
54
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
55
|
+
* specified table, and using the specified full-text search
|
|
180
56
|
* column (TSVECTOR) to perform `q` searches.
|
|
181
57
|
*
|
|
182
|
-
* @param
|
|
58
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
183
59
|
* @param table The table to perform searches on
|
|
184
60
|
* @param fullTextSearchColumn The column to use for full-text searches
|
|
185
61
|
*/
|
|
186
|
-
new <P extends
|
|
62
|
+
new <P extends SearchProvider, T extends string & (P extends SearchProvider<infer S> ? keyof S : never)>(provider: P, table: T, fullTextSearchColumn: string): Search<P extends SearchProvider<infer S> ? S : never, T, {}, true>;
|
|
187
63
|
/**
|
|
188
|
-
* Construct a {@link Search} object using the specified
|
|
189
|
-
*
|
|
64
|
+
* Construct a {@link Search} object using the specified
|
|
65
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
66
|
+
* specified table, joining external tables.
|
|
190
67
|
*
|
|
191
|
-
* @param
|
|
68
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
192
69
|
* @param table The table to perform searches on
|
|
193
70
|
* @param joins The joins to perform
|
|
194
71
|
*/
|
|
195
|
-
new <P extends
|
|
72
|
+
new <P extends SearchProvider, T extends string & (P extends SearchProvider<infer S> ? keyof S : never), J extends SearchJoins<P extends SearchProvider<infer S> ? S : never>>(provider: P, table: T, joins: J): Search<P extends SearchProvider<infer S> ? S : never, T, J, false>;
|
|
196
73
|
/**
|
|
197
|
-
* Construct a {@link Search} object using the specified
|
|
198
|
-
*
|
|
199
|
-
* specified
|
|
74
|
+
* Construct a {@link Search} object using the specified
|
|
75
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
76
|
+
* specified table, joining external tables, and using the specified full-text
|
|
77
|
+
* search column (TSVECTOR) to perform `q` searches.
|
|
200
78
|
*
|
|
201
|
-
* @param
|
|
79
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
202
80
|
* @param table The table to perform searches on
|
|
203
81
|
* @param joins The joins to perform
|
|
204
82
|
* @param fullTextSearchColumn The column to use for full-text searches
|
|
205
83
|
*/
|
|
206
|
-
new <P extends
|
|
84
|
+
new <P extends SearchProvider, T extends string & (P extends SearchProvider<infer S> ? keyof S : never), J extends SearchJoins<P extends SearchProvider<infer S> ? S : never>>(provider: P, table: T, joins: J, fullTextSearchColumn: string): Search<P extends SearchProvider<infer S> ? S : never, T, J, true>;
|
|
207
85
|
}
|
|
208
86
|
/** Revive a JSON, parsing ISO dates as {@link Date} objects */
|
|
209
87
|
export declare function reviver(_key: string, data: any): any;
|
|
210
88
|
export declare const Search: SearchConstructor;
|
|
211
|
-
export {};
|
package/dist/search.mjs
CHANGED
|
@@ -7,16 +7,16 @@ function reviver(_key, data) {
|
|
|
7
7
|
return data;
|
|
8
8
|
}
|
|
9
9
|
var SearchImpl = class {
|
|
10
|
-
/** Our
|
|
11
|
-
#
|
|
10
|
+
/** Our search provider instance */
|
|
11
|
+
#provider;
|
|
12
12
|
/** The escaped table name */
|
|
13
13
|
#eTable;
|
|
14
14
|
/** The escaped joins */
|
|
15
15
|
#eJoins;
|
|
16
16
|
/** The full-text search column (if any) */
|
|
17
17
|
#fullTextSearchColumn;
|
|
18
|
-
constructor(
|
|
19
|
-
this.#
|
|
18
|
+
constructor(provider, table, joinsOrFullTextSearchColumn, maybeFullTextSearchColumn) {
|
|
19
|
+
this.#provider = provider;
|
|
20
20
|
this.#eTable = encodeSchemaAndName(table);
|
|
21
21
|
let joins = {};
|
|
22
22
|
let fullTextSearchColumn = void 0;
|
|
@@ -52,8 +52,8 @@ var SearchImpl = class {
|
|
|
52
52
|
const orderby = [];
|
|
53
53
|
const params = [];
|
|
54
54
|
if (extra) {
|
|
55
|
-
where.push(extra.
|
|
56
|
-
params.push(...extra.params);
|
|
55
|
+
where.push(extra.query);
|
|
56
|
+
if (extra.params) params.push(...extra.params);
|
|
57
57
|
}
|
|
58
58
|
let esearch = "";
|
|
59
59
|
if (count === "only") {
|
|
@@ -155,23 +155,23 @@ var SearchImpl = class {
|
|
|
155
155
|
if (limit) sql += ` LIMIT $${params.push(limit)}`;
|
|
156
156
|
return [sql, params];
|
|
157
157
|
}
|
|
158
|
-
query(options,
|
|
159
|
-
return this.#query(false, options,
|
|
158
|
+
query(options, where) {
|
|
159
|
+
return this.#query(false, options, where);
|
|
160
160
|
}
|
|
161
|
-
async find(options,
|
|
162
|
-
const [sql, params] = this.#query(false, { ...options, offset: 0, limit: 1 },
|
|
163
|
-
const result = await this.#
|
|
161
|
+
async find(options, where) {
|
|
162
|
+
const [sql, params] = this.#query(false, { ...options, offset: 0, limit: 1 }, where);
|
|
163
|
+
const result = await this.#provider.query(sql, params);
|
|
164
164
|
if (result.rows[0]) return JSON.parse(result.rows[0].result, reviver);
|
|
165
165
|
return void 0;
|
|
166
166
|
}
|
|
167
|
-
async search(options,
|
|
168
|
-
const [sql, params] = this.#query(true, options,
|
|
169
|
-
const result = await this.#
|
|
167
|
+
async search(options, where) {
|
|
168
|
+
const [sql, params] = this.#query(true, options, where);
|
|
169
|
+
const result = await this.#provider.query(sql, params).catch((error) => {
|
|
170
170
|
throw new Error(`Error executing search query: ${error.message}`, { cause: { sql, params, error } });
|
|
171
171
|
});
|
|
172
172
|
if (result.rows.length === 0 && (options.offset || 0) > 0) {
|
|
173
|
-
const [sql2, params2] = this.#query("only", { ...options, offset: 0, limit: void 0 },
|
|
174
|
-
const result2 = await this.#
|
|
173
|
+
const [sql2, params2] = this.#query("only", { ...options, offset: 0, limit: void 0 }, where);
|
|
174
|
+
const result2 = await this.#provider.query(sql2, params2);
|
|
175
175
|
assert(result2.rows[0], "Expected total row in count query");
|
|
176
176
|
const total2 = Number(result2.rows[0].total);
|
|
177
177
|
return { total: total2, rows: [] };
|
package/dist/search.mjs.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/search.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,cAAc;AAEvB,SAAS,QAAQ,2BAA2B;
|
|
4
|
+
"mappings": ";AAAA,SAAS,cAAc;AAEvB,SAAS,QAAQ,2BAA2B;AAmJ5C,IAAM,SAAS;AAGR,SAAS,QAAQ,MAAc,MAAgB;AACpD,MAAK,OAAO,SAAS,YAAa,OAAO,KAAK,IAAI,EAAG,QAAO,IAAI,KAAK,IA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5
|
"names": ["evalue", "sql", "params", "result", "total"]
|
|
6
6
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juit/pgproxy-persister",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"main": "./dist/index.cjs",
|
|
5
5
|
"module": "./dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -14,16 +14,6 @@
|
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"default": "./dist/index.mjs"
|
|
16
16
|
}
|
|
17
|
-
},
|
|
18
|
-
"./schema": {
|
|
19
|
-
"require": {
|
|
20
|
-
"types": "./dist/schema.d.ts",
|
|
21
|
-
"default": "./dist/schema.cjs"
|
|
22
|
-
},
|
|
23
|
-
"import": {
|
|
24
|
-
"types": "./dist/schema.d.ts",
|
|
25
|
-
"default": "./dist/schema.mjs"
|
|
26
|
-
}
|
|
27
17
|
}
|
|
28
18
|
},
|
|
29
19
|
"scripts": {},
|
|
@@ -53,7 +43,8 @@
|
|
|
53
43
|
"src/"
|
|
54
44
|
],
|
|
55
45
|
"dependencies": {
|
|
56
|
-
"@juit/pgproxy-client": "1.4.
|
|
57
|
-
"@juit/pgproxy-
|
|
46
|
+
"@juit/pgproxy-client": "1.4.3",
|
|
47
|
+
"@juit/pgproxy-model": "1.4.3",
|
|
48
|
+
"@juit/pgproxy-types": "1.4.3"
|
|
58
49
|
}
|
|
59
50
|
}
|
package/src/index.ts
CHANGED
package/src/model.ts
CHANGED
|
@@ -3,87 +3,14 @@ import { escape } from '@juit/pgproxy-client'
|
|
|
3
3
|
import { assert, assertArray, assertObject, encodeSchemaAndName } from './utils'
|
|
4
4
|
|
|
5
5
|
import type { PGQueryable } from '@juit/pgproxy-client'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/** The definition of a column */
|
|
15
|
-
export interface ColumnDefinition<T = any> {
|
|
16
|
-
/** The TypeScript type of the column (from the type parser) */
|
|
17
|
-
type: T,
|
|
18
|
-
/** Whether the column is _generated_ or not */
|
|
19
|
-
isGenerated?: boolean,
|
|
20
|
-
/** Whether the column is _nullable_ or not */
|
|
21
|
-
isNullable?: boolean,
|
|
22
|
-
/** Whether the column _specifies a default value_ or not */
|
|
23
|
-
hasDefault?: boolean,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Infer the TypeScript type suitable for an `INSERT` in a table */
|
|
27
|
-
export type InferInsertType<Table extends Record<string, ColumnDefinition>> =
|
|
28
|
-
SimplifyIntersection<{
|
|
29
|
-
/* First part: all nullable or defaulted columns are optional */
|
|
30
|
-
[ Column in keyof Table as Column extends string
|
|
31
|
-
? Table[Column]['isGenerated'] extends true ? never
|
|
32
|
-
: Table[Column]['isNullable'] extends true ? Column
|
|
33
|
-
: Table[Column]['hasDefault'] extends true ? Column
|
|
34
|
-
: never
|
|
35
|
-
: never
|
|
36
|
-
] ? :
|
|
37
|
-
Table[Column]['isNullable'] extends true
|
|
38
|
-
? Table[Column]['type'] | null
|
|
39
|
-
: Table[Column]['type']
|
|
40
|
-
} & {
|
|
41
|
-
/* Second part: all non-nullable or non-defaulted columns are required */
|
|
42
|
-
[ Column in keyof Table as Column extends string
|
|
43
|
-
? Table[Column]['isGenerated'] extends true ? never
|
|
44
|
-
: Table[Column]['isNullable'] extends true ? never
|
|
45
|
-
: Table[Column]['hasDefault'] extends true ? never
|
|
46
|
-
: Column
|
|
47
|
-
: never
|
|
48
|
-
] -? :
|
|
49
|
-
Table[Column]['isNullable'] extends true
|
|
50
|
-
? Table[Column]['type'] | null
|
|
51
|
-
: Table[Column]['type']
|
|
52
|
-
}>
|
|
53
|
-
|
|
54
|
-
/** Infer the TypeScript type suitable for a `SELECT` from a table */
|
|
55
|
-
export type InferSelectType<Table extends Record<string, ColumnDefinition>> =
|
|
56
|
-
{ [ Column in keyof Table as Column extends string ? Column : never ] -? :
|
|
57
|
-
( Table[Column]['isNullable'] extends true ?
|
|
58
|
-
Table[Column]['type'] | null :
|
|
59
|
-
Table[Column]['type']
|
|
60
|
-
) & ( Table[Column] extends { branding: infer Brand } ? Brand : unknown )
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Infer the TypeScript type suitable for a `UPDATE` in a table */
|
|
65
|
-
export type InferUpdateType<Table extends Record<string, ColumnDefinition>> ={
|
|
66
|
-
[ Column in keyof Table as Column extends string
|
|
67
|
-
? Table[Column]['isGenerated'] extends true ? never
|
|
68
|
-
: Column
|
|
69
|
-
: never
|
|
70
|
-
] ? :
|
|
71
|
-
Table[Column]['isNullable'] extends true ?
|
|
72
|
-
Table[Column]['type'] | null :
|
|
73
|
-
Table[Column]['type']
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Infer the TypeScript type used for querying records */
|
|
77
|
-
export type InferQueryType<Table extends Record<string, ColumnDefinition>> =
|
|
78
|
-
{ [ Column in keyof Table as Column extends string ? Column : never ] ? :
|
|
79
|
-
Table[Column]['isNullable'] extends true ?
|
|
80
|
-
Table[Column]['type'] | null :
|
|
81
|
-
Table[Column]['type']
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Infer the available sort values for a table (as required by `ORDER BY`) */
|
|
85
|
-
export type InferSort<Table extends Record<string, ColumnDefinition>> =
|
|
86
|
-
`${OnlyStrings<keyof Table>}${' ASC' | ' asc' | ' DESC' | ' desc' | ''}`
|
|
6
|
+
import type {
|
|
7
|
+
ColumnDefinition,
|
|
8
|
+
InferInsertType,
|
|
9
|
+
InferQueryType,
|
|
10
|
+
InferSelectType,
|
|
11
|
+
InferSort,
|
|
12
|
+
InferUpdateType,
|
|
13
|
+
} from '@juit/pgproxy-model'
|
|
87
14
|
|
|
88
15
|
/* ========================================================================== *
|
|
89
16
|
* MODEL INTERFACE *
|
package/src/persister.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { PGClient } from '@juit/pgproxy-client'
|
|
|
3
3
|
import { Model } from './model'
|
|
4
4
|
|
|
5
5
|
import type { PGClientOptions, PGConnection, PGQuery, PGResult, PGTransactionable } from '@juit/pgproxy-client'
|
|
6
|
+
import type { ColumnDefinition } from '@juit/pgproxy-model'
|
|
6
7
|
import type { Registry } from '@juit/pgproxy-types'
|
|
7
|
-
import type { ColumnDefinition } from './model'
|
|
8
8
|
|
|
9
9
|
/* ========================================================================== *
|
|
10
10
|
* TYPES *
|
package/src/search.ts
CHANGED
|
@@ -2,211 +2,29 @@ import { escape } from '@juit/pgproxy-client'
|
|
|
2
2
|
|
|
3
3
|
import { assert, encodeSchemaAndName } from './utils'
|
|
4
4
|
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
5
|
+
import type { PGQuery } from '@juit/pgproxy-client'
|
|
6
|
+
import type {
|
|
7
|
+
SearchJoin,
|
|
8
|
+
SearchJoins,
|
|
9
|
+
SearchOptions,
|
|
10
|
+
SearchQuery,
|
|
11
|
+
SearchResult,
|
|
12
|
+
SearchResults,
|
|
13
|
+
} from '@juit/pgproxy-model'
|
|
14
|
+
import type { Connection, Persister } from './persister'
|
|
7
15
|
|
|
8
16
|
/* ========================================================================== *
|
|
9
17
|
* TYPES & INTERFACES *
|
|
10
18
|
* ========================================================================== */
|
|
11
19
|
|
|
12
|
-
/* ===== JOINS ============================================================== */
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Definition for a simple (straight) join in a {@link Search}
|
|
16
|
-
*/
|
|
17
|
-
export interface SearchJoin<Schema> {
|
|
18
|
-
/**
|
|
19
|
-
* The column in _the search table_ (passed to the constructor of
|
|
20
|
-
* {@link Search}) referencing the specified `refTable` (defined here).
|
|
21
|
-
*
|
|
22
|
-
* ```sql
|
|
23
|
-
* ... LEFT JOIN "refTable" ON "table"."column" = "refTable"."refColumn"
|
|
24
|
-
* ^^^^^^
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
column: string
|
|
28
|
-
/**
|
|
29
|
-
* The name of the table to _left join_.
|
|
30
|
-
*
|
|
31
|
-
* ```sql
|
|
32
|
-
* ... LEFT JOIN "refTable" ON "table"."column" = "refTable"."refColumn"
|
|
33
|
-
* ^^^^^^^^ ^^^^^^^^
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
refTable: string & keyof Schema
|
|
37
|
-
/**
|
|
38
|
-
* The column in the `refTable` referenced by the _the search table_.
|
|
39
|
-
*
|
|
40
|
-
* ```sql
|
|
41
|
-
* ... LEFT JOIN "refTable" ON "table"."column" = "refTable"."refColumn"
|
|
42
|
-
* ^^^^^^^^^
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
refColumn: string
|
|
46
|
-
/**
|
|
47
|
-
* The column in the referenced table to use as default sort column, when
|
|
48
|
-
* sorting by this join.
|
|
49
|
-
*/
|
|
50
|
-
sortColumn?: string
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Definition for joins in a {@link Search}
|
|
55
|
-
*
|
|
56
|
-
* Each key is the name of the join as it will appear in the results, and the
|
|
57
|
-
* value defines how to perform the join.
|
|
58
|
-
*
|
|
59
|
-
* See {@link StraightJoin} and {@link LinkedJoin} for details on the fields.
|
|
60
|
-
*/
|
|
61
|
-
export interface SearchJoins<Schema> {
|
|
62
|
-
[ key: string ]: SearchJoin<Schema>
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/* ===== SEARCH OPTIONS ===================================================== */
|
|
66
|
-
|
|
67
|
-
/** Internal interface defining operators available to *single values* */
|
|
68
|
-
interface ValueSearchFilter<
|
|
69
|
-
Schema,
|
|
70
|
-
Table extends string & keyof Schema,
|
|
71
|
-
> {
|
|
72
|
-
name: string & keyof Schema[Table]
|
|
73
|
-
field?: string
|
|
74
|
-
op?: '=' | '!=' | '>' | '>=' | '<' | '<=' | '~' | 'like' | 'ilike'
|
|
75
|
-
value: string | number | Date | boolean | null
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Internal interface defining operators available to *array values* */
|
|
79
|
-
interface ArraySearchFilter<
|
|
80
|
-
Schema,
|
|
81
|
-
Table extends string & keyof Schema,
|
|
82
|
-
> {
|
|
83
|
-
name: string & keyof Schema[Table]
|
|
84
|
-
field?: string
|
|
85
|
-
op: 'in' | 'not in'
|
|
86
|
-
value: (string | number | Date | boolean | null)[]
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** Internal interface defining operators available to *json values* */
|
|
90
|
-
interface JsonSearchFilter<
|
|
91
|
-
Schema,
|
|
92
|
-
Table extends string & keyof Schema,
|
|
93
|
-
> {
|
|
94
|
-
name: string & keyof Schema[Table]
|
|
95
|
-
field?: never
|
|
96
|
-
op: '@>' | '<@'
|
|
97
|
-
value: any
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* A filter for a search that matches a single value
|
|
102
|
-
*
|
|
103
|
-
* - `name` is the column name to filter on
|
|
104
|
-
* - `field` is a field to filter on when the column is a complex type (JSONB)
|
|
105
|
-
* - `op` is the operator to use for the filter (default: `=`)
|
|
106
|
-
* - `value` is the value to filter for
|
|
107
|
-
*
|
|
108
|
-
* All operators are defined as per PostgreSQL documentation, with few notable
|
|
109
|
-
* exceptions:
|
|
110
|
-
*
|
|
111
|
-
* - `~` is an alias to the `ilike` operator
|
|
112
|
-
* - `in` and `not in` are used to match a value against an array of possible
|
|
113
|
-
* values using the `... = ANY(...)` or `... != ALL(...)` constructs
|
|
114
|
-
* - `@>` and `<@` will accept single values as well as arrays.
|
|
115
|
-
* - `!=` and `=` will use the PostgreSQL `IS (NOT) DISTINCT FROM` semantics
|
|
116
|
-
* to properly handle `NULL` comparisons.
|
|
117
|
-
*/
|
|
118
|
-
export type SearchFilter<
|
|
119
|
-
Schema,
|
|
120
|
-
Table extends string & keyof Schema,
|
|
121
|
-
> = ValueSearchFilter<Schema, Table> | ArraySearchFilter<Schema, Table> | JsonSearchFilter<Schema, Table>
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Base interface for querying results via our {@link Search}.
|
|
125
|
-
*/
|
|
126
|
-
export interface SearchQuery<
|
|
127
|
-
Schema,
|
|
128
|
-
Table extends string & keyof Schema,
|
|
129
|
-
Joins extends SearchJoins<Schema> = {},
|
|
130
|
-
> {
|
|
131
|
-
/** An optional set of filters to apply */
|
|
132
|
-
filters?: SearchFilter<Schema, Table>[]
|
|
133
|
-
/** An optional column to sort by */
|
|
134
|
-
sort?: string & (keyof Schema[Table] | keyof Joins)
|
|
135
|
-
/** The order to sort by (if `sort` is specified, default: 'asc') */
|
|
136
|
-
order?: 'asc' | 'desc'
|
|
137
|
-
/** An optional full-text search query, available for full-text search */
|
|
138
|
-
q?: string
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Full options for querying a limited set of results via our {@link Search}.
|
|
143
|
-
*/
|
|
144
|
-
export interface SearchOptions<
|
|
145
|
-
Schema,
|
|
146
|
-
Table extends string & keyof Schema,
|
|
147
|
-
Joins extends SearchJoins<Schema> = {},
|
|
148
|
-
> extends SearchQuery<Schema, Table, Joins> {
|
|
149
|
-
/** Offset to start returning rows from (default: 0) */
|
|
150
|
-
offset?: number
|
|
151
|
-
/** Maximum number of rows to return (default: 20, unlimited if 0) */
|
|
152
|
-
limit?: number
|
|
153
|
-
}
|
|
154
|
-
|
|
155
20
|
/**
|
|
156
|
-
*
|
|
157
|
-
*/
|
|
158
|
-
export interface SearchExtra {
|
|
159
|
-
/** Extra `WHERE` clause to add to our search */
|
|
160
|
-
where: string
|
|
161
|
-
/** Parameters for the extra `WHERE` clause */
|
|
162
|
-
params: any[]
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/* ===== SEARCH RESULTS ===================================================== */
|
|
166
|
-
|
|
167
|
-
/** A single search result row (with joins) */
|
|
168
|
-
export type SearchResult<
|
|
169
|
-
Schema,
|
|
170
|
-
Table extends string & keyof Schema,
|
|
171
|
-
Joins extends SearchJoins<Schema> = {},
|
|
172
|
-
> =
|
|
173
|
-
Schema[Table] extends Record<string, ColumnDefinition> ?
|
|
174
|
-
// This is the main table's column field
|
|
175
|
-
InferSelectType<Schema[Table]> & {
|
|
176
|
-
// For each join, add a field with the joined table's inferred type
|
|
177
|
-
[ key in keyof Joins ] : Joins[key]['refTable'] extends keyof Schema ?
|
|
178
|
-
// If the column referencing this join is nullable, the result can be null
|
|
179
|
-
Schema[Joins[key]['refTable']] extends Record<string, ColumnDefinition> ?
|
|
180
|
-
Schema[Table][Joins[key]['column']]['isNullable'] extends true ?
|
|
181
|
-
InferSelectType<Schema[Joins[key]['refTable']]> | null :
|
|
182
|
-
InferSelectType<Schema[Joins[key]['refTable']]> :
|
|
183
|
-
// If the joined table isn't a column def, we can't infer anything
|
|
184
|
-
unknown :
|
|
185
|
-
// If the table doesn't exist in the schema, we can't infer anything
|
|
186
|
-
unknown
|
|
187
|
-
} : never
|
|
188
|
-
|
|
189
|
-
/** What's being returned by our `search` */
|
|
190
|
-
export interface SearchResults<
|
|
191
|
-
Schema,
|
|
192
|
-
Table extends string & keyof Schema,
|
|
193
|
-
Joins extends SearchJoins<Schema> = {},
|
|
194
|
-
> {
|
|
195
|
-
/** The total length of all available results (without offset or limit) */
|
|
196
|
-
total: number
|
|
197
|
-
/** The lines queried (truncated by offset and limit) */
|
|
198
|
-
rows: SearchResult<Schema, Table, Joins>[]
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/* ===== SEARCH ============================================================= */
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* An object to perform searches on a given table in our {@link Persister}
|
|
21
|
+
* An object to perform searches on a given table.
|
|
205
22
|
*/
|
|
206
23
|
export interface Search<
|
|
207
24
|
Schema,
|
|
208
25
|
Table extends string & keyof Schema,
|
|
209
26
|
Joins extends SearchJoins<Schema>,
|
|
27
|
+
TextSearch extends boolean,
|
|
210
28
|
> {
|
|
211
29
|
/**
|
|
212
30
|
* Return the first result (if any) matching the specified query.
|
|
@@ -214,105 +32,114 @@ export interface Search<
|
|
|
214
32
|
* This will intrinsically limit the search to 1 result.
|
|
215
33
|
*
|
|
216
34
|
* @param query The query to filter results by
|
|
217
|
-
* @param
|
|
35
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
218
36
|
* @returns The first matching result, or `undefined` if no results matched
|
|
219
37
|
*/
|
|
220
|
-
find(query: SearchQuery<Schema, Table, Joins>,
|
|
38
|
+
find(query: SearchQuery<Schema, Table, Joins, TextSearch>, where?: PGQuery): Promise<SearchResult<Schema, Table, Joins> | undefined>
|
|
221
39
|
|
|
222
40
|
/**
|
|
223
41
|
* Return the raw SQL query and parameters for the specified options.
|
|
224
42
|
*
|
|
225
43
|
* @param options The search options to generate SQL for
|
|
226
|
-
* @param
|
|
44
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
227
45
|
* @returns A tuple containing the SQL string and its parameters
|
|
228
46
|
*/
|
|
229
|
-
query(options: SearchOptions<Schema, Table, Joins>,
|
|
47
|
+
query(options: SearchOptions<Schema, Table, Joins, TextSearch>, where?: PGQuery): [ sql: string, params: any[] ]
|
|
230
48
|
|
|
231
49
|
/**
|
|
232
50
|
* Perform a search with the specified options.
|
|
233
51
|
*
|
|
234
52
|
* @param options The search options to use
|
|
235
|
-
* @param
|
|
53
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
236
54
|
* @returns The search results
|
|
237
55
|
*/
|
|
238
|
-
search(options: SearchOptions<Schema, Table, Joins>,
|
|
56
|
+
search(options: SearchOptions<Schema, Table, Joins, TextSearch>, where?: PGQuery): Promise<SearchResults<Schema, Table, Joins>>
|
|
239
57
|
}
|
|
240
58
|
|
|
59
|
+
/**
|
|
60
|
+
* A query provider for models
|
|
61
|
+
*/
|
|
62
|
+
export type SearchProvider<Schema = any> = Persister<Schema> | Connection<Schema>
|
|
63
|
+
|
|
241
64
|
/**
|
|
242
65
|
* A constructor for our {@link Search} object
|
|
243
66
|
*/
|
|
244
67
|
export interface SearchConstructor {
|
|
245
68
|
/**
|
|
246
|
-
* Construct a {@link Search} object using the specified
|
|
247
|
-
* operating on the
|
|
69
|
+
* Construct a {@link Search} object using the specified
|
|
70
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
71
|
+
* specified table.
|
|
248
72
|
*
|
|
249
|
-
* @param
|
|
73
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
250
74
|
* @param table The table to perform searches on
|
|
251
75
|
*/
|
|
252
76
|
new<
|
|
253
|
-
P extends
|
|
254
|
-
T extends string & (P extends
|
|
77
|
+
P extends SearchProvider,
|
|
78
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
255
79
|
>(
|
|
256
|
-
|
|
80
|
+
provider: P,
|
|
257
81
|
table: T,
|
|
258
|
-
): Search<P extends
|
|
82
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, {}, false>;
|
|
259
83
|
|
|
260
84
|
/**
|
|
261
|
-
* Construct a {@link Search} object using the specified
|
|
262
|
-
*
|
|
85
|
+
* Construct a {@link Search} object using the specified
|
|
86
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
87
|
+
* specified table, and using the specified full-text search
|
|
263
88
|
* column (TSVECTOR) to perform `q` searches.
|
|
264
89
|
*
|
|
265
|
-
* @param
|
|
90
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
266
91
|
* @param table The table to perform searches on
|
|
267
92
|
* @param fullTextSearchColumn The column to use for full-text searches
|
|
268
93
|
*/
|
|
269
94
|
new<
|
|
270
|
-
P extends
|
|
271
|
-
T extends string & (P extends
|
|
95
|
+
P extends SearchProvider,
|
|
96
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
272
97
|
>(
|
|
273
|
-
|
|
98
|
+
provider: P,
|
|
274
99
|
table: T,
|
|
275
100
|
fullTextSearchColumn: string,
|
|
276
|
-
): Search<P extends
|
|
101
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, {}, true>;
|
|
277
102
|
|
|
278
103
|
/**
|
|
279
|
-
* Construct a {@link Search} object using the specified
|
|
280
|
-
*
|
|
104
|
+
* Construct a {@link Search} object using the specified
|
|
105
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
106
|
+
* specified table, joining external tables.
|
|
281
107
|
*
|
|
282
|
-
* @param
|
|
108
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
283
109
|
* @param table The table to perform searches on
|
|
284
110
|
* @param joins The joins to perform
|
|
285
111
|
*/
|
|
286
112
|
new<
|
|
287
|
-
P extends
|
|
288
|
-
T extends string & (P extends
|
|
289
|
-
J extends SearchJoins<P extends
|
|
113
|
+
P extends SearchProvider,
|
|
114
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
115
|
+
J extends SearchJoins<P extends SearchProvider<infer S> ? S : never>,
|
|
290
116
|
>(
|
|
291
|
-
|
|
117
|
+
provider: P,
|
|
292
118
|
table: T,
|
|
293
119
|
joins: J,
|
|
294
|
-
): Search<P extends
|
|
120
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, J, false>;
|
|
295
121
|
|
|
296
122
|
/**
|
|
297
|
-
* Construct a {@link Search} object using the specified
|
|
298
|
-
*
|
|
299
|
-
* specified
|
|
123
|
+
* Construct a {@link Search} object using the specified
|
|
124
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
125
|
+
* specified table, joining external tables, and using the specified full-text
|
|
126
|
+
* search column (TSVECTOR) to perform `q` searches.
|
|
300
127
|
*
|
|
301
|
-
* @param
|
|
128
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
302
129
|
* @param table The table to perform searches on
|
|
303
130
|
* @param joins The joins to perform
|
|
304
131
|
* @param fullTextSearchColumn The column to use for full-text searches
|
|
305
132
|
*/
|
|
306
133
|
new<
|
|
307
|
-
P extends
|
|
308
|
-
T extends string & (P extends
|
|
309
|
-
J extends SearchJoins<P extends
|
|
134
|
+
P extends SearchProvider,
|
|
135
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
136
|
+
J extends SearchJoins<P extends SearchProvider<infer S> ? S : never>,
|
|
310
137
|
>(
|
|
311
|
-
|
|
138
|
+
provider: P,
|
|
312
139
|
table: T,
|
|
313
140
|
joins: J,
|
|
314
141
|
fullTextSearchColumn: string,
|
|
315
|
-
): Search<P extends
|
|
142
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, J, true>;
|
|
316
143
|
}
|
|
317
144
|
|
|
318
145
|
/* ========================================================================== *
|
|
@@ -332,9 +159,9 @@ class SearchImpl<
|
|
|
332
159
|
Schema,
|
|
333
160
|
Table extends string & keyof Schema,
|
|
334
161
|
Joins extends SearchJoins<Schema> = {},
|
|
335
|
-
> implements Search<Schema, Table, Joins> {
|
|
336
|
-
/** Our
|
|
337
|
-
#
|
|
162
|
+
> implements Search<Schema, Table, Joins, true> {
|
|
163
|
+
/** Our search provider instance */
|
|
164
|
+
#provider: SearchProvider<Schema>
|
|
338
165
|
/** The escaped table name */
|
|
339
166
|
#eTable: string
|
|
340
167
|
/** The escaped joins */
|
|
@@ -342,18 +169,18 @@ class SearchImpl<
|
|
|
342
169
|
/** The full-text search column (if any) */
|
|
343
170
|
#fullTextSearchColumn: string | undefined
|
|
344
171
|
|
|
345
|
-
constructor(
|
|
346
|
-
constructor(
|
|
347
|
-
constructor(
|
|
348
|
-
constructor(
|
|
172
|
+
constructor(provider: SearchProvider<Schema>, table: Table)
|
|
173
|
+
constructor(provider: SearchProvider<Schema>, table: Table, fullTextSearchColumn: string)
|
|
174
|
+
constructor(provider: SearchProvider<Schema>, table: Table, joins: Joins)
|
|
175
|
+
constructor(provider: SearchProvider<Schema>, table: Table, joins: Joins, fullTextSearchColumn: string)
|
|
349
176
|
|
|
350
177
|
constructor(
|
|
351
|
-
|
|
178
|
+
provider: SearchProvider<Schema>,
|
|
352
179
|
table: Table,
|
|
353
180
|
joinsOrFullTextSearchColumn?: Joins | string,
|
|
354
181
|
maybeFullTextSearchColumn?: string,
|
|
355
182
|
) {
|
|
356
|
-
this.#
|
|
183
|
+
this.#provider = provider
|
|
357
184
|
this.#eTable = encodeSchemaAndName(table)
|
|
358
185
|
|
|
359
186
|
let joins: Joins = {} as Joins
|
|
@@ -380,8 +207,8 @@ class SearchImpl<
|
|
|
380
207
|
|
|
381
208
|
#query(
|
|
382
209
|
count: boolean | 'only',
|
|
383
|
-
options: SearchOptions<Schema, Table, Joins>,
|
|
384
|
-
extra?:
|
|
210
|
+
options: SearchOptions<Schema, Table, Joins, true>,
|
|
211
|
+
extra?: PGQuery,
|
|
385
212
|
): [ sql: string, params: any[] ] {
|
|
386
213
|
const {
|
|
387
214
|
offset = 0,
|
|
@@ -403,8 +230,8 @@ class SearchImpl<
|
|
|
403
230
|
// Extra manual SQL *always* goes FIRST in our WHERE clause, its
|
|
404
231
|
// parameters always start at $1
|
|
405
232
|
if (extra) {
|
|
406
|
-
where.push(extra.
|
|
407
|
-
params.push(...extra.params)
|
|
233
|
+
where.push(extra.query)
|
|
234
|
+
if (extra.params) params.push(...extra.params)
|
|
408
235
|
}
|
|
409
236
|
|
|
410
237
|
let esearch = '' // falsy!
|
|
@@ -545,28 +372,28 @@ class SearchImpl<
|
|
|
545
372
|
return [ sql, params ]
|
|
546
373
|
}
|
|
547
374
|
|
|
548
|
-
query(options: SearchOptions<Schema, Table, Joins>,
|
|
549
|
-
return this.#query(false, options,
|
|
375
|
+
query(options: SearchOptions<Schema, Table, Joins, true>, where?: PGQuery): [ sql: string, params: any[] ] {
|
|
376
|
+
return this.#query(false, options, where)
|
|
550
377
|
}
|
|
551
378
|
|
|
552
|
-
async find(options: SearchQuery<Schema, Table, Joins>,
|
|
553
|
-
const [ sql, params ] = this.#query(false, { ...options, offset: 0, limit: 1 },
|
|
379
|
+
async find(options: SearchQuery<Schema, Table, Joins, true>, where?: PGQuery): Promise<SearchResult<Schema, Table, Joins> | undefined> {
|
|
380
|
+
const [ sql, params ] = this.#query(false, { ...options, offset: 0, limit: 1 }, where)
|
|
554
381
|
|
|
555
|
-
const result = await this.#
|
|
382
|
+
const result = await this.#provider.query<{ total?: number, result: string }>(sql, params)
|
|
556
383
|
if (result.rows[0]) return JSON.parse(result.rows[0].result, reviver)
|
|
557
384
|
return undefined
|
|
558
385
|
}
|
|
559
386
|
|
|
560
|
-
async search(options: SearchOptions<Schema, Table, Joins>,
|
|
561
|
-
const [ sql, params ] = this.#query(true, options,
|
|
387
|
+
async search(options: SearchOptions<Schema, Table, Joins, true>, where?: PGQuery): Promise<SearchResults<Schema, Table, Joins>> {
|
|
388
|
+
const [ sql, params ] = this.#query(true, options, where)
|
|
562
389
|
|
|
563
|
-
const result = await this.#
|
|
390
|
+
const result = await this.#provider.query<{ total: number, result: string }>(sql, params).catch((error) => {
|
|
564
391
|
throw new Error(`Error executing search query: ${error.message}`, { cause: { sql, params, error } })
|
|
565
392
|
})
|
|
566
393
|
|
|
567
394
|
if ((result.rows.length === 0) && ((options.offset || 0) > 0)) {
|
|
568
|
-
const [ sql, params ] = this.#query('only', { ...options, offset: 0, limit: undefined },
|
|
569
|
-
const result = await this.#
|
|
395
|
+
const [ sql, params ] = this.#query('only', { ...options, offset: 0, limit: undefined }, where)
|
|
396
|
+
const result = await this.#provider.query<{ total: number }>(sql, params)
|
|
570
397
|
assert(result.rows[0], 'Expected total row in count query')
|
|
571
398
|
const total = Number(result.rows[0].total)
|
|
572
399
|
return { total, rows: [] }
|