-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.ts
138 lines (114 loc) · 3.25 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Op, ModelStatic, WhereOptions, Model } from 'sequelize';
import { CursorPayload, OrderConfig } from './types';
export const parseCursor = (cursor: string): CursorPayload | null => {
if (!cursor) {
return null;
}
try {
return JSON.parse(Buffer.from(cursor, 'base64').toString('utf8'));
} catch (e) {
return null;
}
};
export const getPrimaryKeyFields = (model: ModelStatic<any>): string[] => {
const primaryKeyFields = Object.entries(model.rawAttributes)
.filter(([, attribute]) => attribute.primaryKey)
.map(([column]) => column);
return primaryKeyFields;
};
const normalizePrimaryKeyField = (
primaryKeyField: string | string[],
): string[] => {
return Array.isArray(primaryKeyField) ? primaryKeyField : [primaryKeyField];
};
const ensurePrimaryKeyFieldInOrder = (
order: OrderConfig,
primaryKeyField: string[],
): OrderConfig => {
const missingPrimaryKeyFields = primaryKeyField.filter(
(pkField) => !order.find(([field]) => field === pkField),
);
const primaryKeyOrder: OrderConfig = missingPrimaryKeyFields.map((field) => [
field,
'ASC',
]);
return [...order, ...primaryKeyOrder];
};
export const normalizeOrder = (
order: any,
primaryKeyField: string | string[],
omitPrimaryKeyFromOrder: boolean,
): OrderConfig => {
const normalizedPrimaryKeyField = normalizePrimaryKeyField(primaryKeyField);
let normalized = [];
if (Array.isArray(order)) {
normalized = order.map((o) => {
if (typeof o === 'string') {
return [o, 'ASC'];
}
if (Array.isArray(o)) {
const [field, direction] = o;
return [field, direction || 'ASC'];
}
return o;
});
}
return omitPrimaryKeyFromOrder
? normalized
: ensurePrimaryKeyFieldInOrder(normalized, normalizedPrimaryKeyField);
};
export const reverseOrder = (order: OrderConfig): OrderConfig => {
return order.map(([field, direction]) => [
field,
direction.toLowerCase() === 'desc' ? 'ASC' : 'DESC',
]);
};
const serializeCursor = (payload: CursorPayload): string => {
return Buffer.from(JSON.stringify(payload)).toString('base64');
};
export const createCursor = <ModelType extends Model>(
instance: ModelType,
order: OrderConfig,
): string => {
const payload = order.map(([field]) => instance.get(field));
return serializeCursor(payload);
};
const isValidCursor = (cursor: CursorPayload, order: OrderConfig): boolean => {
return cursor.length === order.length;
};
const recursivelyGetPaginationQuery = (
order: OrderConfig,
cursor: CursorPayload,
): WhereOptions<any> => {
const currentOp = order[0][1].toLowerCase() === 'desc' ? Op.lt : Op.gt;
if (order.length === 1) {
return {
[order[0][0]]: {
[currentOp]: cursor[0],
},
};
} else {
return {
[Op.or]: [
{
[order[0][0]]: {
[currentOp]: cursor[0],
},
},
{
[order[0][0]]: cursor[0],
...recursivelyGetPaginationQuery(order.slice(1), cursor.slice(1)),
},
],
};
}
};
export const getPaginationQuery = (
order: OrderConfig,
cursor: CursorPayload,
): WhereOptions<any> | null => {
if (!isValidCursor(cursor, order)) {
return null;
}
return recursivelyGetPaginationQuery(order, cursor);
};