@juit/pgproxy-persister 1.4.0 → 1.4.2
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 +35 -158
- 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 +78 -252
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;AAkJ5C,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,MAI0C;AAAA;AAAA,EAExC;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,138 +1,8 @@
|
|
|
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
|
-
*/
|
|
6
|
-
export interface SearchJoin<Schema> {
|
|
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}
|
|
5
|
+
* An object to perform searches on a given table.
|
|
136
6
|
*/
|
|
137
7
|
export interface Search<Schema, Table extends string & keyof Schema, Joins extends SearchJoins<Schema>> {
|
|
138
8
|
/**
|
|
@@ -141,71 +11,78 @@ export interface Search<Schema, Table extends string & keyof Schema, Joins exten
|
|
|
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>, 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>, 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>, 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, {}>;
|
|
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, {}>;
|
|
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>;
|
|
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>;
|
|
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;AAkJ5C,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,MAI0C;AAAA;AAAA,EAExC;AAAA;AAAA,EAEA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 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juit/pgproxy-persister",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
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.2",
|
|
47
|
+
"@juit/pgproxy-model": "1.4.2",
|
|
48
|
+
"@juit/pgproxy-types": "1.4.2"
|
|
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,206 +2,23 @@ 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,
|
|
@@ -214,105 +31,114 @@ export interface Search<
|
|
|
214
31
|
* This will intrinsically limit the search to 1 result.
|
|
215
32
|
*
|
|
216
33
|
* @param query The query to filter results by
|
|
217
|
-
* @param
|
|
34
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
218
35
|
* @returns The first matching result, or `undefined` if no results matched
|
|
219
36
|
*/
|
|
220
|
-
find(query: SearchQuery<Schema, Table, Joins>,
|
|
37
|
+
find(query: SearchQuery<Schema, Table, Joins>, where?: PGQuery): Promise<SearchResult<Schema, Table, Joins> | undefined>
|
|
221
38
|
|
|
222
39
|
/**
|
|
223
40
|
* Return the raw SQL query and parameters for the specified options.
|
|
224
41
|
*
|
|
225
42
|
* @param options The search options to generate SQL for
|
|
226
|
-
* @param
|
|
43
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
227
44
|
* @returns A tuple containing the SQL string and its parameters
|
|
228
45
|
*/
|
|
229
|
-
query(options: SearchOptions<Schema, Table, Joins>,
|
|
46
|
+
query(options: SearchOptions<Schema, Table, Joins>, where?: PGQuery): [ sql: string, params: any[] ]
|
|
230
47
|
|
|
231
48
|
/**
|
|
232
49
|
* Perform a search with the specified options.
|
|
233
50
|
*
|
|
234
51
|
* @param options The search options to use
|
|
235
|
-
* @param
|
|
52
|
+
* @param where Optional extra SQL `WHERE` clauses to customize the search
|
|
236
53
|
* @returns The search results
|
|
237
54
|
*/
|
|
238
|
-
search(options: SearchOptions<Schema, Table, Joins>,
|
|
55
|
+
search(options: SearchOptions<Schema, Table, Joins>, where?: PGQuery): Promise<SearchResults<Schema, Table, Joins>>
|
|
239
56
|
}
|
|
240
57
|
|
|
58
|
+
/**
|
|
59
|
+
* A query provider for models
|
|
60
|
+
*/
|
|
61
|
+
export type SearchProvider<Schema = any> = Persister<Schema> | Connection<Schema>
|
|
62
|
+
|
|
241
63
|
/**
|
|
242
64
|
* A constructor for our {@link Search} object
|
|
243
65
|
*/
|
|
244
66
|
export interface SearchConstructor {
|
|
245
67
|
/**
|
|
246
|
-
* Construct a {@link Search} object using the specified
|
|
247
|
-
* operating on the
|
|
68
|
+
* Construct a {@link Search} object using the specified
|
|
69
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
70
|
+
* specified table.
|
|
248
71
|
*
|
|
249
|
-
* @param
|
|
72
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
250
73
|
* @param table The table to perform searches on
|
|
251
74
|
*/
|
|
252
75
|
new<
|
|
253
|
-
P extends
|
|
254
|
-
T extends string & (P extends
|
|
76
|
+
P extends SearchProvider,
|
|
77
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
255
78
|
>(
|
|
256
|
-
|
|
79
|
+
provider: P,
|
|
257
80
|
table: T,
|
|
258
|
-
): Search<P extends
|
|
81
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, {}>;
|
|
259
82
|
|
|
260
83
|
/**
|
|
261
|
-
* Construct a {@link Search} object using the specified
|
|
262
|
-
*
|
|
84
|
+
* Construct a {@link Search} object using the specified
|
|
85
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
86
|
+
* specified table, and using the specified full-text search
|
|
263
87
|
* column (TSVECTOR) to perform `q` searches.
|
|
264
88
|
*
|
|
265
|
-
* @param
|
|
89
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
266
90
|
* @param table The table to perform searches on
|
|
267
91
|
* @param fullTextSearchColumn The column to use for full-text searches
|
|
268
92
|
*/
|
|
269
93
|
new<
|
|
270
|
-
P extends
|
|
271
|
-
T extends string & (P extends
|
|
94
|
+
P extends SearchProvider,
|
|
95
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
272
96
|
>(
|
|
273
|
-
|
|
97
|
+
provider: P,
|
|
274
98
|
table: T,
|
|
275
99
|
fullTextSearchColumn: string,
|
|
276
|
-
): Search<P extends
|
|
100
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, {}>;
|
|
277
101
|
|
|
278
102
|
/**
|
|
279
|
-
* Construct a {@link Search} object using the specified
|
|
280
|
-
*
|
|
103
|
+
* Construct a {@link Search} object using the specified
|
|
104
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
105
|
+
* specified table, joining external tables.
|
|
281
106
|
*
|
|
282
|
-
* @param
|
|
107
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
283
108
|
* @param table The table to perform searches on
|
|
284
109
|
* @param joins The joins to perform
|
|
285
110
|
*/
|
|
286
111
|
new<
|
|
287
|
-
P extends
|
|
288
|
-
T extends string & (P extends
|
|
289
|
-
J extends SearchJoins<P extends
|
|
112
|
+
P extends SearchProvider,
|
|
113
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
114
|
+
J extends SearchJoins<P extends SearchProvider<infer S> ? S : never>,
|
|
290
115
|
>(
|
|
291
|
-
|
|
116
|
+
provider: P,
|
|
292
117
|
table: T,
|
|
293
118
|
joins: J,
|
|
294
|
-
): Search<P extends
|
|
119
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, J>;
|
|
295
120
|
|
|
296
121
|
/**
|
|
297
|
-
* Construct a {@link Search} object using the specified
|
|
298
|
-
*
|
|
299
|
-
* specified
|
|
122
|
+
* Construct a {@link Search} object using the specified
|
|
123
|
+
* {@link SearchProvider} (a `Persister`, `Connection`, ...) operating on the
|
|
124
|
+
* specified table, joining external tables, and using the specified full-text
|
|
125
|
+
* search column (TSVECTOR) to perform `q` searches.
|
|
300
126
|
*
|
|
301
|
-
* @param
|
|
127
|
+
* @param provider The {@link SearchProvider} instance to use
|
|
302
128
|
* @param table The table to perform searches on
|
|
303
129
|
* @param joins The joins to perform
|
|
304
130
|
* @param fullTextSearchColumn The column to use for full-text searches
|
|
305
131
|
*/
|
|
306
132
|
new<
|
|
307
|
-
P extends
|
|
308
|
-
T extends string & (P extends
|
|
309
|
-
J extends SearchJoins<P extends
|
|
133
|
+
P extends SearchProvider,
|
|
134
|
+
T extends string & (P extends SearchProvider<infer S> ? keyof S : never),
|
|
135
|
+
J extends SearchJoins<P extends SearchProvider<infer S> ? S : never>,
|
|
310
136
|
>(
|
|
311
|
-
|
|
137
|
+
provider: P,
|
|
312
138
|
table: T,
|
|
313
139
|
joins: J,
|
|
314
140
|
fullTextSearchColumn: string,
|
|
315
|
-
): Search<P extends
|
|
141
|
+
): Search<P extends SearchProvider<infer S> ? S : never, T, J>;
|
|
316
142
|
}
|
|
317
143
|
|
|
318
144
|
/* ========================================================================== *
|
|
@@ -333,8 +159,8 @@ class SearchImpl<
|
|
|
333
159
|
Table extends string & keyof Schema,
|
|
334
160
|
Joins extends SearchJoins<Schema> = {},
|
|
335
161
|
> implements Search<Schema, Table, Joins> {
|
|
336
|
-
/** Our
|
|
337
|
-
#
|
|
162
|
+
/** Our search provider instance */
|
|
163
|
+
#provider: SearchProvider<Schema>
|
|
338
164
|
/** The escaped table name */
|
|
339
165
|
#eTable: string
|
|
340
166
|
/** The escaped joins */
|
|
@@ -342,18 +168,18 @@ class SearchImpl<
|
|
|
342
168
|
/** The full-text search column (if any) */
|
|
343
169
|
#fullTextSearchColumn: string | undefined
|
|
344
170
|
|
|
345
|
-
constructor(
|
|
346
|
-
constructor(
|
|
347
|
-
constructor(
|
|
348
|
-
constructor(
|
|
171
|
+
constructor(provider: SearchProvider<Schema>, table: Table)
|
|
172
|
+
constructor(provider: SearchProvider<Schema>, table: Table, fullTextSearchColumn: string)
|
|
173
|
+
constructor(provider: SearchProvider<Schema>, table: Table, joins: Joins)
|
|
174
|
+
constructor(provider: SearchProvider<Schema>, table: Table, joins: Joins, fullTextSearchColumn: string)
|
|
349
175
|
|
|
350
176
|
constructor(
|
|
351
|
-
|
|
177
|
+
provider: SearchProvider<Schema>,
|
|
352
178
|
table: Table,
|
|
353
179
|
joinsOrFullTextSearchColumn?: Joins | string,
|
|
354
180
|
maybeFullTextSearchColumn?: string,
|
|
355
181
|
) {
|
|
356
|
-
this.#
|
|
182
|
+
this.#provider = provider
|
|
357
183
|
this.#eTable = encodeSchemaAndName(table)
|
|
358
184
|
|
|
359
185
|
let joins: Joins = {} as Joins
|
|
@@ -381,7 +207,7 @@ class SearchImpl<
|
|
|
381
207
|
#query(
|
|
382
208
|
count: boolean | 'only',
|
|
383
209
|
options: SearchOptions<Schema, Table, Joins>,
|
|
384
|
-
extra?:
|
|
210
|
+
extra?: PGQuery,
|
|
385
211
|
): [ sql: string, params: any[] ] {
|
|
386
212
|
const {
|
|
387
213
|
offset = 0,
|
|
@@ -403,8 +229,8 @@ class SearchImpl<
|
|
|
403
229
|
// Extra manual SQL *always* goes FIRST in our WHERE clause, its
|
|
404
230
|
// parameters always start at $1
|
|
405
231
|
if (extra) {
|
|
406
|
-
where.push(extra.
|
|
407
|
-
params.push(...extra.params)
|
|
232
|
+
where.push(extra.query)
|
|
233
|
+
if (extra.params) params.push(...extra.params)
|
|
408
234
|
}
|
|
409
235
|
|
|
410
236
|
let esearch = '' // falsy!
|
|
@@ -545,28 +371,28 @@ class SearchImpl<
|
|
|
545
371
|
return [ sql, params ]
|
|
546
372
|
}
|
|
547
373
|
|
|
548
|
-
query(options: SearchOptions<Schema, Table, Joins>,
|
|
549
|
-
return this.#query(false, options,
|
|
374
|
+
query(options: SearchOptions<Schema, Table, Joins>, where?: PGQuery): [ sql: string, params: any[] ] {
|
|
375
|
+
return this.#query(false, options, where)
|
|
550
376
|
}
|
|
551
377
|
|
|
552
|
-
async find(options: SearchQuery<Schema, Table, Joins>,
|
|
553
|
-
const [ sql, params ] = this.#query(false, { ...options, offset: 0, limit: 1 },
|
|
378
|
+
async find(options: SearchQuery<Schema, Table, Joins>, where?: PGQuery): Promise<SearchResult<Schema, Table, Joins> | undefined> {
|
|
379
|
+
const [ sql, params ] = this.#query(false, { ...options, offset: 0, limit: 1 }, where)
|
|
554
380
|
|
|
555
|
-
const result = await this.#
|
|
381
|
+
const result = await this.#provider.query<{ total?: number, result: string }>(sql, params)
|
|
556
382
|
if (result.rows[0]) return JSON.parse(result.rows[0].result, reviver)
|
|
557
383
|
return undefined
|
|
558
384
|
}
|
|
559
385
|
|
|
560
|
-
async search(options: SearchOptions<Schema, Table, Joins>,
|
|
561
|
-
const [ sql, params ] = this.#query(true, options,
|
|
386
|
+
async search(options: SearchOptions<Schema, Table, Joins>, where?: PGQuery): Promise<SearchResults<Schema, Table, Joins>> {
|
|
387
|
+
const [ sql, params ] = this.#query(true, options, where)
|
|
562
388
|
|
|
563
|
-
const result = await this.#
|
|
389
|
+
const result = await this.#provider.query<{ total: number, result: string }>(sql, params).catch((error) => {
|
|
564
390
|
throw new Error(`Error executing search query: ${error.message}`, { cause: { sql, params, error } })
|
|
565
391
|
})
|
|
566
392
|
|
|
567
393
|
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.#
|
|
394
|
+
const [ sql, params ] = this.#query('only', { ...options, offset: 0, limit: undefined }, where)
|
|
395
|
+
const result = await this.#provider.query<{ total: number }>(sql, params)
|
|
570
396
|
assert(result.rows[0], 'Expected total row in count query')
|
|
571
397
|
const total = Number(result.rows[0].total)
|
|
572
398
|
return { total, rows: [] }
|