knex 0.95.13 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/CHANGELOG.md +74 -3
  2. package/README.md +1 -1
  3. package/UPGRADING.md +7 -0
  4. package/lib/client.js +14 -1
  5. package/lib/constants.js +2 -0
  6. package/lib/dialects/better-sqlite3/index.js +72 -0
  7. package/lib/dialects/cockroachdb/crdb-querycompiler.js +87 -33
  8. package/lib/dialects/cockroachdb/crdb-tablecompiler.js +19 -0
  9. package/lib/dialects/cockroachdb/index.js +13 -0
  10. package/lib/dialects/mssql/index.js +0 -11
  11. package/lib/dialects/mssql/query/mssql-querycompiler.js +122 -64
  12. package/lib/dialects/mssql/schema/mssql-columncompiler.js +41 -6
  13. package/lib/dialects/mssql/schema/mssql-tablecompiler.js +24 -9
  14. package/lib/dialects/mssql/schema/mssql-viewcompiler.js +15 -1
  15. package/lib/dialects/mysql/index.js +3 -7
  16. package/lib/dialects/mysql/query/mysql-querycompiler.js +91 -5
  17. package/lib/dialects/mysql/schema/mysql-columncompiler.js +32 -5
  18. package/lib/dialects/mysql/schema/mysql-tablecompiler.js +28 -4
  19. package/lib/dialects/mysql2/index.js +7 -4
  20. package/lib/dialects/oracle/query/oracle-querycompiler.js +7 -6
  21. package/lib/dialects/oracle/schema/internal/trigger.js +1 -1
  22. package/lib/dialects/oracle/schema/oracle-columncompiler.js +10 -4
  23. package/lib/dialects/oracle/schema/oracle-tablecompiler.js +17 -6
  24. package/lib/dialects/oracledb/index.js +0 -4
  25. package/lib/dialects/oracledb/query/oracledb-querycompiler.js +104 -0
  26. package/lib/dialects/oracledb/schema/oracledb-columncompiler.js +23 -0
  27. package/lib/dialects/postgres/index.js +21 -6
  28. package/lib/dialects/postgres/query/pg-querybuilder.js +8 -0
  29. package/lib/dialects/postgres/query/pg-querycompiler.js +166 -5
  30. package/lib/dialects/postgres/schema/pg-columncompiler.js +24 -4
  31. package/lib/dialects/postgres/schema/pg-tablecompiler.js +55 -47
  32. package/lib/dialects/redshift/index.js +12 -0
  33. package/lib/dialects/redshift/query/redshift-querycompiler.js +62 -26
  34. package/lib/dialects/redshift/schema/redshift-columncompiler.js +2 -1
  35. package/lib/dialects/redshift/schema/redshift-tablecompiler.js +4 -1
  36. package/lib/dialects/sqlite3/index.js +18 -4
  37. package/lib/dialects/sqlite3/query/sqlite-querycompiler.js +85 -18
  38. package/lib/dialects/sqlite3/schema/ddl.js +274 -282
  39. package/lib/dialects/sqlite3/schema/internal/sqlite-ddl-operations.js +18 -8
  40. package/lib/dialects/sqlite3/schema/sqlite-columncompiler.js +20 -0
  41. package/lib/dialects/sqlite3/schema/sqlite-compiler.js +16 -12
  42. package/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js +15 -5
  43. package/lib/dialects/sqlite3/schema/sqlite-viewcompiler.js +31 -2
  44. package/lib/execution/runner.js +37 -2
  45. package/lib/knex-builder/FunctionHelper.js +21 -0
  46. package/lib/migrations/common/MigrationsLoader.js +36 -0
  47. package/lib/migrations/migrate/MigrationGenerator.js +1 -1
  48. package/lib/migrations/migrate/Migrator.js +20 -23
  49. package/lib/migrations/migrate/migration-list-resolver.js +2 -5
  50. package/lib/migrations/migrate/{configuration-merger.js → migrator-configuration-merger.js} +2 -4
  51. package/lib/migrations/migrate/sources/fs-migrations.js +4 -29
  52. package/lib/migrations/migrate/stub/js.stub +8 -1
  53. package/lib/migrations/migrate/stub/knexfile-js.stub +3 -0
  54. package/lib/migrations/migrate/stub/knexfile-ts.stub +5 -2
  55. package/lib/migrations/migrate/table-creator.js +6 -5
  56. package/lib/migrations/seed/Seeder.js +25 -92
  57. package/lib/migrations/seed/seeder-configuration-merger.js +60 -0
  58. package/lib/migrations/seed/sources/fs-seeds.js +65 -0
  59. package/lib/migrations/seed/stub/js.stub +4 -1
  60. package/lib/migrations/util/import-file.js +0 -1
  61. package/lib/query/joinclause.js +24 -5
  62. package/lib/query/method-constants.js +37 -0
  63. package/lib/query/querybuilder.js +230 -5
  64. package/lib/query/querycompiler.js +269 -84
  65. package/lib/schema/columnbuilder.js +8 -0
  66. package/lib/schema/columncompiler.js +132 -5
  67. package/lib/schema/compiler.js +1 -0
  68. package/lib/schema/tablebuilder.js +41 -8
  69. package/lib/schema/tablecompiler.js +57 -0
  70. package/lib/schema/viewcompiler.js +13 -10
  71. package/package.json +35 -22
  72. package/scripts/docker-compose.yml +7 -7
  73. package/scripts/oracledb-install-driver-libs.sh +82 -0
  74. package/scripts/runkit-example.js +1 -1
  75. package/scripts/stress-test/docker-compose.yml +3 -3
  76. package/scripts/stress-test/knex-stress-test.js +1 -1
  77. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +1 -1
  78. package/types/index.d.ts +124 -20
@@ -246,7 +246,6 @@ class Builder extends EventEmitter {
246
246
 
247
247
  // Adds a join clause to the query, allowing for advanced joins
248
248
  // with an anonymous function as the second argument.
249
- // function(table, first, operator, second)
250
249
  join(table, first, ...args) {
251
250
  let join;
252
251
  const schema =
@@ -269,6 +268,12 @@ class Builder extends EventEmitter {
269
268
  return this;
270
269
  }
271
270
 
271
+ using(tables) {
272
+ throw new Error(
273
+ "'using' function is only available in PostgreSQL dialect with Delete statements."
274
+ );
275
+ }
276
+
272
277
  // JOIN blocks:
273
278
  innerJoin(...args) {
274
279
  return this._joinType('inner').join(...args);
@@ -429,7 +434,7 @@ class Builder extends EventEmitter {
429
434
 
430
435
  // Adds an `not where` clause to the query.
431
436
  whereNot(column, ...args) {
432
- if (args.length >= 1) {
437
+ if (args.length >= 2) {
433
438
  if (args[0] === 'in' || args[0] === 'between') {
434
439
  this.client.logger.warn(
435
440
  'whereNot is not suitable for "in" and "between" type subqueries. You should use "not in" and "not between" instead.'
@@ -611,6 +616,29 @@ class Builder extends EventEmitter {
611
616
  return this._bool('or').whereNotBetween(column, values);
612
617
  }
613
618
 
619
+ _whereLike(type, column, value) {
620
+ this._statements.push({
621
+ grouping: 'where',
622
+ type: type,
623
+ column,
624
+ value: value,
625
+ not: this._not(),
626
+ bool: this._bool(),
627
+ asColumn: this._asColumnFlag,
628
+ });
629
+ return this;
630
+ }
631
+
632
+ // Adds a `where like` clause to the query.
633
+ whereLike(column, value) {
634
+ return this._whereLike('whereLike', column, value);
635
+ }
636
+
637
+ // Adds a `where ilike` clause to the query.
638
+ whereILike(column, value) {
639
+ return this._whereLike('whereILike', column, value);
640
+ }
641
+
614
642
  // Adds a `group by` clause to the query.
615
643
  groupBy(item) {
616
644
  if (item && item.isRawInstance) {
@@ -920,8 +948,18 @@ class Builder extends EventEmitter {
920
948
  return this._bool('or').havingRaw(sql, bindings);
921
949
  }
922
950
 
951
+ // set the skip binding parameter (= insert the raw value in the query) for an attribute.
952
+ _setSkipBinding(attribute, options) {
953
+ let skipBinding = options;
954
+ if (isObject(options)) {
955
+ skipBinding = options.skipBinding;
956
+ }
957
+ this._single.skipBinding = this._single.skipBinding || {};
958
+ this._single.skipBinding[attribute] = skipBinding;
959
+ }
960
+
923
961
  // Only allow a single "offset" to be set for the current query.
924
- offset(value) {
962
+ offset(value, options) {
925
963
  if (value == null || value.isRawInstance || value instanceof Builder) {
926
964
  // Builder for backward compatibility
927
965
  this._single.offset = value;
@@ -935,16 +973,18 @@ class Builder extends EventEmitter {
935
973
  this._single.offset = val;
936
974
  }
937
975
  }
976
+ this._setSkipBinding('offset', options);
938
977
  return this;
939
978
  }
940
979
 
941
980
  // Only allow a single "limit" to be set for the current query.
942
- limit(value) {
981
+ limit(value, options) {
943
982
  const val = parseInt(value, 10);
944
983
  if (isNaN(val)) {
945
984
  this.client.logger.warn('A valid integer must be provided to limit');
946
985
  } else {
947
986
  this._single.limit = val;
987
+ this._setSkipBinding('limit', options);
948
988
  }
949
989
  return this;
950
990
  }
@@ -1192,7 +1232,11 @@ class Builder extends EventEmitter {
1192
1232
  // Set a lock for update constraint.
1193
1233
  forUpdate(...tables) {
1194
1234
  this._single.lock = lockMode.forUpdate;
1195
- this._single.lockTables = tables;
1235
+ if (tables.length === 1 && Array.isArray(tables[0])) {
1236
+ this._single.lockTables = tables[0];
1237
+ } else {
1238
+ this._single.lockTables = tables;
1239
+ }
1196
1240
  return this;
1197
1241
  }
1198
1242
 
@@ -1266,6 +1310,11 @@ class Builder extends EventEmitter {
1266
1310
  return this;
1267
1311
  }
1268
1312
 
1313
+ fromRaw(sql, bindings) {
1314
+ const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
1315
+ return this.from(raw);
1316
+ }
1317
+
1269
1318
  // Passes query to provided callback function, useful for e.g. composing
1270
1319
  // domain-specific helpers
1271
1320
  modify(callback) {
@@ -1279,6 +1328,179 @@ class Builder extends EventEmitter {
1279
1328
  );
1280
1329
  }
1281
1330
 
1331
+ // JSON support functions
1332
+ _json(nameFunction, params) {
1333
+ this._statements.push({
1334
+ grouping: 'columns',
1335
+ type: 'json',
1336
+ method: nameFunction,
1337
+ params: params,
1338
+ });
1339
+ return this;
1340
+ }
1341
+
1342
+ jsonExtract() {
1343
+ const column = arguments[0];
1344
+ let path;
1345
+ let alias;
1346
+ let singleValue = true;
1347
+
1348
+ // We use arguments to have the signatures :
1349
+ // - column (string or array)
1350
+ // - column + path
1351
+ // - column + path + alias
1352
+ // - column + path + alias + singleValue
1353
+ // - column array + singleValue
1354
+ if (arguments.length >= 2) {
1355
+ path = arguments[1];
1356
+ }
1357
+ if (arguments.length >= 3) {
1358
+ alias = arguments[2];
1359
+ }
1360
+ if (arguments.length === 4) {
1361
+ singleValue = arguments[3];
1362
+ }
1363
+ if (
1364
+ arguments.length === 2 &&
1365
+ Array.isArray(arguments[0]) &&
1366
+ isBoolean(arguments[1])
1367
+ ) {
1368
+ singleValue = arguments[1];
1369
+ }
1370
+ return this._json('jsonExtract', {
1371
+ column: column,
1372
+ path: path,
1373
+ alias: alias,
1374
+ singleValue, // boolean used only in MSSQL to use function for extract value instead of object/array.
1375
+ });
1376
+ }
1377
+
1378
+ jsonSet(column, path, value, alias) {
1379
+ return this._json('jsonSet', {
1380
+ column: column,
1381
+ path: path,
1382
+ value: value,
1383
+ alias: alias,
1384
+ });
1385
+ }
1386
+
1387
+ jsonInsert(column, path, value, alias) {
1388
+ return this._json('jsonInsert', {
1389
+ column: column,
1390
+ path: path,
1391
+ value: value,
1392
+ alias: alias,
1393
+ });
1394
+ }
1395
+
1396
+ jsonRemove(column, path, alias) {
1397
+ return this._json('jsonRemove', {
1398
+ column: column,
1399
+ path: path,
1400
+ alias: alias,
1401
+ });
1402
+ }
1403
+
1404
+ // Wheres for JSON
1405
+ _isJsonObject(jsonValue) {
1406
+ return isObject(jsonValue) && !(jsonValue instanceof Builder);
1407
+ }
1408
+
1409
+ _whereJsonWrappedValue(type, column, value) {
1410
+ const whereJsonClause = {
1411
+ grouping: 'where',
1412
+ type: type,
1413
+ column,
1414
+ value: value,
1415
+ not: this._not(),
1416
+ bool: this._bool(),
1417
+ asColumn: this._asColumnFlag,
1418
+ };
1419
+ if (arguments[3]) {
1420
+ whereJsonClause.operator = arguments[3];
1421
+ }
1422
+ if (arguments[4]) {
1423
+ whereJsonClause.jsonPath = arguments[4];
1424
+ }
1425
+ this._statements.push(whereJsonClause);
1426
+ }
1427
+
1428
+ whereJsonObject(column, value) {
1429
+ this._whereJsonWrappedValue('whereJsonObject', column, value);
1430
+ return this;
1431
+ }
1432
+
1433
+ orWhereJsonObject(column, operator, value) {
1434
+ return this._bool('or').whereJsonObject(column, operator, value);
1435
+ }
1436
+
1437
+ whereNotJsonObject(column, value) {
1438
+ this._not(true)._whereJsonWrappedValue('whereJsonObject', column, value);
1439
+ return this;
1440
+ }
1441
+
1442
+ orWhereNotJsonObject(column, operator, value) {
1443
+ return this._not(true)._bool('or').whereJsonObject(column, operator, value);
1444
+ }
1445
+
1446
+ whereJsonPath(column, path, operator, value) {
1447
+ this._whereJsonWrappedValue('whereJsonPath', column, value, operator, path);
1448
+ return this;
1449
+ }
1450
+
1451
+ orWhereJsonPath(column, operator, value) {
1452
+ return this._bool('or').whereJsonPath(column, operator, value);
1453
+ }
1454
+
1455
+ // Json superset wheres
1456
+ whereJsonSupersetOf(column, value) {
1457
+ this._whereJsonWrappedValue('whereJsonSupersetOf', column, value);
1458
+ return this;
1459
+ }
1460
+
1461
+ whereJsonNotSupersetOf(column, value) {
1462
+ this._not(true).whereJsonSupersetOf(column, value);
1463
+ return this;
1464
+ }
1465
+
1466
+ orWhereJsonSupersetOf(column, value) {
1467
+ this._whereJsonWrappedValue('whereJsonSupersetOf', column, value);
1468
+ return this;
1469
+ }
1470
+
1471
+ orWhereJsonNotSupersetOf(column, value) {
1472
+ this._not(true)._bool('or').whereJsonSupersetOf(column, value);
1473
+ return this;
1474
+ }
1475
+
1476
+ // Json subset wheres
1477
+ whereJsonSubsetOf(column, value) {
1478
+ this._whereJsonWrappedValue('whereJsonSubsetOf', column, value);
1479
+ return this;
1480
+ }
1481
+
1482
+ whereJsonNotSubsetOf(column, value) {
1483
+ this._not(true).whereJsonSubsetOf(column, value);
1484
+ return this;
1485
+ }
1486
+
1487
+ orWhereJsonSubsetOf(column, value) {
1488
+ this._whereJsonWrappedValue('whereJsonSubsetOf', column, value);
1489
+ return this;
1490
+ }
1491
+
1492
+ orWhereJsonNotSubsetOf(column, value) {
1493
+ this._not(true)._bool('or').whereJsonSubsetOf(column, value);
1494
+ return this;
1495
+ }
1496
+
1497
+ whereJsonHasNone(column, values) {
1498
+ this._not(true).whereJsonHasAll(column, values);
1499
+ return this;
1500
+ }
1501
+
1502
+ // end of wheres for JSON
1503
+
1282
1504
  _analytic(alias, second, third) {
1283
1505
  let analytic;
1284
1506
  const { schema } = this._single;
@@ -1486,6 +1708,9 @@ Builder.prototype.andWhereColumn = Builder.prototype.whereColumn;
1486
1708
  Builder.prototype.andWhereRaw = Builder.prototype.whereRaw;
1487
1709
  Builder.prototype.andWhereBetween = Builder.prototype.whereBetween;
1488
1710
  Builder.prototype.andWhereNotBetween = Builder.prototype.whereNotBetween;
1711
+ Builder.prototype.andWhereJsonObject = Builder.prototype.whereJsonObject;
1712
+ Builder.prototype.andWhereNotJsonObject = Builder.prototype.whereJsonObject;
1713
+ Builder.prototype.andWhereJsonPath = Builder.prototype.whereJsonPath;
1489
1714
  Builder.prototype.andHaving = Builder.prototype.having;
1490
1715
  Builder.prototype.andHavingIn = Builder.prototype.havingIn;
1491
1716
  Builder.prototype.andHavingNotIn = Builder.prototype.havingNotIn;