123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- const {assert} = require('../assert');
- const npm = {
- utils: require('../utils'),
- format: require('../formatting').as
- };
- class TableName {
- constructor(table) {
- if (typeof table === 'string') {
- this.table = table;
- } else {
- const config = assert(table, ['table', 'schema']);
- this.table = config.table;
- if (npm.utils.isText(config.schema)) {
- this.schema = config.schema;
- }
- }
- if (!npm.utils.isText(this.table)) {
- throw new TypeError('Table name must be a non-empty text string.');
- }
- this.name = npm.format.name(this.table);
- if (this.schema) {
- this.name = npm.format.name(this.schema) + '.' + this.name;
- }
- Object.freeze(this);
- }
- }
- TableName.prototype[npm.format.ctf.toPostgres] = function (self) {
- self = this instanceof TableName && this || self;
- return self.name;
- };
- TableName.prototype[npm.format.ctf.rawType] = true;
- TableName.prototype.toString = function () {
- return this.name;
- };
- npm.utils.addInspection(TableName, function () {
- return this.toString();
- });
- function _TN(path, ...args) {
- if (Array.isArray(path)) {
- path = path.reduce((a, c, i) => a + c + (args[i] ?? ''), '');
- }
- const [schema, table] = path.split('.');
- if (table === undefined) {
- return {table: schema};
- }
- return schema ? {schema, table} : {table};
- }
- module.exports = {TableName, _TN};
|