diff --git a/types/sequelize-cursor-pagination/index.d.ts b/types/sequelize-cursor-pagination/index.d.ts new file mode 100644 index 0000000000..1e9d24901b --- /dev/null +++ b/types/sequelize-cursor-pagination/index.d.ts @@ -0,0 +1,53 @@ +// Type definitions for sequelize-cursor-pagination 1.1 +// Project: https://github.com/Kaltsoon/sequelize-cursor-pagination +// Definitions by: 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 { + where?: Sequelize.WhereOptions | Sequelize.where | Sequelize.fn | Array; + attributes?: Sequelize.FindOptionsAttributesArray | { include?: Sequelize.FindOptionsAttributesArray, exclude?: string[] }; + include?: Array | 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 extends BasicPaginateOptions { + raw: true; + } + + interface PaginateInstOptions extends BasicPaginateOptions { + raw?: boolean; + } + + interface WithPaginationOptions { + methodName?: MethodName; // [default: 'paginate'] + primaryKeyField?: string; // [default: 'id'] + } + + type WithPaginationModel = Model & { + [key in MethodName]: { + (options?: PaginateRawOptions): { results: ModelAttrs[], cursors: Cursors } + (options?: PaginateInstOptions): { results: ModelInst[], cursors: Cursors } + } + }; + + function withPagination(options?: WithPaginationOptions): ( + , ModelInst, ModelAttrs>(model: Model) => WithPaginationModel + ); +} + +export = SequelizeCursorPagination.withPagination; diff --git a/types/sequelize-cursor-pagination/sequelize-cursor-pagination-tests.ts b/types/sequelize-cursor-pagination/sequelize-cursor-pagination-tests.ts new file mode 100644 index 0000000000..b8a4fd95c6 --- /dev/null +++ b/types/sequelize-cursor-pagination/sequelize-cursor-pagination-tests.ts @@ -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 {} + +type DataModel = Sequelize.Model; + +const model: DataModel = {} as any ; + +/************************************/ +/* use default pagination options */ +/************************************/ + +const WithPaginationModel = withPagination({ + methodName: 'paginate', + primaryKeyField: 'id' +})(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', +})(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 +}); diff --git a/types/sequelize-cursor-pagination/tsconfig.json b/types/sequelize-cursor-pagination/tsconfig.json new file mode 100644 index 0000000000..73dbf33a68 --- /dev/null +++ b/types/sequelize-cursor-pagination/tsconfig.json @@ -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" + ] +} diff --git a/types/sequelize-cursor-pagination/tslint.json b/types/sequelize-cursor-pagination/tslint.json new file mode 100644 index 0000000000..71ee04c4e1 --- /dev/null +++ b/types/sequelize-cursor-pagination/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "no-unnecessary-generics": false + } +}