uql-orm 0.111.2 → 0.112.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 +4 -4
- package/dist/browser/querier/httpQuerier.js +14 -8
- package/dist/dialect/abstractSqlDialect.js +3 -3
- package/dist/dialect/jsonSql.js +2 -3
- package/dist/dialect/operators.js +4 -5
- package/dist/entity/decorator/members.d.ts +4 -13
- package/dist/entity/decorator/members.js +19 -10
- package/dist/entity/metadata/definedFixture/author.d.ts +2 -1
- package/dist/entity/metadata/definedFixture/folder.d.ts +126 -0
- package/dist/entity/metadata/definedFixture/folder.js +12 -0
- package/dist/entity/metadata/definition.js +3 -3
- package/dist/http/contract.d.ts +15 -0
- package/dist/http/contract.js +14 -14
- package/dist/http/handler.js +6 -3
- package/dist/http/query.d.ts +3 -5
- package/dist/http/query.js +10 -10
- package/dist/http/wireRow.d.ts +18 -0
- package/dist/http/wireRow.js +157 -0
- package/dist/mariadb/mariadbQuerierPool.d.ts +7 -1
- package/dist/mariadb/mariadbQuerierPool.js +0 -1
- package/dist/migrate/codegen/entityCodeGenerator.js +2 -3
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +4 -1
- package/dist/migrate/introspection/mysqlIntrospector.js +12 -2
- package/dist/mongodb/mongoDialect.js +6 -6
- package/dist/mssql/mssqlDialect.js +2 -2
- package/dist/sqlite/hranaQuerier.d.ts +2 -2
- package/dist/sqlite/localSqliteQuerierPool.d.ts +1 -1
- package/dist/sqlite/sqliteQuerierPool.d.ts +2 -2
- package/dist/sqlite/sqliteQuerierPool.js +2 -2
- package/dist/type/definedEntity.d.ts +8 -1
- package/dist/util/dialect.util.d.ts +0 -7
- package/dist/util/dialect.util.js +2 -16
- package/dist/util/object.util.d.ts +5 -2
- package/dist/util/object.util.js +12 -15
- package/dist/util/query.util.js +3 -10
- package/package.json +5 -10
- package/skills/uql-orm/SKILL.md +6 -5
- package/dist/browser/uql-browser.min.js +0 -4
- package/dist/browser/uql-browser.min.js.map +0 -19
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ export class User {
|
|
|
60
60
|
@Field(f.string().unique())
|
|
61
61
|
email!: string;
|
|
62
62
|
|
|
63
|
-
@OneToMany(
|
|
63
|
+
@OneToMany(() => Post, { mappedBy: (post) => post.author })
|
|
64
64
|
posts?: Post[];
|
|
65
65
|
}
|
|
66
66
|
|
|
@@ -78,7 +78,7 @@ export class Post {
|
|
|
78
78
|
@Field(f.references(() => User).nullable())
|
|
79
79
|
authorId?: number | null;
|
|
80
80
|
|
|
81
|
-
@ManyToOne(
|
|
81
|
+
@ManyToOne(() => User, { references: (post) => post.authorId })
|
|
82
82
|
author?: User;
|
|
83
83
|
}
|
|
84
84
|
```
|
|
@@ -100,8 +100,8 @@ export const Post = defineEntity({
|
|
|
100
100
|
author: () => rel.manyToOne(User, { references: (post) => post.authorId }),
|
|
101
101
|
}));
|
|
102
102
|
|
|
103
|
-
// One name for the entity and its rows, as a class has: a function takes `post: Post`.
|
|
104
|
-
export
|
|
103
|
+
// One name for the entity and its rows, as a class has: a function takes `post: Post`, and hovers and errors print `Post`.
|
|
104
|
+
export interface Post extends InstanceType<typeof Post> {}
|
|
105
105
|
```
|
|
106
106
|
|
|
107
107
|
### 2. A pool, and the migrations it drives
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
import { getMeta } from '../../entity/metadata/definition.js';
|
|
1
2
|
import { CRUD_ROUTES, entityPath } from '../../http/contract.js';
|
|
2
3
|
import { stringifyQuery } from '../../http/query.js';
|
|
4
|
+
import { readWireData } from '../../http/wireRow.js';
|
|
3
5
|
import { isScalarId } from '../../util/object.util.js';
|
|
4
6
|
import { UqlUsageError } from '../../util/uqlError.js';
|
|
5
7
|
import { get, query as httpQuery, patch, post, put, remove } from '../http/index.js';
|
|
8
|
+
/** A response with its `data` as the entity holds it, by what its route answers: JSON carried dates and keys as text. */
|
|
9
|
+
function dataOf(entity, response, answers) {
|
|
10
|
+
return { ...response, data: readWireData(getMeta(entity), response.data, answers) };
|
|
11
|
+
}
|
|
6
12
|
/** The id as one path segment, refusing a composite key, which has no spelling in `/:id` yet. Callers are `async`. */
|
|
7
13
|
function idSegment(entity, id) {
|
|
8
14
|
if (!isScalarId(id)) {
|
|
@@ -18,9 +24,9 @@ export class HttpQuerier {
|
|
|
18
24
|
this.defaults = defaults;
|
|
19
25
|
}
|
|
20
26
|
// Typed properties instead of methods, so each takes its signature from `ClientQuerier` rather than repeating it.
|
|
21
|
-
findOneById = async (entity, id, q, opts) => get(`${this.getBasePath(entity)}/${idSegment(entity, id)}${stringifyQuery(q)}`, this.buildOptions(opts));
|
|
22
|
-
findOne = (entity, q, opts) => this.read(`${this.getBasePath(entity)}${CRUD_ROUTES.findOne.path}`, q, opts);
|
|
23
|
-
findMany = (entity, q, opts) => this.read(this.getBasePath(entity), opts?.count ? { ...q, count: true } : q, opts);
|
|
27
|
+
findOneById = async (entity, id, q, opts) => dataOf(entity, await get(`${this.getBasePath(entity)}/${idSegment(entity, id)}${stringifyQuery(q)}`, this.buildOptions(opts)), CRUD_ROUTES.findOneById.answers);
|
|
28
|
+
findOne = async (entity, q, opts) => dataOf(entity, await this.read(`${this.getBasePath(entity)}${CRUD_ROUTES.findOne.path}`, q, opts), CRUD_ROUTES.findOne.answers);
|
|
29
|
+
findMany = async (entity, q, opts) => dataOf(entity, await this.read(this.getBasePath(entity), opts?.count ? { ...q, count: true } : q, opts), CRUD_ROUTES.findMany.answers);
|
|
24
30
|
findManyAndCount = async (entity, q, opts) => {
|
|
25
31
|
const response = await this.findMany(entity, q, { ...opts, count: true });
|
|
26
32
|
if (typeof response.count !== 'number') {
|
|
@@ -28,19 +34,19 @@ export class HttpQuerier {
|
|
|
28
34
|
}
|
|
29
35
|
return { ...response, count: response.count };
|
|
30
36
|
};
|
|
31
|
-
findManyPage = (entity, q, opts) => this.read(`${this.getBasePath(entity)}${CRUD_ROUTES.findManyPage.path}`, q, opts);
|
|
37
|
+
findManyPage = async (entity, q, opts) => dataOf(entity, await this.read(`${this.getBasePath(entity)}${CRUD_ROUTES.findManyPage.path}`, q, opts), CRUD_ROUTES.findManyPage.answers);
|
|
32
38
|
count = (entity, q, opts) => this.read(`${this.getBasePath(entity)}${CRUD_ROUTES.count.path}`, q, opts);
|
|
33
39
|
/** The `count` route capped at one row, so existence needs no endpoint of its own. */
|
|
34
40
|
exists = async (entity, q, opts) => {
|
|
35
41
|
const res = await this.count(entity, { ...q, $limit: 1 }, opts);
|
|
36
42
|
return { ...res, data: res.data > 0 };
|
|
37
43
|
};
|
|
38
|
-
insertOne = (entity, payload, opts) => post(this.getBasePath(entity), payload, this.buildOptions(opts));
|
|
39
|
-
insertMany = (entity, payload, opts) => post(`${this.getBasePath(entity)}${CRUD_ROUTES.insertMany.path}`, payload, this.buildOptions(opts));
|
|
44
|
+
insertOne = async (entity, payload, opts) => dataOf(entity, await post(this.getBasePath(entity), payload, this.buildOptions(opts)), CRUD_ROUTES.insertOne.answers);
|
|
45
|
+
insertMany = async (entity, payload, opts) => dataOf(entity, await post(`${this.getBasePath(entity)}${CRUD_ROUTES.insertMany.path}`, payload, this.buildOptions(opts)), CRUD_ROUTES.insertMany.answers);
|
|
40
46
|
updateOneById = async (entity, id, payload, opts) => patch(`${this.getBasePath(entity)}/${idSegment(entity, id)}`, payload, this.buildOptions(opts));
|
|
41
47
|
updateMany = (entity, q, payload, opts) => patch(`${this.getBasePath(entity)}${stringifyQuery(q)}`, payload, this.buildOptions(opts));
|
|
42
|
-
saveOne = (entity, payload, opts) => put(this.getBasePath(entity), payload, this.buildOptions(opts));
|
|
43
|
-
saveMany = (entity, payload, opts) => put(`${this.getBasePath(entity)}${CRUD_ROUTES.saveMany.path}`, payload, this.buildOptions(opts));
|
|
48
|
+
saveOne = async (entity, payload, opts) => dataOf(entity, await put(this.getBasePath(entity), payload, this.buildOptions(opts)), CRUD_ROUTES.saveOne.answers);
|
|
49
|
+
saveMany = async (entity, payload, opts) => dataOf(entity, await put(`${this.getBasePath(entity)}${CRUD_ROUTES.saveMany.path}`, payload, this.buildOptions(opts)), CRUD_ROUTES.saveMany.answers);
|
|
44
50
|
deleteOneById = async (entity, id, opts = {}) => {
|
|
45
51
|
const qs = opts.hardDelete ? stringifyQuery({ hardDelete: opts.hardDelete }) : '';
|
|
46
52
|
return remove(`${this.getBasePath(entity)}/${idSegment(entity, id)}${qs}`, this.buildOptions(opts));
|
|
@@ -3,7 +3,7 @@ import { generatesKey, valueFieldOf } from '../entity/metadata/definition.js';
|
|
|
3
3
|
import { COUNT_RESULT_KEY, parseQueryLock, QuerySql, REF_ENTITY, REF_KEY, ColumnRef, renderSql, RAW_ALIAS, } from '../type/index.js';
|
|
4
4
|
import { utcTimestamp } from '../util/date.js';
|
|
5
5
|
import { ancestorPaths, isExactDecimal, isInlinedExpression, jsonPathKeys, lacksValueOnInsert, } from '../util/field.util.js';
|
|
6
|
-
import { RETURNED_ID, isSelectList, assertNonNegativeInteger, assertNoUndefined, assertWhere, definedEntries, escapeSqlId, fillOnFields, hasTriggers, foundRowAssigned, filterFieldKeys, getInsertFieldKeys, getKeys, getRelationRequestSummary, deletionOf, hasKeys, idOnlyQuery, keySet, whereAnd, columnFamily, fieldFamily, countedRelations, fieldUpdateOf, fulltextIndexOver, fulltextWeights, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, foldJson, writesKeys,
|
|
6
|
+
import { RETURNED_ID, isSelectList, assertNonNegativeInteger, assertNoUndefined, assertWhere, definedEntries, escapeSqlId, fillOnFields, hasTriggers, foundRowAssigned, filterFieldKeys, getInsertFieldKeys, getKeys, getRelationRequestSummary, deletionOf, hasKeys, idOnlyQuery, keySet, whereAnd, columnFamily, fieldFamily, countedRelations, fieldUpdateOf, fulltextIndexOver, fulltextWeights, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, foldJson, writesKeys, isPlainObject, isOperatorKey, isVectorSearch, normalizeScalarFieldSelection, parentJoins, rankedTextSearch, targetJoins, textSearchFields, textSortOf, textWeightSteps, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, populatesRelations, aggregateOf, sql, columnRef, refOwner, throwUnknownAggregateColumn, } from '../util/index.js';
|
|
7
7
|
import { cachedIn, isJsonScalar } from '../util/object.util.js';
|
|
8
8
|
import { escapeAnsiSqlLiteral } from '../util/sqlLiteral.js';
|
|
9
9
|
import { kindOf, UqlUsageError } from '../util/uqlError.js';
|
|
@@ -823,7 +823,7 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
823
823
|
read: (mode) => this.jsonElemValue(slot, alias, mode),
|
|
824
824
|
};
|
|
825
825
|
const conditions = values.map((value) => {
|
|
826
|
-
const holds =
|
|
826
|
+
const holds = isPlainObject(value) || Array.isArray(value)
|
|
827
827
|
? this.jsonHolds(ctx, element, value, scope)
|
|
828
828
|
: [this.jsonElemEquals(ctx, slot, alias, value)];
|
|
829
829
|
return jsonElemExists(from, holds, this.jsonElemHint);
|
|
@@ -2241,7 +2241,7 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
2241
2241
|
}
|
|
2242
2242
|
/** `<distance> <op> ?`, the `$where` half of a vector search, its bounds checked here since `/http` input is untyped. */
|
|
2243
2243
|
vectorNearCondition(ctx, meta, key, near, prefix) {
|
|
2244
|
-
if (!
|
|
2244
|
+
if (!isPlainObject(near)) {
|
|
2245
2245
|
throw new UqlUsageError(`$near on '${key}' expects an object of its $vector and bounds, got ${kindOf(near)}`);
|
|
2246
2246
|
}
|
|
2247
2247
|
const bounds = Object.entries(near).filter(([op, val]) => !VECTOR_QUERY_KEY_SET.has(op) && val !== undefined);
|
package/dist/dialect/jsonSql.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { QuerySql } from '../type/index.js';
|
|
2
|
-
import { isOperatorMap } from '../util/dialect.util.js';
|
|
3
2
|
import { columnFamily, declaresNotNull, jsonPathKeys } from '../util/field.util.js';
|
|
4
|
-
import { isOperatorKey, someKey } from '../util/object.util.js';
|
|
3
|
+
import { isOperatorKey, isPlainObject, someKey } from '../util/object.util.js';
|
|
5
4
|
import { JSON_SKIP_KEY } from './aliases.js';
|
|
6
5
|
/**
|
|
7
6
|
* A `'$.a.b'` JSON path literal, and `'$'` for an empty path, the document itself. `suffix` appends an
|
|
@@ -108,5 +107,5 @@ export function holdsOperator(value) {
|
|
|
108
107
|
if (Array.isArray(value)) {
|
|
109
108
|
return value.some(holdsOperator);
|
|
110
109
|
}
|
|
111
|
-
return
|
|
110
|
+
return isPlainObject(value) && someKey(value, (key) => isOperatorKey(key) || holdsOperator(value[key]));
|
|
112
111
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ColumnRef, QuerySql, REF_ENTITY, REF_KEY, VECTOR_QUERY_KEYS, } from '../type/index.js';
|
|
2
|
-
import { isOperatorMap } from '../util/dialect.util.js';
|
|
3
2
|
import { aggregateOf, isInlinedExpression } from '../util/field.util.js';
|
|
4
|
-
import { getKeys, hasKeys, isOperatorKey,
|
|
3
|
+
import { getKeys, hasKeys, isOperatorKey, isPlainObject, isRecord, someKey } from '../util/object.util.js';
|
|
5
4
|
import { escapeRegExp } from '../util/string.util.js';
|
|
6
5
|
import { kindOf, UqlUsageError } from '../util/uqlError.js';
|
|
7
6
|
/**
|
|
@@ -37,7 +36,7 @@ export function namesRows(where) {
|
|
|
37
36
|
return (where !== undefined &&
|
|
38
37
|
someKey(where, (key) => isGroupOp(key)
|
|
39
38
|
? groupClauses(key, where[key]).some((clause) => clause instanceof QuerySql || namesRows(clause))
|
|
40
|
-
: !(
|
|
39
|
+
: !(isPlainObject(where[key]) && !hasKeys(where[key]))));
|
|
41
40
|
}
|
|
42
41
|
/**
|
|
43
42
|
* The fields of `meta` a `$where` reads, through every group: each key, a JSON path by its column, and each ref to
|
|
@@ -78,7 +77,7 @@ export function fieldsRead(meta, where) {
|
|
|
78
77
|
* anything else `$eq`. Each key is checked here, once, refused as `refusal` where it is no operator.
|
|
79
78
|
*/
|
|
80
79
|
export function whereOperators(val, refusal) {
|
|
81
|
-
const ops = Array.isArray(val) ? { $in: val } :
|
|
80
|
+
const ops = Array.isArray(val) ? { $in: val } : isPlainObject(val) ? val : { $eq: val };
|
|
82
81
|
return Object.entries(ops).map(([op, value]) => {
|
|
83
82
|
if (!isFieldOp(op)) {
|
|
84
83
|
throw new UqlUsageError(`${refusal}: ${op}`);
|
|
@@ -164,7 +163,7 @@ export function sizeComparisons(size) {
|
|
|
164
163
|
if (typeof size === 'number') {
|
|
165
164
|
return [['$eq', size]];
|
|
166
165
|
}
|
|
167
|
-
if (!
|
|
166
|
+
if (!isPlainObject(size)) {
|
|
168
167
|
throw new UqlUsageError(`$size expects a number or bounds, got ${kindOf(size)}`);
|
|
169
168
|
}
|
|
170
169
|
const comparisons = Object.entries(size).filter(([, bound]) => bound !== undefined);
|
|
@@ -94,19 +94,10 @@ type KeyIsRequired<O, N> = N extends keyof O ? {} extends Pick<O, N> ? {
|
|
|
94
94
|
type VersionIsBranded<This> = [VersionKey<This>] extends [never] ? {
|
|
95
95
|
readonly __versionNeedsVersionKeyBrand: true;
|
|
96
96
|
} : unknown;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
* `O` is inferred from the class the decorator sits on, which types `references`' own side.
|
|
102
|
-
*/
|
|
103
|
-
type WithEntity<E, O> = O & {
|
|
104
|
-
readonly entity: EntityGetter<E>;
|
|
105
|
-
};
|
|
106
|
-
export declare function OneToOne<E extends object, O>(opts: WithEntity<E, RelationOneToOneOptions<E, O>>): MemberDecorator<E | undefined, O>;
|
|
107
|
-
export declare function ManyToOne<E extends object, O>(opts: WithEntity<E, RelationManyToOneOptions<E, O>>): MemberDecorator<E | undefined, O>;
|
|
108
|
-
export declare function OneToMany<E extends object, O>(opts: WithEntity<E, RelationOneToManyOptions<E, O>>): MemberDecorator<readonly E[] | undefined, O>;
|
|
109
|
-
export declare function ManyToMany<E extends object, O>(opts: WithEntity<E, RelationManyToManyOptions<E, O>>): MemberDecorator<readonly E[] | undefined, O>;
|
|
97
|
+
export declare function OneToOne<E extends object, O>(entity: EntityGetter<E>, opts: RelationOneToOneOptions<E, O>): MemberDecorator<E | undefined, O>;
|
|
98
|
+
export declare function ManyToOne<E extends object, O>(entity: EntityGetter<E>, opts?: RelationManyToOneOptions<E, O>): MemberDecorator<E | undefined, O>;
|
|
99
|
+
export declare function OneToMany<E extends object, O>(entity: EntityGetter<E>, opts?: RelationOneToManyOptions<E, O>): MemberDecorator<readonly E[] | undefined, O>;
|
|
100
|
+
export declare function ManyToMany<E extends object, O>(entity: EntityGetter<E>, opts: RelationManyToManyOptions<E, O>): MemberDecorator<readonly E[] | undefined, O>;
|
|
110
101
|
/** What a hook method is called with: the row as `this`, and the querier's context. */
|
|
111
102
|
type HookMethod<This> = (this: This, ctx: HookContext) => unknown;
|
|
112
103
|
export declare const BeforeInsert: () => <This>(_value: HookMethod<This>, context: ClassMethodDecoratorContext<This>) => void;
|
|
@@ -20,22 +20,31 @@ export function Computed(expression) {
|
|
|
20
20
|
computed instanceof RelationAggregate ? aggregateField(computed) : computed[COLUMN_OPTIONS];
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* `E` comes from the target getter, so the context can insist the property really holds that entity:
|
|
25
|
+
* `@ManyToOne(() => Other)` on a `Company` field stops compiling, and a to-many cardinality on a non-array
|
|
26
|
+
* property does too. `O` is inferred from the class the decorator sits on, which types `references`' own side.
|
|
27
|
+
*/
|
|
28
|
+
function relation(cardinality, entity, opts) {
|
|
24
29
|
return (_value, context) => {
|
|
25
|
-
memberRegistrations(context.metadata).relations[String(context.name)] = relationRegistration(
|
|
30
|
+
memberRegistrations(context.metadata).relations[String(context.name)] = relationRegistration({
|
|
31
|
+
cardinality,
|
|
32
|
+
entity,
|
|
33
|
+
...opts,
|
|
34
|
+
});
|
|
26
35
|
};
|
|
27
36
|
}
|
|
28
|
-
export function OneToOne(opts) {
|
|
29
|
-
return relation(
|
|
37
|
+
export function OneToOne(entity, opts) {
|
|
38
|
+
return relation('11', entity, opts);
|
|
30
39
|
}
|
|
31
|
-
export function ManyToOne(opts) {
|
|
32
|
-
return relation(
|
|
40
|
+
export function ManyToOne(entity, opts) {
|
|
41
|
+
return relation('m1', entity, opts);
|
|
33
42
|
}
|
|
34
|
-
export function OneToMany(opts) {
|
|
35
|
-
return relation(
|
|
43
|
+
export function OneToMany(entity, opts) {
|
|
44
|
+
return relation('1m', entity, opts);
|
|
36
45
|
}
|
|
37
|
-
export function ManyToMany(opts) {
|
|
38
|
-
return relation(
|
|
46
|
+
export function ManyToMany(entity, opts) {
|
|
47
|
+
return relation('mm', entity, opts);
|
|
39
48
|
}
|
|
40
49
|
function hook(event) {
|
|
41
50
|
return () =>
|
|
@@ -140,5 +140,6 @@ export declare const Author: import("../../../index.js").RelatedEntity<object &
|
|
|
140
140
|
} & {
|
|
141
141
|
bio?: string | null | undefined;
|
|
142
142
|
}, false>;
|
|
143
|
-
export
|
|
143
|
+
export interface Author extends InstanceType<typeof Author> {
|
|
144
|
+
}
|
|
144
145
|
export declare const label: (author: Author) => string;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
export declare const Folder: import("../../../index.js").RelatedEntity<object & {
|
|
2
|
+
id: number;
|
|
3
|
+
name: string;
|
|
4
|
+
size: number;
|
|
5
|
+
} & {
|
|
6
|
+
parentId?: number | null | undefined;
|
|
7
|
+
} & import("../../../index.js").Brands<{
|
|
8
|
+
readonly id: import("../../../index.js").NumericColumn<{
|
|
9
|
+
readonly type: NumberConstructor;
|
|
10
|
+
} & {
|
|
11
|
+
readonly isId: true;
|
|
12
|
+
readonly autoIncrement: true;
|
|
13
|
+
}, number>;
|
|
14
|
+
readonly name: import("../../../index.js").StringColumn<{
|
|
15
|
+
readonly type: StringConstructor;
|
|
16
|
+
}, string>;
|
|
17
|
+
readonly size: import("../../../index.js").NumericColumn<{
|
|
18
|
+
readonly type: NumberConstructor;
|
|
19
|
+
}, number>;
|
|
20
|
+
readonly parentId: import("../../../index.js").NumericColumn<{
|
|
21
|
+
readonly type: NumberConstructor;
|
|
22
|
+
} & {
|
|
23
|
+
readonly nullable: true;
|
|
24
|
+
}, number>;
|
|
25
|
+
}, object> & import("../../../index.js").RelationRow<{
|
|
26
|
+
readonly parent: () => import("../../../index.js").DefinedRelation<object & {
|
|
27
|
+
id: number;
|
|
28
|
+
name: string;
|
|
29
|
+
size: number;
|
|
30
|
+
} & {
|
|
31
|
+
parentId?: number | null | undefined;
|
|
32
|
+
} & import("../../../index.js").Brands<{
|
|
33
|
+
readonly id: import("../../../index.js").NumericColumn<{
|
|
34
|
+
readonly type: NumberConstructor;
|
|
35
|
+
} & {
|
|
36
|
+
readonly isId: true;
|
|
37
|
+
readonly autoIncrement: true;
|
|
38
|
+
}, number>;
|
|
39
|
+
readonly name: import("../../../index.js").StringColumn<{
|
|
40
|
+
readonly type: StringConstructor;
|
|
41
|
+
}, string>;
|
|
42
|
+
readonly size: import("../../../index.js").NumericColumn<{
|
|
43
|
+
readonly type: NumberConstructor;
|
|
44
|
+
}, number>;
|
|
45
|
+
readonly parentId: import("../../../index.js").NumericColumn<{
|
|
46
|
+
readonly type: NumberConstructor;
|
|
47
|
+
} & {
|
|
48
|
+
readonly nullable: true;
|
|
49
|
+
}, number>;
|
|
50
|
+
}, object> & import("../../../index.js").RelationRow</*elided*/ any> & import("../../../index.js").AggregateRow<{
|
|
51
|
+
readonly childCount: () => import("../../../index.js").RelationAggregate<number>;
|
|
52
|
+
readonly childSize: () => import("../../../index.js").RelationAggregate<number>;
|
|
53
|
+
}>, "m1", object & {
|
|
54
|
+
id: number;
|
|
55
|
+
name: string;
|
|
56
|
+
size: number;
|
|
57
|
+
} & {
|
|
58
|
+
parentId?: number | null | undefined;
|
|
59
|
+
} & import("../../../index.js").Brands<{
|
|
60
|
+
readonly id: import("../../../index.js").NumericColumn<{
|
|
61
|
+
readonly type: NumberConstructor;
|
|
62
|
+
} & {
|
|
63
|
+
readonly isId: true;
|
|
64
|
+
readonly autoIncrement: true;
|
|
65
|
+
}, number>;
|
|
66
|
+
readonly name: import("../../../index.js").StringColumn<{
|
|
67
|
+
readonly type: StringConstructor;
|
|
68
|
+
}, string>;
|
|
69
|
+
readonly size: import("../../../index.js").NumericColumn<{
|
|
70
|
+
readonly type: NumberConstructor;
|
|
71
|
+
}, number>;
|
|
72
|
+
readonly parentId: import("../../../index.js").NumericColumn<{
|
|
73
|
+
readonly type: NumberConstructor;
|
|
74
|
+
} & {
|
|
75
|
+
readonly nullable: true;
|
|
76
|
+
}, number>;
|
|
77
|
+
}, object> & import("../../../index.js").RelationRow</*elided*/ any> & import("../../../index.js").AggregateRow<{
|
|
78
|
+
readonly childCount: () => import("../../../index.js").RelationAggregate<number>;
|
|
79
|
+
readonly childSize: () => import("../../../index.js").RelationAggregate<number>;
|
|
80
|
+
}>>;
|
|
81
|
+
readonly children: () => import("../../../index.js").DefinedRelation<object & {
|
|
82
|
+
id: number;
|
|
83
|
+
name: string;
|
|
84
|
+
size: number;
|
|
85
|
+
} & {
|
|
86
|
+
parentId?: number | null | undefined;
|
|
87
|
+
} & import("../../../index.js").Brands<{
|
|
88
|
+
readonly id: import("../../../index.js").NumericColumn<{
|
|
89
|
+
readonly type: NumberConstructor;
|
|
90
|
+
} & {
|
|
91
|
+
readonly isId: true;
|
|
92
|
+
readonly autoIncrement: true;
|
|
93
|
+
}, number>;
|
|
94
|
+
readonly name: import("../../../index.js").StringColumn<{
|
|
95
|
+
readonly type: StringConstructor;
|
|
96
|
+
}, string>;
|
|
97
|
+
readonly size: import("../../../index.js").NumericColumn<{
|
|
98
|
+
readonly type: NumberConstructor;
|
|
99
|
+
}, number>;
|
|
100
|
+
readonly parentId: import("../../../index.js").NumericColumn<{
|
|
101
|
+
readonly type: NumberConstructor;
|
|
102
|
+
} & {
|
|
103
|
+
readonly nullable: true;
|
|
104
|
+
}, number>;
|
|
105
|
+
}, object> & import("../../../index.js").RelationRow</*elided*/ any> & import("../../../index.js").AggregateRow<{
|
|
106
|
+
readonly childCount: () => import("../../../index.js").RelationAggregate<number>;
|
|
107
|
+
readonly childSize: () => import("../../../index.js").RelationAggregate<number>;
|
|
108
|
+
}>, "1m", {
|
|
109
|
+
id: number;
|
|
110
|
+
name: string;
|
|
111
|
+
size: number;
|
|
112
|
+
} & {
|
|
113
|
+
parentId?: number | null | undefined;
|
|
114
|
+
}>;
|
|
115
|
+
}> & import("../../../index.js").AggregateRow<{
|
|
116
|
+
readonly childCount: () => import("../../../index.js").RelationAggregate<number>;
|
|
117
|
+
readonly childSize: () => import("../../../index.js").RelationAggregate<number>;
|
|
118
|
+
}>, number, {
|
|
119
|
+
id: number;
|
|
120
|
+
name: string;
|
|
121
|
+
size: number;
|
|
122
|
+
} & {
|
|
123
|
+
parentId?: number | null | undefined;
|
|
124
|
+
}, false>;
|
|
125
|
+
export interface Folder extends InstanceType<typeof Folder> {
|
|
126
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { f } from '../../column.js';
|
|
2
|
+
import { defineEntity } from '../definition.js';
|
|
3
|
+
export const Folder = defineEntity({
|
|
4
|
+
name: 'Folder',
|
|
5
|
+
fields: { id: f.number().autoIncrement(), name: f.string(), size: f.number(), parentId: f.number().nullable() },
|
|
6
|
+
}).relations((rel) => ({
|
|
7
|
+
parent: () => rel.manyToOne(Folder, { references: (folder) => folder.parentId }),
|
|
8
|
+
children: () => rel.oneToMany(Folder, { mappedBy: (folder) => folder.parent }),
|
|
9
|
+
}), (folder) => ({
|
|
10
|
+
childCount: () => folder.children.count(),
|
|
11
|
+
childSize: () => folder.children.sum((child) => child.size),
|
|
12
|
+
}));
|
|
@@ -69,8 +69,8 @@ export function keyOf(pick) {
|
|
|
69
69
|
const KEY_MAP = new Proxy({}, { get: (_, key) => key });
|
|
70
70
|
function addRelation(meta, key, registration) {
|
|
71
71
|
const { entity } = meta;
|
|
72
|
-
if (
|
|
73
|
-
throw new UqlUsageError(`'${entity.name}.${key}' needs
|
|
72
|
+
if (typeof registration.entity !== 'function') {
|
|
73
|
+
throw new UqlUsageError(`'${entity.name}.${key}' needs its target first, e.g. '@ManyToOne(() => Company)'.`);
|
|
74
74
|
}
|
|
75
75
|
if (registration.through && registration.references) {
|
|
76
76
|
throw new UqlUsageError(`'${entity.name}.${key}' joins through a junction, whose column referencing each side is the join; ` +
|
|
@@ -246,7 +246,7 @@ function fieldsOf(name, values) {
|
|
|
246
246
|
if (field instanceof RelationAggregate) {
|
|
247
247
|
return [key, aggregateField(field)];
|
|
248
248
|
}
|
|
249
|
-
// The types refuse anything else
|
|
249
|
+
// The types refuse anything else, but for an aggregate thunk, whose constraint cannot read what it returns.
|
|
250
250
|
if (!isColumn(field)) {
|
|
251
251
|
throw new UqlUsageError(`'${name}.${key}' is neither a column nor an aggregate: declare it as one`);
|
|
252
252
|
}
|
package/dist/http/contract.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { AbstractType } from '../type/index.js';
|
|
2
|
+
/** What a route answers in `data`, which says how its values cross JSON: rows, a page of them, keys, or a count. */
|
|
3
|
+
export type WireAnswer = 'rows' | 'page' | 'ids' | 'count';
|
|
2
4
|
/**
|
|
3
5
|
* Single source of truth for the CRUD-over-HTTP surface, shared by server adapters and the browser client.
|
|
4
6
|
* Keys are constrained to {@link UniversalQuerier} method names, so renaming a querier method
|
|
@@ -8,54 +10,67 @@ export declare const CRUD_ROUTES: {
|
|
|
8
10
|
readonly findMany: {
|
|
9
11
|
readonly method: 'GET';
|
|
10
12
|
readonly path: '';
|
|
13
|
+
readonly answers: 'rows';
|
|
11
14
|
};
|
|
12
15
|
readonly findManyPage: {
|
|
13
16
|
readonly method: 'GET';
|
|
14
17
|
readonly path: '/page';
|
|
18
|
+
readonly answers: 'page';
|
|
15
19
|
};
|
|
16
20
|
readonly findOne: {
|
|
17
21
|
readonly method: 'GET';
|
|
18
22
|
readonly path: '/one';
|
|
23
|
+
readonly answers: 'rows';
|
|
19
24
|
};
|
|
20
25
|
readonly count: {
|
|
21
26
|
readonly method: 'GET';
|
|
22
27
|
readonly path: '/count';
|
|
28
|
+
readonly answers: 'count';
|
|
23
29
|
};
|
|
24
30
|
readonly findOneById: {
|
|
25
31
|
readonly method: 'GET';
|
|
26
32
|
readonly path: '/:id';
|
|
33
|
+
readonly answers: 'rows';
|
|
27
34
|
};
|
|
28
35
|
readonly insertOne: {
|
|
29
36
|
readonly method: 'POST';
|
|
30
37
|
readonly path: '';
|
|
38
|
+
readonly answers: 'ids';
|
|
31
39
|
};
|
|
32
40
|
readonly insertMany: {
|
|
33
41
|
readonly method: 'POST';
|
|
34
42
|
readonly path: '/many';
|
|
43
|
+
readonly answers: 'ids';
|
|
35
44
|
};
|
|
36
45
|
readonly saveOne: {
|
|
37
46
|
readonly method: 'PUT';
|
|
38
47
|
readonly path: '';
|
|
48
|
+
readonly answers: 'ids';
|
|
39
49
|
};
|
|
40
50
|
readonly saveMany: {
|
|
41
51
|
readonly method: 'PUT';
|
|
42
52
|
readonly path: '/many';
|
|
53
|
+
readonly answers: 'ids';
|
|
43
54
|
};
|
|
44
55
|
readonly updateMany: {
|
|
45
56
|
readonly method: 'PATCH';
|
|
46
57
|
readonly path: '';
|
|
58
|
+
readonly answers: 'count';
|
|
47
59
|
};
|
|
48
60
|
readonly updateOneById: {
|
|
49
61
|
readonly method: 'PATCH';
|
|
50
62
|
readonly path: '/:id';
|
|
63
|
+
readonly answers: 'count';
|
|
51
64
|
};
|
|
52
65
|
readonly deleteOneById: {
|
|
53
66
|
readonly method: 'DELETE';
|
|
54
67
|
readonly path: '/:id';
|
|
68
|
+
readonly answers: 'count';
|
|
55
69
|
};
|
|
56
70
|
readonly deleteMany: {
|
|
57
71
|
readonly method: 'DELETE';
|
|
58
72
|
readonly path: '';
|
|
73
|
+
readonly answers: 'count';
|
|
59
74
|
};
|
|
60
75
|
};
|
|
61
76
|
export type CrudOperation = keyof typeof CRUD_ROUTES;
|
package/dist/http/contract.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { queryErrorKind } from '../querier/queryError.js';
|
|
2
|
-
// the specific util modules, not the barrel,
|
|
2
|
+
// the specific util modules, not the barrel, which reaches every dialect helper and would land in the browser bundle
|
|
3
3
|
import { getKeys } from '../util/object.util.js';
|
|
4
4
|
import { kebabCase } from '../util/string.util.js';
|
|
5
5
|
/**
|
|
@@ -8,19 +8,19 @@ import { kebabCase } from '../util/string.util.js';
|
|
|
8
8
|
* (or routing a non-existent one) is a compile error.
|
|
9
9
|
*/
|
|
10
10
|
export const CRUD_ROUTES = {
|
|
11
|
-
findMany: { method: 'GET', path: '' },
|
|
12
|
-
findManyPage: { method: 'GET', path: '/page' },
|
|
13
|
-
findOne: { method: 'GET', path: '/one' },
|
|
14
|
-
count: { method: 'GET', path: '/count' },
|
|
15
|
-
findOneById: { method: 'GET', path: '/:id' },
|
|
16
|
-
insertOne: { method: 'POST', path: '' },
|
|
17
|
-
insertMany: { method: 'POST', path: '/many' },
|
|
18
|
-
saveOne: { method: 'PUT', path: '' },
|
|
19
|
-
saveMany: { method: 'PUT', path: '/many' },
|
|
20
|
-
updateMany: { method: 'PATCH', path: '' },
|
|
21
|
-
updateOneById: { method: 'PATCH', path: '/:id' },
|
|
22
|
-
deleteOneById: { method: 'DELETE', path: '/:id' },
|
|
23
|
-
deleteMany: { method: 'DELETE', path: '' },
|
|
11
|
+
findMany: { method: 'GET', path: '', answers: 'rows' },
|
|
12
|
+
findManyPage: { method: 'GET', path: '/page', answers: 'page' },
|
|
13
|
+
findOne: { method: 'GET', path: '/one', answers: 'rows' },
|
|
14
|
+
count: { method: 'GET', path: '/count', answers: 'count' },
|
|
15
|
+
findOneById: { method: 'GET', path: '/:id', answers: 'rows' },
|
|
16
|
+
insertOne: { method: 'POST', path: '', answers: 'ids' },
|
|
17
|
+
insertMany: { method: 'POST', path: '/many', answers: 'ids' },
|
|
18
|
+
saveOne: { method: 'PUT', path: '', answers: 'ids' },
|
|
19
|
+
saveMany: { method: 'PUT', path: '/many', answers: 'ids' },
|
|
20
|
+
updateMany: { method: 'PATCH', path: '', answers: 'count' },
|
|
21
|
+
updateOneById: { method: 'PATCH', path: '/:id', answers: 'count' },
|
|
22
|
+
deleteOneById: { method: 'DELETE', path: '/:id', answers: 'count' },
|
|
23
|
+
deleteMany: { method: 'DELETE', path: '', answers: 'count' },
|
|
24
24
|
};
|
|
25
25
|
const CRUD_OPS = getKeys(CRUD_ROUTES);
|
|
26
26
|
// derived from CRUD_ROUTES (the literal-path GET routes) so the sub-paths live in exactly one place
|
package/dist/http/handler.js
CHANGED
|
@@ -5,6 +5,7 @@ import { whereWith } from '../util/query.util.js';
|
|
|
5
5
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
6
6
|
import { CRUD_ROUTES, entityPath, matchRoute, } from './contract.js';
|
|
7
7
|
import { parseQueryParams } from './query.js';
|
|
8
|
+
import { readWireData, readWireQuery, writeWireData } from './wireRow.js';
|
|
8
9
|
/** `Company (crm.Company)`: the class, and the table it maps, which is what tells two apart. */
|
|
9
10
|
function tableOf(entity) {
|
|
10
11
|
const meta = getMeta(entity);
|
|
@@ -42,7 +43,7 @@ export function createRequestHandler(opts) {
|
|
|
42
43
|
async function run(entity, { op, method, id }, req) {
|
|
43
44
|
const meta = getMeta(entity);
|
|
44
45
|
// QUERY (RFC 10008) carries the JSON query in the body instead of the query string
|
|
45
|
-
const query = parseQueryParams(method === 'QUERY' ? req.body : req.query);
|
|
46
|
+
const query = readWireQuery(meta, parseQueryParams(method === 'QUERY' ? req.body : req.query));
|
|
46
47
|
const { method: verb } = CRUD_ROUTES[op];
|
|
47
48
|
// What the client sent, before the hooks: a relation a hook adds is the server's own to add.
|
|
48
49
|
assertServed(meta, query, served);
|
|
@@ -58,11 +59,13 @@ export function createRequestHandler(opts) {
|
|
|
58
59
|
// A read acquires and releases; a write is one transaction, so a failing cascade takes its parent with it.
|
|
59
60
|
const envelope = await (verb === 'GET' ? resolved.withQuerier(execute) : resolved.transaction(execute));
|
|
60
61
|
await post?.(hookCtx, envelope);
|
|
61
|
-
return { status: 200, body: envelope };
|
|
62
|
+
return { status: 200, body: { ...envelope, data: writeWireData(meta, envelope.data, CRUD_ROUTES[op].answers) } };
|
|
62
63
|
});
|
|
63
64
|
/** Runs `op`, reading the query and body after the hooks, which may have reshaped or reassigned them. */
|
|
64
65
|
async function execute(querier) {
|
|
65
|
-
const { query
|
|
66
|
+
const { query } = hookCtx;
|
|
67
|
+
// A write's rows as the entity holds them: JSON carried each `Date` and `bigint` as text.
|
|
68
|
+
const body = readWireData(meta, hookCtx.body, 'rows');
|
|
66
69
|
const { hardDelete = false, count: counts } = query;
|
|
67
70
|
const scoped = id === undefined
|
|
68
71
|
? query
|
package/dist/http/query.d.ts
CHANGED
|
@@ -18,11 +18,9 @@ export declare function parseQueryParams<E = unknown>(params?: unknown): WireQue
|
|
|
18
18
|
*/
|
|
19
19
|
export declare function stringifyQuery(query?: Record<string, unknown>): string;
|
|
20
20
|
/**
|
|
21
|
-
* What leaves the browser, as JSON, refusing what JSON keeps nothing of rather than letting the server
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* serializes to ISO 8601, which is what a date column reads. This is what a cast, or a JavaScript caller,
|
|
25
|
-
* hits where the client's types already refuse a fragment.
|
|
21
|
+
* What leaves the browser, as JSON, refusing what JSON keeps nothing of rather than letting the server build a
|
|
22
|
+
* statement around the remains: a `sql` fragment, which arrives as `{}`, and binary, keyed by index. A `bigint` goes
|
|
23
|
+
* as its digits and a `Date` as its ISO text, which the server reads back by the entity.
|
|
26
24
|
*/
|
|
27
25
|
export declare function wireJson(value: unknown): string;
|
|
28
26
|
export {};
|
package/dist/http/query.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// the clause table itself, not the barrel: this module is in the browser bundle's graph
|
|
2
2
|
import { QUERY_CLAUSES } from '../type/query.js';
|
|
3
|
-
// the brand
|
|
4
|
-
// with it, in the browser bundle, which is on a size budget
|
|
3
|
+
// the brand, which a `sql` fragment and a column ref both carry
|
|
5
4
|
import { SQL_FN } from '../type/querySql.js';
|
|
6
|
-
// the specific util module, not the barrel,
|
|
7
|
-
import { getKeys, isRecord,
|
|
5
|
+
// the specific util module, not the barrel, which reaches every dialect helper
|
|
6
|
+
import { getKeys, isRecord, isPlainObject } from '../util/object.util.js';
|
|
8
7
|
// the error class alone, from its own leaf module: `queryError.ts` carries every driver's code map
|
|
9
8
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
10
9
|
/** The flags a request carries beside its query: `hardDelete` on a delete, `count` on a `findMany`. */
|
|
@@ -43,7 +42,7 @@ export function parseQueryParams(params = {}) {
|
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
44
|
query['$where'] ??= {};
|
|
46
|
-
if (!
|
|
45
|
+
if (!isPlainObject(query['$where'])) {
|
|
47
46
|
throw new UqlUsageError("'$where' must be a JSON object");
|
|
48
47
|
}
|
|
49
48
|
return query;
|
|
@@ -96,14 +95,15 @@ export function stringifyQuery(query) {
|
|
|
96
95
|
return qs ? `?${qs}` : '';
|
|
97
96
|
}
|
|
98
97
|
/**
|
|
99
|
-
* What leaves the browser, as JSON, refusing what JSON keeps nothing of rather than letting the server
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* serializes to ISO 8601, which is what a date column reads. This is what a cast, or a JavaScript caller,
|
|
103
|
-
* hits where the client's types already refuse a fragment.
|
|
98
|
+
* What leaves the browser, as JSON, refusing what JSON keeps nothing of rather than letting the server build a
|
|
99
|
+
* statement around the remains: a `sql` fragment, which arrives as `{}`, and binary, keyed by index. A `bigint` goes
|
|
100
|
+
* as its digits and a `Date` as its ISO text, which the server reads back by the entity.
|
|
104
101
|
*/
|
|
105
102
|
export function wireJson(value) {
|
|
106
103
|
return JSON.stringify(value, (_key, held) => {
|
|
104
|
+
if (typeof held === 'bigint') {
|
|
105
|
+
return String(held);
|
|
106
|
+
}
|
|
107
107
|
if (typeof held !== 'object' || held === null) {
|
|
108
108
|
return held;
|
|
109
109
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { EntityMeta } from '../type/index.js';
|
|
2
|
+
import type { WireAnswer } from './contract.js';
|
|
3
|
+
/** `data` as JSON is to carry it, by what its route `answers`; the client reads it back by {@link readWireData}. */
|
|
4
|
+
export declare function writeWireData<E>(meta: EntityMeta<E>, data: unknown, answers: WireAnswer): unknown;
|
|
5
|
+
/** `data` JSON carried, a response or a write's body, read back as the entity holds it by what it `answers`. */
|
|
6
|
+
export declare function readWireData<E>(meta: EntityMeta<E>, data: unknown, answers: WireAnswer): unknown;
|
|
7
|
+
/**
|
|
8
|
+
* `query` with each value it compares a field with read back as the entity holds it, its `$populate` and `$count`
|
|
9
|
+
* relations' included: JSON carried each date and `bigint` as text, which an engine would compare as text.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readWireQuery<E, Q extends RelationClauses>(meta: EntityMeta<E>, query: Q): Q;
|
|
12
|
+
/** The clauses of a query that compare or reach a relation's rows. */
|
|
13
|
+
type RelationClauses = {
|
|
14
|
+
readonly $where?: unknown;
|
|
15
|
+
readonly $populate?: object;
|
|
16
|
+
readonly $count?: object;
|
|
17
|
+
};
|
|
18
|
+
export {};
|