@type32/tauri-sqlite-orm 0.2.15 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +145 -4
- package/dist/index.d.ts +145 -4
- package/dist/index.js +270 -108
- package/dist/index.mjs +263 -104
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -56,8 +56,28 @@ type RelationType = 'one' | 'many';
|
|
|
56
56
|
interface RelationConfig {
|
|
57
57
|
type: RelationType;
|
|
58
58
|
foreignTable: AnyTable;
|
|
59
|
+
/** For one: local FK columns. For many (v2): foreign table's FK columns. */
|
|
59
60
|
fields?: AnySQLiteColumn[];
|
|
61
|
+
/** For one: foreign PK columns. For many (v2): parent table's PK columns. */
|
|
60
62
|
references?: AnySQLiteColumn[];
|
|
63
|
+
/** For many-to-many via through(): junction table between parent and foreign */
|
|
64
|
+
junctionTable?: AnyTable;
|
|
65
|
+
/** Parent column -> junction column (parent PK = junction FK to parent) */
|
|
66
|
+
fromJunction?: {
|
|
67
|
+
column: AnySQLiteColumn;
|
|
68
|
+
junctionColumn: AnySQLiteColumn;
|
|
69
|
+
};
|
|
70
|
+
/** Junction column -> foreign column (junction FK to foreign = foreign PK) */
|
|
71
|
+
toJunction?: {
|
|
72
|
+
junctionColumn: AnySQLiteColumn;
|
|
73
|
+
column: AnySQLiteColumn;
|
|
74
|
+
};
|
|
75
|
+
/** When false, one relation is required (type-level: T not T | null) */
|
|
76
|
+
optional?: boolean;
|
|
77
|
+
/** Alias for the relation (e.g. for self-referential disambiguation) */
|
|
78
|
+
alias?: string;
|
|
79
|
+
/** Predefined filter for many relations: (alias) => Condition. Receives the joined table alias. */
|
|
80
|
+
where?: (alias: string) => unknown;
|
|
61
81
|
}
|
|
62
82
|
|
|
63
83
|
type Condition = Expression<SqlBool>;
|
|
@@ -233,6 +253,8 @@ type RelationsBuilder = {
|
|
|
233
253
|
one: <U extends AnyTable>(table: U, config?: {
|
|
234
254
|
fields: AnySQLiteColumn[];
|
|
235
255
|
references: AnySQLiteColumn[];
|
|
256
|
+
optional?: boolean;
|
|
257
|
+
alias?: string;
|
|
236
258
|
}) => OneRelation<U>;
|
|
237
259
|
many: <U extends AnyTable>(table: U) => ManyRelation<U>;
|
|
238
260
|
};
|
|
@@ -361,14 +383,53 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
|
361
383
|
config?: {
|
|
362
384
|
fields: AnySQLiteColumn[];
|
|
363
385
|
references: AnySQLiteColumn[];
|
|
386
|
+
optional?: boolean;
|
|
387
|
+
alias?: string;
|
|
364
388
|
} | undefined;
|
|
365
389
|
constructor(foreignTable: T, config?: {
|
|
366
390
|
fields: AnySQLiteColumn[];
|
|
367
391
|
references: AnySQLiteColumn[];
|
|
392
|
+
optional?: boolean;
|
|
393
|
+
alias?: string;
|
|
368
394
|
} | undefined);
|
|
369
395
|
}
|
|
370
396
|
declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
371
|
-
|
|
397
|
+
config?: {
|
|
398
|
+
from?: AnySQLiteColumn[];
|
|
399
|
+
to?: AnySQLiteColumn[];
|
|
400
|
+
through?: {
|
|
401
|
+
junctionTable: AnyTable;
|
|
402
|
+
fromRef: {
|
|
403
|
+
column: AnySQLiteColumn;
|
|
404
|
+
junctionColumn: AnySQLiteColumn;
|
|
405
|
+
};
|
|
406
|
+
toRef: {
|
|
407
|
+
column: AnySQLiteColumn;
|
|
408
|
+
junctionColumn: AnySQLiteColumn;
|
|
409
|
+
};
|
|
410
|
+
};
|
|
411
|
+
optional?: boolean;
|
|
412
|
+
alias?: string;
|
|
413
|
+
where?: (alias: string) => unknown;
|
|
414
|
+
} | undefined;
|
|
415
|
+
constructor(foreignTable: T, config?: {
|
|
416
|
+
from?: AnySQLiteColumn[];
|
|
417
|
+
to?: AnySQLiteColumn[];
|
|
418
|
+
through?: {
|
|
419
|
+
junctionTable: AnyTable;
|
|
420
|
+
fromRef: {
|
|
421
|
+
column: AnySQLiteColumn;
|
|
422
|
+
junctionColumn: AnySQLiteColumn;
|
|
423
|
+
};
|
|
424
|
+
toRef: {
|
|
425
|
+
column: AnySQLiteColumn;
|
|
426
|
+
junctionColumn: AnySQLiteColumn;
|
|
427
|
+
};
|
|
428
|
+
};
|
|
429
|
+
optional?: boolean;
|
|
430
|
+
alias?: string;
|
|
431
|
+
where?: (alias: string) => unknown;
|
|
432
|
+
} | undefined);
|
|
372
433
|
}
|
|
373
434
|
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
374
435
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
@@ -411,9 +472,18 @@ declare const enumType: <TName extends string, TValues extends readonly [string,
|
|
|
411
472
|
|
|
412
473
|
/** Maps relations() return type to typed relation configs with foreign table preserved */
|
|
413
474
|
type InferRelationsMap<R extends Record<string, OneRelation | ManyRelation>> = {
|
|
414
|
-
[K in keyof R]: R[K] extends OneRelation<infer T> ? {
|
|
475
|
+
[K in keyof R]: R[K] extends OneRelation<infer T> ? R[K] extends {
|
|
476
|
+
config?: {
|
|
477
|
+
optional?: infer O;
|
|
478
|
+
};
|
|
479
|
+
} ? {
|
|
480
|
+
type: 'one';
|
|
481
|
+
foreignTable: T;
|
|
482
|
+
optional: O;
|
|
483
|
+
} : {
|
|
415
484
|
type: 'one';
|
|
416
485
|
foreignTable: T;
|
|
486
|
+
optional?: true;
|
|
417
487
|
} : R[K] extends ManyRelation<infer T> ? {
|
|
418
488
|
type: 'many';
|
|
419
489
|
foreignTable: T;
|
|
@@ -440,7 +510,8 @@ type InferRelationFields<TTable extends AnyTable, TRelationsMap extends Record<s
|
|
|
440
510
|
[K in keyof TWith & keyof TRelationsMap]: TWith[K] extends false | undefined ? never : TRelationsMap[K] extends {
|
|
441
511
|
type: 'one';
|
|
442
512
|
foreignTable: infer T;
|
|
443
|
-
|
|
513
|
+
optional?: infer O;
|
|
514
|
+
} ? T extends AnyTable ? ([O] extends [false] ? InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations> : (InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations>) | null) : never : TRelationsMap[K] extends {
|
|
444
515
|
type: 'many';
|
|
445
516
|
foreignTable: infer T;
|
|
446
517
|
} ? T extends AnyTable ? (InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations>)[] : never : never;
|
|
@@ -484,6 +555,76 @@ type InferRelationalSelectModel<TTable extends AnyTable, TRelations extends Reco
|
|
|
484
555
|
[K in TTable['_']['name']]: TRelations;
|
|
485
556
|
}> = InferSelectModel<TTable> & InferRelationFields<TTable, InferRelationsMap<TRelations>, TAllRelations, TWith>;
|
|
486
557
|
|
|
558
|
+
/**
|
|
559
|
+
* Relations v2 API - Drizzle-style defineRelations with from/to and many-without-one.
|
|
560
|
+
* Use defineRelations() for a single place to define all relations, or defineRelationsPart() to split into parts.
|
|
561
|
+
*/
|
|
562
|
+
|
|
563
|
+
/** Options for one() relation - from/to replace fields/references */
|
|
564
|
+
interface OneRelationOptions {
|
|
565
|
+
from: AnySQLiteColumn | AnySQLiteColumn[];
|
|
566
|
+
to: AnySQLiteColumn | AnySQLiteColumn[];
|
|
567
|
+
optional?: boolean;
|
|
568
|
+
alias?: string;
|
|
569
|
+
}
|
|
570
|
+
/** Reference for through() - column with junction column for many-to-many */
|
|
571
|
+
interface ThroughRef {
|
|
572
|
+
column: AnySQLiteColumn;
|
|
573
|
+
junctionColumn: AnySQLiteColumn;
|
|
574
|
+
junctionTable: AnyTable;
|
|
575
|
+
}
|
|
576
|
+
/** Create a through reference for many-to-many: through(column, junctionColumn, junctionTable) */
|
|
577
|
+
declare function through(column: AnySQLiteColumn, junctionColumn: AnySQLiteColumn, junctionTable: AnyTable): ThroughRef;
|
|
578
|
+
/** Options for many() relation - optional explicit from/to for many-without-one, or through() for many-to-many */
|
|
579
|
+
interface ManyRelationOptions {
|
|
580
|
+
from?: AnySQLiteColumn | AnySQLiteColumn[] | ThroughRef;
|
|
581
|
+
to?: AnySQLiteColumn | AnySQLiteColumn[] | ThroughRef;
|
|
582
|
+
optional?: boolean;
|
|
583
|
+
alias?: string;
|
|
584
|
+
/** Predefined filter: (alias) => Condition. Applied to the joined relation table. */
|
|
585
|
+
where?: (alias: string) => Condition;
|
|
586
|
+
}
|
|
587
|
+
/** Build the r object passed to defineRelations callback */
|
|
588
|
+
declare function buildR<Tables extends Record<string, AnyTable>>(tables: Tables): {
|
|
589
|
+
[K in keyof Tables]: Record<string, AnySQLiteColumn>;
|
|
590
|
+
} & {
|
|
591
|
+
one: {
|
|
592
|
+
[K in keyof Tables]: (opts: OneRelationOptions) => OneRelation<Tables[K]>;
|
|
593
|
+
};
|
|
594
|
+
many: {
|
|
595
|
+
[K in keyof Tables]: (opts?: ManyRelationOptions) => ManyRelation<Tables[K]>;
|
|
596
|
+
};
|
|
597
|
+
};
|
|
598
|
+
type DefineRelationsCallback<Tables extends Record<string, AnyTable>> = (r: ReturnType<typeof buildR>) => {
|
|
599
|
+
[K in keyof Tables]?: Record<string, OneRelation | ManyRelation>;
|
|
600
|
+
};
|
|
601
|
+
/**
|
|
602
|
+
* Define all relations for your schema in one place (v2 API).
|
|
603
|
+
* Uses from/to instead of fields/references, and supports many-without-one.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* import * as schema from './schema'
|
|
608
|
+
* import { defineRelations } from '@type32/tauri-sqlite-orm'
|
|
609
|
+
*
|
|
610
|
+
* export const relations = defineRelations(schema, (r) => ({
|
|
611
|
+
* users: {
|
|
612
|
+
* posts: r.many.posts({ from: r.users.id, to: r.posts.userId }),
|
|
613
|
+
* },
|
|
614
|
+
* posts: {
|
|
615
|
+
* user: r.one.users({ from: r.posts.userId, to: r.users.id }),
|
|
616
|
+
* postTags: r.many.postTags({ from: r.posts.id, to: r.postTags.postId }),
|
|
617
|
+
* },
|
|
618
|
+
* }))
|
|
619
|
+
* ```
|
|
620
|
+
*/
|
|
621
|
+
declare function defineRelations<TSchema extends Record<string, unknown>>(schema: TSchema, callback: DefineRelationsCallback<Record<string, AnyTable>>): Record<string, Record<string, OneRelation | ManyRelation>>;
|
|
622
|
+
/**
|
|
623
|
+
* Define a part of relations - merge multiple parts when passing to TauriORM.
|
|
624
|
+
* Useful for splitting large schema definitions.
|
|
625
|
+
*/
|
|
626
|
+
declare function defineRelationsPart<TSchema extends Record<string, unknown>>(schema: TSchema, callback: DefineRelationsCallback<Record<string, AnyTable>>): Record<string, Record<string, OneRelation | ManyRelation>>;
|
|
627
|
+
|
|
487
628
|
declare class TauriORMError extends Error {
|
|
488
629
|
constructor(message: string);
|
|
489
630
|
}
|
|
@@ -515,4 +656,4 @@ declare class TableNotFoundError extends TauriORMError {
|
|
|
515
656
|
constructor(tableName: string);
|
|
516
657
|
}
|
|
517
658
|
|
|
518
|
-
export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, ColumnNotFoundError, type ColumnOptions, type ColumnValueTypes, type Condition, type DatabaseLike, DeleteQueryBuilder, type ExtractColumnType, type InferInsertModel, type InferRelationalSelectModel, type InferRelationsMap, type InferSelectModel, InsertQueryBuilder, InsertValidationError, ManyRelation, MigrationError, MissingWhereClauseError, type Mode, OneRelation, QueryBuilderError, Relation, type RelationConfig, RelationError, type RelationType, type RelationsBuilder, type SQLAggregate, type SQLCondition, type SQLSubquery, SQLiteColumn, SelectQueryBuilder, Table, TableNotFoundError, TauriDialect, TauriORM, TauriORMError, UpdateQueryBuilder, UpdateValidationError, ValidationError, WithQueryBuilder, alias, and, as, asc, avg, between, blob, boolean, contains, count, countDistinct, desc, endsWith, enumType, eq, eqSubquery, exists, getTableColumns, groupConcat, gt, gtSubquery, gte, gteSubquery, ilike, inArray, integer, isNotNull, isNull, like, lt, ltSubquery, lte, lteSubquery, max, min, ne, neSubquery, not, notExists, notIn, numeric, or, real, relations, scalarSubquery, sqliteTable, startsWith, subquery, sum, text };
|
|
659
|
+
export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, ColumnNotFoundError, type ColumnOptions, type ColumnValueTypes, type Condition, type DatabaseLike, type DefineRelationsCallback, DeleteQueryBuilder, type ExtractColumnType, type InferInsertModel, type InferRelationalSelectModel, type InferRelationsMap, type InferSelectModel, InsertQueryBuilder, InsertValidationError, ManyRelation, type ManyRelationOptions, MigrationError, MissingWhereClauseError, type Mode, OneRelation, type OneRelationOptions, QueryBuilderError, Relation, type RelationConfig, RelationError, type RelationType, type RelationsBuilder, type SQLAggregate, type SQLCondition, type SQLSubquery, SQLiteColumn, SelectQueryBuilder, Table, TableNotFoundError, TauriDialect, TauriORM, TauriORMError, type ThroughRef, UpdateQueryBuilder, UpdateValidationError, ValidationError, WithQueryBuilder, alias, and, as, asc, avg, between, blob, boolean, contains, count, countDistinct, defineRelations, defineRelationsPart, desc, endsWith, enumType, eq, eqSubquery, exists, getTableColumns, groupConcat, gt, gtSubquery, gte, gteSubquery, ilike, inArray, integer, isNotNull, isNull, like, lt, ltSubquery, lte, lteSubquery, max, min, ne, neSubquery, not, notExists, notIn, numeric, or, real, relations, scalarSubquery, sqliteTable, startsWith, subquery, sum, text, through };
|
package/dist/index.d.ts
CHANGED
|
@@ -56,8 +56,28 @@ type RelationType = 'one' | 'many';
|
|
|
56
56
|
interface RelationConfig {
|
|
57
57
|
type: RelationType;
|
|
58
58
|
foreignTable: AnyTable;
|
|
59
|
+
/** For one: local FK columns. For many (v2): foreign table's FK columns. */
|
|
59
60
|
fields?: AnySQLiteColumn[];
|
|
61
|
+
/** For one: foreign PK columns. For many (v2): parent table's PK columns. */
|
|
60
62
|
references?: AnySQLiteColumn[];
|
|
63
|
+
/** For many-to-many via through(): junction table between parent and foreign */
|
|
64
|
+
junctionTable?: AnyTable;
|
|
65
|
+
/** Parent column -> junction column (parent PK = junction FK to parent) */
|
|
66
|
+
fromJunction?: {
|
|
67
|
+
column: AnySQLiteColumn;
|
|
68
|
+
junctionColumn: AnySQLiteColumn;
|
|
69
|
+
};
|
|
70
|
+
/** Junction column -> foreign column (junction FK to foreign = foreign PK) */
|
|
71
|
+
toJunction?: {
|
|
72
|
+
junctionColumn: AnySQLiteColumn;
|
|
73
|
+
column: AnySQLiteColumn;
|
|
74
|
+
};
|
|
75
|
+
/** When false, one relation is required (type-level: T not T | null) */
|
|
76
|
+
optional?: boolean;
|
|
77
|
+
/** Alias for the relation (e.g. for self-referential disambiguation) */
|
|
78
|
+
alias?: string;
|
|
79
|
+
/** Predefined filter for many relations: (alias) => Condition. Receives the joined table alias. */
|
|
80
|
+
where?: (alias: string) => unknown;
|
|
61
81
|
}
|
|
62
82
|
|
|
63
83
|
type Condition = Expression<SqlBool>;
|
|
@@ -233,6 +253,8 @@ type RelationsBuilder = {
|
|
|
233
253
|
one: <U extends AnyTable>(table: U, config?: {
|
|
234
254
|
fields: AnySQLiteColumn[];
|
|
235
255
|
references: AnySQLiteColumn[];
|
|
256
|
+
optional?: boolean;
|
|
257
|
+
alias?: string;
|
|
236
258
|
}) => OneRelation<U>;
|
|
237
259
|
many: <U extends AnyTable>(table: U) => ManyRelation<U>;
|
|
238
260
|
};
|
|
@@ -361,14 +383,53 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
|
361
383
|
config?: {
|
|
362
384
|
fields: AnySQLiteColumn[];
|
|
363
385
|
references: AnySQLiteColumn[];
|
|
386
|
+
optional?: boolean;
|
|
387
|
+
alias?: string;
|
|
364
388
|
} | undefined;
|
|
365
389
|
constructor(foreignTable: T, config?: {
|
|
366
390
|
fields: AnySQLiteColumn[];
|
|
367
391
|
references: AnySQLiteColumn[];
|
|
392
|
+
optional?: boolean;
|
|
393
|
+
alias?: string;
|
|
368
394
|
} | undefined);
|
|
369
395
|
}
|
|
370
396
|
declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
371
|
-
|
|
397
|
+
config?: {
|
|
398
|
+
from?: AnySQLiteColumn[];
|
|
399
|
+
to?: AnySQLiteColumn[];
|
|
400
|
+
through?: {
|
|
401
|
+
junctionTable: AnyTable;
|
|
402
|
+
fromRef: {
|
|
403
|
+
column: AnySQLiteColumn;
|
|
404
|
+
junctionColumn: AnySQLiteColumn;
|
|
405
|
+
};
|
|
406
|
+
toRef: {
|
|
407
|
+
column: AnySQLiteColumn;
|
|
408
|
+
junctionColumn: AnySQLiteColumn;
|
|
409
|
+
};
|
|
410
|
+
};
|
|
411
|
+
optional?: boolean;
|
|
412
|
+
alias?: string;
|
|
413
|
+
where?: (alias: string) => unknown;
|
|
414
|
+
} | undefined;
|
|
415
|
+
constructor(foreignTable: T, config?: {
|
|
416
|
+
from?: AnySQLiteColumn[];
|
|
417
|
+
to?: AnySQLiteColumn[];
|
|
418
|
+
through?: {
|
|
419
|
+
junctionTable: AnyTable;
|
|
420
|
+
fromRef: {
|
|
421
|
+
column: AnySQLiteColumn;
|
|
422
|
+
junctionColumn: AnySQLiteColumn;
|
|
423
|
+
};
|
|
424
|
+
toRef: {
|
|
425
|
+
column: AnySQLiteColumn;
|
|
426
|
+
junctionColumn: AnySQLiteColumn;
|
|
427
|
+
};
|
|
428
|
+
};
|
|
429
|
+
optional?: boolean;
|
|
430
|
+
alias?: string;
|
|
431
|
+
where?: (alias: string) => unknown;
|
|
432
|
+
} | undefined);
|
|
372
433
|
}
|
|
373
434
|
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
374
435
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
@@ -411,9 +472,18 @@ declare const enumType: <TName extends string, TValues extends readonly [string,
|
|
|
411
472
|
|
|
412
473
|
/** Maps relations() return type to typed relation configs with foreign table preserved */
|
|
413
474
|
type InferRelationsMap<R extends Record<string, OneRelation | ManyRelation>> = {
|
|
414
|
-
[K in keyof R]: R[K] extends OneRelation<infer T> ? {
|
|
475
|
+
[K in keyof R]: R[K] extends OneRelation<infer T> ? R[K] extends {
|
|
476
|
+
config?: {
|
|
477
|
+
optional?: infer O;
|
|
478
|
+
};
|
|
479
|
+
} ? {
|
|
480
|
+
type: 'one';
|
|
481
|
+
foreignTable: T;
|
|
482
|
+
optional: O;
|
|
483
|
+
} : {
|
|
415
484
|
type: 'one';
|
|
416
485
|
foreignTable: T;
|
|
486
|
+
optional?: true;
|
|
417
487
|
} : R[K] extends ManyRelation<infer T> ? {
|
|
418
488
|
type: 'many';
|
|
419
489
|
foreignTable: T;
|
|
@@ -440,7 +510,8 @@ type InferRelationFields<TTable extends AnyTable, TRelationsMap extends Record<s
|
|
|
440
510
|
[K in keyof TWith & keyof TRelationsMap]: TWith[K] extends false | undefined ? never : TRelationsMap[K] extends {
|
|
441
511
|
type: 'one';
|
|
442
512
|
foreignTable: infer T;
|
|
443
|
-
|
|
513
|
+
optional?: infer O;
|
|
514
|
+
} ? T extends AnyTable ? ([O] extends [false] ? InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations> : (InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations>) | null) : never : TRelationsMap[K] extends {
|
|
444
515
|
type: 'many';
|
|
445
516
|
foreignTable: infer T;
|
|
446
517
|
} ? T extends AnyTable ? (InferSelectModel<T> & InferNestedFields<T, TWith[K], TAllRelations>)[] : never : never;
|
|
@@ -484,6 +555,76 @@ type InferRelationalSelectModel<TTable extends AnyTable, TRelations extends Reco
|
|
|
484
555
|
[K in TTable['_']['name']]: TRelations;
|
|
485
556
|
}> = InferSelectModel<TTable> & InferRelationFields<TTable, InferRelationsMap<TRelations>, TAllRelations, TWith>;
|
|
486
557
|
|
|
558
|
+
/**
|
|
559
|
+
* Relations v2 API - Drizzle-style defineRelations with from/to and many-without-one.
|
|
560
|
+
* Use defineRelations() for a single place to define all relations, or defineRelationsPart() to split into parts.
|
|
561
|
+
*/
|
|
562
|
+
|
|
563
|
+
/** Options for one() relation - from/to replace fields/references */
|
|
564
|
+
interface OneRelationOptions {
|
|
565
|
+
from: AnySQLiteColumn | AnySQLiteColumn[];
|
|
566
|
+
to: AnySQLiteColumn | AnySQLiteColumn[];
|
|
567
|
+
optional?: boolean;
|
|
568
|
+
alias?: string;
|
|
569
|
+
}
|
|
570
|
+
/** Reference for through() - column with junction column for many-to-many */
|
|
571
|
+
interface ThroughRef {
|
|
572
|
+
column: AnySQLiteColumn;
|
|
573
|
+
junctionColumn: AnySQLiteColumn;
|
|
574
|
+
junctionTable: AnyTable;
|
|
575
|
+
}
|
|
576
|
+
/** Create a through reference for many-to-many: through(column, junctionColumn, junctionTable) */
|
|
577
|
+
declare function through(column: AnySQLiteColumn, junctionColumn: AnySQLiteColumn, junctionTable: AnyTable): ThroughRef;
|
|
578
|
+
/** Options for many() relation - optional explicit from/to for many-without-one, or through() for many-to-many */
|
|
579
|
+
interface ManyRelationOptions {
|
|
580
|
+
from?: AnySQLiteColumn | AnySQLiteColumn[] | ThroughRef;
|
|
581
|
+
to?: AnySQLiteColumn | AnySQLiteColumn[] | ThroughRef;
|
|
582
|
+
optional?: boolean;
|
|
583
|
+
alias?: string;
|
|
584
|
+
/** Predefined filter: (alias) => Condition. Applied to the joined relation table. */
|
|
585
|
+
where?: (alias: string) => Condition;
|
|
586
|
+
}
|
|
587
|
+
/** Build the r object passed to defineRelations callback */
|
|
588
|
+
declare function buildR<Tables extends Record<string, AnyTable>>(tables: Tables): {
|
|
589
|
+
[K in keyof Tables]: Record<string, AnySQLiteColumn>;
|
|
590
|
+
} & {
|
|
591
|
+
one: {
|
|
592
|
+
[K in keyof Tables]: (opts: OneRelationOptions) => OneRelation<Tables[K]>;
|
|
593
|
+
};
|
|
594
|
+
many: {
|
|
595
|
+
[K in keyof Tables]: (opts?: ManyRelationOptions) => ManyRelation<Tables[K]>;
|
|
596
|
+
};
|
|
597
|
+
};
|
|
598
|
+
type DefineRelationsCallback<Tables extends Record<string, AnyTable>> = (r: ReturnType<typeof buildR>) => {
|
|
599
|
+
[K in keyof Tables]?: Record<string, OneRelation | ManyRelation>;
|
|
600
|
+
};
|
|
601
|
+
/**
|
|
602
|
+
* Define all relations for your schema in one place (v2 API).
|
|
603
|
+
* Uses from/to instead of fields/references, and supports many-without-one.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* import * as schema from './schema'
|
|
608
|
+
* import { defineRelations } from '@type32/tauri-sqlite-orm'
|
|
609
|
+
*
|
|
610
|
+
* export const relations = defineRelations(schema, (r) => ({
|
|
611
|
+
* users: {
|
|
612
|
+
* posts: r.many.posts({ from: r.users.id, to: r.posts.userId }),
|
|
613
|
+
* },
|
|
614
|
+
* posts: {
|
|
615
|
+
* user: r.one.users({ from: r.posts.userId, to: r.users.id }),
|
|
616
|
+
* postTags: r.many.postTags({ from: r.posts.id, to: r.postTags.postId }),
|
|
617
|
+
* },
|
|
618
|
+
* }))
|
|
619
|
+
* ```
|
|
620
|
+
*/
|
|
621
|
+
declare function defineRelations<TSchema extends Record<string, unknown>>(schema: TSchema, callback: DefineRelationsCallback<Record<string, AnyTable>>): Record<string, Record<string, OneRelation | ManyRelation>>;
|
|
622
|
+
/**
|
|
623
|
+
* Define a part of relations - merge multiple parts when passing to TauriORM.
|
|
624
|
+
* Useful for splitting large schema definitions.
|
|
625
|
+
*/
|
|
626
|
+
declare function defineRelationsPart<TSchema extends Record<string, unknown>>(schema: TSchema, callback: DefineRelationsCallback<Record<string, AnyTable>>): Record<string, Record<string, OneRelation | ManyRelation>>;
|
|
627
|
+
|
|
487
628
|
declare class TauriORMError extends Error {
|
|
488
629
|
constructor(message: string);
|
|
489
630
|
}
|
|
@@ -515,4 +656,4 @@ declare class TableNotFoundError extends TauriORMError {
|
|
|
515
656
|
constructor(tableName: string);
|
|
516
657
|
}
|
|
517
658
|
|
|
518
|
-
export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, ColumnNotFoundError, type ColumnOptions, type ColumnValueTypes, type Condition, type DatabaseLike, DeleteQueryBuilder, type ExtractColumnType, type InferInsertModel, type InferRelationalSelectModel, type InferRelationsMap, type InferSelectModel, InsertQueryBuilder, InsertValidationError, ManyRelation, MigrationError, MissingWhereClauseError, type Mode, OneRelation, QueryBuilderError, Relation, type RelationConfig, RelationError, type RelationType, type RelationsBuilder, type SQLAggregate, type SQLCondition, type SQLSubquery, SQLiteColumn, SelectQueryBuilder, Table, TableNotFoundError, TauriDialect, TauriORM, TauriORMError, UpdateQueryBuilder, UpdateValidationError, ValidationError, WithQueryBuilder, alias, and, as, asc, avg, between, blob, boolean, contains, count, countDistinct, desc, endsWith, enumType, eq, eqSubquery, exists, getTableColumns, groupConcat, gt, gtSubquery, gte, gteSubquery, ilike, inArray, integer, isNotNull, isNull, like, lt, ltSubquery, lte, lteSubquery, max, min, ne, neSubquery, not, notExists, notIn, numeric, or, real, relations, scalarSubquery, sqliteTable, startsWith, subquery, sum, text };
|
|
659
|
+
export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, ColumnNotFoundError, type ColumnOptions, type ColumnValueTypes, type Condition, type DatabaseLike, type DefineRelationsCallback, DeleteQueryBuilder, type ExtractColumnType, type InferInsertModel, type InferRelationalSelectModel, type InferRelationsMap, type InferSelectModel, InsertQueryBuilder, InsertValidationError, ManyRelation, type ManyRelationOptions, MigrationError, MissingWhereClauseError, type Mode, OneRelation, type OneRelationOptions, QueryBuilderError, Relation, type RelationConfig, RelationError, type RelationType, type RelationsBuilder, type SQLAggregate, type SQLCondition, type SQLSubquery, SQLiteColumn, SelectQueryBuilder, Table, TableNotFoundError, TauriDialect, TauriORM, TauriORMError, type ThroughRef, UpdateQueryBuilder, UpdateValidationError, ValidationError, WithQueryBuilder, alias, and, as, asc, avg, between, blob, boolean, contains, count, countDistinct, defineRelations, defineRelationsPart, desc, endsWith, enumType, eq, eqSubquery, exists, getTableColumns, groupConcat, gt, gtSubquery, gte, gteSubquery, ilike, inArray, integer, isNotNull, isNull, like, lt, ltSubquery, lte, lteSubquery, max, min, ne, neSubquery, not, notExists, notIn, numeric, or, real, relations, scalarSubquery, sqliteTable, startsWith, subquery, sum, text, through };
|