Repository: knex/knex
Pull request #5174 (2022-05-12 08:22:11)

Use correct paths in package.json browser field
The seeder entry is the only one really required to easily build knex for
the browser without shimming `fs`, but I updated the migrations path to
match.

However, there's also `lib/migrations/seed/seed-stub.js`, which actually
seems to provide errors when attempting to use the seed functionality in
a browser, but isn't actually used anywhere

I'm not exactly sure which way is intended.

If the seed stub is intended, then I'll change it to use that, otherwise it should probably be removed.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/49055410/badge)](https://coveralls.io/builds/49055410)

Coverage remained the same at 92.282% when pulling **cd3a7cc5f97ff9055f6abea76a6b511ed82aadc2 on jrobeson:fix-browser-package-json** into **e2516cd058df984410c113cafdd8808b3ca97cc7 on knex:master**.


Patch:
diff --git a/.gitignore b/.gitignore
index b0d47e0f..4cc39126 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,7 @@ node_modules
 test.sqlite3
 npm-debug.log
 tmp
+knex-*.tgz
 
 # Testing for the knex cli:
 bin/migrations-folder
diff --git a/.husky/post-checkout b/.husky/post-checkout
new file mode 100755
index 00000000..9b3d2e81
--- /dev/null
+++ b/.husky/post-checkout
@@ -0,0 +1,4 @@
+#!/bin/sh
+. "$(dirname "$0")/_/husky.sh"
+
+npm run clean && npm run build
diff --git a/.husky/pre-commit b/.husky/pre-commit
old mode 100644
new mode 100755
index 36af2198..d24fdfc6
--- a/.husky/pre-commit
+++ b/.husky/pre-commit
@@ -1,4 +1,4 @@
-#!/bin/sh
-. "$(dirname "$0")/_/husky.sh"
+#!/usr/bin/env sh
+. "$(dirname -- "$0")/_/husky.sh"
 
 npx lint-staged
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 1c146567..634539df 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -171,11 +171,11 @@ psql -h localhost -U postgres -d knex_test
 
 ## Typescript source files
 
-> TL;DR: Starting with release 2.0.0 Knex is adding support for Typescript source files. Thus to develop in this repo you will need to run `npm run build` each time you edit `.ts` files to generate the resulting `.js` files. This is automatically run whenever you run `npm install` so when developing in Javascript you don't have to worry about it. It is encouraged that new functionality and sources be written in Typescript but this is not required.
+> TL;DR: Starting with release 2.0.0 Knex is adding support for Typescript source files. Thus to develop in this repo you will need to run `npm run build` each time you edit `.ts` files to generate the resulting `.js` files. This is automatically run whenever you run `npm install` or checkout a new Git branch so when developing in Javascript you don't have to worry about it. It is encouraged that new functionality and sources be written in Typescript but this is not required.
 
 Starting with release 2.0.0, Knex is support source additions in Typescript! This allows for better safety in the code added. However, pre-2.0.0 Knex was always written in pure Javascript and thus a "hybrid" approach is being used for 2.0.0 to allow for the new `.ts` files to exist along `.js` files that make up the majority of this repository.
 
-To develop in this repository use the `npm run build` and `npm run clean` commands to compile and delete the `.js` and related files from `.ts` files. If you wish to have the `tsc` compiled watch and recompile on changes then run `npm run build:ts -- --watch`. Note that for easy integration with Javascript the outputted files are done in a "side-by-side" manner meaning that `lib/foo/bar.ts` will result in `lib/foo/bar.js`. This is done automatically via the npm `postinstall` script trigger which executes when you run commands like `npm install` or `npm ci`, thus making it easier to not have to worry about this if you're working in pure Javascript.
+To develop in this repository use the `npm run build` and `npm run clean` commands to compile and delete the `.js` and related files from `.ts` files. If you wish to have the `tsc` compiled watch and recompile on changes then run `npm run build:ts -- --watch`. Note that for easy integration with Javascript the outputted files are done in a "side-by-side" manner meaning that `lib/foo/bar.ts` will result in `lib/foo/bar.js`. This is done automatically via the npm script command `"prepare"` whenever you run `npm install` and Git hook for `post-checkout` (added by Husky) which executes when you run commands like `git checkout` , thus making it easier to not have to worry about this if you're working in pure Javascript.
 
 The script file `./scripts/update_gitignore_for_tsc_output.js` file is called as part of the `npm run build` command which will update the `lib/.gitignore` file which is used to ensure generated `.js` and related files from `tsc` compilation are not checked into the git repo.
 
diff --git a/lib/dialects/oracledb/index.js b/lib/dialects/oracledb/index.js
index cba691d8..a0444209 100644
--- a/lib/dialects/oracledb/index.js
+++ b/lib/dialects/oracledb/index.js
@@ -111,7 +111,8 @@ class Client_Oracledb extends Client_Oracle {
         this
       );
     } else if (value instanceof BlobHelper) {
-      return 'EMPTY_BLOB()';
+      formatter.bindings.push(value.value);
+      return '?';
     }
     return unwrapRaw(value, true, builder, this, formatter) || '?';
   }
diff --git a/lib/knex-builder/Knex.js b/lib/knex-builder/Knex.js
index fd59b6d1..3f89233e 100644
--- a/lib/knex-builder/Knex.js
+++ b/lib/knex-builder/Knex.js
@@ -5,6 +5,10 @@ const QueryInterface = require('../query/method-constants');
 const makeKnex = require('./make-knex');
 const { KnexTimeoutError } = require('../util/timeout');
 const { resolveConfig } = require('./internal/config-resolver');
+const SchemaBuilder = require('../schema/builder');
+const ViewBuilder = require('../schema/viewbuilder');
+const ColumnBuilder = require('../schema/columnbuilder');
+const TableBuilder = require('../schema/tablebuilder');
 
 function knex(config) {
   const { resolvedConfig, Dialect } = resolveConfig(...arguments);
@@ -28,4 +32,28 @@ knex.QueryBuilder = {
   },
 };
 
+knex.SchemaBuilder = {
+  extend: function (methodName, fn) {
+    SchemaBuilder.extend(methodName, fn);
+  },
+};
+
+knex.ViewBuilder = {
+  extend: function (methodName, fn) {
+    ViewBuilder.extend(methodName, fn);
+  },
+};
+
+knex.ColumnBuilder = {
+  extend: function (methodName, fn) {
+    ColumnBuilder.extend(methodName, fn);
+  },
+};
+
+knex.TableBuilder  = {
+  extend: function (methodName, fn) {
+    TableBuilder.extend(methodName, fn);
+  },
+};
+
 module.exports = knex;
diff --git a/lib/migrations/migrate/MigrationGenerator.js b/lib/migrations/migrate/MigrationGenerator.js
index a33d55bd..979510a2 100644
--- a/lib/migrations/migrate/MigrationGenerator.js
+++ b/lib/migrations/migrate/MigrationGenerator.js
@@ -41,7 +41,7 @@ class MigrationGenerator {
 
   _getNewMigrationName(name) {
     if (name[0] === '-') name = name.slice(1);
-    return yyyymmddhhmmss() + '_' + name + '.' + this.config.extension;
+    return yyyymmddhhmmss() + '_' + name + '.' + this.config.extension.split('-')[0];
   }
 
   _getNewMigrationPath(name) {
diff --git a/lib/migrations/migrate/stub/js-schema.stub b/lib/migrations/migrate/stub/js-schema.stub
new file mode 100644
index 00000000..bf8d2743
--- /dev/null
+++ b/lib/migrations/migrate/stub/js-schema.stub
@@ -0,0 +1,22 @@
+/**
+ * @param { import("knex").Knex } knex
+ * @returns { Promise<void> }
+ */
+exports.up = function({schema}) {
+  <% if (d.tableName) { %>
+  return schema.createTable("<%= d.tableName %>", function(t) {
+    t.increments();
+    t.timestamp();
+  });
+  <% } %>
+};
+
+/**
+ * @param { import("knex").Knex } knex
+ * @returns { Promise<void> }
+ */
+exports.down = function({schema}) {
+  <% if (d.tableName) { %>
+  return schema.dropTable("<%= d.tableName %>");
+  <% } %>
+};
diff --git a/lib/migrations/migrate/stub/ts-schema.stub b/lib/migrations/migrate/stub/ts-schema.stub
new file mode 100644
index 00000000..850d1bfc
--- /dev/null
+++ b/lib/migrations/migrate/stub/ts-schema.stub
@@ -0,0 +1,21 @@
+import { Knex } from "knex";
+
+<% if (d.tableName) { %>
+export async function up({schema}: Knex): Promise<Knex.SchemaBuilder> {
+    return schema.createTable("<%= d.tableName %>", (t) => {
+        t.increments();
+        t.timestamps();
+    });
+}
+<% } else { %>
+export async function up({schema}: Knex): Promise<void> {
+}
+<% } %>
+<% if (d.tableName) { %>
+export async function down({schema}: Knex): Promise<Knex.SchemaBuilder> {
+    return schema.dropTable("<%= d.tableName %>");
+}
+<% } else { %>
+export async function down({schema}: Knex): Promise<void> {
+}
+<% } %>
diff --git a/lib/schema/builder.js b/lib/schema/builder.js
index f9a38e97..fc4fa959 100644
--- a/lib/schema/builder.js
+++ b/lib/schema/builder.js
@@ -1,5 +1,6 @@
 const { EventEmitter } = require('events');
 const toArray = require('lodash/toArray');
+const assign = require('lodash/assign');
 const { addQueryContext } = require('../util/helpers');
 const saveAsyncStack = require('../util/save-async-stack');
 const {
@@ -96,6 +97,17 @@ class SchemaBuilder extends EventEmitter {
   };
 });
 
+
+SchemaBuilder.extend = (methodName, fn) => {
+  if (Object.prototype.hasOwnProperty.call(SchemaBuilder.prototype, methodName)) {
+    throw new Error(
+      `Can't extend SchemaBuilder with existing method ('${methodName}').`
+    );
+  }
+
+  assign(SchemaBuilder.prototype, { [methodName]: fn });
+};
+
 augmentWithBuilderInterface(SchemaBuilder);
 addQueryContext(SchemaBuilder);
 
diff --git a/lib/schema/columnbuilder.js b/lib/schema/columnbuilder.js
index 685872c0..d520644c 100644
--- a/lib/schema/columnbuilder.js
+++ b/lib/schema/columnbuilder.js
@@ -1,4 +1,5 @@
 const extend = require('lodash/extend');
+const assign = require('lodash/assign');
 const toArray = require('lodash/toArray');
 const { addQueryContext } = require('../util/helpers');
 
@@ -87,6 +88,17 @@ ColumnBuilder.prototype.notNull = ColumnBuilder.prototype.notNullable =
   };
 });
 
+
+ColumnBuilder.extend = (methodName, fn) => {
+  if (Object.prototype.hasOwnProperty.call(ColumnBuilder.prototype, methodName)) {
+    throw new Error(
+      `Can't extend ColumnBuilder with existing method ('${methodName}').`
+    );
+  }
+
+  assign(ColumnBuilder.prototype, { [methodName]: fn });
+};
+
 const AlterMethods = {};
 
 // Specify that the column is to be dropped. This takes precedence
diff --git a/lib/schema/tablebuilder.js b/lib/schema/tablebuilder.js
index b52c031e..953b09cc 100644
--- a/lib/schema/tablebuilder.js
+++ b/lib/schema/tablebuilder.js
@@ -8,6 +8,7 @@
 // ------
 const each = require('lodash/each');
 const extend = require('lodash/extend');
+const assign = require('lodash/assign');
 const toArray = require('lodash/toArray');
 const helpers = require('../util/helpers');
 const { isString, isFunction, isObject } = require('../util/is');
@@ -360,4 +361,15 @@ AlterMethods.dropColumn = AlterMethods.dropColumns = function () {
   return this;
 };
 
+
+TableBuilder.extend = (methodName, fn) => {
+  if (Object.prototype.hasOwnProperty.call(TableBuilder.prototype, methodName)) {
+    throw new Error(
+      `Can't extend TableBuilder with existing method ('${methodName}').`
+    );
+  }
+
+  assign(TableBuilder.prototype, { [methodName]: fn });
+};
+
 module.exports = TableBuilder;
diff --git a/lib/schema/viewbuilder.js b/lib/schema/viewbuilder.js
index f33e7067..dd7e0e99 100644
--- a/lib/schema/viewbuilder.js
+++ b/lib/schema/viewbuilder.js
@@ -1,5 +1,6 @@
 const helpers = require('../util/helpers');
 const extend = require('lodash/extend');
+const assign = require('lodash/assign');
 
 class ViewBuilder {
   constructor(client, method, viewName, fn) {
@@ -78,4 +79,15 @@ const AlterMethods = {
 
 helpers.addQueryContext(ViewBuilder);
 
+
+ViewBuilder.extend = (methodName, fn) => {
+  if (Object.prototype.hasOwnProperty.call(ViewBuilder.prototype, methodName)) {
+    throw new Error(
+      `Can't extend ViewBuilder with existing method ('${methodName}').`
+    );
+  }
+
+  assign(ViewBuilder.prototype, { [methodName]: fn });
+};
+
 module.exports = ViewBuilder;
diff --git a/package.json b/package.json
index 1189e333..1d5032df 100644
--- a/package.json
+++ b/package.json
@@ -9,9 +9,9 @@
   },
   "scripts": {
     "build": "npm run build:gitignore && npm run build:ts",
-    "clean": "git clean -f -X lib/",
+    "clean": "node scripts/clean.js",
     "build:ts": "tsc",
-    "build:gitignore": "./scripts/update_gitignore_for_tsc_output.js run",
+    "build:gitignore": "node scripts/update_gitignore_for_tsc_output.js run",
     "format": "prettier --write \"{lib,bin,scripts,test}/**/*.js\"",
     "debug:test": "mocha --inspect-brk --exit -t 0 test/all-tests-suite.js",
     "debug:tape": "node --inspect-brk test/tape/index.js",
@@ -56,9 +56,7 @@
     "stress:init": "docker-compose -f scripts/stress-test/docker-compose.yml up --no-start && docker-compose -f scripts/stress-test/docker-compose.yml start",
     "stress:test": "node scripts/stress-test/knex-stress-test.js | grep -A 5 -B 60 -- '- STATS '",
     "stress:destroy": "docker-compose -f scripts/stress-test/docker-compose.yml stop",
-    "prepare": "husky install",
-    "prepublishOnly": "npm run clean && npm run build",
-    "postinstall": "npm run clean && npm run build"
+    "prepare": "husky install && npm run clean && npm run build"
   },
   "dependencies": {
     "colorette": "2.0.16",
@@ -101,8 +99,7 @@
   },
   "lint-staged": {
     "*.{js,json}": [
-      "prettier --write",
-      "git add"
+      "prettier --write"
     ]
   },
   "devDependencies": {
@@ -120,7 +117,7 @@
     "eslint-config-prettier": "^8.5.0",
     "eslint-plugin-import": "^2.26.0",
     "eslint-plugin-mocha-no-only": "^1.1.1",
-    "husky": "^7.0.4",
+    "husky": "^8.0.1",
     "jake": "^10.8.5",
     "JSONStream": "^1.3.5",
     "lint-staged": "^12.3.7",
@@ -201,9 +198,9 @@
     }
   ],
   "browser": {
-    "./lib/migrate/Migrator.js": "./lib/util/noop.js",
+    "./lib/migrations/migrate/Migrator.js": "./lib/util/noop.js",
     "./lib/bin/cli.js": "./lib/util/noop.js",
-    "./lib/seed/Seeder.js": "./lib/util/noop.js",
+    "./lib/migrations/seed/Seeder.js": "./lib/util/noop.js",
     "tedious": false,
     "mysql": false,
     "mysql2": false,
@@ -221,7 +218,8 @@
   },
   "files": [
     "bin/*",
-    "lib/*",
+    "lib/",
+    "lib/**/*.js",
     "!lib/**/*.ts",
     "!lib/**/*.d.ts",
     "!lib/**/*.js.map",
diff --git a/scripts/clean.js b/scripts/clean.js
new file mode 100755
index 00000000..fa1b7884
--- /dev/null
+++ b/scripts/clean.js
@@ -0,0 +1,29 @@
+#!/usr/bin/env node
+
+const fs = require('fs')
+const path = require('path')
+const { execSync } = require("child_process");
+
+function main() {
+  const repoDir = path.dirname(__dirname)
+  const gitDir = path.join(repoDir, '.git')
+  const gitDirExists = doesDirectoryExist(gitDir)
+  if (!gitDirExists) {
+    console.log("No .git directory detected so can not clean 'lib/'. Exiting.")
+    process.exit(0)
+  }
+  console.log("Cleaning 'lib/' of outputted files from Typescript compilation ...")
+  const cmd = 'git clean -f -X lib/'
+  const output = execSync(cmd, { cwd: repoDir })
+  console.log(output.toString('utf8'))
+  console.log('Done')
+}
+
+function doesDirectoryExist(p) {
+  if (fs.existsSync(p)) {
+    return fs.lstatSync(p).isDirectory()
+  }
+  return false
+}
+
+main()
diff --git a/types/index.d.ts b/types/index.d.ts
index 1c999f53..98bed908 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -384,6 +384,43 @@ export declare namespace knex {
     ): void;
   }
 
+  class TableBuilder {
+    static extend(
+      methodName: string,
+      fn: (
+        this: Knex.TableBuilder,
+        ...args: any[]
+      ) => Knex.TableBuilder
+    ): void;
+  }
+  class ViewBuilder {
+    static extend(
+      methodName: string,
+      fn: (
+        this: Knex.ViewBuilder,
+        ...args: any[]
+      ) => Knex.ViewBuilder
+    ): void;
+  }
+  class SchemaBuilder {
+    static extend(
+      methodName: string,
+      fn: (
+        this: Knex.SchemaBuilder,
+        ...args: any[]
+      ) => Knex.SchemaBuilder
+    ): void;
+  }
+  class ColumnBuilder {
+    static extend(
+      methodName: string,
+      fn: (
+        this: Knex.ColumnBuilder,
+        ...args: any[]
+      ) => Knex.ColumnBuilder
+    ): void;
+  }
+
   export class KnexTimeoutError extends Error {}
 
   export const Client: typeof Knex.Client;


Test patch:
diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml
index f1bab091..c38c7637 100644
--- a/.github/workflows/integration-tests.yml
+++ b/.github/workflows/integration-tests.yml
@@ -91,3 +91,29 @@ jobs:
             --file "scripts/docker-compose.yml" \
             down
         if: matrix.database-type != 'sqlite3' && matrix.database-type != 'better-sqlite3'
+
+  user_experience:
+    runs-on: ${{ matrix.os }}
+    name: Test user experience
+    strategy:
+      matrix:
+        node-version: [18.x, 16.x, 14.x, 12.x]
+        os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
+    steps:
+      - name: Checkout Repository
+        uses: actions/checkout@v3
+        with: { fetch-depth: 1 }
+
+      - name: Setup Node ${{ matrix.node-version }}
+        uses: actions/setup-node@v3
+        with:
+          always-auth: false
+          node-version: ${{ matrix.node-version }}
+      
+      - name: Test npm git dependency
+        shell: bash
+        run: |
+          mkdir TMP/
+          echo "{ \"dependencies\": { \"knex\": \"git+file://$(pwd)/\" } }" > TMP/package.json
+          cd TMP/
+          npm install --verbose
diff --git a/test/db-less-test-suite.js b/test/db-less-test-suite.js
index ed5a5935..5dd92807 100644
--- a/test/db-less-test-suite.js
+++ b/test/db-less-test-suite.js
@@ -20,6 +20,7 @@ describe('Query Building Tests', function () {
   require('./unit/query/string');
   require('./unit/schema-builder/mysql')('mysql');
   require('./unit/schema-builder/mysql')('mysql2');
+  require('./unit/schema-builder/extensions');
   require('./unit/schema-builder/postgres');
   require('./unit/schema-builder/redshift');
   require('./unit/schema-builder/sqlite3');
diff --git a/test/integration2/dialects/oracledb.spec.js b/test/integration2/dialects/oracledb.spec.js
new file mode 100644
index 00000000..8d747715
--- /dev/null
+++ b/test/integration2/dialects/oracledb.spec.js
@@ -0,0 +1,46 @@
+const { expect } = require('chai');
+const { getAllDbs, getKnexForDb } = require('../util/knex-instance-provider');
+
+describe('Oracledb dialect', () => {
+  describe('Connection configuration', () => {
+    const dbs = getAllDbs().filter((db) => db === 'oracledb');
+
+    dbs.forEach((db) => {
+      describe(db, () => {
+        let knex;
+        before(() => {
+          knex = getKnexForDb(db, {
+            connection: {
+              user: 'user',
+              password: 'password',
+              connectString: 'connect-string',
+              externalAuth: true,
+              host: 'host',
+              database: 'database',
+            },
+          });
+        });
+
+        after(() => {
+          return knex.destroy();
+        });
+
+        describe('#4869 inserting Buffer', async () => {
+          it('.toSQL().toNative() generates correct sql and bindings for INSERT of Buffer', async () => {
+            const b = Buffer.from('hello', 'utf-8')
+            const query = knex('table1').insert({ value: b })
+            const queryObj = query.toSQL().toNative()
+            // Ensure we have two bindings, before fix for #4869 there would only have been one due
+            // to silent dropping of the Buffer.
+            expect(queryObj.bindings.length).to.eql(2)
+            expect(queryObj).to.eql({
+              sql: 'insert into "table1" ("value") values (:1) returning "value" into :2',
+              bindings: [b, { type: 2019, dir: 3003 }],
+            });
+          });
+        });
+
+      });
+    });
+  });
+});
diff --git a/test/unit/schema-builder/extensions.js b/test/unit/schema-builder/extensions.js
new file mode 100644
index 00000000..d300b3dd
--- /dev/null
+++ b/test/unit/schema-builder/extensions.js
@@ -0,0 +1,52 @@
+'use strict';
+
+const expect = require('chai').expect;
+const MySQL_Client = require('../../../lib/dialects/mysql');
+const knex = require('../../../knex');
+describe('SchemaBuilder Extensions', () => {
+  const client = new MySQL_Client({ client: 'mysql' });
+
+  const equal = require('assert').equal;
+  const functionToExtend = () => true;
+  knex.SchemaBuilder.extend('tryCreateTable', functionToExtend);
+  knex.TableBuilder.extend('testCol', functionToExtend);
+  knex.ColumnBuilder.extend('testCol', functionToExtend);
+  knex.ViewBuilder.extend('testCol', functionToExtend);
+
+  it('schemabuilder has method', () => {
+    equal(client.schemaBuilder().tryCreateTable, functionToExtend);
+    expect(client.schemaBuilder().tryCreateTable()).to.be.true;
+  });
+
+  it('tablebuilder has method', () => {
+    client
+      .schemaBuilder()
+      .createTable('users', function (table) {
+        equal(table.testCol, functionToExtend);
+      })
+      .toSQL();
+  });
+
+  it('columnbuilder has method', () => {
+    client
+      .schemaBuilder()
+      .createTable('users', function (table) {
+        equal(table.string('t').testCol, functionToExtend);
+      })
+      .toSQL();
+  });
+
+  it('viewbuilder has method', () => {
+    client
+      .schemaBuilder()
+      .createView('users', function (view) {
+        equal(view.testCol, functionToExtend);
+        view.as(
+          knex('users')
+            .join('user_roles', 'users.user_id', 'user_roles.user_id')
+            .select('users.*', 'user_roles.role_code')
+        );
+      })
+      .toSQL();
+  });
+});