Merge pull request #28359 from pilagod/sequelize-cursor-pagination

add sequelize-cursor-pagination type declaration
This commit is contained in:
Daniel Rosenwasser
2018-08-26 17:50:37 -07:00
committed by GitHub
4 changed files with 185 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
// Type definitions for sequelize-cursor-pagination 1.1
// Project: https://github.com/Kaltsoon/sequelize-cursor-pagination
// Definitions by: pilagod <https://github.com/pilagod>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped/
// TypeScript Version: 2.9
import Sequelize = require('sequelize');
declare namespace SequelizeCursorPagination {
interface Cursors {
before: string | null;
after: string | null;
hasNext: boolean;
hasPrevious: boolean;
}
interface BasicPaginateOptions<T> {
where?: Sequelize.WhereOptions<T> | Sequelize.where | Sequelize.fn | Array<Sequelize.col | Sequelize.and | Sequelize.or | string>;
attributes?: Sequelize.FindOptionsAttributesArray | { include?: Sequelize.FindOptionsAttributesArray, exclude?: string[] };
include?: Array<Sequelize.Model<any, any> | Sequelize.IncludeOptions>;
limit?: number; // limit the number of records returned
desc?: boolean; // [default: false]
before?: string; // the before cursor
after?: string; // the after cursor
paginationField?: string; // [default: primaryKeyField]
}
interface PaginateRawOptions<T> extends BasicPaginateOptions<T> {
raw: true;
}
interface PaginateInstOptions<T> extends BasicPaginateOptions<T> {
raw?: boolean;
}
interface WithPaginationOptions<MethodName extends string> {
methodName?: MethodName; // [default: 'paginate']
primaryKeyField?: string; // [default: 'id']
}
type WithPaginationModel<MethodName extends string, Model, ModelInst, ModelAttrs> = Model & {
[key in MethodName]: {
(options?: PaginateRawOptions<ModelAttrs>): { results: ModelAttrs[], cursors: Cursors }
(options?: PaginateInstOptions<ModelAttrs>): { results: ModelInst[], cursors: Cursors }
}
};
function withPagination<MethodName extends string = 'paginate'>(options?: WithPaginationOptions<MethodName>): (
<Model extends Sequelize.Model<ModelInst, ModelAttrs>, ModelInst, ModelAttrs>(model: Model) => WithPaginationModel<MethodName, Model, ModelInst, ModelAttrs>
);
}
export = SequelizeCursorPagination.withPagination;

View File

@@ -0,0 +1,103 @@
import Sequelize = require('sequelize');
import withPagination = require('sequelize-cursor-pagination');
interface Data {
id: number;
name: string;
}
interface DataInstance extends Sequelize.Instance<Data>, Data {}
type DataModel = Sequelize.Model<DataInstance, Data>;
const model: DataModel = {} as any ;
/************************************/
/* use default pagination options */
/************************************/
const WithPaginationModel = withPagination({
methodName: 'paginate',
primaryKeyField: 'id'
})<DataModel, DataInstance, Data>(model);
// @CAVEAT: function withPagination has no return value, in order to get the augmented type,
// we need to temporarily store the return value (actually is undefined) to later cast
// the original model to the augmented type to acquire additional paginate function.
const withPaginationModel = (model as typeof WithPaginationModel);
withPaginationModel.paginate();
withPaginationModel.paginate({
where: {
id: 1,
name: 'hello'
},
attributes: ['id', 'name'],
include: [],
before: 'beforeCursor',
after: 'afterCursor',
limit: 10,
desc: true,
paginationField: 'id',
raw: false
});
// when 'raw' option is false or undefined
const insts = withPaginationModel.paginate({
raw: false
});
insts.cursors; // cursors => { after, before, hasNext, hasPrevious }
insts.cursors.after;
insts.cursors.before;
insts.cursors.hasNext;
insts.cursors.hasPrevious;
insts.results; // results => DataInstance[]
insts.results.forEach((inst) => {
const data = inst.get({ plain: true });
const { id, name } = data;
});
// when 'raw' option is true
const raws = withPaginationModel.paginate({
raw: true
});
raws.cursors; // cursors => { after, before, hasNext, hasPrevious }
raws.cursors.after;
raws.cursors.before;
raws.cursors.hasNext;
raws.cursors.hasPrevious;
raws.results; // results => Data[]
raws.results.forEach((data) => {
const { id, name } = data;
});
/***********************************/
/* use custom pagination options */
/***********************************/
const WithPaginationCustomModel = withPagination({
methodName: 'customPaginate',
})<DataModel, DataInstance, Data>(model);
const withPaginationCustomModel = (model as typeof WithPaginationCustomModel);
withPaginationCustomModel.customPaginate();
withPaginationCustomModel.customPaginate({
where: {
id: 1,
name: 'hello'
},
attributes: ['id', 'name'],
include: [],
before: 'beforeCursor',
after: 'afterCursor',
limit: 10,
desc: true,
paginationField: 'id',
raw: false
});

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"sequelize-cursor-pagination-tests.ts"
]
}

View File

@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"no-unnecessary-generics": false
}
}