Skip to content

Commit 3625ac8

Browse files
authored
Merge pull request feathersjs-ecosystem#502 from j2L4e/seqeulizeConvert
add types for sequelizeConvert hook
2 parents a42975f + 759de92 commit 3625ac8

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

types/index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,24 @@ export function setNow(...fieldNames: string[]): Hook;
456456
*/
457457
export function setSlug(slug: string, fieldName?: string): Hook;
458458

459+
export interface SequelizeConversion {
460+
js: (sqlValue: any) => any;
461+
sql: (jsValue: any) => any;
462+
}
463+
464+
export interface SequelizeConverts<C> {
465+
[name: string]: keyof C | 'boolean' | 'date' | 'json';
466+
}
467+
468+
/**
469+
* {@link https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#SequelizeConvert}
470+
*/
471+
export function sequelizeConvert<C extends {[name: string]: SequelizeConversion}>(
472+
converts: SequelizeConverts<C> | null | undefined | false,
473+
ignores: string[] | null | undefined | false,
474+
conversions?: C
475+
): Hook;
476+
459477
/**
460478
* Filter data or result records using a MongoDB-like selection syntax.
461479
* {@link https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#Sifter}

types/tests.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
existsByDot,
2626
fastJoin,
2727
fgraphql,
28+
FGraphQLHookOptions,
2829
getByDot,
2930
getItems,
3031
iff,
@@ -47,6 +48,7 @@ import {
4748
ResolverMap,
4849
runHook,
4950
runParallel,
51+
sequelizeConvert,
5052
serialize,
5153
setByDot,
5254
setNow,
@@ -63,11 +65,10 @@ import {
6365
validate,
6466
validateSchema,
6567
when,
66-
FGraphQLHookOptions,
6768
} from 'feathers-hooks-common';
68-
import ajv = require('ajv');
6969
import { parse } from 'graphql';
7070
import * as libphonenumberjs from 'libphonenumber-js';
71+
import ajv = require('ajv');
7172

7273
const context1: HookContext = {
7374
type: 'before',
@@ -386,6 +387,19 @@ setNow('createdAt', 'updatedAt');
386387
// $ExpectType Hook
387388
setSlug('storeId');
388389

390+
// $ExpectType Hook
391+
sequelizeConvert ({
392+
aBool: 'boolean',
393+
aDate: 'date',
394+
anObject: 'json',
395+
aNumber: 'strToInt'
396+
}, null, {
397+
strToInt: {
398+
js: (sqlValue: string) => +sqlValue,
399+
sql: (jsValue: number) => `${jsValue}`,
400+
}
401+
});
402+
389403
// $ExpectType Hook
390404
sifter(ctx => item => true);
391405

0 commit comments

Comments
 (0)