├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ └── nodejs.yml ├── .gitignore ├── .husky ├── .gitignore └── pre-commit ├── .npmignore ├── .travis.yml ├── .vscode └── settings.json ├── CHANGELOG.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── redirect ├── package.json └── readme.md ├── src ├── ICustomDatabaseType.ts ├── IsNullable.ts ├── NestedKeysOf.ts ├── NestedRecord.ts ├── NonForeignKeyObjects.ts ├── NonNullableRecursive.ts ├── PartialAndUndefined.ts ├── PropertyTypes.ts ├── SelectableColumnTypes.ts ├── cli.ts ├── decorators.ts ├── index.ts ├── mapObjectToTableObject.ts ├── registerQueryBuilderExtension.ts ├── typedKnex.ts ├── unflatten.ts ├── upgrade │ ├── upgradeRunner.ts │ └── useStringParameters.ts └── validateTables.ts ├── test ├── compilation │ ├── compilationTests.ts │ └── typedQueryBuilder.compilation.test.ts ├── integration │ └── validateTablesTests.ts ├── testTables.ts ├── unit │ ├── mapObjectToTableObjectTests.ts │ ├── registerQueryBuilderExtensionTests.ts │ └── typedQueryBuilderTests.ts └── upgrade │ ├── upgradeProjectJoinOnColumnsToOnTests.ts │ └── upgradeProjectStringParametersTests.ts ├── tsconfig.json └── upgradeTestProjects └── v2-v3-stringParameters ├── src └── typedKnexTypes.ts └── tsconfig.json /.eslintignore: -------------------------------------------------------------------------------- 1 | *.js 2 | node_modules -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | es6: true, 4 | node: true, 5 | }, 6 | extends: ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:prettier/recommended", "prettier"], 7 | parser: "@typescript-eslint/parser", 8 | parserOptions: { 9 | project: ["./tsconfig.json"], 10 | sourceType: "module", 11 | ecmaFeatures: { 12 | jsx: true, // Allows for the parsing of JSX 13 | }, 14 | }, 15 | plugins: ["eslint-plugin-import", "eslint-plugin-jsdoc", "eslint-plugin-no-null", "@typescript-eslint"], 16 | rules: { 17 | "prefer-rest-params": "warn", 18 | "no-unused-vars": "warn", 19 | }, 20 | }; 21 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | node-version: [14.x, 16.x, 18.x] 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Use Node.js ${{ matrix.node-version }} 16 | uses: actions/setup-node@v1 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - name: npm install, build, and test 20 | run: | 21 | npm install 22 | npm run build:dist 23 | npm test 24 | npm run prettier:check 25 | npm run lint 26 | env: 27 | CI: true 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | 13 | # Directory for instrumented libs generated by jscoverage/JSCover 14 | lib-cov 15 | 16 | # Coverage directory used by tools like istanbul 17 | coverage 18 | 19 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 20 | .grunt 21 | 22 | # node-waf configuration 23 | .lock-wscript 24 | 25 | # Compiled binary addons (http://nodejs.org/api/addons.html) 26 | build/Release 27 | 28 | # Dependency directory 29 | node_modules 30 | 31 | # Optional npm cache directory 32 | .npm 33 | 34 | # Optional REPL history 35 | .node_repl_history 36 | 37 | # redis 38 | dump.rdb 39 | 40 | .idea 41 | 42 | /build/ 43 | /dist/ 44 | 45 | .packagejson.checksum 46 | 47 | .env 48 | 49 | /upload/ 50 | 51 | /kue-dist 52 | /src/backend/config/*/*.p12 53 | 54 | # terraform secrets 55 | secret.tfvars 56 | .terraform 57 | terraform.tfstate.backup 58 | terraform/**/*.pem 59 | terraform/**/*.retry 60 | 61 | .tmp-globalize-webpack 62 | 63 | # txcsimport 64 | kettle.properties -------------------------------------------------------------------------------- /.husky/.gitignore: -------------------------------------------------------------------------------- 1 | _ 2 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npx lint-staged 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | tests 7 | example 8 | 9 | lib-cov 10 | *.seed 11 | *.log 12 | *.csv 13 | *.dat 14 | *.out 15 | *.pid 16 | *.gz 17 | 18 | pids 19 | results 20 | 21 | node_modules 22 | 23 | .idea 24 | .vscode 25 | 26 | npm-debug.log 27 | 28 | build 29 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "14" 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true, 3 | "typescript.tsdk": "node_modules/typescript/lib" 4 | } 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 4.14.1 4 | 5 | - Deprecated message 6 | 7 | ## 4.14.0 8 | 9 | - Support `"exactOptionalPropertyTypes": true` by allowing undefined values in `insertItem`,`insertItems`,`updateItem` and `updateItems`. 10 | 11 | ## 4.13.1 12 | 13 | - Chore: correctly deprecate Entity 14 | 15 | ## 4.13.0 16 | 17 | - Chore: Fix links to Knex.js documentation. 18 | - Chore: package upgrades. 19 | 20 | ## 4.12.0 21 | 22 | - Feature: Added bindings argument to `selectRaw` to utilize knex raw string bindings 23 | - Added `orWhereParentheses` 24 | 25 | ## 4.11.0 26 | 27 | - Feature: Added `getKnexQueryBuilder`. 28 | - Feature: Added extra parameter to `validateTables` to filter on specific tables. 29 | 30 | ## 4.10.0 31 | 32 | - Fix: Use knex count return type. See [getCount()](readme.md#getcount) 33 | - Chore: package upgrade. 34 | - Chore: CI tests 14.x, 16.x, 18.x. 35 | - Chore: Remove tslint, add eslint. 36 | 37 | ## 4.9.1 38 | 39 | - Bug: Correct `registerBeforeUpdateTransform` and `registerBeforeInsertTransform` calls. 40 | 41 | ## 4.9.0 42 | 43 | - Chore: Use prepack script instead of prepublish. 44 | - Chore: Properly deprecate mentions of "entity". 45 | - Chore: Deprecate `primary` property of column. 46 | - Feature: Added `getSingleOrUndefined` and `getFirstOrUndefined`. 47 | 48 | ## 4.8.4 49 | 50 | - Deprecation message for functions using the primary key: `findByPrimaryKey`, `delByPrimaryKey`, `updateItemByPrimaryKey`, `updateItemsByPrimaryKey`. 51 | 52 | ## 4.8.3 53 | 54 | - Bug (#51): Don't override query builder in `updateItemWithReturning`, `insertItemWithReturning`, `insertItems` and `updateItemsByPrimaryKey`. 55 | 56 | ## 4.8.2 57 | 58 | - Bug: Export registerQueryBuilderExtension 59 | 60 | ## 4.8.1 61 | 62 | - Bug: Export registerQueryBuilderExtension (NOT FIXED) 63 | 64 | ## 4.8.0 65 | 66 | - Chore: Upgrade packages: Knex.js is now tested at v1.0.4 67 | - Feature: registerQueryBuilderExtension 68 | - Chore: use prettier 69 | 70 | ## 4.7.0 71 | 72 | - Bug: `insertItemWithReturning` and `updateItemWithReturning` didn't use alias. 73 | 74 | ## 4.6.0 75 | 76 | - Attach `tableMetadataKey` and `tableColumns` references to the `Table` class prototypes 77 | - This fixes a rare and unique issue where the same `Column`s and `Table`s can get registered multiple times as a side-effect of overenthusiatic tree-shaking 78 | 79 | ## 4.5.1 80 | 81 | - Republish of 4.5.0 with correct /dist. 82 | 83 | ## 4.5.0 84 | 85 | - Feature: added `updateItemWithReturning`. 86 | - Bug: not using database name on joined column names when using `innerJoinTableOnFunction`. 87 | 88 | ## 4.4.1 89 | 90 | - Bug: result of `getFirstOrNull` and `getSingleOrNull` not always union with `null`. 91 | 92 | ## 4.4.0 93 | 94 | - Feature: use `getColumn` in `whereColumn` in cases with nested subqueries. 95 | 96 | ## 4.3.0 97 | 98 | - Feature: added `getColumnAlias` to get alias of column name to use in `raw` functions. 99 | - Feature: `insertItemWithReturning`. 100 | - Feature: `distinctOn`. 101 | 102 | ## 4.2.1 103 | 104 | - Bug: `where` didn't use alias of joined column. 105 | 106 | ## 4.2.0 107 | 108 | - Added functions `innerJoin` and `leftOuterJoin`. 109 | 110 | ## 4.1.0 111 | 112 | - Upgrade Knex.js to v0.95.0. 113 | - Upgrade TypeScript to v4.2.3. 114 | - Fix example code for `registerBeforeInsertTransform`. 115 | 116 | ## 4.0.0 117 | 118 | - Dropped lamba support. 119 | 120 | ## 3.1.0 121 | 122 | - Return correct object(s) when omitting a select clause. 123 | 124 | ## 3.0.1 125 | 126 | - Fix: `orderBy` with `innerJoinTableOnFunction`. 127 | 128 | ## 3.0.0 129 | 130 | - Dropped support for Node 8 (Knex.js v0.21.0) 131 | - Minimal TypeScript version is 4.1. 132 | - Updated all functions to use strings as parameters and deprecated the use of lambas. Use `npx typed-knex -u string-parameters` to upgrade. 133 | - Better error message when there is a foreign key object, but not a foreign key property. 134 | 135 | ## 2.18.0 136 | 137 | - `findByPrimaryKey` returns `| undefined` instead of `| void`. 138 | 139 | ## 2.17.0 140 | 141 | - Allow `Date` columns in `orderBy`. 142 | 143 | ## 2.16.0 144 | 145 | - Documentation: registerBeforeInsertTransform and registerBeforeUpdateTransform. 146 | - Allow Date columns in `findByPrimaryKey`. 147 | - Tested with Node.js v14.11.0. 148 | - Added "Breaking changes in next major release" in README. 149 | 150 | ## 2.15.0 151 | 152 | - Fixed insert and update queries for tables with column name mappings. 153 | 154 | ## 2.14.0 155 | 156 | - Fixed compilation errors for sub-queries using `code` (`whereExists`, `union`, `selectQuery`) 157 | 158 | ## 2.13.0 159 | 160 | - Added `FlattenOption` to the default import. 161 | - Make `tableName` optional for `@Table`. 162 | 163 | ## 2.12.1 164 | 165 | - Fix `getCount` result. 166 | 167 | ## 2.12.0 168 | 169 | - Fix: export `validateTables`. 170 | - Added `orderByRaw`. 171 | 172 | ## 2.11.0 173 | 174 | - Fix: Table joined with `leftOuterJoinTableOnFunction` could not be referenced in a following `leftOuterJoinTableOnFunction`. 175 | - Add `Table` as alias for `Entity` and `validateTables` for `validateEntities`. 176 | 177 | ## 2.10.0 178 | 179 | - Added `insertSelect` for inserting selects (eg `INSERT INTO table (column) SELECT 'value'`) 180 | 181 | ## 2.9.0 182 | 183 | - Updated on clauses for joins: 184 | - Deprecated `.onColumns`. Use `npx typed-knex -u join-on-columns-to-on` to upgrade. 185 | - Added `.on`, `andOn`, `orOn`, `onVal`, `andOnVal`, `orOnVal`. 186 | 187 | ## 2.8.1 188 | 189 | - Add deprecation warning for `FlattenOption.noFlatten`. 190 | 191 | ## 2.8.0 192 | 193 | - Updated support for `Date` to also include nullable values. 194 | - Added support for array type in entities. 195 | 196 | ## 2.7.0 197 | 198 | - Added `getTableName` and `getColumnName`. 199 | - Added `keepFlat`. 200 | - Upgraded packages. 201 | - Rewritten some internal helper types. 202 | - Added support for `Date` type in entities. 203 | 204 | ## 2.6.0 205 | 206 | - Overloaded `orWhere` and `andWhere` as to also accept an operator. 207 | - Added `orWhereNull` and `orWhereNotNull`. 208 | 209 | ## 2.5.0 210 | 211 | - Added `innerJoinTableOnFunction`. 212 | - Overloaded `where` as to also accept an operator. 213 | 214 | ## 2.4.1 215 | 216 | - Fix: better handling of optional properties. 217 | 218 | ## 2.1.0 219 | 220 | - Added `selectQuery`. 221 | 222 | ## 2.0.0 223 | 224 | Completely different interface, feels like I'm doing a Python 3. 225 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to typed-knex 2 | 3 | We would love for you to contribute to typed-knex and help make it even better than it is today! 4 | As a contributor, here are the guidelines we would like you to follow: 5 | 6 | - [Question or Problem?](#question) 7 | - [Issues and Bugs](#issue) 8 | - [Feature Requests](#feature) 9 | - [Submission Guidelines](#submit) 10 | 11 | ## Got a Question or Problem? 12 | 13 | There are several ways how you can ask your question: 14 | 15 | - You can create a question on [StackOverflow](https://stackoverflow.com/questions/tagged/typed-knex) where the questions should be tagged with tag `typed-knex`. 16 | - You can create issue on [github](https://github.com/wwwouter/typed-knex/issues) 17 | 18 | Preferred way if you create your question on StackOverflow, or create a github issue. 19 | 20 | ## Found a security vulnerability? 21 | 22 | If you find a security vulnerability or something that should be discussed personally, 23 | please contact me within my [email](https://github.com/wwwouter/typed-knex/blob/master/package.json#L10). 24 | 25 | ## Found a Bug? 26 | 27 | If you find a bug in the source code, you can help us by [submitting an issue](#submit-issue) to our 28 | [GitHub Repository](https://github.com/wwwouter/typed-knex). 29 | Even better, you can [submit a Pull Request](#submit-pr) with a fix. 30 | 31 | ## Missing a Feature? 32 | 33 | You can _request_ a new feature by [submitting an issue](#submit-issue) to our GitHub 34 | Repository. If you would like to _implement_ a new feature, please submit an issue with 35 | a proposal for your work first, to be sure that we can use it. 36 | Please consider what kind of change it is: 37 | 38 | - For a **Major Feature**, first open an issue and outline your proposal so that it can be 39 | discussed. This will also allow us to better coordinate our efforts, prevent duplication of work, 40 | and help you to craft the change so that it is successfully accepted into the project. 41 | - **Small Features** can be crafted and directly [submitted as a Pull Request](#submit-pr). 42 | 43 | ## Submission Guidelines 44 | 45 | ### Submitting an Issue 46 | 47 | Before you submit an issue, please search the issue tracker, 48 | maybe an issue for your problem already exists and the discussion might inform you of workarounds readily available. 49 | 50 | We want to fix all the issues as soon as possible, but before fixing a bug we need to reproduce and confirm it. 51 | In order to reproduce bugs we ask you to provide a minimal code snippet that shows a reproduction of the problem. 52 | 53 | You can file new issues by filling out our [new issue form](https://github.com/wwwouter/typed-knex/issues/new). 54 | 55 | ### Submitting a Pull Request (PR) 56 | 57 | Before you submit your Pull Request (PR) consider the following guidelines: 58 | 59 | - Search [GitHub](https://github.com/wwwouter/typed-knex/pulls) for an open or closed PR 60 | that relates to your submission. You don't want to duplicate effort. 61 | - Make your changes in a new git branch: 62 | 63 | ```shell 64 | git checkout -b my-fix-branch master 65 | ``` 66 | 67 | - Create your patch, **including appropriate test cases**. Without tests your PR will not be accepted. 68 | - Add changelog record (short description, link to PR or issues) to the **Unreleased** section of `CHANGELOG.md`. 69 | - Run the full typed-knex test suite (`npm test`) and ensure that all tests pass. 70 | - Commit your changes using a descriptive commit message 71 | 72 | ```shell 73 | git commit -a 74 | ``` 75 | 76 | - Push your branch to GitHub: 77 | 78 | ```shell 79 | git push origin my-fix-branch 80 | ``` 81 | 82 | - In GitHub, send a pull request to `typed-knex:master`. 83 | - If we suggest changes then: 84 | 85 | - Make the required updates. 86 | - Re-run the typed-knex test suites to ensure tests are still passing. 87 | - Rebase your branch and force push to your GitHub repository (this will update your Pull Request): 88 | 89 | ```shell 90 | git rebase master -i 91 | git push -f 92 | ``` 93 | 94 | That's it! Thank you for your contribution! 95 | 96 | #### After your pull request is merged 97 | 98 | After your pull request is merged, you can safely delete your branch and pull the changes 99 | from the main (upstream) repository: 100 | 101 | - Delete the remote branch on GitHub either through the GitHub web UI or your local shell as follows: 102 | 103 | ```shell 104 | git push origin --delete my-fix-branch 105 | ``` 106 | 107 | - Check out the master branch: 108 | 109 | ```shell 110 | git checkout master -f 111 | ``` 112 | 113 | - Delete the local branch: 114 | 115 | ```shell 116 | git branch -D my-fix-branch 117 | ``` 118 | 119 | - Update your master with the latest upstream version: 120 | 121 | ```shell 122 | git pull --ff upstream master 123 | ``` 124 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Issue type:** 2 | 3 | [ ] Question 4 | [ ] Bug report 5 | [ ] Feature request 6 | [ ] Documentation issue 7 | 8 | **Database system/driver:** 9 | 10 | [ ] Postgres 11 | [ ] MSSQL 12 | [ ] MySQL 13 | [ ] MariaDB 14 | [ ] SQLite3 15 | [ ] Oracle 16 | [ ] Amazon Redshift 17 | 18 | **typed-knex version:** 19 | 20 | [ ] `latest` 21 | [ ] `@next` 22 | [ ] `0.x.x` (or put your version here) 23 | 24 | **Knex.js version:** 25 | 26 | **Steps to reproduce or a small repository showing the problem:** 27 | 28 |