orchid-orm 1.3.15 → 1.4.16
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/CHANGELOG.md +20 -0
- package/dist/index.d.ts +59 -54
- package/dist/index.esm.js +777 -547
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +776 -546
- package/dist/index.js.map +1 -1
- package/jest-setup.ts +4 -0
- package/package.json +8 -4
- package/src/appCodeUpdater/appCodeUpdater.ts +19 -0
- package/src/appCodeUpdater/fileChanges.ts +41 -0
- package/src/appCodeUpdater/testUtils.ts +31 -0
- package/src/appCodeUpdater/tsUtils.ts +137 -0
- package/src/appCodeUpdater/updateMainFile.test.ts +230 -0
- package/src/appCodeUpdater/updateMainFile.ts +163 -0
- package/src/appCodeUpdater/updateTableFile.ts +19 -0
- package/src/index.ts +5 -1
- package/src/orm.test.ts +13 -13
- package/src/orm.ts +21 -21
- package/src/relations/belongsTo.test.ts +1 -1
- package/src/relations/belongsTo.ts +291 -186
- package/src/relations/hasAndBelongsToMany.test.ts +1 -1
- package/src/relations/hasAndBelongsToMany.ts +292 -218
- package/src/relations/hasMany.test.ts +16 -10
- package/src/relations/hasMany.ts +243 -172
- package/src/relations/hasOne.test.ts +10 -10
- package/src/relations/hasOne.ts +211 -138
- package/src/relations/relations.ts +85 -77
- package/src/relations/utils.ts +154 -4
- package/src/repo.test.ts +29 -29
- package/src/repo.ts +6 -6
- package/src/{model.test.ts → table.test.ts} +15 -15
- package/src/{model.ts → table.ts} +17 -17
- package/src/test-utils/test-db.ts +15 -15
- package/src/test-utils/{test-models.ts → test-tables.ts} +42 -42
- package/src/transaction.test.ts +1 -1
- package/src/transaction.ts +4 -4
- package/src/utils.ts +9 -0
- package/tsconfig.json +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# orchid-orm
|
|
2
2
|
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 3f25b4d: Rename all model words to table words, because all models here are not models in OOP meaning
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [3f25b4d]
|
|
12
|
+
- pqb@0.8.0
|
|
13
|
+
- rake-db@2.2.0
|
|
14
|
+
|
|
15
|
+
## 1.3.16
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Refactor create and update of relations
|
|
20
|
+
- Updated dependencies
|
|
21
|
+
- pqb@0.7.13
|
|
22
|
+
|
|
3
23
|
## 1.3.15
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,44 @@
|
|
|
1
|
-
import { BelongsToRelation, HasOneRelation, Db, Adapter, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption,
|
|
1
|
+
import { BelongsToRelation, HasManyRelation, HasOneRelation, Db, Adapter, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, HasAndBelongsToManyRelation, Query, defaultsKey, EmptyObject, BaseRelation, RelationQuery, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsAll, ColumnTypesBase, ColumnsShape, ColumnShapeOutput, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
|
|
2
|
+
import { AppCodeUpdater } from 'rake-db';
|
|
2
3
|
|
|
3
4
|
interface BelongsTo extends RelationThunkBase {
|
|
4
5
|
type: 'belongsTo';
|
|
5
6
|
returns: 'one';
|
|
6
7
|
options: BelongsToRelation['options'];
|
|
7
8
|
}
|
|
8
|
-
declare type BelongsToInfo<T extends
|
|
9
|
+
declare type BelongsToInfo<T extends Table, Relation extends BelongsTo, FK extends string = Relation['options']['foreignKey']> = {
|
|
9
10
|
params: Record<FK, T['columns']['shape'][FK]['type']>;
|
|
10
11
|
populate: never;
|
|
11
12
|
chainedCreate: false;
|
|
12
13
|
chainedDelete: false;
|
|
13
14
|
};
|
|
14
15
|
|
|
16
|
+
interface HasMany extends RelationThunkBase {
|
|
17
|
+
type: 'hasMany';
|
|
18
|
+
returns: 'many';
|
|
19
|
+
options: HasManyRelation['options'];
|
|
20
|
+
}
|
|
21
|
+
declare type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation extends HasMany> = {
|
|
22
|
+
params: Relation['options'] extends {
|
|
23
|
+
primaryKey: string;
|
|
24
|
+
} ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends {
|
|
25
|
+
through: string;
|
|
26
|
+
} ? RelationInfo<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
|
|
27
|
+
populate: Relation['options'] extends {
|
|
28
|
+
foreignKey: string;
|
|
29
|
+
} ? Relation['options']['foreignKey'] : never;
|
|
30
|
+
chainedCreate: Relation['options'] extends {
|
|
31
|
+
primaryKey: string;
|
|
32
|
+
} ? true : false;
|
|
33
|
+
chainedDelete: true;
|
|
34
|
+
};
|
|
35
|
+
|
|
15
36
|
interface HasOne extends RelationThunkBase {
|
|
16
37
|
type: 'hasOne';
|
|
17
38
|
returns: 'one';
|
|
18
39
|
options: HasOneRelation['options'];
|
|
19
40
|
}
|
|
20
|
-
declare type HasOneInfo<T extends
|
|
41
|
+
declare type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation extends HasOne> = {
|
|
21
42
|
params: Relation['options'] extends {
|
|
22
43
|
primaryKey: string;
|
|
23
44
|
} ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends {
|
|
@@ -36,47 +57,27 @@ declare function transaction<T extends {
|
|
|
36
57
|
$queryBuilder: Db;
|
|
37
58
|
}, Result>(this: T, fn: (db: T) => Promise<Result>): Promise<Result>;
|
|
38
59
|
|
|
39
|
-
declare type OrchidORM<T extends
|
|
40
|
-
[K in keyof T]:
|
|
60
|
+
declare type OrchidORM<T extends TableClasses> = {
|
|
61
|
+
[K in keyof T]: DbTable<T[K]>;
|
|
41
62
|
} & {
|
|
42
63
|
$transaction: typeof transaction;
|
|
43
64
|
$adapter: Adapter;
|
|
44
65
|
$queryBuilder: Db;
|
|
45
66
|
$close(): Promise<void>;
|
|
46
67
|
};
|
|
47
|
-
declare const orchidORM: <T extends
|
|
68
|
+
declare const orchidORM: <T extends TableClasses>({ log, logger, autoPreparedStatements, noPrimaryKey, ...options }: (Omit<AdapterOptions, "log"> | {
|
|
48
69
|
adapter: Adapter;
|
|
49
70
|
}) & QueryLogOptions & {
|
|
50
71
|
autoPreparedStatements?: boolean | undefined;
|
|
51
72
|
noPrimaryKey?: NoPrimaryKeyOption | undefined;
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
interface HasMany extends RelationThunkBase {
|
|
55
|
-
type: 'hasMany';
|
|
56
|
-
returns: 'many';
|
|
57
|
-
options: HasManyRelation['options'];
|
|
58
|
-
}
|
|
59
|
-
declare type HasManyInfo<T extends Model, Relations extends RelationThunks, Relation extends HasMany> = {
|
|
60
|
-
params: Relation['options'] extends {
|
|
61
|
-
primaryKey: string;
|
|
62
|
-
} ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends {
|
|
63
|
-
through: string;
|
|
64
|
-
} ? RelationInfo<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
|
|
65
|
-
populate: Relation['options'] extends {
|
|
66
|
-
foreignKey: string;
|
|
67
|
-
} ? Relation['options']['foreignKey'] : never;
|
|
68
|
-
chainedCreate: Relation['options'] extends {
|
|
69
|
-
primaryKey: string;
|
|
70
|
-
} ? true : false;
|
|
71
|
-
chainedDelete: true;
|
|
72
|
-
};
|
|
73
|
+
}, tables: T) => OrchidORM<T>;
|
|
73
74
|
|
|
74
75
|
interface HasAndBelongsToMany extends RelationThunkBase {
|
|
75
76
|
type: 'hasAndBelongsToMany';
|
|
76
77
|
returns: 'many';
|
|
77
78
|
options: HasAndBelongsToManyRelation['options'];
|
|
78
79
|
}
|
|
79
|
-
declare type HasAndBelongsToManyInfo<T extends
|
|
80
|
+
declare type HasAndBelongsToManyInfo<T extends Table, Relation extends HasAndBelongsToMany> = {
|
|
80
81
|
params: Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']>;
|
|
81
82
|
populate: never;
|
|
82
83
|
chainedCreate: true;
|
|
@@ -86,66 +87,64 @@ declare type HasAndBelongsToManyInfo<T extends Model, Relation extends HasAndBel
|
|
|
86
87
|
interface RelationThunkBase {
|
|
87
88
|
type: string;
|
|
88
89
|
returns: 'one' | 'many';
|
|
89
|
-
fn():
|
|
90
|
+
fn(): TableClass;
|
|
90
91
|
options: BaseRelation['options'];
|
|
91
92
|
}
|
|
92
93
|
declare type RelationThunk = BelongsTo | HasOne | HasMany | HasAndBelongsToMany;
|
|
93
94
|
declare type RelationThunks = Record<string, RelationThunk>;
|
|
94
|
-
declare type Relation<T extends
|
|
95
|
+
declare type Relation<T extends Table, Relations extends RelationThunks, K extends keyof Relations, M extends Query = DbTable<ReturnType<Relations[K]['fn']>>, Info extends RelationInfo = RelationInfo<T, Relations, Relations[K]>> = {
|
|
95
96
|
type: Relations[K]['type'];
|
|
96
97
|
returns: Relations[K]['returns'];
|
|
97
98
|
key: K;
|
|
98
|
-
|
|
99
|
+
table: M;
|
|
99
100
|
query: M;
|
|
100
101
|
joinQuery(fromQuery: Query, toQuery: Query): Query;
|
|
101
102
|
defaults: Info['populate'];
|
|
102
103
|
nestedCreateQuery: [Info['populate']] extends [never] ? M : M & {
|
|
103
104
|
[defaultsKey]: Record<Info['populate'], true>;
|
|
104
105
|
};
|
|
105
|
-
nestedInsert: BaseRelation['nestedInsert'];
|
|
106
|
-
nestedUpdate: BaseRelation['nestedUpdate'];
|
|
107
106
|
primaryKey: string;
|
|
108
107
|
options: Relations[K]['options'];
|
|
109
108
|
};
|
|
110
|
-
declare type
|
|
111
|
-
declare type RelationInfo<T extends
|
|
112
|
-
declare type MapRelation<T extends
|
|
109
|
+
declare type RelationScopeOrTable<Relation extends RelationThunkBase> = Relation['options']['scope'] extends (q: Query) => Query ? ReturnType<Relation['options']['scope']> : DbTable<ReturnType<Relation['fn']>>;
|
|
110
|
+
declare type RelationInfo<T extends Table = Table, Relations extends RelationThunks = RelationThunks, Relation extends RelationThunk = RelationThunk> = Relation extends BelongsTo ? BelongsToInfo<T, Relation> : Relation extends HasOne ? HasOneInfo<T, Relations, Relation> : Relation extends HasMany ? HasManyInfo<T, Relations, Relation> : Relation extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, Relation> : never;
|
|
111
|
+
declare type MapRelation<T extends Table, Relations extends RelationThunks, RelationName extends keyof Relations, Relation extends RelationThunk = Relations[RelationName], RelatedQuery extends Query = RelationScopeOrTable<Relation>, Info extends {
|
|
113
112
|
params: Record<string, unknown>;
|
|
114
113
|
populate: string;
|
|
115
114
|
chainedCreate: boolean;
|
|
116
115
|
chainedDelete: boolean;
|
|
117
116
|
} = RelationInfo<T, Relations, Relation>> = RelationQuery<RelationName, Info['params'], Info['populate'], Relation['returns'] extends 'one' ? Relation['options']['required'] extends true ? SetQueryReturnsOne<RelatedQuery> : SetQueryReturnsOneOptional<RelatedQuery> : SetQueryReturnsAll<RelatedQuery>, Relation['options']['required'] extends boolean ? Relation['options']['required'] : false, Info['chainedCreate'], Info['chainedDelete']>;
|
|
118
|
-
declare type MapRelations<T extends
|
|
117
|
+
declare type MapRelations<T extends Table> = 'relations' extends keyof T ? T['relations'] extends RelationThunks ? {
|
|
119
118
|
[K in keyof T['relations']]: MapRelation<T, T['relations'], K>;
|
|
120
119
|
} : EmptyObject : EmptyObject;
|
|
121
120
|
|
|
122
|
-
declare type
|
|
123
|
-
declare type
|
|
124
|
-
declare type
|
|
121
|
+
declare type TableClass<T extends Table = Table> = new () => T;
|
|
122
|
+
declare type TableClasses = Record<string, TableClass>;
|
|
123
|
+
declare type TableToDb<T extends Table> = Db<T['table'], T['columns']['shape'], 'relations' extends keyof T ? T['relations'] extends RelationThunks ? {
|
|
125
124
|
[K in keyof T['relations']]: Relation<T, T['relations'], K>;
|
|
126
125
|
} : Query['relations'] : Query['relations'], T['columnTypes']> & {
|
|
127
126
|
definedAs: string;
|
|
128
|
-
db: OrchidORM<
|
|
127
|
+
db: OrchidORM<TableClasses>;
|
|
129
128
|
};
|
|
130
|
-
declare type
|
|
131
|
-
declare type
|
|
129
|
+
declare type DbTable<T extends TableClass> = TableToDb<InstanceType<T>> & Omit<MapRelations<InstanceType<T>>, keyof Query>;
|
|
130
|
+
declare type TableConfig = {
|
|
132
131
|
shape: ColumnsShape;
|
|
133
132
|
type: unknown;
|
|
134
133
|
};
|
|
135
|
-
declare type ScopeFn<Related extends
|
|
136
|
-
declare type
|
|
134
|
+
declare type ScopeFn<Related extends TableClass, Scope extends Query> = (q: DbTable<Related>) => Scope;
|
|
135
|
+
declare type Table = {
|
|
137
136
|
table: string;
|
|
138
|
-
columns:
|
|
137
|
+
columns: TableConfig;
|
|
139
138
|
schema?: string;
|
|
140
139
|
columnTypes: ColumnTypesBase;
|
|
141
140
|
noPrimaryKey?: boolean;
|
|
142
141
|
};
|
|
143
|
-
declare const
|
|
142
|
+
declare const createBaseTable: <CT extends ColumnTypesBase>(options: {
|
|
144
143
|
columnTypes: CT;
|
|
145
144
|
}) => {
|
|
146
145
|
new (): {
|
|
147
146
|
table: string;
|
|
148
|
-
columns:
|
|
147
|
+
columns: TableConfig;
|
|
149
148
|
schema?: string | undefined;
|
|
150
149
|
columnTypes: CT;
|
|
151
150
|
noPrimaryKey?: boolean | undefined;
|
|
@@ -153,7 +152,7 @@ declare const createModel: <CT extends ColumnTypesBase>(options: {
|
|
|
153
152
|
shape: T;
|
|
154
153
|
type: ColumnShapeOutput<T>;
|
|
155
154
|
};
|
|
156
|
-
belongsTo<Self extends any, Related extends
|
|
155
|
+
belongsTo<Self extends any, Related extends TableClass<Table>, Scope extends Query, Options extends {
|
|
157
156
|
primaryKey: keyof InstanceType<Related>["columns"]["shape"];
|
|
158
157
|
foreignKey: keyof Self["columns"]["shape"];
|
|
159
158
|
scope?: ScopeFn<Related, Scope> | undefined;
|
|
@@ -164,7 +163,7 @@ declare const createModel: <CT extends ColumnTypesBase>(options: {
|
|
|
164
163
|
fn: () => Related;
|
|
165
164
|
options: Options;
|
|
166
165
|
};
|
|
167
|
-
hasOne<Self_1 extends any, Related_1 extends
|
|
166
|
+
hasOne<Self_1 extends any, Related_1 extends TableClass<Table>, Scope_1 extends Query, Through extends string, Source extends string, Options_1 extends ({
|
|
168
167
|
primaryKey: keyof Self_1["columns"]["shape"];
|
|
169
168
|
foreignKey: keyof InstanceType<Related_1>["columns"]["shape"];
|
|
170
169
|
} | {
|
|
@@ -179,7 +178,7 @@ declare const createModel: <CT extends ColumnTypesBase>(options: {
|
|
|
179
178
|
fn: () => Related_1;
|
|
180
179
|
options: Options_1;
|
|
181
180
|
};
|
|
182
|
-
hasMany<Self_2 extends any, Related_2 extends
|
|
181
|
+
hasMany<Self_2 extends any, Related_2 extends TableClass<Table>, Scope_2 extends Query, Through_1 extends string, Source_1 extends string, Options_2 extends ({
|
|
183
182
|
primaryKey: keyof Self_2["columns"]["shape"];
|
|
184
183
|
foreignKey: keyof InstanceType<Related_2>["columns"]["shape"];
|
|
185
184
|
} | {
|
|
@@ -194,7 +193,7 @@ declare const createModel: <CT extends ColumnTypesBase>(options: {
|
|
|
194
193
|
fn: () => Related_2;
|
|
195
194
|
options: Options_2;
|
|
196
195
|
};
|
|
197
|
-
hasAndBelongsToMany<Self_3 extends any, Related_3 extends
|
|
196
|
+
hasAndBelongsToMany<Self_3 extends any, Related_3 extends TableClass<Table>, Scope_3 extends Query, Options_3 extends {
|
|
198
197
|
primaryKey: keyof Self_3["columns"]["shape"];
|
|
199
198
|
associationPrimaryKey: keyof InstanceType<Related_3>["columns"]["shape"];
|
|
200
199
|
foreignKey: string;
|
|
@@ -228,6 +227,12 @@ declare type Repo<T extends Query, Methods extends MethodsBase<T>, Mapped = MapM
|
|
|
228
227
|
table: T['table'];
|
|
229
228
|
shape: T['shape'];
|
|
230
229
|
}>(q: Q) => Q & Mapped) & T & Mapped;
|
|
231
|
-
declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(
|
|
230
|
+
declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(table: T, methods: Methods) => Repo<T, Methods, MapMethods<T, Methods>>;
|
|
231
|
+
|
|
232
|
+
declare type AppCodeUpdaterConfig = {
|
|
233
|
+
tablePath(tableName: string): string;
|
|
234
|
+
mainFilePath: string;
|
|
235
|
+
};
|
|
236
|
+
declare const appCodeUpdater: () => AppCodeUpdater;
|
|
232
237
|
|
|
233
|
-
export {
|
|
238
|
+
export { AppCodeUpdaterConfig, DbTable, MapMethods, MapQueryMethods, MethodsBase, OrchidORM, QueryMethods, Repo, Table, TableClass, TableClasses, TableToDb, appCodeUpdater, createBaseTable, createRepo, orchidORM };
|