diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index e0765e04bc..5b894f7fbe 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -29,7 +29,11 @@ If issue is about TypeScript definitions, tag @ lorefnon. # Missing / erroneus documentation -Send issue to documentation repo, or fix it and send PR https://github.com/knex/documentation +1. What is the missing or erroneus part of the documentation + +2. What is the correct information + +3. If you have time, you can also make a pull request to fix the documentation # Questions about how to use knex diff --git a/.github/workflows/docs-deploy.yml b/.github/workflows/docs-deploy.yml new file mode 100644 index 0000000000..87415df36a --- /dev/null +++ b/.github/workflows/docs-deploy.yml @@ -0,0 +1,52 @@ +name: Knex / Documentation deployment + +on: + push: + branches: + - 'main' + paths: + - docs/** + +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: true + +jobs: + deploy: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./docs + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16.x + + # cache node_modules + - name: Restore cached dependencies + uses: actions/cache@v3 + id: npm-cache + with: + path: | + **/node_modules + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-npm- + + # install dependencies if the cache did not hit + - name: Install dependencies + if: steps.npm-cache.outputs.cache-hit != 'true' + run: npm ci + + - name: Build documentation + run: npm run build + + - name: Deploy to gh-pages + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: .vitepress/dist + cname: knexjs.org + force_orphan: true diff --git a/.gitignore b/.gitignore index 4f4c5a5ac2..c5673b7994 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ yarn.lock package-lock.json +!docs/package-lock.json raw *.sw? .idea diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6540defd4a..bf2de4b3d4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,9 +18,7 @@ ## Documentation -Documentation is no longer maintained in knex master repository. All the documentation pull requests should be sent to https://github.com/knex/documentation - -Documentation pull requests should not be merged before knex version which has the new documented feature is released. +Documentation is maintained in the `/docs` folder. every pull request that changes the public API should also update the docs ## I would like to add support for new dialect to knex, is it possible? diff --git a/docs/.DS_Store b/docs/.DS_Store new file mode 100644 index 0000000000..65c80e7e40 Binary files /dev/null and b/docs/.DS_Store differ diff --git a/docs/.gitignore b/docs/.gitignore new file mode 100644 index 0000000000..d342815824 --- /dev/null +++ b/docs/.gitignore @@ -0,0 +1,2 @@ +node_modules +.vitepress/dist diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts new file mode 100644 index 0000000000..7c40d5336a --- /dev/null +++ b/docs/.vitepress/config.ts @@ -0,0 +1,105 @@ +import { defineConfig } from 'vitepress'; +import KnexDialectsPlugins from './knexDialects'; + +export default defineConfig({ + title: 'Knex.js', + description: 'Beta knex.js documentation.', + base: '/', + srcDir: 'src', + head: [['link', { rel: 'icon', type: 'image/png', href: '/knex-logo.png' }]], + themeConfig: { + logo: '/knex-logo.png', + repo: 'knex/knex', + docsRepo: 'knex/knex', + docsDir: 'docs/src', + docsBranch: 'main', + editLinks: true, + editLinkText: 'Edit this page on GitHub', + lastUpdated: 'Last Updated', + nav: [ + { text: 'Guide', link: '/guide/', activeMatch: '^/guide/' }, + { + text: 'F.A.Q.', + link: '/faq/', + }, + { + text: 'Changelog', + link: '/changelog.html', + }, + ], + sidebar: { + '/guide/': getGuideSidebar(), + '/faq/': getFaqSidebar(), + }, + algolia: { + appId: 'V7E3EHUPD6', + apiKey: '44b5077836c1c8fba0f364383dde7fb4', + indexName: 'knex', + initialQuery: '', + }, + }, + vite: { + plugins: [KnexDialectsPlugins()], + }, +}); + +function getGuideSidebar() { + return [ + { + text: 'Installation', + link: '/guide/', + }, + { + text: 'Query Builder', + link: '/guide/query-builder', + }, + { + text: 'Transactions', + link: '/guide/transactions', + }, + { + text: 'Schema Builder', + link: '/guide/schema-builder', + }, + { + text: 'Raw', + link: '/guide/raw', + }, + { + text: 'Ref', + link: '/guide/ref', + }, + { + text: 'Utility', + link: '/guide/utility', + }, + { + text: 'Interfaces', + link: '/guide/interfaces', + }, + { + text: 'Migrations', + link: '/guide/migrations', + }, + { + text: 'Extending', + link: '/guide/extending', + }, + ]; +} +function getFaqSidebar() { + return [ + { + text: 'F.A.Q.', + link: '/faq/', + }, + { + text: 'Recipes', + link: '/faq/recipes', + }, + { + text: 'Support', + link: '/faq/support', + }, + ]; +} diff --git a/docs/.vitepress/knexDialects.ts b/docs/.vitepress/knexDialects.ts new file mode 100644 index 0000000000..7cc9d26596 --- /dev/null +++ b/docs/.vitepress/knexDialects.ts @@ -0,0 +1,46 @@ +import Knex from 'knex'; +import type { PluginOption } from 'vite'; + +const dialects = { + 'better-sqlite3': Knex({ client: 'better-sqlite3' }), + cockroachdb: Knex({ client: 'cockroachdb' }), + mssql: Knex({ client: 'mssql' }), + mysql: Knex({ client: 'mysql' }), + mysql2: Knex({ client: 'mysql2' }), + oracledb: Knex({ client: 'oracledb' }), + pgnative: Knex({ client: 'pgnative' }), + postgres: Knex({ client: 'postgres' }), + redshift: Knex({ client: 'redshift' }), + sqlite3: Knex({ client: 'sqlite3' }), +}; + +export default function knexDialects(): PluginOption { + const regex = //gi; + + return { + name: 'transform-file', + enforce: 'pre', + + transform(src, id) { + if (id.endsWith('.md')) { + const matches = src.matchAll(regex); + for (const match of matches) { + let markdown = ''; + const getCode = Function('knex', `return knex.raw(${match[1]});`); + + for (const dialect in dialects) { + const knex = dialects[dialect]; + const { sql } = getCode(knex); + const output = sql.toString(); + + markdown += `
\n\n\`\`\`sql\n${output}\n\`\`\`\n\n
\n`; + } + + src = src.replace(match[0], markdown); + } + } + + return src; + }, + }; +} diff --git a/docs/.vitepress/theme/AlgoliaSearchBox.vue b/docs/.vitepress/theme/AlgoliaSearchBox.vue new file mode 100644 index 0000000000..5abb4196f2 --- /dev/null +++ b/docs/.vitepress/theme/AlgoliaSearchBox.vue @@ -0,0 +1,191 @@ + + + + + diff --git a/docs/.vitepress/theme/Layout.vue b/docs/.vitepress/theme/Layout.vue new file mode 100644 index 0000000000..608b729974 --- /dev/null +++ b/docs/.vitepress/theme/Layout.vue @@ -0,0 +1,31 @@ + + + diff --git a/docs/.vitepress/theme/SqlDialectSelector.vue b/docs/.vitepress/theme/SqlDialectSelector.vue new file mode 100644 index 0000000000..f8fc0bba36 --- /dev/null +++ b/docs/.vitepress/theme/SqlDialectSelector.vue @@ -0,0 +1,50 @@ + + + + + diff --git a/docs/.vitepress/theme/SqlOutput.vue b/docs/.vitepress/theme/SqlOutput.vue new file mode 100644 index 0000000000..ee9e3f2a5c --- /dev/null +++ b/docs/.vitepress/theme/SqlOutput.vue @@ -0,0 +1,9 @@ + + + diff --git a/docs/.vitepress/theme/ToggleDark.vue b/docs/.vitepress/theme/ToggleDark.vue new file mode 100644 index 0000000000..dc65f95b18 --- /dev/null +++ b/docs/.vitepress/theme/ToggleDark.vue @@ -0,0 +1,123 @@ + + + + + diff --git a/docs/.vitepress/theme/dialect.js b/docs/.vitepress/theme/dialect.js new file mode 100644 index 0000000000..6bd3b87560 --- /dev/null +++ b/docs/.vitepress/theme/dialect.js @@ -0,0 +1,39 @@ +import { watch, ref, nextTick, inject } from 'vue'; + +export function createDialect(app) { + const dialect = ref('mysql'); + + if (!import.meta.url) { + watch(dialect, (value) => { + localStorage.setItem('sql-dialect', value); + }); + nextTick(() => { + const value = localStorage.getItem('sql-dialect'); + if (value) { + dialect.value = value; + } + }); + } + + // provide for later inject + app.provide('dialect', dialect); + + // expose $dialect to templates + Object.defineProperty(app.config.globalProperties, '$dialect', { + get() { + return dialect.value; + }, + }); + + return { + dialect, + }; +} + +export function useDialect() { + const dialect = inject('dialect'); + + return { + dialect, + }; +} diff --git a/docs/.vitepress/theme/index.js b/docs/.vitepress/theme/index.js new file mode 100644 index 0000000000..87655944db --- /dev/null +++ b/docs/.vitepress/theme/index.js @@ -0,0 +1,20 @@ +import defaultTheme from 'vitepress/theme'; +import Layout from './Layout.vue'; +import { createDialect } from './dialect'; +import SqlOutput from './SqlOutput.vue'; +import './styles.css'; + +// @todo: hack, vite.config.ts define option seem not to work +globalThis.process = globalThis.process || { + env: {}, +}; + +export default { + Layout, + NotFound: defaultTheme.NotFound, + + enhanceApp({ app }) { + createDialect(app); + app.component('SqlOutput', SqlOutput); + }, +}; diff --git a/docs/.vitepress/theme/styles.css b/docs/.vitepress/theme/styles.css new file mode 100644 index 0000000000..d8b2d89f02 --- /dev/null +++ b/docs/.vitepress/theme/styles.css @@ -0,0 +1,124 @@ +@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&family=Source+Code+Pro&display=swap'); + +:root { + --c-brand: #d26b38; + --c-white-dark: #f5f5f5; + --c-white-darker: #c5bab5; + --c-brand-light: #ff8144; + --font-family-base: 'Poppins', -apple-system, BlinkMacSystemFont, 'Segoe UI', + Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', + 'Helvetica Neue', sans-serif; + --font-family-mono: 'Source Code Pro', source-code-pro, Menlo, Monaco, + Consolas, 'Courier New', monospace; + + --code-font-size: 14px; + --code-line-height: 22px; + /* --c-text-light-1: #c5bab5; */ + --c-text-light-2: #cd7244; + --c-text-light-3: #d26a38; + --c-text-dark-1: #7f7f7f; + --code-bg-color: #362f2d; + --code-inline-bg-color: rgb(68 52 47 / 5%); +} +html { + font-size: 14px; +} +.dark { + --c-white: #2a2420; + --c-white-dark: #342c27; + --c-white-darker: #201d1c; + --c-black: #ffffff; + --c-text-light-1: #c5bab5; + --c-text-light-2: #dfa486; + --c-text-light-3: #d26a38; + --c-text-dark-1: #9f9f9f; + --code-bg-color: #201d1c; + --code-inline-bg-color: #201d1c; + + color-scheme: dark; +} + +.nav-bar .item { + font-size: 1rem; +} +.nav-bar .nav-bar-title { + font-size: 1.4rem; +} +.sidebar > .sidebar-links > .sidebar-link + .sidebar-link { + padding-top: 0.3rem; +} +.sidebar > .sidebar-links > .sidebar-link > a.sidebar-link-item { + font-weight: 600; +} +.sidebar > .sidebar-links > .sidebar-link > .sidebar-link-item { + padding: 0.35rem 1.5rem 0.35rem 1.25rem; +} + +a.header-anchor { + float: left; + margin-top: 0.125em; + margin-left: -1.1em; + padding-right: 0.23em; + font-size: 0.85em; + opacity: 0; +} +.custom-block.warning { + border-color: var(--c-brand); + color: #914926; + background-color: rgb(210 106 56 / 15%); +} +.custom-block.warning .custom-block-title { + color: #d66026; +} +.custom-block.info { + background-color: #fbf6f4; +} +.dark .custom-block.info { + background-color: #3a3532; +} +.dark .custom-block.warning { + color: var(--c-text-light-1); + background-color: #462414; +} +.dark .custom-block.warning .custom-block-title { + color: #d66026; +} + +.home-hero { + max-width: 42rem; + margin-left: auto !important; + margin-right: auto !important; +} +.home-hero .image { + animation: spin 5s linear infinite; + animation-play-state: paused; +} +.home-hero:hover .image { + animation-play-state: running; +} +.theme .container { + max-width: 54rem; +} +.container-home { + max-width: 42rem; + margin: 0 auto; + background: var(--c-white-dark); + padding: 0.5rem 2rem; + border-radius: 6px; +} +@keyframes spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +[data-dialect]::before { + content: attr(data-dialect); +} + +.language-sql code { + color: #ccc; +} diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000000..effeb972eb --- /dev/null +++ b/docs/README.md @@ -0,0 +1,29 @@ +# knex.js documentation + +The vitepress documentation for [http://knexjs.org](http://knexjs.org) + +#### Development: + +```bash +yarn dev # or npm run dev + +``` + +npm run dev + +```bash +yarn install # or npm i +yarn dev # or npm run dev + + +``` + +#### Production: + +```bash +yarn build # or npm run build +``` + +#### License: + +MIT diff --git a/docs/package-lock.json b/docs/package-lock.json new file mode 100644 index 0000000000..64b2a887c9 --- /dev/null +++ b/docs/package-lock.json @@ -0,0 +1,1257 @@ +{ + "name": "@knex/documentation", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@knex/documentation", + "version": "0.1.0", + "license": "MIT", + "devDependencies": { + "knex": "^2.4.0", + "typescript": "^4.6.3", + "vitepress": "^0.22.4" + } + }, + "node_modules/@algolia/autocomplete-core": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/@algolia/autocomplete-core/-/autocomplete-core-1.9.3.tgz", + "integrity": "sha512-009HdfugtGCdC4JdXUbVJClA0q0zh24yyePn+KUGk3rP7j8FEe/m5Yo/z65gn6nP/cM39PxpzqKrL7A6fP6PPw==", + "dev": true, + "dependencies": { + "@algolia/autocomplete-plugin-algolia-insights": "1.9.3", + "@algolia/autocomplete-shared": "1.9.3" + } + }, + "node_modules/@algolia/autocomplete-plugin-algolia-insights": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/@algolia/autocomplete-plugin-algolia-insights/-/autocomplete-plugin-algolia-insights-1.9.3.tgz", + "integrity": "sha512-a/yTUkcO/Vyy+JffmAnTWbr4/90cLzw+CC3bRbhnULr/EM0fGNvM13oQQ14f2moLMcVDyAx/leczLlAOovhSZg==", + "dev": true, + "dependencies": { + "@algolia/autocomplete-shared": "1.9.3" + }, + "peerDependencies": { + "search-insights": ">= 1 < 3" + } + }, + "node_modules/@algolia/autocomplete-preset-algolia": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/@algolia/autocomplete-preset-algolia/-/autocomplete-preset-algolia-1.9.3.tgz", + "integrity": "sha512-d4qlt6YmrLMYy95n5TB52wtNDr6EgAIPH81dvvvW8UmuWRgxEtY0NJiPwl/h95JtG2vmRM804M0DSwMCNZlzRA==", + "dev": true, + "dependencies": { + "@algolia/autocomplete-shared": "1.9.3" + }, + "peerDependencies": { + "@algolia/client-search": ">= 4.9.1 < 6", + "algoliasearch": ">= 4.9.1 < 6" + } + }, + "node_modules/@algolia/autocomplete-shared": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/@algolia/autocomplete-shared/-/autocomplete-shared-1.9.3.tgz", + "integrity": "sha512-Wnm9E4Ye6Rl6sTTqjoymD+l8DjSTHsHboVRYrKgEt8Q7UHm9nYbqhN/i0fhUYA3OAEH7WA8x3jfpnmJm3rKvaQ==", + "dev": true, + "peerDependencies": { + "@algolia/client-search": ">= 4.9.1 < 6", + "algoliasearch": ">= 4.9.1 < 6" + } + }, + "node_modules/@algolia/cache-browser-local-storage": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/cache-browser-local-storage/-/cache-browser-local-storage-4.22.1.tgz", + "integrity": "sha512-Sw6IAmOCvvP6QNgY9j+Hv09mvkvEIDKjYW8ow0UDDAxSXy664RBNQk3i/0nt7gvceOJ6jGmOTimaZoY1THmU7g==", + "dev": true, + "dependencies": { + "@algolia/cache-common": "4.22.1" + } + }, + "node_modules/@algolia/cache-common": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/cache-common/-/cache-common-4.22.1.tgz", + "integrity": "sha512-TJMBKqZNKYB9TptRRjSUtevJeQVXRmg6rk9qgFKWvOy8jhCPdyNZV1nB3SKGufzvTVbomAukFR8guu/8NRKBTA==", + "dev": true + }, + "node_modules/@algolia/cache-in-memory": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/cache-in-memory/-/cache-in-memory-4.22.1.tgz", + "integrity": "sha512-ve+6Ac2LhwpufuWavM/aHjLoNz/Z/sYSgNIXsinGofWOysPilQZPUetqLj8vbvi+DHZZaYSEP9H5SRVXnpsNNw==", + "dev": true, + "dependencies": { + "@algolia/cache-common": "4.22.1" + } + }, + "node_modules/@algolia/client-account": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/client-account/-/client-account-4.22.1.tgz", + "integrity": "sha512-k8m+oegM2zlns/TwZyi4YgCtyToackkOpE+xCaKCYfBfDtdGOaVZCM5YvGPtK+HGaJMIN/DoTL8asbM3NzHonw==", + "dev": true, + "dependencies": { + "@algolia/client-common": "4.22.1", + "@algolia/client-search": "4.22.1", + "@algolia/transporter": "4.22.1" + } + }, + "node_modules/@algolia/client-analytics": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/client-analytics/-/client-analytics-4.22.1.tgz", + "integrity": "sha512-1ssi9pyxyQNN4a7Ji9R50nSdISIumMFDwKNuwZipB6TkauJ8J7ha/uO60sPJFqQyqvvI+px7RSNRQT3Zrvzieg==", + "dev": true, + "dependencies": { + "@algolia/client-common": "4.22.1", + "@algolia/client-search": "4.22.1", + "@algolia/requester-common": "4.22.1", + "@algolia/transporter": "4.22.1" + } + }, + "node_modules/@algolia/client-common": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/client-common/-/client-common-4.22.1.tgz", + "integrity": "sha512-IvaL5v9mZtm4k4QHbBGDmU3wa/mKokmqNBqPj0K7lcR8ZDKzUorhcGp/u8PkPC/e0zoHSTvRh7TRkGX3Lm7iOQ==", + "dev": true, + "dependencies": { + "@algolia/requester-common": "4.22.1", + "@algolia/transporter": "4.22.1" + } + }, + "node_modules/@algolia/client-personalization": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/client-personalization/-/client-personalization-4.22.1.tgz", + "integrity": "sha512-sl+/klQJ93+4yaqZ7ezOttMQ/nczly/3GmgZXJ1xmoewP5jmdP/X/nV5U7EHHH3hCUEHeN7X1nsIhGPVt9E1cQ==", + "dev": true, + "dependencies": { + "@algolia/client-common": "4.22.1", + "@algolia/requester-common": "4.22.1", + "@algolia/transporter": "4.22.1" + } + }, + "node_modules/@algolia/client-search": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/client-search/-/client-search-4.22.1.tgz", + "integrity": "sha512-yb05NA4tNaOgx3+rOxAmFztgMTtGBi97X7PC3jyNeGiwkAjOZc2QrdZBYyIdcDLoI09N0gjtpClcackoTN0gPA==", + "dev": true, + "dependencies": { + "@algolia/client-common": "4.22.1", + "@algolia/requester-common": "4.22.1", + "@algolia/transporter": "4.22.1" + } + }, + "node_modules/@algolia/logger-common": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/logger-common/-/logger-common-4.22.1.tgz", + "integrity": "sha512-OnTFymd2odHSO39r4DSWRFETkBufnY2iGUZNrMXpIhF5cmFE8pGoINNPzwg02QLBlGSaLqdKy0bM8S0GyqPLBg==", + "dev": true + }, + "node_modules/@algolia/logger-console": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/logger-console/-/logger-console-4.22.1.tgz", + "integrity": "sha512-O99rcqpVPKN1RlpgD6H3khUWylU24OXlzkavUAMy6QZd1776QAcauE3oP8CmD43nbaTjBexZj2nGsBH9Tc0FVA==", + "dev": true, + "dependencies": { + "@algolia/logger-common": "4.22.1" + } + }, + "node_modules/@algolia/requester-browser-xhr": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/requester-browser-xhr/-/requester-browser-xhr-4.22.1.tgz", + "integrity": "sha512-dtQGYIg6MteqT1Uay3J/0NDqD+UciHy3QgRbk7bNddOJu+p3hzjTRYESqEnoX/DpEkaNYdRHUKNylsqMpgwaEw==", + "dev": true, + "dependencies": { + "@algolia/requester-common": "4.22.1" + } + }, + "node_modules/@algolia/requester-common": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/requester-common/-/requester-common-4.22.1.tgz", + "integrity": "sha512-dgvhSAtg2MJnR+BxrIFqlLtkLlVVhas9HgYKMk2Uxiy5m6/8HZBL40JVAMb2LovoPFs9I/EWIoFVjOrFwzn5Qg==", + "dev": true + }, + "node_modules/@algolia/requester-node-http": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/requester-node-http/-/requester-node-http-4.22.1.tgz", + "integrity": "sha512-JfmZ3MVFQkAU+zug8H3s8rZ6h0ahHZL/SpMaSasTCGYR5EEJsCc8SI5UZ6raPN2tjxa5bxS13BRpGSBUens7EA==", + "dev": true, + "dependencies": { + "@algolia/requester-common": "4.22.1" + } + }, + "node_modules/@algolia/transporter": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/@algolia/transporter/-/transporter-4.22.1.tgz", + "integrity": "sha512-kzWgc2c9IdxMa3YqA6TN0NW5VrKYYW/BELIn7vnLyn+U/RFdZ4lxxt9/8yq3DKV5snvoDzzO4ClyejZRdV3lMQ==", + "dev": true, + "dependencies": { + "@algolia/cache-common": "4.22.1", + "@algolia/logger-common": "4.22.1", + "@algolia/requester-common": "4.22.1" + } + }, + "node_modules/@babel/parser": { + "version": "7.23.9", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.23.9.tgz", + "integrity": "sha512-9tcKgqKbs3xGJ+NtKF2ndOBBLVwPjl1SHxPQkd36r3Dlirw3xWUeGaTbqr7uGZcTaxkVNwc+03SVP7aCdWrTlA==", + "dev": true, + "bin": { + "parser": "bin/babel-parser.js" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@docsearch/css": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/@docsearch/css/-/css-3.5.2.tgz", + "integrity": "sha512-SPiDHaWKQZpwR2siD0KQUwlStvIAnEyK6tAE2h2Wuoq8ue9skzhlyVQ1ddzOxX6khULnAALDiR/isSF3bnuciA==", + "dev": true + }, + "node_modules/@docsearch/js": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/@docsearch/js/-/js-3.5.2.tgz", + "integrity": "sha512-p1YFTCDflk8ieHgFJYfmyHBki1D61+U9idwrLh+GQQMrBSP3DLGKpy0XUJtPjAOPltcVbqsTjiPFfH7JImjUNg==", + "dev": true, + "dependencies": { + "@docsearch/react": "3.5.2", + "preact": "^10.0.0" + } + }, + "node_modules/@docsearch/react": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/@docsearch/react/-/react-3.5.2.tgz", + "integrity": "sha512-9Ahcrs5z2jq/DcAvYtvlqEBHImbm4YJI8M9y0x6Tqg598P40HTEkX7hsMcIuThI+hTFxRGZ9hll0Wygm2yEjng==", + "dev": true, + "dependencies": { + "@algolia/autocomplete-core": "1.9.3", + "@algolia/autocomplete-preset-algolia": "1.9.3", + "@docsearch/css": "3.5.2", + "algoliasearch": "^4.19.1" + }, + "peerDependencies": { + "@types/react": ">= 16.8.0 < 19.0.0", + "react": ">= 16.8.0 < 19.0.0", + "react-dom": ">= 16.8.0 < 19.0.0", + "search-insights": ">= 1 < 3" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "react": { + "optional": true + }, + "react-dom": { + "optional": true + }, + "search-insights": { + "optional": true + } + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.14.54.tgz", + "integrity": "sha512-bZBrLAIX1kpWelV0XemxBZllyRmM6vgFQQG2GdNb+r3Fkp0FOh1NJSvekXDs7jq70k4euu1cryLMfU+mTXlEpw==", + "cpu": [ + "loong64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==", + "dev": true + }, + "node_modules/@vitejs/plugin-vue": { + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/@vitejs/plugin-vue/-/plugin-vue-2.3.4.tgz", + "integrity": "sha512-IfFNbtkbIm36O9KB8QodlwwYvTEsJb4Lll4c2IwB3VHc2gie2mSPtSzL0eYay7X2jd/2WX02FjSGTWR6OPr/zg==", + "dev": true, + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "vite": "^2.5.10", + "vue": "^3.2.25" + } + }, + "node_modules/@vue/compiler-core": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/compiler-core/-/compiler-core-3.4.19.tgz", + "integrity": "sha512-gj81785z0JNzRcU0Mq98E56e4ltO1yf8k5PQ+tV/7YHnbZkrM0fyFyuttnN8ngJZjbpofWE/m4qjKBiLl8Ju4w==", + "dev": true, + "dependencies": { + "@babel/parser": "^7.23.9", + "@vue/shared": "3.4.19", + "entities": "^4.5.0", + "estree-walker": "^2.0.2", + "source-map-js": "^1.0.2" + } + }, + "node_modules/@vue/compiler-dom": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/compiler-dom/-/compiler-dom-3.4.19.tgz", + "integrity": "sha512-vm6+cogWrshjqEHTzIDCp72DKtea8Ry/QVpQRYoyTIg9k7QZDX6D8+HGURjtmatfgM8xgCFtJJaOlCaRYRK3QA==", + "dev": true, + "dependencies": { + "@vue/compiler-core": "3.4.19", + "@vue/shared": "3.4.19" + } + }, + "node_modules/@vue/compiler-sfc": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/compiler-sfc/-/compiler-sfc-3.4.19.tgz", + "integrity": "sha512-LQ3U4SN0DlvV0xhr1lUsgLCYlwQfUfetyPxkKYu7dkfvx7g3ojrGAkw0AERLOKYXuAGnqFsEuytkdcComei3Yg==", + "dev": true, + "dependencies": { + "@babel/parser": "^7.23.9", + "@vue/compiler-core": "3.4.19", + "@vue/compiler-dom": "3.4.19", + "@vue/compiler-ssr": "3.4.19", + "@vue/shared": "3.4.19", + "estree-walker": "^2.0.2", + "magic-string": "^0.30.6", + "postcss": "^8.4.33", + "source-map-js": "^1.0.2" + } + }, + "node_modules/@vue/compiler-ssr": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/compiler-ssr/-/compiler-ssr-3.4.19.tgz", + "integrity": "sha512-P0PLKC4+u4OMJ8sinba/5Z/iDT84uMRRlrWzadgLA69opCpI1gG4N55qDSC+dedwq2fJtzmGald05LWR5TFfLw==", + "dev": true, + "dependencies": { + "@vue/compiler-dom": "3.4.19", + "@vue/shared": "3.4.19" + } + }, + "node_modules/@vue/reactivity": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/reactivity/-/reactivity-3.4.19.tgz", + "integrity": "sha512-+VcwrQvLZgEclGZRHx4O2XhyEEcKaBi50WbxdVItEezUf4fqRh838Ix6amWTdX0CNb/b6t3Gkz3eOebfcSt+UA==", + "dev": true, + "dependencies": { + "@vue/shared": "3.4.19" + } + }, + "node_modules/@vue/runtime-core": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/runtime-core/-/runtime-core-3.4.19.tgz", + "integrity": "sha512-/Z3tFwOrerJB/oyutmJGoYbuoadphDcJAd5jOuJE86THNZji9pYjZroQ2NFsZkTxOq0GJbb+s2kxTYToDiyZzw==", + "dev": true, + "dependencies": { + "@vue/reactivity": "3.4.19", + "@vue/shared": "3.4.19" + } + }, + "node_modules/@vue/runtime-dom": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/runtime-dom/-/runtime-dom-3.4.19.tgz", + "integrity": "sha512-IyZzIDqfNCF0OyZOauL+F4yzjMPN2rPd8nhqPP2N1lBn3kYqJpPHHru+83Rkvo2lHz5mW+rEeIMEF9qY3PB94g==", + "dev": true, + "dependencies": { + "@vue/runtime-core": "3.4.19", + "@vue/shared": "3.4.19", + "csstype": "^3.1.3" + } + }, + "node_modules/@vue/server-renderer": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/server-renderer/-/server-renderer-3.4.19.tgz", + "integrity": "sha512-eAj2p0c429RZyyhtMRnttjcSToch+kTWxFPHlzGMkR28ZbF1PDlTcmGmlDxccBuqNd9iOQ7xPRPAGgPVj+YpQw==", + "dev": true, + "dependencies": { + "@vue/compiler-ssr": "3.4.19", + "@vue/shared": "3.4.19" + }, + "peerDependencies": { + "vue": "3.4.19" + } + }, + "node_modules/@vue/shared": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/@vue/shared/-/shared-3.4.19.tgz", + "integrity": "sha512-/KliRRHMF6LoiThEy+4c1Z4KB/gbPrGjWwJR+crg2otgrf/egKzRaCPvJ51S5oetgsgXLfc4Rm5ZgrKHZrtMSw==", + "dev": true + }, + "node_modules/algoliasearch": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/algoliasearch/-/algoliasearch-4.22.1.tgz", + "integrity": "sha512-jwydKFQJKIx9kIZ8Jm44SdpigFwRGPESaxZBaHSV0XWN2yBJAOT4mT7ppvlrpA4UGzz92pqFnVKr/kaZXrcreg==", + "dev": true, + "dependencies": { + "@algolia/cache-browser-local-storage": "4.22.1", + "@algolia/cache-common": "4.22.1", + "@algolia/cache-in-memory": "4.22.1", + "@algolia/client-account": "4.22.1", + "@algolia/client-analytics": "4.22.1", + "@algolia/client-common": "4.22.1", + "@algolia/client-personalization": "4.22.1", + "@algolia/client-search": "4.22.1", + "@algolia/logger-common": "4.22.1", + "@algolia/logger-console": "4.22.1", + "@algolia/requester-browser-xhr": "4.22.1", + "@algolia/requester-common": "4.22.1", + "@algolia/requester-node-http": "4.22.1", + "@algolia/transporter": "4.22.1" + } + }, + "node_modules/colorette": { + "version": "2.0.19", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.19.tgz", + "integrity": "sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==", + "dev": true + }, + "node_modules/commander": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz", + "integrity": "sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==", + "dev": true, + "engines": { + "node": ">=14" + } + }, + "node_modules/csstype": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", + "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==", + "dev": true + }, + "node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dev": true, + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/entities": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz", + "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==", + "dev": true, + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/esbuild": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.14.54.tgz", + "integrity": "sha512-Cy9llcy8DvET5uznocPyqL3BFRrFXSVqbgpMJ9Wz8oVjZlh/zUSNbPRbov0VX7VxN2JH1Oa0uNxZ7eLRb62pJA==", + "dev": true, + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/linux-loong64": "0.14.54", + "esbuild-android-64": "0.14.54", + "esbuild-android-arm64": "0.14.54", + "esbuild-darwin-64": "0.14.54", + "esbuild-darwin-arm64": "0.14.54", + "esbuild-freebsd-64": "0.14.54", + "esbuild-freebsd-arm64": "0.14.54", + "esbuild-linux-32": "0.14.54", + "esbuild-linux-64": "0.14.54", + "esbuild-linux-arm": "0.14.54", + "esbuild-linux-arm64": "0.14.54", + "esbuild-linux-mips64le": "0.14.54", + "esbuild-linux-ppc64le": "0.14.54", + "esbuild-linux-riscv64": "0.14.54", + "esbuild-linux-s390x": "0.14.54", + "esbuild-netbsd-64": "0.14.54", + "esbuild-openbsd-64": "0.14.54", + "esbuild-sunos-64": "0.14.54", + "esbuild-windows-32": "0.14.54", + "esbuild-windows-64": "0.14.54", + "esbuild-windows-arm64": "0.14.54" + } + }, + "node_modules/esbuild-android-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-android-64/-/esbuild-android-64-0.14.54.tgz", + "integrity": "sha512-Tz2++Aqqz0rJ7kYBfz+iqyE3QMycD4vk7LBRyWaAVFgFtQ/O8EJOnVmTOiDWYZ/uYzB4kvP+bqejYdVKzE5lAQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-android-arm64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-android-arm64/-/esbuild-android-arm64-0.14.54.tgz", + "integrity": "sha512-F9E+/QDi9sSkLaClO8SOV6etqPd+5DgJje1F9lOWoNncDdOBL2YF59IhsWATSt0TLZbYCf3pNlTHvVV5VfHdvg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-darwin-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-0.14.54.tgz", + "integrity": "sha512-jtdKWV3nBviOd5v4hOpkVmpxsBy90CGzebpbO9beiqUYVMBtSc0AL9zGftFuBon7PNDcdvNCEuQqw2x0wP9yug==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-darwin-arm64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.14.54.tgz", + "integrity": "sha512-OPafJHD2oUPyvJMrsCvDGkRrVCar5aVyHfWGQzY1dWnzErjrDuSETxwA2HSsyg2jORLY8yBfzc1MIpUkXlctmw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-freebsd-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-64/-/esbuild-freebsd-64-0.14.54.tgz", + "integrity": "sha512-OKwd4gmwHqOTp4mOGZKe/XUlbDJ4Q9TjX0hMPIDBUWWu/kwhBAudJdBoxnjNf9ocIB6GN6CPowYpR/hRCbSYAg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-freebsd-arm64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-arm64/-/esbuild-freebsd-arm64-0.14.54.tgz", + "integrity": "sha512-sFwueGr7OvIFiQT6WeG0jRLjkjdqWWSrfbVwZp8iMP+8UHEHRBvlaxL6IuKNDwAozNUmbb8nIMXa7oAOARGs1Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-32": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-32/-/esbuild-linux-32-0.14.54.tgz", + "integrity": "sha512-1ZuY+JDI//WmklKlBgJnglpUL1owm2OX+8E1syCD6UAxcMM/XoWd76OHSjl/0MR0LisSAXDqgjT3uJqT67O3qw==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-0.14.54.tgz", + "integrity": "sha512-EgjAgH5HwTbtNsTqQOXWApBaPVdDn7XcK+/PtJwZLT1UmpLoznPd8c5CxqsH2dQK3j05YsB3L17T8vE7cp4cCg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-arm": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm/-/esbuild-linux-arm-0.14.54.tgz", + "integrity": "sha512-qqz/SjemQhVMTnvcLGoLOdFpCYbz4v4fUo+TfsWG+1aOu70/80RV6bgNpR2JCrppV2moUQkww+6bWxXRL9YMGw==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-arm64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm64/-/esbuild-linux-arm64-0.14.54.tgz", + "integrity": "sha512-WL71L+0Rwv+Gv/HTmxTEmpv0UgmxYa5ftZILVi2QmZBgX3q7+tDeOQNqGtdXSdsL8TQi1vIaVFHUPDe0O0kdig==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-mips64le": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-mips64le/-/esbuild-linux-mips64le-0.14.54.tgz", + "integrity": "sha512-qTHGQB8D1etd0u1+sB6p0ikLKRVuCWhYQhAHRPkO+OF3I/iSlTKNNS0Lh2Oc0g0UFGguaFZZiPJdJey3AGpAlw==", + "cpu": [ + "mips64el" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-ppc64le": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-ppc64le/-/esbuild-linux-ppc64le-0.14.54.tgz", + "integrity": "sha512-j3OMlzHiqwZBDPRCDFKcx595XVfOfOnv68Ax3U4UKZ3MTYQB5Yz3X1mn5GnodEVYzhtZgxEBidLWeIs8FDSfrQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-riscv64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-riscv64/-/esbuild-linux-riscv64-0.14.54.tgz", + "integrity": "sha512-y7Vt7Wl9dkOGZjxQZnDAqqn+XOqFD7IMWiewY5SPlNlzMX39ocPQlOaoxvT4FllA5viyV26/QzHtvTjVNOxHZg==", + "cpu": [ + "riscv64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-s390x": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-linux-s390x/-/esbuild-linux-s390x-0.14.54.tgz", + "integrity": "sha512-zaHpW9dziAsi7lRcyV4r8dhfG1qBidQWUXweUjnw+lliChJqQr+6XD71K41oEIC3Mx1KStovEmlzm+MkGZHnHA==", + "cpu": [ + "s390x" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-netbsd-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-netbsd-64/-/esbuild-netbsd-64-0.14.54.tgz", + "integrity": "sha512-PR01lmIMnfJTgeU9VJTDY9ZerDWVFIUzAtJuDHwwceppW7cQWjBBqP48NdeRtoP04/AtO9a7w3viI+PIDr6d+w==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-openbsd-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.14.54.tgz", + "integrity": "sha512-Qyk7ikT2o7Wu76UsvvDS5q0amJvmRzDyVlL0qf5VLsLchjCa1+IAvd8kTBgUxD7VBUUVgItLkk609ZHUc1oCaw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-sunos-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.14.54.tgz", + "integrity": "sha512-28GZ24KmMSeKi5ueWzMcco6EBHStL3B6ubM7M51RmPwXQGLe0teBGJocmWhgwccA1GeFXqxzILIxXpHbl9Q/Kw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-windows-32": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-windows-32/-/esbuild-windows-32-0.14.54.tgz", + "integrity": "sha512-T+rdZW19ql9MjS7pixmZYVObd9G7kcaZo+sETqNH4RCkuuYSuv9AGHUVnPoP9hhuE1WM1ZimHz1CIBHBboLU7w==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-windows-64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-0.14.54.tgz", + "integrity": "sha512-AoHTRBUuYwXtZhjXZbA1pGfTo8cJo3vZIcWGLiUcTNgHpJJMC1rVA44ZereBHMJtotyN71S8Qw0npiCIkW96cQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-windows-arm64": { + "version": "0.14.54", + "resolved": "https://registry.npmjs.org/esbuild-windows-arm64/-/esbuild-windows-arm64-0.14.54.tgz", + "integrity": "sha512-M0kuUvXhot1zOISQGXwWn6YtS+Y/1RT9WrVIOywZnJHo3jCDyewAc79aKNQWFCQm+xNHVTq9h8dZKvygoXQQRg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/escalade": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.2.tgz", + "integrity": "sha512-ErCHMCae19vR8vQGe50xIsVomy19rg6gFu3+r3jkEO46suLMWBksvVyoGgQV+jOfl84ZSOSlmv6Gxa89PmTGmA==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/esm": { + "version": "3.2.25", + "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", + "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/estree-walker": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", + "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==", + "dev": true + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-package-type": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/get-package-type/-/get-package-type-0.1.0.tgz", + "integrity": "sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==", + "dev": true, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/getopts": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/getopts/-/getopts-2.3.0.tgz", + "integrity": "sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA==", + "dev": true + }, + "node_modules/hasown": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.1.tgz", + "integrity": "sha512-1/th4MHjnwncwXsIW6QMzlvYL9kG5e/CpVvLRZe4XPa8TOUNbCELqmvhDmnkNsAjwaG4+I8gJJL0JBvTTLO9qA==", + "dev": true, + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/interpret": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-2.2.0.tgz", + "integrity": "sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw==", + "dev": true, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/is-core-module": { + "version": "2.13.1", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz", + "integrity": "sha512-hHrIjvZsftOsvKSn2TRYl63zvxsgE0K+0mYMoH6gD4omR5IWB2KynivBQczo3+wF1cCkjzvptnI9Q0sPU66ilw==", + "dev": true, + "dependencies": { + "hasown": "^2.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/knex": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/knex/-/knex-2.5.1.tgz", + "integrity": "sha512-z78DgGKUr4SE/6cm7ku+jHvFT0X97aERh/f0MUKAKgFnwCYBEW4TFBqtHWFYiJFid7fMrtpZ/gxJthvz5mEByA==", + "dev": true, + "dependencies": { + "colorette": "2.0.19", + "commander": "^10.0.0", + "debug": "4.3.4", + "escalade": "^3.1.1", + "esm": "^3.2.25", + "get-package-type": "^0.1.0", + "getopts": "2.3.0", + "interpret": "^2.2.0", + "lodash": "^4.17.21", + "pg-connection-string": "2.6.1", + "rechoir": "^0.8.0", + "resolve-from": "^5.0.0", + "tarn": "^3.0.2", + "tildify": "2.0.0" + }, + "bin": { + "knex": "bin/cli.js" + }, + "engines": { + "node": ">=12" + }, + "peerDependenciesMeta": { + "better-sqlite3": { + "optional": true + }, + "mysql": { + "optional": true + }, + "mysql2": { + "optional": true + }, + "pg": { + "optional": true + }, + "pg-native": { + "optional": true + }, + "sqlite3": { + "optional": true + }, + "tedious": { + "optional": true + } + } + }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, + "node_modules/magic-string": { + "version": "0.30.7", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.7.tgz", + "integrity": "sha512-8vBuFF/I/+OSLRmdf2wwFCJCz+nSn0m6DPvGH1fS/KiQoSaR+sETbov0eIk9KhEKy8CYqIkIAnbohxT/4H0kuA==", + "dev": true, + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.4.15" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true + }, + "node_modules/nanoid": { + "version": "3.3.7", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz", + "integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/path-parse": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", + "dev": true + }, + "node_modules/pg-connection-string": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.6.1.tgz", + "integrity": "sha512-w6ZzNu6oMmIzEAYVw+RLK0+nqHPt8K3ZnknKi+g48Ak2pr3dtljJW3o+D/n2zzCG07Zoe9VOX3aiKpj+BN0pjg==", + "dev": true + }, + "node_modules/picocolors": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", + "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==", + "dev": true + }, + "node_modules/postcss": { + "version": "8.4.35", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.35.tgz", + "integrity": "sha512-u5U8qYpBCpN13BsiEB0CbR1Hhh4Gc0zLFuedrHJKMctHCHAGrMdG0PRM/KErzAL3CU6/eckEtmHNB3x6e3c0vA==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "dependencies": { + "nanoid": "^3.3.7", + "picocolors": "^1.0.0", + "source-map-js": "^1.0.2" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/preact": { + "version": "10.19.6", + "resolved": "https://registry.npmjs.org/preact/-/preact-10.19.6.tgz", + "integrity": "sha512-gympg+T2Z1fG1unB8NH29yHJwnEaCH37Z32diPDku316OTnRPeMbiRV9kTrfZpocXjdfnWuFUl/Mj4BHaf6gnw==", + "dev": true, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/preact" + } + }, + "node_modules/prismjs": { + "version": "1.29.0", + "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.29.0.tgz", + "integrity": "sha512-Kx/1w86q/epKcmte75LNrEoT+lX8pBpavuAbvJWRXar7Hz8jrtF+e3vY751p0R8H9HdArwaCTNDDzHg/ScJK1Q==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/rechoir": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.8.0.tgz", + "integrity": "sha512-/vxpCXddiX8NGfGO/mTafwjq4aFa/71pvamip0++IQk3zG8cbCj0fifNPrjjF1XMXUne91jL9OoxmdykoEtifQ==", + "dev": true, + "dependencies": { + "resolve": "^1.20.0" + }, + "engines": { + "node": ">= 10.13.0" + } + }, + "node_modules/resolve": { + "version": "1.22.8", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.8.tgz", + "integrity": "sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==", + "dev": true, + "dependencies": { + "is-core-module": "^2.13.0", + "path-parse": "^1.0.7", + "supports-preserve-symlinks-flag": "^1.0.0" + }, + "bin": { + "resolve": "bin/resolve" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/resolve-from": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz", + "integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/rollup": { + "version": "2.77.3", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.77.3.tgz", + "integrity": "sha512-/qxNTG7FbmefJWoeeYJFbHehJ2HNWnjkAFRKzWN/45eNBBF/r8lo992CwcJXEzyVxs5FmfId+vTSTQDb+bxA+g==", + "dev": true, + "bin": { + "rollup": "dist/bin/rollup" + }, + "engines": { + "node": ">=10.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/search-insights": { + "version": "2.13.0", + "resolved": "https://registry.npmjs.org/search-insights/-/search-insights-2.13.0.tgz", + "integrity": "sha512-Orrsjf9trHHxFRuo9/rzm0KIWmgzE8RMlZMzuhZOJ01Rnz3D0YBAe+V6473t6/H6c7irs6Lt48brULAiRWb3Vw==", + "dev": true, + "peer": true + }, + "node_modules/source-map-js": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz", + "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/supports-preserve-symlinks-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", + "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", + "dev": true, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/tarn": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/tarn/-/tarn-3.0.2.tgz", + "integrity": "sha512-51LAVKUSZSVfI05vjPESNc5vwqqZpbXCsU+/+wxlOrUjk2SnFTt97v9ZgQrD4YmxYW1Px6w2KjaDitCfkvgxMQ==", + "dev": true, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/tildify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tildify/-/tildify-2.0.0.tgz", + "integrity": "sha512-Cc+OraorugtXNfs50hU9KS369rFXCfgGLpfCfvlc+Ud5u6VWmUQsOAa9HbTvheQdYnrdJqqv1e5oIqXppMYnSw==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/typescript": { + "version": "4.9.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz", + "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, + "node_modules/vite": { + "version": "2.9.17", + "resolved": "https://registry.npmjs.org/vite/-/vite-2.9.17.tgz", + "integrity": "sha512-XxcRzra6d7xrKXH66jZUgb+srThoPu+TLJc06GifUyKq9JmjHkc1Numc8ra0h56rju2jfVWw3B3fs5l3OFMvUw==", + "dev": true, + "dependencies": { + "esbuild": "^0.14.27", + "postcss": "^8.4.13", + "resolve": "^1.22.0", + "rollup": ">=2.59.0 <2.78.0" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": ">=12.2.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + }, + "peerDependencies": { + "less": "*", + "sass": "*", + "stylus": "*" + }, + "peerDependenciesMeta": { + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "stylus": { + "optional": true + } + } + }, + "node_modules/vitepress": { + "version": "0.22.4", + "resolved": "https://registry.npmjs.org/vitepress/-/vitepress-0.22.4.tgz", + "integrity": "sha512-oZUnLO/SpYdThaBKefDeOiVlr0Rie4Ppx3FzMnMyLtJnI5GlBMNjqYqMy/4+umm/iC+ZDJfI+IlDKxv5fZnYzA==", + "dev": true, + "dependencies": { + "@docsearch/css": "^3.0.0", + "@docsearch/js": "^3.0.0", + "@vitejs/plugin-vue": "^2.3.2", + "prismjs": "^1.25.0", + "vite": "^2.9.7", + "vue": "^3.2.33" + }, + "bin": { + "vitepress": "bin/vitepress.js" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/vue": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/vue/-/vue-3.4.19.tgz", + "integrity": "sha512-W/7Fc9KUkajFU8dBeDluM4sRGc/aa4YJnOYck8dkjgZoXtVsn3OeTGni66FV1l3+nvPA7VBFYtPioaGKUmEADw==", + "dev": true, + "dependencies": { + "@vue/compiler-dom": "3.4.19", + "@vue/compiler-sfc": "3.4.19", + "@vue/runtime-dom": "3.4.19", + "@vue/server-renderer": "3.4.19", + "@vue/shared": "3.4.19" + }, + "peerDependencies": { + "typescript": "*" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + } + } +} diff --git a/docs/package.json b/docs/package.json new file mode 100644 index 0000000000..506261900f --- /dev/null +++ b/docs/package.json @@ -0,0 +1,22 @@ +{ + "name": "@knex/documentation", + "private": true, + "version": "0.1.0", + "description": "Knex Documentation Builder", + "scripts": { + "dev": "vitepress dev .", + "build": "vitepress build .", + "serve": "vitepress serve ." + }, + "devDependencies": { + "knex": "^2.4.0", + "typescript": "^4.6.3", + "vitepress": "^0.22.4" + }, + "dependencies": {}, + "author": { + "name": "Tim Griesser", + "web": "https://github.com/tgriesser" + }, + "license": "MIT" +} diff --git a/docs/scripts/deploy-doc.sh b/docs/scripts/deploy-doc.sh new file mode 100755 index 0000000000..3d745603ae --- /dev/null +++ b/docs/scripts/deploy-doc.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env sh + +# abort on errors +set -e + +# build +npm run build + +# navigate into the build output directory +cd .vitepress/dist + +# if you are deploying to a custom domain +# echo 'www.example.com' > CNAME + +git init +git add -A +git commit -m 'deploy' + +git push -f git@github.com:knex/knex.git master:gh-pages + +cd - diff --git a/docs/src/changelog.md b/docs/src/changelog.md new file mode 100644 index 0000000000..c2fa8e2c19 --- /dev/null +++ b/docs/src/changelog.md @@ -0,0 +1,2341 @@ +## Changelog + +### 2.5.1 - 12 July, 2023 + +**Bug fixes** + +- Fix Linting [#5455](https://github.com/knex/knex/issues/5460) - [#5460](https://github.com/knex/knex/issues/5460) + +### 2.5.0 - 08 July, 2023 + +**New features** + +- Add uuid helper function [#5617](https://github.com/knex/knex/issues/5617) +- Add `nativeBindings` option to `better-sqlite3` options [#5461](https://github.com/knex/knex/issues/5461) +- Add QueryBuilder#updateFrom [#5386](https://github.com/knex/knex/issues/5386) +- Add readonly transaction access mode [#5445](https://github.com/knex/knex/issues/5445) +- Add readonly option to Better-SQLite3 [#5530](https://github.com/knex/knex/issues/5530) +- Add EXCEPT as a valid keyword [#5357](https://github.com/knex/knex/issues/5357) +- Add ability to prepend query comments [#5289](https://github.com/knex/knex/issues/5289) +- Add fetchAsString option [#5484](https://github.com/knex/knex/issues/5484) + +**Bug fixes** + +- Avoid password leaks on query logs [#5559](https://github.com/knex/knex/issues/5559) +- Add knex.mjs to files in package.json [#5518](https://github.com/knex/knex/issues/5518) +- Handle numeric array elements in .orderBy() [#5551](https://github.com/knex/knex/issues/5551) +- Attach error handler early enough [#5552](https://github.com/knex/knex/issues/5552) +- Fix Returning \* in Oracle [#5598](https://github.com/knex/knex/issues/5598) +- Fix indexType option in `Postgres` [#5601](https://github.com/knex/knex/issues/5601) +- Add mjs extension type [#5616](https://github.com/knex/knex/issues/5616) +- Use implicit check on json fields for OracleDB [#5478](https://github.com/knex/knex/issues/5478) +- Fix when manually close source stream [#5466](https://github.com/knex/knex/issues/5466) +- Fix case sensitive issue with get table [#5509](https://github.com/knex/knex/issues/5509) + +**Typings** + +- Add Object syntax overload to increment method [#5512](https://github.com/knex/knex/issues/5512) +- Add object syntax overload to decrement method [#5555](https://github.com/knex/knex/issues/5555) +- Fix typing for toSql [#5594](https://github.com/knex/knex/issues/5594) +- Add ResolveTableType for `.merge()` [#5605](https://github.com/knex/knex/issues/5605) +- Add missing types for havingNull and havingNotNull [#5529](https://github.com/knex/knex/issues/5529) +- Add collate to the columnbuilder interface [#5568](https://github.com/knex/knex/issues/5568) +- TableBuilder methods return the SchemaBuilder. [#5486](https://github.com/knex/knex/issues/5486) + +### 2.4.2 - 22 January, 2023 + +**Bug fixes** + +- CLI: Fix incorrent EOL causing errors on Linux [#5455](https://github.com/knex/knex/issues/5455) + +### 2.4.1 - 18 January, 2023 + +**Bug fixes** + +- Fix Postgres Malformed array literal 2.4.0 Regression - [#5439](https://github.com/knex/knex/issues/5439) + +### 2.4.0 - 6 January, 2023 + +**New features** + +- Support partial unique indexes [#5316](https://github.com/knex/knex/issues/5316) +- Make compiling SQL in error message optional [#5282](https://github.com/knex/knex/issues/5282) + +**Bug fixes** + +- Insert array into json column [#5321](https://github.com/knex/knex/issues/5321) +- Fix unexpected max acquire-timeout [#5377](https://github.com/knex/knex/issues/5377) +- Fix: orWhereJson [#5361](https://github.com/knex/knex/issues/5361) +- MySQL: Add assertion for basic where clause not to be object or array [#1227](https://github.com/knex/knex/issues/1227) +- SQLite: Fix changing the default value of a boolean column in SQLite [#5319](https://github.com/knex/knex/issues/5319) + +**Typings** + +- add missing type for 'expirationChecker' on PgConnectionConfig [#5334](https://github.com/knex/knex/issues/5334) + +### 2.3.0 - 31 August, 2022 + +**New features** + +- PostgreSQL: Explicit jsonb support for custom pg clients [#5201](https://github.com/knex/knex/issues/5201) +- SQLite: Support returning with sqlite3 and better-sqlite3 [#5285](https://github.com/knex/knex/issues/5285) +- MSSQL: Implement mapBinding mssql dialect option [#5292](https://github.com/knex/knex/issues/5292) + +**Typings** + +- Update types for TS 4.8 [#5279](https://github.com/knex/knex/issues/5279) +- Fix typo [#5267](https://github.com/knex/knex/issues/5267) +- Fix WhereJsonObject withCompositeTableType [#5306](https://github.com/knex/knex/issues/5306) +- Fix AnalyticFunction type [#5304](https://github.com/knex/knex/issues/5304) +- Infer specific column value type in aggregations [#5297](https://github.com/knex/knex/issues/5297) + +### 2.2.0 - 18 July, 2022 + +**New features** + +- Inline primary key creation for postgres flavours [#5233](https://github.com/knex/knex/issues/5233) +- SQLite: Add warning for undefined connection file [#5223](https://github.com/knex/knex/issues/5223) +- MSSQL: Add JSON parameter support for connection [#5200](https://github.com/knex/knex/issues/5200) + +**Bug fixes** + +- PostgreSQL: add primaryKey option for uuid [#5212](https://github.com/knex/knex/issues/5212) + +**Typings** + +- Add promisable and better types [#5222](https://github.com/knex/knex/issues/5222) +- Update raw query bind parameter type [#5208](https://github.com/knex/knex/issues/5208) + +### 2.1.0 - 26 May, 2022 + +**New features** + +- Improve bundling experience to safely import dialects while using static paths [#5142](https://github.com/knex/knex/issues/5142) +- Implement extendable builders [#5041](https://github.com/knex/knex/issues/5041) +- PostgreSQL: Refresh materialized view concurrently [#5166](https://github.com/knex/knex/issues/5166) + +**Bug fixes** + +- Use correct paths in package.json browser field [#5174](https://github.com/knex/knex/issues/5174) +- MariaDB: Fix 'NULL' returned instead of NULL on MariaDB 10.2.6+ [#5181](https://github.com/knex/knex/issues/5181) +- MySQL: fix hasColumn Error (hasColumn ('a_id') is true, but hasColumn('a_Id') is false) [#5148](https://github.com/knex/knex/issues/5148) +- MSSQL: Fix .hasTable result when using .withSchema [#5176](https://github.com/knex/knex/issues/5176) +- Oracle: correctly INSERTS Buffer [#4869](https://github.com/knex/knex/issues/4869) + +**Typings** + +- Update type definitions for pg connection [#5139](https://github.com/knex/knex/issues/5139) + +### 2.0.0 - 21 April, 2022 + +**Breaking changes** + +- Restore sqlite3 package [#5136](https://github.com/knex/knex/issues/5136) + +**Test / internal changes** + +- Migrate Husky from 4 to 7 [#5137](https://github.com/knex/knex/issues/5137) +- Migrate Jake to 10.8.5 [#5138](https://github.com/knex/knex/issues/5138) + +### 1.0.7 - 13 March, 2022 + +**Bug fixes** + +- CLI: Fix cli migrate:make SQLite dependency [#5106](https://github.com/knex/knex/issues/5106) + +### 1.0.6 - 12 March, 2022 + +**Bug fixes** + +- PostgreSQL: Wait for search path to be set before returning connection [#5107](https://github.com/knex/knex/issues/5107) +- CLI: No client override during migrate:make [#5109](https://github.com/knex/knex/issues/5109) + +### 1.0.5 - 05 March, 2022 + +**New features** + +- Override knexfile options with CLI options [#4047](https://github.com/knex/knex/issues/4047) + +**Bug fixes** + +- Stringify json value in update [#5063](https://github.com/knex/knex/issues/5063) +- Fix isModuleType() for yarn [#4447](https://github.com/knex/knex/issues/4447) +- Wrapped Unions Fixes [#5072](https://github.com/knex/knex/issues/5072) +- SQLite: Fix @vscode-sqlite3 error message [#5081](https://github.com/knex/knex/issues/5081) +- CLI: Fix completed migration listing [#5060](https://github.com/knex/knex/issues/5060) + +**Typings** + +- Make default generic parameters of `Knex` match the generic parameter types of `knex` [#5021](https://github.com/knex/knex/issues/5021) +- Update knex types for TS 4.7 [#5095](https://github.com/knex/knex/issues/5095) + +### 1.0.4 - 13 March, 2022 + +**New features** + +- Add whereLike functions [#5044](https://github.com/knex/knex/issues/5044) + +**Bug fixes** + +- Fix orWhereJsonPath clause [#5022](https://github.com/knex/knex/issues/5022) +- Subquery in on clause missing parenthesis [#5049](https://github.com/knex/knex/issues/5049) +- Rework Union Wrapping [#5030](https://github.com/knex/knex/issues/5030) +- Oracle: Fix batch inserts with DEFAULT values with OracleDB [#2592](https://github.com/knex/knex/issues/2592) [#5037](https://github.com/knex/knex/issues/5037) + +**Typings** + +- Fix types for "returning" methods [#5031](https://github.com/knex/knex/issues/5031) +- createTableLike callback should be optional [#5055](https://github.com/knex/knex/issues/5055) + +**Documentation** + +- Website URL changed to https://knex.github.io/documentation/ + +### 1.0.3 - 11 February, 2022 + +**Bug fixes** + +- Fix error message for missing migration files [#4937](https://github.com/knex/knex/issues/4937) +- Add withMaterialized and withNotMaterialized to method-constants [#5009](https://github.com/knex/knex/issues/5009) +- PostgreSQL: Fix whereJsonPath queries [#5011](https://github.com/knex/knex/issues/5011) +- PostgreSQL: Fix delete joins [#5016](https://github.com/knex/knex/issues/5016) +- CockroachDB: Fix whereJsonPath queries [#5011](https://github.com/knex/knex/issues/5011) +- MySQL: Create primary keys in same statement [#5017](https://github.com/knex/knex/issues/5017) + +**Typings** + +- Fix type definition for getMigration in MigrationSource [#4998](https://github.com/knex/knex/issues/4998) +- Fix argument type of alter method [#4996](https://github.com/knex/knex/issues/4996) + +**Improvements** + +- Use async / await syntax in seeds as default [#5005](https://github.com/knex/knex/issues/5005) + +**Documentation** + +- Add Firebird dialect to ECOSYSTEM.md [#5003](https://github.com/knex/knex/issues/5003) + +### 1.0.2 - 02 February, 2022 + +**New features** + +- Support of MATERIALIZED and NOT MATERIALIZED with WITH/CTE [#4940](https://github.com/knex/knex/issues/4940) +- Add raw support in onConflict clause [#4960](https://github.com/knex/knex/issues/4960) +- Alter nullable constraint when alterNullable is set to true [#4730](https://github.com/knex/knex/issues/4730) +- Add alterType parameter for alter function [#4967](https://github.com/knex/knex/issues/4967) +- Support string json in json values [#4988](https://github.com/knex/knex/issues/4988) +- MySQL: add with clause [#4508](https://github.com/knex/knex/issues/4508) + +**Bug fixes** + +- Fix error message for missing migration files [#4937](https://github.com/knex/knex/issues/4937) +- Move deferrable to after on update/on delete [#4976](https://github.com/knex/knex/issues/4976) +- Do not use sys.tables to find if a table exists [#2328](https://github.com/knex/knex/issues/2328) +- PostgreSQL: Fix Order nulls [#4989](https://github.com/knex/knex/issues/4989) +- MySQL: Fix collation when renaming column [#2666](https://github.com/knex/knex/issues/2666) +- SQLite: Same boolean handling in better-sqlite3 as in sqlite3 [#4982](https://github.com/knex/knex/issues/4982) + +**Typings** + +- WhereILike - fix typo [#4941](https://github.com/knex/knex/issues/4941) + +### 1.0.1 - 16 January, 2022 + +**Bug fixes** + +- Fix package.json metadata + +### 1.0.0 - 16 January, 2022 + +**Breaking changes** + +- Dropped support for Node 10; +- Replaced unsupported `sqlite3` driver with `@vscode/sqlite3`; +- Changed data structure from `RETURNING` operation to be consistent with `SELECT`; +- Changed Migrator to return list of migrations as objects consistently. + +**New features** + +- Support fromRaw [#4781](https://github.com/knex/knex/issues/4781) +- Support zero precision in timestamp/datetime [#4784](https://github.com/knex/knex/issues/4784) +- Support whereLike and whereILike [#4779](https://github.com/knex/knex/issues/4779) +- Add JSDoc (TS flavor) to stub files [#4809](https://github.com/knex/knex/issues/4809) +- Allow skip binding in limit and offset [#4811](https://github.com/knex/knex/issues/4811) +- Support creating a new table in the database based on another table [#4821](https://github.com/knex/knex/issues/4821) +- Accept Raw on onIn joins [#4830](https://github.com/knex/knex/issues/4830) +- Implement support for custom seed sources [#4842](https://github.com/knex/knex/issues/4842) +- Add binary uuid option [#4836](https://github.com/knex/knex/issues/4836) +- ForUpdate array parameter [#4882](https://github.com/knex/knex/issues/4882) +- Add camel case to timestamps method [#4803](https://github.com/knex/knex/issues/4803) +- Advanced JSON support [#4859](https://github.com/knex/knex/issues/4859) +- Add type to TypeScript knexfile [#4909](https://github.com/knex/knex/issues/4909) +- Checks Constraints Support [#4874](https://github.com/knex/knex/issues/4874) +- Support creating multiple PKs with increments [#4903](https://github.com/knex/knex/issues/4903) +- Enable wrapIdentifier for SQLite .hasTable [#4915](https://github.com/knex/knex/issues/4915) +- MSSQL: Add support for unique constraint [#4887](https://github.com/knex/knex/issues/4887) +- SQLite: New dialect, using better-sqlite3 driver [#4871](https://github.com/knex/knex/issues/4871) +- SQLite: Switch to @vscode/sqlite3 [#4866](https://github.com/knex/knex/issues/4866) +- SQLite: Support createViewOrReplace [#4856](https://github.com/knex/knex/issues/4856) +- SQLite: Support RETURNING statements for better-sqlite3 driver [#4934](https://github.com/knex/knex/issues/4934) +- PostgreSQL: Support JOIN and USING syntax for Delete Statement [#4800](https://github.com/knex/knex/issues/4800) + +**Bug fixes** + +- Fix overzealous warning on use of whereNot with "in" or "between" [#4780](https://github.com/knex/knex/issues/4780) +- Fix Union all + first syntax error [#4799](https://github.com/knex/knex/issues/4799) +- Make view columns optional in create view like [#4829](https://github.com/knex/knex/issues/4829) +- Insert lock row fix during migration [#4865](https://github.com/knex/knex/issues/4865) +- Fix for createViewOrReplace [#4856](https://github.com/knex/knex/issues/4856) +- SQLite: Fix foreign key constraints when altering a table [#4189](https://github.com/knex/knex/issues/4189) +- MySQL: Validate connection fix [#4794](https://github.com/knex/knex/issues/4794) +- MySQL: Set comment size warning limit to 1024 [#4867](https://github.com/knex/knex/issues/4867) + +**Typings** + +- Allow string indexType in index creation [#4791](https://github.com/knex/knex/issues/4791) +- Add missing ints typings [#4832](https://github.com/knex/knex/issues/4832) +- Returning method types [#4881](https://github.com/knex/knex/issues/4881) +- Improve columnInfo type [#4868](https://github.com/knex/knex/issues/4868) + +### 0.95.15 - 22 December, 2021 + +**Bug fixes** + +- Oracle: +- MariaDB: lock row fix during migration in MariaDB and Oracle [#4865](https://github.com/knex/knex/issues/4865) + +### 0.95.14 - 09 November, 2021 + +**Bug fixes** + +- MySQL: mysql2 dialect validate connection fix [#4794](https://github.com/knex/knex/issues/4794) + +### 0.95.13 - 02 November, 2021 + +**Bug fixes** + +- PostgreSQL: Support zero precision in timestamp/datetime [#4784](https://github.com/knex/knex/issues/4784) + +**Typings** + +- Allow string indexType in index creation [#4791](https://github.com/knex/knex/issues/4791) + +### 0.95.12 - 28 October, 2021 + +**New features** + +- New dialect: CockroachDB [#4742](https://github.com/knex/knex/issues/4742) +- New dialect: pg-native [#4327](https://github.com/knex/knex/issues/4327) +- CockroachDB: add support for upsert [#4767](https://github.com/knex/knex/issues/4767) +- PostgreSQL: Support SELECT .. FOR NO KEY UPDATE / KEY SHARE row level locking clauses [#4755](https://github.com/knex/knex/issues/4755) +- PostgreSQL: Add support for 'CASCADE' in PostgreSQL 'DROP SCHEMA' queries [#4713](https://github.com/knex/knex/issues/4713) +- MySQL: Add storage engine index Type support to index() and unique() schema [#4756](https://github.com/knex/knex/issues/4756) +- MSSQL: Support table.primary, table.unique variant with options object [#4710](https://github.com/knex/knex/issues/4710) +- SQLite: Add setNullable support to SQLite [#4684](https://github.com/knex/knex/issues/4684) +- Add geometry column building [#4776](https://github.com/knex/knex/issues/4776) +- Add support for creating table copies [#1373](https://github.com/knex/knex/issues/1373) +- Implement support for views and materialized views [#1626](https://github.com/knex/knex/issues/1626) +- Implement partial index support [#4768](https://github.com/knex/knex/issues/4768) +- Support for 'is null' in 'order by' [#3667](https://github.com/knex/knex/issues/3667) + +**Bug fixes** + +- Fix support for Oracle connections passed via knex.connection() [#4757](https://github.com/knex/knex/issues/4757) +- Avoid inserting multiple locks if a migration lock already exists [#4694](https://github.com/knex/knex/issues/4694) + +**Typings** + +- Some TableBuilder methods return wrong types [#4764](https://github.com/knex/knex/issues/4764) +- Update JoinRaw bindings type to accept arrays [#4752](https://github.com/knex/knex/issues/4752) +- fix onDelete/onUpdate for ColumnBuilder [#4656](https://github.com/knex/knex/issues/4656) + +### 0.95.11 - 03 September, 2021 + +**New features** + +- Add support for nullability modification via schema builder (table.setNullable() and table.dropNullable()) [#4657](https://github.com/knex/knex/issues/4657) +- MySQL: Add support for mysql/mariadb-client JSON parameters in connectionURIs [#4629](https://github.com/knex/knex/issues/4629) +- MSSQL: Support comments as MS_Description properties [#4632](https://github.com/knex/knex/issues/4632) + +**Bug fixes** + +- Fix Analytic orderBy and partitionBy to follow the SQL documentation [#4602](https://github.com/knex/knex/issues/4602) +- CLI: fix migrate:up for migrations disabling transactions [#4550](https://github.com/knex/knex/issues/4550) +- SQLite: Fix adding a column with a foreign key constraint in SQLite [#4649](https://github.com/knex/knex/issues/4649) +- MSSQL: columnInfo() support case-sensitive database collations [#4633](https://github.com/knex/knex/issues/4633) +- MSSQL: Generate valid SQL for withRecursive() [#4514](https://github.com/knex/knex/issues/4514) +- Oracle: withRecursive: omit invalid RECURSIVE keyword, include column list [#4514](https://github.com/knex/knex/issues/4514) + +**Improvements** + +- Add .mjs migration and seed stubs [#4631](https://github.com/knex/knex/issues/4631) +- SQLite: Clean up DDL handling and move all operations to the parser-based approach [#4648](https://github.com/knex/knex/issues/4648) + +### 0.95.10 - 20 August, 2021 + +**Improvements** + +- Use sys info function instead of connection db name [#4623](https://github.com/knex/knex/issues/4623) + +**Typings** + +- Deferrable and withkeyName should not be in ColumnBuilder [#4600](https://github.com/knex/knex/issues/4600) + +### 0.95.9 - 31 July, 2021 + +**New features** + +- Oracle: support specifying schema for dropTable and dropSequence [#4596](https://github.com/knex/knex/issues/4596) +- Oracle: support specifying schema for autoincrement [#4594](https://github.com/knex/knex/issues/4594) + +**Typings** + +- Add TypeScript support for deferrable, new Primary/Unique syntax [#4589](https://github.com/knex/knex/issues/4589) + +### 0.95.8 - 25 July, 2021 + +**New features** + +- Add deferrable support for constraint [#4584](https://github.com/knex/knex/issues/4584) +- Implement delete with join [#4568](https://github.com/knex/knex/issues/4568) +- Add DPI error codes for Oracle [#4536](https://github.com/knex/knex/issues/4536) + +**Bug fixes** + +- Fixing PostgreSQL datetime and timestamp column created with wrong format [#4578](https://github.com/knex/knex/issues/4578) + +**Typings** + +- Improve analytic types [#4576](https://github.com/knex/knex/issues/4576) +- MSSQL: Add trustServerCertificate option [#4500](https://github.com/knex/knex/issues/4500) + +### 0.95.7 - 10 July, 2021 + +**New features** + +- Add ability to omit columns on an onConflict().ignore() [#4557](https://github.com/knex/knex/issues/4557) +- CLI: Log error message [#4534](https://github.com/knex/knex/issues/4534) + +**Typings** + +- Export Knex.TransactionConfig [#4498](https://github.com/knex/knex/issues/4498) +- Include options object in count(Distinct) typings [#4491](https://github.com/knex/knex/issues/4491) +- Add types for analytic functions [#4544](https://github.com/knex/knex/issues/4544) + +### 0.95.6 - 17 May, 2021 + +**Typings** + +- Export TransactionProvider type [#4489](https://github.com/knex/knex/issues/4489) + +### 0.95.5 - 11 May, 2021 + +**New features** + +- SQLite: Add support for file open flags [#4446](https://github.com/knex/knex/issues/4446) +- Add .cjs extension to Seeder.js to support Node ESM [#4381](https://github.com/knex/knex/issues/4381) [#4382](https://github.com/knex/knex/issues/4382) + +**Bug fixes** + +- Remove peerDependencies to avoid auto-install on npm 7 [#4480](https://github.com/knex/knex/issues/4480) + +**Typings** + +- Fix typing for increments and bigIncrements [#4406](https://github.com/knex/knex/issues/4406) +- Add typings for on JoinClause for onVal [#4436](https://github.com/knex/knex/issues/4436) +- Adding Type Definition for isTransaction [#4418](https://github.com/knex/knex/issues/4418) +- Export client class from knex namespace [#4479](https://github.com/knex/knex/issues/4479) + +### 0.95.4 - 26 March, 2021 + +**Typings** + +- Fix mistyping of stream [#4400](https://github.com/knex/knex/issues/4400) + +### 0.95.3 - 25 March, 2021 + +**New features** + +- PostgreSQL: Add "same" as operator [#4372](https://github.com/knex/knex/issues/4372) +- MSSQL: Improve an estimate of the max comment length [#4362](https://github.com/knex/knex/issues/4362) +- Throw an error if negative offset is provided [#4361](https://github.com/knex/knex/issues/4361) + +**Bug fixes** + +- Fix timeout method [#4324](https://github.com/knex/knex/issues/4324) +- SQLite: prevent dropForeign from being silently ignored [#4376](https://github.com/knex/knex/issues/4376) + +**Typings** + +- Allow config.client to be non-client instance [#4367](https://github.com/knex/knex/issues/4367) +- Add dropForeign arg type for single column [#4363](https://github.com/knex/knex/issues/4363) +- Update typings for TypePreservingAggregation and stream [#4377](https://github.com/knex/knex/issues/4377) + +### 0.95.2 - 11 March, 2021 + +**New features** + +- Improve ESM import support [#4350](https://github.com/knex/knex/issues/4350) + +**Bug fixes** + +- CLI: update ts.stub files to new TypeScript namespace [#4344](https://github.com/knex/knex/issues/4344) +- CLI: fix TypeScript migration stub after 0.95.0 changes [#4366](https://github.com/knex/knex/issues/4366) + +**Typings** + +- Move QueryBuilder and KnexTimeoutError into knex namespace [#4358](https://github.com/knex/knex/issues/4358) + +**Test / internal changes** + +- Unify db test helpers [#4356](https://github.com/knex/knex/issues/4356) + +### 0.95.1 - 04 March, 2021 + +**Bug fixes** + +- CLI: fix `knex init` not finding default knexfile [#4339](https://github.com/knex/knex/issues/4339) + +### 0.95.0 - 03 March, 2021 + +Note: there are many breaking changes in this version, particularly in TypeScript support. Please see `UPGRADING.md` for details. + +**New features** + +- Add transaction isolation support [#4185](https://github.com/knex/knex/issues/4185) +- Add analytic functions [#4188](https://github.com/knex/knex/issues/4188) +- Change default to not trigger a promise rejection for transactions with a specified handler [#4195](https://github.com/knex/knex/issues/4195) +- Make toSQL().toNative() work for Raw to match the API for QueryBuilder [#4058](https://github.com/knex/knex/issues/4058) +- Allow 'match' operator [#3569](https://github.com/knex/knex/issues/3569) +- Support optimizer hints [#4243](https://github.com/knex/knex/issues/4243) +- Add parameter to prevent autoincrement columns from being primary keys [#4266](https://github.com/knex/knex/issues/4266) +- Make "first" and "pluck" mutually exclusive [#4280](https://github.com/knex/knex/issues/4280) +- Added merge strategy to allow selecting columns to upsert. [#4252](https://github.com/knex/knex/issues/4252) +- Throw error if the array passed to insert is empty [#4289](https://github.com/knex/knex/issues/4289) +- Events: introduce queryContext on query-error [#4301](https://github.com/knex/knex/issues/4301) +- CLI: Use UTC timestamp for new migrations [#4245](https://github.com/knex/knex/issues/4245) +- MSSQL: Replace MSSQL dialect with Tedious.js implementation [#2857](https://github.com/knex/knex/issues/2857) [#4281](https://github.com/knex/knex/issues/4281) +- MSSQL: Use "nvarchar(max)" for ".json()" [#4278](https://github.com/knex/knex/issues/4278) +- MSSQL: Schema builder - add predictable constraint names for default values [#4319](https://github.com/knex/knex/issues/4319) +- MSSQL: Schema builder - attempt to drop default constraints when changing default value on columns [#4321](https://github.com/knex/knex/issues/4321) +- SQLite: Fallback to json for sqlite3 when using jsonb [#4186](https://github.com/knex/knex/issues/4186) +- SQLite: Return complete list of DDL commands for creating foreign keys [#4194](https://github.com/knex/knex/issues/4194) +- SQLite: Support dropping composite foreign keys [#4202](https://github.com/knex/knex/issues/4202) +- SQLite: Recreate indices when altering a table [#4277](https://github.com/knex/knex/issues/4277) +- SQLite: Add support for altering columns [#4322](https://github.com/knex/knex/issues/4322) + +**Bug fixes** + +- Fix issue with .withSchema usage with joins on a subquery [#4267](https://github.com/knex/knex/issues/4267) +- Fix issue with schema usage with FROM clause contain QueryBuilder, function or Raw [#4268](https://github.com/knex/knex/issues/4268) +- CLI: Address raised security warnings by dropping liftoff [#4122](https://github.com/knex/knex/issues/4122) +- CLI: Fix an issue with npm@7 and ESM when `type` was set to `'module'` in `package.json` [#4295](https://github.com/knex/knex/issues/4295) +- PostgreSQL: Add check to only create native enum once [#3658](https://github.com/knex/knex/issues/3658) +- SQLite: Fix foreign key "on delete" when altering a table [#4225](https://github.com/knex/knex/issues/4225) +- SQLite: Made the constraint detection case-insensitive [#4330](https://github.com/knex/knex/issues/4330) +- MySQL: Keep auto increment after rename [#4266](https://github.com/knex/knex/issues/4266) +- MSSQL: don't raise query-error twice [#4314](https://github.com/knex/knex/issues/4314) +- MSSQL: Alter column must have its own query [#4317](https://github.com/knex/knex/issues/4317) + +**Typings** + +- TypeScript 4.1+ is now required +- Add missing onConflict overrides [#4182](https://github.com/knex/knex/issues/4182) +- Introduce the "infamous triplet" export [#4181](https://github.com/knex/knex/issues/4181) +- Fix type definition of Transaction [#4172](https://github.com/knex/knex/issues/4172) +- Add typedefinitions for havingNotIn [#4265](https://github.com/knex/knex/issues/4265) +- Include 'name' property in MigratorConfig [#4300](https://github.com/knex/knex/issues/4300) +- Improve join and conflict types [#4318](https://github.com/knex/knex/issues/4318) +- Fix ArrayIfAlready type [#4331](https://github.com/knex/knex/issues/4331) + +**Test / internal changes** + +- Drop global Knex.raw [#4180](https://github.com/knex/knex/issues/4180) +- Stop using legacy url.parse API [#3702](https://github.com/knex/knex/issues/3702) +- Various internal refactorings [#4175](https://github.com/knex/knex/issues/4175) [#4177](https://github.com/knex/knex/issues/4177) [#4178](https://github.com/knex/knex/issues/4178) [#4192](https://github.com/knex/knex/issues/4192) +- Refactor to classes [#4190](https://github.com/knex/knex/issues/4190) [#4191](https://github.com/knex/knex/issues/4191) [#4193](https://github.com/knex/knex/issues/4193) [#4210](https://github.com/knex/knex/issues/4210) [#4253](https://github.com/knex/knex/issues/4253) +- Move transaction type tests to TSD [#4208](https://github.com/knex/knex/issues/4208) +- Clean up destroy logic [#4248](https://github.com/knex/knex/issues/4248) +- Colorize code snippets in readme files [#4234](https://github.com/knex/knex/issues/4234) +- Add "Ecosystem" documentation for Knex plugins [#4183](https://github.com/knex/knex/issues/4183) +- Documentation cleanup +- SQLite: Use SQLite "rename column" instead of a DDL helper [#4200](https://github.com/knex/knex/issues/4200) +- SQLite: Simplify reinsert logic when altering a table [#4272](https://github.com/knex/knex/issues/4272) + +### 0.21.19 - 02 March, 2021 + +- SQLite: Made the constraint detection case-insensitive [#4332](https://github.com/knex/knex/issues/4332) + +### 0.21.18 - 22 February, 2021 + +- CLI: Fix an issue with npm@7 and ESM when type was set to 'module' in package.json [#4295](https://github.com/knex/knex/issues/4295) + +### 0.21.17 - 30 January, 2021 + +**Bug fixes** + +- SQLite: Fix SQLite foreign on delete when altering a table [#4261](https://github.com/knex/knex/issues/4261) + +**New features** + +- Add support for optimizer hints (see https://github.com/knex/documentation/pull/306 for documentation) [#4243](https://github.com/knex/knex/issues/4243) + +### 0.21.16 - 17 January, 2021 + +**Bug fixes** + +- MSSQL: Avoid passing unsupported pool param. Fixes node-mssql 7+ support [#4236](https://github.com/knex/knex/issues/4236) + +### 0.21.15 - 26 December, 2020 + +**New features** + +- SQLite: Add primary/foreign support on alterTable [#4162](https://github.com/knex/knex/issues/4162) +- SQLite: Add dropPrimary/dropForeign support on alterTable [#4162](https://github.com/knex/knex/issues/4162) + +**Typings** + +- Add "after" and "first" to columnBuilder types [#3549](https://github.com/knex/knex/issues/3549) [#4169](https://github.com/knex/knex/issues/4169) + +**Test / internal changes** + +- Extract knex config resolution logic [#4166](https://github.com/knex/knex/issues/4166) +- Run CI using GitHub Actions [#4168](https://github.com/knex/knex/issues/4168) +- Add Node.js 15 to CI matrix [#4173](https://github.com/knex/knex/issues/4173) + +### 0.21.14 - 18 December, 2020 + +**New features** + +- MSSQL: support "returning" on inserts, updates and deletes on tables with triggers [#4152](https://github.com/knex/knex/issues/4152) +- Use esm import if package.json type is "module" [#4158](https://github.com/knex/knex/issues/4158) + +**Bug fixes** + +- Make sure query-response and query-error events contain \_knexTxId [#4160](https://github.com/knex/knex/issues/4160) + +**Test / internal changes** + +- Improved integration test framework [#4161](https://github.com/knex/knex/issues/4161) + +### 0.21.13 - 12 December, 2020 + +**New features** + +- SQLite: Add support for `dropForeign` [#4092](https://github.com/knex/knex/issues/4092) +- Add support for WHERE clauses to "upsert" queries [#4148](https://github.com/knex/knex/issues/4148) + +**Bug fixes** + +- MSSQL: Avoid connection getting stuck on socket hangup [#4157](https://github.com/knex/knex/issues/4157) +- Oracle: Support specifying non-default DB port [#4147](https://github.com/knex/knex/issues/4147) +- Oracle: Support inserts with only default values (empty body) [#4092](https://github.com/knex/knex/issues/4092) +- CLI: fix irregular seed file execution order [#4156](https://github.com/knex/knex/issues/4156) +- Fix performance of asyncStackTraces with enable-source-maps node flag [#4154](https://github.com/knex/knex/issues/4154) + +**Typings** + +- PostgreSQL: Add support for application_name [#4153](https://github.com/knex/knex/issues/4153) +- Fix types for insert to allow array [#4105](https://github.com/knex/knex/issues/4105) +- Add types for userParams and withUserParams [#4119](https://github.com/knex/knex/issues/4119) +- Added type for withKeyName [#4139](https://github.com/knex/knex/issues/4139) +- Fix batchInsert definitions [#4131](https://github.com/knex/knex/issues/4131) +- Fix types for WhereIn signature (value or query builder) [#3863](https://github.com/knex/knex/issues/3863) +- Add types for connection config of mysql2 driver [#4144](https://github.com/knex/knex/issues/4144) + +**Test / internal changes** + +- Move TS tests to tsd (WIP) [#4109](https://github.com/knex/knex/issues/4109) [#4110](https://github.com/knex/knex/issues/4110) + +### 0.21.12 - 02 November, 2020 + +**Typings** + +- Reintroduce support for globally defining table/record mapping [#4100](https://github.com/knex/knex/issues/4100) +- Add a few missing types for MSSQL Connection [#4103](https://github.com/knex/knex/issues/4103) +- Make .ignore() and .merge() return QueryBuilder rather than QueryInterface [#4102](https://github.com/knex/knex/issues/4102) +- Use tarn config TS types instead of generic-pool [#4064](https://github.com/knex/knex/issues/4064) + +### 0.21.11 - 01 November, 2020 + +**Typings** + +- Revert support for globally defining table/record mapping [#4099](https://github.com/knex/knex/issues/4099) + +### 0.21.10 - 31 October, 2020 + +**New features** + +- Upsert support (Postgres/MySQL/Sqlite) [#3763](https://github.com/knex/knex/issues/3763) + +**Bug fixes** + +- Switch to non-uuid knexQueryUids to avoid issues when mocking global date [#4089](https://github.com/knex/knex/issues/4089) + +**Typings** + +- Allow to globally define table/record mapping [#4071](https://github.com/knex/knex/issues/4071) + +### 0.21.9 - 27 October, 2020 + +**New features** + +- add method clear(statement) to QueryBuilder [#4051](https://github.com/knex/knex/issues/4051) + +**Bug fixes** + +- CLI: fix help text being printed twice [#4072](https://github.com/knex/knex/issues/4072) +- Oracle: columnInfo() no longer requires an Owner User [#4053](https://github.com/knex/knex/issues/4053) +- Add missing "start" event propagation from transaction [#4087](https://github.com/knex/knex/issues/4087) + +### 0.21.8 - 27 October, 2020 + +**Bug fixes** + +- MSSQL: Escape properly if literal '?' is needed [#4053](https://github.com/knex/knex/issues/4053) +- Make toQuery behavior consistent with pre-0.21.7 (do not break on empty builder) [#4083](https://github.com/knex/knex/issues/4083) +- Fix comment escaping for MySQL and PostgreSQL [#4084](https://github.com/knex/knex/issues/4084) + +### 0.21.7 - 25 October, 2020 + +**New features** + +- CLI: Add migration stub for .cjs extension [#4065](https://github.com/knex/knex/issues/4065) + +**Bug fixes** + +- MSSQL: Add dynamic scaling for decimal values and prevents a UInt64 overflow [#3910](https://github.com/knex/knex/issues/3910) +- MSSQL: Fix apostrophe escaping [#4077](https://github.com/knex/knex/issues/4077) +- Ensure that semicolon is not appended to statements that already end with a semicolon [#4052](https://github.com/knex/knex/issues/4052) + +**Typings** + +- Add arguments to QueryCallback in Where [#4034](https://github.com/knex/knex/issues/4034) + +**Test / internal changes** + +- Replace lodash type-checks with native solutions [#4056](https://github.com/knex/knex/issues/4056) +- Replace mkdirp with native recursive flag [#4060](https://github.com/knex/knex/issues/4060) +- Replace inherits package with builtin utility [#4059](https://github.com/knex/knex/issues/4059) + +### 0.21.6 - 27 September, 2020 + +**New features** + +- CLI: New config parameter / CLI flag to prefixing seed filename with timestamp [#3873](https://github.com/knex/knex/issues/3873) +- CLI: throw an error when specific seed file cannot be found [#4011](https://github.com/knex/knex/issues/4011) +- Warn if whereNot is used with 'in' or 'between' [#4038](https://github.com/knex/knex/issues/4038) + +**Bug fixes** + +- CLI: Fix double merging of config for migrator [#4040](https://github.com/knex/knex/issues/4040) + +**Typings** + +- Unify SeedsConfig and SeederConfig [#4003](https://github.com/knex/knex/issues/4003) +- Allow string[] type for directory in SeedsConfig [#4033](https://github.com/knex/knex/issues/4033) + +### 0.21.5 - 17 August, 2020 + +**New features** + +- CLI: Improve Esm interop [#3985](https://github.com/knex/knex/issues/3985) +- CLI: Improve mjs module support [#3980](https://github.com/knex/knex/issues/3980) + +**Test / internal changes** + +- Bump version of dtslint [#3984](https://github.com/knex/knex/issues/3984) +- Test/document esm interop mixed formats (knexfile/migrations/seeds) [#3986](https://github.com/knex/knex/issues/3986) + +### 0.21.4 - 10 August, 2020 + +**New features** + +- CLI: Add new option for seed: recursive [#3974](https://github.com/knex/knex/issues/3974) + +**Bug fixes** + +- CLI: Do not load seeds from subfolders recursively by default [#3974](https://github.com/knex/knex/issues/3974) + +### 0.21.3 - 08 August, 2020 + +**New features** + +- CLI: Support multiple directories for seeds [#3967](https://github.com/knex/knex/issues/3967) + +**Bug fixes** + +- Ensure DB stream is destroyed when the PassThrough is destroyed [#2324](https://github.com/knex/knex/issues/2324) +- Support postProcessResponse for streams [#3931](https://github.com/knex/knex/issues/3931) +- Fix ESM module interop for calling module/package of type 'module' [#3938](https://github.com/knex/knex/issues/3938) +- CLI: Fix migration source name in rollback all [#3956](https://github.com/knex/knex/issues/3956) +- Fix getMergedConfig calls to include client logger [#3920](https://github.com/knex/knex/issues/3920) +- Escape single quoted values passed to defaultTo function [#3899](https://github.com/knex/knex/issues/3899) + +**Typings** + +- Add .timeout(ms) to .raw()'s typescript typings [#3885](https://github.com/knex/knex/issues/3885) +- Add typing for double table column builder [#3950](https://github.com/knex/knex/issues/3950) +- Add a phantom tag to Ref type to mark received type parameters as used [#3934](https://github.com/knex/knex/issues/3934) +- Add `null` as valid binding type [#3946](https://github.com/knex/knex/issues/3946) + +**Test / internal changes** + +- Change query lab link to https [#3933](https://github.com/knex/knex/issues/3933) + +### 0.21.2 - 10 July, 2020 + +**New features** + +- Warn user if custom migration source is being reset [#3839](https://github.com/knex/knex/issues/3839) +- Prefer `void` as return type on migration generator ts stub [#3865](https://github.com/knex/knex/issues/3865) +- MSSQL: Added the removal of a columns default constraint, before dropping the column [#3855](https://github.com/knex/knex/issues/3855) + +**Typings** + +- Fix definition for raw querybuilders [#3846](https://github.com/knex/knex/issues/3846) + +**Test / internal changes** + +- Refactor migration logic to use async/await [#3838](https://github.com/knex/knex/issues/3838) + +### 0.21.1 - 28 April, 2020 + +**New features** + +- CLI: Add migrate:unlock command, truncate on forceFreeMigrationsLock [#3822](https://github.com/knex/knex/issues/3822) +- CLI: Add support for cjs files by default [#3829](https://github.com/knex/knex/issues/3829) + +**Bug fixes** + +- CLI: Fix inference of seed/migration extension from knexfile extension [#3814](https://github.com/knex/knex/issues/3814) +- rewrite delay to not node-only version. Fixes compatibility with browsers [#3820](https://github.com/knex/knex/issues/3820) + +**Test / internal changes** + +- Update dependencies. Explicitly support Node.js 14 [#3825](https://github.com/knex/knex/issues/3825) [#3830](https://github.com/knex/knex/issues/3830) + +### 0.21.0 - 18 April, 2020 + +**Improvements** + +- Reduce size of lodash in bundle [#3804](https://github.com/knex/knex/issues/3804) + +**Breaking changes** + +- Dropped support for Node 8 +- Breaking upstream change in `pg-query-stream`: `Changed stream.close to stream.destroy which is the official way to terminate a readable stream. This is a breaking change if you rely on the stream.close method on pg-query-stream...though should be just a find/replace type operation to upgrade as the semantics remain very similar (not exactly the same, since internals are rewritten, but more in line with how streams are "supposed" to behave).` + +**Test / internal changes** + +- Updated Tarn.js to a version 3.0.0 +- Updated mkdirp to a version 1.0.4 +- Updated examples to use ES2015 style [#3810](https://github.com/knex/knex/issues/3810) + +### 0.20.15 - 16 April, 2020 + +**Bug fixes** + +- Support for `.finally(..)` on knex's Promise-alikes [#3800](https://github.com/knex/knex/issues/3800) + +**Typings** + +- Add types for `.distinctOn` [#3784](https://github.com/knex/knex/issues/3784) + +### 0.20.14 - 13 April, 2020 + +**New features** + +- CLI: adds support for asynchronous knexfile loading [#3748](https://github.com/knex/knex/issues/3748) +- Add clearGroup method [#3771](https://github.com/knex/knex/issues/3771) + +**Typings** + +- Support Raw types for insert, where, update [#3730](https://github.com/knex/knex/issues/3730) +- Add typings for MigrationSource [#3756](https://github.com/knex/knex/issues/3756) +- Update signature of orderBy to support QueryBuilder inside array [#3757](https://github.com/knex/knex/issues/3757) +- Add toSQL and toString to SchemaBuilder [#3758](https://github.com/knex/knex/issues/3758) +- `interface Knex` and `function Knex` should have the same types [#3787](https://github.com/knex/knex/issues/3787) +- Fix minor issues around typings [#3765](https://github.com/knex/knex/issues/3765) + +**Test / internal changes** + +- Minor test internal enhancements [#3747](https://github.com/knex/knex/issues/3747) +- Minor improvements on the usage of fs utilities [#3749](https://github.com/knex/knex/issues/3749) +- Split tests in groups [#3785](https://github.com/knex/knex/issues/3785) + +### 0.20.13 - 23 March, 2020 + +**Bug fixes** + +- Correctly handle dateToString escaping without timezone passed [#3742](https://github.com/knex/knex/issues/3742) +- Make protocol length check more defensive [#3744](https://github.com/knex/knex/issues/3744) + +**Typings** + +- Make the ChainableInterface conform to `Promise` [#3724](https://github.com/knex/knex/issues/3724) + +### 0.20.12 - 19 March, 2020 + +**Bug fixes** + +- Added missing call to \_reject in Transactor#transaction [#3706](https://github.com/knex/knex/issues/3706) +- Fix method binding on knex proxy [#3717](https://github.com/knex/knex/issues/3717) +- Oracle: Transaction_OracleDB can use config.connection [#3731](https://github.com/knex/knex/issues/3731) + +**Typings** + +- Fix incorrect type signature of Having [#3719](https://github.com/knex/knex/issues/3719) + +**Test / internal changes** + +- Cleanup/remove transaction stalling [#3716](https://github.com/knex/knex/issues/3716) +- Rewrote Transaction#acquireConnection() methods to use async [#3707](https://github.com/knex/knex/issues/3707) + +### 0.20.11 - 26 February, 2020 + +**Breaking changes** + +- Knex returns native JS promises instead of Bluebird ones. This means that you no longer use such methods as `map`, `spread` and `reduce` on QueryBuilder instance. + +**New features** + +- Oracle: Add OracleDB handling for buffer type in fetchAsString [#3685](https://github.com/knex/knex/issues/3685) + +**Bug fixes** + +- Fix race condition in non-container transactions [#3671](https://github.com/knex/knex/issues/3671) + +**Typings** + +- Mark knex arguments of composite/collection types to be readonly [#3680](https://github.com/knex/knex/issues/3680) + +**Test / internal changes** + +- Remove dependency on Bluebird methods from sources [#3683](https://github.com/knex/knex/issues/3683) +- Cleanup and extract Transaction Workflow logic [#3674](https://github.com/knex/knex/issues/3674) + +### 0.20.10 - 13 February, 2020 + +**Bug fixes** + +- Oracle: commit was a no-op causing race conditions [#3668](https://github.com/knex/knex/issues/3668) +- CLI: Knex calls process.chdir() before opening Knexfile [#3661](https://github.com/knex/knex/issues/3661) +- Fixed unresolved promise in cancelQuery() [#3666](https://github.com/knex/knex/issues/3666) + +**Typings** + +- `fn.now` takes optionally a precision argument. [#3662](https://github.com/knex/knex/issues/3662) +- PG: Include SSL in connection definition [#3659](https://github.com/knex/knex/issues/3659) + +**Test / internal changes** + +- replace Bluebird.timeout [#3634](https://github.com/knex/knex/issues/3634) + +### 0.20.9 - 08 February, 2020 + +**Bug fixes** + +- CLI: Improve Support for Liftoff's Preloaders - this should fix some cases like using TS for your migrations [#3613](https://github.com/knex/knex/issues/3613) + +**Typings** + +- MSSQL: Add `enableArithAbort` to `MsSqlConnectionConfig` + +**Test / internal changes** + +- Refactor more tests to use cli-testlab [#3640](https://github.com/knex/knex/issues/3640) +- Update QueryCompiler implementation to use classes [#3647](https://github.com/knex/knex/issues/3647) + +### 0.20.8 - 14 January, 2020 + +**New features** + +- CLI: Support ES6 modules via flag --esm [#3616](https://github.com/knex/knex/issues/3616) + +**Bug fixes** + +- CLI: Print help only when there are no arguments [#3617](https://github.com/knex/knex/issues/3617) + +**Typings** + +- Fix incorrect type of QueryBuilder.first('\*') result [#3621](https://github.com/knex/knex/issues/3621) + +### 0.20.7 - 07 January, 2020 + +**New features** + +- Throw better error when trying to modify schema while using unsupported dialect [#3609](https://github.com/knex/knex/issues/3609) + +**Bug fixes** + +- Oracle: dispose connection on connection error [#3611](https://github.com/knex/knex/issues/3611) +- Oracle: fix not releasing connection from pool on disconnect [#3605](https://github.com/knex/knex/issues/3605) +- CLI: prevent warning with root command [#3604](https://github.com/knex/knex/issues/3604) + +**Typings** + +- Add create/drop schema methods to SchemaBuilder [#3579](https://github.com/knex/knex/issues/3579) + +### 0.20.6 - 29 December, 2019 + +**Bug fixes** + +- Enforce Unix (lf) line terminators [#3598](https://github.com/knex/knex/issues/3598) + +### 0.20.5 - 29 December, 2019 + +**New features** + +- Return more information about empty updates [#3597](https://github.com/knex/knex/issues/3597) + +**Bug fixes** + +- Fix colors in debug logs [#3592](https://github.com/knex/knex/issues/3592) + +**Test / internal changes** + +- Use more efficient algorithm for generating internal ids [#3595](https://github.com/knex/knex/issues/3595) [#3596](https://github.com/knex/knex/issues/3596) +- Use Buffer.alloc() instead of deprecated constructor [#3574](https://github.com/knex/knex/issues/3574) + +### 0.20.4 - 08 December, 2019 + +**Bug fixes** + +- Fix debug logger messing up queries with % [#3566](https://github.com/knex/knex/issues/3566) +- Make logger methods mutually consistent [#3567](https://github.com/knex/knex/issues/3567) + +**Typings** + +- Add missing methods to client type [#3565](https://github.com/knex/knex/issues/3565) +- Fix queryContext function defintion [#3562](https://github.com/knex/knex/issues/3562) +- Fix QueryBuilder.extend this type [#3526](https://github.com/knex/knex/issues/3526) [#3528](https://github.com/knex/knex/issues/3528) + +**Test / internal changes** + +- Remove bluebird.using [#3552](https://github.com/knex/knex/issues/3552) + +### 0.20.3 - 27 November, 2019 + +**New features** + +- MSSQL, MySQL: Add connection string qs to connection params [#3547](https://github.com/knex/knex/issues/3547) + +**Bug fixes** + +- Oracle: Fix issue retrieving BLOB from database [#3545](https://github.com/knex/knex/issues/3545) +- PostgreSQL: Timeout for postgresql use cancel instead of terminate [#3518](https://github.com/knex/knex/issues/3518) +- Make sure CLI works for namespaced knex packages [#2539](https://github.com/knex/knex/issues/2539) + +**Typings** + +- Lift up dialect specific methods in the CreateTableBuilder [#3532](https://github.com/knex/knex/issues/3532) +- Add client property to QueryBuilder type [#3541](https://github.com/knex/knex/issues/3541) +- Support 'only' option [#3551](https://github.com/knex/knex/issues/3551) + +### 0.20.2 - 14 November, 2019 + +**New features** + +- Add support for distinct on for postgres [#3513](https://github.com/knex/knex/issues/3513) + +**Bug fixes** + +- Make sqlite3 hasColumn case insensitive [#3435](https://github.com/knex/knex/issues/3435) + +**Typings** + +- Fix PoolConfig typing [#3505](https://github.com/knex/knex/issues/3505) +- Expand SeedsConfig types [#3531](https://github.com/knex/knex/issues/3531) +- Make the default type parameters of QueryBuilder less strict [#3520](https://github.com/knex/knex/issues/3520) +- Fix regression in older version of node when Promise#finally was not available [#3507](https://github.com/knex/knex/issues/3507) + +### 0.20.1 - 29 October, 2019 + +**New features** + +- Declare drivers as optional peerDependencies [#3081](https://github.com/knex/knex/issues/3081) +- Dynamic connection configuration resolution [#3497](https://github.com/knex/knex/issues/3497) + +**Bug fixes** + +- Wrap subQuery with parenthesis when it appears as table name [#3496](https://github.com/knex/knex/issues/3496) +- Fix Oracle error codes [#3498](https://github.com/knex/knex/issues/3498) + +**Typings** + +- Add interface for PG Connection object [#3372](https://github.com/knex/knex/issues/3372) +- Gracefully handle global promise pollution [#3502](https://github.com/knex/knex/issues/3502) + +### 0.20.0 - 25 October, 2019 + +**New features** + +- orderBy accepts QueryBuilder [#3491](https://github.com/knex/knex/issues/3491) +- Add validation in `.offset()` [#2908](https://github.com/knex/knex/issues/2908) +- disable_migrations_list_validation feature [#3448](https://github.com/knex/knex/issues/3448) + +**Bug fixes** + +- Fix oracledb driver v4 support [#3480](https://github.com/knex/knex/issues/3480) +- Fix some issues around seed and migration generation [#3479](https://github.com/knex/knex/issues/3479) +- Fix bugs in replacement logic used when dropping columns in SQLite [#3476](https://github.com/knex/knex/issues/3476) + +**Typings** + +- Add types to the Migrator interface [#3459](https://github.com/knex/knex/issues/3459) +- Fix typings of index and dropIndex TableBuilder methods [#3486](https://github.com/knex/knex/issues/3486) +- Fixes types for Seeder#run [#3438](https://github.com/knex/knex/issues/3438) + +**Test / internal changes** + +- Execute CI on Node.js 13 +- Bluebird: remove usage of `return`, `reflect`, `fromCallback` methods [#3483](https://github.com/knex/knex/issues/3483) +- Bluebird: remove Bluebird.bind [#3477](https://github.com/knex/knex/issues/3477) +- Bluebird: use util.promisify instead of Bluebird.promisify [#3470](https://github.com/knex/knex/issues/3470) +- Bluebird: remove Bluebird.each [#3471](https://github.com/knex/knex/issues/3471) +- Bluebird: remove Bluebird.map and Bluebird.mapSeries [#3474](https://github.com/knex/knex/issues/3474) +- Bluebird: replace Bluebird.map with Promise.all [#3469](https://github.com/knex/knex/issues/3469) +- Update badges [#3482](https://github.com/knex/knex/issues/3482) + +### 0.19.5 - 06 October, 2019 + +**New features** + +- CLI: Migrations up/down commands - filename parameter [#3416](https://github.com/knex/knex/issues/3416) +- Oracle: Support stored procedures [#3449](https://github.com/knex/knex/issues/3449) + +**Bug fixes** + +- MSSQL: Escape column ids correctly in all cases (reported by Snyk Security Research Team) [#3382](https://github.com/knex/knex/issues/3382) +- SQLite: Fix handling of multiline SQL in SQLite3 schema [#3411](https://github.com/knex/knex/issues/3411) +- Fix concurrent child transactions failing [#2213](https://github.com/knex/knex/issues/2213) [#3440](https://github.com/knex/knex/issues/3440) + +**Typings** + +- Add missing Migrator.list typing [#3460](https://github.com/knex/knex/issues/3460) +- Fix Typescript type inference for to better support wildcard (\*) calls [#3444](https://github.com/knex/knex/issues/3444) +- Make options argument optional in timeout [#3442](https://github.com/knex/knex/issues/3442) + +**Test / internal changes** + +- Enable linting in CI [#3450](https://github.com/knex/knex/issues/3450) + +### 0.19.4 - 09 September, 2019 + +**New features** + +- Add undefined columns to undefined binding(s) error [#3425](https://github.com/knex/knex/issues/3425) + +**Typings** + +- Add `specific` to SeederConfig type [#3429](https://github.com/knex/knex/issues/3429) +- Fix some issues with QueryBuilder types [#3427](https://github.com/knex/knex/issues/3427) + +### 0.19.3 - 25 August, 2019 + +**Bug fixes** + +- Fix migrations for native enums to use table schema [#3307](https://github.com/knex/knex/issues/3307) + +**New features** + +- Add ability to manually define schema for native enums [#3307](https://github.com/knex/knex/issues/3307) +- Add SSL/TLS support for Postgres connection string [#3410](https://github.com/knex/knex/issues/3410) +- CLI: new command that lists all migrations with status [#3390](https://github.com/knex/knex/issues/3390) + +**Typings** + +- Include schemaName in EnumOptions [#3415](https://github.com/knex/knex/issues/3415) +- Allow `ColumnBuilder.defaultTo()` to be `null` [#3407](https://github.com/knex/knex/issues/3407) + +**Other Changes** + +- migrate: Refactor \_lockMigrations to avoid forUpdate - makes migrations compatible with CockroachDB [#3395](https://github.com/knex/knex/issues/3395) + +### 0.19.2 - 17 August, 2019 + +**Other Changes** + +- Make transaction rejection consistent across dialects [#3399](https://github.com/knex/knex/issues/3399) +- More consistent handling of nested transactions [#3393](https://github.com/knex/knex/issues/3393) + +**New features** + +- Fallback to JSON when using JSONB in MySQL [#3394](https://github.com/knex/knex/issues/3394) + +### 0.19.1 - 23 July, 2019 + +**New features** + +- Allow to extend knex query builder [#3334](https://github.com/knex/knex/issues/3334) +- Add .isCompleted() to transaction [#3368](https://github.com/knex/knex/issues/3368) +- Minor enhancements around aliasing of aggregates [#3354](https://github.com/knex/knex/issues/3354) + +**Typings** + +- Update configuration typings to allow for oracle db connectionstring [#3361](https://github.com/knex/knex/issues/3361) +- Update Knex.raw type to be any by default because the actual type is dialect specific [#3349](https://github.com/knex/knex/issues/3349) + +### 0.19.0 - 11 July, 2019 + +**Other Changes** + +- Pooling: tarn.js connection pool was updated to version 2.0.0. This fixes issue with destroying connections and introduces support for connection pool event handlers. Please see tarn.js documentation for more details [#3345](https://github.com/knex/knex/issues/3345) +- Pooling: Passing unsupported pooling configuration options now throws an error +- Pooling: `beforeDestroy` configuration option was removed + +### 0.18.4 - 10 July, 2019 + +**New features** + +- Seeds: Option to run specific seed file [#3335](https://github.com/knex/knex/issues/3335) +- Implement "skipLocked()" and "noWait()" [#2961](https://github.com/knex/knex/issues/2961) + +**Bug fixes** + +- CLI: Respect the knexfile stub option while generating a migration [#3337](https://github.com/knex/knex/issues/3337) +- Fix mssql import not being ignored, breaking webpack builds [#3336](https://github.com/knex/knex/issues/3336) + +### 0.18.3 - 04 July, 2019 + +**New features** + +- CLI: add --stub option to migration:make [#3316](https://github.com/knex/knex/issues/3316) + +**Bug fixes** + +- Fix return duplicate transaction promise for standalone transactions [#3328](https://github.com/knex/knex/issues/3328) + +### 0.18.2 - 03 July, 2019 + +**Bug fixes** + +- Fix remove duplicate transaction rejection [#3324](https://github.com/knex/knex/issues/3324) +- Fix issues around specifying default values for columns [#3318](https://github.com/knex/knex/issues/3318) +- CLI: Fix empty --version output [#3312](https://github.com/knex/knex/issues/3312) + +### 0.18.1 - 30 June, 2019 + +**Bug fixes** + +- Do not reject duplicate promise on transaction rollback [#3319](https://github.com/knex/knex/issues/3319) + +### 0.18.0 - 26 June, 2019 + +**Bug fixes** + +- Do not reject promise on transaction rollback (by default only for new, non-callback, style of transactions for now to avoid breaking old code) [#3235](https://github.com/knex/knex/issues/3235) + +**New features** + +- Added `doNotRejectOnRollback` options for starting transactions, to prevent rejecting promises on rollback for callback-style transactions. +- Use extension from knexfile for generating migrations unless overriden [#3282](https://github.com/knex/knex/issues/3282) +- Use migrations.extension from config when generating migration [#3242](https://github.com/knex/knex/issues/3242) +- Expose executionPromise for transactors [#3297](https://github.com/knex/knex/issues/3297) + +**Bug fixes** + +- Oracle: Updated handling of connection errors for disposal [#2608](https://github.com/knex/knex/issues/2608) +- Fix extension resolution from env configs [#3294](https://github.com/knex/knex/issues/3294) + +**Test / internal changes** + +- Drop support for Node.js 6 [#3227](https://github.com/knex/knex/issues/3227) +- Remove Babel [#3227](https://github.com/knex/knex/issues/3227) +- Remove Bluebird [#3290](https://github.com/knex/knex/issues/3290) [#3287](https://github.com/knex/knex/issues/3287) [#3285](https://github.com/knex/knex/issues/3285) [#3267](https://github.com/knex/knex/issues/3267) [#3266](https://github.com/knex/knex/issues/3266) [#3263](https://github.com/knex/knex/issues/3263) +- Fix comments that were modified by find & replace [#3308](https://github.com/knex/knex/issues/3308) + +**Typings** + +- Add workarounds for degraded inference when strictNullChecks is set to false [#3275](https://github.com/knex/knex/issues/3275) +- Add stub type definition for Migrator config [#3279](https://github.com/knex/knex/issues/3279) +- Add stub to seeds type [#3296](https://github.com/knex/knex/issues/3296) +- Fix MSSQL config typings [#3269](https://github.com/knex/knex/issues/3269) +- Add pgsql specific table builder method typings [#3146](https://github.com/knex/knex/issues/3146) + +### 0.17.5 - 8 June, 2019 + +**Typings** + +- Include result.d.ts in published package [#3271](https://github.com/knex/knex/issues/3271) + +### 0.17.4 - 8 June, 2019 + +**Typings** + +- Fix some cases of left-to-right inference causing type mismatch [#3265](https://github.com/knex/knex/issues/3265) +- Improve count typings [#3249](https://github.com/knex/knex/issues/3249) + +**Bug fixes** + +- Fix error message bubbling up on seed error [#3248](https://github.com/knex/knex/issues/3248) + +### 0.17.3 - 2 June, 2019 + +**Typings** + +- Improve typings for aggregations [#3245](https://github.com/knex/knex/issues/3245) +- Add decimalNumbers to MySqlConnectionConfig interface [#3244](https://github.com/knex/knex/issues/3244) + +### 0.17.2 - 1 June, 2019 + +**Typings** + +- Improve count typings [#3239](https://github.com/knex/knex/issues/3239) + +**Bug fixes** + +- "colorette" dependency breaks browserify builds [#3238](https://github.com/knex/knex/issues/3238) + +### 0.17.1 - 31 May, 2019 + +**New features** + +- Add migrate:down functionality [#3228](https://github.com/knex/knex/issues/3228) + +**Typings** + +- Update type of aggregation results to not be arrays when first has been invoked before [#3237](https://github.com/knex/knex/issues/3237) +- Include undefined in type of single row results [#3231](https://github.com/knex/knex/issues/3231) +- Fix incorrect type definitions for single row queries [#3230](https://github.com/knex/knex/issues/3230) + +### 0.17.0 - 28 May, 2019 + +**New features** + +- Add support for returning started transaction without immediately executing it [#3099](https://github.com/knex/knex/issues/3099) +- Add support for passing transaction around with only starting it when needed [#3099](https://github.com/knex/knex/issues/3099) +- Add clearHaving function [#3141](https://github.com/knex/knex/issues/3141) +- Add --all flag for rollback in CLI [#3187](https://github.com/knex/knex/issues/3187) +- Add error detail log to knex CLI [#3149](https://github.com/knex/knex/issues/3149) +- Support multi-column whereIn in sqlite through values clause [#3220](https://github.com/knex/knex/issues/3220) +- Allow users to specify the migrations "tableName" parameter via the CLI [#3214](https://github.com/knex/knex/issues/3214) +- Unify object options handling for datetime/timestamp across dialects [#3181](https://github.com/knex/knex/issues/3181) +- Add "up" command for migrations [#3205](https://github.com/knex/knex/issues/3205) + +**Typings** + +- Add default values for generic types (fixes backwards compatibility broken by 0.16.6) [#3189](https://github.com/knex/knex/issues/3189) +- Make function types generic in type definitions [#3168](https://github.com/knex/knex/issues/3168) +- Add missing types to MigratorConfig [#3174](https://github.com/knex/knex/issues/3174) +- Add types for havingBetween, orHavingBetween, havingNotBetween and orHavingNotBetween [#3144](https://github.com/knex/knex/issues/3144) +- Update Knex.Config types to include log [#3221](https://github.com/knex/knex/issues/3221) +- Fix some more cases of missing typings [#3223](https://github.com/knex/knex/issues/3223) +- Support type safe refs [#3215](https://github.com/knex/knex/issues/3215) +- Expose some utility types [#3211](https://github.com/knex/knex/issues/3211) +- Fix issues with typings of joins and some conflicts with Bluebird typings [#3209](https://github.com/knex/knex/issues/3209) + +**Bug fixes** + +- Fix order of migration rollback [#3172](https://github.com/knex/knex/issues/3172) + +**Test / internal changes** + +- Execute CI tests on Node.js 12 [#3171](https://github.com/knex/knex/issues/3171) +- Docker-based test dbs [#3157](https://github.com/knex/knex/issues/3157) +- Use cli-testlab for testing CLI [#3191](https://github.com/knex/knex/issues/3191) + +### 0.16.5 - 11 Apr, 2019 + +- Bundle polyfills with knex for 0.16.x line again [#3139](https://github.com/knex/knex/issues/3139) + +### 0.16.4 - 11 Apr, 2019 + +**New features** + +- Boolean param for rollback() to rollback all migrations [#2968](https://github.com/knex/knex/issues/2968) +- seed:run print the file name of the failing seed [#2972](https://github.com/knex/knex/issues/2972) [#2973](https://github.com/knex/knex/issues/2973) +- verbose option to CLI commands [#2887](https://github.com/knex/knex/issues/2887) +- add intersect() [#3023](https://github.com/knex/knex/issues/3023) +- Improved format for TS stubs [#3080](https://github.com/knex/knex/issues/3080) +- MySQL: Support nullable timestamps [#3100](https://github.com/knex/knex/issues/3100) +- MySQL: Warn `.returning()` does not have any effect [#3039](https://github.com/knex/knex/issues/3039) + +**Bug fixes** + +- Respect "loadExtensions" configuration [#2969](https://github.com/knex/knex/issues/2969) +- Fix event listener duplication when using Migrator [#2982](https://github.com/knex/knex/issues/2982) +- Fix fs-migrations breaking docs [#3022](https://github.com/knex/knex/issues/3022) +- Fix sqlite3 drop/renameColumn() breaks with postProcessResponse [#3040](https://github.com/knex/knex/issues/3040) +- Fix transaction support for migrations [#3084](https://github.com/knex/knex/issues/3084) +- Fix queryContext not being passed to raw queries [#3111](https://github.com/knex/knex/issues/3111) +- Typings: Allow to pass query builders, identifiers and raw in various places as parameters [#2960](https://github.com/knex/knex/issues/2960) +- Typings: toNative() definition [#2996](https://github.com/knex/knex/issues/2996) +- Typings: asCallback() definition [#2963](https://github.com/knex/knex/issues/2963) +- Typings: queryContext() type definition Knex.Raw [#3002](https://github.com/knex/knex/issues/3002) +- Typings: Add "constraintName" arg to primary() definition [#3006](https://github.com/knex/knex/issues/3006) +- Typings: Add missing schemaName in MigratorConfig [#3016](https://github.com/knex/knex/issues/3016) +- Typings: Add missing supported parameter types and toSQL method [#2960](https://github.com/knex/knex/issues/2960) +- Typings: Update enum arguments to reflect latest signature [#3043](https://github.com/knex/knex/issues/3043) +- Typings: Add size parameter to integer method [#3074](https://github.com/knex/knex/issues/3074) +- Typings: Add 'string' as accepted Knex constructor type definition [#3105](https://github.com/knex/knex/issues/3105) +- Typings: Add boolean as a column name in join [#3121](https://github.com/knex/knex/issues/3121) +- Typings: Add missing clearOrder & clearCounters types [#3109](https://github.com/knex/knex/issues/3109) +- Dependencies: Fix security warning [#3082](https://github.com/knex/knex/issues/3082) +- Do not use unsupported column width/length arguments on data types int and tinyint in MSSQL [#2738](https://github.com/knex/knex/issues/2738) + +**Other Changes** + +- Make unionAll()'s call signature match union() [#3055](https://github.com/knex/knex/issues/3055) + +**Test / internal changes** + +- Swap chalk→colorette / minimist→getopts [#2718](https://github.com/knex/knex/issues/2718) +- Always use well documented pg client query() config argument [#3004](https://github.com/knex/knex/issues/3004) +- Do not bundle polyfills with knex [#3024](https://github.com/knex/knex/issues/3024) + +### 0.16.3 - 19 Dec, 2018 + +**Bug fixes** + +- @babel/polyfill loaded multiple times [#2955](https://github.com/knex/knex/issues/2955) +- Resolve migrations and seeds relatively to knexfile directory when specified (the way it used to be before 0.16.1) [#2952](https://github.com/knex/knex/issues/2952) + +### 0.16.2 - 10 Dec, 2018 + +**Bug fixes** + +- Add TypeScript types to the "files" entry so they are properly included in the release [#2943](https://github.com/knex/knex/issues/2943) + +### 0.16.1 - 28 Nov, 2018 + +**Breaking changes** + +- Use datetime2 for MSSQL datetime + timestamp types. This change is incompatible with MSSQL older than 2008 [#2757](https://github.com/knex/knex/issues/2757) +- Knex.VERSION() method was removed, run "require('knex/package').version" instead [#2776](https://github.com/knex/knex/issues/2776) +- Knex transpilation now targets Node.js 6, meaning it will no longer run on older Node.js versions [#2813](https://github.com/knex/knex/issues/2813) +- Add json type support for SQLite 3.9+ (tested to work with Node package 'sqlite3' 4.0.2+) [#2814](https://github.com/knex/knex/issues/2814) + +**New features** + +- Support passing explicit connection to query builder ([#2817](https://github.com/knex/knex/issues/2817)) +- Introduced abstraction for getting migrations to make migration bundling easier [#2775](https://github.com/knex/knex/issues/2775) +- Allow timestamp with timezone on mssql databases [#2724](https://github.com/knex/knex/issues/2724) +- Allow specifying multiple migration directories [#2735](https://github.com/knex/knex/issues/2735) +- Allow cloning query builder with .userParams({}) assigned to it [#2802](https://github.com/knex/knex/issues/2802) +- Allow chaining of increment, decrement, and update [#2740](https://github.com/knex/knex/issues/2740) +- Allow table names with `forUpdate`/`forShare` [#2834](https://github.com/knex/knex/issues/2834) +- Added `whereColumn` and the associated `not` / `and` / `or` methods for using columns on the right side of a where clause [#2837](https://github.com/knex/knex/issues/2837) +- Added `whereRecursive` method to make self-referential CTEs possible [#2889](https://github.com/knex/knex/issues/2889) +- Added support for named unique, primary and foreign keys to SQLite3 [#2840](https://github.com/knex/knex/issues/2840) +- Added support for generating new migration and seed files without knexfile [#2884](https://github.com/knex/knex/issues/2884) [#2905](https://github.com/knex/knex/issues/2905) [#2935](https://github.com/knex/knex/issues/2935) +- Added support for multiple columns in `.orderBy()` [#2881](https://github.com/knex/knex/issues/2881) +- Added option of `existingType` to `.enum()` method to support repeated use of enums [#2719](https://github.com/knex/knex/issues/2719) +- Added option to pass `indexType` for MySQL dialect [#2890](https://github.com/knex/knex/issues/2890) +- Added `onVal` and the associated `not` / `and` / `or` methods for using values in `on` clauses within joins [#2746](https://github.com/knex/knex/issues/2746) +- Kill queries after timeout for PostgreSQL [#2636](https://github.com/knex/knex/issues/2636) +- Manage TypeScript types internally [#2845](https://github.com/knex/knex/issues/2845) +- Support 5.0.0+ versions of mssql driver [#2861](https://github.com/knex/knex/issues/2861) +- Typescript migration stub [#2816](https://github.com/knex/knex/issues/2816) +- Options object for passing timestamp parameters + regression tests [#2919](https://github.com/knex/knex/issues/2919) + +**Bug fixes** + +- Implement fail-fast logic for dialect resolution [#2776](https://github.com/knex/knex/issues/2776) +- Fixed identifier wrapping for `using()`. Use columnize instead of wrap in using() [#2713](https://github.com/knex/knex/issues/2713) +- Fix issues with warnPromise when migration does not return a promise [#2730](https://github.com/knex/knex/issues/2730) +- Compile with before update so that bindings are put in correct order [#2733](https://github.com/knex/knex/issues/2733) +- Fix join using builder withSchema [#2744](https://github.com/knex/knex/issues/2744) +- Throw instead of process.exit when client module missing [#2843](https://github.com/knex/knex/issues/2843) +- Display correct filename of a migration that failed [#2910](https://github.com/knex/knex/issues/2910) +- Fixed support of knexSnakeCaseWrappers in migrations [#2914](https://github.com/knex/knex/issues/2914) +- SQlite3 renameColunm quote fix [#2833](https://github.com/knex/knex/issues/2833) +- Adjust typing for forUpdate()/forShare() variant with table names [#2858](https://github.com/knex/knex/issues/2858) +- Fix execution of Oracle tests on Node 11 [#2920](https://github.com/knex/knex/issues/2920) +- Fix failures in oracle test bench and added it back to mandatory CI tests [#2924](https://github.com/knex/knex/issues/2924) +- Knex client knexfile resolution fix [#2923](https://github.com/knex/knex/issues/2923) +- Add queryContext to type declarations [#2931](https://github.com/knex/knex/issues/2931) + +**Test / internal changes** + +- Add tests for multiple union arguments with callbacks and builders [#2749](https://github.com/knex/knex/issues/2749) +- Update dependencies [#2772](https://github.com/knex/knex/issues/2772) [#2810](https://github.com/knex/knex/issues/2810) [#2842](https://github.com/knex/knex/issues/2842) [#2848](https://github.com/knex/knex/issues/2848) [#2893](https://github.com/knex/knex/issues/2893) [#2904](https://github.com/knex/knex/issues/2904) +- Separate migration generator [#2786](https://github.com/knex/knex/issues/2786) +- Do not postprocess internal queries in Migrator [#2914](https://github.com/knex/knex/issues/2914) [#2934](https://github.com/knex/knex/issues/2934) +- Use Babel 7 [#2813](https://github.com/knex/knex/issues/2813) +- Introduce LGTM.com badge [#2755](https://github.com/knex/knex/issues/2755) +- Cleanup based on analysis by https://lgtm.com [#2870](https://github.com/knex/knex/issues/2870) +- Add test for retrieving null dates [#2865](https://github.com/knex/knex/issues/2865) +- Add link to wiki [#2866](https://github.com/knex/knex/issues/2866) +- Add tests for specifying explicit pg version [#2895](https://github.com/knex/knex/issues/2895) +- Execute tests on Node.js 11 [#2873](https://github.com/knex/knex/issues/2873) +- Version upgrade guide [#2894](https://github.com/knex/knex/issues/2894) + +### 0.16.0 - 27 Nov, 2018 + +**Other Changes** + +- THIS RELEASE WAS UNPUBLISHED FROM NPM BECAUSE IT HAD BROKEN MIGRATIONS USING `postprocessResponse` FEATURE ([#2644](https://github.com/knex/knex/issues/2644)) + +### 0.15.2 - 19 Jul, 2018 + +**Other Changes** + +- Rolled back changes introduced by [#2542](https://github.com/knex/knex/issues/2542), in favor of opt-in behavior by adding a precision option in `date` / `timestamp` / `datetime` / `knex.fn.now` ([#2715](https://github.com/knex/knex/issues/2715), [#2721](https://github.com/knex/knex/issues/2721)) + +### 0.15.1 - 12 Jul, 2018 + +**Bug fixes** + +- Fix warning erroneously displayed for mysql [#2705](https://github.com/knex/knex/issues/2705) + +### 0.15.0 - 1 Jul, 2018 + +**Breaking changes** + +- Stop executing tests on Node 4 and 5. [#2451](https://github.com/knex/knex/issues/2451) (not supported anymore) +- `json` data type is no longer converted to `text` within a schema builder migration for MySQL databases (note that JSON data type is only supported for MySQL 5.7.8+) [#2635](https://github.com/knex/knex/issues/2635) +- Removed WebSQL dialect [#2461](https://github.com/knex/knex/issues/2461) +- Drop mariadb support [#2681](https://github.com/knex/knex/issues/2681) +- Primary Key for Migration Lock Table [#2569](https://github.com/knex/knex/issues/2569). This shouldn't affect to old loc tables, but if you like to have your locktable to have primary key, delete the old table and it will be recreated when migrations are ran next time. +- Ensure knex.destroy() returns a bluebird promise [#2589](https://github.com/knex/knex/issues/2589) +- Increment floats [#2614](https://github.com/knex/knex/issues/2614) +- Testing removal of 'skim' [#2520](https://github.com/knex/knex/issues/2520), Now rows are not converted to plain js objects, returned row objects might have changed type with oracle, mssql, mysql and sqlite3 +- Drop support for strong-oracle [#2487](https://github.com/knex/knex/issues/2487) +- Timeout errors doesn't silently ignore the passed errors anymore [#2626](https://github.com/knex/knex/issues/2626) +- Removed WebSQL dialect [#2647](https://github.com/knex/knex/issues/2647) +- Various fixes to mssql dialect to make it compatible with other dialects [#2653](https://github.com/knex/knex/issues/2653), Unique constraint now allow multiple null values, float type is now float instaed of decimal, rolling back transaction with undefined rejects with Error, select for update and select for share actually locks selected row, so basically old schema migrations will work a lot different and produce different schema like before. Also now MSSQL is included in CI tests. + +**Bug fixes** + +- Fixes onIn with empty values array [#2513](https://github.com/knex/knex/issues/2513) +- fix wrapIdentifier not being called in postgres alter column [#2612](https://github.com/knex/knex/issues/2612) +- fixes wrapIdentifier to work with postgres `returning` statement 2630 [#2642](https://github.com/knex/knex/issues/2642) +- Fix mssql driver crashing in certain cases when conneciton is closed unexpectedly [#2637](https://github.com/knex/knex/issues/2637) +- Removed semicolon from rollback stmt for oracle [#2564](https://github.com/knex/knex/issues/2564) +- Make the stream catch errors in the query [#2638](https://github.com/knex/knex/issues/2638) + +**New features** + +- Create timestamp columns with microsecond precision on MySQL 5.6 and newer [#2542](https://github.com/knex/knex/issues/2542) +- Allow storing stacktrace, where builder is initialized to be able trace back where certain query was created [#2500](https://github.com/knex/knex/issues/2500) [#2505](https://github.com/knex/knex/issues/2505) +- Added 'ref' function [#2509](https://github.com/knex/knex/issues/2509), no need for knex.raw('??', ['id']) anymore, one can do knex.ref('id') +- Support postgresql connection uri protocol [#2609](https://github.com/knex/knex/issues/2609) +- Add support for native enums on Postgres [#2632](https://github.com/knex/knex/issues/2632) +- Allow overwriting log functions [#2625](https://github.com/knex/knex/issues/2625) + +**Test / internal changes** + +- chore: cache node_modules [#2595](https://github.com/knex/knex/issues/2595) +- Remove babel-plugin-lodash [#2634](https://github.com/knex/knex/issues/2634) +- Remove readable-stream and safe-buffer [#2640](https://github.com/knex/knex/issues/2640) +- chore: add Node.js 10 [#2594](https://github.com/knex/knex/issues/2594) +- add homepage field to package.json [#2650](https://github.com/knex/knex/issues/2650) + +### 0.14.6 - 12 Apr, 2018 + +**Bug fixes** + +- Restored functionality of query event [#2566](https://github.com/knex/knex/issues/2566) ([#2549](https://github.com/knex/knex/issues/2549)) + +### 0.14.5 - 8 Apr, 2018 + +**Bug fixes** + +- Fix wrapping returning column on oracledb [#2554](https://github.com/knex/knex/issues/2554) + +**New features** + +- Support passing DB schema name for migrations [#2499](https://github.com/knex/knex/issues/2499) [#2559](https://github.com/knex/knex/issues/2559) +- add clearOrder method [#2360](https://github.com/knex/knex/issues/2360) [#2553](https://github.com/knex/knex/issues/2553) +- Added knexTxId to query events and debug calls [#2476](https://github.com/knex/knex/issues/2476) +- Support multi-column `whereIn` with query [#1390](https://github.com/knex/knex/issues/1390) +- Added error if chaining update/insert/etc with first() [#2506](https://github.com/knex/knex/issues/2506) +- Checks for an empty, undefined or null object on transacting [#2494](https://github.com/knex/knex/issues/2494) +- countDistinct with multiple columns [#2449](https://github.com/knex/knex/issues/2449) + +**Test / internal changes** + +- Added npm run test:oracledb command that runs oracledb tests in docker [#2491](https://github.com/knex/knex/issues/2491) +- Runnin mssql tests in docker [#2496](https://github.com/knex/knex/issues/2496) +- Update dependencies [#2561](https://github.com/knex/knex/issues/2561) + +### 0.14.4 - 19 Feb, 2018 + +**Bug fixes** + +- containsUndefined only validate plain objects. Fixes [#1898](https://github.com/knex/knex/issues/1898) ([#2468](https://github.com/knex/knex/issues/2468)) +- Add warning when using .returning() in sqlite3. Fixes [#1660](https://github.com/knex/knex/issues/1660) ([#2471](https://github.com/knex/knex/issues/2471)) +- Throw an error if .update() results in an empty sql ([#2472](https://github.com/knex/knex/issues/2472)) +- Removed unnecessary createTableIfNotExist and replaced with createTable ([#2473](https://github.com/knex/knex/issues/2473)) + +**New features** + +- Allow calling lock procedures (such as forUpdate) outside of transaction. Fixes [#2403](https://github.com/knex/knex/issues/2403). ([#2475](https://github.com/knex/knex/issues/2475)) +- Added test and documentation for Event 'start' ([#2488](https://github.com/knex/knex/issues/2488)) + +**Test / internal changes** + +- Added stress test, which uses TCP proxy to simulate flaky connection [#2460](https://github.com/knex/knex/issues/2460) +- Removed old docker tests, new stress test setup ([#2474](https://github.com/knex/knex/issues/2474)) +- Removed unused property \_\_cid on the base client ([#2481](https://github.com/knex/knex/issues/2481)) +- Changed rm to rimraf in 'npm run dev' ([#2483](https://github.com/knex/knex/issues/2483)) +- Changed babel preset and use latest node as target when running dev ([#2484](https://github.com/knex/knex/issues/2484)) + +### 0.14.3 - 8 Feb, 2018 + +**Bug fixes** + +- Use tarn as pool instead of generic-pool which has been given various problems [#2450](https://github.com/knex/knex/issues/2450) +- Fixed mysql issue where add columns failed if using both after and collate [#2432](https://github.com/knex/knex/issues/2432) +- CLI sets exit-code 1 if the command supplied was not parseable [#2358](https://github.com/knex/knex/issues/2358) +- Set toNative() to be not enumerable [#2388](https://github.com/knex/knex/issues/2388) +- Use wrapIdentifier in columnInfo. fixes [#2402](https://github.com/knex/knex/issues/2402) [#2405](https://github.com/knex/knex/issues/2405) +- Fixed a bug when using .returning (OUTPUT) in an update query with joins in MSSQL [#2399](https://github.com/knex/knex/issues/2399) +- Better error message when running migrations fail before even starting run migrations [#2373](https://github.com/knex/knex/issues/2373) +- Read oracle's UV_THREADPOOL_SIZE env variable correctly [#2372](https://github.com/knex/knex/issues/2372) +- Added decimal variable precision / scale support [#2353](https://github.com/knex/knex/issues/2353) + +**New features** + +- Added queryContext to schema and query builders [#2314](https://github.com/knex/knex/issues/2314) +- Added redshift dialect [#2233](https://github.com/knex/knex/issues/2233) +- Added warning when one uses .createTableIfNotExist and deprecated it from docs [#2458](https://github.com/knex/knex/issues/2458) + +**Test / internal changes** + +- Update dependencies and fix ESLint warnings accordingly [#2433](https://github.com/knex/knex/issues/2433) +- Disable oracledb tests from non LTS nodes [#2407](https://github.com/knex/knex/issues/2407) +- Update dependencies [#2422](https://github.com/knex/knex/issues/2422) + +### 0.14.2 - 24 Nov, 2017 + +**Bug fixes** + +- Fix sqlite3 truncate method to work again [#2348](https://github.com/knex/knex/issues/2348) + +### 0.14.1 - 19 Nov, 2017 + +**Bug fixes** + +- Fix support for multiple schema names in in postgres `searchPath` [#2340](https://github.com/knex/knex/issues/2340) +- Fix create new connection to pass errors to query instead of retry loop [#2336](https://github.com/knex/knex/issues/2336) +- Fix recognition of connections closed by server [#2341](https://github.com/knex/knex/issues/2341) + +### 0.14.0 - 6 Nov, 2017 + +**Breaking changes** + +- Remove sorting of statements from update queries [#2171](https://github.com/knex/knex/issues/2171) +- Updated allowed operator list with some missing operators and make all to lower case [#2239](https://github.com/knex/knex/issues/2239) +- Use node-mssql 4.0.0 [#2029](https://github.com/knex/knex/issues/2029) +- Support for enum columns to SQlite3 dialect [#2055](https://github.com/knex/knex/issues/2055) +- Better identifier quoting in Sqlite3 [#2087](https://github.com/knex/knex/issues/2087) +- Migration Errors - Display filename of of failed migration [#2272](https://github.com/knex/knex/issues/2272) + +**Other Features** + +- Post processing hook for query result [#2261](https://github.com/knex/knex/issues/2261) +- Build native SQL where binding parameters are dialect specific [#2237](https://github.com/knex/knex/issues/2237) +- Configuration option to allow override identifier wrapping [#2217](https://github.com/knex/knex/issues/2217) +- Implemented select syntax: select({ alias: 'column' }) [#2227](https://github.com/knex/knex/issues/2227) +- Allows to filter seeds and migrations by extensions [#2168](https://github.com/knex/knex/issues/2168) +- Reconnecting after database server disconnect/reconnect + tests [#2017](https://github.com/knex/knex/issues/2017) +- Removed filering from allowed configuration settings of mysql2 [#2040](https://github.com/knex/knex/issues/2040) +- Allow raw expressions in query builder aggregate methods [#2257](https://github.com/knex/knex/issues/2257) +- Throw error on non-string table comment [#2126](https://github.com/knex/knex/issues/2126) +- Support for mysql stream query options [#2301](https://github.com/knex/knex/issues/2301) + +**Bug fixes** + +- Allow update queries and passing query builder to with statements [#2298](https://github.com/knex/knex/issues/2298) +- Fix escape table name in SQLite columnInfo call [#2281](https://github.com/knex/knex/issues/2281) +- Preventing containsUndefined from going to recursion loop [#1711](https://github.com/knex/knex/issues/1711) +- Fix error caused by call to knex.migrate.currentVersion [#2123](https://github.com/knex/knex/issues/2123) +- Upgraded generic-pool to 3.1.7 (did resolve some memory issues) [#2208](https://github.com/knex/knex/issues/2208) +- Allow using NOT ILIKE operator [#2195](https://github.com/knex/knex/issues/2195) +- Fix postgres searchPath to be case-sensitive [#2172](https://github.com/knex/knex/issues/2172) +- Fix drop of multiple columns in sqlite3 [#2107](https://github.com/knex/knex/issues/2107) +- Fix adding multiple columns in Oracle [#2115](https://github.com/knex/knex/issues/2115) +- Use selected schema when dropping indices in Postgres. [#2105](https://github.com/knex/knex/issues/2105) +- Fix hasTable for MySQL to not do partial matches [#2097](https://github.com/knex/knex/issues/2097) +- Fix setting autoTransaction in batchInsert [#2113](https://github.com/knex/knex/issues/2113) +- Fix connection error propagation when streaming [#2199](https://github.com/knex/knex/issues/2199) +- Fix comments not being applied to increments columns [#2243](https://github.com/knex/knex/issues/2243) +- Fix mssql wrong binding order of queries that combine a limit with select raw or update [#2066](https://github.com/knex/knex/issues/2066) +- Fixed mysql alter table attributes order [#2062](https://github.com/knex/knex/issues/2062) + +**Test / internal changes** + +- Update each out-of-date dependency according to david-dm.org [#2297](https://github.com/knex/knex/issues/2297) +- Update v8flags to version 3.0.0 [#2288](https://github.com/knex/knex/issues/2288) +- Update interpret version [#2283](https://github.com/knex/knex/issues/2283) +- Fix debug output typo [#2187](https://github.com/knex/knex/issues/2187) +- Docker CI tests [#2164](https://github.com/knex/knex/issues/2164) +- Unit test for right/rightOuterJoin combination [#2117](https://github.com/knex/knex/issues/2117) +- Unit test for fullOuterJoin [#2118](https://github.com/knex/knex/issues/2118) +- Unit tests for table comment [#2098](https://github.com/knex/knex/issues/2098) +- Test referencing non-existent column with sqlite3 [#2104](https://github.com/knex/knex/issues/2104) +- Unit test for renaming column in postgresql [#2099](https://github.com/knex/knex/issues/2099) +- Unit test for cross-join [#2102](https://github.com/knex/knex/issues/2102) +- Fix incorrect parameter name [#2068](https://github.com/knex/knex/issues/2068) + +### 0.13.0 - 29 Apr, 2017 + +**Breaking changes** + +- Multiple concurrent migration runners blocks instead of throwing error when possible [#1962](https://github.com/knex/knex/issues/1962) +- Fixed transaction promise mutation issue [#1991](https://github.com/knex/knex/issues/1991) + +**Other Changes** + +- Allow passing version of connected db in configuration file [#1993](https://github.com/knex/knex/issues/1993) +- Bugfixes on batchInsert and transactions for mysql/maria [#1992](https://github.com/knex/knex/issues/1992) +- Add fetchAsString optional parameter to oracledb dialect [#1998](https://github.com/knex/knex/issues/1998) +- fix: escapeObject parameter order for Postgres dialect. [#2003](https://github.com/knex/knex/issues/2003) + +### 0.12.9 - 23 Mar, 2017 + +- Fixed unhandled exception in batchInsert when the rows to be inserted resulted in duplicate key violation [#1880](https://github.com/knex/knex/issues/1880) + +### 0.12.8 - 15 Mar, 2017 + +- Added clearSelect and clearWhere to query builder [#1912](https://github.com/knex/knex/issues/1912) +- Properly close Postgres query streams on error [#1935](https://github.com/knex/knex/issues/1935) +- Transactions should never reject with undefined [#1970](https://github.com/knex/knex/issues/1970) +- Clear acquireConnectionTimeout if an error occurs when acquiring a connection [#1973](https://github.com/knex/knex/issues/1973) + +### 0.12.7 - 17 Feb, 2017 + +**Accidental Breaking Change** + +- Ensure that 'client' is provided in knex config object [#1822](https://github.com/knex/knex/issues/1822) + +**Other Changes** + +- Support custom foreign key names [#1311](https://github.com/knex/knex/issues/1311), [#1726](https://github.com/knex/knex/issues/1726) +- Fixed named bindings to work with queries containing `:`-chars [#1890](https://github.com/knex/knex/issues/1890) +- Exposed more promise functions [#1896](https://github.com/knex/knex/issues/1896) +- Pass rollback errors to transaction promise in mssql [#1885](https://github.com/knex/knex/issues/1885) +- ONLY keyword support for PostgreSQL (for table inheritance) [#1874](https://github.com/knex/knex/issues/1874) +- Fixed Mssql update with join syntax [#1777](https://github.com/knex/knex/issues/1777) +- Replace migrations and seed for react-native packager [#1813](https://github.com/knex/knex/issues/1813) +- Support knexfile, migration and seeds in TypeScript [#1769](https://github.com/knex/knex/issues/1769) +- Fix float to integer conversion of decimal fields in MSSQL [#1781](https://github.com/knex/knex/issues/1781) +- External authentication capability when using oracledb driver [#1716](https://github.com/knex/knex/issues/1716) +- Fixed MSSQL incorect query build when locks are used [#1707](https://github.com/knex/knex/issues/1707) +- Allow to use `first` method as aliased select [#1784](https://github.com/knex/knex/issues/1784) +- Alter column for nullability, type and default value [#46](https://github.com/knex/knex/issues/46), [#1759](https://github.com/knex/knex/issues/1759) +- Add more having* methods / join clause on* methods [#1674](https://github.com/knex/knex/issues/1674) +- Compatibility fixes and cleanups [#1788](https://github.com/knex/knex/issues/1788), [#1792](https://github.com/knex/knex/issues/1792), [#1794](https://github.com/knex/knex/issues/1794), [#1814](https://github.com/knex/knex/issues/1814), [#1857](https://github.com/knex/knex/issues/1857), [#1649](https://github.com/knex/knex/issues/1649) + +### 0.12.6 - 19 Oct, 2016 + +- Address warnings mentioned in [#1388](https://github.com/knex/knex/issues/1388) ([#1740](https://github.com/knex/knex/issues/1740)) +- Remove postinstall script ([#1746](https://github.com/knex/knex/issues/1746)) + +### 0.12.5 - 12 Oct, 2016 + +- Fix broken 0.12.4 build (removed from npm) +- Fix [#1733](https://github.com/knex/knex/issues/1733), [#920](https://github.com/knex/knex/issues/920), incorrect postgres array bindings + +### 0.12.3 - 9 Oct, 2016 + +- Fix [#1703](https://github.com/knex/knex/issues/1703), [#1694](https://github.com/knex/knex/issues/1694) - connections should be returned to pool if acquireConnectionTimeout is triggered +- Fix [#1710](https://github.com/knex/knex/issues/1710) regression in postgres array escaping + +### 0.12.2 - 27 Sep, 2016 + +- Restore pool min: 1 for sqlite3, [#1701](https://github.com/knex/knex/issues/1701) +- Fix for connection error after it's closed / released, [#1691](https://github.com/knex/knex/issues/1691) +- Fix oracle prefetchRowCount setting, [#1675](https://github.com/knex/knex/issues/1675) + +### 0.12.1 - 16 Sep, 2016 + +- Fix MSSQL sql execution error, [#1669](https://github.com/knex/knex/issues/1669) +- Added DEBUG=knex:bindings for debugging query bindings, [#1557](https://github.com/knex/knex/issues/1557) + +### 0.12.0 - 13 Sep, 2016 + +- Remove build / built files, [#1616](https://github.com/knex/knex/issues/1616) +- Upgrade to Babel 6, [#1617](https://github.com/knex/knex/issues/1617) +- Reference Bluebird module directly, remove deprecated .exec method, [#1618](https://github.com/knex/knex/issues/1618) +- Remove documentation files from main repo +- Fix broken behavior on WebSQL build, [#1638](https://github.com/knex/knex/issues/1638) +- Oracle id sequence now handles manual inserts, [#906](https://github.com/knex/knex/issues/906) +- Cleanup PG escaping, fix [#1602](https://github.com/knex/knex/issues/1602), [#1548](https://github.com/knex/knex/issues/1548) +- Added [`with`](/guide/query-builder#with) to builder for [common table expressions](https://www.postgresql.org/docs/9.4/static/queries-with.html), [#1599](https://github.com/knex/knex/issues/1599) +- Fix [#1619](https://github.com/knex/knex/issues/1619), pluck with explicit column names +- Switching back to [generic-pool](https://github.com/coopernurse/node-pool) for pooling resource management +- Removed index.html, please direct all PR's for docs against the files in [knex/documentation](https://github.com/knex/documentation) + +### 0.11.10 - 9 Aug, 2016 + +- Added CHANGELOG.md for a [new documentation](https://github.com/knex/documentation) builder coming soon, [#1615](https://github.com/knex/knex/issues/1615) +- Minor documentation tweaks +- PG: Fix Uint8Array being considered undefined, [#1601](https://github.com/knex/knex/issues/1601) +- MSSQL: Make columnInfo schema dynamic, [#1585](https://github.com/knex/knex/issues/1585) + +### 0.11.9 - 21 Jul, 2016 + +- Reverted knex client breaking change (commit b74cd69e906), fixes [#1587](https://github.com/knex/knex/issues/1587) + +### 0.11.8 - 21 Jul, 2016 + +- Oracledb dialect [#990](https://github.com/knex/knex/issues/990) +- Documentation fix [#1532](https://github.com/knex/knex/issues/1532) +- Allow named bindings to be escaped. [#1576](https://github.com/knex/knex/issues/1576) +- Several bugs with MS SQL schema creation and installing from gihub fix [#1577](https://github.com/knex/knex/issues/1577) +- Fix incorrect escaping of backslashes in SqlString.escape [#1545](https://github.com/knex/knex/issues/1545) + +### 0.11.7 - 19 Jun, 2016 + +- Add missing dependency. [#1516](https://github.com/knex/knex/issues/1516) + +### 0.11.6 - 18 Jun, 2016 + +- Allow cancellation on timeout (MySQL) [#1454](https://github.com/knex/knex/issues/1454) +- Better bigint support. (MSSQL) [#1445](https://github.com/knex/knex/issues/1445) +- More consistent handling of `undefined` values in `QueryBuilder#where` and `Raw`. [#1459](https://github.com/knex/knex/issues/1459) +- Fix Webpack build. [#1447](https://github.com/knex/knex/issues/1447) +- Fix code that triggered Bluebird warnings. [#1460](https://github.com/knex/knex/issues/1460), [#1489](https://github.com/knex/knex/issues/1489) +- Fix `ping` function. (Oracle) [#1486](https://github.com/knex/knex/issues/1486) +- Fix `columnInfo`. (MSSQL) [#1464](https://github.com/knex/knex/issues/1464) +- Fix `ColumnCompiler#binary`. (MSSQL) [#1464](https://github.com/knex/knex/issues/1464) +- Allow connection strings that do not contain a password. [#1473](https://github.com/knex/knex/issues/1473) +- Fix race condition in seed stubs. [#1493](https://github.com/knex/knex/issues/1493) +- Give each query a UUID. [#1510](https://github.com/knex/knex/issues/1510) + +### 0.11.5 - 26 May, 2016 + +- Bugfix: Using `Raw` or `QueryBuilder` as a binding to `Raw` now works as intended + +### 0.11.4 - 22 May, 2016 + +- Bugfix: Inconsistency of `.primary()` and `.dropPrimary()` between dialects [#1430](https://github.com/knex/knex/issues/1430) +- Feature: Allow using custom Client/Dialect (you can pass your own client in knex config) [#1428](https://github.com/knex/knex/issues/1428) +- Docs: Add documentation for .dropTimestamps [#1432](https://github.com/knex/knex/issues/1432) +- Bugfix: Fixed passing undefined fields for insert/update inside transaction [#1423](https://github.com/knex/knex/issues/1423) +- Feature: `batchInsert` with existing transaction [#1354](https://github.com/knex/knex/issues/1354) +- Build: eslint instead of jshint [#1416](https://github.com/knex/knex/issues/1416) +- Bugfix: Pooled connections not releasing [#1382](https://github.com/knex/knex/issues/1382) +- Bugfix: Support passing `knex.raw` to `.whereNot` [#1402](https://github.com/knex/knex/issues/1402) +- Docs: Fixed list of dialects which supports `.returning` [#1398](https://github.com/knex/knex/issues/1398) +- Bugfix: rename table does not fail anymore even with schema defined [#1403](https://github.com/knex/knex/issues/1403) + +### 0.11.3 - 14 May, 2016 + +- Support nested joins. [#1397](https://github.com/knex/knex/issues/1397) + +### 0.11.2 - 14 May, 2016 + +- Prevent crash on `knex seed:make`. [#1389](https://github.com/knex/knex/issues/1389) +- Improvements to `batchInsert`. [#1391](https://github.com/knex/knex/issues/1391) +- Improvements to inserting `DEFAULT` with `undefined` binding. [#1396](https://github.com/knex/knex/issues/1396) +- Correct generated code for adding/dropping multiple columns. (MSSQL) [#1401](https://github.com/knex/knex/issues/1401) + +### 0.11.1 - 6 May, 2016 + +- Fix error in CLI command `migrate:make`. [#1386](https://github.com/knex/knex/issues/1386) + +### 0.11.0 - 5 May, 2016 + +**Breaking changes** + +- `QueryBuilder#orWhere` joins multiple arguments with `AND`. [#1164](https://github.com/knex/knex/issues/1164) + +**Other Changes** + +- Collate for columns. (MySQL) [#1147](https://github.com/knex/knex/issues/1147) +- Add `QueryBuilder#timeout`, `Raw#timeout`. [#1201](https://github.com/knex/knex/issues/1201) [#1260](https://github.com/knex/knex/issues/1260) +- Exit with error code when appropriate. [#1238](https://github.com/knex/knex/issues/1238) +- MSSQL connection accepts `host` as an alias for `server` in accordance with other dialects. [#1239](https://github.com/knex/knex/issues/1239) +- Add `query-response` event. [#1231](https://github.com/knex/knex/issues/1231) +- Correct behaviour of sibling nested transactions. [#1226](https://github.com/knex/knex/issues/1226) +- Support `RETURNING` with `UPDATE`. (Oracle) [#1253](https://github.com/knex/knex/issues/1253) +- Throwing callbacks from transactions automatically rolls them back. [#1257](https://github.com/knex/knex/issues/1257) +- Fixes to named `Raw` bindings. [#1251](https://github.com/knex/knex/issues/1251) +- `timestamps` accepts an argument to set `NOT NULL` and default to current timestamp. +- Add `TableBuilder#inherits` for PostgreSQL. [#601](https://github.com/knex/knex/issues/601) +- Wrap index names. [#1289](https://github.com/knex/knex/issues/1289) +- Restore coffeescript knexfiles and configurations. [#1292](https://github.com/knex/knex/issues/1292) +- Add `andWhereBetween` and `andWhereNotBetween` [#1132](https://github.com/knex/knex/issues/1132) +- Fix `valueForUndefined` failure. [#1269](https://github.com/knex/knex/issues/1269) +- `renameColumn` no longer drops default value or nullability. [#1326](https://github.com/knex/knex/issues/1326) +- Correct MySQL2 error handling. [#1315](https://github.com/knex/knex/issues/1315) +- Fix MSSQL `createTableIfNotExists`. [#1362](https://github.com/knex/knex/issues/1362) +- Fix MSSQL URL parsing. [#1342](https://github.com/knex/knex/issues/1342) +- Update Lodash to 4.6.0 [#1242](https://github.com/knex/knex/issues/1242) +- Update Bluebird to 3.3.4 [#1279](https://github.com/knex/knex/issues/1279) + +### 0.10.0 - 15 Feb, 2016 + +**Breaking changes** + +- `insert` and `update` now ignore `undefined` values. Back compatibility is provided through the option `useNullAsDefault`. [#1174](https://github.com/knex/knex/issues/1174), [#1043](https://github.com/knex/knex/issues/1043) + +**Other Changes** + +- [`countDistinct`](/guide/query-builder#countdistinct), [`avgDistinct`](/guide/query-builder#avgdistinct) and [`sumDistinct`](/guide/query-builder#sumdistinct). [#1046](https://github.com/knex/knex/issues/1046) +- Add [`schema.jsonb`](#Schema-jsonb). Deprecated `schema.json(column, true)`. [#991](https://github.com/knex/knex/issues/991) +- Support binding identifiers with `??`. [#1103](https://github.com/knex/knex/issues/1103) +- Restore `query` event when triggered by transactions. [#855](https://github.com/knex/knex/issues/855) +- Correct question mark escaping in rendered queries. [#519](https://github.com/knex/knex/issues/519), [#1058](https://github.com/knex/knex/issues/1058) +- Add per-dialect escaping, allowing quotes to be escaped correctly. [#886](https://github.com/knex/knex/issues/886), [#1095](https://github.com/knex/knex/issues/1095) +- Add MSSQL support. [#1090](https://github.com/knex/knex/issues/1090) +- Add migration locking. [#1094](https://github.com/knex/knex/issues/1094) +- Allow column aliases to contain `.`. [#1181](https://github.com/knex/knex/issues/1181) +- Add `batchInsert`. [#1182](https://github.com/knex/knex/issues/1182) +- Support non-array arguments to [`knex.raw`](#Raw-Bindings). +- Global `query-error` event. [#1163](https://github.com/knex/knex/issues/1163) +- Add `batchInsert`. [#1182](https://github.com/knex/knex/issues/1182) +- Better support for Mysql2 dialect options. [#980](https://github.com/knex/knex/issues/980) +- Support for `acquireConnectionTimeout` default 60 seconds preventing [#1040](https://github.com/knex/knex/issues/1040) from happening. [#1177](https://github.com/knex/knex/issues/1177) +- Fixed constraint name escaping when dropping a constraint. [#1177](https://github.com/knex/knex/issues/1177) +- Show also `.raw` queries in debug output. [#1169](https://github.com/knex/knex/issues/1169) +- Support for `cli` to use basic configuration without specific environment set. [#1101](https://github.com/knex/knex/issues/1101) + +### 0.9.0 - Nov 2, 2015 + +- Fix error when merging `knex.raw` instances without arguments. [#853](https://github.com/knex/knex/issues/853) +- Fix error that caused the connection to time out while streaming. [#849](https://github.com/knex/knex/issues/849) +- Correctly parse SSL query parameter for PostgreSQL. [#852](https://github.com/knex/knex/issues/852) +- Pass `compress` option to MySQL2. [#843](https://github.com/knex/knex/issues/843) +- Schema: Use `timestamp with timezone` by default for `time`, `datetime` and `timestamp` for Oracle. [#876](https://github.com/knex/knex/issues/876) +- Add [`QueryBuilder#modify`](/guide/query-builder#modify) [#881](https://github.com/knex/knex/issues/881) +- Add LiveScript and Early Gray support for seeds and migrations. +- Add [`QueryBuilder#withSchema`](/guide/query-builder#withSchema) [#518](https://github.com/knex/knex/issues/518) +- Allow escaping of `?` in `knex.raw` queries. [#946](https://github.com/knex/knex/issues/946) +- Allow `0` in join clause. [#953](https://github.com/knex/knex/issues/953) +- Add migration config to allow disabling/enabling transactions per migration. [#834](https://github.com/knex/knex/issues/834) + +### 0.8.6 - May 20, 2015 + +- Fix for several transaction / migration issues, [#832](https://github.com/knex/knex/issues/832), [#833](https://github.com/knex/knex/issues/833), [#834](https://github.com/knex/knex/issues/834), [#835](https://github.com/knex/knex/issues/835) + +### 0.8.5 - May 14, 2015 + +- Pool should be initialized if no pool options are specified + +### 0.8.4 - May 13, 2015 + +- Pool should not be initialized if {max: 0} is sent in config options + +### 0.8.3 - May 2, 2015 + +- Alias postgresql -> postgres in connection config options + +### 0.8.2 - May 1, 2015 + +- Fix regression in using query string in connection config + +### 0.8.1 - May 1, 2015 + +- Warn rather than error when implicit commits wipe out savepoints in mysql / mariadb, [#805](https://github.com/knex/knex/issues/805). +- Fix for incorrect seed config reference, [#804](https://github.com/knex/knex/issues/804) + +### 0.8.0 - Apr 30, 2015 + +**New features** + +- Fixes several major outstanding bugs with the connection pool, switching to [Pool2](https://github.com/myndzi/pool2) in place of generic-pool-redux +- strong-oracle module support +- Nested transactions automatically become savepoints, with `commit` & `rollback` releasing or rolling back the current savepoint. +- Database seed file support, [#391](https://github.com/knex/knex/issues/391) +- Improved support for sub-raw queries within raw statements +- Migrations are now wrapped in transactions where possible +- Subqueries supported in insert statements, [#627](https://github.com/knex/knex/issues/627) +- Support for nested having, [#572](https://github.com/knex/knex/issues/572) +- Support object syntax for joins, similar to "where" [#743](https://github.com/knex/knex/issues/743) + +**Major Changes** + +- Transactions are immediately invoked as A+ promises, [#470](https://github.com/knex/knex/issues/470) (this is a feature and should not actually break anything in practice) +- Heavy refactoring internal APIs (public APIs should not be affected) + +**Other Changes** + +- Allow mysql2 to use non-default port, [#588](https://github.com/knex/knex/issues/588) +- Support creating & dropping extensions in PostgreSQL, [#540](https://github.com/knex/knex/issues/540) +- CLI support for knexfiles that do not provide environment keys, [#527](https://github.com/knex/knex/issues/527) +- Added sqlite3 dialect version of whereRaw/andWhereRaw ([#477](https://github.com/knex/knex/issues/477)) + +### 0.7.5 - Mar 9, 2015 + +- Fix bug in validateMigrationList, ([#697](https://github.com/knex/knex/issues/697)) + +### 0.7.4 - Feb 25, 2015 + +- Fix incorrect order of query parameters when using subqueries, [#704](https://github.com/knex/knex/issues/704) +- Properly handle limit 0, ([#655](https://github.com/knex/knex/issues/655)) +- Apply promise args from then instead of [explicitly passing](https://github.com/petkaantonov/bluebird/issues/482). +- Respect union parameter as last argument ([#660](https://github.com/knex/knex/issues/660)). +- Added sqlite3 dialect version of whereRaw/andWhereRaw ([#477](https://github.com/knex/knex/issues/477)). +- Fix SQLite dropColumn doesn't work for last column ([#544](https://github.com/knex/knex/issues/544)). +- Add POSIX operator support for Postgres ([#562](https://github.com/knex/knex/issues/562)) +- Sample seed files now correctly ([#391](https://github.com/knex/knex/issues/391)) + +### 0.7.3 - Oct 3, 2014 + +- Support for `join(table, rawOrBuilder)` syntax. +- Fix for regression in PostgreSQL connection ([#516](https://github.com/knex/knex/issues/516)) + +### 0.7.2 - Oct 1, 2014 + +- Fix for regression in migrations + +### 0.7.1 - Oct 1, 2014 + +- Better disconnect handling & pool removal for MySQL clients, [#452](https://github.com/knex/knex/issues/452) + +### 0.7.0 - Oct 1, 2014 + +**New features** + +- Oracle support, [#419](https://github.com/knex/knex/issues/419) +- Database seed file support, [#391](https://github.com/knex/knex/issues/391) +- Improved support for sub-raw queries within raw statements + +**Breaking changes** + +- "collate nocase" no longer used by default in sqlite3 [#396](https://github.com/knex/knex/issues/396) + +**Other Changes** + +- Bumping Bluebird to ^2.x +- Transactions in websql are now a no-op (unsupported) [#375](https://github.com/knex/knex/issues/375) +- Improved test suite +- knex.fn namespace as function helper (knex.fn.now), [#372](https://github.com/knex/knex/issues/372) +- Better handling of disconnect errors +- Support for offset without limit, [#446](https://github.com/knex/knex/issues/446) +- Chainable first method for mysql schema, [#406](https://github.com/knex/knex/issues/406) +- Support for empty array in `whereIn` +- Create/drop schema for postgres, [#511](https://github.com/knex/knex/issues/511) +- Inserting multiple rows with default values, [#468](https://github.com/knex/knex/issues/468) +- Join columns are optional for cross-join, [#508](https://github.com/knex/knex/issues/508) +- Flag for creating jsonb columns in Postgresql, [#500](https://github.com/knex/knex/issues/500) + +### 0.6.22 - July 10, 2014 + +- Bug fix for properly binding postgresql streaming queries, ([#363](https://github.com/knex/knex/issues/363)) + +### 0.6.21 - July 9, 2014 + +- Bug fix for raw queries not being transaction context aware, ([#351](https://github.com/knex/knex/issues/351)). +- Properly forward stream errors in sqlite3 runner, ([#359](https://github.com/knex/knex/issues/359)) + +### 0.6.20 - June 30, 2014 + +- Allow case insensitive operators in sql clauses, ([#344](https://github.com/knex/knex/issues/344)) + +### 0.6.19 - June 27, 2014 + +- Add `groupByRaw` / `orderByRaw` methods, better support for raw statements in group / order ([#282](https://github.com/knex/knex/issues/282)). +- Support more config options for node-mysql2 dialect ([#341](https://github.com/knex/knex/issues/341)). +- CLI help text fix, ([#342](https://github.com/knex/knex/issues/342)) + +### 0.6.18 - June 25, 2014 + +- Patch for the method, calling without a handler should return the stream, not a promise ([#337](https://github.com/knex/knex/issues/337)) + +### 0.6.17 - June 23, 2014 + +- Adding missing map / reduce proxies to bluebird's implementation + +### 0.6.16 - June 18, 2014 + +- Increment / decrement returns the number of affectedRows ([#330](https://github.com/knex/knex/issues/330)). +- Allow --cwd option flag to be passed to CLI tool ([#326](https://github.com/knex/knex/issues/326)) + +### 0.6.15 - June 14, 2014 + +- Added the as method for aliasing subqueries + +### 0.6.14 - June 14, 2014 + +- whereExists / whereNotExists may now take a query builder instance as well as a callback + +### 0.6.13 - June 12, 2014 + +- Fix regression with onUpdate / onDelete in PostgreSQL, ([#308](https://github.com/knex/knex/issues/308)). +- Add missing `Promise` require to knex.js, unit test for knex.destroy ([#314](https://github.com/knex/knex/issues/314)) + +### 0.6.12 - June 10, 2014 + +- Fix for regression with boolean default types in PostgreSQL + +### 0.6.11 - June 10, 2014 + +- Fix for regression with queries containing multiple order by statements in sqlite3 + +### 0.6.10 - June 10, 2014 + +- Fix for big regression in memoization of column names from 0.5 -> 0.6 + +### 0.6.9 - June 9, 2014 + +- Fix for regression in specificType method + +### 0.6.8 - June 9, 2014 + +- Package.json fix for CLI + +### 0.6.7 - June 9, 2014 + +- Adds support for [node-mysql2](https://github.com/sidorares/node-mysql2) library. +- Bundles CLI with the knex install, various related migrate CLI fixes + +### 0.6.6 - June 9, 2014 + +- console.warn rather than throw when adding foreignKeys in SQLite3. +- Add support for dropColumn in SQLite3. +- Document `raw.wrap` + +### 0.6.5 - June 9, 2014 + +- Add missing \_ require to WebSQL builds + +### 0.6.4 - June 9, 2014 + +- Fix & document schema.raw method + +### 0.6.3 - June 6, 2014 + +- Schema methods on transaction object are now transaction aware ([#301](https://github.com/knex/knex/issues/301)). +- Fix for resolved value from transactions, ([#298](https://github.com/knex/knex/issues/298)). +- Undefined columns are not added to builder + +### 0.6.2 - June 4, 2014 + +- Fix regression in raw query output, ([#297](https://github.com/knex/knex/issues/297)). +- Fix regression in "pluck" method ([#296](https://github.com/knex/knex/issues/296)). +- Document [first](/guide/query-builder#first) method + +### 0.6.1 - June 4, 2014 + +- Reverting to using .npmignore, the "files" syntax forgot the knex.js file + +### 0.6.0 - June 4, 2014 + +**Major Library refactor** + +- Major internal overhaul to clean up the various dialect code. +- Improved unit test suite. +- Support for the [mariasql](https://github.com/mscdex/node-mariasql) driver. +- More consistent use of raw query bindings throughout the library. +- Queries are more composable, may be injected in various points throughout the builder. +- Added [streaming](/guide/interfaces#streams) interface +- Deprecated 5 argument [join](/guide/query-builder#join) in favor of additional join methods. +- The wrapValue function to allow for array column operations in PostgreSQL ([#287](https://github.com/knex/knex/issues/287)). +- An explicit connection can be passed for any query ([#56](https://github.com/knex/knex/issues/56)). +- Drop column support for sqlite3 +- All schema actions are run sequentially on the same connection if chained. +- Schema actions can now be wrapped in a transaction +- `.references(tableName.columnName)` as shorthand for `.references(columnName).inTable(tableName)` +- `.join('table.column', 'otherTable.column')` as shorthand for .join('table.column', '=', 'otherTable.column') +- Streams are supported for selects, passing through to the streaming capabilities of node-mysql and node-postgres +- For More information, see this [pull-request](https://github.com/tgriesser/knex/pull/252) + +### 0.5.15 - June 4, 2014 + +- Dropped indexes feature now functions correctly, ([#278](https://github.com/knex/knex/issues/278)) + +### 0.5.14 - May 6, 2014 + +- Remove the charset encoding if it's utf8 for mysql, as it's the default but also currently causes some issues in recent versions of node-mysql + +### 0.5.13 - April 2, 2014 + +- Fix regression in array bindings for postgresql ([#228](https://github.com/knex/knex/issues/228)) + +### 0.5.12 - Mar 31, 2014 + +- Add more operators for where clauses, including && ([#226](https://github.com/knex/knex/issues/226)) + +### 0.5.11 - Mar 25, 2014 + +- `.where(col, 'is', null)` or `.where(col, 'is not', null)` are not supported ([#221](https://github.com/knex/knex/issues/221)). +- Case insensitive `where` operators now allowed ([#212](https://github.com/knex/knex/issues/212)). +- Fix bug in increment/decrement truncating to an integer ([#210](https://github.com/knex/knex/issues/210)). +- Disconnected connections are now properly handled & removed from the pool ([#206](https://github.com/knex/knex/issues/206)). +- Internal tweaks to binding concatenations for performance ([#207](https://github.com/knex/knex/issues/207)) + +### 0.5.10 - Mar 19, 2014 + +- Add the .exec method to the internal promise shim + +### 0.5.9 - Mar 18, 2014 + +- Remove error'ed connections from the connection pool ([#206](https://github.com/knex/knex/issues/206)), added support for node-postgres-pure (pg.js) ([#200](https://github.com/knex/knex/issues/200)) + +### 0.5.8 - Feb 27, 2014 + +- Fix for chaining on forUpdate / forShare, adding map & reduce from bluebird + +### 0.5.7 - Feb 18, 2014 + +- Fix for a null limit / offset breaking query chain ([#182](https://github.com/knex/knex/issues/182)) + +### 0.5.6 - Feb 5, 2014 + +- Bump bluebird dependency to ~1.0.0, fixing regression in Bluebird 1.0.2 ([#176](https://github.com/knex/knex/issues/176)) + +### 0.5.5 - Jan 28, 2014 + +- Fix for the exit code on the migrations cli ([#151](https://github.com/knex/knex/issues/151)). +- The `init` method in `knex.migrate` now uses `this.config` if one isn't passed in ([#156](https://github.com/knex/knex/issues/156)) + +### 0.5.4 - Jan 7, 2014 + +- Fix for using raw statements in defaultTo schema builder methods ([#146](https://github.com/knex/knex/issues/146)) + +### 0.5.3 - Jan 2, 2014 + +- Fix for incorrectly formed sql when aggregates are used with columns ([#144](https://github.com/knex/knex/issues/144)) + +### 0.5.2 - Dec 18, 2013 + +- Adding passthrough "catch", "finally" to bluebird implementations, use bluebird's "nodeify" internally for exec + +### 0.5.1 - Dec 12, 2013 + +- The [returning](/guide/query-builder#returning) in PostgreSQL may now accept \* or an array of columns to return. If either of these are passed, the response will be an array of objects rather than an array of values. Updates may also now use a `returning` value. ([#132](https://github.com/knex/knex/issues/132)) +- Added `bigint` and `bigserial` type to PostgreSQL. ([#111](https://github.com/knex/knex/issues/111)) +- Fix for the [specificType](/guide/schema-builder#specifictype) schema call ([#118](https://github.com/knex/knex/issues/118)) +- Several fixes for migrations, including migration file path fixes, passing a Promise constructor to the migration `up` and `down` methods, allowing the "knex" module to be used globally, file ordering on migrations, and other small improvements. ([#112](https://github.com/knex/knex/issues/112)-115, [#125](https://github.com/knex/knex/issues/125), [#135](https://github.com/knex/knex/issues/135)) + +### 0.5.0 - Nov 25, 2013 + +- Initial pass at a [migration](/guide/migrations) api. +- Aggregate methods are no longer aliased as "aggregate", but may now be aliased and have more than one aggregate in a query ([#108](https://github.com/knex/knex/issues/108), [#110](https://github.com/knex/knex/issues/110)). +- Adding bigint and bigserial to PostgreSQL ([#111](https://github.com/knex/knex/issues/111)). +- Bugfix on increment/decrement values ([#100](https://github.com/knex/knex/issues/100)). +- Bugfix with having method ([#107](https://github.com/knex/knex/issues/107)). +- Switched from when.js to [bluebird](https://github.com/petkaantonov/bluebird) for promise implementation, with shim for backward compatibility. +- Switched from underscore to lodash, for semver reliability + +### 0.4.13 - Oct 31, 2013 + +- Fix for aggregate methods on toString and clone, ([#98](https://github.com/knex/knex/issues/98)) + +### 0.4.12 - Oct 29, 2013 + +- Fix incorrect values passed to float in MySQL and decimal in PostgreSQL + +### 0.4.11 - Oct 15, 2013 + +- Fix potential sql injection vulnerability in orderBy, thanks to @sebgie + +### 0.4.10 - Oct 14, 2013 + +- Added [forUpdate](/guide/query-builder#forupdate) and [forShare](#Builder-forShare) for select modes in transactions. ([#84](https://github.com/knex/knex/issues/84)) +- Fix bug where current query chain type is not copied on [clone](#Builder-clone). ([#90](https://github.com/knex/knex/issues/90)) +- Charset and collate are now added as methods on the schema builder. ([#89](https://github.com/knex/knex/issues/89)) +- Added `into` as an alias of [from](/guide/query-builder#from), for builder syntax of: `insert(value).into(tableName)` +- Internal pool fixes. ([#90](https://github.com/knex/knex/issues/90)) + +### 0.4.9 - Oct 7, 2013 + +- Fix for documentation of [hasColumn](/guide/schema-builder#hascolumn), ensure that `hasColumn` works with MySQL ([#87](https://github.com/knex/knex/issues/87)). +- More cleanup of error messages, showing the original error message concatenated with the sql and bindings + +### 0.4.8 - Oct 2, 2013 + +- Connections are no longer pushed back into the pool if they never existed to begin with ([#85](https://github.com/knex/knex/issues/85)) + +### 0.4.7 - Sep 27, 2013 + +- The column is now a documented method on the builder api, and takes either an individual column or an array of columns to select + +### 0.4.6 - Sep 25, 2013 + +- Standardizing handling of errors for easier debugging, as noted in ([#39](https://github.com/knex/knex/issues/39)) + +### 0.4.5 - Sep 24, 2013 + +- Fix for hasTable always returning true in MySQL ([#82](https://github.com/knex/knex/issues/82)), fix where sql queries were duplicated with multiple calls on toSql with the schema builder + +### 0.4.4 - Sep 22, 2013 + +- Fix for debug method not properly debugging individual queries + +### 0.4.3 - Sep 18, 2013 + +- Fix for underscore not being defined in various grammar files + +### 0.4.2 - Sep 17, 2013 + +- Fix for an error being thrown when an initialized ClientBase instance was passed into Knex.initialize. pool.destroy now optionally accepts a callback to notify when it has completed draining and destroying all connections + +### 0.4.1 - Sep 16, 2013 + +- Cleanup from the 0.4.0 release, fix a potential exploit in "where" clauses pointed out by Andri Möll, fix for clients not being properly released from the pool [#70](https://github.com/knex/knex/issues/70), fix for where("foo", "<>", null) doing an "IS NULL" statement + +### 0.4.0 - Sep 13, 2013 + +**Breaking changes** + +- Global state is no longer stored in the library, an instance is returned from `Knex.initialize`, so you will need to call this once and then reference this `knex` client elsewhere in your application. +- Lowercasing of `knex.raw`, `knex.transaction`, and `knex.schema`. +- Created columns are now nullable by default, unless `notNullable` is chained as an option. +- Keys created with `increments` are now assumed to be unsigned (MySQL) by default. +- The `destroyAllNow` is no longer called by the library on `process.exit` event. If you need to call it explicitly yourself, you may use `knex.client.destroyPool` + +### 0.2.6 - Aug 29, 2013 + +- Reject the transaction promise if the transaction "commit" fails, ([#50](https://github.com/knex/knex/issues/50)) + +### 0.2.5 - Aug 25, 2013 + +- Fix error if a callback isn't specified for exec, ([#49](https://github.com/knex/knex/issues/49)) + +### 0.2.4 - Aug 22, 2013 + +- Fix SQLite3 delete not returning affected row count, ([#45](https://github.com/knex/knex/issues/45)) + +### 0.2.3 - Aug 22, 2013 + +- Fix insert with default values in PostgreSQL and SQLite3, ([#44](https://github.com/knex/knex/issues/44)) + +### 0.2.2 - Aug 20, 2013 + +- Allowing Raw queries to be passed as the primary table names + +### 0.2.1 - Aug 13, 2013 + +- Fix for an array passed to insert being mutated + +### 0.2.0 - Aug 7, 2013 + +**Breaking changes** + +- [hasTable](/guide/schema-builder#hastable) now returns a boolean rather than a failed promise. +- Changed syntax for insert in postgresql, where the `id` is not assumed on inserts ([#18](https://github.com/knex/knex/issues/18)). The second parameter of [insert](#Builder-insert) is now required to return an array of insert id's for the last insert. +- The [timestamp](/guide/schema-builder#timestamp) method on the schema builder now uses a `dateTime` rather than a `timestamp` + +### 0.1.8 - July 7, 2013 + +- Somehow missing the != operator. Using _.find rather than _.where in getCommandsByName([#22](https://github.com/knex/knex/issues/22)) + +### 0.1.7 - June 12, 2013 + +- Ensures unhandled errors in the exec callback interface are re-thrown + +### 0.1.6 - June 9, 2013 + +- Renaming beforeCreate to afterCreate. Better handling of errors in the connection pooling + +### 0.1.5 - June 9, 2013 + +- Added the ability to specify beforeCreate and beforeDestroy hooks on the initialize's options.pool to perform any necessary database setup/teardown on connections before use ([#14](https://github.com/knex/knex/issues/14)). where and having may now accept Knex.Raw instances, for consistency ([#15](https://github.com/knex/knex/issues/15)). Added an orHaving method to the builder. The ability to specify bindings on Raw queries has been removed + +### 0.1.4 - May 22, 2013 + +- defaultTo now accepts "false" for boolean columns, allows for empty strings as default values + +### 0.1.3 - May 18, 2013 + +- Enabling table aliases ([#11](https://github.com/knex/knex/issues/11)). Fix for issues with transactions not functioning ([#12](https://github.com/knex/knex/issues/12)) + +### 0.1.2 - May 15, 2013 + +- Bug fixes for groupBy ([#7](https://github.com/knex/knex/issues/7)). Mysql using collation, charset config settings in createTable. Added engine on schemaBuilder specifier ([#6](https://github.com/knex/knex/issues/6)). Other doc fixes, tests + +### 0.1.1 - May 14, 2013 + +- Bug fixes for sub-queries, minor changes to initializing "main" instance, adding "pg" as a valid parameter for the client name in the connection settings + +### 0.1.0 - May 13, 2013 + +- Initial Knex release diff --git a/docs/src/faq/index.md b/docs/src/faq/index.md new file mode 100644 index 0000000000..977300f185 --- /dev/null +++ b/docs/src/faq/index.md @@ -0,0 +1,37 @@ +# F.A.Q. + +## How do I help contribute? + +Glad you asked! Pull requests, or feature requests, though not always implemented, are a great way to help make Knex even better than it is now. If you're looking for something specific to help out with, there's a number of unit tests that aren't implemented yet, the library could never have too many of those. If you want to submit a fix or feature, take a look at the [Contributing](https://github.com/knex/knex/blob/master/CONTRIBUTING.md) readme in the Github and go ahead and open a ticket. + +## How do I debug? + +Knex is beginning to make use of the [debug](https://github.com/visionmedia/debug) module internally, so you can set the `DEBUG` environment variable to `knex:*` to see all debugging, or select individual namespaces `DEBUG=knex:query,knex:tx` to constrain a bit. + +If you pass `{debug: true}` as one of the options in your initialize settings, you can see all of the query calls being made. Sometimes you need to dive a bit further into the various calls and see what all is going on behind the scenes. I'd recommend [node-inspector](https://github.com/dannycoates/node-inspector), which allows you to debug code with `debugger` statements like you would in the browser. + +At the start of your application code will catch any errors not otherwise caught in the normal promise chain handlers, which is very helpful in debugging. + +## How do I run the test suite? + +The test suite looks for an environment variable called `KNEX_TEST` for the path to the database configuration. If you run the following command: + +```bash +$ export KNEX_TEST='/path/to/your/knex_config.js' +$ npm test +``` + +replacing with the path to your config file, and the config file is valid, the test suite should run properly. + +## My tests are failing because slow DB connection and short test timeouts! How to extend test timeouts? + +Sometimes, e.g. when running CI on travis, test suite's default timeout of 5 seconds might be too short. In such cases an alternative test timeout value in milliseconds can be specified using the `KNEX_TEST_TIMEOUT` environment variable. + +```bash +$ export KNEX_TEST_TIMEOUT=30000 +$ npm test +``` + +## I found something broken with Amazon Redshift! Can you help? + +Because there is no testing platform available for Amazon Redshift, be aware that it is included as a dialect but is unsupported. With that said, please file an issue if something is found to be broken that is not noted in the documentation, and we will do our best. diff --git a/docs/src/faq/recipes.md b/docs/src/faq/recipes.md new file mode 100644 index 0000000000..0d3610d7e6 --- /dev/null +++ b/docs/src/faq/recipes.md @@ -0,0 +1,237 @@ +# Recipes + +## Using non-standard database that is compatible with PostgreSQL wire protocol (such as CockroachDB) + +Specify PostgreSQL version that database you are using is compatible with protocol-wise using `version` option, e. g.: + +```js +const knex = require('knex')({ + client: 'pg', + version: '7.2', + connection: { + host: '127.0.0.1', + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, +}); +``` + +Note that value of `version` option should be not the version of the database that you are using, but version of PostgreSQL that most closely matches functionality of the database that you are using. If not provided by database vendor, try using '7.2' as a baseline and keep increasing (within the range of existing PostgreSQL versions) until it starts (or stops) working. + +There are also known incompatibilities with migrations for databases that do not support select for update. See https://github.com/tgriesser/knex/issues/2002 for a workaround. + +## Connecting to MSSQL on Azure SQL Database + +`{encrypt: true}` should be included in options branch of connection configuration: + +```js +knex({ + client: 'mssql', + connection: { + database: 'mydatabase', + server: 'myserver.database.windows.net', + user: 'myuser', + password: 'mypass', + port: 1433, + connectionTimeout: 30000, + options: { + encrypt: true, + }, + }, +}); +``` + +[See all of node-mssql's connection options](https://github.com/tediousjs/node-mssql#configuration-1) + +## Adding a full-text index for PostgreSQL + +```js +exports.up = (knex) => { + return knex.schema.createTable('foo', (table) => { + table.increments('id'); + table.specificType('fulltext', 'tsvector'); + table.index('fulltext', null, 'gin'); + }); +}; +``` + +## DB access using SQLite and SQLCipher + +After you build the SQLCipher source and the npm SQLite3 package, and encrypt your DB (look elsewhere for these things), then anytime you open your database, you need to provide your encryption key using the SQL statement: + +```sql +PRAGMA KEY = 'secret' +``` + +This PRAGMA is more completely documented in the SQLCipher site. When working with Knex this is best done when opening the DB, via the following: + +```js +const myDBConfig = { + client: 'sqlite3', + connection: { + filename: 'myEncryptedSQLiteDbFile.db', + }, + pool: { + afterCreate: function (conn, done) { + conn.run("PRAGMA KEY = 'secret'"); + done(); + }, + }, +}; +const knex = require('knex')(myDBConfig); +``` + +Of course embedding the key value in your code is a poor security practice. Instead, retrieve the 'secret' from elsewhere. + +The key Knex thing to note here is the "afterCreate" function. This is documented in the knexjs.org site, but is not in the Table of Contents at this time, so do a browser find when on the site to get to it. It allows auto-updating DB settings when creating any new pool connections (of which there will only ever be one per file for Knex-SQLite). + +If you don't use the "afterCreate" configuration, then you will need to run a knex.raw statement with each and every SQL you execute, something like as follows: + +```js +return knex.raw("PRAGMA KEY = 'secret'").then(() => + knex('some_table') + .select() + .on('query-error', function (ex, obj) { + console.log('KNEX select from some_table ERR ex:', ex, 'obj:', obj); + }) +); +``` + +## Maintaining changelog for seeds (version >= 0.16.0-next1) + +In case you would like to use Knex.js changelog functionality to ensure your environments are only seeded once, but don't want to mix seed files with migration files, you can specify multiple directories as a source for your migrations: + +```ts +await knex.migrate.latest({ + directory: [ + 'src/services/orders/database/migrations', + 'src/services/orders/database/seeds', + ], + sortDirsSeparately: true, + tableName: 'orders_migrations', + schemaName: 'orders', +}); +``` + +## Using explicit transaction management together with async code + +```ts +await knex.transaction((trx) => { + async function stuff() { + trx.rollback(new Error('Foo')); + } + stuff().then(() => { + // do something + }); +}); +``` + +Or alternatively: + +```ts +try { + await knex.transaction((trx) => { + async function stuff() { + trx.rollback(new Error('always explicit rollback this time')); + } + stuff(); + }); + // transaction was committed +} catch (err) { + // transaction was rolled back +} +``` + +(note that promise for `knex.transaction` resolves after transaction is rolled back or committed) + +## Using parentheses with AND operator + +In order to generate query along the lines of + +```sql +SELECT "firstName", "lastName", "status" +FROM "userInfo" +WHERE "status" = 'active' +AND ("firstName" ILIKE '%Ali%' OR "lastName" ILIKE '%Ali%'); +``` + +you need to use following approach: + +```js +queryBuilder + .where('status', status.uuid) + .andWhere((qB) => + qB + .where('firstName', 'ilike', `%${q}%`) + .orWhere('lastName', 'ilike', `%${q}%`) + ); +``` + +## Calling an oracle stored procedure with bindout variables + +How to call and retrieve output from an oracle stored procedure + +```ts +const oracle = require('oracledb'); +const bindVars = { + input_var1: 6, + input_var2: 7, + output_var: { + dir: oracle.BIND_OUT, + }, + output_message: { + dir: oracle.BIND_OUT, + }, +}; + +const sp = + 'BEGIN MULTIPLY_STORED_PROCEDURE(:input_var1, :input_var2, :output_var, :output_message); END;'; +const results = await knex.raw(sp, bindVars); +console.log(results[0]); // 42 +console.log(results[1]); // 6 * 7 is the answer to life +``` + +## Node instance doesn't stop after using knex + +Make sure to close knex instance after execution to avoid Node process hanging due to open connections: + +```js +async function migrate() { + try { + await knex.migrate.latest({ + /**config**/ + }); + } catch (e) { + process.exit(1); + } finally { + try { + knex.destroy(); + } catch (e) { + // ignore + } + } +} + +migrate(); +``` + +## Manually Closing Streams + +When using Knex's [stream interface](/guide/interfaces#streams), you can typically just `pipe` the return stream to any writable stream. However, with [`HTTPIncomingMessage`](http://nodejs.org/api/http.html#http_http_incomingmessage), you'll need to take special care to handle aborted requests. + +An `HTTPIncomingMessage` object is typically called `request`. This is the first argument in `'request'` events emitted on `http.Server` instances. [Express's `req`](http://expressjs.com/4x/api.html#request) implements a compatible interface and Hapi exposes this object on [its request objects](http://hapijs.com/api#request-object) as `request.raw.req`. + +You need to explicitly handle the case where an `HTTPIncomingMessage` is closed prematurely when streaming from a database with Knex. The easiest way to cause this is: + +1. Visit an endpoint that takes several seconds to fully transmit a response +2. Close the browser window immediately after beginning the request + +When this happens while you are streaming a query to a client, you need to manually tell Knex that it can release the database connection in use back to the connection pool. + +```js +server.on('request', function (request, response) { + const stream = knex.select('*').from('items').stream(); + request.on('close', stream.end.bind(stream)); +}); +``` diff --git a/docs/src/faq/support.md b/docs/src/faq/support.md new file mode 100644 index 0000000000..afaed5048f --- /dev/null +++ b/docs/src/faq/support.md @@ -0,0 +1,3 @@ +# Support + +Have questions about the library? Come join us in the [#bookshelf freenode IRC](http://webchat.freenode.net/?channels=bookshelf) channel for support on knex.js and [bookshelf.js](http://bookshelfjs.org), or post an issue on [Stack Overflow](http://stackoverflow.com/questions/tagged/knex.js) or in the GitHub [issue tracker](https://github.com/knex/knex/issues). diff --git a/docs/src/guide/extending.md b/docs/src/guide/extending.md new file mode 100644 index 0000000000..9cd285368f --- /dev/null +++ b/docs/src/guide/extending.md @@ -0,0 +1,44 @@ +# Extending + +To extend knex's builders, we have the following methods + +```js +knex.SchemaBuilder.extend('functionName', function () { + console.log('Custom Schema Builder Function'); + return this; +}); +knex.TableBuilder.extend('functionName', function () { + console.log('Custom Table Builder Function'); + return this; +}); +knex.ViewBuilder.extend('functionName', function () { + console.log('Custom View Builder Function'); + return this; +}); +knex.ColumnBuilder.extend('functionName', function () { + console.log('Custom Column Builder Function'); + return this; +}); +``` + +To add typescript support you can add the following (.d.ts): + +```ts +import 'knex'; +declare module 'knex' { + namespace Knex { + interface SchemaBuilder { + functionName(): Knex.SchemaBuilder; + } + interface TableBuilder { + functionName(): Knex.TableBuilder; + } + interface ViewBuilder { + functionName(): Knex.ViewBuilder; + } + interface ColumnBuilder { + functionName(): Knex.ColumnBuilder; + } + } +} +``` diff --git a/docs/src/guide/index.md b/docs/src/guide/index.md new file mode 100644 index 0000000000..ac42fda285 --- /dev/null +++ b/docs/src/guide/index.md @@ -0,0 +1,597 @@ +# Installation + +Knex can be used as an SQL query builder in both Node.JS and the browser, limited to WebSQL's constraints (like the inability to drop tables or read schemas). Composing SQL queries in the browser for execution on the server is highly discouraged, as this can be the cause of serious security vulnerabilities. The browser builds outside of WebSQL are primarily for learning purposes - for example, you can pop open the console and build queries on this page using the **knex** object. + +## Node.js + +The primary target environment for Knex is Node.js, you will need to install the `knex` library, and then install the appropriate database library: [`pg`](https://github.com/brianc/node-postgres) for PostgreSQL, CockroachDB and Amazon Redshift, [`pg-native`](https://github.com/brianc/node-pg-native) for PostgreSQL with native C++ `libpq` bindings (requires PostgresSQL installed to link against), [`mysql`](https://github.com/felixge/node-mysql) for MySQL or MariaDB, [`sqlite3`](https://github.com/mapbox/node-sqlite3) for SQLite3, or [`tedious`](https://github.com/tediousjs/tedious) for MSSQL. + +```bash +$ npm install knex --save + +# Then add one of the following (adding a --save) flag: +$ npm install pg +$ npm install pg-native +$ npm install sqlite3 +$ npm install better-sqlite3 +$ npm install mysql +$ npm install mysql2 +$ npm install oracledb +$ npm install tedious +``` + +_If you want to use CockroachDB or Redshift instance, you can use the `pg` driver._ + +_If you want to use a MariaDB instance, you can use the `mysql` driver._ + +## Browser + +Knex can be built using a JavaScript build tool such as [browserify](http://browserify.org/) or [webpack](https://github.com/webpack/webpack). In fact, this documentation uses a webpack build which [includes knex](https://github.com/knex/documentation/blob/a4de1b2eb50d6699f126be8d134f3d1acc4fc69d/components/Container.jsx#L3). View source on this page to see the browser build in-action (the global `knex` variable). + +## Configuration Options + +The `knex` module is itself a function which takes a configuration object for Knex, accepting a few parameters. The `client` parameter is required and determines which client adapter will be used with the library. + +```js +const knex = require('knex')({ + client: 'mysql', + connection: { + host: '127.0.0.1', + port: 3306, + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, +}); +``` + +The connection options are passed directly to the appropriate database client to create the connection, and may be either an object, a connection string, or a function returning an object: + +::: info PostgreSQL +Knex's PostgreSQL client allows you to set the initial search path for each connection automatically using an additional option "searchPath" as shown below. + +```js +const pg = require('knex')({ + client: 'pg', + connection: process.env.PG_CONNECTION_STRING, + searchPath: ['knex', 'public'], +}); +``` + +::: + +When using the PostgreSQL driver, another usage pattern for instantiating the Knex configuration object could be to use a `connection: {}` object details to specify various flags such as enabling SSL, a connection string, and individual connection configuration fields all in the same object. Consider the following example: + +::: info PostgreSQL +If `connectionString` is highest priority to use. If left unspecified then connection details will be determined using the individual connection fields (`host`, `port`, etc), and finally an SSL configuration will be enabled based on a truthy value of `config["DB_SSL"]` which will also accept self-signed certificates. + +```js +const pg = require('knex')({ + client: 'pg', + connection: { + connectionString: config.DATABASE_URL, + host: config['DB_HOST'], + port: config['DB_PORT'], + user: config['DB_USER'], + database: config['DB_NAME'], + password: config['DB_PASSWORD'], + ssl: config['DB_SSL'] ? { rejectUnauthorized: false } : false, + }, +}); +``` + +::: + +The following are SQLite usage patterns for instantiating the Knex configuration object: + +::: info SQLite3 or Better-SQLite3 +When you use the SQLite3 or Better-SQLite3 adapter, there is a filename required, not a network connection. For example: + +```js +const knex = require('knex')({ + client: 'sqlite3', // or 'better-sqlite3' + connection: { + filename: './mydb.sqlite', + }, +}); +``` + +You can also run either SQLite3 or Better-SQLite3 with an in-memory database by providing `:memory:` as the filename. For example: + +```js +const knex = require('knex')({ + client: 'sqlite3', // or 'better-sqlite3' + connection: { + filename: ':memory:', + }, +}); +``` + +::: + +::: info SQLite3 +When you use the SQLite3 adapter, you can set flags used to open the connection. For example: + +```js +const knex = require('knex')({ + client: 'sqlite3', + connection: { + filename: 'file:memDb1?mode=memory&cache=shared', + flags: ['OPEN_URI', 'OPEN_SHAREDCACHE'], + }, +}); +``` + +::: + +::: info Better-SQLite3 +With the Better-SQLite3 adapter, you can use `options.nativeBinding` to specify the location of the adapter's compiled C++ addon. This can be useful when your build system does a lot of transformation/relocation of files. + +Example use: + +```js +const knex = require('knex')({ + client: 'better-sqlite3', + connection: { + filename: ':memory:', + options: { + nativeBinding: '/path/to/better_sqlite3.node', + }, + }, +}); +``` + +Additionally, you can open the database in read-only mode using `options.readonly`: + +```js +const knex = require('knex')({ + client: 'better-sqlite3', + connection: { + filename: '/path/to/db.sqlite3', + options: { + readonly: true, + }, + }, +}); +``` + +For more information, see the [Better-SQLite3 documentation](https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#new-databasepath-options) on database connection options. + +::: + +::: info MSSQL +When you use the MSSQL client, you can define a `mapBinding` function to define your own logic for mapping from knex query parameters to `tedious` types. +Returning undefined from the function will fallback to the default mapping. + +```js +import { TYPES } from 'tedious'; + +const knex = require('knex')({ + client: 'mssql', + connection: { + options: { + mapBinding: (value) => { + // bind all strings to varchar instead of nvarchar + if (typeof value === 'string') { + return { + type: TYPES.VarChar, + value, + }; + } + + // allow devs to pass tedious type at query time + if (value != null && value.type) { + return { + type: value.type, + value: value.value, + }; + } + + // undefined is returned; falling back to default mapping function + }, + }, + }, +}); +``` + +::: + +::: info +The database version can be added in knex configuration, when you use the PostgreSQL adapter to connect a non-standard database. + +```js +const knex = require('knex')({ + client: 'pg', + version: '7.2', + connection: { + host: '127.0.0.1', + port: 5432, + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, +}); +``` + +```js +const knex = require('knex')({ + client: 'mysql', + version: '5.7', + connection: { + host: '127.0.0.1', + port: 3306, + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, +}); +``` + +::: + +::: info +When using a custom PostgreSQL client like `knex-aurora-data-api-client`, you can explicitly state if it supports jsonb column types + +```js +const knex = require('knex')({ + client: require('knex-aurora-data-api-client').postgres, + connection: { resourceArn, secretArn, database: `mydb` }, + version: 'data-api', + jsonbSupport: true, +}); +``` + +::: + +A function can be used to determine the connection configuration dynamically. This function receives no parameters, and returns either a configuration object or a promise for a configuration object. + +```js +const knex = require('knex')({ + client: 'sqlite3', + connection: () => ({ + filename: process.env.SQLITE_FILENAME, + }), +}); +``` + +By default, the configuration object received via a function is cached and reused for all connections. To change this behavior, an `expirationChecker` function can be returned as part of the configuration object. The `expirationChecker` is consulted before trying to create new connections, and in case it returns `true`, a new configuration object is retrieved. For example, to work with an authentication token that has a limited lifespan: + +```js +const knex = require('knex')({ + client: 'postgres', + connection: async () => { + const { token, tokenExpiration } = await someCallToGetTheToken(); + + return { + host: 'your_host', + port: 5432, + user: 'your_database_user', + password: token, + database: 'myapp_test', + expirationChecker: () => { + return tokenExpiration <= Date.now(); + }, + }; + }, +}); +``` + +You can also connect via a unix domain socket, which will ignore host and port. + +```js +const knex = require('knex')({ + client: 'mysql', + connection: { + socketPath: '/path/to/socket.sock', + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, +}); +``` + +`userParams` is an optional parameter that allows you to pass arbitrary parameters which will be accessible via `knex.userParams` property: + +```js +const knex = require('knex')({ + client: 'mysql', + connection: { + host: '127.0.0.1', + port: 3306, + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, + userParams: { + userParam1: '451', + }, +}); +``` + +Initializing the library should normally only ever happen once in your application, as it creates a connection pool for the current database, you should use the instance returned from the initialize call throughout your library. + +Specify the client for the particular flavour of SQL you are interested in. + +```js +const pg = require('knex')({ client: 'pg' }); + +knex('table').insert({ a: 'b' }).returning('*').toString(); +// "insert into "table" ("a") values ('b')" + +pg('table').insert({ a: 'b' }).returning('*').toString(); +// "insert into "table" ("a") values ('b') returning *" +``` + +### withUserParams + +You can call method `withUserParams` on a Knex instance if you want to get a copy (with same connections) with custom parameters (e. g. to execute same migrations with different parameters) + +```js +const knex = require('knex')({ + // Params +}); + +const knexWithParams = knex.withUserParams({ + customUserParam: 'table1', +}); +const customUserParam = knexWithParams.userParams.customUserParam; +``` + +### debug + +Passing a `debug: true` flag on your initialization object will turn on [debugging](/guide/query-builder.html#debug) for all queries. + +### asyncStackTraces + +Passing an `asyncStackTraces: true` flag on your initialization object will turn on stack trace capture for all query builders, raw queries and schema builders. When a DB driver returns an error, this previously captured stack trace is thrown instead of a new one. This helps to mitigate default behaviour of `await` in node.js/V8 which blows the stack away. This has small performance overhead, so it is advised to use only for development. Turned off by default. + +### pool + +The client created by the configuration initializes a connection pool, using the [tarn.js](https://github.com/vincit/tarn.js) library. This connection pool has a default setting of a `min: 2, max: 10` for the MySQL and PG libraries, and a single connection for sqlite3 (due to issues with utilizing multiple connections on a single file). To change the config settings for the pool, pass a `pool` option as one of the keys in the initialize block. + +Note that the default value of `min` is 2 only for historical reasons. It can result in problems with stale connections, despite tarn's default idle connection timeout of 30 seconds, which is only applied when there are more than `min` active connections. It is recommended to set `min: 0` so all idle connections can be terminated. + +Checkout the [tarn.js](https://github.com/vincit/tarn.js) library for more information. + +```js +const knex = require('knex')({ + client: 'mysql', + connection: { + host: '127.0.0.1', + port: 3306, + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, + pool: { min: 0, max: 7 }, +}); +``` + +If you ever need to explicitly teardown the connection pool, you may use `knex.destroy([callback])`. You may use `knex.destroy` by passing a callback, or by chaining as a promise, just not both. To manually initialize a destroyed connection pool, you may use knex.initialize(\[config\]), if no config is passed, it will use the first knex configuration used. + +### afterCreate + +`afterCreate` callback (rawDriverConnection, done) is called when the pool aquires a new connection from the database server. done(err, connection) callback must be called for `knex` to be able to decide if the connection is ok or if it should be discarded right away from the pool. + +```js +const knex = require('knex')({ + client: 'pg', + connection: { + /*...*/ + }, + pool: { + afterCreate: function (conn, done) { + // in this example we use pg driver's connection API + conn.query('SET timezone="UTC";', function (err) { + if (err) { + // first query failed, + // return error and don't try to make next query + done(err, conn); + } else { + // do the second query... + conn.query('SELECT set_limit(0.01);', function (err) { + // if err is not falsy, + // connection is discarded from pool + // if connection aquire was triggered by a + // query the error is passed to query promise + done(err, conn); + }); + } + }); + }, + }, +}); +``` + +### acquireConnectionTimeout + +`acquireConnectionTimeout` defaults to 60000ms and is used to determine how long knex should wait before throwing a timeout error when acquiring a connection is not possible. The most common cause for this is using up all the pool for transaction connections and then attempting to run queries outside of transactions while the pool is still full. The error thrown will provide information on the query the connection was for to simplify the job of locating the culprit. + +```js +const knex = require('knex')({ + client: 'pg', + connection: { + /*...*/ + }, + pool: { + /*...*/ + }, + acquireConnectionTimeout: 10000, +}); +``` + +### fetchAsString + +Utilized by Oracledb. An array of types. The valid types are 'DATE', 'NUMBER' and 'CLOB'. When any column having one of the specified types is queried, the column data is returned as a string instead of the default representation. + +```js +const knex = require('knex')({ + client: 'oracledb', + connection: { + /*...*/ + }, + fetchAsString: ['number', 'clob'], +}); +``` + +### migrations + +For convenience, any migration configuration may be specified when initializing the library. Read the [Migrations](/guide/migrations.html) section for more information and a full list of configuration options. + +```js +const knex = require('knex')({ + client: 'mysql', + connection: { + host: '127.0.0.1', + port: 3306, + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, + migrations: { + tableName: 'migrations', + }, +}); +``` + +### postProcessResponse + +Hook for modifying returned rows, before passing them forward to user. One can do for example snake_case -> camelCase conversion for returned columns with this hook. The `queryContext` is only available if configured for a query builder instance via [queryContext](/guide/schema-builder.html#querycontext). + +```js +const knex = require('knex')({ + client: 'mysql', + // overly simplified snake_case -> camelCase converter + postProcessResponse: (result, queryContext) => { + // TODO: add special case for raw results + // (depends on dialect) + if (Array.isArray(result)) { + return result.map((row) => convertToCamel(row)); + } else { + return convertToCamel(result); + } + }, +}); +``` + +### wrapIdentifier + +Knex supports transforming identifier names automatically to quoted versions for each dialect. For example `'Table.columnName as foo'` for PostgreSQL is converted to "Table"."columnName" as "foo". + +With `wrapIdentifier` one may override the way how identifiers are transformed. It can be used to override default functionality and for example to help doing `camelCase` -> `snake_case` conversion. + +Conversion function `wrapIdentifier(value, dialectImpl, context): string` gets each part of the identifier as a single `value`, the original conversion function from the dialect implementation and the `queryContext`, which is only available if configured for a query builder instance via [builder.queryContext](/guide/query-builder.html#querycontext), and for schema builder instances via [schema.queryContext](/guide/schema-builder.html#querycontext) or [table.queryContext](/guide/schema-builder.html#querycontext-1). For example, with the query builder, `knex('table').withSchema('foo').select('table.field as otherName').where('id', 1)` will call `wrapIdentifier` converter for following values `'table'`, `'foo'`, `'table'`, `'field'`, `'otherName'` and `'id'`. + +```js +const knex = require('knex')({ + client: 'mysql', + // overly simplified camelCase -> snake_case converter + wrapIdentifier: (value, origImpl, queryContext) => + origImpl(convertToSnakeCase(value)), +}); +``` + +### log + +Knex contains some internal log functions for printing warnings, errors, deprecations, and debug information when applicable. These log functions typically log to the console, but can be overwritten using the log option and providing alternative functions. Different log functions can be used for separate knex instances. + +```js +const knex = require('knex')({ + log: { + warn(message) {}, + error(message) {}, + deprecate(message) {}, + debug(message) {}, + }, +}); +``` + +### compileSqlOnError + +Knex builds an error message in case of query error. By default Knex adds compiled SQL (`SELECT * FROM users WHERE password = 'myPassword'`) to the error message. This can be changed to parameterized SQL (`SELECT * FROM users WHERE password = ?`) by setting `compileSqlOnError` to `false`. + +```js +const knex = require('knex')({ + compileSqlOnError: false, +}); +``` + +## TypeScript + +While knex is written in JavaScript, officially supported TypeScript bindings are available (within the knex npm package). + +However it is to be noted that TypeScript support is currently best-effort. Knex has a very flexible API and not all usage patterns can be type-checked and in most such cases we err on the side of flexibility. In particular, lack of type errors doesn't currently guarantee that the generated queries will be correct and therefore writing tests for them is recommended even if you are using TypeScript. + +Many of the APIs accept `TRecord` and `TResult` type parameters, using which we can specify the type of a row in the database table and the type of the result of the query respectively. This is helpful for auto-completion when using TypeScript-aware editors like VSCode. + +To reduce boilerplate and add inferred types, you can augment `Tables` interface in `'knex/types/tables'` module. + +```ts +import { Knex } from 'knex'; + +declare module 'knex/types/tables' { + interface User { + id: number; + name: string; + created_at: string; + updated_at: string; + } + + interface Tables { + // This is same as specifying `knex('users')` + users: User; + // For more advanced types, you can specify separate type + // for base model, "insert" type and "update" type. + // But first: notice that if you choose to use this, + // the basic typing showed above can be ignored. + // So, this is like specifying + // knex + // .insert<{ name: string }>({ name: 'name' }) + // .into<{ name: string, id: number }>('users') + users_composite: Knex.CompositeTableType< + // This interface will be used for return type and + // `where`, `having` etc where full type is required + User, + // Specifying "insert" type will also make sure + // data matches interface in full. Meaning + // if interface is `{ a: string, b: string }`, + // `insert({ a: '' })` will complain about missing fields. + // + // For example, this will require only "name" field when inserting + // and make created_at and updated_at optional. + // And "id" can't be provided at all. + // Defaults to "base" type. + Pick & Partial>, + // This interface is used for "update()" calls. + // As opposed to regular specifying interface only once, + // when specifying separate update interface, user will be + // required to match it exactly. So it's recommended to + // provide partial interfaces for "update". Unless you want to always + // require some field (e.g., `Partial & { updated_at: string }` + // will allow updating any field for User but require updated_at to be + // always provided as well. + // + // For example, this wil allow updating all fields except "id". + // "id" will still be usable for `where` clauses so + // knex('users_composite') + // .update({ name: 'name2' }) + // .where('id', 10)` + // will still work. + // Defaults to Partial "insert" type + Partial> + >; + } +} +``` + +When TypeScript is configured to use a modern module resolution setting (`node16`, `nodenext`, etc.), the compiler expects that the declared module name ends with a `.js` file type. You will need to declare your inferred types as follows instead: + +```ts +// The trailing `.js` is required by the TypeScript compiler in certain configs: +declare module 'knex/types/tables.js' { + // <----- Different module path!!! + interface Tables { + // ... + } +} +``` diff --git a/docs/src/guide/interfaces.md b/docs/src/guide/interfaces.md new file mode 100644 index 0000000000..45cc7b09e7 --- /dev/null +++ b/docs/src/guide/interfaces.md @@ -0,0 +1,290 @@ +# Interfaces + +Knex.js provides several options to deal with query output. The following methods are present on the query builder, schema builder, and the raw builder: + +## Promises + +[Promises](https://github.com/petkaantonov/bluebird#what-are-promises-and-why-should-i-use-them) are the preferred way of dealing with queries in knex, as they allow you to return values from a fulfillment handler, which in turn become the value of the promise. The main benefit of promises are the ability to catch thrown errors without crashing the node app, making your code behave like a **.try / .catch / .finally** in synchronous code. + +```js +knex + .select('name') + .from('users') + .where('id', '>', 20) + .andWhere('id', '<', 200) + .limit(10) + .offset(x) + .then(function (rows) { + return _.pluck(rows, 'name'); + }) + .then(function (names) { + return knex.select('id').from('nicknames').whereIn('nickname', names); + }) + .then(function (rows) { + console.log(rows); + }) + .catch(function (error) { + console.error(error); + }); +``` + +### then + +**.then(onFulfilled, [onRejected])** + +Coerces the current query builder chain into a promise state, accepting the resolve and reject handlers as specified by the Promises/A+ spec. As stated in the spec, more than one call to the then method for the current query chain will resolve with the same value, in the order they were called; the query will not be executed multiple times. + +```js +knex + .select('*') + .from('users') + .where({ name: 'Tim' }) + .then(function (rows) { + return knex + .insert({ user_id: rows[0].id, name: 'Test' }, 'id') + .into('accounts'); + }) + .then(function (id) { + console.log('Inserted Account ' + id); + }) + .catch(function (error) { + console.error(error); + }); +``` + +### catch + +**.catch(onRejected)** + +Coerces the current query builder into a promise state, catching any error thrown by the query, the same as calling .then(null, onRejected). + +```js +return knex + .insert({ id: 1, name: 'Test' }, 'id') + .into('accounts') + .catch(function (error) { + console.error(error); + }) + .then(function () { + return knex.select('*').from('accounts').where('id', 1); + }) + .then(function (rows) { + console.log(rows[0]); + }) + .catch(function (error) { + console.error(error); + }); +``` + +## Callbacks + +### asCallback + +**.asCallback(callback)** + +If you'd prefer a callback interface over promises, the asCallback function accepts a standard node style callback for executing the query chain. Note that as with the then method, subsequent calls to the same query chain will return the same result. + +```js +knex + .select('name') + .from('users') + .where('id', '>', 20) + .andWhere('id', '<', 200) + .limit(10) + .offset(x) + .asCallback(function (err, rows) { + if (err) return console.error(err); + knex + .select('id') + .from('nicknames') + .whereIn('nickname', _.pluck(rows, 'name')) + .asCallback(function (err, rows) { + if (err) return console.error(err); + console.log(rows); + }); + }); +``` + +## Streams + +Streams are a powerful way of piping data through as it comes in, rather than all at once. You can read more about streams [here at substack's stream handbook](https://github.com/substack/stream-handbook). See the following for example uses of stream & pipe. If you wish to use streams with PostgreSQL, you must also install the [pg-query-stream](https://github.com/brianc/node-pg-query-stream) module. If you wish to use streams with the `pgnative` dialect, please be aware that the results will not be streamed as they are received, but rather streamed after the entire result set has returned. On an HTTP server, make sure to [manually close your streams](https://github.com/knex/knex/wiki/Manually-Closing-Streams) if a request is aborted. + +### stream + +**.stream([options], [callback])** + +If called with a callback, the callback is passed the stream and a promise is returned. Otherwise, the readable stream is returned. +When the stream is consumed as an [iterator](https://nodejs.org/api/stream.html#readablesymbolasynciterator), if the loop terminates with a `break`, `return`, or a `throw`, the stream will be destroyed. In other terms, iterating over a stream will consume the stream fully. + +```js +// Retrieve the stream: +const stream = knex.select('*').from('users').stream(); +stream.pipe(writableStream); +``` + +```js +// With options: +const stream = knex.select('*').from('users').stream({ highWaterMark: 5 }); +stream.pipe(writableStream); +``` + +```js +// Use as an iterator +const stream = knex.select('*').from('users').stream(); + +for await (const row of stream) { + /* ... */ +} +``` + +```js +// Use as a promise: +const stream = knex + .select('*') + .from('users') + .where(knex.raw('id = ?', [1])) + .stream(function (stream) { + stream.pipe(writableStream); + }) + .then(function () { + /* ... */ + }) + .catch(function (e) { + console.error(e); + }); +``` + +### pipe + +**.pipe(writableStream)** + +Pipe a stream for the current query to a writableStream. + +```js +const stream = knex.select('*').from('users').pipe(writableStream); +``` + +## Events + +### query + +A query event is fired just before a query takes place, providing data about the query, including the connection's `__knexUid` / `__knexTxId` properties and any other information about the query as described in toSQL. Useful for logging all queries throughout your application. + +```js +knex + .select('*') + .from('users') + .on('query', function (data) { + app.log(data); + }) + .then(function () { + // ... + }); +``` + +### query-error + +A query-error event is fired when an error occurs when running a query, providing the error object and data about the query, including the connection's `__knexUid` / `__knexTxId` properties and any other information about the query as described in toSQL. Useful for logging all query errors throughout your application. + +```js +knex + .select(['NonExistentColumn']) + .from('users') + .on('query-error', function (error, obj) { + app.log(error); + }) + .then(function () { + /* ... */ + }) + .catch(function (error) { + // Same error object as the query-error event provides. + }); +``` + +### query-response + +A query-response event is fired when a successful query has been run, providing the response of the query and data about the query, including the connection's `__knexUid` / `__knexTxId` properties and any other information about the query as described in toSQL, and finally the query builder used for the query. + +```js +knex + .select('*') + .from('users') + .on('query-response', function (response, obj, builder) { + // ... + }) + .then(function (response) { + // Same response as the emitted event + }) + .catch(function (error) {}); +``` + +### start + +A `start` event is fired right before a query-builder is compiled. + +::: info +While this event can be used to alter a builders state prior to compilation it is not to be recommended. Future goals include ways of doing this in a different manner such as hooks. +::: + +```js +knex + .select('*') + .from('users') + .on('start', function (builder) { + builder.where('IsPrivate', 0); + }) + .then(function (Rows) { + //Only contains Rows where IsPrivate = 0 + }) + .catch(function (error) {}); +``` + +## Other + +### toString + +**.toString()** + +Returns an array of query strings filled out with the correct values based on bindings, etc. Useful for debugging, but should not be used to create queries for running them against DB. + +```js +const toStringQuery = knex.select('*').from('users').where('id', 1).toString(); + +// Outputs: console.log(toStringQuery); +// select * from "users" where "id" = 1 +``` + +### toSQL + +**.toSQL()** +**.toSQL().toNative()** + +Returns an array of query strings filled out with the correct values based on bindings, etc. Useful for debugging and building queries for running them manually with DB driver. `.toSQL().toNative()` outputs object with sql string and bindings in a dialects format in the same way that knex internally sends them to underlying DB driver. + +```js +knex + .select('*') + .from('users') + .where(knex.raw('id = ?', [1])) + .toSQL(); +// Outputs: +// { +// bindings: [1], +// method: 'select', +// sql: 'select * from "users" where id = ?', +// options: undefined, +// toNative: function () {} +// } + +knex + .select('*') + .from('users') + .where(knex.raw('id = ?', [1])) + .toSQL() + .toNative(); +// Outputs for postgresql dialect: +// { +// bindings: [1], +// sql: 'select * from "users" where id = $1', +// } +``` diff --git a/docs/src/guide/migrations.md b/docs/src/guide/migrations.md new file mode 100644 index 0000000000..c834e60620 --- /dev/null +++ b/docs/src/guide/migrations.md @@ -0,0 +1,644 @@ +# Migrations + +Migrations allow for you to define sets of schema changes so upgrading a database is a breeze. + +## Migration CLI + +The migration CLI is bundled with the knex install, and is driven by the [node-liftoff](https://github.com/tkellen/node-liftoff) module. To install globally, run: + +```bash +$ npm install knex -g +``` + +The migration CLI accepts the following general command-line options. You can view help text and additional options for each command using `--help`. E.g. `knex migrate:latest --help`. + +- `--debug`: Run with debugging +- `--knexfile [path]`: Specify the knexfile path +- `--knexpath [path]`: Specify the path to the knex instance +- `--cwd [path]`: Specify the working directory +- `--client [name]`: Set the DB client +- `--connection [address]`: Set the DB connection +- `--migrations-table-name`: Set the migration table name +- `--migrations-directory`: Set the migrations directory +- `--env`: environment, default: `process.env.NODE_ENV || development` +- `--esm`: [Enables ESM module interoperability](#ecmascript-modules-esm-interoperability) +- `--help`: Display help text for a particular command and exit. + +Migrations use a **knexfile**, which specify various configuration settings for the module. To create a new knexfile, run the following: + +```bash +$ knex init + +# or for .ts + +$ knex init -x ts +``` + +will create a sample knexfile.js - the file which contains our various database configurations. Once you have a knexfile.js, you can use the migration tool to create migration files to the specified directory (default migrations). Creating new migration files can be achieved by running: + +```bash +$ knex migrate:make migration_name + +# or for .ts + +$ knex migrate:make migration_name -x ts +``` + +- you can also create your migration using a specific stub file, this serves as a migration template to speed up development for common migration operations +- if the --stub option is not passed, the CLI will use either the knex default stub for the chosen extension, or the config.stub file + +```bash +$ knex migrate:make --stub + +# or + +$ knex migrate:make --stub +``` + +- if a stub path is provided, it must be relative to the knexfile.\[js, ts, etc\] location +- if a is used, the stub is selected by its file name. The CLI will look for this file in the config.migrations.directory folder. If the config.migrations.directory is not defined, this operation will fail + +Once you have finished writing the migrations, you can update the database matching your `NODE_ENV` by running: + +```bash +$ knex migrate:latest +``` + +You can also pass the `--env` flag or set `NODE_ENV` to select an alternative environment: + +```bash +$ knex migrate:latest --env production + +# or + +$ NODE_ENV=production knex migrate:latest +``` + +To rollback the last batch of migrations: + +```bash +$ knex migrate:rollback +``` + +To rollback all the completed migrations: + +```bash +$ knex migrate:rollback --all +``` + +To run the next migration that has not yet been run + +```bash +$ knex migrate:up +``` + +To run the specified migration that has not yet been run + +```bash +$ knex migrate:up 001_migration_name.js +``` + +To undo the last migration that was run + +```bash +$ knex migrate:down +``` + +To undo the specified migration that was run + +```bash +$ knex migrate:down 001_migration_name.js +``` + +To list both completed and pending migrations: + +```bash +$ knex migrate:list +``` + +## Seed files + +Seed files allow you to populate your database with test or seed data independent of your migration files. + +### Seed CLI + +To create a seed file, run: + +```bash +$ knex seed:make seed_name +``` + +Seed files are created in the directory specified in your knexfile.js for the current environment. A sample seed configuration looks like: + +```js +module.exports = { + // ... + development: { + client: { + /* ... */ + }, + connection: { + /* ... */ + }, + seeds: { + directory: './seeds/dev', + }, + }, + // ... +}; +``` + +If no `seeds.directory` is defined, files are created in `./seeds`. Note that the seed directory needs to be a relative path. Absolute paths are not supported (nor is it good practice). + +To run seed files, execute: + +```bash +$ knex seed:run +``` + +Seed files are executed in alphabetical order. Unlike migrations, _every_ seed file will be executed when you run the command. You should design your seed files to reset tables as needed before inserting data. + +To run specific seed files, execute: + +```bash +$ knex seed:run --specific=seed-filename.js --specific=another-seed-filename.js +``` + +## knexfile.js + +A knexfile.js generally contains all of the configuration for your database. It can optionally provide different configuration for different environments. You may pass a `--knexfile` option to any of the command line statements to specify an alternate path to your knexfile. + +### Basic configuration + +```js +module.exports = { + client: 'pg', + connection: process.env.DATABASE_URL || { + user: 'me', + database: 'my_app', + }, +}; +``` + +You can also use an async function to get connection details for your configuration. This is useful when you need to fetch credentials from a secure location like vault. + +```js +const getPassword = async () => { + // TODO: implement me + return 'my_pass'; +}; + +module.exports = { + client: 'pg', + connection: async () => { + const password = await getPassword(); + return { user: 'me', password }; + }, + migrations: {}, +}; +``` + +### Environment configuration + +```js +module.exports = { + development: { + client: 'pg', + connection: { user: 'me', database: 'my_app' }, + }, + production: { + client: 'pg', + connection: process.env.DATABASE_URL, + }, +}; +``` + +### Custom migration + +You may provide a custom migration stub to be used in place of the default option. + +```js +module.exports = { + client: 'pg', + migrations: { + stub: 'migration.stub', + }, +}; +``` + +### Custom migration name + +You may provide a custom migration name to be used in place of the default option. + +```js +module.exports = { + client: 'pg', + migrations: { + getNewMigrationName: (name) => { + return `${+new Date()}-${name}.js`; + }, + }, +}; +``` + +### Generated migration extension + +You can control extension of generated migrations. + +```js +module.exports = { + client: 'pg', + migrations: { + extension: 'ts', + }, +}; +``` + +### Knexfile in other languages + +Knex uses [Liftoff](https://github.com/js-cli/js-liftoff) to support knexfile written in other compile-to-js languages. + +Depending on the language, this may require you to install additional dependencies. The complete list of dependencies for each supported language can be found [here](https://github.com/gulpjs/interpret#extensions). + +Most common cases are typescript (for which [typescript](https://www.npmjs.com/package/typescript) and [ts-node](https://www.npmjs.com/package/ts-node) packages are recommended), and coffeescript (for which [coffeescript](https://www.npmjs.com/package/coffeescript) dependency is required). + +If you don't specify the extension explicitly, the extension of generated migrations/seed files will be inferred from the knexfile extension + +## Migration API + +`knex.migrate` is the class utilized by the knex migrations cli. + +Each method takes an optional `config` object, which may specify the following properties: + +- `directory`: a relative path to the directory containing the migration files. Can be an array of paths (default `./migrations`) +- `extension`: the file extension used for the generated migration files (default `js`) +- `tableName`: the table name used for storing the migration state (default `knex_migrations`) +- `schemaName`: the schema name used for storing the table with migration state (optional parameter, only works on DBs that support multiple schemas in a single DB, such as PostgreSQL) +- `disableTransactions`: don't run migrations inside transactions (default `false`) +- `disableMigrationsListValidation`: do not validate that all the already executed migrations are still present in migration directories (default `false`) +- `sortDirsSeparately`: if true and multiple directories are specified, all migrations from a single directory will be executed before executing migrations in the next folder (default `false`) +- `loadExtensions`: array of file extensions which knex will treat as migrations. For example, if you have typescript transpiled into javascript in the same folder, you want to execute only javascript migrations. In this case, set `loadExtensions` to `['.js']` (Notice the dot!) (default `['.co', '.coffee', '.eg', '.iced', '.js', '.litcoffee', '.ls', '.ts']`) +- `migrationSource`: specify a custom migration source, see [Custom Migration Source](#custom-migration-sources) for more info (default filesystem) + +### Transactions in migrations + +By default, each migration is run inside a transaction. Whenever needed, one can disable transactions for all migrations via the common migration config option `config.disableTransactions` or per-migration, via exposing a boolean property `config.transaction` from a migration file: + +```js +exports.up = function (knex) { + return knex.schema + .createTable('users', function (table) { + table.increments('id'); + table.string('first_name', 255).notNullable(); + table.string('last_name', 255).notNullable(); + }) + .createTable('products', function (table) { + table.increments('id'); + table.decimal('price').notNullable(); + table.string('name', 1000).notNullable(); + }); +}; + +exports.down = function (knex) { + return knex.schema.dropTable('products').dropTable('users'); +}; + +exports.config = { transaction: false }; +``` + +The same config property can be used for enabling transaction per-migration in case the common configuration has `disableTransactions: true`. + +### make + +**knex.migrate.make(name, [config])** + +Creates a new migration, with the name of the migration being added. + +### latest + +**knex.migrate.latest([config])** + +Runs all migrations that have not yet been run. + +If you need to run something only after all migrations have finished their execution, you can do something like this: + +```js +knex.migrate + .latest() + .then(function () { + return knex.seed.run(); + }) + .then(function () { + // migrations are finished + }); +``` + +### rollback + +**knex.migrate.rollback([config], [all])** + +Rolls back the latest migration group. If the `all` parameter is truthy, all applied migrations will be rolled back instead of just the last batch. The default value for this parameter is `false`. + +### up + +**knex.migrate.up([config])** + +Runs the specified (by `config.name` parameter) or the next chronological migration that has not yet be run. + +### down + +**knex.migrate.down([config])** + +Will undo the specified (by `config.name` parameter) or the last migration that was run. + +### currentVersion + +**knex.migrate.currentVersion([config])** + +Retrieves and returns the current migration version, as a promise. If there aren't any migrations run yet, returns "none" as the value for the currentVersion. + +### list + +**knex.migrate.list([config])** + +Will return list of completed and pending migrations + +### unlock + +**knex.migrate.forceFreeMigrationsLock([config])** + +Forcibly unlocks the migrations lock table, and ensures that there is only one row in it. + +## Notes about locks + +A lock system is there to prevent multiple processes from running the same migration batch in the same time. When a batch of migrations is about to be run, the migration system first tries to get a lock using a `SELECT ... FOR UPDATE` statement (preventing race conditions from happening). If it can get a lock, the migration batch will run. If it can't, it will wait until the lock is released. + +Please note that if your process unfortunately crashes, the lock will have to be _manually_ removed with `knex migrate:unlock` in order to let migrations run again. + +The locks are saved in a table called "`tableName`\_lock"; it has a column called `is_locked` that `knex migrate:unlock` sets to `0` in order to release the lock. The `index` column in the lock table exists for compatibility with some database clusters that require a primary key, but is otherwise unused. There must be only one row in this table, or an error will be thrown when running migrations: "Migration table is already locked". Run `knex migrate:unlock` to ensure that there is only one row in the table. + +## Custom migration sources + +Knex supports custom migration sources, allowing you full control of where your migrations come from. This can be useful for custom folder structures, when bundling with webpack/browserify and other scenarios. + +```js +// Create a custom migration source class +class MyMigrationSource { + // Must return a Promise containing a list of migrations. + // Migrations can be whatever you want, + // they will be passed as arguments to getMigrationName + // and getMigration + getMigrations() { + // In this example we are just returning migration names + return Promise.resolve(['migration1']); + } + + getMigrationName(migration) { + return migration; + } + + getMigration(migration) { + switch (migration) { + case 'migration1': + return { + up(knex) { + /* ... */ + }, + down(knex) { + /* ... */ + }, + }; + } + } +} + +// pass an instance of your migration source as knex config +knex.migrate.latest({ + migrationSource: new MyMigrationSource(), +}); +``` + +### Webpack migration source example + +An example of how to create a migration source where migrations are included in a webpack bundle. + +```js +const path = require('path'); + +class WebpackMigrationSource { + constructor(migrationContext) { + this.migrationContext = migrationContext; + } + + getMigrations() { + return Promise.resolve(this.migrationContext.keys().sort()); + } + + getMigrationName(migration) { + return path.parse(migration).base; + } + + getMigration(migration) { + return this.migrationContext(migration); + } +} + +// pass an instance of your migration source as knex config +knex.migrate.latest({ + migrationSource: new WebpackMigrationSource( + require.context('./migrations', false, /.js$/) + ), +}); + +// with webpack >=5, require.context will add +// both the relative and absolute paths to the context +// to avoid duplicate migration errors, you'll need +// to filter out one or the other this example filters +// out absolute paths, leaving only the relative +// ones(./migrations/*.js): +knex.migrate.latest({ + migrationSource: new WebpackMigrationSource( + require.context('./migrations', false, /^\.\/.*\.js$/) + ), +}); +``` + +## ECMAScript modules (ESM) Interoperability + +ECMAScript Module support for knex CLI's configuration, migration and seeds +enabled by the `--esm` flag, ECMAScript Interoperability is provided by the [_'esm'_](https://github.com/standard-things/esm) module. +You can find [here](https://github.com/standard-things/esm) more information about 'esm' superpowers. + +Node 'mjs' files are handled by NodeJS own import mechanics +and do not require the use of the '--esm' flag. +But you might need it anyway for Node v10 under certain scenarios. +You can find details about NodeJS ECMAScript modules [here](https://nodejs.org/api/esm.html) + +While it is possible to mix and match different module formats (extensions) +between your knexfile, seeds and migrations, +some format combinations will require specific NodeJS versions, +_Notably mjs/cjs files will follow NodeJS import and require restrictions._ +You can see [here](https://github.com/knex/knex/blob/master/test/cli/esm-interop.spec.js) many possible scenarios, +and [here](https://github.com/knex/knex/tree/master/test/jake-util/knexfile-imports) some sample configurations + +Node v10.\* require the use of the '--experimental-module' flag in order to use the 'mjs' or 'cjs' extension. + +```bash +# launching knex on Node v10 to use mjs/cjs modules +node --experimental-modules ./node_modules/.bin/knex $@ +``` + +When using migration and seed files with '.cjs' or '.mjs' extensions, you will need to specify that explicitly: + +```ts +/** + * knexfile.mjs + */ +export default { + migrations: { + // ... client, connection,etc .... + directory: './migrations', + loadExtensions: ['.mjs'], // + }, +}; +``` + +When using '.mjs' extensions for your knexfile and '.js' for the seeds/migrations, you will need to specify that explicitly. + +```ts +/** + * knexfile.mjs + */ +export default { + migrations: { + // ... client, connection,etc .... + directory: './migrations', + loadExtensions: ['.js'], // knex will search for 'mjs' file by default + }, +}; +``` + +For the knexfile you can use a default export, +it will take precedence over named export. + +```ts +/** + * filename: knexfile.js + * For the knexfile you can use a default export + **/ +export default { + client: 'sqlite3', + connection: { + filename: '../test.sqlite3', + }, + migrations: { + directory: './migrations', + }, + seeds: { + directory: './seeds', + }, +}; + +/** + * filename: knexfile.js + * Let knex find the configuration by providing named exports, + * but if exported a default, it will take precedence, and it will be used instead + **/ +const config = { + client: 'sqlite3', + connection: { + filename: '../test.sqlite3', + }, + migrations: { + directory: './migrations', + }, + seeds: { + directory: './seeds', + }, +}; +/** this will be used, it has precedence over named export */ +export default config; +/** Named exports, will be used if you didn't provide a default export */ +export const { client, connection, migrations, seeds } = config; +``` + +Seed and migration files need to follow Knex conventions + +```ts +// file: seed.js +/** + * Same as with the CommonJS modules + * You will need to export a "seed" named function. + * */ +export function seed(knex) { + // ... seed logic here +} + +// file: migration.js +/** + * Same as the CommonJS version, the miration file should export + * "up" and "down" named functions + */ +export function up(knex) { + // ... migration logic here +} +export function down(knex) { + // ... migration logic here +} +``` + +## Seed API + +`knex.seed` is the class utilized by the knex seed CLI. + +Each method takes an optional `config` object, which may specify the following properties: + +- `directory`: a relative path to the directory containing the seed files. Can be an array of paths (default `./seeds`) +- `loadExtensions`: array of file extensions which knex will treat as seeds. For example, if you have typescript transpiled into javascript in the same folder, you want to execute only javascript seeds. In this case, set `loadExtensions` to `['.js']` (Notice the dot!) (default `['.co', '.coffee', '.eg', '.iced', '.js', '.litcoffee', '.ls', '.ts']`) +- `recursive`: if true, will find seed files recursively in the directory / directories specified +- `specific`: a specific seed file or an array of seed files to run from the seeds directory, if its value is `undefined` it will run all the seeds (default `undefined`). If an array is specified, seed files will be run in the same order as the array +- `sortDirsSeparately`: if true and multiple directories are specified, all seeds from a single directory will be executed before executing seeds in the next folder (default `false`) +- `seedSource`: specify a custom seed source, see [Custom Seed Source](#custom-seed-sources) for more info (default filesystem) +- `extension`: extension to be used for newly generated seeds (default `js`) +- `timestampFilenamePrefix`: whether timestamp should be added as a prefix for newly generated seeds (default `false`) + +### make + +**knex.seed.make(name, [config])** + +Creates a new seed file, with the name of the seed file being added. If the seed directory config is an array of paths, the seed file will be generated in the latest specified. + +### run + +**knex.seed.run([config])** + +Runs all seed files for the current environment. + +## Custom seed sources + +Knex supports custom seed sources, allowing you full control of where your seeds come from. This can be useful for custom folder structures, when bundling with webpack/browserify and other scenarios. + +```js +// Create a custom seed source class +class MySeedSource { + // Must return a Promise containing a list of seeds. + // Seeds can be whatever you want, they will be passed as + // arguments to getSeed + getSeeds() { + // In this example we are just returning seed names + return Promise.resolve(['seed1']); + } + + getSeed(seed) { + switch (seed) { + case 'seed1': + return (knex) => { + /* ... */ + }; + } + } +} + +// pass an instance of your seed source as knex config +knex.seed.run({ seedSource: new MySeedSource() }); +``` diff --git a/docs/src/guide/query-builder.md b/docs/src/guide/query-builder.md new file mode 100644 index 0000000000..9ecc191ece --- /dev/null +++ b/docs/src/guide/query-builder.md @@ -0,0 +1,2656 @@ +# Knex Query Builder + +The heart of the library, the knex query builder is the interface used for building and executing standard SQL queries, such as `select`, `insert`, `update`, `delete`. + +## Identifier Syntax + +In many places in APIs identifiers like table name or column name can be passed to methods. + +Most commonly one needs just plain `tableName.columnName`, `tableName` or `columnName`, but in many cases one also needs to pass an alias how that identifier is referred later on in the query. + +There are two ways to declare an alias for identifier. One can directly give `as aliasName` suffix for the identifier (e.g. `identifierName as aliasName`) or one can pass an object `{ aliasName: 'identifierName' }`. + +If the object has multiple aliases `{ alias1: 'identifier1', alias2: 'identifier2' }`, then all the aliased identifiers are expanded to comma separated list. + +::: info +Identifier syntax has no place for selecting schema, so if you are doing `schemaName.tableName`, query might be rendered wrong. Use `.withSchema('schemaName')` instead. + +```js +knex({ a: 'table', b: 'table' }) + .select({ + aTitle: 'a.title', + bTitle: 'b.title', + }) + .whereRaw('?? = ??', ['a.column_1', 'b.column_2']); +``` + +::: + +## Common + +### knex + +**knex(tableName, options={only: boolean})** +**knex.[methodName]** + +The query builder starts off either by specifying a tableName you wish to query against, or by calling any method directly on the knex object. This kicks off a jQuery-like chain, with which you can call additional query builder methods as needed to construct the query, eventually calling any of the interface methods, to either convert toString, or execute the query with a promise, callback, or stream. Optional second argument for passing options:\* **only**: if `true`, the ONLY keyword is used before the `tableName` to discard inheriting tables' data. + +::: warning +Only supported in PostgreSQL for now. +::: + +#### Usage with TypeScript + +If using TypeScript, you can pass the type of database row as a type parameter to get better autocompletion support down the chain. + +```ts +interface User { + id: number; + name: string; + age: number; +} + +knex('users').where('id').first(); // Resolves to any + +knex('users') // User is the type of row in database + .where('id', 1) // Your IDE will be able to help with the completion of id + .first(); // Resolves to User | undefined +``` + +It is also possible to take advantage of auto-completion support (in TypeScript-aware IDEs) with generic type params when writing code in plain JavaScript through JSDoc comments. + +```js +/** + * @typedef {Object} User + * @property {number} id + * @property {number} age + * @property {string} name + * + * @returns {Knex.QueryBuilder} + */ +const Users = () => knex('Users'); + +// 'id' property can be autocompleted by editor +Users().where('id', 1); +``` + +##### Caveat with type inference and mutable fluent APIs + +Most of the knex APIs mutate current object and return it. This pattern does not work well with type-inference. + +```ts +knex('users') + .select('id') + .then((users) => { + // Type of users is inferred as Pick[] + // Do something with users + }); + +knex('users') + .select('id') + .select('age') + .then((users) => { + // Type of users is inferred as Pick[] + // Do something with users + }); + +// The type of usersQueryBuilder is determined here +const usersQueryBuilder = knex('users').select('id'); + +if (someCondition) { + // This select will not change the type of usersQueryBuilder + // We can not change the type of a pre-declared variable in TypeScript + usersQueryBuilder.select('age'); +} +usersQueryBuilder.then((users) => { + // Type of users here will be Pick[] + // which may not be what you expect. +}); + +// You can specify the type of result explicitly through a second type parameter: +const queryBuilder = knex>('users'); + +// But there is no type constraint to ensure that these properties have actually been +// selected. + +// So, this will compile: +queryBuilder.select('name').then((users) => { + // Type of users is Pick but it will only have name +}); +``` + +If you don't want to manually specify the result type, it is recommended to always use the type of last value of the chain and assign result of any future chain continuation to a separate variable (which will have a different type). + +#### timeout + +**.timeout(ms, options={cancel: boolean})** + +Sets a timeout for the query and will throw a TimeoutError if the timeout is exceeded. The error contains information about the query, bindings, and the timeout that was set. Useful for complex queries that you want to make sure are not taking too long to execute. Optional second argument for passing options:\* **cancel**: if `true`, cancel query if timeout is reached. + +::: warning +Only supported in MySQL and PostgreSQL for now. +::: + +```js +knex.select().from('books').timeout(1000); + +knex.select().from('books').timeout(1000, { + cancel: true, // MySQL and PostgreSQL only +}); +``` + +### select + +**.select([\*columns])** + +Creates a select query, taking an optional array of columns for the query, eventually defaulting to \* if none are specified when the query is built. The response of a select call will resolve with an array of objects selected from the database. + +```js +knex.select('title', 'author', 'year').from('books'); + +knex.select().table('books'); +``` + +##### Usage with TypeScript + +We are generally able to infer the result type based on the columns being selected as long as the select arguments match exactly the key names in record type. However, aliasing and scoping can get in the way of inference. + +```ts +knex.select('id').from('users'); // Resolves to Pick[] + +knex.select('users.id').from('users'); // Resolves to any[] +// ^ TypeScript doesn't provide us a way to look into a string and infer the type +// from a substring, so we fall back to any + +// We can side-step this using knex.ref: +knex.select(knex.ref('id').withSchema('users')).from('users'); // Resolves to Pick[] + +knex.select('id as identifier').from('users'); // Resolves to any[], for same reason as above + +// Refs are handy here too: +knex.select(knex.ref('id').as('identifier')).from('users'); // Resolves to { identifier: number; }[] +``` + +### as + +**.as(name)** + +Allows for aliasing a subquery, taking the string you wish to name the current query. If the query is not a sub-query, it will be ignored. + +```ts +knex + .avg('sum_column1') + .from(function () { + this.sum('column1 as sum_column1').from('t1').groupBy('column1').as('t1'); + }) + .as('ignored_alias'); +``` + +### column + +**.column(columns)** + +Specifically set the columns to be selected on a select query, taking an array, an object or a list of column names. Passing an object will automatically alias the columns with the given keys. + +```js +knex.column('title', 'author', 'year').select().from('books'); + +knex.column(['title', 'author', 'year']).select().from('books'); + +knex.column('title', { by: 'author' }, 'year').select().from('books'); +``` + +### from + +**.from([tableName], options={only: boolean})** + +Specifies the table used in the current query, replacing the current table name if one has already been specified. This is typically used in the sub-queries performed in the advanced where or union methods. Optional second argument for passing options:\* **only**: if `true`, the ONLY keyword is used before the `tableName` to discard inheriting tables' data. + +::: warning +Only supported in PostgreSQL for now. +::: + +```js +knex.select('*').from('users'); +``` + +#### Usage with TypeScript + +We can specify the type of database row through the TRecord type parameter + +```ts +knex.select('id').from('users'); // Resolves to any[] + +knex.select('id').from('users'); // Results to Pick[] +``` + +### fromRaw + +**.fromRaw(sql, [bindings])** + +```js +knex.select('*').fromRaw('(select * from "users" where "age" > ?)', '18'); +``` + +### with + +**.with(alias, [columns], callback|builder|raw)** + +Add a "with" clause to the query. "With" clauses are supported by PostgreSQL, Oracle, SQLite3 and MSSQL. An optional column list can be provided after the alias; if provided, it must include at least one column name. + +```js +knex + .with( + 'with_alias', + knex.raw('select * from "books" where "author" = ?', 'Test') + ) + .select('*') + .from('with_alias'); + +knex + .with( + 'with_alias', + ['title'], + knex.raw('select "title" from "books" where "author" = ?', 'Test') + ) + .select('*') + .from('with_alias'); + +knex + .with('with_alias', (qb) => { + qb.select('*').from('books').where('author', 'Test'); + }) + .select('*') + .from('with_alias'); +``` + +### withRecursive + +**.withRecursive(alias, [columns], callback|builder|raw)** + +Identical to the `with` method except "recursive" is appended to "with" (or not, as required by the target database) to make self-referential CTEs possible. Note that some databases, such as Oracle, require a column list be provided when using an rCTE. + +```js +knex + .withRecursive('ancestors', (qb) => { + qb.select('*') + .from('people') + .where('people.id', 1) + .union((qb) => { + qb.select('*') + .from('people') + .join('ancestors', 'ancestors.parentId', 'people.id'); + }); + }) + .select('*') + .from('ancestors'); + +knex + .withRecursive('family', ['name', 'parentName'], (qb) => { + qb.select('name', 'parentName') + .from('folks') + .where({ name: 'grandchild' }) + .unionAll((qb) => + qb + .select('folks.name', 'folks.parentName') + .from('folks') + .join('family', knex.ref('family.parentName'), knex.ref('folks.name')) + ); + }) + .select('name') + .from('family'); +``` + +### withMaterialized + +**.withMaterialized(alias, [columns], callback|builder|raw)** + +Add a "with" materialized clause to the query. "With" materialized clauses are supported by PostgreSQL and SQLite3. An optional column list can be provided after the alias; if provided, it must include at least one column name. + +```js +knex + .withMaterialized( + 'with_alias', + knex.raw('select * from "books" where "author" = ?', 'Test') + ) + .select('*') + .from('with_alias'); + +knex + .withMaterialized( + 'with_alias', + ['title'], + knex.raw('select "title" from "books" where "author" = ?', 'Test') + ) + .select('*') + .from('with_alias'); + +knex + .withMaterialized('with_alias', (qb) => { + qb.select('*').from('books').where('author', 'Test'); + }) + .select('*') + .from('with_alias'); +``` + +### withNotMaterialized + +**.withNotMaterialized(alias, [columns], callback|builder|raw)** + +Add a "with" not materialized clause to the query. "With" not materialized clauses are supported by PostgreSQL and SQLite3. An optional column list can be provided after the alias; if provided, it must include at least one column name. + +```js +knex + .withNotMaterialized( + 'with_alias', + knex.raw('select * from "books" where "author" = ?', 'Test') + ) + .select('*') + .from('with_alias'); + +knex + .withNotMaterialized( + 'with_alias', + ['title'], + knex.raw('select "title" from "books" where "author" = ?', 'Test') + ) + .select('*') + .from('with_alias'); + +knex + .withNotMaterialized('with_alias', (qb) => { + qb.select('*').from('books').where('author', 'Test'); + }) + .select('*') + .from('with_alias'); +``` + +### withSchema + +**.withSchema([schemaName])** + +Specifies the schema to be used as prefix of table name. + +```js +knex.withSchema('public').select('*').from('users'); +``` + +### jsonExtract + +**.jsonExtract(column|builder|raw|array[], path, [alias], [singleValue])** + +Extract a value from a json column given a JsonPath. An alias can be specified. The singleValue boolean can be used to specify, with Oracle or MSSQL, if the value returned by the function is a single value or an array/object value. An array of arrays can be used to specify multiple extractions with one call to this function. + +```js +knex('accounts').jsonExtract('json_col', '$.name'); + +knex('accounts').jsonExtract('json_col', '$.name', 'accountName'); + +knex('accounts').jsonExtract('json_col', '$.name', 'accountName', true); + +knex('accounts').jsonExtract([ + ['json_col', '$.name', 'accountName'], + ['json_col', '$.lastName', 'accountLastName'], +]); +``` + +All json\*() functions can be used directly from knex object and can be nested. + +```js +knex('cities').jsonExtract([ + [knex.jsonRemove('population', '$.min'), '$', 'withoutMin'], + [knex.jsonRemove('population', '$.max'), '$', 'withoutMax'], + [knex.jsonSet('population', '$.current', '1234'), '$', 'currentModified'], +]); +``` + +### jsonSet + +**.jsonSet(column|builder|raw, path, value, [alias])** + +Return a json value/object/array where a given value is set at the given JsonPath. Value can be single value or json object. If a value already exists at the given place, the value is replaced. Not supported by Redshift and versions before Oracle 21c. + +```js +knex('accounts').jsonSet('json_col', '$.name', 'newName', 'newNameCol'); + +knex('accounts').jsonSet( + 'json_col', + '$.name', + { name: 'newName' }, + 'newNameCol' +); +``` + +### jsonInsert + +**.jsonInsert(column|builder|raw, path, value, [alias])** + +Return a json value/object/array where a given value is inserted at the given JsonPath. Value can be single value or json object. If a value exists at the given path, the value is not replaced. Not supported by Redshift and versions before Oracle 21c. + +```js +knex('accounts').jsonInsert('json_col', '$.name', 'newName', 'newNameCol'); + +knex('accounts').jsonInsert( + 'json_col', + '$.name', + { name: 'newName' }, + 'newNameCol' +); + +knex('accounts').jsonInsert( + knex.jsonExtract('json_col', '$.otherAccount'), + '$.name', + { name: 'newName' }, + 'newNameCol' +); +``` + +### jsonRemove + +**.jsonRemove(column|builder|raw, path, [alias])** + +Return a json value/object/array where a given value is removed at the given JsonPath. Not supported by Redshift and versions before Oracle 21c. + +```js +knex('accounts').jsonRemove('json_col', '$.name', 'colWithRemove'); + +knex('accounts').jsonInsert( + 'json_col', + '$.name', + { name: 'newName' }, + 'newNameCol' +); +``` + +### offset + +**.offset(value, options={skipBinding: boolean})** + +Adds an offset clause to the query. An optional skipBinding parameter may be specified which would avoid setting offset as a prepared value (some databases don't allow prepared values for offset). + +```js +knex.select('*').from('users').offset(10); + +knex.select('*').from('users').offset(10).toSQL().sql; + +// Offset value isn't a prepared value. +knex.select('*').from('users').offset(10, { skipBinding: true }).toSQL().sql; +``` + +### limit + +**.limit(value, options={skipBinding: boolean})** + +Adds a limit clause to the query. An optional skipBinding parameter may be specified to avoid adding limit as a prepared value (some databases don't allow prepared values for limit). + +```js +knex.select('*').from('users').limit(10).offset(30); + +knex.select('*').from('users').limit(10).offset(30).toSQL().sql; + +// Limit value isn't a prepared value. +knex + .select('*') + .from('users') + .limit(10, { skipBinding: true }) + .offset(30) + .toSQL().sql; +``` + +### union + +**.union([\*queries], [wrap])** + +Creates a union query, taking an array or a list of callbacks, builders, or raw statements to build the union statement, with optional boolean wrap. If the `wrap` parameter is `true`, the queries will be individually wrapped in parentheses. + +```js +knex + .select('*') + .from('users') + .whereNull('last_name') + .union(function () { + this.select('*').from('users').whereNull('first_name'); + }); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .union([knex.select('*').from('users').whereNull('first_name')]); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .union( + knex.raw('select * from users where first_name is null'), + knex.raw('select * from users where email is null') + ); +``` + +If you want to apply `orderBy`, `groupBy`, `limit`, `offset` or `having` to inputs of the union you need to use `knex.union` as a base statement. If you don't do this, those clauses will get appended to the end of the union. + +```js +// example showing how clauses get appended to the end of the query +knex('users') + .select('id', 'name') + .groupBy('id') + .union(knex('invitations').select('id', 'name').orderBy('expires_at')); + +knex.union([ + knex('users').select('id', 'name').groupBy('id'), + knex('invitations').select('id', 'name').orderBy('expires_at'), +]); +``` + +[before](https://michaelavila.com/knex-querylab/?query=NYOwpgHgFA5ArgZzAJwTAlAOiQGzAYwBdYBLAExgBoACGEAQwFswNMBzZAezgAcAhAJ6kKWOCBKcQUUJFIgAbiUL1CEkGiy4CxGOSq0GzVp2RkUg2JB4lkYBAH0VGdEA) and [after](https://michaelavila.com/knex-querylab/?query=NYOwpgHgdAriCWB7EAKA2qSKDkMDOYATntgJRQEA2YAxgC47wAm2ANAATYgCGAtmGSgBzQohgAHAEIBPRi1IdMERiABu8OtzpIQJclVoNszNpx79BiQkyIyckcfEJg8AfS1kAuqSA) + +### unionAll + +**.unionAll([\*queries], [wrap])** + +Creates a union all query, with the same method signature as the union method. If the `wrap` parameter is `true`, the queries will be individually wrapped in parentheses. + +```js +knex + .select('*') + .from('users') + .whereNull('last_name') + .unionAll(function () { + this.select('*').from('users').whereNull('first_name'); + }); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .unionAll([knex.select('*').from('users').whereNull('first_name')]); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .unionAll( + knex.raw('select * from users where first_name is null'), + knex.raw('select * from users where email is null') + ); +``` + +### intersect + +**.intersect([\*queries], [wrap])** + +Creates an intersect query, taking an array or a list of callbacks, builders, or raw statements to build the intersect statement, with optional boolean wrap. If the `wrap` parameter is `true`, the queries will be individually wrapped in parentheses. The intersect method is unsupported on MySQL. + +```js +knex + .select('*') + .from('users') + .whereNull('last_name') + .intersect(function () { + this.select('*').from('users').whereNull('first_name'); + }); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .intersect([knex.select('*').from('users').whereNull('first_name')]); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .intersect( + knex.raw('select * from users where first_name is null'), + knex.raw('select * from users where email is null') + ); +``` + +### except + +**.except([\*queries], [wrap])** + +Creates an except query, taking an array or a list of callbacks, builders, or raw statements to build the except statement, with optional boolean wrap. If the `wrap` parameter is `true`, the queries will be individually wrapped in parentheses. The except method is unsupported on MySQL. + +```js +knex + .select('*') + .from('users') + .whereNull('last_name') + .except(function () { + this.select('*').from('users').whereNull('first_name'); + }); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .except([knex.select('*').from('users').whereNull('first_name')]); + +knex + .select('*') + .from('users') + .whereNull('last_name') + .except( + knex.raw('select * from users where first_name is null'), + knex.raw('select * from users where email is null') + ); +``` + +### insert + +**.insert(data, [returning], [options])** + +Creates an insert query, taking either a hash of properties to be inserted into the row, or an array of inserts, to be executed as a single insert command. If returning array is passed e.g. \['id', 'title'\], it resolves the promise / fulfills the callback with an array of all the added rows with specified columns. It's a shortcut for [returning method](#returning) + +```js +// Returns [1] in "mysql", "sqlite", "oracle"; +// [] in "postgresql" +// unless the 'returning' parameter is set. +knex('books').insert({ title: 'Slaughterhouse Five' }); + +// Normalizes for empty keys on multi-row insert: +knex('coords').insert([{ x: 20 }, { y: 30 }, { x: 10, y: 20 }]); + +// Returns [2] in "mysql", "sqlite"; [2, 3] in "postgresql" +knex + .insert([{ title: 'Great Gatsby' }, { title: 'Fahrenheit 451' }], ['id']) + .into('books'); +``` + +For MSSQL, triggers on tables can interrupt returning a valid value from the standard insert statements. You can add the `includeTriggerModifications` option to get around this issue. This modifies the SQL so the proper values can be returned. This only modifies the statement if you are using MSSQL, a returning value is specified, and the `includeTriggerModifications` option is set. + +```js +// Adding the option includeTriggerModifications +// allows you to run statements on tables +// that contain triggers. Only affects MSSQL. +knex('books').insert({ title: 'Alice in Wonderland' }, ['id'], { + includeTriggerModifications: true, +}); +``` + +If one prefers that undefined keys are replaced with `NULL` instead of `DEFAULT` one may give `useNullAsDefault` configuration parameter in knex config. + +```js +const knex = require('knex')({ + client: 'mysql', + connection: { + host: '127.0.0.1', + port: 3306, + user: 'your_database_user', + password: 'your_database_password', + database: 'myapp_test', + }, + useNullAsDefault: true, +}); + +knex('coords').insert([{ x: 20 }, { y: 30 }, { x: 10, y: 20 }]); +``` + +```sql +insert into `coords` (`x`, `y`) values (20, NULL), (NULL, 30), (10, 20)" +``` + +### onConflict + +**insert(..).onConflict(column)** +**insert(..).onConflict([column1, column2, ...])** +**insert(..).onConflict(knex.raw(...))** + +Implemented for the PostgreSQL, MySQL, and SQLite databases. A modifier for insert queries that specifies alternative behaviour in the case of a conflict. A conflict occurs when a table has a PRIMARY KEY or a UNIQUE index on a column (or a composite index on a set of columns) and a row being inserted has the same value as a row which already exists in the table in those column(s). The default behaviour in case of conflict is to raise an error and abort the query. Using this method you can change this behaviour to either silently ignore the error by using .onConflict().ignore() or to update the existing row with new data (perform an "UPSERT") by using .onConflict().merge(). + +::: info +For PostgreSQL and SQLite, the column(s) specified by this method must either be the table's PRIMARY KEY or have a UNIQUE index on them, or the query will fail to execute. When specifying multiple columns, they must be a composite PRIMARY KEY or have composite UNIQUE index. MySQL will ignore the specified columns and always use the table's PRIMARY KEY. For cross-platform support across PostgreSQL, MySQL, and SQLite you must both explicitly specify the columns in .onConflict() and those column(s) must be the table's PRIMARY KEY. + +For PostgreSQL and SQLite, you can use knex.raw(...) function in onConflict. It can be useful to specify condition when you have partial index : +::: + +```js +knex('tableName') + .insert({ + email: 'ignore@example.com', + name: 'John Doe', + active: true, + }) + // ignore only on email conflict and active is true. + .onConflict(knex.raw('(email) where active')) + .ignore(); +``` + +See documentation on .ignore() and .merge() methods for more details. + +#### ignore + +**insert(..).onConflict(..).ignore()** + +Implemented for the PostgreSQL, MySQL, and SQLite databases. Modifies an insert query, and causes it to be silently dropped without an error if a conflict occurs. Uses INSERT IGNORE in MySQL, and adds an ON CONFLICT (columns) DO NOTHING clause to the insert statement in PostgreSQL and SQLite. + +```js +knex('tableName') + .insert({ + email: 'ignore@example.com', + name: 'John Doe', + }) + .onConflict('email') + .ignore(); +``` + +#### merge + +**insert(..).onConflict(..).merge()** +**insert(..).onConflict(..).merge(updates)** + +Implemented for the PostgreSQL, MySQL, and SQLite databases. Modifies an insert query, to turn it into an 'upsert' operation. Uses ON DUPLICATE KEY UPDATE in MySQL, and adds an ON CONFLICT (columns) DO UPDATE clause to the insert statement in PostgreSQL and SQLite. By default, it merges all columns. + +```js +knex('tableName') + .insert({ + email: 'ignore@example.com', + name: 'John Doe', + }) + .onConflict('email') + .merge(); +``` + +This also works with batch inserts: + +```js +knex('tableName') + .insert([ + { email: 'john@example.com', name: 'John Doe' }, + { email: 'jane@example.com', name: 'Jane Doe' }, + { email: 'alex@example.com', name: 'Alex Doe' }, + ]) + .onConflict('email') + .merge(); +``` + +It is also possible to specify a subset of the columns to merge when a conflict occurs. For example, you may want to set a 'created_at' column when inserting but would prefer not to update it if the row already exists: + +```js +const timestamp = Date.now(); +knex('tableName') + .insert({ + email: 'ignore@example.com', + name: 'John Doe', + created_at: timestamp, + updated_at: timestamp, + }) + .onConflict('email') + .merge(['email', 'name', 'updated_at']); +``` + +It is also possible to specify data to update separately from the data to insert. This is useful if you want to update with different data to the insert. For example, you may want to change a value if the row already exists: + +```js +const timestamp = Date.now(); +knex('tableName') + .insert({ + email: 'ignore@example.com', + name: 'John Doe', + created_at: timestamp, + updated_at: timestamp, + }) + .onConflict('email') + .merge({ + name: 'John Doe The Second', + }); +``` + +**For PostgreSQL/SQLite databases only**, it is also possible to add [a WHERE clause](#where) to conditionally update only the matching rows: + +```js +const timestamp = Date.now(); +knex('tableName') + .insert({ + email: 'ignore@example.com', + name: 'John Doe', + created_at: timestamp, + updated_at: timestamp, + }) + .onConflict('email') + .merge({ + name: 'John Doe', + updated_at: timestamp, + }) + .where('updated_at', '<', timestamp); +``` + +### upsert + +**.upsert(data, [returning], [options])** + +Implemented for the CockroachDB. Creates an upsert query, taking either a hash of properties to be inserted into the row, or an array of upserts, to be executed as a single upsert command. If returning array is passed e.g. \['id', 'title'\], it resolves the promise / fulfills the callback with an array of all the added rows with specified columns. It's a shortcut for [returning method](#returning) + +```js +// insert new row with unique index on title column +knex('books').upsert({ title: 'Great Gatsby' }); + +// update row by unique title 'Great Gatsby' +// and insert row with title 'Fahrenheit 451' +knex('books').upsert( + [{ title: 'Great Gatsby' }, { title: 'Fahrenheit 451' }], + ['id'] +); + +// Normalizes for empty keys on multi-row upsert, +// result sql: +// ("x", "y") values (20, default), (default, 30), (10, 20): +knex('coords').upsert([{ x: 20 }, { y: 30 }, { x: 10, y: 20 }]); +``` + +### update + +**.update(data, [returning], [options])** +**.update(key, value, [returning], [options])** + +Creates an update query, taking a hash of properties or a key/value pair to be updated based on the other query constraints. If returning array is passed e.g. \['id', 'title'\], it resolves the promise / fulfills the callback with an array of all the updated rows with specified columns. It's a shortcut for [returning method](#returning) + +```js +knex('books').where('published_date', '<', 2000).update({ + status: 'archived', + thisKeyIsSkipped: undefined, +}); + +// Returns [1] in "mysql", "sqlite", "oracle"; +// [] in "postgresql" +// unless the 'returning' parameter is set. +knex('books').update('title', 'Slaughterhouse Five'); + +/** Returns + * [{ + * id: 42, + * title: "The Hitchhiker's Guide to the Galaxy" + * }] **/ +knex('books').where({ id: 42 }).update( + { + title: "The Hitchhiker's Guide to the Galaxy", + }, + ['id', 'title'] +); +``` + +For MSSQL, triggers on tables can interrupt returning a valid value from the standard update statements. You can add the `includeTriggerModifications` option to get around this issue. This modifies the SQL so the proper values can be returned. This only modifies the statement if you are using MSSQL, a returning value is specified, and the `includeTriggerModifications` option is set. + +```js +// Adding the option includeTriggerModifications allows you +// to run statements on tables that contain triggers. +// Only affects MSSQL. +knex('books').update({ title: 'Alice in Wonderland' }, ['id', 'title'], { + includeTriggerModifications: true, +}); +``` + +### updateFrom + +**.updateFrom(tableName)** + +Can be used to define in PostgreSQL an update statement with explicit 'from' syntax which can be referenced in 'where' conditions. + +```js +knex('accounts') + .update({ enabled: false }) + .updateFrom('clients') + .where('accounts.id', '=', 'clients.id') + .where('clients.active', '=', false); +``` + +### del / delete + +**.del([returning], [options])** + +Aliased to del as delete is a reserved word in JavaScript, this method deletes one or more rows, based on other conditions specified in the query. Resolves the promise / fulfills the callback with the number of affected rows for the query. + +```js +knex('accounts').where('activated', false).del(); +``` + +For MSSQL, triggers on tables can interrupt returning a valid value from the standard delete statements. You can add the `includeTriggerModifications` option to get around this issue. This modifies the SQL so the proper values can be returned. This only modifies the statement if you are using MSSQL, a returning value is specified, and the `includeTriggerModifications` option is set. + +```js +// Adding the option includeTriggerModifications allows you +// to run statements on tables that contain triggers. +// Only affects MSSQL. +knex('books') + .where('title', 'Alice in Wonderland') + .del(['id', 'title'], { includeTriggerModifications: true }); +``` + +For PostgreSQL, Delete statement with joins is both supported with classic 'join' syntax and 'using' syntax. + +```js +knex('accounts') + .where('activated', false) + .join('accounts', 'accounts.id', 'users.account_id') + .del(); +``` + +### using + +**.using(tableName|tableNames)** + +Can be used to define in PostgreSQL a delete statement with joins with explicit 'using' syntax. Classic join syntax can be used too. + +```js +knex('accounts') + .where('activated', false) + .using('accounts') + .whereRaw('accounts.id = users.account_id') + .del(); +``` + +### returning + +**.returning(column, [options])** +**.returning([column1, column2, ...], [options])** + +Utilized by PostgreSQL, MSSQL, SQLite, and Oracle databases, the returning method specifies which column should be returned by the insert, update and delete methods. Passed column parameter may be a string or an array of strings. The SQL result be reported as an array of objects, each containing a single property for each of the specified columns. The returning method is not supported on Amazon Redshift. + +```js +// Returns [ { id: 1 } ] +knex('books').returning('id').insert({ title: 'Slaughterhouse Five' }); + +// Returns [{ id: 2 } ] in "mysql", "sqlite"; +// [ { id: 2 }, { id: 3 } ] in "postgresql" +knex('books') + .returning('id') + .insert([{ title: 'Great Gatsby' }, { title: 'Fahrenheit 451' }]); + +// Returns [ { id: 1, title: 'Slaughterhouse Five' } ] +knex('books') + .returning(['id', 'title']) + .insert({ title: 'Slaughterhouse Five' }); +``` + +For MSSQL, triggers on tables can interrupt returning a valid value from the standard DML statements. You can add the `includeTriggerModifications` option to get around this issue. This modifies the SQL so the proper values can be returned. This only modifies the statement if you are using MSSQL, a returning value is specified, and the `includeTriggerModifications` option is set. + +```js +// Adding the option includeTriggerModifications allows you +// to run statements on tables that contain triggers. +// Only affects MSSQL. +knex('books') + .returning(['id', 'title'], { includeTriggerModifications: true }) + .insert({ title: 'Slaughterhouse Five' }); +``` + +### transacting + +**.transacting(transactionObj)** + +Used by knex.transaction, the transacting method may be chained to any query and passed the object you wish to join the query as part of the transaction for. + +```js +const Promise = require('bluebird'); +knex + .transaction(function (trx) { + knex('books') + .transacting(trx) + .insert({ name: 'Old Books' }) + .then(function (resp) { + const id = resp[0]; + return someExternalMethod(id, trx); + }) + .then(trx.commit) + .catch(trx.rollback); + }) + .then(function (resp) { + console.log('Transaction complete.'); + }) + .catch(function (err) { + console.error(err); + }); +``` + +#### forUpdate + +**.transacting(t).forUpdate()** + +Dynamically added after a transaction is specified, the forUpdate adds a FOR UPDATE in PostgreSQL and MySQL during a select statement. Not supported on Amazon Redshift due to lack of table locks. + +```js +knex('tableName').transacting(trx).forUpdate().select('*'); +``` + +#### forShare + +**.transacting(t).forShare()** + +Dynamically added after a transaction is specified, the forShare adds a FOR SHARE in PostgreSQL and a LOCK IN SHARE MODE for MySQL during a select statement. Not supported on Amazon Redshift due to lack of table locks. + +```js +knex('tableName').transacting(trx).forShare().select('*'); +``` + +#### forNoKeyUpdate + +**.transacting(t).forNoKeyUpdate()** + +Dynamically added after a transaction is specified, the forNoKeyUpdate adds a FOR NO KEY UPDATE in PostgreSQL. + +```js +knex('tableName').transacting(trx).forNoKeyUpdate().select('*'); +``` + +#### forKeyShare + +**.transacting(t).forKeyShare()** + +Dynamically added after a transaction is specified, the forKeyShare adds a FOR KEY SHARE in PostgreSQL. + +```js +knex('tableName').transacting(trx).forKeyShare().select('*'); +``` + +### skipLocked + +**.skipLocked()** + +MySQL 8.0+, MariaDB-10.6+ and PostgreSQL 9.5+ only. This method can be used after a lock mode has been specified with either forUpdate or forShare, and will cause the query to skip any locked rows, returning an empty set if none are available. + +```js +knex('tableName').select('*').forUpdate().skipLocked(); +``` + +### noWait + +**.noWait()** + +MySQL 8.0+, MariaDB-10.3+ and PostgreSQL 9.5+ only. This method can be used after a lock mode has been specified with either forUpdate or forShare, and will cause the query to fail immediately if any selected rows are currently locked. + +```js +knex('tableName').select('*').forUpdate().noWait(); +``` + +### count + +**.count(column|columns|raw, [options])** + +Performs a count on the specified column or array of columns (note that some drivers do not support multiple columns). Also accepts raw expressions. The value returned from count (and other aggregation queries) is an array of objects like: `[{'COUNT(*)': 1}]`. The actual keys are dialect specific, so usually we would want to specify an alias (Refer examples below). Note that in Postgres, count returns a bigint type which will be a String and not a Number ([more info](https://github.com/brianc/node-pg-types#use)). + +```js +knex('users').count('active'); + +knex('users').count('active', { as: 'a' }); + +knex('users').count('active as a'); + +knex('users').count({ a: 'active' }); + +knex('users').count({ a: 'active', v: 'valid' }); + +knex('users').count('id', 'active'); + +knex('users').count({ count: ['id', 'active'] }); + +knex('users').count(knex.raw('??', ['active'])); +``` + +##### Usage with TypeScript + +The value of count will, by default, have type of `string | number`. This may be counter-intuitive but some connectors (eg. postgres) will automatically cast BigInt result to string when javascript's Number type is not large enough for the value. + +```ts +knex('users').count('age'); // Resolves to: Record + +knex('users').count({ count: '*' }); // Resolves to { count?: string | number | undefined; } +``` + +Working with `string | number` can be inconvenient if you are not working with large tables. Two alternatives are available: + +```ts +// Be explicit about what you want as a result: +knex('users').count>('age'); + +// Setup a one time declaration to make knex use number as result type for all +// count and countDistinct invocations (for any table) +declare module 'knex/types/result' { + interface Registry { + Count: number; + } +} +``` + +Use **countDistinct** to add a distinct expression inside the aggregate function. + +```ts +knex('users').countDistinct('active'); +``` + +### min + +**.min(column|columns|raw, [options])** + +Gets the minimum value for the specified column or array of columns (note that some drivers do not support multiple columns). Also accepts raw expressions. + +```js +knex('users').min('age'); + +knex('users').min('age', { as: 'a' }); + +knex('users').min('age as a'); + +knex('users').min({ a: 'age' }); + +knex('users').min({ a: 'age', b: 'experience' }); + +knex('users').min('age', 'logins'); + +knex('users').min({ min: ['age', 'logins'] }); + +knex('users').min(knex.raw('??', ['age'])); +``` + +### max + +**.max(column|columns|raw, [options])** + +Gets the maximum value for the specified column or array of columns (note that some drivers do not support multiple columns). Also accepts raw expressions. + +```js +knex('users').max('age'); + +knex('users').max('age', { as: 'a' }); + +knex('users').max('age as a'); + +knex('users').max({ a: 'age' }); + +knex('users').max('age', 'logins'); + +knex('users').max({ max: ['age', 'logins'] }); + +knex('users').max({ max: 'age', exp: 'experience' }); + +knex('users').max(knex.raw('??', ['age'])); +``` + +### sum + +**.sum(column|columns|raw)** + +Retrieve the sum of the values of a given column or array of columns (note that some drivers do not support multiple columns). Also accepts raw expressions. + +```js +knex('users').sum('products'); + +knex('users').sum('products as p'); + +knex('users').sum({ p: 'products' }); + +knex('users').sum('products', 'orders'); + +knex('users').sum({ sum: ['products', 'orders'] }); + +knex('users').sum(knex.raw('??', ['products'])); +``` + +Use **sumDistinct** to add a distinct expression inside the aggregate function. + +```js +knex('users').sumDistinct('products'); +``` + +### avg + +**.avg(column|columns|raw)** + +Retrieve the average of the values of a given column or array of columns (note that some drivers do not support multiple columns). Also accepts raw expressions. + +```js +knex('users').avg('age'); + +knex('users').avg('age as a'); + +knex('users').avg({ a: 'age' }); + +knex('users').avg('age', 'logins'); + +knex('users').avg({ avg: ['age', 'logins'] }); + +knex('users').avg(knex.raw('??', ['age'])); +``` + +Use **avgDistinct** to add a distinct expression inside the aggregate function. + +```js +knex('users').avgDistinct('age'); +``` + +### increment + +**.increment(column, amount)** + +Increments a column value by the specified amount. Object syntax is supported for `column`. + +```js +knex('accounts').where('userid', '=', 1).increment('balance', 10); + +knex('accounts').where('id', '=', 1).increment({ + balance: 10, + times: 1, +}); +``` + +### decrement + +**.decrement(column, amount)** + +Decrements a column value by the specified amount. Object syntax is supported for `column`. + +```js +knex('accounts').where('userid', '=', 1).decrement('balance', 5); + +knex('accounts').where('id', '=', 1).decrement({ + balance: 50, +}); +``` + +### truncate + +**.truncate()** + +Truncates the current table. + +```js +knex('accounts').truncate(); +``` + +### pluck + +**.pluck(id)** + +This will pluck the specified column from each row in your results, yielding a promise which resolves to the array of values selected. + +```js +knex + .table('users') + .pluck('id') + .then(function (ids) { + console.log(ids); + }); +``` + +### first + +**.first([columns])** + +Similar to select, but only retrieves & resolves with the first record from the query. + +```js +knex + .table('users') + .first('id', 'name') + .then(function (row) { + console.log(row); + }); +``` + +### hintComment + +**.hintComment(hint|hints)** + +Add hints to the query using comment-like syntax `/*+ ... */`. MySQL and Oracle use this syntax for optimizer hints. Also various DB proxies and routers use this syntax to pass hints to alter their behavior. In other dialects the hints are ignored as simple comments. + +```js +knex('accounts').where('userid', '=', 1).hintComment('NO_ICP(accounts)'); +``` + +### comment + +**.comment(comment)** + +Prepend comment to the sql query using the syntax `/* ... */`. Some characters are forbidden such as `/*`, `*/` and `?`. + +```js +knex('users').where('id', '=', 1).comment('Get user by id'); +``` + +### clone + +**.clone()** + +Clones the current query chain, useful for re-using partial query snippets in other queries without mutating the original. + +### denseRank + +**.denseRank(alias, ~mixed~)** + +Add a dense_rank() call to your query. For all the following queries, alias can be set to a falsy value if not needed. + +String Syntax — .denseRank(alias, orderByClause, \[partitionByClause\]) : + +```js +knex('users').select('*').denseRank('alias_name', 'email', 'firstName'); +``` + +It also accepts arrays of strings as argument : + +```js +knex('users') + .select('*') + .denseRank('alias_name', ['email', 'address'], ['firstName', 'lastName']); +``` + +Raw Syntax — .denseRank(alias, rawQuery) : + +```js +knex('users') + .select('*') + .denseRank('alias_name', knex.raw('order by ??', ['email'])); +``` + +Function Syntax — .denseRank(alias, function) : + +Use orderBy() and partitionBy() (both chainable) to build your query : + +```js +knex('users') + .select('*') + .denseRank('alias_name', function () { + this.orderBy('email').partitionBy('firstName'); + }); +``` + +### rank + +**.rank(alias, ~mixed~)** + +Add a rank() call to your query. For all the following queries, alias can be set to a falsy value if not needed. + +String Syntax — .rank(alias, orderByClause, \[partitionByClause\]) : + +```js +knex('users').select('*').rank('alias_name', 'email', 'firstName'); +``` + +It also accepts arrays of strings as argument : + +```js +knex('users') + .select('*') + .rank('alias_name', ['email', 'address'], ['firstName', 'lastName']); +``` + +Raw Syntax — .rank(alias, rawQuery) : + +```js +knex('users') + .select('*') + .rank('alias_name', knex.raw('order by ??', ['email'])); +``` + +Function Syntax — .rank(alias, function) : + +Use orderBy() and partitionBy() (both chainable) to build your query : + +```js +knex('users') + .select('*') + .rank('alias_name', function () { + this.orderBy('email').partitionBy('firstName'); + }); +``` + +### rowNumber + +**.rowNumber(alias, ~mixed~)** + +Add a row_number() call to your query. For all the following queries, alias can be set to a falsy value if not needed. + +String Syntax — .rowNumber(alias, orderByClause, \[partitionByClause\]) : + +```js +knex('users').select('*').rowNumber('alias_name', 'email', 'firstName'); +``` + +It also accepts arrays of strings as argument : + +```js +knex('users') + .select('*') + .rowNumber('alias_name', ['email', 'address'], ['firstName', 'lastName']); +``` + +Raw Syntax — .rowNumber(alias, rawQuery) : + +```js +knex('users') + .select('*') + .rowNumber('alias_name', knex.raw('order by ??', ['email'])); +``` + +Function Syntax — .rowNumber(alias, function) : + +Use orderBy() and partitionBy() (both chainable) to build your query : + +```js +knex('users') + .select('*') + .rowNumber('alias_name', function () { + this.orderBy('email').partitionBy('firstName'); + }); +``` + +### partitionBy + +**.partitionBy(column, direction)** + +Partitions rowNumber, denseRank, rank after a specific column or columns. If direction is not supplied it will default to ascending order. + +No direction sort : + +```js +knex('users') + .select('*') + .rowNumber('alias_name', function () { + this.partitionBy('firstName'); + }); +``` + +With direction sort : + +```js +knex('users') + .select('*') + .rowNumber('alias_name', function () { + this.partitionBy('firstName', 'desc'); + }); +``` + +With multiobject : + +```js +knex('users') + .select('*') + .rowNumber('alias_name', function () { + this.partitionBy([ + { column: 'firstName', order: 'asc' }, + { column: 'lastName', order: 'desc' }, + ]); + }); +``` + +### modify + +**.modify(fn, \*arguments)** + +Allows encapsulating and re-using query snippets and common behaviors as functions. The callback function should receive the query builder as its first argument, followed by the rest of the (optional) parameters passed to modify. + +```js +const withUserName = function (queryBuilder, foreignKey) { + queryBuilder + .leftJoin('users', foreignKey, 'users.id') + .select('users.user_name'); +}; +knex + .table('articles') + .select('title', 'body') + .modify(withUserName, 'articles_user.id') + .then(function (article) { + console.log(article.user_name); + }); +``` + +### columnInfo + +**.columnInfo([columnName])** + +Returns an object with the column info about the current table, or an individual column if one is passed, returning an object with the following keys: + +- **defaultValue**: the default value for the column +- **type**: the column type +- **maxLength**: the max length set for the column +- **nullable**: whether the column may be null + +```js +knex('users') + .columnInfo() + .then(function (info) { + /*...*/ + }); +``` + +### debug + +**.debug([enabled])** + +Overrides the global debug setting for the current query chain. If enabled is omitted, query debugging will be turned on. + +### connection + +**.connection(dbConnection)** + +The method sets the db connection to use for the query without using the connection pool. You should pass to it the same object that acquireConnection() for the corresponding driver returns + +```ts +const Pool = require('pg-pool'); +const pool = new Pool({ + /* ... */ +}); +const connection = await pool.connect(); + +try { + return await knex.connection(connection); // knex here is a query builder with query already built +} catch (error) { + // Process error +} finally { + connection.release(); +} +``` + +### options + +**.options()** + +Allows for mixing in additional options as defined by database client specific libraries: + +```js +knex('accounts as a1') + .leftJoin('accounts as a2', function () { + this.on('a1.email', '<>', 'a2.email'); + }) + .select(['a1.email', 'a2.email']) + .where(knex.raw('a1.id = 1')) + .options({ nestTables: true, rowMode: 'array' }) + .limit(2) + .then({ + /*...*/ + }); +``` + +### queryContext + +**.queryContext(context)** + +Allows for configuring a context to be passed to the [wrapIdentifier](/guide/#wrapidentifier) and [postProcessResponse](/guide/#postprocessresponse) hooks: + +```js +knex('accounts as a1') + .queryContext({ foo: 'bar' }) + .select(['a1.email', 'a2.email']); +``` + +The context can be any kind of value and will be passed to the hooks without modification. However, note that **objects will be shallow-cloned** when a query builder instance is [cloned](#clone), which means that they will contain all the properties of the original object but will not be the same object reference. This allows modifying the context for the cloned query builder instance. + +Calling `queryContext` with no arguments will return any context configured for the query builder instance. + +##### Extending Query Builder + +**Important:** this feature is experimental and its API may change in the future. + +It allows to add custom function to the Query Builder. + +Example: + +```ts +const { knex } = require('knex'); +knex.QueryBuilder.extend('customSelect', function (value) { + return this.select(this.client.raw(`${value} as value`)); +}); + +const meaningOfLife = await knex('accounts').customSelect(42); +``` + +If using TypeScript, you can extend the QueryBuilder interface with your custom method. + +1. Create a `knex.d.ts` file inside a `@types` folder (or any other folder). + +```ts +// knex.d.ts + +import { Knex as KnexOriginal } from 'knex'; + +declare module 'knex' { + namespace Knex { + interface QueryInterface { + customSelect( + value: number + ): KnexOriginal.QueryBuilder; + } + } +} +``` + +2. Add the new `@types` folder to `typeRoots` in your `tsconfig.json`. + +```json +// tsconfig.json +{ + "compilerOptions": { + "typeRoots": ["node_modules/@types", "@types"] + } +} +``` + +## Where Clauses + +Several methods exist to assist in dynamic where clauses. In many places functions may be used in place of values, constructing subqueries. In most places existing knex queries may be used to compose sub-queries, etc. Take a look at a few of the examples for each method for instruction on use: + +**Important:** Supplying knex with an `undefined` value to any of the `where` functions will cause knex to throw an error during sql compilation. This is both for yours and our sake. Knex cannot know what to do with undefined values in a where clause, and generally it would be a programmatic error to supply one to begin with. The error will throw a message containing the type of query and the compiled query-string. Example: + +```js +knex('accounts').where('login', undefined).select().toSQL(); +``` + +### where + +**.where(~mixed~)** +**.orWhere** + +Object Syntax: + +```js +knex('users') + .where({ + first_name: 'Test', + last_name: 'User', + }) + .select('id'); +``` + +Key, Value: + +```js +knex('users').where('id', 1); +``` + +Functions: + +```js +knex('users') + .where((builder) => + builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]) + ) + .andWhere(function () { + this.where('id', '>', 10); + }); +``` + +Grouped Chain: + +```js +knex('users') + .where(function () { + this.where('id', 1).orWhere('id', '>', 10); + }) + .orWhere({ name: 'Tester' }); +``` + +Operator: + +```js +knex('users').where('columnName', 'like', '%rowlikeme%'); +``` + +The above query demonstrates the common use case of returning all users for which a specific pattern appears within a designated column. + +```js +knex('users').where('votes', '>', 100); + +const subquery = knex('users') + .where('votes', '>', 100) + .andWhere('status', 'active') + .orWhere('name', 'John') + .select('id'); + +knex('accounts').where('id', 'in', subquery); +``` + +.orWhere with an object automatically wraps the statement and creates an `or (and - and - and)` clause + +```js +knex('users').where('id', 1).orWhere({ votes: 100, user: 'knex' }); +``` + +### whereNot + +**.whereNot(~mixed~)** +**.orWhereNot** + +Object Syntax: + +```js +knex('users') + .whereNot({ + first_name: 'Test', + last_name: 'User', + }) + .select('id'); +``` + +Key, Value: + +```js +knex('users').whereNot('id', 1); +``` + +Grouped Chain: + +```js +knex('users') + .whereNot(function () { + this.where('id', 1).orWhereNot('id', '>', 10); + }) + .orWhereNot({ name: 'Tester' }); +``` + +Operator: + +```js +knex('users').whereNot('votes', '>', 100); +``` + +::: warning +WhereNot is not suitable for "in" and "between" type subqueries. You should use "not in" and "not between" instead. +::: + +```js +const subquery = knex('users') + .whereNot('votes', '>', 100) + .andWhere('status', 'active') + .orWhere('name', 'John') + .select('id'); + +knex('accounts').where('id', 'not in', subquery); +``` + +### whereIn + +**.whereIn(column|columns, array|callback|builder)** +**.orWhereIn** + +Shorthand for .where('id', 'in', obj), the .whereIn and .orWhereIn methods add a "where in" clause to the query. Note that passing empty array as the value results in a query that never returns any rows (`WHERE 1 = 0`) + +```js +knex + .select('name') + .from('users') + .whereIn('id', [1, 2, 3]) + .orWhereIn('id', [4, 5, 6]); + +knex + .select('name') + .from('users') + .whereIn('account_id', function () { + this.select('id').from('accounts'); + }); + +const subquery = knex.select('id').from('accounts'); + +knex.select('name').from('users').whereIn('account_id', subquery); + +knex + .select('name') + .from('users') + .whereIn( + ['account_id', 'email'], + [ + [3, 'test3@example.com'], + [4, 'test4@example.com'], + ] + ); + +knex + .select('name') + .from('users') + .whereIn( + ['account_id', 'email'], + knex.select('id', 'email').from('accounts') + ); +``` + +### whereNotIn + +**.whereNotIn(column, array|callback|builder)** +**.orWhereNotIn** + +```js +knex('users').whereNotIn('id', [1, 2, 3]); + +knex('users').where('name', 'like', '%Test%').orWhereNotIn('id', [1, 2, 3]); +``` + +### whereNull + +**.whereNull(column)** +**.orWhereNull** + +```js +knex('users').whereNull('updated_at'); +``` + +### whereNotNull + +**.whereNotNull(column)** +**.orWhereNotNull** + +```js +knex('users').whereNotNull('created_at'); +``` + +### whereExists + +**.whereExists(builder | callback)** +**.orWhereExists** + +```js +knex('users').whereExists(function () { + this.select('*').from('accounts').whereRaw('users.account_id = accounts.id'); +}); + +knex('users').whereExists( + knex.select('*').from('accounts').whereRaw('users.account_id = accounts.id') +); +``` + +### whereNotExists + +**.whereNotExists(builder | callback)** +**.orWhereNotExists** + +```js +knex('users').whereNotExists(function () { + this.select('*').from('accounts').whereRaw('users.account_id = accounts.id'); +}); + +knex('users').whereNotExists( + knex.select('*').from('accounts').whereRaw('users.account_id = accounts.id') +); +``` + +### whereBetween + +**.whereBetween(column, range)** +**.orWhereBetween** + +```js +knex('users').whereBetween('votes', [1, 100]); +``` + +### whereNotBetween + +**.whereNotBetween(column, range)** +**.orWhereNotBetween** + +```js +knex('users').whereNotBetween('votes', [1, 100]); +``` + +### whereRaw + +**.whereRaw(query, [bindings])** + +Convenience helper for .where(knex.raw(query)). + +```js +knex('users').whereRaw('id = ?', [1]); +``` + +### whereLike + +**.whereLike(column, string|builder|raw)** +**.orWhereLike** + +Adds a where clause with case-sensitive substring comparison on a given column with a given value. + +```js +knex('users').whereLike('email', '%mail%'); + +knex('users') + .whereLike('email', '%mail%') + .andWhereLike('email', '%.com') + .orWhereLike('email', '%name%'); +``` + +### whereILike + +**.whereILike(column, string|builder|raw)** +**.orWhereILike** + +Adds a where clause with case-insensitive substring comparison on a given column with a given value. + +```js +knex('users').whereILike('email', '%mail%'); + +knex('users') + .whereILike('email', '%MAIL%') + .andWhereILike('email', '%.COM') + .orWhereILike('email', '%NAME%'); +``` + +### whereJsonObject + +**.whereJsonObject(column, string|json|builder|raw)** + +Adds a where clause with json object comparison on given json column. + +```js +knex('users').whereJsonObject('json_col', { name: 'user_name' }); +``` + +### whereJsonPath + +**.whereJsonPath(column, jsonPath, operator, value)** + +Adds a where clause with comparison of a value returned by a JsonPath given an operator and a value. + +```js +knex('users').whereJsonPath('json_col', '$.age', '>', 18); + +knex('users').whereJsonPath('json_col', '$.name', '=', 'username'); +``` + +### whereJsonSupersetOf + +**.whereJsonSupersetOf(column, string|json|builder|raw)** + +Adds a where clause where the comparison is true if a json given by the column include a given value. Only on MySQL, PostgreSQL and CockroachDB. + +```js +knex('users').whereJsonSupersetOf('hobbies', { sport: 'foot' }); +``` + +### whereJsonSubsetOf + +**.whereJsonSubsetOf(column, string|json|builder|raw)** + +Adds a where clause where the comparison is true if a json given by the column is included in a given value. Only on MySQL, PostgreSQL and CockroachDB. + +```js +// given a hobby column with { "sport" : "tennis" }, +// the where clause is true +knex('users').whereJsonSubsetOf('hobby', { sport: 'tennis', book: 'fantasy' }); +``` + +## Join Methods + +Several methods are provided which assist in building joins. + +### join + +**.join(table, first, [operator], second)** + +The join builder can be used to specify joins between tables, with the first argument being the joining table, the next three arguments being the first join column, the join operator and the second join column, respectively. + +```js +knex('users') + .join('contacts', 'users.id', '=', 'contacts.user_id') + .select('users.id', 'contacts.phone'); + +knex('users') + .join('contacts', 'users.id', 'contacts.user_id') + .select('users.id', 'contacts.phone'); +``` + +For grouped joins, specify a function as the second argument for the join query, and use `on` with `orOn` or `andOn` to create joins that are grouped with parentheses. + +```js +knex + .select('*') + .from('users') + .join('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); + }); +``` + +For nested join statements, specify a function as first argument of `on`, `orOn` or `andOn` + +```js +knex + .select('*') + .from('users') + .join('accounts', function () { + this.on(function () { + this.on('accounts.id', '=', 'users.account_id'); + this.orOn('accounts.owner_id', '=', 'users.id'); + }); + }); +``` + +It is also possible to use an object to represent the join syntax. + +```js +knex + .select('*') + .from('users') + .join('accounts', { 'accounts.id': 'users.account_id' }); +``` + +If you need to use a literal value (string, number, or boolean) in a join instead of a column, use `knex.raw`. + +```js +knex + .select('*') + .from('users') + .join('accounts', 'accounts.type', knex.raw('?', ['admin'])); +``` + +### innerJoin + +**.innerJoin(table, ~mixed~)** + +```js +knex.from('users').innerJoin('accounts', 'users.id', 'accounts.user_id'); + +knex.table('users').innerJoin('accounts', 'users.id', '=', 'accounts.user_id'); + +knex('users').innerJoin('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); +}); +``` + +### leftJoin + +**.leftJoin(table, ~mixed~)** + +```js +knex + .select('*') + .from('users') + .leftJoin('accounts', 'users.id', 'accounts.user_id'); + +knex + .select('*') + .from('users') + .leftJoin('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); + }); +``` + +### leftOuterJoin + +**.leftOuterJoin(table, ~mixed~)** + +```js +knex + .select('*') + .from('users') + .leftOuterJoin('accounts', 'users.id', 'accounts.user_id'); + +knex + .select('*') + .from('users') + .leftOuterJoin('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); + }); +``` + +### rightJoin + +**.rightJoin(table, ~mixed~)** + +```js +knex + .select('*') + .from('users') + .rightJoin('accounts', 'users.id', 'accounts.user_id'); + +knex + .select('*') + .from('users') + .rightJoin('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); + }); +``` + +### rightOuterJoin + +**.rightOuterJoin(table, ~mixed~)** + +```js +knex + .select('*') + .from('users') + .rightOuterJoin('accounts', 'users.id', 'accounts.user_id'); + +knex + .select('*') + .from('users') + .rightOuterJoin('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); + }); +``` + +### fullOuterJoin + +**.fullOuterJoin(table, ~mixed~)** + +```js +knex + .select('*') + .from('users') + .fullOuterJoin('accounts', 'users.id', 'accounts.user_id'); + +knex + .select('*') + .from('users') + .fullOuterJoin('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); + }); +``` + +### crossJoin + +**.crossJoin(table, ~mixed~)** + +Cross join conditions are only supported in MySQL and SQLite3. For join conditions rather use innerJoin. + +```js +knex.select('*').from('users').crossJoin('accounts'); + +knex + .select('*') + .from('users') + .crossJoin('accounts', 'users.id', 'accounts.user_id'); + +knex + .select('*') + .from('users') + .crossJoin('accounts', function () { + this.on('accounts.id', '=', 'users.account_id').orOn( + 'accounts.owner_id', + '=', + 'users.id' + ); + }); +``` + +### joinRaw + +**.joinRaw(sql, [bindings])** + +```js +knex + .select('*') + .from('accounts') + .joinRaw('natural full join table1') + .where('id', 1); + +knex + .select('*') + .from('accounts') + .join(knex.raw('natural full join table1')) + .where('id', 1); +``` + +## OnClauses + +### onIn + +**.onIn(column, values)** + +Adds a onIn clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onIn( + 'contacts.id', + [7, 15, 23, 41] + ); + }); +``` + +### onNotIn + +**.onNotIn(column, values)** + +Adds a onNotIn clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onNotIn( + 'contacts.id', + [7, 15, 23, 41] + ); + }); +``` + +### onNull + +**.onNull(column)** + +Adds a onNull clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onNull('contacts.email'); + }); +``` + +### onNotNull + +**.onNotNull(column)** + +Adds a onNotNull clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onNotNull('contacts.email'); + }); +``` + +### onExists + +**.onExists(builder | callback)** + +Adds a onExists clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onExists(function () { + this.select('*') + .from('accounts') + .whereRaw('users.account_id = accounts.id'); + }); + }); +``` + +### onNotExists + +**.onNotExists(builder | callback)** + +Adds a onNotExists clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onNotExists(function () { + this.select('*') + .from('accounts') + .whereRaw('users.account_id = accounts.id'); + }); + }); +``` + +### onBetween + +**.onBetween(column, range)** + +Adds a onBetween clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onBetween('contacts.id', [5, 30]); + }); +``` + +### onNotBetween + +**.onNotBetween(column, range)** + +Adds a onNotBetween clause to the query. + +```js +knex + .select('*') + .from('users') + .join('contacts', function () { + this.on('users.id', '=', 'contacts.id').onNotBetween( + 'contacts.id', + [5, 30] + ); + }); +``` + +### onJsonPathEquals + +**.onJsonPathEquals(column, range)** + +Adds a onJsonPathEquals clause to the query. The clause performs a join on value returned by two json paths on two json columns. + +```js +knex('cities') + .select('cities.name as cityName', 'country.name as countryName') + .join('country', function () { + this.onJsonPathEquals( + // json column in cities + 'country_name', + // json path to country name in 'country_name' column + '$.country.name', + // json column in country + 'description', + // json field in 'description' column + '$.name' + ); + }); +``` + +## ClearClauses + +### clear + +**.clear(statement)** + +Clears the specified operator from the query. Available operators: 'select' alias 'columns', 'with', 'select', 'columns', 'where', 'union', 'join', 'group', 'order', 'having', 'limit', 'offset', 'counter', 'counters'. Counter(s) alias for method .clearCounter() + +```js +knex + .select('email', 'name') + .from('users') + .where('id', '<', 10) + .clear('select') + .clear('where'); +``` + +### clearSelect + +**.clearSelect()** + +Deprecated, use clear('select'). Clears all select clauses from the query, excluding subqueries. + +```js +knex.select('email', 'name').from('users').clearSelect(); +``` + +### clearWhere + +**.clearWhere()** + +Deprecated, use clear('where'). Clears all where clauses from the query, excluding subqueries. + +```js +knex.select('email', 'name').from('users').where('id', 1).clearWhere(); +``` + +### clearGroup + +**.clearGroup()** + +Deprecated, use clear('group'). Clears all group clauses from the query, excluding subqueries. + +```js +knex.select().from('users').groupBy('id').clearGroup(); +``` + +### clearOrder + +**.clearOrder()** + +Deprecated, use clear('order'). Clears all order clauses from the query, excluding subqueries. + +```js +knex.select().from('users').orderBy('name', 'desc').clearOrder(); +``` + +### clearHaving + +**.clearHaving()** + +Deprecated, use clear('having'). Clears all having clauses from the query, excluding subqueries. + +```js +knex.select().from('users').having('id', '>', 5).clearHaving(); +``` + +### clearCounters + +**.clearCounters()** + +Clears all increments/decrements clauses from the query. + +```js +knex('accounts') + .where('id', '=', 1) + .update({ email: 'foo@bar.com' }) + .decrement({ + balance: 50, + }) + .clearCounters(); +``` + +### distinct + +**.distinct([\*columns])** + +Sets a distinct clause on the query. If the parameter is falsy or empty array, method falls back to '\*'. + +```js +// select distinct 'first_name' from customers +knex('customers').distinct('first_name', 'last_name'); + +// select which eliminates duplicate rows +knex('customers').distinct(); +``` + +### distinctOn + +**.distinctOn([\*columns])** + +PostgreSQL only. Adds a distinctOn clause to the query. + +```js +knex('users').distinctOn('age'); +``` + +### groupBy + +**.groupBy(\*names)** + +Adds a group by clause to the query. + +```js +knex('users').groupBy('count'); +``` + +### groupByRaw + +**.groupByRaw(sql)** + +Adds a raw group by clause to the query. + +```js +knex + .select('year', knex.raw('SUM(profit)')) + .from('sales') + .groupByRaw('year WITH ROLLUP'); +``` + +### orderBy + +**.orderBy(column|columns, [direction], [nulls])** + +Adds an order by clause to the query. column can be string, or list mixed with string and object. nulls specify where the nulls values are put (can be 'first' or 'last'). + +Single Column: + +```js +knex('users').orderBy('email'); + +knex('users').orderBy('name', 'desc'); + +knex('users').orderBy('name', 'desc', 'first'); +``` + +Multiple Columns: + +```js +knex('users').orderBy(['email', { column: 'age', order: 'desc' }]); + +knex('users').orderBy([{ column: 'email' }, { column: 'age', order: 'desc' }]); + +knex('users').orderBy([ + { column: 'email' }, + { column: 'age', order: 'desc', nulls: 'last' }, +]); +``` + +### orderByRaw + +**.orderByRaw(sql)** + +Adds an order by raw clause to the query. + +```js +knex.select('*').from('table').orderByRaw('col DESC NULLS LAST'); +``` + +## Having Clauses + +### having + +**.having(column, operator, value)** + +Adds a having clause to the query. + +```js +knex('users') + .groupBy('count') + .orderBy('name', 'desc') + .having('count', '>', 100); +``` + +### havingIn + +**.havingIn(column, values)** + +Adds a havingIn clause to the query. + +```js +knex.select('*').from('users').havingIn('id', [5, 3, 10, 17]); +``` + +### havingNotIn + +**.havingNotIn(column, values)** + +Adds a havingNotIn clause to the query. + +```js +knex.select('*').from('users').havingNotIn('id', [5, 3, 10, 17]); +``` + +### havingNull + +**.havingNull(column)** + +Adds a havingNull clause to the query. + +```js +knex.select('*').from('users').havingNull('email'); +``` + +### havingNotNull + +**.havingNotNull(column)** + +Adds a havingNotNull clause to the query. + +```js +knex.select('*').from('users').havingNotNull('email'); +``` + +### havingExists + +**.havingExists(builder | callback)** + +Adds a havingExists clause to the query. + +```js +knex + .select('*') + .from('users') + .havingExists(function () { + this.select('*') + .from('accounts') + .whereRaw('users.account_id = accounts.id'); + }); +``` + +### havingNotExists + +**.havingNotExists(builder | callback)** + +Adds a havingNotExists clause to the query. + +```js +knex + .select('*') + .from('users') + .havingNotExists(function () { + this.select('*') + .from('accounts') + .whereRaw('users.account_id = accounts.id'); + }); +``` + +### havingBetween + +**.havingBetween(column, range)** + +Adds a havingBetween clause to the query. + +```js +knex.select('*').from('users').havingBetween('id', [5, 10]); +``` + +### havingNotBetween + +**.havingNotBetween(column, range)** + +Adds a havingNotBetween clause to the query. + +```js +knex.select('*').from('users').havingNotBetween('id', [5, 10]); +``` + +### havingRaw + +**.havingRaw(sql, [bindings])** + +Adds a havingRaw clause to the query. + +```js +knex('users') + .groupBy('count') + .orderBy('name', 'desc') + .havingRaw('count > ?', [100]); +``` diff --git a/docs/src/guide/raw.md b/docs/src/guide/raw.md new file mode 100644 index 0000000000..a60ec0f49f --- /dev/null +++ b/docs/src/guide/raw.md @@ -0,0 +1,138 @@ +# Raw + +Sometimes you may need to use a raw expression in a query. Raw query object may be injected pretty much anywhere you want, and using proper bindings can ensure your values are escaped properly, preventing SQL-injection attacks. + +## Raw Parameter Binding + +One can parameterize sql given to `knex.raw(sql, bindings)`. Parameters can be positional named. One can also choose if parameter should be treated as value or as sql identifier e.g. in case of `'TableName.ColumnName'` reference. + +```js +knex('users') + .select(knex.raw('count(*) as user_count, status')) + .where(knex.raw(1)) + .orWhere(knex.raw('status <> ?', [1])) + .groupBy('status'); +``` + +Positional bindings `?` are interpreted as values and `??` are interpreted as identifiers. + +```js +knex('users').where(knex.raw('?? = ?', ['user.name', 1])); +``` + +Named bindings such as `:name` are interpreted as values and `:name:` interpreted as identifiers. Named bindings are processed so long as the value is anything other than `undefined`. + +```js +const raw = + ':name: = :thisGuy or :name: = :otherGuy or :name: = :undefinedBinding'; + +knex('users').where( + knex.raw(raw, { + name: 'users.name', + thisGuy: 'Bob', + otherGuy: 'Jay', + undefinedBinding: undefined, + }) +); +``` + +For simpler queries where one only has a single binding, `.raw` can accept said binding as its second parameter. + +```js +knex('users') + .where(knex.raw('LOWER("login") = ?', 'knex')) + .orWhere(knex.raw('accesslevel = ?', 1)) + .orWhere(knex.raw('updtime = ?', '01-01-2016')); +``` + +Since there is no unified syntax for array bindings, instead you need to treat them as multiple values by adding `?` directly in your query. + +```js +const myArray = [1, 2, 3]; +knex.raw( + 'select * from users where id in (' + myArray.map((_) => '?').join(',') + ')', + [...myArray] +); +``` + +query will become: + +```sql +select * from users where id in (?, ?, ?) /* with bindings [1,2,3] */ +``` + +To prevent replacement of `?` one can use the escape sequence `\\?`. + +```js +knex + .select('*') + .from('users') + .where('id', '=', 1) + .whereRaw('?? \\? ?', ['jsonColumn', 'jsonKey']); +``` + +To prevent replacement of named bindings one can use the escape sequence `\\:`. + +```js +knex + .select('*') + .from('users') + .whereRaw(":property: = '\\:value' OR \\:property: = :value", { + property: 'name', + value: 'Bob', + }); +``` + +## Raw Expressions + +Raw expressions are created by using `knex.raw(sql, [bindings])` and passing this as a value for any value in the query chain. + +```js +knex('users') + .select(knex.raw('count(*) as user_count, status')) + .where(knex.raw(1)) + .orWhere(knex.raw('status <> ?', [1])) + .groupBy('status'); +``` + +## Raw Queries + +The `knex.raw` may also be used to build a full query and execute it, as a standard query builder query would be executed. The benefit of this is that it uses the connection pool and provides a standard interface for the different client libraries. + +```js +knex.raw('select * from users where id = ?', [1]).then(function (resp) { + /*...*/ +}); +``` + +Note that the response will be whatever the underlying sql library would typically return on a normal query, so you may need to look at the documentation for the base library the queries are executing against to determine how to handle the response. + +## Wrapped Queries + +The raw query builder also comes with a `wrap` method, which allows wrapping the query in a value: + +```js +const subcolumn = knex + .raw('select avg(salary) from employee where dept_no = e.dept_no') + .wrap('(', ') avg_sal_dept'); + +knex + .select('e.lastname', 'e.salary', subcolumn) + .from('employee as e') + .whereRaw('dept_no = e.dept_no'); +``` + +Note that the example above be achieved more easily using the [as](/guide/query-builder#as) method. + +```js +const subcolumn = knex + .avg('salary') + .from('employee') + .whereRaw('dept_no = e.dept_no') + .as('avg_sal_dept'); + +knex + .select('e.lastname', 'e.salary', subcolumn) + .from('employee as e') + .whereRaw('dept_no = e.dept_no'); +``` diff --git a/docs/src/guide/ref.md b/docs/src/guide/ref.md new file mode 100644 index 0000000000..f8a7b368fb --- /dev/null +++ b/docs/src/guide/ref.md @@ -0,0 +1,38 @@ +# Ref + +Can be used to create references in a query, such as column- or tablenames. This is a good and shorter alternative to using `knex.raw('??', 'tableName.columName')` which essentially does the same thing. + +## Usage + +`knex.ref` can be used essentially anywhere in a build-chain. Here is an example: + +```js +knex(knex.ref('Users').withSchema('TenantId')) + .where(knex.ref('Id'), 1) + .orWhere(knex.ref('Name'), 'Admin') + .select(['Id', knex.ref('Name').as('Username')]); +``` + + + +### withSchema + +The Ref function supports schema using `.withSchema(string)`: + +```js +knex(knex.ref('users').withSchema('TenantId')).select(); +``` + +### alias + +Alias is supported using `.alias(string)` + +```js +knex('users').select(knex.ref('Id').as('UserId')); +``` + + diff --git a/docs/src/guide/schema-builder.md b/docs/src/guide/schema-builder.md new file mode 100644 index 0000000000..72157d9a9f --- /dev/null +++ b/docs/src/guide/schema-builder.md @@ -0,0 +1,1189 @@ +# Schema Builder + +The `knex.schema` is a **getter function**, which returns a stateful object containing the query. Therefore be sure to obtain a new instance of the `knex.schema` for every query. These methods return [promises](/guide/interfaces.html#promises). + +## Essentials + +### withSchema + +**knex.schema.withSchema([schemaName])** + +Specifies the schema to be used when using the schema-building commands. + +```js +knex.schema.withSchema('public').createTable('users', function (table) { + table.increments(); +}); +``` + +### createTable + +**knex.schema.createTable(tableName, callback)** + +Creates a new table on the database, with a callback function to modify the table's structure, using the schema-building commands. + +```js +knex.schema.createTable('users', function (table) { + table.increments(); + table.string('name'); + table.timestamps(); +}); +``` + +### createTableLike + +**knex.schema.createTableLike(tableName, tableNameToCopy, [callback])** + +Creates a new table on the database based on another table. Copy only the structure : columns, keys and indexes (expected on SQL Server which only copy columns) and not the data. Callback function can be specified to add columns in the duplicated table. + +```js +knex.schema.createTableLike('new_users', 'users'); + +// "new_users" table contains columns +// of users and two new columns 'age' and 'last_name'. +knex.schema.createTableLike('new_users', 'users', (table) => { + table.integer('age'); + table.string('last_name'); +}); +``` + +### dropTable + +**knex.schema.dropTable(tableName)** + +Drops a table, specified by tableName. + +```js +knex.schema.dropTable('users'); +``` + +### dropTableIfExists + +**knex.schema.dropTableIfExists(tableName)** + +Drops a table conditionally if the table exists, specified by tableName. + +```js +knex.schema.dropTableIfExists('users'); +``` + +### renameTable + +**knex.schema.renameTable(from, to)** + +Renames a table from a current tableName to another. + +```js +knex.schema.renameTable('old_users', 'users'); +``` + +### hasTable + +**knex.schema.hasTable(tableName)** + +Checks for a table's existence by tableName, resolving with a boolean to signal if the table exists. + +```js +knex.schema.hasTable('users').then(function (exists) { + if (!exists) { + return knex.schema.createTable('users', function (t) { + t.increments('id').primary(); + t.string('first_name', 100); + t.string('last_name', 100); + t.text('bio'); + }); + } +}); +``` + +### hasColumn + +**knex.schema.hasColumn(tableName, columnName)** + +Checks if a column exists in the current table, resolves the promise with a boolean, true if the column exists, false otherwise. + +### table + +**knex.schema.table(tableName, callback)** + +Chooses a database table, and then modifies the table, using the Schema Building functions inside of the callback. + +```js +knex.schema.table('users', function (table) { + table.dropColumn('name'); + table.string('first_name'); + table.string('last_name'); +}); +``` + +### alterTable + +**knex.schema.alterTable(tableName, callback)** + +Chooses a database table, and then modifies the table, using the Schema Building functions inside of the callback. + +```js +knex.schema.alterTable('users', function (table) { + table.dropColumn('name'); + table.string('first_name'); + table.string('last_name'); +}); +``` + +### createView + +**knex.schema.createView(tableName, callback)** + +Creates a new view on the database, with a callback function to modify the view's structure, using the schema-building commands. + +```js +knex.schema.createView('users_view', function (view) { + view.columns(['first_name']); + view.as(knex('users').select('first_name').where('age', '>', '18')); +}); +``` + +### createViewOrReplace + +**knex.schema.createViewOrReplace(tableName, callback)** + +Creates a new view or replace it on the database, with a callback function to modify the view's structure, using the schema-building commands. You need to specify at least the same columns in same order (you can add extra columns). In SQLite, this function generate drop/create view queries (view columns can be different). + +```js +knex.schema.createViewOrReplace('users_view', function (view) { + view.columns(['first_name']); + view.as(knex('users').select('first_name').where('age', '>', '18')); +}); +``` + +### createMaterializedView + +**knex.schema.createMaterializedView(viewName, callback)** + +Creates a new materialized view on the database, with a callback function to modify the view's structure, using the schema-building commands. Only on PostgreSQL, CockroachDb, Redshift and Oracle. + +```js +knex.schema.createMaterializedView('users_view', function (view) { + view.columns(['first_name']); + view.as(knex('users').select('first_name').where('age', '>', '18')); +}); +``` + +### refreshMaterializedView + +**knex.schema.refreshMaterializedView(viewName)** + +Refresh materialized view on the database. Only on PostgreSQL, CockroachDb, Redshift and Oracle. + +```js +knex.schema.refreshMaterializedView('users_view'); +``` + +### dropView + +**knex.schema.dropView(viewName)** + +Drop view on the database. + +```js +knex.schema.dropView('users_view'); +``` + +### dropViewIfExists + +**knex.schema.dropViewIfExists(viewName)** + +Drop view on the database if exists. + +```js +knex.schema.dropViewIfExists('users_view'); +``` + +### dropMaterializedView + +**knex.schema.dropMaterializedView(viewName)** + +Drop materialized view on the database. Only on PostgreSQL, CockroachDb, Redshift and Oracle. + +```js +knex.schema.dropMaterializedView('users_view'); +``` + +### dropMaterializedViewIfExists + +**knex.schema.dropMaterializedViewIfExists(viewName)** + +Drop materialized view on the database if exists. Only on PostgreSQL, CockroachDb, Redshift and Oracle. + +```js +knex.schema.dropMaterializedViewIfExists('users_view'); +``` + +### renameView + +**knex.schema.renameView(viewName)** + +Rename a existing view in the database. Not supported by Oracle and SQLite. + +```js +knex.schema.renameView('users_view'); +``` + +### alterView + +**knex.schema.alterView(viewName)** + +Alter view to rename columns or change default values. Only available on PostgreSQL, MSSQL and Redshift. + +```js +knex.schema.alterView('view_test', function (view) { + view.column('first_name').rename('name_user'); + view.column('bio').defaultTo('empty'); +}); +``` + +### generateDdlCommands + +**knex.schema.generateDdlCommands()** + +Generates complete SQL commands for applying described schema changes, without executing anything. Useful when knex is being used purely as a query builder. Generally produces same result as .toSQL(), with a notable exception with SQLite, which relies on asynchronous calls to the database for building part of its schema modification statements + +```js +const ddlCommands = knex.schema + .alterTable('users', (table) => { + table + .foreign('companyId') + .references('company.companyId') + .withKeyName('fk_fkey_company'); + }) + .generateDdlCommands(); +``` + +### raw + +**knex.schema.raw(statement)** + +Run an arbitrary sql query in the schema builder chain. + +```js +knex.schema.raw("SET sql_mode='TRADITIONAL'").table('users', function (table) { + table.dropColumn('name'); + table.string('first_name'); + table.string('last_name'); +}); +``` + +### queryContext + +**knex.schema.queryContext(context)** + +Allows configuring a context to be passed to the [wrapIdentifier](/guide/#wrapidentifier) hook. The context can be any kind of value and will be passed to `wrapIdentifier` without modification. + +```js +knex.schema.queryContext({ foo: 'bar' }).table('users', function (table) { + table.string('first_name'); + table.string('last_name'); +}); +``` + +The context configured will be passed to `wrapIdentifier` for each identifier that needs to be formatted, including the table and column names. However, a different context can be set for the column names via [table.queryContext](/guide/query-builder#querycontext). + +Calling `queryContext` with no arguments will return any context configured for the schema builder instance. + +### dropSchema + +**knex.schema.dropSchema(schemaName, [cascade])** + +Drop a schema, specified by the schema's name, with optional cascade option (default to false). Only supported by PostgreSQL. + +```js +//drop schema 'public' +knex.schema.dropSchema('public'); +//drop schema 'public' cascade +knex.schema.dropSchema('public', true); +``` + +### dropSchemaIfExists + +**knex.schema.dropSchemaIfExists(schemaName, [cascade])** + +Drop a schema conditionally if the schema exists, specified by the schema's name, with optional cascade option (default to false). Only supported by PostgreSQL. + +```js +//drop schema if exists 'public' +knex.schema.dropSchemaIfExists('public'); +//drop schema if exists 'public' cascade +knex.schema.dropSchemaIfExists('public', true); +``` + +## Schema Building + +### dropColumn + +**table.dropColumn(name)** + +Drops a column, specified by the column's name + +### dropColumns + +**table.dropColumns(columns)** + +Drops multiple columns, taking a variable number of column names. + +### renameColumn + +**table.renameColumn(from, to)** + +Renames a column from one name to another. + +### increments + +**table.increments(name, options={[primaryKey: boolean = true])** + +Adds an auto incrementing column. In PostgreSQL this is a serial; in Amazon Redshift an integer identity(1,1). This will be used as the primary key for the table if the column isn't in another primary key. Also available is a bigIncrements if you wish to add a bigint incrementing number (in PostgreSQL bigserial). Note that a primary key is created by default if the column isn't in primary key (with primary function), but you can override this behaviour by passing the `primaryKey` option. If you use this function with primary function, the column is added to the composite primary key. With SQLite, autoincrement column need to be a primary key, so if primary function is used, primary keys are transformed in unique index. MySQL don't support autoincrement column without primary key, so multiple queries are generated to create int column, add increments column to composite primary key then modify the column to autoincrement column. + +```js +// create table 'users' +// with a primary key using 'increments()' +knex.schema.createTable('users', function (table) { + table.increments('userId'); + table.string('name'); +}); + +// create table 'users' +// with a composite primary key ('userId', 'name'). +// increments doesn't generate primary key. +knex.schema.createTable('users', function (table) { + table.primary(['userId', 'name']); + table.increments('userId'); + table.string('name'); +}); + +// reference the 'users' primary key in new table 'posts' +knex.schema.createTable('posts', function (table) { + table.integer('author').unsigned().notNullable(); + table.string('title', 30); + table.string('content'); + + table.foreign('author').references('userId').inTable('users'); +}); +``` + +A primaryKey option may be passed, to disable to automatic primary key creation: + +```js +// create table 'users' +// with a primary key using 'increments()' +// but also increments field 'other_id' +// that does not need primary key +knex.schema.createTable('users', function (table) { + table.increments('id'); + table.increments('other_id', { primaryKey: false }); +}); +``` + +### integer + +**table.integer(name, length)** + +Adds an integer column. On PostgreSQL you cannot adjust the length, you need to use other option such as bigInteger, etc + +### bigInteger + +**table.bigInteger(name)** + +In MySQL or PostgreSQL, adds a bigint column, otherwise adds a normal integer. Note that bigint data is returned as a string in queries because JavaScript may be unable to parse them without loss of precision. + +### tinyint + +**table.tinyint(name, length)** + +Adds a tinyint column + +### smallint + +**table.smallint(name)** + +Adds a smallint column + +### mediumint + +**table.mediumint(name)** + +Adds a mediumint column + +### bigint + +**table.bigint(name)** + +Adds a bigint column + +### text + +**table.text(name, [textType])** + +Adds a text column, with optional textType for MySql text datatype preference. textType may be mediumtext or longtext, otherwise defaults to text. + +### string + +**table.string(name, [length])** + +Adds a string column, with optional length defaulting to 255. + +### float + +**table.float(column, [precision], [scale])** + +Adds a float column, with optional precision (defaults to 8) and scale (defaults to 2). + +### double + +**table.double(column, [precision], [scale])** + +Adds a double column, with optional precision (defaults to 8) and scale (defaults to 2). In SQLite/MSSQL this is a float with no precision/scale; In PostgreSQL this is a double precision; In Oracle this is a number with matching precision/scale. + +### decimal + +**table.decimal(column, [precision], [scale])** + +Adds a decimal column, with optional precision (defaults to 8) and scale (defaults to 2). Specifying NULL as precision creates a decimal column that can store numbers of any precision and scale. (Only supported for Oracle, SQLite, Postgres) + +### boolean + +**table.boolean(name)** + +Adds a boolean column. + +### date + +**table.date(name)** + +Adds a date column. + +### datetime + +**table.datetime(name, options={[useTz: boolean], [precision: number]})** + +Adds a datetime column. By default PostgreSQL creates column with timezone (timestamptz type). This behaviour can be overriden by passing the useTz option (which is by default true for PostgreSQL). MySQL and MSSQL do not have useTz option. + +A precision option may be passed: + +```js +table.datetime('some_time', { precision: 6 }).defaultTo(knex.fn.now(6)); +``` + +### time + +**table.time(name, [precision])** + +Adds a time column, with optional precision for MySQL. Not supported on Amazon Redshift. + +In MySQL a precision option may be passed: + +```js +table.time('some_time', { precision: 6 }); +``` + +### timestamp + +**table.timestamp(name, options={[useTz: boolean], [precision: number]})** + +Adds a timestamp column. By default PostgreSQL creates column with timezone (timestamptz type) and MSSQL does not (datetime2). This behaviour can be overriden by passing the useTz option (which is by default false for MSSQL and true for PostgreSQL). MySQL does not have useTz option. + +```js +table.timestamp('created_at').defaultTo(knex.fn.now()); +``` + +In PostgreSQL and MySQL a precision option may be passed: + +```js +table.timestamp('created_at', { precision: 6 }).defaultTo(knex.fn.now(6)); +``` + +In PostgreSQL and MSSQL a timezone option may be passed: + +```js +table.timestamp('created_at', { useTz: true }); +``` + +### timestamps + +**table.timestamps([useTimestamps], [defaultToNow], [useCamelCase])** + +Adds created_at and updated_at columns on the database, setting each to datetime types. When true is passed as the first argument a timestamp type is used instead. Both columns default to being not null and using the current timestamp when true is passed as the second argument. Note that on MySQL the .timestamps() only have seconds precision, to get better precision use the .datetime or .timestamp methods directly with precision. If useCamelCase is true, the name of columns are createdAt and updatedAt. + +::: info +PostgreSQL `updated_at` field will not automatically be updated. Please see this [issue](https://github.com/knex/knex/issues/1928 'issue') for details +::: + +### dropTimestamps + +**table.dropTimestamps([useCamelCase])** + +Drops the columns created_at and updated_at from the table, which can be created via timestamps. If useCamelCase is true, the name of columns are createdAt and updatedAt. + +### binary + +**table.binary(name, [length])** + +Adds a binary column, with optional length argument for MySQL. + +### enum / enu + +**table.enu(col, values, [options])** + +Adds a enum column, (aliased to enu, as enum is a reserved word in JavaScript). Implemented as unchecked varchar(255) on Amazon Redshift. Note that the second argument is an array of values. Example: + +```js +table.enu('column', ['value1', 'value2']); +``` + +For Postgres, an additional options argument can be provided to specify whether or not to use Postgres's native TYPE: + +```js +table.enu('column', ['value1', 'value2'], { + useNative: true, + enumName: 'foo_type', +}); +``` + +It will use the values provided to generate the appropriate TYPE. Example: + +```sql +CREATE TYPE "foo_type" AS ENUM ('value1', 'value2'); +``` + +To use an existing native type across columns, specify 'existingType' in the options (this assumes the type has already been created): + +::: info +Since the enum values aren't utilized for a native && existing type, the type being passed in for values is immaterial. +::: + +```js +table.enu('column', null, { + useNative: true, + existingType: true, + enumName: 'foo_type', +}); +``` + +If you want to use existing enums from a schema, different from the schema of your current table, specify 'schemaName' in the options: + +```js +table.enu('column', null, { + useNative: true, + existingType: true, + enumName: 'foo_type', + schemaName: 'public', +}); +``` + +Knex does not provide any way to alter enumerations after creation. To change an enumeration later on you must use Knex.raw, and the appropriate command for your database. + +### json + +**table.json(name)** + +Adds a json column, using the built-in json type in PostgreSQL, MySQL and SQLite, defaulting to a text column in older versions or in unsupported databases. + +For PostgreSQL, due to incompatibility between native array and json types, when setting an array (or a value that could be an array) as the value of a json or jsonb column, you should use JSON.stringify() to convert your value to a string prior to passing it to the query builder, e.g. + +```js +knex + .table('users') + .where({ id: 1 }) + .update({ json_data: JSON.stringify(mightBeAnArray) }); +``` + +### jsonb + +**table.jsonb(name)** + +Adds a jsonb column. Works similar to table.json(), but uses native jsonb type if possible. + +### uuid + +**table.uuid(name, options=({[useBinaryUuid:boolean],[primaryKey:boolean]})** + +Adds a uuid column - this uses the built-in uuid type in PostgreSQL, and falling back to a char(36) in other databases by default. +If useBinaryUuid is true, binary(16) is used. See uuidToBin function to convert uuid in binary before inserting and binToUuid to convert binary uuid to uuid. +If primaryKey is true, then for PostgreSQL the field will be configured as `uuid primary key`, for CockroackDB an additional `default gen_random_uuid()` is set on the type. + +You may set the default value to the uuid helper function. Not supported by Redshift. + +```js +knex.schema.createTable(tblName, (table) => { + table.uuid('uuidColumn').defaultTo(knex.fn.uuid()); +}); +``` + +### geometry + +**table.geometry(name)** + +Adds a geometry column. Supported by SQLite, MSSQL and PostgreSQL. + +```js +knex.schema.createTable(tblName, (table) => { + table.geometry('geometryColumn'); +}); +``` + +### geography + +**table.geography(name)** + +Adds a geography column. Supported by SQLite, MSSQL and PostgreSQL (in PostGIS extension). + +```js +knex.schema.createTable(tblName, (table) => { + table.geography('geographyColumn'); +}); +``` + +### point + +**table.point(name)** + +Add a point column. Not supported by CockroachDB and MSSQL. + +```js +knex.schema.createTable(tblName, (table) => { + table.point('pointColumn'); +}); +``` + +### comment + +**table.comment(value)** + +Sets the comment for a table. + +### engine + +**table.engine(val)** + +Sets the engine for the database table, only available within a createTable call, and only applicable to MySQL. + +### charset + +**table.charset(val)** + +Sets the charset for the database table, only available within a createTable call, and only applicable to MySQL. + +### collate + +**table.collate(val)** + +Sets the collation for the database table, only available within a createTable call, and only applicable to MySQL. + +### inherits + +**table.inherits(val)** + +Sets the tables that this table inherits, only available within a createTable call, and only applicable to PostgreSQL. + +### specificType + +**table.specificType(name, type)** + +Sets a specific type for the column creation, if you'd like to add a column type that isn't supported here. + +### index + +**table.index(columns, [indexName], options=({[indexType: string], [storageEngineIndexType: 'btree'|'hash'], [predicate: QueryBuilder]}))** + +Adds an index to a table over the given columns. A default index name using the columns is used unless indexName is specified. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : [https://dev.mysql.com/doc/refman/8.0/en/create-index.html](https://dev.mysql.com/doc/refman/8.0/en/create-index.html). The indexType can be optionally specified for PostgreSQL and MySQL. Amazon Redshift does not allow creating an index. In PostgreSQL, SQLite and MSSQL a partial index can be specified by setting a 'where' predicate. + +```js +knex.table('users', function (table) { + table.index(['name', 'last_name'], 'idx_name_last_name', { + indexType: 'FULLTEXT', + storageEngineIndexType: 'hash', + predicate: knex.whereNotNull('email'), + }); +}); +``` + +### dropIndex + +**table.dropIndex(columns, [indexName])** + +Drops an index from a table. A default index name using the columns is used unless indexName is specified (in which case columns is ignored). Amazon Redshift does not allow creating an index. + +### setNullable + +**table.setNullable(column)** + +Makes table column nullable. + +### dropNullable + +**table.dropNullable(column)** + +Makes table column not nullable. Note that this operation will fail if there are already null values in this column. + +### primary + +**table.primary(columns, options=({[constraintName:string],[deferrable:'not deferrable'|'deferred'|'immediate']})** + +Create a primary key constraint on table using input `columns`. If you need to create a composite primary key, pass an array of columns to `columns`. Constraint name defaults to `tablename_pkey` unless `constraintName` is specified. On Amazon Redshift, all columns included in a primary key must be not nullable. Deferrable primary constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object. + +```js +knex.schema.alterTable('users', function (t) { + t.unique('email'); +}); +knex.schema.alterTable('job', function (t) { + t.primary('email', { + constraintName: 'users_primary_key', + deferrable: 'deferred', + }); +}); +``` + +::: info +If you want to chain primary() while creating new column you can use [primary](#primary-1) +::: + +### unique + +**table.unique(columns, options={[indexName: string], [deferrable:'not deferrable'|'immediate'|'deferred'], [storageEngineIndexType:'btree'|'hash'], [useConstraint:true|false], [predicate: QueryBuilder]})** + +Adds an unique index to a table over the given `columns`. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : [https://dev.mysql.com/doc/refman/8.0/en/create-index.html](https://dev.mysql.com/doc/refman/8.0/en/create-index.html). A default index name using the columns is used unless indexName is specified. If you need to create a composite index, pass an array of column to `columns`. Deferrable unique constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object. In MSSQL and Postgres, you can set the `useConstraint` option to true to create a unique constraint instead of a unique index (defaults to false for MSSQL, true for Postgres without `predicate`, false for Postgres with `predicate`). In PostgreSQL, SQLite and MSSQL a partial unique index can be specified by setting a 'where' predicate. + +```js +knex.schema.alterTable('users', function (t) { + t.unique('email'); +}); +knex.schema.alterTable('job', function (t) { + t.unique(['account_id', 'program_id'], { + indexName: 'job_composite_index', + deferrable: 'deferred', + storageEngineIndexType: 'hash', + }); +}); +knex.schema.alterTable('job', function (t) { + t.unique(['account_id', 'program_id'], { + indexName: 'job_composite_index', + useConstraint: true, + }); +}); +knex.schema.alterTable('job', function (t) { + t.unique(['account_id', 'program_id'], { + indexName: 'job_composite_index', + predicate: knex.whereNotNull('account_id'), + }); +}); +``` + +::: info +If you want to chain unique() while creating new column you can use [unique](#unique-1) +::: + +### foreign + +**table.foreign(columns, [foreignKeyName])[.onDelete(statement).onUpdate(statement).withKeyName(foreignKeyName).deferrable(type)]** + +Adds a foreign key constraint to a table for an existing column using `table.foreign(column).references(column)` or multiple columns using `table.foreign(columns).references(columns).inTable(table)`. + +A default key name using the columns is used unless `foreignKeyName` is specified. + +You can also chain `onDelete()` and/or `onUpdate()` to set the reference option `(RESTRICT, CASCADE, SET NULL, NO ACTION)` for the operation. You can also chain `withKeyName()` to override default key name that is generated from table and column names (result is identical to specifying second parameter to function `foreign()`). + +Deferrable foreign constraint is supported on Postgres and Oracle and can be set by chaining `.deferrable(type)` + +Note that using `foreign()` is the same as `column.references(column)` but it works for existing columns. + +```js +knex.schema.table('users', function (table) { + table.integer('user_id').unsigned(); + table + .foreign('user_id') + .references('Items.user_id_in_items') + .deferrable('deferred'); +}); +``` + +### dropForeign + +**table.dropForeign(columns, [foreignKeyName])** + +Drops a foreign key constraint from a table. A default foreign key name using the columns is used unless foreignKeyName is specified (in which case columns is ignored). + +### dropUnique + +**table.dropUnique(columns, [indexName])** + +Drops a unique key constraint from a table. A default unique key name using the columns is used unless indexName is specified (in which case columns is ignored). + +### dropPrimary + +**table.dropPrimary([constraintName])** + +Drops the primary key constraint on a table. Defaults to tablename_pkey unless constraintName is specified. + +### queryContext + +**table.queryContext(context)** + +Allows configuring a context to be passed to the [wrapIdentifier](/guide/#wrapidentifier) hook for formatting table builder identifiers. The context can be any kind of value and will be passed to `wrapIdentifier` without modification. + +```js +knex.schema.table('users', function (table) { + table.queryContext({ foo: 'bar' }); + table.string('first_name'); + table.string('last_name'); +}); +``` + +This method also enables overwriting the context configured for a schema builder instance via [schema.queryContext](/guide/schema-builder#querycontext): + +```js +knex.schema.queryContext('schema context').table('users', function (table) { + table.queryContext('table context'); + table.string('first_name'); + table.string('last_name'); +}); +``` + +Note that it's also possible to overwrite the table builder context for any column in the table definition: + +```js +knex.schema.queryContext('schema context').table('users', function (table) { + table.queryContext('table context'); + table.string('first_name').queryContext('first_name context'); + table.string('last_name').queryContext('last_name context'); +}); +``` + +Calling `queryContext` with no arguments will return any context configured for the table builder instance. + +## Chainable Methods + +The following three methods may be chained on the schema building methods, as modifiers to the column. + +### alter + +**column.alter(options={[alterNullable: boolean = true, alterType: boolean = true])** + +Marks the column as an alter / modify, instead of the default add. + +::: warning +This only works in .alterTable() and is not supported by SQlite or Amazon Redshift. Alter is _not_ done incrementally over older column type so if you like to add `notNullable` and keep the old default value, the alter statement must contain both `.notNullable().defaultTo(1).alter()`. If one just tries to add `.notNullable().alter()` the old default value will be dropped. Nullable alterations are done only if alterNullable is true. Type alterations are done only if alterType is true. +::: + +```js +knex.schema.alterTable('user', function (t) { + t.increments().primary(); // add + // drops previous default value from column, + // change type to string and add not nullable constraint + t.string('username', 35).notNullable().alter(); + // drops both not null constraint and the default value + t.integer('age').alter(); + // if alterNullable is false, drops only the default value + t.integer('age').alter({ alterNullable: false }); + // if alterType is false, type of column is not altered. + t.integer('age').alter({ alterType: false }); +}); +``` + +### index + +**column.index([indexName], options=({[indexType: string], [storageEngineIndexType: 'btree'|'hash'], [predicate: QueryBuilder]}))** + +Specifies a field as an index. If an indexName is specified, it is used in place of the standard index naming convention of tableName_columnName. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : [https://dev.mysql.com/doc/refman/8.0/en/create-index.html](https://dev.mysql.com/doc/refman/8.0/en/create-index.html). The indexType can be optionally specified for PostgreSQL and MySQL. No-op if this is chained off of a field that cannot be indexed. In PostgreSQL, SQLite and MSSQL a partial index can be specified by setting a 'where' predicate. + +### primary + +**column.primary(options=({[constraintName:string],[deferrable:'not deferrable'|'deferred'|'immediate']}));** + +Sets a primary key constraint on `column`. Constraint name defaults to `tablename_pkey` unless `constraintName` is specified. On Amazon Redshift, all columns included in a primary key must be not nullable. Deferrable primary constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object. + +```js +knex.schema.table('users', function (table) { + table.integer('user_id').primary('email', { + constraintName: 'users_primary_key', + deferrable: 'deferred', + }); +}); +``` + +::: info +If you want to create primary constraint on existing column use [primary](#primary) +::: + +### unique + +**column.unique(options={[indexName:string],[deferrable:'not deferrable'|'immediate'|'deferred']})** + +Sets the `column` as unique. On Amazon Redshift, this constraint is not enforced, but it is used by the query planner. Deferrable unique constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object. + +```js +knex.schema.table('users', function (table) { + table + .integer('user_id') + .unique({ indexName: 'user_unique_id', deferrable: 'immediate' }); +}); +``` + +::: info +If you want to create unique constraint on existing column use [unique](#unique) +::: + +### references + +**column.references(column)** + +Sets the "column" that the current column references as a foreign key. "column" can either be "." syntax, or just the column name followed up with a call to inTable to specify the table. + +### inTable + +**column.inTable(table)** + +Sets the "table" where the foreign key column is located after calling column.references. + +### onDelete + +**column.onDelete(command)** + +Sets the SQL command to be run "onDelete". + +### onUpdate + +**column.onUpdate(command)** + +Sets the SQL command to be run "onUpdate". + +### defaultTo + +**column.defaultTo(value, options={[constraintName: string = undefined]))** + +Sets the default value for the column on an insert. + +In MSSQL a constraintName option may be passed to ensure a specific constraint name: + +```js +column.defaultTo('value', { constraintName: 'df_table_value' }); +``` + +### unsigned + +**column.unsigned()** + +Specifies a number as unsigned. Only for numeric values. + +### notNullable + +**column.notNullable()** + +Adds a not null on the current column being created. + +### nullable + +**column.nullable()** + +Default on column creation, this explicitly sets a field to be nullable. + +### first + +**column.first()** + +Sets the column to be inserted on the first position, only used in MySQL alter tables. + +### after + +**column.after(field)** + +Sets the column to be inserted after another, only used in MySQL alter tables. + +### comment + +**column.comment(value)** + +Sets the comment for a column. + +```js +knex.schema.createTable('accounts', function (t) { + t.increments().primary(); + t.string('email').unique().comment('This is the email field'); +}); +``` + +### collate + +**column.collate(collation)** + +Sets the collation for a column (only works in MySQL). Here is a list of all available collations: [https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html](https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html) + +```js +knex.schema.createTable('users', function (t) { + t.increments(); + t.string('email').unique().collate('utf8_unicode_ci'); +}); +``` + +## View + +### columns + +**view.columns([columnNames])** + +Specify the columns of the view. + +```js +knex.schema.createView('users_view', function (view) { + view.columns(['first_name', 'last_name']); + view.as(knex('users').select('first_name').where('age', '>', '18')); +}); +``` + +### as + +**view.as(selectQuery)** + +Specify the select query of the view. + +### checkOption + +**view.checkOption()** + +Add check option on the view definition. On OracleDb, MySQL, PostgreSQL and Redshift. + +### localCheckOption + +**view.localCheckOption()** + +Add local check option on the view definition. On MySQL, PostgreSQL and Redshift. + +### cascadedCheckOption + +**view.cascadedCheckOption()** + +Add cascaded check option on the view definition. On MySQL, PostgreSQL and Redshift. + +## Checks + +### check + +**table.check(checkPredicate, [bindings], [constraintName]))** + +Specify a check on table or column with raw predicate. + +```js +knex.schema.createTable('product', function (table) { + table.integer('price_min'); + table.integer('price'); + table.check('?? >= ??', ['price', 'price_min']); +}); +``` + +### checkPositive + +**column.checkPositive([constraintName])** + +Specify a check on column that test if the value of column is positive. + +```js +knex.schema.createTable('product', function (table) { + table.integer('price').checkPositive(); +}); +``` + +### checkNegative + +**column.checkNegative([constraintName])** + +Specify a check on column that test if the value of column is negative. + +```js +knex.schema.createTable('product', function (table) { + table.integer('price_decrease').checkNegative(); +}); +``` + +### checkIn + +**column.checkIn(values, [constraintName])** + +Specify a check on column that test if the value of column is contained in a set of specified values. + +```js +knex.schema.createTable('product', function (table) { + table.string('type').checkIn(['table', 'chair', 'sofa']); +}); +``` + +### checkNotIn + +**column.checkNotIn(values, [constraintName])** + +Specify a check on column that test if the value of column is not contains in a set of specified values. + +```js +knex.schema.createTable('product', function (table) { + table.string('type').checkNotIn(['boot', 'shoe']); +}); +``` + +### checkBetween + +**column.checkBetween(values, [constraintName])** + +Specify a check on column that test if the value of column is within a range of values. + +```js +knex.schema.createTable('product', function (table) { + table.integer('price').checkBetween([0, 100]); +}); +// You can add checks on multiple intervals +knex.schema.createTable('product', function (table) { + table.integer('price').checkBetween([ + [0, 20], + [30, 40], + ]); +}); +``` + +### checkLength + +**column.checkLength(operator, length, [constraintName])** + +Specify a check on column that test if the length of a string match the predicate. + +```js +knex.schema.createTable('product', function (table) { + // operator can be =, !=, <=, >=, <, > + t.varchar('phone').checkLength('=', 8); +}); +``` + +### checkRegex + +**column.checkRegex(regex, [constraintName])** + +Specify a check on column that test if the value match the specified regular expression. In MSSQL only simple pattern matching in supported but not regex syntax. + +```js +knex.schema.createTable('product', function (table) { + table.string('phone').checkRegex('[0-9]{8}'); + // In MSSQL, {8} syntax don't work, + // you need to duplicate [0-9]. + table.string('phone').checkRegex('[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'); +}); +``` + +### dropChecks + +**table.dropChecks([checkConstraintNames])** + +Drop checks constraint given an array of constraint names. + +```js +knex.schema.createTable('product', function (table) { + table.integer('price').checkPositive('price_check'); + table + .integer('price_proportion') + .checkBetween([0, 100], 'price_proportion_check'); + table.dropChecks(['price_check', 'price_proportion_check']); +}); +``` diff --git a/docs/src/guide/transactions.md b/docs/src/guide/transactions.md new file mode 100644 index 0000000000..890201af91 --- /dev/null +++ b/docs/src/guide/transactions.md @@ -0,0 +1,245 @@ +# Transactions + +Transactions are an important feature of relational databases, as they allow correct recovery from failures and keep a database consistent even in cases of system failure. All queries within a transaction are executed on the same database connection, and run the entire set of queries as a single unit of work. Any failure will mean the database will rollback any queries executed on that connection to the pre-transaction state. + +Transactions are handled by passing a handler function into `knex.transaction`. The handler function accepts a single argument, an object which may be used in two ways: + +1. As the "promise aware" knex connection +2. As an object passed into a query with [transacting](/guide/query-builder#transacting) and eventually call commit or rollback. + +Consider these two examples: + +```js +// Using trx as a query builder: +knex + .transaction(function (trx) { + const books = [ + { title: 'Canterbury Tales' }, + { title: 'Moby Dick' }, + { title: 'Hamlet' }, + ]; + + return trx + .insert({ name: 'Old Books' }, 'id') + .into('catalogues') + .then(function (ids) { + books.forEach((book) => (book.catalogue_id = ids[0])); + return trx('books').insert(books); + }); + }) + .then(function (inserts) { + console.log(inserts.length + ' new books saved.'); + }) + .catch(function (error) { + // If we get here, that means that + // neither the 'Old Books' catalogues insert, + // nor any of the books inserts will have taken place. + console.error(error); + }); +``` + +And then this example: + +```js +// Using trx as a transaction object: +knex + .transaction(function (trx) { + const books = [ + { title: 'Canterbury Tales' }, + { title: 'Moby Dick' }, + { title: 'Hamlet' }, + ]; + + knex + .insert({ name: 'Old Books' }, 'id') + .into('catalogues') + .transacting(trx) + .then(function (ids) { + books.forEach((book) => (book.catalogue_id = ids[0])); + return knex('books').insert(books).transacting(trx); + }) + .then(trx.commit) + .catch(trx.rollback); + }) + .then(function (inserts) { + console.log(inserts.length + ' new books saved.'); + }) + .catch(function (error) { + // If we get here, that means that + // neither the 'Old Books' catalogues insert, + // nor any of the books inserts will have taken place. + console.error(error); + }); +``` + +Same example as above using await/async: + +```ts +try { + await knex.transaction(async (trx) => { + const books = [ + { title: 'Canterbury Tales' }, + { title: 'Moby Dick' }, + { title: 'Hamlet' }, + ]; + + const ids = await trx('catalogues').insert( + { + name: 'Old Books', + }, + 'id' + ); + + books.forEach((book) => (book.catalogue_id = ids[0])); + const inserts = await trx('books').insert(books); + + console.log(inserts.length + ' new books saved.'); + }); +} catch (error) { + // If we get here, that means that neither the 'Old Books' catalogues insert, + // nor any of the books inserts will have taken place. + console.error(error); +} +``` + +Same example as above using another await/async approach: + +```ts +try { + await knex.transaction(async (trx) => { + const books = [ + { title: 'Canterbury Tales' }, + { title: 'Moby Dick' }, + { title: 'Hamlet' }, + ]; + + const ids = await knex('catalogues') + .insert( + { + name: 'Old Books', + }, + 'id' + ) + .transacting(trx); + + books.forEach((book) => (book.catalogue_id = ids[0])); + await knex('books').insert(books).transacting(trx); + + console.log(inserts.length + ' new books saved.'); + }); +} catch (error) { + console.error(error); +} +``` + +Throwing an error directly from the transaction handler function automatically rolls back the transaction, same as returning a rejected promise. + +Notice that if a promise is not returned within the handler, it is up to you to ensure `trx.commit`, or `trx.rollback` are called, otherwise the transaction connection will hang. + +Calling `trx.rollback` will return a rejected Promise. If you don't pass any argument to `trx.rollback`, a generic `Error` object will be created and passed in to ensure the Promise always rejects with something. + +Note that Amazon Redshift does not support savepoints in transactions. + +In some cases you may prefer to create transaction but only execute statements in it later. In such case call method `transaction` without a handler function: + +```ts +// Using trx as a transaction object: +const trx = await knex.transaction(); + +const books = [ + { title: 'Canterbury Tales' }, + { title: 'Moby Dick' }, + { title: 'Hamlet' }, +]; + +trx('catalogues') + .insert({ name: 'Old Books' }, 'id') + .then(function (ids) { + books.forEach((book) => (book.catalogue_id = ids[0])); + return trx('books').insert(books); + }) + .then(trx.commit) + .catch(trx.rollback); +``` + +If you want to create a reusable transaction instance, but do not want to actually start it until it is used, you can create a transaction provider instance. It will start transaction after being called for the first time, and return same transaction on subsequent calls: + +```ts +// Does not start a transaction yet +const trxProvider = knex.transactionProvider(); + +const books = [ + { title: 'Canterbury Tales' }, + { title: 'Moby Dick' }, + { title: 'Hamlet' }, +]; + +// Starts a transaction +const trx = await trxProvider(); +const ids = await trx('catalogues').insert({ name: 'Old Books' }, 'id'); +books.forEach((book) => (book.catalogue_id = ids[0])); +await trx('books').insert(books); + +// Reuses same transaction +const sameTrx = await trxProvider(); +const ids2 = await sameTrx('catalogues').insert({ name: 'New Books' }, 'id'); +books.forEach((book) => (book.catalogue_id = ids2[0])); +await sameTrx('books').insert(books); +``` + +You can access the promise that gets resolved after transaction is rolled back explicitly by user or committed, or rejected if it gets rolled back by DB itself, when using either way of creating transaction, from field `executionPromise`: + +```ts +const trxProvider = knex.transactionProvider(); +const trx = await trxProvider(); +const trxPromise = trx.executionPromise; + +const trx2 = await knex.transaction(); +const trx2Promise = trx2.executionPromise; + +const trxInitPromise = new Promise(async (resolve, reject) => { + knex.transaction((transaction) => { + resolve(transaction); + }); +}); +const trx3 = await trxInitPromise; +const trx3Promise = trx3.executionPromise; +``` + +You can check if a transaction has been committed or rolled back with the method `isCompleted`: + +```ts +const trx = await knex.transaction(); +trx.isCompleted(); // false +await trx.commit(); +trx.isCompleted(); // true + +const trx2 = knex.transactionProvider(); +await trx2.rollback(); +trx2.isCompleted(); // true +``` + +You can check the property `knex.isTransaction` to see if the current knex instance you are working with is a transaction. + +## Transaction Modes + +In case you need to specify an isolation level for your transaction, you can use a config parameter `isolationLevel`. Not supported by oracle and sqlite, options are `read uncommitted`, `read committed`, `repeatable read`, `snapshot` (mssql only), `serializable`. + +```ts +// Simple read skew example +const isolationLevel = 'read committed'; +const trx = await knex.transaction({ isolationLevel }); +const result1 = await trx(tableName).select(); +await knex(tableName).insert({ id: 1, value: 1 }); +const result2 = await trx(tableName).select(); +await trx.commit(); +// result1 may or may not deep equal result2 depending on isolation level +``` + +You may also set the transaction mode as `read only` using the `readOnly` config parameter. It is currently only supported on mysql, postgres, and redshift. + +```ts +const trx = await knex.transaction({ readOnly: true }); +// 💥 Cannot `INSERT` while inside a `READ ONLY` transaction +const result = await trx(tableName).insert({ id: 1, foo: 'bar' }); +``` diff --git a/docs/src/guide/utility.md b/docs/src/guide/utility.md new file mode 100644 index 0000000000..7ff0fad6bc --- /dev/null +++ b/docs/src/guide/utility.md @@ -0,0 +1,93 @@ +# Utility + +A collection of utilities that the knex library provides for convenience. + +## batchInsert + +**knex.batchInsert(tableName)** + +The `batchInsert` utility will insert a batch of rows wrapped inside a transaction _(which is automatically created unless explicitly given a transaction using [transacting](/guide/query-builder#transacting))_, at a given `chunkSize`. + +It's primarily designed to be used when you have thousands of rows to insert into a table. + +By default, the `chunkSize` is set to 1000. + +BatchInsert also allows for [returning values](/guide/query-builder#returning) and supplying transactions using [transacting](/guide/query-builder#transacting). + +```js +const rows = [ + { + /*...*/ + }, + { + /*...*/ + }, +]; +const chunkSize = 30; +knex + .batchInsert('TableName', rows, chunkSize) + .returning('id') + .then(function (ids) { + /*...*/ + }) + .catch(function (error) { + /*...*/ + }); + +knex + .transaction(function (tr) { + return knex.batchInsert('TableName', rows, chunkSize).transacting(tr); + }) + .then(function () { + /*...*/ + }) + .catch(function (error) { + /*...*/ + }); +``` + +## now + +**knex.fn.now(precision)** + +Return the current timestamp with a precision (optional) + +```js +table.datetime('some_time', { precision: 6 }).defaultTo(knex.fn.now(6)); +``` + +## uuid + +**knex.fn.uuid()** + +Return a uuid generation function. Not supported by Redshift + +```js +table.uuid('uuid').defaultTo(knex.fn.uuid()); +``` + +## uuidToBin + +**knex.fn.uuidToBin(uuid)** + +Convert a string uuid (char(36)) to a binary uuid (binary(16)) + +```js +knex.schema.createTable('uuid_table', (t) => { + t.uuid('uuid_col_binary', { useBinaryUuid: true }); +}); +knex('uuid_table').insert({ + uuid_col_binary: knex.fn.uuidToBin('3f06af63-a93c-11e4-9797-00505690773f'), +}); +``` + +## binToUuid + +**knex.fn.binToUuid(binaryUuid)** + +Convert a binary uuid (binary(16)) to a string uuid (char(36)) + +```js +const res = await knex('uuid_table').select('uuid_col_binary'); +knex.fn.binToUuid(res[0].uuid_col_binary); +``` diff --git a/docs/src/index.md b/docs/src/index.md new file mode 100644 index 0000000000..2352596286 --- /dev/null +++ b/docs/src/index.md @@ -0,0 +1,20 @@ +--- +home: true +heroImage: /knex-logo.png +heroAlt: Logo knex +heroText: Knex.js +tagline: SQL query builder +actionText: View guide +actionLink: /guide/ +altActionText: Star on GitHub +altActionLink: https://github.com/knex/knex +title: SQL Query Builder for Javascript +--- + +
+ +**Knex.js** (pronounced [/kəˈnɛks/](https://youtu.be/19Av0Lxml-I?t=521)) is a "batteries included" SQL query builder for **PostgreSQL**, **CockroachDB**, **MSSQL**, **MySQL**, **MariaDB**, **SQLite3**, **Better-SQLite3**, **Oracle**, and **Amazon Redshift** designed to be flexible, portable, and fun to use. + +It features both traditional node style [callbacks](/guide/interfaces#callbacks) as well as a [promise](/guide/interfaces#promises) interface for cleaner async flow control, [a stream interface](/guide/interfaces#streams), full-featured [query](/guide/query-builder) and [schema](/guide/schema-builder) builders, [**transaction support (with savepoints)**](/guide/transactions), connection [pooling](/guide/#pool) and standardized responses between different query clients and dialects. + +
diff --git a/docs/src/public/knex-logo.png b/docs/src/public/knex-logo.png new file mode 100644 index 0000000000..eb2e902072 Binary files /dev/null and b/docs/src/public/knex-logo.png differ