table-name.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2015-present, Vitaly Tomilov
  3. *
  4. * See the LICENSE file at the top-level directory of this distribution
  5. * for licensing information.
  6. *
  7. * Removal or modification of this copyright notice is prohibited.
  8. */
  9. const {assert} = require('../assert');
  10. const npm = {
  11. utils: require('../utils'),
  12. format: require('../formatting').as // formatting namespace
  13. };
  14. /**
  15. * @class helpers.TableName
  16. * @description
  17. * Represents a full table name that can be injected into queries directly.
  18. *
  19. * This is a read-only type that can be used wherever parameter `table` is supported.
  20. *
  21. * It supports $[Custom Type Formatting], which means you can use the type directly as a formatting
  22. * parameter, without specifying any escaping.
  23. *
  24. * Filter `:alias` is an alternative approach to splitting an SQL name into multiple ones.
  25. *
  26. * @param {string|object} table
  27. * Table name details, depending on the type:
  28. *
  29. * - table name, if `table` is a string
  30. * - object `{table, [schema]}`
  31. *
  32. * @property {string} name
  33. * Formatted/escaped full table name, combining `schema` + `table`.
  34. *
  35. * @property {string} table
  36. * Table name.
  37. *
  38. * @property {string} schema
  39. * Database schema name.
  40. *
  41. * It is `undefined` when no valid schema was specified.
  42. *
  43. * @returns {helpers.TableName}
  44. *
  45. * @see
  46. * {@link helpers.TableName#toPostgres toPostgres}
  47. *
  48. * @example
  49. *
  50. * const table = new pgp.helpers.TableName({table: 'my-table', schema: 'my-schema'});
  51. * console.log(table);
  52. * //=> "my-schema"."my-table"
  53. *
  54. * // Formatting the type directly:
  55. * pgp.as.format('SELECT * FROM $1', table);
  56. * //=> SELECT * FROM "my-schema"."my-table"
  57. *
  58. */
  59. class TableName {
  60. constructor(table) {
  61. if (typeof table === 'string') {
  62. this.table = table;
  63. } else {
  64. const config = assert(table, ['table', 'schema']);
  65. this.table = config.table;
  66. if (npm.utils.isText(config.schema)) {
  67. this.schema = config.schema;
  68. }
  69. }
  70. if (!npm.utils.isText(this.table)) {
  71. throw new TypeError('Table name must be a non-empty text string.');
  72. }
  73. this.name = npm.format.name(this.table);
  74. if (this.schema) {
  75. this.name = npm.format.name(this.schema) + '.' + this.name;
  76. }
  77. Object.freeze(this);
  78. }
  79. }
  80. /**
  81. * @method helpers.TableName#toPostgres
  82. * @description
  83. * $[Custom Type Formatting], based on $[Symbolic CTF], i.e. the actual method is available only via {@link external:Symbol Symbol}:
  84. *
  85. * ```js
  86. * const ctf = pgp.as.ctf; // Custom Type Formatting symbols namespace
  87. * const fullName = tn[ctf.toPostgres]; // tn = an object of type TableName
  88. * ```
  89. *
  90. * This is a raw formatting type (`rawType = true`), i.e. when used as a query-formatting parameter, type `TableName`
  91. * injects full table name as raw text.
  92. *
  93. * @param {helpers.TableName} [self]
  94. * Optional self-reference, for ES6 arrow functions.
  95. *
  96. * @returns {string}
  97. * Escaped full table name that includes optional schema name, if specified.
  98. */
  99. TableName.prototype[npm.format.ctf.toPostgres] = function (self) {
  100. self = this instanceof TableName && this || self;
  101. return self.name;
  102. };
  103. TableName.prototype[npm.format.ctf.rawType] = true; // use as pre-formatted
  104. /**
  105. * @method helpers.TableName#toString
  106. * @description
  107. * Creates a well-formatted string that represents the object.
  108. *
  109. * It is called automatically when writing the object into the console.
  110. *
  111. * @returns {string}
  112. */
  113. TableName.prototype.toString = function () {
  114. return this.name;
  115. };
  116. npm.utils.addInspection(TableName, function () {
  117. return this.toString();
  118. });
  119. module.exports = {TableName};