node-pg-migrate
Version:
PostgreSQL database migration management tool for node.js
85 lines (84 loc) • 3.37 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var createTable_exports = {};
__export(createTable_exports, {
createTable: () => createTable
});
module.exports = __toCommonJS(createTable_exports);
var import_utils = require("../../utils");
var import_dropTable = require("./dropTable");
var import_shared = require("./shared");
function createTable(mOptions) {
const _create = (tableName, columns, options = {}) => {
const {
temporary = false,
ifNotExists = false,
inherits,
like,
constraints: optionsConstraints = {},
comment,
partition
} = options;
const {
columns: columnLines,
constraints: crossColumnConstraints,
comments: columnComments = []
} = (0, import_shared.parseColumns)(tableName, columns, mOptions);
const dupes = (0, import_utils.intersection)(
Object.keys(optionsConstraints),
Object.keys(crossColumnConstraints)
);
if (dupes.length > 0) {
const dupesStr = dupes.join(", ");
throw new Error(
`There is duplicate constraint definition in table and columns options: ${dupesStr}`
);
}
const constraints = {
...optionsConstraints,
...crossColumnConstraints
};
const { constraints: constraintLines, comments: constraintComments } = (0, import_shared.parseConstraints)(tableName, constraints, "", mOptions.literal);
const tableDefinition = [
...columnLines,
...constraintLines,
...like ? [(0, import_shared.parseLike)(like, mOptions.literal)] : []
];
const temporaryStr = temporary ? " TEMPORARY" : "";
const ifNotExistsStr = ifNotExists ? " IF NOT EXISTS" : "";
const inheritsStr = inherits ? ` INHERITS (${mOptions.literal(inherits)})` : "";
const partitionStr = partition ? ` PARTITION BY ${partition.strategy} (${(0, import_utils.formatPartitionColumns)(partition, mOptions.literal)})` : "";
const tableNameStr = mOptions.literal(tableName);
const createTableQuery = `CREATE${temporaryStr} TABLE${ifNotExistsStr} ${tableNameStr} (
${(0, import_utils.formatLines)(tableDefinition)}
)${inheritsStr}${partitionStr};`;
const comments = [...columnComments, ...constraintComments];
if (comment !== void 0) {
comments.push((0, import_utils.makeComment)("TABLE", mOptions.literal(tableName), comment));
}
return `${createTableQuery}${comments.length > 0 ? `
${comments.join("\n")}` : ""}`;
};
_create.reverse = (0, import_dropTable.dropTable)(mOptions);
return _create;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createTable
});