Skip to content

Commit 7ba7025

Browse files
committed
table.ts addUniqueKey() freaks out
1 parent c0440fa commit 7ba7025

File tree

6 files changed

+182
-12
lines changed

6 files changed

+182
-12
lines changed

Diff for: TODO.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ function transformBuilder (s : SelectBuilder) {
4646
.joinUsing(table, /*columns*/)
4747
}
4848
query.transform(transformBuilder)
49-
.orderBy(/*other stuff*/);
49+
.orderBy(/*other stuff*/);
50+
51+
-----
52+
53+
+ Proper Unique Key support, rather than just auto-increment keys
54+
55+
Enables convenient (if badly named) `fetchByUk()` method

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"test": "ts-node ./src/main/sandbox/select-builder-v2",
99
"check": "tsc --noEmit --diagnostics",
10-
"check2": "node --max-old-space-size=8192 ./node_modules/typescript/lib/tsc.js --noEmit --noErrorTruncation --diagnostics",
10+
"check2": "node --max-old-space-size=8192 ./node_modules/typescript/lib/tsc.js --noEmit --noErrorTruncation --diagnostics --listFiles",
1111
"build": "(rm -r ./dist || true) && tsc -d"
1212
},
1313
"author": "anyhowstep",

Diff for: src/main/declaration-v2/column/tuple-util.ts

+14
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,18 @@ export namespace ColumnTupleUtil {
4747
}[TupleKeys<TupleT>]
4848
)
4949
);
50+
export type MapToColumnNames<TupleT extends Tuple<AnyColumn>> = (
51+
{
52+
[index in TupleKeys<TupleT>] : (
53+
TupleT[index] extends AnyColumn ?
54+
TupleT[index]["name"] :
55+
never
56+
)
57+
} &
58+
{
59+
"0" : TupleT[0]["name"],
60+
length : TupleLength<TupleT>
61+
} &
62+
string[]
63+
);
5064
}

Diff for: src/main/declaration-v2/table-data/table-data.ts

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export interface TableData {
3636
isMutable : {
3737
[name : string] : true;
3838
};
39+
40+
uniqueKeys : undefined|(Tuple<Tuple<string>>);
3941
}
4042

4143
//The `number` requirement is only a compile-time constraint
@@ -99,4 +101,9 @@ export type IsMutableDelegate<DataT extends TableData, ColumnCollectionT extends
99101
>
100102
>
101103
>
104+
);
105+
export type AddUniqueKeyDelegate<ColumnCollectionT extends ColumnCollection> = (
106+
(columns : ColumnCollectionT) => Tuple<
107+
ColumnCollectionUtil.Columns<ColumnCollectionT>
108+
>
102109
);

Diff for: src/main/declaration-v2/table-data/util.ts

+115-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import {
33
AutoIncrementDelegate,
44
IsGeneratedDelegate,
55
HasDefaultValueDelegate,
6-
IsMutableDelegate
6+
IsMutableDelegate,
7+
AddUniqueKeyDelegate
78
} from "./table-data";
89
import {RemoveKey, ReplaceValue, ReplaceValue3, ReplaceValue4} from "../obj-util";
910
import {ColumnCollection, ColumnCollectionUtil} from "../column-collection";
10-
import {TupleKeys} from "../tuple";
11-
import {AnyColumn} from "../column";
11+
import {Tuple, TupleKeys, TupleWPush} from "../tuple";
12+
import {AnyColumn, ColumnUtil, ColumnTupleUtil} from "../column";
1213

1314
export namespace TableDataUtil {
1415
export type AutoIncrement<
@@ -303,4 +304,114 @@ export namespace TableDataUtil {
303304
isMutable : {},
304305
} as any;
305306
}
306-
}
307+
export type AddUniqueKey<
308+
DataT extends TableData,
309+
ColumnCollectionT extends ColumnCollection,
310+
AddUniqueKeyDelegateT extends AddUniqueKeyDelegate<ColumnCollectionT>
311+
> = (
312+
ReplaceValue<
313+
DataT,
314+
"uniqueKeys",
315+
DataT["uniqueKeys"] extends Tuple<Tuple<string>> ?
316+
(
317+
TupleWPush<
318+
Tuple<string>,
319+
DataT["uniqueKeys"],
320+
ColumnTupleUtil.MapToColumnNames<
321+
ReturnType<AddUniqueKeyDelegateT>
322+
>
323+
>
324+
) :
325+
undefined
326+
>
327+
);
328+
export function addUniqueKey<
329+
DataT extends TableData,
330+
ColumnCollectionT extends ColumnCollection,
331+
AddUniqueKeyDelegateT extends AddUniqueKeyDelegate<ColumnCollectionT>
332+
> (
333+
data : DataT,
334+
columnCollection : ColumnCollectionT,
335+
delegate : AddUniqueKeyDelegateT
336+
) : (
337+
AddUniqueKey<
338+
DataT,
339+
ColumnCollectionT,
340+
AddUniqueKeyDelegateT
341+
>
342+
) {
343+
const uniqueKey = delegate(columnCollection);
344+
ColumnCollectionUtil.assertHasColumns(columnCollection, uniqueKey);
345+
return {
346+
...(data as any),
347+
uniqueKeys : (data.uniqueKeys == undefined) ?
348+
undefined :
349+
data.uniqueKeys.push(uniqueKey.map(c => c.name) as any)
350+
} as any;
351+
}
352+
353+
export type WithTableAlias<DataT extends TableData, TableAliasT extends string> = (
354+
ReplaceValue<
355+
DataT,
356+
"autoIncrement",
357+
(
358+
DataT["autoIncrement"] extends AnyColumn ?
359+
ColumnUtil.WithTableAlias<DataT["autoIncrement"], TableAliasT> :
360+
undefined
361+
)
362+
>
363+
/*{
364+
autoIncrement : (
365+
DataT["autoIncrement"] extends AnyColumn ?
366+
ColumnUtil.WithTableAlias<DataT["autoIncrement"], TableAliasT> :
367+
undefined
368+
),
369+
isGenerated : DataT["isGenerated"],
370+
hasDefaultValue : DataT["hasDefaultValue"],
371+
isMutable : DataT["isMutable"],
372+
uniqueKeys : (
373+
DataT["uniqueKeys"] extends Tuple<Tuple<AnyColumn>> ?
374+
UniqueKeysWithTableAlias<DataT["uniqueKeys"], TableAliasT> :
375+
undefined|Tuple<Tuple<AnyColumn>>
376+
),
377+
}*/
378+
/*
379+
{
380+
isGenerated : DataT["isGenerated"],
381+
hasDefaultValue : DataT["hasDefaultValue"],
382+
isMutable : DataT["isMutable"],
383+
} &
384+
(
385+
DataT["autoIncrement"] extends AnyColumn ?
386+
{
387+
autoIncrement : ColumnUtil.WithTableAlias<DataT["autoIncrement"], TableAliasT>,
388+
} :
389+
{
390+
autoIncrement : undefined,
391+
}
392+
) &
393+
(
394+
DataT["uniqueKeys"] extends Tuple<Tuple<AnyColumn>> ?
395+
{
396+
uniqueKeys : UniqueKeysWithTableAlias<DataT["uniqueKeys"], TableAliasT>,
397+
} :
398+
{
399+
uniqueKeys : undefined,
400+
}
401+
)
402+
*/
403+
);
404+
export function withTableAlias<
405+
DataT extends TableData,
406+
TableAliasT extends string
407+
> (data : DataT, tableAlias : TableAliasT) : (
408+
WithTableAlias<DataT, TableAliasT>
409+
) {
410+
return {
411+
...(data as any),
412+
autoIncrement : (data.autoIncrement == undefined) ?
413+
undefined :
414+
ColumnUtil.withTableAlias(data.autoIncrement, tableAlias),
415+
} as any;
416+
}
417+
}

Diff for: src/main/declaration-v2/table/table.ts

+38-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
IsGeneratedDelegate,
88
HasDefaultValueDelegate,
99
IsMutableDelegate,
10-
TableDataUtil
10+
TableDataUtil,
11+
//AddUniqueKeyDelegate
1112
} from "../table-data";
1213
import * as fieldUtil from "../field-util";
1314

@@ -148,7 +149,8 @@ export class Table<
148149
newAlias,
149150
this.name,
150151
ColumnCollectionUtil.withTableAlias(this.columns, newAlias),
151-
this.data
152+
TableDataUtil.withTableAlias(this.data, newAlias)
153+
//this.data
152154
);
153155
}
154156

@@ -160,14 +162,14 @@ export class Table<
160162
ColumnCollectionT,
161163
NewNameT
162164
>,
163-
DataT
165+
TableDataUtil.WithTableAlias<DataT, NewNameT>
164166
>
165167
) {
166168
return new Table(
167169
newName,
168170
newName,
169171
ColumnCollectionUtil.withTableAlias(this.columns, newName),
170-
this.data
172+
TableDataUtil.withTableAlias(this.data, newName)
171173
);
172174
}
173175
addColumns<RawColumnCollectionT extends RawColumnCollection> (
@@ -223,6 +225,33 @@ export class Table<
223225
this.data
224226
);
225227
}
228+
//This method causes `tsc` to not terminate if uncommented
229+
/*
230+
addUniqueKey<
231+
AddUniqueKeyDelegateT extends AddUniqueKeyDelegate<ColumnCollectionT>
232+
> (delegate : AddUniqueKeyDelegateT) : (
233+
Table<
234+
AliasT,
235+
NameT,
236+
ColumnCollectionT,
237+
TableDataUtil.AddUniqueKey<
238+
DataT,
239+
ColumnCollectionT,
240+
AddUniqueKeyDelegateT
241+
>
242+
>
243+
) {
244+
return new Table(
245+
this.alias,
246+
this.name,
247+
this.columns,
248+
TableDataUtil.addUniqueKey(
249+
this.data,
250+
this.columns,
251+
delegate
252+
)
253+
) as any;
254+
}*/
226255
}
227256

228257
export type AnyTable = Table<string, string, ColumnCollection, TableData>;
@@ -248,7 +277,8 @@ export function table<
248277
},
249278
isMutable : {
250279
[name in Extract<keyof RawColumnCollectionT, string>] : true
251-
}
280+
},
281+
uniqueKeys : undefined,
252282
}
253283
>
254284
);
@@ -273,7 +303,8 @@ export function table<
273303
},
274304
isMutable : {
275305
[name in Extract<keyof fieldUtil.FieldsToObject<TupleT>, string>] : true
276-
}
306+
},
307+
uniqueKeys : undefined,
277308
}
278309
>
279310
);
@@ -306,6 +337,7 @@ export function table (
306337
isGenerated : {},
307338
hasDefaultValue : hasDefaultValue,
308339
isMutable : isMutable,
340+
uniqueKeys : undefined,
309341
}
310342
);
311343
}

0 commit comments

Comments
 (0)