uql-orm 0.82.0 → 0.83.1
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/README.md +116 -5
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +3 -3
- package/dist/cockroachdb/crdbQuerierPool.js +2 -2
- package/dist/dialect/abstractSqlDialect.d.ts +1 -2
- package/dist/dialect/abstractSqlDialect.js +15 -10
- package/dist/dialect/hydrateColumn.js +2 -12
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
- package/dist/dialect/mysqlLikeSqlDialect.js +5 -0
- package/dist/dialect/operators.d.ts +16 -0
- package/dist/dialect/operators.js +47 -4
- package/dist/dialect/pgLikeSqlDialect.d.ts +1 -0
- package/dist/dialect/pgLikeSqlDialect.js +13 -4
- package/dist/http/handler.js +17 -23
- package/dist/http/query.d.ts +3 -3
- package/dist/http/query.js +6 -3
- package/dist/maria/mariadbQuerierPool.js +4 -2
- package/dist/migrate/builder/expressions.d.ts +2 -0
- package/dist/migrate/builder/expressions.js +18 -9
- package/dist/migrate/builder/tableBuilder.js +1 -1
- package/dist/migrate/generator/mongoSchemaGenerator.d.ts +1 -1
- package/dist/migrate/generator/mongoSchemaGenerator.js +1 -1
- package/dist/migrate/introspection/mongoIntrospector.d.ts +1 -1
- package/dist/migrate/introspection/mongoIntrospector.js +1 -1
- package/dist/migrate/introspection/mssqlIntrospector.js +13 -1
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +2 -0
- package/dist/migrate/introspection/mysqlIntrospector.js +6 -2
- package/dist/migrate/introspection/postgresIntrospector.js +1 -1
- package/dist/migrate/storage/databaseStorage.js +1 -1
- package/dist/mongo/mongoDialect.js +12 -24
- package/dist/mongo/mongodbQuerier.js +4 -3
- package/dist/mssql/mssqlQuerier.d.ts +2 -0
- package/dist/mssql/mssqlQuerier.js +8 -5
- package/dist/mysql/mysql2QuerierPool.d.ts +1 -0
- package/dist/mysql/mysql2QuerierPool.js +20 -2
- package/dist/neon/neonQuerierPool.js +2 -2
- package/dist/pglite/pgliteQuerierPool.js +10 -4
- package/dist/postgres/pgQuerierPool.js +2 -2
- package/dist/postgres/{pgNumericTypes.d.ts → pgWireTypes.d.ts} +4 -3
- package/dist/postgres/{pgNumericTypes.js → pgWireTypes.js} +7 -3
- package/dist/schema/canonicalType.d.ts +3 -0
- package/dist/schema/canonicalType.js +31 -9
- package/dist/schema/schemaASTDiffer.js +4 -2
- package/dist/sqlite/sqliteDialect.d.ts +1 -3
- package/dist/sqlite/sqliteDialect.js +3 -6
- package/dist/type/entity.d.ts +1 -1
- package/dist/type/queryWhere.d.ts +10 -8
- package/dist/util/date.d.ts +11 -0
- package/dist/util/date.js +19 -0
- package/dist/util/dialect.util.d.ts +2 -5
- package/dist/util/dialect.util.js +3 -6
- package/dist/util/fieldOption.util.d.ts +5 -3
- package/dist/util/fieldOption.util.js +6 -5
- package/dist/util/sqlLiteral.d.ts +8 -1
- package/dist/util/sqlLiteral.js +11 -7
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
<h3>The JSON-native TypeScript ORM</h3>
|
|
11
11
|
|
|
12
|
-
<p align="left">UQL
|
|
12
|
+
<p align="left">UQL queries SQL databases and MongoDB with plain, type-safe JSON-syntax.
|
|
13
13
|
</p>
|
|
14
14
|
|
|
15
15
|
<p>
|
|
@@ -44,16 +44,127 @@ That is the whole install ([setup](https://uql-orm.dev/getting-started)). No com
|
|
|
44
44
|
|
|
45
45
|
The compiler catches each of those, with no codegen: the entity classes are the schema. Try the editor [on the home page](https://uql-orm.dev).
|
|
46
46
|
|
|
47
|
+
## How it fits together
|
|
48
|
+
|
|
49
|
+
### 1. The entities are the schema
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// entities.ts
|
|
53
|
+
import { Entity, Field, Id, ManyToOne, OneToMany } from 'uql-orm';
|
|
54
|
+
|
|
55
|
+
@Entity()
|
|
56
|
+
export class User {
|
|
57
|
+
@Id({ type: Number })
|
|
58
|
+
id?: number;
|
|
59
|
+
|
|
60
|
+
@Field({ type: String, unique: true })
|
|
61
|
+
email?: string | null;
|
|
62
|
+
|
|
63
|
+
@OneToMany({ entity: () => Post, mappedBy: (post) => post.author })
|
|
64
|
+
posts?: Post[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@Entity()
|
|
68
|
+
export class Post {
|
|
69
|
+
@Id({ type: Number })
|
|
70
|
+
id?: number;
|
|
71
|
+
|
|
72
|
+
@Field({ type: String })
|
|
73
|
+
title?: string | null;
|
|
74
|
+
|
|
75
|
+
@Field({ type: Number })
|
|
76
|
+
likes?: number | null;
|
|
77
|
+
|
|
78
|
+
@Field({ references: () => User })
|
|
79
|
+
authorId?: number | null;
|
|
80
|
+
|
|
81
|
+
@ManyToOne({ entity: () => User, references: (post) => post.authorId })
|
|
82
|
+
author?: User;
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 2. A pool, and the migrations it drives
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
// uql.config.ts
|
|
90
|
+
import type { Config } from 'uql-orm';
|
|
91
|
+
import { PgQuerierPool } from 'uql-orm/postgres';
|
|
92
|
+
import { Post, User } from './entities.js';
|
|
93
|
+
|
|
94
|
+
export const pool = new PgQuerierPool({ connectionString: process.env.DATABASE_URL });
|
|
95
|
+
|
|
96
|
+
export default { pool, entities: [User, Post] } satisfies Config;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
npx uql-migrate generate:entities initial # diffs the entities against the database into a migration you review
|
|
101
|
+
npx uql-migrate up # applies it
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 3. Query on the server
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { Post } from './entities.js';
|
|
108
|
+
import { pool } from './uql.config.js';
|
|
109
|
+
|
|
110
|
+
const posts = await pool.findMany(Post, {
|
|
111
|
+
$select: { title: true },
|
|
112
|
+
$populate: { author: { $select: { email: true } } },
|
|
113
|
+
$where: { likes: { $gte: 10 } },
|
|
114
|
+
$sort: { likes: 'desc' },
|
|
115
|
+
$limit: 10,
|
|
116
|
+
});
|
|
117
|
+
// SELECT "Post"."title", "author"."id" "author.id", "author"."email" "author.email"
|
|
118
|
+
// FROM "Post" LEFT JOIN "User" "author" ON "author"."id" = "Post"."authorId"
|
|
119
|
+
// WHERE "Post"."likes" >= $1 ORDER BY "Post"."likes" DESC LIMIT 10
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The result is typed to what the query selected: `posts[0].author?.email` compiles, `posts[0].likes` does not.
|
|
123
|
+
|
|
124
|
+
### 4. Serve it, and send the same query from the browser
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
// server.ts: Bun, Deno, Cloudflare Workers, or any framework that takes a fetch handler
|
|
128
|
+
import { createFetchHandler } from 'uql-orm/http';
|
|
129
|
+
import { Post, User } from './entities.js';
|
|
130
|
+
import { pool } from './uql.config.js';
|
|
131
|
+
|
|
132
|
+
export default { fetch: createFetchHandler({ pool, include: [User, Post] }) };
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
// browser.ts
|
|
137
|
+
import { HttpQuerier } from 'uql-orm/browser';
|
|
138
|
+
import { Post } from './entities.js';
|
|
139
|
+
|
|
140
|
+
const api = new HttpQuerier('https://api.example.com');
|
|
141
|
+
|
|
142
|
+
const { data: posts } = await api.findMany(Post, {
|
|
143
|
+
$select: { title: true },
|
|
144
|
+
$populate: { author: { $select: { email: true } } },
|
|
145
|
+
$where: { likes: { $gte: 10 } },
|
|
146
|
+
$sort: { likes: 'desc' },
|
|
147
|
+
$limit: 10,
|
|
148
|
+
});
|
|
149
|
+
// GET /post?$select={"title":true}&$populate={"author":{"$select":{"email":true}}}&$where={"likes":{"$gte":10}}&$sort={"likes":"desc"}&$limit=10
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
The query object is the same on both sides, and so is its type check. Authorization goes in the handler's [hooks](https://uql-orm.dev/http#authorization-hooks), and tenant isolation in [security filters](https://uql-orm.dev/multi-tenancy) that apply to every query, including the server's own.
|
|
153
|
+
|
|
154
|
+
### When CRUD is not enough
|
|
155
|
+
|
|
156
|
+
- [`raw()`](https://uql-orm.dev/querying/raw-sql) fits anywhere a value or a field goes, and a migration can be plain SQL.
|
|
157
|
+
- [Computed fields](https://uql-orm.dev/entities/computed-fields) are SQL expressions that you can filter and sort on. [Triggers](https://uql-orm.dev/entities/triggers) run inside the database.
|
|
158
|
+
- [Transactions](https://uql-orm.dev/querying/transactions) hold one connection across many operations, and [lifecycle hooks](https://uql-orm.dev/entities/lifecycle-hooks) run your code around each write.
|
|
159
|
+
- Anything the CRUD routes do not cover goes in a route you write, beside the handler and under the same prefix.
|
|
160
|
+
|
|
47
161
|
## Why UQL?
|
|
48
162
|
|
|
49
|
-
- **Queries are JSON, not method chains.** Build one dynamically, store it, or send it from the browser; the same object runs on every database. No DSL to learn.
|
|
50
163
|
- **One API, everywhere it runs.** PostgreSQL, PGlite, CockroachDB, MySQL, MariaDB, MSSQL, SQLite, Turso, libSQL, Neon, Cloudflare D1, Bun's native SQL, and even MongoDB. The same code on Node 24+, Bun, Deno, [Cloudflare Workers](https://uql-orm.dev/cloudflare-d1), [AWS Lambda and Vercel](https://uql-orm.dev/serverless), and [the browser](https://uql-orm.dev/browser), with no native binaries on the `fetch`-based drivers.
|
|
51
164
|
- **Type-safe to the leaf, nothing to generate.** Every key is checked against your entity, down into populated relations and [JSON/JSONB](https://uql-orm.dev/querying/json) dot-paths, so `$like` on a numeric column is a compile error. No `.prisma` file, no generated client.
|
|
52
165
|
- **Relations without N+1.** [`$populate`](https://uql-orm.dev/querying/relations) reads a to-many inside the parent's statement, so a read is one round trip. Nothing is lazy, so nothing fires behind your back in a serializer.
|
|
53
|
-
- **Migrations you read before they run.** Edit an entity, run `uql-migrate generate:entities`, review the SQL in the PR like any other file. [`drift:check`](https://uql-orm.dev/migrations) catches a database that no longer matches.
|
|
54
|
-
- **Raw SQL when you want it.** [`raw()`](https://uql-orm.dev/querying/raw-sql) fits anywhere in a query, [computed fields](https://uql-orm.dev/entities/computed-fields) are expressions you can filter on, and a migration can be plain SQL.
|
|
55
166
|
- **Light.** Zero runtime dependencies and every dialect in one package, yet `uql-orm/postgres` is about 27 kB gzipped. See [what we deleted to get there](https://uql-orm.dev/blog/zero-dependencies).
|
|
56
|
-
- **The hard things are built in.** [Semantic and vector search](https://uql-orm.dev/ai-semantic-search), [multi-tenant filters you cannot bypass by accident](https://uql-orm.dev/multi-tenancy), [soft-delete with restore](https://uql-orm.dev/entities/soft-delete), [streaming](https://uql-orm.dev/querying/streaming), and [
|
|
167
|
+
- **The hard things are built in.** [Semantic and vector search](https://uql-orm.dev/ai-semantic-search), [multi-tenant filters you cannot bypass by accident](https://uql-orm.dev/multi-tenancy), [soft-delete with restore](https://uql-orm.dev/entities/soft-delete), [streaming](https://uql-orm.dev/querying/streaming), and [drift checks](https://uql-orm.dev/migrations) that catch a database that no longer matches.
|
|
57
168
|
- **The fastest ORM.** On a full PostgreSQL round trip it adds the least over hand-written driver code of any ORM in our open-source [benchmark](https://github.com/rogerpadilla/ts-orm-benchmark), on Bun, Node and Deno alike. The same benchmark [scores the types](https://github.com/rogerpadilla/ts-orm-benchmark#type-safety) by compiling ordinary mistakes in each ORM's API: UQL is the only one that catches them all.
|
|
58
169
|
|
|
59
170
|
## Get started
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var c=[];function d(e){for(let r of c)r(e)}function A(e){c.push(e);let r=c.length-1;return()=>{c.splice(r,1)}}var O=["$select","$populate","$exclude","$where","$sort"],f=["$count"],
|
|
1
|
+
var c=[];function d(e){for(let r of c)r(e)}function A(e){c.push(e);let r=c.length-1;return()=>{c.splice(r,1)}}var O=["$select","$populate","$exclude","$where","$sort"],f=["$count"],b=["$skip","$limit"],R=["$candidates"],T=["$distinct"],v=["$lock",...f,...R];var C=Symbol("rawValue"),W=Symbol("rawAlias"),B=Symbol("rawText");class i extends TypeError{name="UqlUsageError";kind="usage";status=400}function l(e){return e?Object.keys(e):[]}function k(e){if(typeof e!=="object"||e===null)return!0;if(Array.isArray(e))return!1;let r=Object.getPrototypeOf(e);return r!==Object.prototype&&r!==null}var Z=new Set([...O,...f,...b,...R,...T,"hardDelete","count"]);function u(e){if(!e)return"";let r=new URLSearchParams;for(let n of l(e)){let o=e[n];if(o===void 0)continue;r.append(n,typeof o==="object"&&o!==null?a(o):String(o))}let t=r.toString();return t?`?${t}`:""}function a(e){return JSON.stringify(e,(r,t)=>{if(typeof t!=="object"||t===null)return t;if(C in t)throw new i("raw SQL cannot travel over HTTP: what leaves the browser is JSON");if(t instanceof ArrayBuffer||ArrayBuffer.isView(t))throw new i("binary cannot travel over HTTP: what leaves the browser is JSON");return t})}class K extends Error{status;constructor(e,r){super(e);this.status=r;this.name="RequestError"}}function h(e,r){return y(e,{method:"get"},r)}function x(e,r,t){return y(e,{method:"post",body:a(r)},t)}function Q(e,r,t){return y(e,{method:"patch",body:a(r)},t)}function g(e,r,t){return y(e,{method:"put",body:a(r)},t)}function m(e,r){return y(e,{method:"delete"},r)}function U(e,r,t){return y(e,{method:"QUERY",body:a(r)},t)}function y(e,r,t){if(d({phase:"start",opts:t}),r.headers={accept:"application/json","content-type":"application/json",...t?.headers},t?.signal)r.signal=t.signal;return fetch(e,r).then((n)=>n.json().then((o)=>{if(n.status>=200&&n.status<300)return d({phase:"success",opts:t}),o;let P=o,E={message:P?.error?.message??n.statusText,code:P?.error?.code??n.status};throw d({phase:"error",error:E,opts:t}),new K(E.message,E.code)})).finally(()=>{d({phase:"complete",opts:t})})}function q(e){let r=e.charAt(0).toLowerCase();for(let t=1;t<e.length;++t)r+=e[t]===e[t].toUpperCase()?"-"+e[t].toLowerCase():e[t];return r}var s={findMany:{method:"GET",path:""},findOne:{method:"GET",path:"/one"},count:{method:"GET",path:"/count"},findOneById:{method:"GET",path:"/:id"},insertOne:{method:"POST",path:""},insertMany:{method:"POST",path:"/many"},saveOne:{method:"PUT",path:""},saveMany:{method:"PUT",path:"/many"},updateMany:{method:"PATCH",path:""},updateOneById:{method:"PATCH",path:"/:id"},deleteOneById:{method:"DELETE",path:"/:id"},deleteMany:{method:"DELETE",path:""}},F=l(s),ue=new Map(F.filter((e)=>s[e].method==="GET"&&s[e].path!=="/:id").map((e)=>[s[e].path,e]));function j(e){return q(e.name)}function w(e,r){if(!k(r))throw new i(`'${e.name}' was addressed by an id object, which the HTTP route cannot carry.`);return String(r)}class S{basePath;defaults;constructor(e,r={}){this.basePath=e;this.defaults=r}async findOneById(e,r,t,n){let o=this.getBasePath(e),p=u(t);return h(`${o}/${w(e,r)}${p}`,this.buildOptions(n))}findOne(e,r,t){return this.read(`${this.getBasePath(e)}${s.findOne.path}`,r,t)}findMany(e,r,t){let n={...r};if(t?.count)n.count=!0;return this.read(this.getBasePath(e),n,t)}async findManyAndCount(e,r,t){let n=await this.findMany(e,r,{...t,count:!0});if(typeof n.count!=="number")throw TypeError("findManyAndCount response has an invalid count");return{...n,count:n.count}}count(e,r,t){return this.read(`${this.getBasePath(e)}${s.count.path}`,r,t)}async exists(e,r,t){let n=await this.count(e,{...r,$limit:1},t);return{...n,data:n.data>0}}insertOne(e,r,t){let n=this.getBasePath(e);return x(n,r,this.buildOptions(t))}insertMany(e,r,t){let n=this.getBasePath(e);return x(`${n}${s.insertMany.path}`,r,this.buildOptions(t))}async updateOneById(e,r,t,n){let o=this.getBasePath(e);return Q(`${o}/${w(e,r)}`,t,this.buildOptions(n))}updateMany(e,r,t,n){let o=this.getBasePath(e),p=u(r);return Q(`${o}${p}`,t,this.buildOptions(n))}saveOne(e,r,t){let n=this.getBasePath(e);return g(n,r,this.buildOptions(t))}saveMany(e,r,t){let n=this.getBasePath(e);return g(`${n}${s.saveMany.path}`,r,this.buildOptions(t))}async deleteOneById(e,r,t={}){let n=this.getBasePath(e),o=t.hardDelete?u({hardDelete:t.hardDelete}):"";return m(`${n}/${w(e,r)}${o}`,this.buildOptions(t))}deleteMany(e,r,t={}){let n=this.getBasePath(e),o=u(t.hardDelete?{...r,hardDelete:t.hardDelete}:r);return m(`${n}${o}`,this.buildOptions(t))}getBasePath(e){return`${this.basePath}/${(this.defaults.entityPath??j)(e)}`}read(e,r,t){if(this.defaults.readMethod==="QUERY")return U(e,r??{},this.buildOptions(t));return h(`${e}${u(r)}`,this.buildOptions(t))}buildOptions(e){if(!this.defaults.headers&&!e?.headers)return e;return{...e,headers:{...this.defaults.headers,...e?.headers}}}}var V={getQuerier:()=>new S("/api")};function Re(e){V=e}function _(){return V}function he(){return _().getQuerier()}export{S as HttpQuerier,K as RequestError,h as get,he as getQuerier,_ as getQuerierPool,d as notify,A as on,Q as patch,x as post,g as put,U as query,m as remove,Re as setQuerierPool};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=54F53C9583121D9864756E2164756E21
|
|
4
4
|
//# sourceMappingURL=uql-browser.min.js.map
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
"import type { QueryContext, RelationAggregateSpec, SqlQueryDialect } from './dialect.js';\nimport type { Type } from './utility.js';\n\n/** What a `raw` callback receives. See {@link QueryRawFn}. */\nexport type QueryRawRenderOptions = {\n /** The dialect rendering the SQL. */\n dialect: SqlQueryDialect;\n /** The alias of the table in scope, unescaped; empty where there is none. */\n prefix: string;\n /** {@link prefix} escaped, with its trailing dot. */\n escapedPrefix: string;\n /** The query context the SQL is written into. */\n ctx: QueryContext;\n /**\n * The entity being rendered, which a ref read off a definition's map resolves its column against: a\n * computed field's own, or the one whose schema is built. Absent where a statement renders SQL.\n */\n entity?: Type<unknown>;\n /**\n * The `FROM` a set-based trigger's body reads its rows through, `FROM inserted` and the like, which a\n * write in it names. Absent where the body reads `NEW` and `OLD` bare, and outside a trigger.\n */\n rows?: string;\n};\n\n/** {@link QueryRawRenderOptions} as the callers along the way fill them in, every one still optional. */\nexport type QueryRawFnOptions = Partial<QueryRawRenderOptions>;\n\n/**\n * A `raw` callback: write into `ctx`, or return a string or number to have it appended. Anything else\n * it returns is ignored, which is why the return type is `unknown` rather than `void | Scalar` - the\n * latter rejected `({ ctx }) => ctx.append(...)`, the form every computed field is written in, because\n * TypeScript's \"returning a value where void is expected\" allowance does not apply to a union.\n */\nexport type QueryRawFn = (opts: QueryRawRenderOptions) => unknown;\n\nexport const RAW_VALUE: unique symbol = Symbol('rawValue');\nexport const RAW_ALIAS: unique symbol = Symbol('rawAlias');\nexport const RAW_TEXT: unique symbol = Symbol('rawText');\n\nexport class QueryRaw {\n readonly [RAW_VALUE]: QueryRawFn;\n readonly [RAW_ALIAS]?: string;\n /**\n * The SQL verbatim, set only where it is a constant: a template that interpolates nothing binds no\n * value and reads no column, so it needs no dialect to render. What a DDL clause with nowhere to\n * bind reads - see {@link constantSql}.\n */\n readonly [RAW_TEXT]?: string;\n\n constructor(value: QueryRawFn, alias?: string, text?: string) {\n this[RAW_VALUE] = value;\n this[RAW_ALIAS] = alias;\n this[RAW_TEXT] = text;\n }\n\n /** The same expression under an alias, for a `$select` projection. */\n as(alias: string): QueryRaw {\n return new QueryRaw(this[RAW_VALUE], alias, this[RAW_TEXT]);\n }\n\n /** Writes the expression into `opts.ctx`. The alias is the projection's to write, after the term. */\n render(opts: QueryRawRenderOptions): void {\n const emitted = this[RAW_VALUE](opts);\n if (typeof emitted === 'string' || (typeof emitted === 'number' && !Number.isNaN(emitted))) {\n opts.ctx.append(String(emitted));\n }\n }\n}\n\n/**\n * A field of an entity as SQL, read off `refs(Entity)` or a definition's refs: interpolated into `raw`, it\n * renders as the field's column. Its `key` is how an index tells a column from an expression, and `V`,\n * the field's type, is what a value slot checks it against: see {@link RawFor}.\n */\nexport class ColumnRef<K extends string = string, V = unknown> extends QueryRaw {\n declare readonly __value?: V;\n\n constructor(\n readonly key: K,\n value: QueryRawFn,\n ) {\n super(value);\n }\n}\n\n/**\n * SQL where a value of type `V` goes: bare SQL, whose type is its author's to know, or a ref to a column\n * holding one, nullability aside. `Raw` is what the transport carries, so the wire's `never` stays one.\n */\nexport type RawFor<Raw, V> = Raw & { readonly __value?: V | null };\n\n/**\n * A relation aggregate as SQL, read off a `computed` field's refs: `(user) => user.resources.count()`.\n * It renders as the correlated subquery a `$count` reads, so a field holding one is read, filtered and\n * sorted like any other.\n *\n * `V` is the value it reads and `Storable` whether a trigger could keep it, both carried in phantom\n * fields so the aggregate a field declares decides the property's type and refuses `stored: true` on\n * one no delta can maintain.\n */\nexport class RelationAggregate<V = unknown, Storable extends boolean = boolean> extends QueryRaw {\n declare readonly __value?: V;\n declare private readonly __storable: Storable;\n\n constructor(\n /** What it reads, kept beside the SQL so a read decodes the value the way the target's field does. */\n readonly spec: RelationAggregateSpec,\n value: QueryRawFn,\n ) {\n super(value);\n }\n}\n",
|
|
8
8
|
"/**\n * What a failed query ran into, named the same on every engine - what {@link queryErrorKind} answers\n * with, whether a driver raised the error or UQL did. `retryable` is a deadlock, a serialization\n * failure, a lock timeout or a busy database: the transaction can simply run again. `usage` is the\n * caller's own mistake, which running it again will not fix.\n */\nexport type QueryErrorKind =\n | 'uniqueViolation'\n | 'foreignKeyViolation'\n | 'notNullViolation'\n | 'checkViolation'\n | 'optimisticLock'\n | 'retryable'\n | 'usage';\n\n/**\n * Thrown where the caller used the API in a way no statement can carry out: an update payload with no\n * version, a `$lock` outside a transaction, a method with no version to match. A `TypeError` still,\n * since the call itself is wrong, but one carrying the `status` an HTTP transport answers with - the\n * request is malformed, not the server's failure, and an untyped client is exactly who reaches this.\n */\nexport class UqlUsageError extends TypeError {\n override name = 'UqlUsageError';\n /** What `queryErrorKind` answers, so a caller branches on it rather than on the class. */\n readonly kind = 'usage';\n /** What an HTTP transport answers with. */\n readonly status = 400;\n}\n\n/** What a value is, for a refusal naming what `/http` handed over instead of what the types require. */\nexport function kindOf(value: unknown): string {\n return value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value;\n}\n\n/**\n * @deprecated since 0.77.1 - use {@link UqlUsageError}, which every misuse throws, lock or not. The\n * same class under both names, so an existing `instanceof` keeps working.\n */\nexport const UqlLockUsageError = UqlUsageError;\nexport type UqlLockUsageError = UqlUsageError;\n\n/**\n * Thrown when an update's `@Field({ version })` no longer matches the row: another writer moved it on,\n * or it is gone. `expected` is what the payload carried, `actual` what the row holds now, `undefined`\n * where there is no row left.\n */\nexport class UqlOptimisticLockError extends Error {\n override name = 'UqlOptimisticLockError';\n readonly kind = 'optimisticLock';\n readonly status = 409;\n\n constructor(\n message: string,\n readonly expected: unknown,\n readonly actual: unknown,\n ) {\n super(message);\n }\n}\n",
|
|
9
9
|
"import type { EntityMeta } from '../type/index.js';\nimport { UqlUsageError } from './uqlError.js';\n\nexport function throwPendingTransaction(): never {\n throw new UqlUsageError('pending transaction');\n}\n\nexport function throwNoPendingTransaction(): never {\n throw new UqlUsageError('not a pending transaction');\n}\n\nexport function clone<T>(value: T): T {\n if (typeof value !== 'object' || value === null) {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map((it) => clone(it)) as T;\n }\n return { ...value };\n}\n\n/** Whether `obj` has at least one enumerable key. Narrows away `undefined`/`null` for callers. */\nexport function hasKeys<T>(obj: T): obj is NonNullable<T> {\n if (typeof obj !== 'object' || obj === null) return false;\n for (const _ in obj) return true;\n return false;\n}\n\n/**\n * Whether any enumerable key of `obj` satisfies `pred`, short-circuiting on the first match\n * without materializing a key array (unlike `Object.keys(obj).some(pred)`).\n */\nexport function someKey<T extends object>(obj: T, pred: (key: keyof T & string) => boolean): boolean {\n for (const key in obj) {\n if (pred(key)) return true;\n }\n return false;\n}\n\n/** Whether `key` names an operator (`$eq`, `$push`...) rather than a field. */\nexport function isOperatorKey(key: string): boolean {\n return key.startsWith('$');\n}\n\n/** Whether `value` is a non-empty object with an operator key (`$eq`, `$push`...): the one test every dialect classifies with. */\nexport function isOperatorObject(value: unknown): value is Record<string, unknown> {\n return isRecord(value) && someKey(value, isOperatorKey);\n}\n\n/** Whether `value` is an object that is not an array, whose keys can be read. */\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value);\n}\n\nexport function getKeys<T extends object>(obj: T | null | undefined): (keyof T & string)[] {\n return obj ? (Object.keys(obj) as (keyof T & string)[]) : [];\n}\n\n/** The entries of `record` holding a value: a key declared but left `undefined` is no entry at all. */\nexport function definedEntries<K extends string, V>(record: Partial<Record<K, V>>): [K, V][] {\n return (Object.entries(record) as [K, V | undefined][]).filter((entry): entry is [K, V] => entry[1] !== undefined);\n}\n\n/**\n * The entity's own name, declared or its class's. `meta.name` holds only what the author wrote, so\n * the fallback is what an entity that named no table is called - which is why the sites spelling this\n * out reached for three different fallbacks, `?? ''` among them, and named nothing at all.\n */\nexport function entityName<E>(meta: EntityMeta<E>): string {\n return meta.name ?? meta.entity.name;\n}\n\n/**\n * Whether `value` addresses a row by itself rather than naming columns: every primitive, and the\n * object ids a driver deals in (`ObjectId`, `Date`, bytes). Only a plain object names columns, which\n * is what a `$where` map and a composite key's id object both are; an array is a list of either.\n */\nexport function isScalarId(value: unknown): boolean {\n if (typeof value !== 'object' || value === null) {\n return true;\n }\n if (Array.isArray(value)) {\n return false;\n }\n // `null` as well as `Object.prototype`: an object with no prototype is what a query-string parser\n // hands back (`qs`, express's `req.params`), and reading one as a bare id would name one column\n // with a map of several.\n const proto = Object.getPrototypeOf(value);\n return proto !== Object.prototype && proto !== null;\n}\n\n/** Whether `value` is a plain object naming columns, the one shape a `$where` takes. */\nexport function isWhereMap(value: unknown): value is Record<string, unknown> {\n return !Array.isArray(value) && !isScalarId(value);\n}\n",
|
|
10
|
-
"import type { QueryOptions, WireQuery } from '../type/index.js';\n// the clause lists themselves, not the barrel: this module is in the browser bundle's graph\nimport {\n QUERY_BOOLEAN_CLAUSES,\n QUERY_NUMBER_CLAUSES,\n QUERY_OBJECT_CLAUSES,\n QUERY_ROOT_NUMBER_CLAUSES,\n QUERY_ROOT_OBJECT_CLAUSES,\n} from '../type/query.js';\n// the brand alone, not the class: importing `QueryRaw` for an `instanceof` kept it, and `ColumnRef`\n// with it, in the browser bundle, which is on a size budget\nimport { RAW_VALUE } from '../type/queryRaw.js';\n// the specific util module, not the barrel, so the browser bundle does not pull in entity metadata\nimport { getKeys, isWhereMap } from '../util/object.util.js';\n// the error class alone, from its own leaf module: `queryError.ts` carries every driver's code map\nimport { UqlUsageError } from '../util/uqlError.js';\n\n/**\n * Keys accepted from the wire - query structure ({@link Query}) plus the `hardDelete`/`count` scalar\n * flags. Anything else (e.g. `filters`, `context`, `$entity`) is dropped so a remote client can't\n * bypass a security filter or inject ambient context - those are server-only. The `satisfies` ties\n * every entry to a real query/option key, so a typo or a renamed option fails to compile.\n */\nconst ALLOWED_QUERY_KEYS = new Set<string>([\n ...QUERY_OBJECT_CLAUSES,\n ...QUERY_ROOT_OBJECT_CLAUSES,\n ...QUERY_NUMBER_CLAUSES,\n ...QUERY_ROOT_NUMBER_CLAUSES,\n ...QUERY_BOOLEAN_CLAUSES,\n 'hardDelete',\n 'count',\n] satisfies (keyof WireQuery<unknown> | keyof Pick<QueryOptions, 'hardDelete'> | 'count')[]);\n\n/**\n * Keys that mean something locally but that this transport can never honor, so they are rejected\n * rather than dropped like the rest. Each request runs on its own auto-committing connection, so a\n * row lock taken here is released before the response is written: honoring `$lock` is impossible,\n * and ignoring it would hand the caller a read they believe is serialized and is not.\n */\nconst REJECTED_QUERY_KEYS = new Set<string>(['$lock'] satisfies (keyof WireQuery<unknown>)[]);\n\n/**\n * Parse raw query-string entries (with JSON-stringified values) into a UQL query
|
|
10
|
+
"import type { QueryOptions, WireQuery } from '../type/index.js';\n// the clause lists themselves, not the barrel: this module is in the browser bundle's graph\nimport {\n QUERY_BOOLEAN_CLAUSES,\n QUERY_NUMBER_CLAUSES,\n QUERY_OBJECT_CLAUSES,\n QUERY_ROOT_NUMBER_CLAUSES,\n QUERY_ROOT_OBJECT_CLAUSES,\n} from '../type/query.js';\n// the brand alone, not the class: importing `QueryRaw` for an `instanceof` kept it, and `ColumnRef`\n// with it, in the browser bundle, which is on a size budget\nimport { RAW_VALUE } from '../type/queryRaw.js';\n// the specific util module, not the barrel, so the browser bundle does not pull in entity metadata\nimport { getKeys, isRecord, isWhereMap } from '../util/object.util.js';\n// the error class alone, from its own leaf module: `queryError.ts` carries every driver's code map\nimport { UqlUsageError } from '../util/uqlError.js';\n\n/**\n * Keys accepted from the wire - query structure ({@link Query}) plus the `hardDelete`/`count` scalar\n * flags. Anything else (e.g. `filters`, `context`, `$entity`) is dropped so a remote client can't\n * bypass a security filter or inject ambient context - those are server-only. The `satisfies` ties\n * every entry to a real query/option key, so a typo or a renamed option fails to compile.\n */\nconst ALLOWED_QUERY_KEYS = new Set<string>([\n ...QUERY_OBJECT_CLAUSES,\n ...QUERY_ROOT_OBJECT_CLAUSES,\n ...QUERY_NUMBER_CLAUSES,\n ...QUERY_ROOT_NUMBER_CLAUSES,\n ...QUERY_BOOLEAN_CLAUSES,\n 'hardDelete',\n 'count',\n] satisfies (keyof WireQuery<unknown> | keyof Pick<QueryOptions, 'hardDelete'> | 'count')[]);\n\n/**\n * Keys that mean something locally but that this transport can never honor, so they are rejected\n * rather than dropped like the rest. Each request runs on its own auto-committing connection, so a\n * row lock taken here is released before the response is written: honoring `$lock` is impossible,\n * and ignoring it would hand the caller a read they believe is serialized and is not.\n */\nconst REJECTED_QUERY_KEYS = new Set<string>(['$lock'] satisfies (keyof WireQuery<unknown>)[]);\n\n/**\n * Parse raw query-string entries (with JSON-stringified values), or a `QUERY` body, into a UQL query\n * object. Symmetric counterpart of {@link stringifyQuery}. Only {@link ALLOWED_QUERY_KEYS} are honored.\n */\nexport function parseQueryParams<E = unknown>(params: unknown = {}): WireQuery<E> {\n if (!isRecord(params)) {\n throw new UqlUsageError('the query must be a JSON object');\n }\n const query: Record<string, unknown> = {};\n for (const key of getKeys(params)) {\n if (REJECTED_QUERY_KEYS.has(key)) {\n throw new UqlUsageError(`'${key}' is not supported over HTTP`);\n }\n if (ALLOWED_QUERY_KEYS.has(key)) {\n query[key] = params[key];\n }\n }\n\n for (const key of [...QUERY_OBJECT_CLAUSES, ...QUERY_ROOT_OBJECT_CLAUSES]) {\n const value = query[key];\n if (typeof value === 'string') {\n try {\n query[key] = JSON.parse(value);\n } catch {\n throw Object.assign(new SyntaxError(`invalid JSON in '${key}'`), { status: 400 });\n }\n }\n }\n\n query['$where'] ??= {};\n if (!isWhereMap(query['$where'])) {\n throw new UqlUsageError(\"'$where' must be a JSON object\");\n }\n\n // A query string carries every value as text, so what decodes a clause is the shape its group\n // declares. `'false'` is the reason the boolean pass exists rather than the raw value being taken:\n // it is a non-empty string, so a `$distinct=false` would otherwise read as asking for one.\n for (const key of [...QUERY_NUMBER_CLAUSES, ...QUERY_ROOT_NUMBER_CLAUSES]) {\n if (query[key] !== undefined) {\n query[key] = Number(query[key]);\n }\n }\n for (const key of QUERY_BOOLEAN_CLAUSES) {\n if (query[key] !== undefined) {\n query[key] = query[key] === true || query[key] === 'true';\n }\n }\n\n return query;\n}\n\n/**\n * Serialize a UQL query object into a percent-encoded query string where object values\n * are JSON-stringified. Symmetric counterpart of {@link parseQueryParams}.\n */\nexport function stringifyQuery(query?: Record<string, unknown>): string {\n if (!query) {\n return '';\n }\n const params = new URLSearchParams();\n for (const key of getKeys(query)) {\n const value = query[key];\n if (value === undefined) {\n continue;\n }\n params.append(key, typeof value === 'object' && value !== null ? wireJson(value) : String(value));\n }\n const qs = params.toString();\n return qs ? `?${qs}` : '';\n}\n\n/**\n * What leaves the browser, as JSON, refusing what JSON keeps nothing of rather than letting the server\n * build a statement around the remains. A `raw` fragment renders SQL against a dialect the client does not\n * have and arrives as `{}`; binary arrives as an object keyed by index. A `Date` is not among them - it\n * serializes to ISO 8601, which is what a date column reads. This is what a cast, or a JavaScript caller,\n * hits where the client's types already refuse a fragment.\n */\nexport function wireJson(value: unknown): string {\n return JSON.stringify(value, (_key: string, held: unknown) => {\n if (typeof held !== 'object' || held === null) {\n return held;\n }\n if (RAW_VALUE in held) {\n throw new UqlUsageError('raw SQL cannot travel over HTTP: what leaves the browser is JSON');\n }\n // A blob is a field value, so no type parameter reaches it: this is the only place it is caught.\n if (held instanceof ArrayBuffer || ArrayBuffer.isView(held)) {\n throw new UqlUsageError('binary cannot travel over HTTP: what leaves the browser is JSON');\n }\n return held;\n });\n}\n",
|
|
11
11
|
"import type { RequestErrorResponse } from '../../http/contract.js';\nimport { wireJson } from '../../http/query.js';\nimport type { RequestSuccessResponse } from '../../type/index.js';\nimport type { RequestOptions } from '../type/index.js';\nimport { notify } from './bus.js';\n\n/**\n * Error thrown for non-2xx responses. Carries the HTTP status so callers can key\n * behavior on it (401 redirects, 402 payment flows, error-boundary routing).\n */\nexport class RequestError extends Error {\n constructor(\n message: string,\n readonly status: number,\n ) {\n super(message);\n this.name = 'RequestError';\n }\n}\n\nexport function get<T>(url: string, opts?: RequestOptions) {\n return request<T>(url, { method: 'get' }, opts);\n}\n\nexport function post<T>(url: string, payload: unknown, opts?: RequestOptions) {\n return request<T>(url, { method: 'post', body: wireJson(payload) }, opts);\n}\n\nexport function patch<T>(url: string, payload: unknown, opts?: RequestOptions) {\n return request<T>(url, { method: 'patch', body: wireJson(payload) }, opts);\n}\n\nexport function put<T>(url: string, payload: unknown, opts?: RequestOptions) {\n return request<T>(url, { method: 'put', body: wireJson(payload) }, opts);\n}\n\nexport function remove<T>(url: string, opts?: RequestOptions) {\n return request<T>(url, { method: 'delete' }, opts);\n}\n\n/**\n * HTTP QUERY (RFC 10008): a safe, idempotent read whose JSON query travels in the\n * request body, avoiding URL-length limits. Method name must stay uppercase\n * (fetch only normalizes the classic verbs).\n */\nexport function query<T>(url: string, payload: unknown, opts?: RequestOptions) {\n return request<T>(url, { method: 'QUERY', body: wireJson(payload) }, opts);\n}\n\nfunction request<T>(url: string, init: RequestInit, opts?: RequestOptions) {\n notify({ phase: 'start', opts });\n\n init.headers = {\n accept: 'application/json',\n 'content-type': 'application/json',\n ...opts?.headers,\n };\n if (opts?.signal) {\n init.signal = opts.signal;\n }\n\n return fetch(url, init)\n .then((rawResp) =>\n rawResp.json().then((resp: unknown) => {\n const isSuccess = rawResp.status >= 200 && rawResp.status < 300;\n if (isSuccess) {\n notify({ phase: 'success', opts });\n return resp as RequestSuccessResponse<T>;\n }\n const errorResp = resp as Partial<RequestErrorResponse> | undefined;\n const error = {\n message: errorResp?.error?.message ?? rawResp.statusText,\n code: errorResp?.error?.code ?? rawResp.status,\n };\n notify({ phase: 'error', error, opts });\n throw new RequestError(error.message, error.code);\n }),\n )\n .finally(() => {\n notify({ phase: 'complete', opts });\n });\n}\n",
|
|
12
12
|
"export function kebabCase(val: string): string {\n let resp = val.charAt(0).toLowerCase();\n for (let i = 1; i < val.length; ++i) {\n resp += val[i] === val[i].toUpperCase() ? '-' + val[i].toLowerCase() : val[i];\n }\n return resp;\n}\n\nexport function upperFirst(text: string): string {\n return text.charAt(0).toUpperCase() + text.slice(1);\n}\n\nexport function lowerFirst(text: string): string {\n return text.charAt(0).toLowerCase() + text.slice(1);\n}\n\nexport function snakeCase(val: string): string {\n if (!val) return '';\n let resp = val.charAt(0).toLowerCase();\n for (let i = 1; i < val.length; ++i) {\n const char = val[i];\n const charLower = char.toLowerCase();\n if (char !== charLower && char === char.toUpperCase()) {\n resp += '_' + charLower;\n } else {\n resp += char;\n }\n }\n return resp;\n}\n\n/**\n * Convert a string to PascalCase (UpperCamelCase).\n * @example 'user_profile' -> 'UserProfile'\n * @example 'some-text' -> 'SomeText'\n */\nexport function pascalCase(str: string): string {\n if (!str) return '';\n return str\n .split(/[_\\s-]+/)\n .map((word) => {\n // Lower-casing the rest is only right for a word that carries no case of its own: it turns\n // `USER_ID` into `UserId`, but it also turns `tenantId` into `Tenantid`.\n const rest = word === word.toUpperCase() ? word.slice(1).toLowerCase() : word.slice(1);\n return word.charAt(0).toUpperCase() + rest;\n })\n .join('');\n}\n\n/**\n * Convert a string to camelCase.\n * @example 'user_profile' -> 'userProfile'\n * @example 'SomeText' -> 'someText'\n */\nexport function camelCase(str: string): string {\n const pascal = pascalCase(str);\n return pascal.charAt(0).toLowerCase() + pascal.slice(1);\n}\n\n/**\n * Simple singularize function for English words.\n * @example 'users' -> 'user'\n * @example 'categories' -> 'category'\n */\nexport function singularize(name: string): string {\n if (!name) return '';\n if (name.endsWith('ies')) {\n return name.slice(0, -3) + 'y';\n }\n if (name.endsWith('ses') || name.endsWith('xes') || name.endsWith('zes')) {\n return name.slice(0, -2);\n }\n if (name.endsWith('s') && !name.endsWith('ss')) {\n return name.slice(0, -1);\n }\n return name;\n}\n\n/**\n * Simple pluralize function for English words.\n * @example 'user' -> 'users'\n * @example 'category' -> 'categories'\n */\nexport function pluralize(name: string): string {\n if (!name) return '';\n if (name.endsWith('y') && name.length > 1 && !/[aeiou]/.test(name[name.length - 2])) {\n return name.slice(0, -1) + 'ies';\n }\n if (name.endsWith('s') || name.endsWith('x') || name.endsWith('z') || name.endsWith('ch') || name.endsWith('sh')) {\n return name + 'es';\n }\n return name + 's';\n}\n",
|
|
13
13
|
"import { queryErrorKind } from '../querier/queryError.js';\nimport type { Type, UniversalQuerier } from '../type/index.js';\n// the specific util modules, not the barrel, so the browser bundle does not pull in entity metadata\nimport { getKeys } from '../util/object.util.js';\nimport { kebabCase } from '../util/string.util.js';\nimport type { QueryErrorKind } from '../util/uqlError.js';\n\ntype RouteShape = {\n readonly method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';\n readonly path: '' | `/${string}`;\n};\n\n/**\n * Single source of truth for the CRUD-over-HTTP surface, shared by server adapters and the browser client.\n * Keys are constrained to {@link UniversalQuerier} method names, so renaming a querier method\n * (or routing a non-existent one) is a compile error.\n */\nexport const CRUD_ROUTES = {\n findMany: { method: 'GET', path: '' },\n findOne: { method: 'GET', path: '/one' },\n count: { method: 'GET', path: '/count' },\n findOneById: { method: 'GET', path: '/:id' },\n insertOne: { method: 'POST', path: '' },\n insertMany: { method: 'POST', path: '/many' },\n saveOne: { method: 'PUT', path: '' },\n saveMany: { method: 'PUT', path: '/many' },\n updateMany: { method: 'PATCH', path: '' },\n updateOneById: { method: 'PATCH', path: '/:id' },\n deleteOneById: { method: 'DELETE', path: '/:id' },\n deleteMany: { method: 'DELETE', path: '' },\n} as const satisfies Partial<Record<keyof UniversalQuerier, RouteShape>>;\n\nexport type CrudOperation = keyof typeof CRUD_ROUTES;\n\nexport type CrudRoute = (typeof CRUD_ROUTES)[CrudOperation];\n\n/**\n * `QUERY` (RFC 10008) is an alternate transport for the read operations: same semantics as the\n * GET routes, but the JSON query travels in the request body instead of the query string,\n * avoiding URL-length limits for large queries.\n */\nexport type HttpMethod = CrudRoute['method'] | 'QUERY';\n\nconst CRUD_OPS = getKeys(CRUD_ROUTES);\n\n// derived from CRUD_ROUTES (the literal-path GET routes) so the sub-paths live in exactly one place\nconst QUERY_READ_OPS: ReadonlyMap<string, CrudOperation> = new Map(\n CRUD_OPS.filter((op) => CRUD_ROUTES[op].method === 'GET' && CRUD_ROUTES[op].path !== '/:id').map((op) => [\n CRUD_ROUTES[op].path,\n op,\n ]),\n);\n\n/**\n * URL segment for an entity, e.g. `entityPath(UserProfile) === 'user-profile'`.\n */\nexport function entityPath<E>(entity: Type<E>): string {\n return kebabCase(entity.name);\n}\n\nexport type RouteMatch = {\n readonly op: CrudOperation;\n /**\n * the resolved transport method - differs from the op's canonical route method for QUERY.\n */\n readonly method: HttpMethod;\n readonly id?: string;\n};\n\n/**\n * Resolve a (method, sub-path) pair to a CRUD operation. Literal sub-paths win over `:id`.\n */\nexport function matchRoute(method: string, subPath: string | undefined): RouteMatch | undefined {\n const raw = method.toUpperCase();\n const literal = subPath === undefined ? '' : `/${subPath}`;\n if (raw === 'QUERY') {\n const op = QUERY_READ_OPS.get(literal);\n return op ? { op, method: 'QUERY' } : undefined;\n }\n // HEAD reads like GET per HTTP semantics; the server runtime omits the response body\n const verb = raw === 'HEAD' ? 'GET' : raw;\n let idOp: CrudOperation | undefined;\n for (const op of CRUD_OPS) {\n const route = CRUD_ROUTES[op];\n if (route.method !== verb) {\n continue;\n }\n if (route.path === literal) {\n return { op, method: route.method };\n }\n if (route.path === '/:id') {\n idOp = op;\n }\n }\n return idOp && subPath !== undefined ? { op: idOp, method: CRUD_ROUTES[idOp].method, id: subPath } : undefined;\n}\n\nexport type RequestErrorResponse = {\n readonly error: {\n readonly message: string;\n readonly code: number;\n };\n};\n\n/** Generic on purpose: a driver's constraint message names tables and constraints, and Postgres echoes the value. */\nconst CONSTRAINT_ERRORS: ReadonlyMap<QueryErrorKind | undefined, RequestErrorResponse['error']> = new Map([\n ['uniqueViolation', { message: 'Conflict', code: 409 }],\n ['foreignKeyViolation', { message: 'Conflict', code: 409 }],\n ['notNullViolation', { message: 'Bad Request', code: 400 }],\n ['checkViolation', { message: 'Bad Request', code: 400 }],\n]);\n\n/**\n * Map a thrown error to the wire error envelope. Honors a numeric `status` on the error\n * (e.g. hooks throwing 403), then a constraint violation (409/400), defaults to 500; `code` mirrors the HTTP status.\n */\nexport function toErrorResponse(err: unknown): { status: number; body: RequestErrorResponse } {\n const error =\n err instanceof Error && 'status' in err && typeof err.status === 'number'\n ? { message: err.message, code: err.status }\n : (CONSTRAINT_ERRORS.get(queryErrorKind(err)) ?? {\n message: err instanceof Error ? err.message : 'Internal Server Error',\n code: 500,\n });\n return { status: error.code, body: { error } };\n}\n",
|
|
14
14
|
"import { CRUD_ROUTES, entityPath, type HttpMethod } from '../../http/contract.js';\nimport { stringifyQuery } from '../../http/query.js';\nimport type {\n EntityWrite,\n EntityId,\n FieldKey,\n QueryFilter,\n QueryFindResult,\n QueryOneProjected,\n QueryOptions,\n QueryPage,\n QueryProjected,\n QuerySearch,\n RelationKey,\n RequestCountedSuccessResponse,\n RequestSuccessResponse,\n Type,\n UpdateWrite,\n WireQuery,\n WrittenId,\n} from '../../type/index.js';\nimport { isScalarId } from '../../util/object.util.js';\nimport { UqlUsageError } from '../../util/uqlError.js';\nimport { get, query as httpQuery, patch, post, put, remove } from '../http/index.js';\nimport type { ClientQuerier, RequestFindOptions, RequestOptions } from '../type/index.js';\n\nexport type HttpQuerierDefaults = {\n /**\n * headers sent with every request from this instance, merged under per-call headers.\n * Create one instance per request (e.g. during SSR) to scope auth headers safely.\n */\n readonly headers?: Record<string, string>;\n /**\n * transport for read queries (findOne, findMany, count). 'QUERY' (RFC 10008) sends the\n * JSON query in the request body, avoiding URL-length limits for large queries; requires\n * infrastructure (proxies, CDNs) that forwards the QUERY method. Defaults to 'GET'.\n */\n readonly readMethod?: Extract<HttpMethod, 'GET' | 'QUERY'>;\n /**\n * The URL segment an entity is addressed by, defaulting to its kebab-cased class name - the same\n * option the server handler takes, so one map serves both. State it where the default cannot: a\n * build that minifies class names renames every route.\n */\n readonly entityPath?: (entity: Type<unknown>) => string;\n};\n\n/** The id as one path segment, refusing a composite key, which has no spelling in `/:id` yet. Callers are `async`. */\nfunction idSegment<E>(entity: Type<E>, id: EntityId<E>): string {\n if (!isScalarId(id)) {\n throw new UqlUsageError(`'${entity.name}' was addressed by an id object, which the HTTP route cannot carry.`);\n }\n return String(id);\n}\n\nexport class HttpQuerier implements ClientQuerier {\n constructor(\n readonly basePath: string,\n readonly defaults: HttpQuerierDefaults = {},\n ) {}\n\n async findOneById<\n E extends object,\n const S extends FieldKey<E> = never,\n const V = true,\n const X extends FieldKey<E> = never,\n const P extends RelationKey<E> = never,\n const C extends RelationKey<E> = never,\n >(\n entity: Type<E>,\n id: EntityId<E>,\n q?: QueryOneProjected<E, S, V, X, P, C, never>,\n opts?: RequestOptions,\n ): Promise<RequestSuccessResponse<QueryFindResult<E, S, V, X, P, C> | undefined>> {\n const basePath = this.getBasePath(entity);\n const qs = stringifyQuery(q);\n return get<QueryFindResult<E, S, V, X, P, C> | undefined>(\n `${basePath}/${idSegment(entity, id)}${qs}`,\n this.buildOptions(opts),\n );\n }\n\n findOne<\n E extends object,\n const S extends FieldKey<E> = never,\n const V = true,\n const X extends FieldKey<E> = never,\n const P extends RelationKey<E> = never,\n const C extends RelationKey<E> = never,\n >(\n entity: Type<E>,\n q: QueryOneProjected<E, S, V, X, P, C, never>,\n opts?: RequestOptions,\n ): Promise<RequestSuccessResponse<QueryFindResult<E, S, V, X, P, C> | undefined>> {\n return this.read<QueryFindResult<E, S, V, X, P, C> | undefined>(\n `${this.getBasePath(entity)}${CRUD_ROUTES.findOne.path}`,\n q,\n opts,\n );\n }\n\n findMany<\n E extends object,\n const S extends FieldKey<E> = never,\n const V = true,\n const X extends FieldKey<E> = never,\n const P extends RelationKey<E> = never,\n const C extends RelationKey<E> = never,\n >(\n entity: Type<E>,\n q: QueryProjected<E, S, V, X, P, C, never>,\n opts?: RequestFindOptions,\n ): Promise<RequestSuccessResponse<QueryFindResult<E, S, V, X, P, C>[]>> {\n const data: WireQuery<E> & { count?: boolean } = { ...q };\n if (opts?.count) {\n data.count = true;\n }\n return this.read<QueryFindResult<E, S, V, X, P, C>[]>(this.getBasePath(entity), data, opts);\n }\n\n async findManyAndCount<\n E extends object,\n const S extends FieldKey<E> = never,\n const V = true,\n const X extends FieldKey<E> = never,\n const P extends RelationKey<E> = never,\n const C extends RelationKey<E> = never,\n >(\n entity: Type<E>,\n q: QueryProjected<E, S, V, X, P, C, never>,\n opts?: RequestFindOptions,\n ): Promise<RequestCountedSuccessResponse<QueryFindResult<E, S, V, X, P, C>[]>> {\n const response = await this.findMany(entity, q, { ...opts, count: true });\n if (typeof response.count !== 'number') {\n throw new TypeError('findManyAndCount response has an invalid count');\n }\n return { ...response, count: response.count };\n }\n\n count<E extends object>(entity: Type<E>, q?: QueryPage<E, never>, opts?: RequestOptions) {\n return this.read<number>(`${this.getBasePath(entity)}${CRUD_ROUTES.count.path}`, q, opts);\n }\n\n /** The `count` route capped at one row, so existence needs no endpoint of its own. */\n async exists<E extends object>(entity: Type<E>, q?: QueryFilter<E, never>, opts?: RequestOptions) {\n const res = await this.count(entity, { ...q, $limit: 1 }, opts);\n return { ...res, data: res.data > 0 };\n }\n\n insertOne<E extends object>(entity: Type<E>, payload: EntityWrite<E>, opts?: RequestOptions) {\n const basePath = this.getBasePath(entity);\n return post<WrittenId<E> | undefined>(basePath, payload, this.buildOptions(opts));\n }\n\n insertMany<E extends object>(entity: Type<E>, payload: readonly EntityWrite<E>[], opts?: RequestOptions) {\n const basePath = this.getBasePath(entity);\n return post<(WrittenId<E> | undefined)[]>(\n `${basePath}${CRUD_ROUTES.insertMany.path}`,\n payload,\n this.buildOptions(opts),\n );\n }\n\n async updateOneById<E extends object>(\n entity: Type<E>,\n id: EntityId<E>,\n payload: UpdateWrite<E, never>,\n opts?: RequestOptions,\n ) {\n const basePath = this.getBasePath(entity);\n return patch<number>(`${basePath}/${idSegment(entity, id)}`, payload, this.buildOptions(opts));\n }\n\n updateMany<E extends object>(\n entity: Type<E>,\n q: QuerySearch<E, never>,\n payload: UpdateWrite<E, never>,\n opts?: RequestOptions,\n ) {\n const basePath = this.getBasePath(entity);\n const qs = stringifyQuery(q);\n return patch<number>(`${basePath}${qs}`, payload, this.buildOptions(opts));\n }\n\n saveOne<E extends object>(entity: Type<E>, payload: EntityWrite<E>, opts?: RequestOptions) {\n const basePath = this.getBasePath(entity);\n return put<WrittenId<E> | undefined>(basePath, payload, this.buildOptions(opts));\n }\n\n saveMany<E extends object>(entity: Type<E>, payload: readonly EntityWrite<E>[], opts?: RequestOptions) {\n const basePath = this.getBasePath(entity);\n return put<(WrittenId<E> | undefined)[]>(\n `${basePath}${CRUD_ROUTES.saveMany.path}`,\n payload,\n this.buildOptions(opts),\n );\n }\n\n async deleteOneById<E extends object>(entity: Type<E>, id: EntityId<E>, opts: QueryOptions & RequestOptions = {}) {\n const basePath = this.getBasePath(entity);\n const qs = opts.hardDelete ? stringifyQuery({ hardDelete: opts.hardDelete }) : '';\n return remove<number>(`${basePath}/${idSegment(entity, id)}${qs}`, this.buildOptions(opts));\n }\n\n deleteMany<E extends object>(entity: Type<E>, q: QuerySearch<E, never>, opts: QueryOptions & RequestOptions = {}) {\n const basePath = this.getBasePath(entity);\n const qs = stringifyQuery(opts.hardDelete ? { ...q, hardDelete: opts.hardDelete } : q);\n return remove<number>(`${basePath}${qs}`, this.buildOptions(opts));\n }\n\n getBasePath<E>(entity: Type<E>) {\n return `${this.basePath}/${(this.defaults.entityPath ?? entityPath)(entity)}`;\n }\n\n protected read<T>(path: string, q: Record<string, unknown> | undefined, opts?: RequestOptions) {\n if (this.defaults.readMethod === 'QUERY') {\n return httpQuery<T>(path, q ?? {}, this.buildOptions(opts));\n }\n return get<T>(`${path}${stringifyQuery(q)}`, this.buildOptions(opts));\n }\n\n protected buildOptions(opts?: RequestOptions): RequestOptions | undefined {\n if (!this.defaults.headers && !opts?.headers) {\n return opts;\n }\n return { ...opts, headers: { ...this.defaults.headers, ...opts?.headers } };\n }\n}\n",
|
|
15
15
|
"import { HttpQuerier } from './querier/httpQuerier.js';\nimport type { ClientQuerier, ClientQuerierPool } from './type/index.js';\n\nlet defaultPool: ClientQuerierPool = {\n getQuerier: () => new HttpQuerier('/api'),\n};\n\nexport function setQuerierPool<T extends ClientQuerierPool>(pool: T) {\n defaultPool = pool;\n}\n\nexport function getQuerierPool(): ClientQuerierPool {\n return defaultPool;\n}\n\nexport function getQuerier(): ClientQuerier {\n return getQuerierPool().getQuerier();\n}\n"
|
|
16
16
|
],
|
|
17
|
-
"mappings": "AAEA,IAAM,EAAkC,CAAC,EAElC,SAAS,CAAM,CAAC,EAAyC,CAC9D,QAAW,KAAe,EACxB,EAAY,CAAY,EAIrB,SAAS,CAAE,CAAC,EAAiC,CAClD,EAAa,KAAK,CAAE,EACpB,IAAM,EAAQ,EAAa,OAAS,EACpC,MAAO,IAAY,CACjB,EAAa,OAAO,EAAO,CAAC,GC6UzB,IAAM,EAAuB,CAClC,UACA,YACA,WACA,SACA,OACF,EAMa,EAA4B,CAAC,QAAQ,EAErC,EAAuB,CAAC,QAAS,QAAQ,EAOzC,EAA4B,CAAC,aAAa,EAE1C,EAAwB,CAAC,WA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IAGlE,IAAO,CAAC,EAAc,EAAwC,EAAuB,CAC7F,GAAI,KAAK,SAAS,aAAe,QAC/B,OAAO,EAAa,EAAM,GAAK,CAAC,EAAG,KAAK,aAAa,CAAI,CAAC,EAE5D,OAAO,EAAO,GAAG,IAAO,EAAe,CAAC,IAAK,KAAK,aAAa,CAAI,CAAC,EAG5D,YAAY,CAAC,EAAmD,CACxE,GAAI,CAAC,KAAK,SAAS,SAAW,CAAC,GAAM,QACnC,OAAO,EAET,MAAO,IAAK,EAAM,QAAS,IAAK,KAAK,SAAS,WAAY,GAAM,OAAQ,CAAE,EAE9E,CC/NA,IAAI,EAAiC,CACnC,WAAY,IAAM,IAAI,EAAY,MAAM,CAC1C,EAEO,SAAS,EAA2C,CAAC,EAAS,CACnE,EAAc,EAGT,SAAS,CAAc,EAAsB,CAClD,OAAO,EAGF,SAAS,EAAU,EAAkB,CAC1C,OAAO,EAAe,EAAE,WAAW",
|
|
18
|
+
"debugId": "54F53C9583121D9864756E2164756E21",
|
|
19
19
|
"names": []
|
|
20
20
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Pool, types } from 'pg';
|
|
2
2
|
import { dialectOptionsFrom } from '../dialect/abstractDialect.js';
|
|
3
3
|
import { AbstractPgQuerierPool } from '../postgres/abstractPgQuerierPool.js';
|
|
4
|
-
import {
|
|
4
|
+
import { wireTypes } from '../postgres/pgWireTypes.js';
|
|
5
5
|
import { CockroachDialect } from './cockroachDialect.js';
|
|
6
6
|
/**
|
|
7
7
|
* QuerierPool for CockroachDB using the `pg` driver Pool.
|
|
8
8
|
*/
|
|
9
9
|
export class CrdbQuerierPool extends AbstractPgQuerierPool {
|
|
10
10
|
constructor(opts, extra) {
|
|
11
|
-
super(new CockroachDialect(dialectOptionsFrom(extra)), new Pool({ keepAlive: true, types:
|
|
11
|
+
super(new CockroachDialect(dialectOptionsFrom(extra)), new Pool({ keepAlive: true, types: wireTypes(types), ...opts }), extra);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
@@ -698,7 +698,7 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
698
698
|
private sizeCondition;
|
|
699
699
|
/** `<distance> <op> ?`, the `$where` half of a vector search, its bounds checked here since `/http` input is untyped. */
|
|
700
700
|
private vectorNearCondition;
|
|
701
|
-
/** ANSI-style single-quote escaping.
|
|
701
|
+
/** ANSI-style single-quote escaping. */
|
|
702
702
|
escape(value: unknown): string;
|
|
703
703
|
protected get regexpOp(): string;
|
|
704
704
|
/**
|
|
@@ -708,7 +708,6 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
708
708
|
* throw, the way {@link appendTextSearch} already does.
|
|
709
709
|
*/
|
|
710
710
|
protected regexCondition(operand: string, placeholder: string): string;
|
|
711
|
-
protected get likeFn(): string;
|
|
712
711
|
/**
|
|
713
712
|
* Two fragments compared null-safely: true where they differ, and where one side alone is NULL. What a
|
|
714
713
|
* trigger compares a column's two rows with, where the portable `<>` would miss a column set to or
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { fieldOf, getMeta, relationOf, soleIdOf } from '../entity/index.js';
|
|
2
2
|
import { COUNT_RESULT_KEY, parseQueryLock, QueryRaw, RAW_ALIAS, } from '../type/index.js';
|
|
3
3
|
import { isInlinedExpression } from '../util/field.util.js';
|
|
4
|
-
import {
|
|
4
|
+
import { isSelectList, assertNonNegativeInteger, assertWhere, definedEntries, escapeSqlId, fillOnFields, filterFieldKeys, getInsertFieldKeys, getKeys, getRelationRequestSummary, getSoftDeleteValue, hasKeys, idOnlyQuery, columnFamily, countedRelations, fieldUpdateOf, fulltextIndexOver, fulltextWeights, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, isOperatorMap, isOperatorKey, isVectorSearch, normalizeScalarFieldSelection, parentJoins, rankedTextSearch, targetKeyColumns, textSearchFields, textSortOf, textWeightSteps, parseGroupMap, parseRelationAtKey, parseRelationSize, populatesRelations, aggregateOf, raw, refs, throwUnknownAggregateColumn, withoutSoftDeleteFilter, } from '../util/index.js';
|
|
5
5
|
import { escapeAnsiSqlLiteral } from '../util/sqlLiteral.js';
|
|
6
6
|
import { kindOf, UqlUsageError } from '../util/uqlError.js';
|
|
7
7
|
import { AGGREGATE_PAGE_ALIAS, AGGREGATE_VALUE_ALIAS, ROWS_ALIAS, JSON_ELEM_ALIAS, JSON_PULL_ALIAS, relationSortColumn, } from './aliases.js';
|
|
@@ -22,11 +22,18 @@ export function relationTermKey({ sql, key }) {
|
|
|
22
22
|
* none is left of a row crossing JSON, which answers only under keys.
|
|
23
23
|
*/
|
|
24
24
|
function projectedKeys(meta, select, exclude, json) {
|
|
25
|
-
const selected =
|
|
26
|
-
? select
|
|
27
|
-
: normalizeScalarFieldSelection(meta,
|
|
25
|
+
const selected = isSelectList(select)
|
|
26
|
+
? select.map(selectRaw)
|
|
27
|
+
: normalizeScalarFieldSelection(meta, select, exclude);
|
|
28
28
|
return selected.length || !json ? selected : normalizeScalarFieldSelection(meta);
|
|
29
29
|
}
|
|
30
|
+
/** An item of a `$select` list, which only `raw()` fills: anything else arrived from an untyped client. */
|
|
31
|
+
function selectRaw(item) {
|
|
32
|
+
if (item instanceof QueryRaw) {
|
|
33
|
+
return item;
|
|
34
|
+
}
|
|
35
|
+
throw new UqlUsageError(`a $select list takes raw() expressions only, not a ${kindOf(item)}: name fields in its map form, { field: true }`);
|
|
36
|
+
}
|
|
30
37
|
/** `value`, where there is one; a `UqlUsageError` saying `refusal` where there is none. */
|
|
31
38
|
function orRefuse(value, refusal) {
|
|
32
39
|
if (value === undefined) {
|
|
@@ -564,8 +571,9 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
564
571
|
const fold = like.insensitive && this.caseInsensitiveMatch === 'fold';
|
|
565
572
|
const value = String(val);
|
|
566
573
|
const ph = this.addValue(ctx, like.pattern(fold ? value.toLowerCase() : value));
|
|
567
|
-
const matchOp = like.insensitive && this.caseInsensitiveMatch === 'ilike' ? 'ILIKE' :
|
|
568
|
-
|
|
574
|
+
const matchOp = like.insensitive && this.caseInsensitiveMatch === 'ilike' ? 'ILIKE' : 'LIKE';
|
|
575
|
+
// Stated on every engine, although only SQLite and SQL Server lack `\` as their default escape.
|
|
576
|
+
return `${fold ? `LOWER(${operand})` : operand} ${matchOp} ${ph} ESCAPE ${this.escape('\\')}`;
|
|
569
577
|
}
|
|
570
578
|
/** Builds `prefix.column` from an already-resolved field, through the same memo writes use. */
|
|
571
579
|
columnWithPrefix(key, field, opts) {
|
|
@@ -1953,7 +1961,7 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
1953
1961
|
const distance = (fragmentCtx) => this.appendVectorDistance(fragmentCtx, meta, key, near, prefix);
|
|
1954
1962
|
return this.boundConditions(ctx, distance, bounds, (operand, op, val) => (isOrderedOp(op) ? this.operatorCondition(ctx, operand, op, val) : undefined), 'unsupported $near bound');
|
|
1955
1963
|
}
|
|
1956
|
-
/** ANSI-style single-quote escaping.
|
|
1964
|
+
/** ANSI-style single-quote escaping. */
|
|
1957
1965
|
escape(value) {
|
|
1958
1966
|
return escapeAnsiSqlLiteral(value);
|
|
1959
1967
|
}
|
|
@@ -1969,9 +1977,6 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
1969
1977
|
regexCondition(operand, placeholder) {
|
|
1970
1978
|
return `${operand} ${this.regexpOp} ${placeholder}`;
|
|
1971
1979
|
}
|
|
1972
|
-
get likeFn() {
|
|
1973
|
-
return 'LIKE';
|
|
1974
|
-
}
|
|
1975
1980
|
/** `operand IN (...)` of each value as `bind` renders it, or the constant an empty set reduces to: no value is in it. */
|
|
1976
1981
|
formatIn(_ctx, operand, values, negate, bind) {
|
|
1977
1982
|
if (!values.length) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { decodeDate } from '../util/date.js';
|
|
1
2
|
import { decodeWideNumber } from '../util/wideNumber.js';
|
|
2
3
|
import { decodeFloat32s, parseVectorLiteral } from './vectorCast.js';
|
|
3
4
|
/**
|
|
@@ -31,7 +32,7 @@ const float32Decoder = (value) => {
|
|
|
31
32
|
const DECODERS = {
|
|
32
33
|
// 0/1 from SQLite's INTEGER or MySQL's TINYINT(1). Already a boolean on Postgres.
|
|
33
34
|
boolean: (value) => (typeof value === 'boolean' ? value : Boolean(value)),
|
|
34
|
-
date: (value) => (typeof value === 'string' ? (
|
|
35
|
+
date: (value) => (typeof value === 'string' ? decodeDate(value) : value),
|
|
35
36
|
// Only a string can be bytes that crossed JSON: bytes a driver already decoded stay as they are.
|
|
36
37
|
bytes: (value) => typeof value === 'string' && value.startsWith(BYTES_PREFIX) ? hexBytes(value.slice(BYTES_PREFIX.length)) : value,
|
|
37
38
|
// A number too, not just text: `type: BigInt` is BIGINT, which the pg pools decode at the wire.
|
|
@@ -61,17 +62,6 @@ const DECODERS = {
|
|
|
61
62
|
halfvec: vectorDecoder('halfvec'),
|
|
62
63
|
sparsevec: vectorDecoder('sparsevec'),
|
|
63
64
|
};
|
|
64
|
-
/**
|
|
65
|
-
* An ISO 8601 timestamp as a `Date`, its fraction cut to the milliseconds one holds, and a bare date at
|
|
66
|
-
* local midnight, which is how `pg` reads a `date`. `undefined` for text that is neither.
|
|
67
|
-
*/
|
|
68
|
-
function parseDate(text) {
|
|
69
|
-
const day = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
|
|
70
|
-
const date = day
|
|
71
|
-
? new Date(Number(day[1]), Number(day[2]) - 1, Number(day[3]))
|
|
72
|
-
: new Date(text.replace(/(\.\d{3})\d+/, '$1'));
|
|
73
|
-
return Number.isNaN(date.getTime()) ? undefined : date;
|
|
74
|
-
}
|
|
75
65
|
/**
|
|
76
66
|
* What bytes crossing JSON start with, before two hex digits per byte: Postgres's own text for `bytea`,
|
|
77
67
|
* which every dialect spells, so a string a driver reads from a column on its own is never mistaken.
|
|
@@ -78,6 +78,8 @@ export declare abstract class MysqlLikeSqlDialect extends AbstractSqlDialect {
|
|
|
78
78
|
/** Bytes as the hex text `decodeColumn` reads back, whole. */
|
|
79
79
|
protected bytesAsText(expr: string): string;
|
|
80
80
|
escape(value: unknown): string;
|
|
81
|
+
/** A date as UTC text, which a `DATETIME` stores as is, where a driver would convert it to its own zone. */
|
|
82
|
+
normalizeValue(value: unknown): unknown;
|
|
81
83
|
/**
|
|
82
84
|
* `MATCH(cols) AGAINST(?)`, which needs a `FULLTEXT` index over exactly those columns: without one
|
|
83
85
|
* the server answers "Can't find FULLTEXT index matching the column list". Declare it with
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getMeta } from '../entity/index.js';
|
|
2
|
+
import { utcTimestamp } from '../util/date.js';
|
|
2
3
|
import { textSearchFields } from '../util/index.js';
|
|
3
4
|
import { escapeMysqlSqlLiteral, escapeSingleQuotes } from '../util/sqlLiteral.js';
|
|
4
5
|
import { AbstractSqlDialect, } from './abstractSqlDialect.js';
|
|
@@ -187,6 +188,10 @@ export class MysqlLikeSqlDialect extends AbstractSqlDialect {
|
|
|
187
188
|
escape(value) {
|
|
188
189
|
return escapeMysqlSqlLiteral(value);
|
|
189
190
|
}
|
|
191
|
+
/** A date as UTC text, which a `DATETIME` stores as is, where a driver would convert it to its own zone. */
|
|
192
|
+
normalizeValue(value) {
|
|
193
|
+
return value instanceof Date ? utcTimestamp(value) : super.normalizeValue(value);
|
|
194
|
+
}
|
|
190
195
|
/**
|
|
191
196
|
* `MATCH(cols) AGAINST(?)`, which needs a `FULLTEXT` index over exactly those columns: without one
|
|
192
197
|
* the server answers "Can't find FULLTEXT index matching the column list". Declare it with
|
|
@@ -61,6 +61,22 @@ export declare const ORDERED_OPS: ReadonlySet<string>;
|
|
|
61
61
|
export declare function isOrderedOp(op: string): op is QueryOrderedOp;
|
|
62
62
|
/** The operators an equality compares by value, which a JSON path reads the way that value compares. */
|
|
63
63
|
export declare const EQUALITY_OPS: ReadonlySet<string>;
|
|
64
|
+
/**
|
|
65
|
+
* `value` as a `$like` pattern matching it literally. The pattern language is the same on every engine:
|
|
66
|
+
* `%` and `_` are wildcards, `\` escapes, and `[` is escaped since SQL Server reads it as a character class.
|
|
67
|
+
*/
|
|
68
|
+
export declare function likeLiteral(value: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* `pattern` with its `[` escaped, as {@link likeLiteral} does; refused where its last `\` has nothing
|
|
71
|
+
* after it to escape (`'John\'`): Postgres throws on one, MySQL reads it literally and SQLite matches nothing.
|
|
72
|
+
*/
|
|
73
|
+
export declare function likePattern(pattern: string): string;
|
|
74
|
+
/**
|
|
75
|
+
* A `$like` pattern as the anchored regex matching the same strings, for an engine with no `LIKE`. A
|
|
76
|
+
* wildcard spans lines, as it does in SQL; an edge `%` drops its anchor, which keeps a prefix indexable.
|
|
77
|
+
* The end anchor is `\z`, since `$` also matches before a trailing newline.
|
|
78
|
+
*/
|
|
79
|
+
export declare function likeRegex(pattern: string): string;
|
|
64
80
|
/**
|
|
65
81
|
* Every `$like`-family operator: the pattern it wraps its value in, and whether it ignores case.
|
|
66
82
|
* Each case-sensitive operator is paired here with the `$i` twin that shares its pattern, so the
|
|
@@ -122,6 +122,49 @@ export function isOrderedOp(op) {
|
|
|
122
122
|
}
|
|
123
123
|
/** The operators an equality compares by value, which a JSON path reads the way that value compares. */
|
|
124
124
|
export const EQUALITY_OPS = new Set(['$eq', '$ne', '$in', '$nin']);
|
|
125
|
+
/**
|
|
126
|
+
* `value` as a `$like` pattern matching it literally. The pattern language is the same on every engine:
|
|
127
|
+
* `%` and `_` are wildcards, `\` escapes, and `[` is escaped since SQL Server reads it as a character class.
|
|
128
|
+
*/
|
|
129
|
+
export function likeLiteral(value) {
|
|
130
|
+
return value.replace(/[\\%_[]/g, '\\$&');
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* `pattern` with its `[` escaped, as {@link likeLiteral} does; refused where its last `\` has nothing
|
|
134
|
+
* after it to escape (`'John\'`): Postgres throws on one, MySQL reads it literally and SQLite matches nothing.
|
|
135
|
+
*/
|
|
136
|
+
export function likePattern(pattern) {
|
|
137
|
+
const tokens = likeTokens(pattern);
|
|
138
|
+
const last = tokens.at(-1);
|
|
139
|
+
if (last?.char === '\\' && !last.escaped) {
|
|
140
|
+
throw new UqlUsageError("a $like pattern cannot end in a '\\' with nothing after it to escape: write '\\\\' to match a backslash");
|
|
141
|
+
}
|
|
142
|
+
return tokens.map(({ char, escaped }) => (escaped || char === '[' ? `\\${char}` : char)).join('');
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* A `$like` pattern as the anchored regex matching the same strings, for an engine with no `LIKE`. A
|
|
146
|
+
* wildcard spans lines, as it does in SQL; an edge `%` drops its anchor, which keeps a prefix indexable.
|
|
147
|
+
* The end anchor is `\z`, since `$` also matches before a trailing newline.
|
|
148
|
+
*/
|
|
149
|
+
export function likeRegex(pattern) {
|
|
150
|
+
const tokens = likeTokens(pattern).map(({ char, escaped }) => (escaped ? undefined : LIKE_WILDCARDS[char]) ?? escapeRegex(char));
|
|
151
|
+
const open = tokens[0] === LIKE_WILDCARDS['%'];
|
|
152
|
+
const closed = tokens.at(-1) !== LIKE_WILDCARDS['%'];
|
|
153
|
+
const body = tokens.slice(open ? 1 : 0, closed ? undefined : -1).join('');
|
|
154
|
+
return `${open ? '' : '^'}${body}${closed ? String.raw `\z` : ''}`;
|
|
155
|
+
}
|
|
156
|
+
/** A `$like` pattern's characters, each marked where a `\` escaped it; a last `\` escaping nothing reads as plain. */
|
|
157
|
+
function likeTokens(pattern) {
|
|
158
|
+
return [...pattern.matchAll(/\\([\s\S])|[\s\S]/g)].map(([token, escaped]) => ({
|
|
159
|
+
char: escaped ?? token,
|
|
160
|
+
escaped: escaped !== undefined,
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
/** Each `$like` wildcard as the regex matching it, across lines as SQL does. */
|
|
164
|
+
const LIKE_WILDCARDS = { '%': String.raw `[\s\S]*`, _: String.raw `[\s\S]` };
|
|
165
|
+
function escapeRegex(char) {
|
|
166
|
+
return char.replace(/[\\^$.*+?()[\]{}|]/, '\\$&');
|
|
167
|
+
}
|
|
125
168
|
/**
|
|
126
169
|
* Every `$like`-family operator: the pattern it wraps its value in, and whether it ignores case.
|
|
127
170
|
* Each case-sensitive operator is paired here with the `$i` twin that shares its pattern, so the
|
|
@@ -129,10 +172,10 @@ export const EQUALITY_OPS = new Set(['$eq', '$ne', '$in', '$nin']);
|
|
|
129
172
|
* `AbstractSqlDialect.caseInsensitiveMatch`'s single call.
|
|
130
173
|
*/
|
|
131
174
|
export const LIKE_OPS = new Map([
|
|
132
|
-
['$like', '$ilike',
|
|
133
|
-
['$startsWith', '$istartsWith', (v) => `${v}%`],
|
|
134
|
-
['$endsWith', '$iendsWith', (v) => `%${v}`],
|
|
135
|
-
['$includes', '$iincludes', (v) => `%${v}%`],
|
|
175
|
+
['$like', '$ilike', likePattern],
|
|
176
|
+
['$startsWith', '$istartsWith', (v) => `${likeLiteral(v)}%`],
|
|
177
|
+
['$endsWith', '$iendsWith', (v) => `%${likeLiteral(v)}`],
|
|
178
|
+
['$includes', '$iincludes', (v) => `%${likeLiteral(v)}%`],
|
|
136
179
|
].flatMap(([sensitive, insensitive, pattern]) => [
|
|
137
180
|
[sensitive, { pattern, insensitive: false }],
|
|
138
181
|
[insensitive, { pattern, insensitive: true }],
|
|
@@ -105,6 +105,7 @@ export declare abstract class PgLikeSqlDialect extends AbstractSqlDialect {
|
|
|
105
105
|
* array takes its type from `operand`, so it needs none of the casts `bind` would give each value.
|
|
106
106
|
*/
|
|
107
107
|
protected formatIn(ctx: QueryContext, operand: string, values: unknown[], negate: boolean, bind: (value: unknown) => string): string;
|
|
108
|
+
escape(value: unknown): string;
|
|
108
109
|
protected numericCast(expr: string): string;
|
|
109
110
|
protected appendJsonValue(ctx: QueryContext, value: unknown, type: JsonColumnType): void;
|
|
110
111
|
/**
|