uql-orm 0.110.0 → 0.111.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 +3 -3
- package/dist/browser/uql-browser.min.js.map +1 -1
- package/dist/bunSql/bunSqlQuerier.d.ts +2 -5
- package/dist/d1/d1Querier.d.ts +1 -2
- package/dist/d1/d1Querier.js +0 -2
- package/dist/dialect/abstractSqlDialect.js +2 -2
- package/dist/dialect/aliases.d.ts +2 -0
- package/dist/dialect/aliases.js +2 -0
- package/dist/entity/columnOptionsBuilder.d.ts +1 -1
- package/dist/entity/columnOptionsBuilder.js +2 -2
- package/dist/entity/decorator/members.d.ts +15 -6
- package/dist/entity/metadata/definedFixture/animal.d.ts +4 -0
- package/dist/entity/metadata/definedFixture/animal.js +1 -1
- package/dist/entity/metadata/definedFixture/author.d.ts +4 -0
- package/dist/entity/metadata/definedFixture/author.js +1 -1
- package/dist/entity/metadata/definedFixture/book.d.ts +5 -0
- package/dist/entity/metadata/definedFixture/book.js +1 -1
- package/dist/entity/metadata/definition.d.ts +0 -2
- package/dist/entity/metadata/definition.js +3 -8
- package/dist/mariadb/mariadbQuerier.d.ts +2 -4
- package/dist/migrate/builder/columnBuilder.js +2 -2
- package/dist/migrate/codegen/entityCodeGenerator.js +1 -1
- package/dist/migrate/codegen/fieldOptionsSource.js +2 -13
- package/dist/migrate/schemaGenerator.d.ts +2 -2
- package/dist/migrate/schemaGenerator.js +9 -5
- package/dist/mongodb/mongodbQuerier.d.ts +1 -2
- package/dist/mongodb/mongodbQuerier.js +0 -2
- package/dist/mssql/mssqlQuerier.d.ts +2 -4
- package/dist/mysql/mysql2Querier.d.ts +2 -10
- package/dist/pglite/pgliteQuerier.d.ts +4 -7
- package/dist/pglite/pgliteQuerier.js +0 -2
- package/dist/postgres/pgQuerier.d.ts +2 -1
- package/dist/querier/abstractPoolQuerier.d.ts +1 -2
- package/dist/querier/abstractPoolQuerier.js +0 -2
- package/dist/querier/abstractQuerier.d.ts +5 -8
- package/dist/querier/abstractQuerier.js +8 -11
- package/dist/querier/abstractSqlQuerier.d.ts +9 -2
- package/dist/querier/abstractSqlQuerier.js +36 -4
- package/dist/schema/canonicalType.d.ts +3 -3
- package/dist/schema/canonicalType.js +9 -4
- package/dist/schema/declaredColumn.d.ts +2 -4
- package/dist/schema/declaredColumn.js +4 -5
- package/dist/schema/schemaASTBuilder.js +1 -2
- package/dist/sqlite/hranaQuerier.d.ts +1 -2
- package/dist/sqlite/hranaQuerier.js +0 -2
- package/dist/sqlite/sqliteQuerier.d.ts +3 -9
- package/dist/sqlite/sqliteQuerier.js +0 -2
- package/dist/turso/tursoSessionQuerier.d.ts +1 -2
- package/dist/turso/tursoSessionQuerier.js +0 -2
- package/dist/type/column.d.ts +4 -6
- package/dist/type/definedEntity.d.ts +1 -1
- package/dist/type/entity.d.ts +4 -5
- package/dist/type/querier.d.ts +2 -2
- package/dist/type/querierPool.d.ts +1 -1
- package/dist/type/query.d.ts +3 -1
- package/dist/type/universalQuerier.d.ts +2 -2
- package/dist/util/field.util.d.ts +1 -7
- package/dist/util/field.util.js +2 -12
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +5 -5
|
@@ -18,8 +18,8 @@ export declare class SqlSchemaGenerator implements SchemaGenerator {
|
|
|
18
18
|
/** Escape an identifier (table name, column name, etc.) */
|
|
19
19
|
private escapeId;
|
|
20
20
|
/**
|
|
21
|
-
* The SQL type a column is spelled with: its canonical type, plus the engine's generated
|
|
22
|
-
* auto-increment key, so a foreign key taking its type from the key gets the same one.
|
|
21
|
+
* The SQL type a column is spelled with: its canonical type, the key's where it is one, plus the engine's generated
|
|
22
|
+
* suffix on an auto-increment key, so a foreign key taking its type from the key gets the same one.
|
|
23
23
|
*/
|
|
24
24
|
private columnSqlType;
|
|
25
25
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getMeta } from '../entity/index.js';
|
|
2
|
-
import { canonicalToSql, engineType, isVectorCategory } from '../schema/canonicalType.js';
|
|
2
|
+
import { canonicalToSql, engineType, isVectorCategory, suppliedKeySql } from '../schema/canonicalType.js';
|
|
3
3
|
import { indexChanges } from '../schema/indexDifferences.js';
|
|
4
4
|
import { matchByKey } from '../schema/matchByKey.js';
|
|
5
5
|
import { buildSchemaAST } from '../schema/schemaASTBuilder.js';
|
|
@@ -40,12 +40,16 @@ export class SqlSchemaGenerator {
|
|
|
40
40
|
return this.dialect.escapeId(identifier);
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
|
-
* The SQL type a column is spelled with: its canonical type, plus the engine's generated
|
|
44
|
-
* auto-increment key, so a foreign key taking its type from the key gets the same one.
|
|
43
|
+
* The SQL type a column is spelled with: its canonical type, the key's where it is one, plus the engine's generated
|
|
44
|
+
* suffix on an auto-increment key, so a foreign key taking its type from the key gets the same one.
|
|
45
45
|
*/
|
|
46
46
|
columnSqlType(col) {
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (!col.isPrimaryKey) {
|
|
48
|
+
return canonicalToSql(col.type, this.dialect);
|
|
49
|
+
}
|
|
50
|
+
return col.isAutoIncrement
|
|
51
|
+
? `${canonicalToSql(col.type, this.dialect)} ${this.dialect.autoIncrementSuffix}`
|
|
52
|
+
: suppliedKeySql(col.type, this.dialect);
|
|
49
53
|
}
|
|
50
54
|
/**
|
|
51
55
|
* The entity side as an AST, carrying this generator's default referential action. Named by the dialect's
|
|
@@ -8,10 +8,9 @@ export declare class MongodbQuerier extends AbstractQuerier implements MongoQuer
|
|
|
8
8
|
#private;
|
|
9
9
|
readonly dialect: MongoDialect;
|
|
10
10
|
readonly conn: MongoClient;
|
|
11
|
-
readonly extra?: ExtraOptions | undefined;
|
|
12
11
|
/** The open transaction's session, for a raw call through `db` to run inside it: `{ session: querier.session }`. */
|
|
13
12
|
get session(): ClientSession | undefined;
|
|
14
|
-
constructor(dialect: MongoDialect, conn: MongoClient, extra?: ExtraOptions
|
|
13
|
+
constructor(dialect: MongoDialect, conn: MongoClient, extra?: ExtraOptions);
|
|
15
14
|
/** Runs `task` on the open transaction's session, if any, after everything queued before it, timed as `label`. */
|
|
16
15
|
private execute;
|
|
17
16
|
protected internalFindMany<E extends Document>(entity: Type<E>, q: Query<E>, opts?: QueryOptions): Promise<E[]>;
|
|
@@ -26,7 +26,6 @@ function refuseTriggers(entity) {
|
|
|
26
26
|
export class MongodbQuerier extends AbstractQuerier {
|
|
27
27
|
dialect;
|
|
28
28
|
conn;
|
|
29
|
-
extra;
|
|
30
29
|
#session;
|
|
31
30
|
/** The open transaction's session, for a raw call through `db` to run inside it: `{ session: querier.session }`. */
|
|
32
31
|
get session() {
|
|
@@ -36,7 +35,6 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
36
35
|
super(extra);
|
|
37
36
|
this.dialect = dialect;
|
|
38
37
|
this.conn = conn;
|
|
39
|
-
this.extra = extra;
|
|
40
38
|
}
|
|
41
39
|
/** Runs `task` on the open transaction's session, if any, after everything queued before it, timed as `label`. */
|
|
42
40
|
execute(label, task) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import mssql from 'mssql';
|
|
2
2
|
import { AbstractPoolQuerier } from '../querier/abstractPoolQuerier.js';
|
|
3
3
|
import type { RawRow, TransactionOptions } from '../type/index.js';
|
|
4
|
+
import type { SqlExecution } from '../util/sql.util.js';
|
|
4
5
|
/** What `tedious` hands back for one statement, whichever shape it took. */
|
|
5
6
|
type MsSqlResult = {
|
|
6
7
|
recordset?: RawRow[] & {
|
|
@@ -46,10 +47,7 @@ export declare class MsSqlQuerier extends AbstractPoolQuerier<MsSqlConnection> {
|
|
|
46
47
|
* `rowsAffected` carries one entry per statement, and a write runs in a batch of several - the table variable
|
|
47
48
|
* its ids go through, the read handing them back uncounted - so the counts are summed.
|
|
48
49
|
*/
|
|
49
|
-
protected execute(sql: string, values?: unknown[]): Promise<
|
|
50
|
-
rows: RawRow[];
|
|
51
|
-
changes: number;
|
|
52
|
-
}>;
|
|
50
|
+
protected execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
53
51
|
/**
|
|
54
52
|
* The driver's own stream over the request, which pauses the request while the loop is behind, so
|
|
55
53
|
* rows arrive only as fast as they are read. A failure the driver reports through the promise alone
|
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
import type { PoolConnection } from 'mysql2';
|
|
2
|
-
import type { RowDataPacket } from 'mysql2/promise';
|
|
3
2
|
import { AbstractPoolQuerier } from '../querier/abstractPoolQuerier.js';
|
|
3
|
+
import type { SqlExecution } from '../util/sql.util.js';
|
|
4
4
|
/**
|
|
5
5
|
* Holds the driver's own connection rather than its promise wrapper, whose types name the connection
|
|
6
6
|
* inside it a promise one too: streaming is the own connection's alone.
|
|
7
7
|
*/
|
|
8
8
|
export declare class MySql2Querier extends AbstractPoolQuerier<PoolConnection> {
|
|
9
9
|
/** A read answers its rows; a write, the header naming how many rows it changed and the first id it generated. */
|
|
10
|
-
protected execute(sql: string, values?: unknown[]): Promise<
|
|
11
|
-
rows: RowDataPacket[];
|
|
12
|
-
changes?: undefined;
|
|
13
|
-
id?: undefined;
|
|
14
|
-
} | {
|
|
15
|
-
rows: never[];
|
|
16
|
-
changes: number;
|
|
17
|
-
id: number;
|
|
18
|
-
}>;
|
|
10
|
+
protected execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
19
11
|
internalStream(query: string, values?: unknown[]): AsyncGenerator<any, void, any>;
|
|
20
12
|
protected releaseConn(conn: PoolConnection, discard: boolean): Promise<void>;
|
|
21
13
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbstractSqlQuerier } from '../querier/index.js';
|
|
2
|
-
import type { ExtraOptions
|
|
2
|
+
import type { ExtraOptions } from '../type/index.js';
|
|
3
|
+
import type { SqlExecution } from '../util/sql.util.js';
|
|
3
4
|
import type { PgliteDialect } from './pgliteDialect.js';
|
|
4
5
|
/** The two methods uql uses of `@electric-sql/pglite`, stated so its published types do not depend on a pre-1.0 package. */
|
|
5
6
|
export type PgliteDatabase = {
|
|
@@ -17,16 +18,12 @@ export type PgliteDatabase = {
|
|
|
17
18
|
*/
|
|
18
19
|
export declare class PgliteQuerier extends AbstractSqlQuerier {
|
|
19
20
|
readonly db: PgliteDatabase;
|
|
20
|
-
|
|
21
|
-
constructor(db: PgliteDatabase, dialect: PgliteDialect, extra?: ExtraOptions | undefined);
|
|
21
|
+
constructor(db: PgliteDatabase, dialect: PgliteDialect, extra?: ExtraOptions);
|
|
22
22
|
/** Every querier of the pool is this one handle's session, so they share its transaction. */
|
|
23
23
|
protected get connection(): object;
|
|
24
24
|
/**
|
|
25
25
|
* `affectedRows`, not `rowCount`: PGlite derives the former from the command tag of a write only, where the
|
|
26
26
|
* latter also counts a `SELECT`'s rows and is absent altogether from a DDL tag.
|
|
27
27
|
*/
|
|
28
|
-
protected execute(sql: string, values?: unknown[]): Promise<
|
|
29
|
-
rows: RawRow[];
|
|
30
|
-
changes: number | undefined;
|
|
31
|
-
}>;
|
|
28
|
+
protected execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
32
29
|
}
|
|
@@ -7,11 +7,9 @@ import { AbstractSqlQuerier } from '../querier/index.js';
|
|
|
7
7
|
*/
|
|
8
8
|
export class PgliteQuerier extends AbstractSqlQuerier {
|
|
9
9
|
db;
|
|
10
|
-
extra;
|
|
11
10
|
constructor(db, dialect, extra) {
|
|
12
11
|
super(dialect, extra);
|
|
13
12
|
this.db = db;
|
|
14
|
-
this.extra = extra;
|
|
15
13
|
}
|
|
16
14
|
/** Every querier of the pool is this one handle's session, so they share its transaction. */
|
|
17
15
|
get connection() {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbstractPoolQuerier } from '../querier/abstractPoolQuerier.js';
|
|
2
2
|
import type { RawRow } from '../type/index.js';
|
|
3
|
+
import type { SqlExecution } from '../util/sql.util.js';
|
|
3
4
|
import { type PgResult } from './pgExecution.js';
|
|
4
5
|
export interface PgAnyClient {
|
|
5
6
|
query(text: string, values?: unknown[]): Promise<PgResult>;
|
|
@@ -19,7 +20,7 @@ export declare class PgQuerier extends AbstractPoolQuerier<PgAnyClient> {
|
|
|
19
20
|
* the process: the querier hears it while it holds the client, and what it hands the client back to after it.
|
|
20
21
|
*/
|
|
21
22
|
protected acquired(conn: PgAnyClient): PgAnyClient;
|
|
22
|
-
protected execute(sql: string, values?: unknown[]): Promise<
|
|
23
|
+
protected execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
23
24
|
internalStream(query: string, values?: unknown[]): AsyncGenerator<RawRow, void, any>;
|
|
24
25
|
protected releaseConn(conn: PgAnyClient, discard: boolean): Promise<void>;
|
|
25
26
|
}
|
|
@@ -3,10 +3,9 @@ import type { ExtraOptions } from '../type/index.js';
|
|
|
3
3
|
import { AbstractSqlQuerier } from './abstractSqlQuerier.js';
|
|
4
4
|
export declare abstract class AbstractPoolQuerier<C> extends AbstractSqlQuerier {
|
|
5
5
|
protected readonly connect: () => Promise<C>;
|
|
6
|
-
readonly extra?: ExtraOptions | undefined;
|
|
7
6
|
protected conn: C | undefined;
|
|
8
7
|
protected getConn(): C;
|
|
9
|
-
constructor(connect: () => Promise<C>, dialect: AbstractSqlDialect, extra?: ExtraOptions
|
|
8
|
+
constructor(connect: () => Promise<C>, dialect: AbstractSqlDialect, extra?: ExtraOptions);
|
|
10
9
|
protected lazyConnect(): Promise<void>;
|
|
11
10
|
/** A connection as this querier takes it from its pool, before its first statement: see {@link releaseConn}. */
|
|
12
11
|
protected acquired(conn: C): C;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AbstractSqlQuerier } from './abstractSqlQuerier.js';
|
|
2
2
|
export class AbstractPoolQuerier extends AbstractSqlQuerier {
|
|
3
3
|
connect;
|
|
4
|
-
extra;
|
|
5
4
|
conn;
|
|
6
5
|
getConn() {
|
|
7
6
|
if (!this.conn)
|
|
@@ -11,7 +10,6 @@ export class AbstractPoolQuerier extends AbstractSqlQuerier {
|
|
|
11
10
|
constructor(connect, dialect, extra) {
|
|
12
11
|
super(dialect, extra);
|
|
13
12
|
this.connect = connect;
|
|
14
|
-
this.extra = extra;
|
|
15
13
|
}
|
|
16
14
|
async lazyConnect() {
|
|
17
15
|
await super.lazyConnect();
|
|
@@ -221,12 +221,12 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
221
221
|
restoreMany<E extends object>(entity: Type<E>, q: QuerySearch<E>): Promise<number>;
|
|
222
222
|
upsertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QueryUpsert<E> & {
|
|
223
223
|
$entity: Type<E>;
|
|
224
|
-
}, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['
|
|
225
|
-
upsertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QueryUpsert<E>, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['
|
|
224
|
+
}, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['upserted']>;
|
|
225
|
+
upsertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QueryUpsert<E>, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['upserted']>;
|
|
226
226
|
upsertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QueryUpsert<E> & {
|
|
227
227
|
$entity: Type<E>;
|
|
228
|
-
}, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['
|
|
229
|
-
upsertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QueryUpsert<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['
|
|
228
|
+
}, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['upserted'][]>;
|
|
229
|
+
upsertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QueryUpsert<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['upserted'][]>;
|
|
230
230
|
/** Upserts `rows`, reading first where {@link upsertsByReading} says so, and answers the id each row got. */
|
|
231
231
|
private upsertIds;
|
|
232
232
|
/**
|
|
@@ -296,10 +296,7 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
296
296
|
private replaceOwned;
|
|
297
297
|
/** Writes each parent's children: rows pointed at their parent, or targets linked through the junction. */
|
|
298
298
|
private saveChildren;
|
|
299
|
-
/**
|
|
300
|
-
* Saves the rows a relation points at, each one's id what the pointer needs: a MySQL batch mixing supplied and
|
|
301
|
-
* generated keys cannot report it, so one it could not is refused rather than left pointing nowhere.
|
|
302
|
-
*/
|
|
299
|
+
/** Saves the rows a relation points at, each one's id what the pointer needs. */
|
|
303
300
|
private saveTargets;
|
|
304
301
|
/**
|
|
305
302
|
* Deletes the children of the parents `ids` names, as the parents go: a hard delete takes them all, a soft
|
|
@@ -362,7 +362,12 @@ export class AbstractQuerier {
|
|
|
362
362
|
const meta = getMeta(entity);
|
|
363
363
|
return this.inserting(entity, payload, async (rows) => {
|
|
364
364
|
await this.insertRows(entity, rows, (inserting) => this.internalInsertMany(entity, inserting));
|
|
365
|
-
return writtenIds(meta, rows)
|
|
365
|
+
return writtenIds(meta, rows).map((id) => {
|
|
366
|
+
if (id == null) {
|
|
367
|
+
throw new UqlUsageError(`an insert into '${meta.name}' left its key empty: name it`);
|
|
368
|
+
}
|
|
369
|
+
return id;
|
|
370
|
+
});
|
|
366
371
|
});
|
|
367
372
|
}
|
|
368
373
|
/** `insert` of `payload` between the insert hooks, {@link atomically} where it writes relations too. */
|
|
@@ -917,18 +922,10 @@ export class AbstractQuerier {
|
|
|
917
922
|
const targets = targetJoins(relOpts, meta.ids.length);
|
|
918
923
|
await this.insertMany(relOpts.through(), children.map(({ id }, index) => ({ ...pointAt(parents, id), ...pointAt(targets, targetIds[index]) })));
|
|
919
924
|
}
|
|
920
|
-
/**
|
|
921
|
-
* Saves the rows a relation points at, each one's id what the pointer needs: a MySQL batch mixing supplied and
|
|
922
|
-
* generated keys cannot report it, so one it could not is refused rather than left pointing nowhere.
|
|
923
|
-
*/
|
|
925
|
+
/** Saves the rows a relation points at, each one's id what the pointer needs. */
|
|
924
926
|
async saveTargets(entity, relKey, rows) {
|
|
925
927
|
const relEntity = relationOf(getMeta(entity), relKey).entity();
|
|
926
|
-
|
|
927
|
-
if (ids.includes(undefined)) {
|
|
928
|
-
throw new UqlUsageError(`'${relEntity.name}' rows saved through '${entity.name}.${relKey}' reported no id, so they cannot be pointed at. ` +
|
|
929
|
-
'Insert them with their own ids, or save the relation in its own statement.');
|
|
930
|
-
}
|
|
931
|
-
return ids.filter((id) => id !== undefined);
|
|
928
|
+
return this.saveMany(relEntity, rows);
|
|
932
929
|
}
|
|
933
930
|
/**
|
|
934
931
|
* Deletes the children of the parents `ids` names, as the parents go: a hard delete takes them all, a soft
|
|
@@ -10,9 +10,8 @@ export type BoundStatement = {
|
|
|
10
10
|
export declare abstract class AbstractSqlQuerier extends AbstractQuerier implements SqlQuerier {
|
|
11
11
|
#private;
|
|
12
12
|
readonly dialect: AbstractSqlDialect;
|
|
13
|
-
readonly extra?: ExtraOptions | undefined;
|
|
14
13
|
hasOpenTransaction: boolean;
|
|
15
|
-
constructor(dialect: AbstractSqlDialect, extra?: ExtraOptions
|
|
14
|
+
constructor(dialect: AbstractSqlDialect, extra?: ExtraOptions);
|
|
16
15
|
/** Runs one statement on the driver: what it read, how many rows it changed, and any id its header reports. */
|
|
17
16
|
protected abstract execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
18
17
|
/** What a write reports: the rows it changed, and their ids, inferred from a header's first id where the dialect reads them so. */
|
|
@@ -104,6 +103,14 @@ export declare abstract class AbstractSqlQuerier extends AbstractQuerier impleme
|
|
|
104
103
|
estimatedCount<E extends object>(entity: Type<E>): Promise<number>;
|
|
105
104
|
protected internalAggregate<E extends object>(entity: Type<E>, q: QueryAggregate<E>, opts?: QueryOptions): Promise<RawRow[]>;
|
|
106
105
|
internalInsertMany<E extends object>(entity: Type<E>, rows: EntityData<E>[]): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Settles the key of each row leaving it out, unless the database generates it. With no default nothing fills it, so
|
|
108
|
+
* the insert is refused unwritten; where the engine reports only a key it generates, the row names the value its
|
|
109
|
+
* default would have taken, SQL evaluated first.
|
|
110
|
+
*/
|
|
111
|
+
private settleLeftOutKeys;
|
|
112
|
+
/** What SQL evaluates to on its own, `count` times, each as a column's default would: one statement, a value a row. */
|
|
113
|
+
private evaluated;
|
|
107
114
|
/**
|
|
108
115
|
* The statements an insert of `rows` takes, as the indexes of the rows each writes. Grouped by whether a row names
|
|
109
116
|
* its key, as the two carry different columns: a budget taken over their union would under-fill the statement
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { AGGREGATE_VALUE_ALIAS, TOTAL_ALIAS } from '../dialect/aliases.js';
|
|
1
|
+
import { AGGREGATE_VALUE_ALIAS, KEY_DEFAULT_ALIAS, TOTAL_ALIAS } from '../dialect/aliases.js';
|
|
2
2
|
import { getMeta } from '../entity/index.js';
|
|
3
3
|
import { generatesKey, idOf, namesEvery, namesKey, soleId } from '../entity/metadata/definition.js';
|
|
4
|
+
import { QuerySql } from '../type/querySql.js';
|
|
4
5
|
import { buildUpdateResult, chunk, clone, getInsertFieldKeys, insertShapeOf, upsertKeepsOwnKey, getKeys, rowKey, } from '../util/index.js';
|
|
5
|
-
import { statementOf } from '../util/sql.js';
|
|
6
|
+
import { sql, statementOf } from '../util/sql.js';
|
|
6
7
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
7
8
|
import { AbstractQuerier } from './abstractQuerier.js';
|
|
8
9
|
import { streamViaCursor } from './cursorStream.js';
|
|
@@ -64,14 +65,12 @@ function inPayloadOrder(meta, rows, reported) {
|
|
|
64
65
|
}
|
|
65
66
|
export class AbstractSqlQuerier extends AbstractQuerier {
|
|
66
67
|
dialect;
|
|
67
|
-
extra;
|
|
68
68
|
hasOpenTransaction = false;
|
|
69
69
|
/** Cached `auto_increment_increment` stride; see {@link loadInsertIdIncrement}. */
|
|
70
70
|
#insertIdIncrement;
|
|
71
71
|
constructor(dialect, extra) {
|
|
72
72
|
super(extra);
|
|
73
73
|
this.dialect = dialect;
|
|
74
|
-
this.extra = extra;
|
|
75
74
|
}
|
|
76
75
|
/** What a write reports: the rows it changed, and their ids, inferred from a header's first id where the dialect reads them so. */
|
|
77
76
|
updateResultOf(result) {
|
|
@@ -286,6 +285,7 @@ export class AbstractSqlQuerier extends AbstractQuerier {
|
|
|
286
285
|
const [idKey] = meta.ids;
|
|
287
286
|
const sole = soleId(meta);
|
|
288
287
|
const generatedKey = generatesKey(meta);
|
|
288
|
+
await this.settleLeftOutKeys(meta, rows);
|
|
289
289
|
// Only a statement whose every row left its key to the database needs its ids, which a supplied one would void
|
|
290
290
|
// in a header: a `RETURNING` reports them all, a header only a generated key's.
|
|
291
291
|
const statements = this.insertChunks(meta, rows).map((indexes) => ({
|
|
@@ -318,6 +318,38 @@ export class AbstractSqlQuerier extends AbstractQuerier {
|
|
|
318
318
|
}
|
|
319
319
|
});
|
|
320
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* Settles the key of each row leaving it out, unless the database generates it. With no default nothing fills it, so
|
|
323
|
+
* the insert is refused unwritten; where the engine reports only a key it generates, the row names the value its
|
|
324
|
+
* default would have taken, SQL evaluated first.
|
|
325
|
+
*/
|
|
326
|
+
async settleLeftOutKeys(meta, rows) {
|
|
327
|
+
const sole = soleId(meta);
|
|
328
|
+
if (!sole || generatesKey(meta)) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const unnamed = rows.filter((row) => !namesKey(meta, row));
|
|
332
|
+
if (!unnamed.length) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const value = sole.field.defaultValue;
|
|
336
|
+
if (value === undefined) {
|
|
337
|
+
throw new UqlUsageError(`'${meta.name}.${sole.key}' is filled by nothing: name it, or declare what fills it`);
|
|
338
|
+
}
|
|
339
|
+
if (this.dialect.insertIdSource === 'returning') {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const keys = value instanceof QuerySql ? await this.evaluated(value, unnamed.length) : unnamed.map(() => value);
|
|
343
|
+
unnamed.forEach((row, at) => {
|
|
344
|
+
row[sole.key] = keys[at];
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
/** What SQL evaluates to on its own, `count` times, each as a column's default would: one statement, a value a row. */
|
|
348
|
+
async evaluated(value, count) {
|
|
349
|
+
const select = sql `SELECT ${value} AS ${sql.id(KEY_DEFAULT_ALIAS)}`;
|
|
350
|
+
const rows = await this.rowsOf(sql.join(new Array(count).fill(select), ' UNION ALL '));
|
|
351
|
+
return rows.map((row) => row[KEY_DEFAULT_ALIAS]);
|
|
352
|
+
}
|
|
321
353
|
/**
|
|
322
354
|
* The statements an insert of `rows` takes, as the indexes of the rows each writes. Grouped by whether a row names
|
|
323
355
|
* its key, as the two carry different columns: a budget taken over their union would under-fill the statement
|
|
@@ -19,9 +19,9 @@ type TypeBounds = {
|
|
|
19
19
|
* `TINYINT(1)` means boolean on MySQL.
|
|
20
20
|
*/
|
|
21
21
|
export declare function sqlToCanonical(sqlType: string, reported?: TypeBounds): CanonicalType;
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
*/
|
|
22
|
+
/** {@link canonicalToSql} for a key the database does not generate, which an engine may spell apart from one it does. */
|
|
23
|
+
export declare function suppliedKeySql(type: CanonicalType, dialect: AbstractDialect): string;
|
|
24
|
+
/** Convert a canonical type to a SQL type string for a specific dialect instance. */
|
|
25
25
|
export declare function canonicalToSql(type: CanonicalType, dialect: AbstractDialect): string;
|
|
26
26
|
/**
|
|
27
27
|
* Convert a canonical type to a TypeScript type string.
|
|
@@ -203,7 +203,7 @@ const ENGINE_TYPES = {
|
|
|
203
203
|
timestampPrecision: 0,
|
|
204
204
|
},
|
|
205
205
|
// SQLite uses affinity, so no size variants. `F32_BLOB` is libSQL's vector type; elsewhere just a name of BLOB affinity.
|
|
206
|
-
sqlite: { scalars: withVectorType(SQLITE_SCALAR_MAP, 'F32_BLOB') },
|
|
206
|
+
sqlite: { scalars: withVectorType(SQLITE_SCALAR_MAP, 'F32_BLOB'), suppliedIntegerKey: 'INT' },
|
|
207
207
|
// 2025 and up; below that the server refuses the type rather than storing it as text.
|
|
208
208
|
mssql: { scalars: withVectorType(MSSQL_SCALAR_MAP, 'VECTOR'), sizes: MSSQL_SIZES, timestampPrecision: 7 },
|
|
209
209
|
mongodb: { scalars: withVectorType(MONGO_SCALAR_MAP, 'array') },
|
|
@@ -277,9 +277,14 @@ function declaredSqlType(options) {
|
|
|
277
277
|
}
|
|
278
278
|
return columnType ?? (typeof type === 'string' ? type : undefined);
|
|
279
279
|
}
|
|
280
|
-
/**
|
|
281
|
-
|
|
282
|
-
|
|
280
|
+
/** {@link canonicalToSql} for a key the database does not generate, which an engine may spell apart from one it does. */
|
|
281
|
+
export function suppliedKeySql(type, dialect) {
|
|
282
|
+
const { suppliedIntegerKey } = ENGINE_TYPES[dialect.dialectName];
|
|
283
|
+
return suppliedIntegerKey && type.category === 'integer' && !type.raw
|
|
284
|
+
? suppliedIntegerKey
|
|
285
|
+
: canonicalToSql(type, dialect);
|
|
286
|
+
}
|
|
287
|
+
/** Convert a canonical type to a SQL type string for a specific dialect instance. */
|
|
283
288
|
export function canonicalToSql(type, dialect) {
|
|
284
289
|
if (type.raw)
|
|
285
290
|
return type.raw;
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { Except, FieldMeta, QuerySql } from '../type/index.js';
|
|
2
2
|
import type { ColumnNode } from './types.js';
|
|
3
3
|
/**
|
|
4
|
-
* The column a field's options declare, whether an entity's table or a migration holds it: `
|
|
5
|
-
* is its table's whole and own key, which a numeric one auto-increments as, and `compile` renders its SQL.
|
|
4
|
+
* The column a field's options declare, whether an entity's table or a migration holds it: `compile` renders its SQL.
|
|
6
5
|
*/
|
|
7
|
-
export declare function declaredColumn(field: FieldMeta, { name,
|
|
6
|
+
export declare function declaredColumn(field: FieldMeta, { name, compile }: {
|
|
8
7
|
readonly name: string;
|
|
9
|
-
readonly soleKey: boolean;
|
|
10
8
|
readonly compile: (sql: QuerySql) => string;
|
|
11
9
|
}): Except<ColumnNode, 'table'>;
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { declaresNotNull, fieldFamily
|
|
1
|
+
import { declaresNotNull, fieldFamily } from '../util/field.util.js';
|
|
2
2
|
import { resolveColumnCanonicalType } from './canonicalType.js';
|
|
3
3
|
import { schemaDefault } from './sqlExpression.js';
|
|
4
4
|
/**
|
|
5
|
-
* The column a field's options declare, whether an entity's table or a migration holds it: `
|
|
6
|
-
* is its table's whole and own key, which a numeric one auto-increments as, and `compile` renders its SQL.
|
|
5
|
+
* The column a field's options declare, whether an entity's table or a migration holds it: `compile` renders its SQL.
|
|
7
6
|
*/
|
|
8
|
-
export function declaredColumn(field, { name,
|
|
7
|
+
export function declaredColumn(field, { name, compile }) {
|
|
9
8
|
return {
|
|
10
9
|
name,
|
|
11
10
|
type: resolveColumnCanonicalType(field),
|
|
12
11
|
nullable: !declaresNotNull(field),
|
|
13
12
|
defaultValue: schemaDefault(field.defaultValue, compile, fieldFamily(field) === 'json'),
|
|
14
13
|
isPrimaryKey: field.isId === true,
|
|
15
|
-
isAutoIncrement:
|
|
14
|
+
isAutoIncrement: field.autoIncrement === true,
|
|
16
15
|
isUnique: field.unique ?? false,
|
|
17
16
|
// A stamp is filled by a trigger, so it is a column like any other; only `stored: true` generates.
|
|
18
17
|
generatedAs: field.stored === true && field.computed ? compile(field.computed) : undefined,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fieldOf, foreignKeysOf, getMeta
|
|
1
|
+
import { fieldOf, foreignKeysOf, getMeta } from '../entity/metadata/definition.js';
|
|
2
2
|
import { declaredIndexes, enumCheck, ownedCheck, renderIndex } from '../util/ddlExpression.util.js';
|
|
3
3
|
import { fulltextWeights, textWeightSteps } from '../util/dialect.util.js';
|
|
4
4
|
import { isInlinedExpression } from '../util/field.util.js';
|
|
@@ -50,7 +50,6 @@ function addTableFromEntity(ctx, meta) {
|
|
|
50
50
|
const column = {
|
|
51
51
|
...declaredColumn(field, {
|
|
52
52
|
name: columnName,
|
|
53
|
-
soleKey: ownsSoleKey(meta, key),
|
|
54
53
|
compile: (sql) => ctx.compileDdl(sql, meta.entity),
|
|
55
54
|
}),
|
|
56
55
|
...(notNull.has(key) && { nullable: false }),
|
|
@@ -45,10 +45,9 @@ export type HranaQuerierConnectionOptions = {
|
|
|
45
45
|
*/
|
|
46
46
|
export declare class HranaQuerier extends AbstractSqlQuerier {
|
|
47
47
|
readonly client: HranaClient;
|
|
48
|
-
readonly extra?: ExtraOptions | undefined;
|
|
49
48
|
private tx?;
|
|
50
49
|
private readonly closeClientOnRelease;
|
|
51
|
-
constructor(client: HranaClient, dialect: SqliteDialect, extra?: ExtraOptions
|
|
50
|
+
constructor(client: HranaClient, dialect: SqliteDialect, extra?: ExtraOptions, connection?: HranaQuerierConnectionOptions);
|
|
52
51
|
/** Runs on the open transaction's handle when there is one. */
|
|
53
52
|
protected execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
54
53
|
/** A list as one request: the client's `batch`, all or none; inside an open transaction, its own, which joins it. */
|
|
@@ -9,13 +9,11 @@ import { sqliteStatements, toBindValues } from './bindValues.js';
|
|
|
9
9
|
*/
|
|
10
10
|
export class HranaQuerier extends AbstractSqlQuerier {
|
|
11
11
|
client;
|
|
12
|
-
extra;
|
|
13
12
|
tx;
|
|
14
13
|
closeClientOnRelease;
|
|
15
14
|
constructor(client, dialect, extra, connection) {
|
|
16
15
|
super(dialect, extra);
|
|
17
16
|
this.client = client;
|
|
18
|
-
this.extra = extra;
|
|
19
17
|
this.closeClientOnRelease = connection?.closeClientOnRelease ?? false;
|
|
20
18
|
}
|
|
21
19
|
/** Runs on the open transaction's handle when there is one. */
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbstractSqlQuerier } from '../querier/index.js';
|
|
2
2
|
import type { ExtraOptions, RawRow } from '../type/index.js';
|
|
3
|
+
import type { SqlExecution } from '../util/sql.util.js';
|
|
3
4
|
import { type SqliteBindValue } from './bindValues.js';
|
|
4
5
|
import type { SqliteDialect } from './sqliteDialect.js';
|
|
5
6
|
/**
|
|
@@ -32,18 +33,11 @@ export type SqliteDatabase = {
|
|
|
32
33
|
*/
|
|
33
34
|
export declare class SqliteQuerier extends AbstractSqlQuerier {
|
|
34
35
|
readonly db: SqliteDatabase;
|
|
35
|
-
|
|
36
|
-
constructor(db: SqliteDatabase, dialect: SqliteDialect, extra?: ExtraOptions | undefined);
|
|
36
|
+
constructor(db: SqliteDatabase, dialect: SqliteDialect, extra?: ExtraOptions);
|
|
37
37
|
/** Every querier of the pool is this one handle's session, so they share its transaction. */
|
|
38
38
|
protected get connection(): object;
|
|
39
39
|
/** `reader` picks the call: `run()` would discard the rows of a statement that reads, RETURNING included. */
|
|
40
|
-
protected execute(sql: string, values?: unknown[]): Promise<
|
|
41
|
-
rows: RawRow[];
|
|
42
|
-
changes?: undefined;
|
|
43
|
-
} | {
|
|
44
|
-
rows: never[];
|
|
45
|
-
changes: number;
|
|
46
|
-
}>;
|
|
40
|
+
protected execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
47
41
|
/** Row by row, as the driver steps the statement. */
|
|
48
42
|
internalStream(sql: string, values?: unknown[]): AsyncGenerator<RawRow, void, unknown>;
|
|
49
43
|
}
|
|
@@ -8,11 +8,9 @@ import { toBindValues } from './bindValues.js';
|
|
|
8
8
|
*/
|
|
9
9
|
export class SqliteQuerier extends AbstractSqlQuerier {
|
|
10
10
|
db;
|
|
11
|
-
extra;
|
|
12
11
|
constructor(db, dialect, extra) {
|
|
13
12
|
super(dialect, extra);
|
|
14
13
|
this.db = db;
|
|
15
|
-
this.extra = extra;
|
|
16
14
|
}
|
|
17
15
|
/** Every querier of the pool is this one handle's session, so they share its transaction. */
|
|
18
16
|
get connection() {
|
|
@@ -49,8 +49,7 @@ export type TursoSession = {
|
|
|
49
49
|
/** A Turso Cloud querier on a stream of its own, which holds its transaction and which `release` closes. */
|
|
50
50
|
export declare class TursoSessionQuerier extends AbstractSqlQuerier {
|
|
51
51
|
readonly session: TursoSession;
|
|
52
|
-
|
|
53
|
-
constructor(session: TursoSession, dialect: SqliteDialect, extra?: ExtraOptions | undefined);
|
|
52
|
+
constructor(session: TursoSession, dialect: SqliteDialect, extra?: ExtraOptions);
|
|
54
53
|
protected execute(sql: string, values?: unknown[]): Promise<SqlExecution>;
|
|
55
54
|
/**
|
|
56
55
|
* A list as one request, all or none: its own `BEGIN IMMEDIATE` and `COMMIT` in it, or, inside an open
|
|
@@ -4,11 +4,9 @@ import { decodeBigInts } from '../util/wideNumber.js';
|
|
|
4
4
|
/** A Turso Cloud querier on a stream of its own, which holds its transaction and which `release` closes. */
|
|
5
5
|
export class TursoSessionQuerier extends AbstractSqlQuerier {
|
|
6
6
|
session;
|
|
7
|
-
extra;
|
|
8
7
|
constructor(session, dialect, extra) {
|
|
9
8
|
super(dialect, extra);
|
|
10
9
|
this.session = session;
|
|
11
|
-
this.extra = extra;
|
|
12
10
|
}
|
|
13
11
|
async execute(sql, values) {
|
|
14
12
|
return executionOf(await this.session.execute(sql, toBindValues(values), true));
|
package/dist/type/column.d.ts
CHANGED
|
@@ -74,7 +74,7 @@ export interface ColumnBuilder<in out O, in out V, F extends Family> extends Col
|
|
|
74
74
|
readonly nullable: true;
|
|
75
75
|
}, F>;
|
|
76
76
|
/** Makes it the primary key, or a column of a composite one. */
|
|
77
|
-
id(this: Lacking<'nullable'>): With<O, V, {
|
|
77
|
+
id(this: Lacking<'nullable' | 'autoIncrement'>): With<O, V, {
|
|
78
78
|
readonly isId: true;
|
|
79
79
|
}, F>;
|
|
80
80
|
/** The column's DDL default: a value, or SQL the database evaluates per row (`currentTimestamp`, `sql`). */
|
|
@@ -150,13 +150,11 @@ export interface NumericColumn<in out O, in out V> extends ColumnBuilder<O, V, '
|
|
|
150
150
|
scale(this: TypeNotSql, digits: number): NumericColumn<O & {
|
|
151
151
|
readonly scale: number;
|
|
152
152
|
}, V>;
|
|
153
|
-
/**
|
|
154
|
-
autoIncrement(): NumericColumn<O & {
|
|
153
|
+
/** Makes it the primary key, which the database generates. A key the caller supplies is `.id()`. */
|
|
154
|
+
autoIncrement(this: Lacking<'nullable' | 'isId'>): NumericColumn<O & {
|
|
155
|
+
readonly isId: true;
|
|
155
156
|
readonly autoIncrement: true;
|
|
156
157
|
}, V>;
|
|
157
|
-
autoIncrement(generated: false): NumericColumn<O & {
|
|
158
|
-
readonly autoIncrement: false;
|
|
159
|
-
}, V>;
|
|
160
158
|
}
|
|
161
159
|
export interface StringColumn<in out O, in out V> extends ColumnBuilder<O, V, 'string'> {
|
|
162
160
|
length(this: TypeNotSql, length: number): StringColumn<O & {
|
|
@@ -100,7 +100,7 @@ type IdKeyOf<F> = FlagKeys<F, Column<{
|
|
|
100
100
|
type VersionKeyOf<F> = FlagKeys<F, Column<{
|
|
101
101
|
readonly version: true;
|
|
102
102
|
}>>;
|
|
103
|
-
type FilledKeyOf<F> =
|
|
103
|
+
type FilledKeyOf<F> = FlagKeys<F, Column<FilledOptions>>;
|
|
104
104
|
/** The relation constructors a defined entity's `relations` receives, typed by the columns of the entity declaring them. */
|
|
105
105
|
export type RelationBuilder<O> = {
|
|
106
106
|
oneToOne<T extends object, K = IdValue<T>, Columns = T>(target: Type<T> & DefinedShape<K, Columns>, opts: RelationOwnerOptions<T, O, DefinedJoins<T, O, K, Columns>> | RelationInverseOptions<T, DefinedJoins<T, O, K, Columns>>): DefinedRelation<T, '11'>;
|
package/dist/type/entity.d.ts
CHANGED
|
@@ -49,11 +49,10 @@ export type WritableKey<E> = {
|
|
|
49
49
|
readonly [K in FieldKey<E>]-?: IsEqual<Pick<E, K>, Writable<Pick<E, K>>> extends true ? K : never;
|
|
50
50
|
}[FieldKey<E>];
|
|
51
51
|
/**
|
|
52
|
-
* What an insert may leave out
|
|
53
|
-
*
|
|
54
|
-
* key is the references a row is made of, so an insert names it.
|
|
52
|
+
* What an insert may leave out, the class saying so: `_id`, which MongoDB fills, the version, which uql starts at 0,
|
|
53
|
+
* and what {@link filledKey} lists. A `?` property a write keeps as it is.
|
|
55
54
|
*/
|
|
56
|
-
type FilledKey<E> =
|
|
55
|
+
type FilledKey<E> = VersionKey<E> | (('_id' | BrandNames<E, typeof filledKey>) & FieldKey<E>);
|
|
57
56
|
/**
|
|
58
57
|
* A whole-record write as a caller supplies one: {@link EntityData} without the fields it cannot write,
|
|
59
58
|
* a {@link FilledKey} optional. That part is a conditional, which TypeScript leaves shut while it types a
|
|
@@ -343,7 +342,7 @@ export type FieldOptions<V = unknown> = {
|
|
|
343
342
|
readonly unique?: boolean;
|
|
344
343
|
/** The column's DDL default: a literal, or SQL the database evaluates per inserted row (`currentTimestamp`, `sql`). */
|
|
345
344
|
readonly defaultValue?: DdlDefault<V> | QuerySql;
|
|
346
|
-
/** Whether the database generates the value
|
|
345
|
+
/** Whether the database generates the value. */
|
|
347
346
|
readonly autoIncrement?: boolean;
|
|
348
347
|
/**
|
|
349
348
|
* `true` for an index over the column, a string to name it. A foreign key column is indexed unless
|