knex-schema-inspector
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -16,3 +16,2 @@ import { Knex } from 'knex'; | ||
set schema(value: string); | ||
parseDefaultValue(value: string | null): string | number | null; | ||
/** | ||
@@ -19,0 +18,0 @@ * List all existing tables in the current schema/database |
@@ -50,2 +50,13 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function rawColumnToColumn(rawColumn) { | ||
return __assign(__assign({}, rawColumn), { default_value: parseDefaultValue(rawColumn.default_value) || | ||
parseDefaultValue(rawColumn.generation_expression), is_generated: !!rawColumn.is_generated, is_unique: rawColumn.is_unique === 'YES', is_primary_key: rawColumn.is_primary_key === 'YES', is_nullable: rawColumn.is_nullable === 'YES', has_auto_increment: rawColumn.has_auto_increment === 'YES', numeric_precision: rawColumn.numeric_precision || null, numeric_scale: rawColumn.numeric_precision || null }); | ||
} | ||
function parseDefaultValue(value) { | ||
if (!value) | ||
return null; | ||
value = value.replace(/^\((.*)\)$/, '$1'); | ||
value = value.replace(/^\'(.*)\'$/, '$1'); | ||
return isNaN(value) ? String(value) : Number(value); | ||
} | ||
var MSSQL = /** @class */ (function () { | ||
@@ -74,15 +85,2 @@ function MSSQL(knex) { | ||
}); | ||
MSSQL.prototype.parseDefaultValue = function (value) { | ||
if (!value) | ||
return null; | ||
if (value.startsWith('(') && value.endsWith(')')) { | ||
value = value.slice(1, -1); | ||
} | ||
if (value.startsWith("'") && value.endsWith("'")) { | ||
value = value.slice(1, -1); | ||
} | ||
if (Number.isNaN(Number(value))) | ||
return String(value); | ||
return Number(value); | ||
}; | ||
// Tables | ||
@@ -217,3 +215,2 @@ // =============================================================================================== | ||
var dbName, query, rawColumn, records; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
@@ -224,3 +221,3 @@ switch (_a.label) { | ||
query = this.knex | ||
.select(this.knex.raw("\n [o].[name] AS [table],\n [c].[name] AS [name],\n [t].[name] AS [data_type],\n [c].[max_length] AS [max_length],\n [c].[precision] AS [numeric_precision],\n [c].[scale] AS [numeric_scale],\n CASE WHEN [c].[is_nullable] = 0 THEN\n 'NO'\n ELSE\n 'YES'\n END AS [is_nullable],\n object_definition ([c].[default_object_id]) AS default_value,\n CASE [i].[is_unique]\n WHEN 1 THEN\n 'YES'\n ELSE\n 'NO'\n END AS [is_unique],\n CASE [i].[is_primary_key]\n WHEN 1 THEN\n 'YES'\n ELSE\n 'NO'\n END AS [is_primary_key],\n CASE [c].[is_identity]\n WHEN 1 THEN\n 'YES'\n ELSE\n 'NO'\n END AS [has_auto_increment],\n OBJECT_NAME ([fk].[referenced_object_id]) AS [foreign_key_table],\n COL_NAME ([fk].[referenced_object_id],\n [fk].[referenced_column_id]) AS [foreign_key_column]")) | ||
.select(this.knex.raw("\n [o].[name] AS [table],\n [c].[name] AS [name],\n [t].[name] AS [data_type],\n [c].[max_length] AS [max_length],\n [c].[precision] AS [numeric_precision],\n [c].[scale] AS [numeric_scale],\n CASE WHEN [c].[is_nullable] = 0 THEN\n 'NO'\n ELSE\n 'YES'\n END AS [is_nullable],\n COALESCE(\n object_definition ([c].[default_object_id]),\n [cc].[definition]\n ) AS [default_value],\n CASE [i].[is_unique]\n WHEN 1 THEN\n 'YES'\n ELSE\n 'NO'\n END AS [is_unique],\n CASE [i].[is_primary_key]\n WHEN 1 THEN\n 'YES'\n ELSE\n 'NO'\n END AS [is_primary_key],\n CASE [c].[is_identity]\n WHEN 1 THEN\n 'YES'\n ELSE\n 'NO'\n END AS [has_auto_increment],\n OBJECT_NAME ([fk].[referenced_object_id]) AS [foreign_key_table],\n COL_NAME ([fk].[referenced_object_id],\n [fk].[referenced_column_id]) AS [foreign_key_column],\n [cc].[is_computed] as [is_generated]")) | ||
.from(this.knex.raw("??.[sys].[columns] [c]", [dbName])) | ||
@@ -230,2 +227,3 @@ .joinRaw("JOIN [sys].[types] [t] ON [c].[user_type_id] = [t].[user_type_id]") | ||
.joinRaw("JOIN [sys].[schemas] [s] ON [s].[schema_id] = [o].[schema_id]") | ||
.joinRaw("LEFT JOIN [sys].[computed_columns] AS [cc] ON [cc].[object_id] = [c].[object_id] AND [cc].[column_id] = [c].[column_id]") | ||
.joinRaw("LEFT JOIN [sys].[index_columns] [ic] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id]") | ||
@@ -244,9 +242,7 @@ .joinRaw("LEFT JOIN [sys].[indexes] AS [i] ON [i].[object_id] = [c].[object_id] AND [i].[index_id] = [ic].[index_id]") | ||
rawColumn = _a.sent(); | ||
return [2 /*return*/, __assign(__assign({}, rawColumn), { default_value: this.parseDefaultValue(rawColumn.default_value), is_unique: rawColumn.is_unique === 'YES', is_primary_key: rawColumn.is_primary_key === 'YES', is_nullable: rawColumn.is_nullable === 'YES', has_auto_increment: rawColumn.has_auto_increment === 'YES', numeric_precision: rawColumn.numeric_precision || null, numeric_scale: rawColumn.numeric_precision || null })]; | ||
return [2 /*return*/, rawColumnToColumn(rawColumn)]; | ||
case 2: return [4 /*yield*/, query]; | ||
case 3: | ||
records = _a.sent(); | ||
return [2 /*return*/, records.map(function (rawColumn) { | ||
return __assign(__assign({}, rawColumn), { default_value: _this.parseDefaultValue(rawColumn.default_value), is_unique: rawColumn.is_unique === 'YES', is_primary_key: rawColumn.is_primary_key === 'YES', is_nullable: rawColumn.is_nullable === 'YES', has_auto_increment: rawColumn.has_auto_increment === 'YES', numeric_precision: rawColumn.numeric_precision || null, numeric_scale: rawColumn.numeric_precision || null }); | ||
})]; | ||
return [2 /*return*/, records.map(rawColumnToColumn)]; | ||
} | ||
@@ -253,0 +249,0 @@ }); |
@@ -50,2 +50,28 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function rawColumnToColumn(rawColumn) { | ||
var _a, _b; | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: (_a = parseDefaultValue(rawColumn.COLUMN_DEFAULT)) !== null && _a !== void 0 ? _a : parseDefaultValue(rawColumn.GENERATION_EXPRESSION || null), | ||
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH, | ||
numeric_precision: rawColumn.NUMERIC_PRECISION, | ||
numeric_scale: rawColumn.NUMERIC_SCALE, | ||
is_generated: !!((_b = rawColumn.EXTRA) === null || _b === void 0 ? void 0 : _b.endsWith('GENERATED')), | ||
is_nullable: rawColumn.IS_NULLABLE === 'YES', | ||
is_unique: rawColumn.COLUMN_KEY === 'UNI', | ||
is_primary_key: rawColumn.CONSTRAINT_NAME === 'PRIMARY' || rawColumn.COLUMN_KEY === 'PRI', | ||
has_auto_increment: rawColumn.EXTRA === 'auto_increment', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
// onDelete: rawColumn.DELETE_RULE, | ||
// onUpdate: rawColumn.UPDATE_RULE, | ||
}; | ||
} | ||
function parseDefaultValue(value) { | ||
// MariaDB returns string NULL for not-nullable varchar fields | ||
return /null|NULL/.test(value) ? null : value; | ||
} | ||
var MySQL = /** @class */ (function () { | ||
@@ -181,8 +207,2 @@ function MySQL(knex) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
function parseDefault(value) { | ||
// MariaDB returns string NULL for not-nullable varchar fields | ||
if (value === 'NULL' || value === 'null') | ||
return null; | ||
return value; | ||
} | ||
var query, rawColumn, records; | ||
@@ -193,3 +213,3 @@ return __generator(this, function (_a) { | ||
query = this.knex | ||
.select('c.TABLE_NAME', 'c.COLUMN_NAME', 'c.COLUMN_DEFAULT', 'c.DATA_TYPE', 'c.CHARACTER_MAXIMUM_LENGTH', 'c.IS_NULLABLE', 'c.COLUMN_KEY', 'c.EXTRA', 'c.COLLATION_NAME', 'c.COLUMN_COMMENT', 'c.NUMERIC_PRECISION', 'c.NUMERIC_SCALE', 'fk.REFERENCED_TABLE_NAME', 'fk.REFERENCED_COLUMN_NAME', 'fk.CONSTRAINT_NAME', 'rc.UPDATE_RULE', 'rc.DELETE_RULE', 'rc.MATCH_OPTION') | ||
.select('c.TABLE_NAME', 'c.COLUMN_NAME', 'c.COLUMN_DEFAULT', 'c.DATA_TYPE', 'c.CHARACTER_MAXIMUM_LENGTH', 'c.IS_NULLABLE', 'c.COLUMN_KEY', 'c.EXTRA', 'c.COLLATION_NAME', 'c.COLUMN_COMMENT', 'c.NUMERIC_PRECISION', 'c.NUMERIC_SCALE', 'c.GENERATION_EXPRESSION', 'fk.REFERENCED_TABLE_NAME', 'fk.REFERENCED_COLUMN_NAME', 'fk.CONSTRAINT_NAME', 'rc.UPDATE_RULE', 'rc.DELETE_RULE', 'rc.MATCH_OPTION') | ||
.from('INFORMATION_SCHEMA.COLUMNS as c') | ||
@@ -218,45 +238,7 @@ .leftJoin('INFORMATION_SCHEMA.KEY_COLUMN_USAGE as fk', function () { | ||
rawColumn = _a.sent(); | ||
return [2 /*return*/, { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: parseDefault(rawColumn.COLUMN_DEFAULT), | ||
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH, | ||
numeric_precision: rawColumn.NUMERIC_PRECISION, | ||
numeric_scale: rawColumn.NUMERIC_SCALE, | ||
is_nullable: rawColumn.IS_NULLABLE === 'YES', | ||
is_unique: rawColumn.COLUMN_KEY === 'UNI', | ||
is_primary_key: rawColumn.CONSTRAINT_NAME === 'PRIMARY' || | ||
rawColumn.COLUMN_KEY === 'PRI', | ||
has_auto_increment: rawColumn.EXTRA === 'auto_increment', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
// onDelete: rawColumn.DELETE_RULE, | ||
// onUpdate: rawColumn.UPDATE_RULE, | ||
}]; | ||
return [2 /*return*/, rawColumnToColumn(rawColumn)]; | ||
case 2: return [4 /*yield*/, query]; | ||
case 3: | ||
records = _a.sent(); | ||
return [2 /*return*/, records.map(function (rawColumn) { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: parseDefault(rawColumn.COLUMN_DEFAULT), | ||
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH, | ||
numeric_precision: rawColumn.NUMERIC_PRECISION, | ||
numeric_scale: rawColumn.NUMERIC_SCALE, | ||
is_nullable: rawColumn.IS_NULLABLE === 'YES', | ||
is_unique: rawColumn.COLUMN_KEY === 'UNI', | ||
is_primary_key: rawColumn.CONSTRAINT_NAME === 'PRIMARY' || | ||
rawColumn.COLUMN_KEY === 'PRI', | ||
has_auto_increment: rawColumn.EXTRA === 'auto_increment', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
// onDelete: rawColumn.DELETE_RULE, | ||
// onUpdate: rawColumn.UPDATE_RULE, | ||
}; | ||
})]; | ||
return [2 /*return*/, records.map(rawColumnToColumn)]; | ||
} | ||
@@ -263,0 +245,0 @@ }); |
@@ -39,2 +39,21 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function rawColumnToColumn(rawColumn) { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: rawColumn.DATA_DEFAULT, | ||
max_length: rawColumn.DATA_LENGTH, | ||
numeric_precision: rawColumn.DATA_PRECISION, | ||
numeric_scale: rawColumn.DATA_SCALE, | ||
is_generated: rawColumn.VIRTUAL_COLUMN === 'YES', | ||
is_nullable: rawColumn.NULLABLE === 'Y', | ||
is_unique: rawColumn.CONSTRAINT_TYPE === 'U', | ||
is_primary_key: rawColumn.CONSTRAINT_TYPE === 'P', | ||
has_auto_increment: rawColumn.IDENTITY_COLUMN === 'YES', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
}; | ||
} | ||
var oracleDB = /** @class */ (function () { | ||
@@ -128,3 +147,3 @@ function oracleDB(knex) { | ||
.select('TABLE_NAME', 'COLUMN_NAME') | ||
.from('USER_TAB_COLUMNS'); | ||
.from('USER_TAB_COLS'); | ||
if (table) { | ||
@@ -154,4 +173,4 @@ query.where({ TABLE_NAME: table }); | ||
query = this.knex | ||
.select('c.TABLE_NAME', 'c.COLUMN_NAME', 'c.DATA_DEFAULT', 'c.DATA_TYPE', 'c.DATA_LENGTH', 'c.DATA_PRECISION', 'c.DATA_SCALE', 'c.NULLABLE', 'cm.COMMENTS as COLUMN_COMMENT', 'pk.CONSTRAINT_TYPE', 'fk.REFERENCED_TABLE_NAME', 'fk.REFERENCED_COLUMN_NAME') | ||
.from('USER_TAB_COLUMNS as c') | ||
.select('c.TABLE_NAME', 'c.COLUMN_NAME', 'c.DATA_DEFAULT', 'c.DATA_TYPE', 'c.DATA_LENGTH', 'c.DATA_PRECISION', 'c.DATA_SCALE', 'c.NULLABLE', 'c.IDENTITY_COLUMN', 'c.VIRTUAL_COLUMN', 'cm.COMMENTS as COLUMN_COMMENT', 'pk.CONSTRAINT_TYPE', 'fk.REFERENCED_TABLE_NAME', 'fk.REFERENCED_COLUMN_NAME') | ||
.from('USER_TAB_COLS as c') | ||
.leftJoin('USER_COL_COMMENTS as cm', { | ||
@@ -190,37 +209,7 @@ 'c.TABLE_NAME': 'cm.TABLE_NAME', | ||
rawColumn = _a.sent(); | ||
return [2 /*return*/, { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: rawColumn.DATA_DEFAULT, | ||
max_length: rawColumn.DATA_LENGTH, | ||
numeric_precision: rawColumn.DATA_PRECISION, | ||
numeric_scale: rawColumn.DATA_SCALE, | ||
is_nullable: rawColumn.NULLABLE === 'Y', | ||
is_unique: rawColumn.CONSTRAINT_TYPE === 'U', | ||
is_primary_key: rawColumn.CONSTRAINT_TYPE === 'P', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
}]; | ||
return [2 /*return*/, rawColumnToColumn(rawColumn)]; | ||
case 2: return [4 /*yield*/, query]; | ||
case 3: | ||
records = _a.sent(); | ||
return [2 /*return*/, records.map(function (rawColumn) { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: rawColumn.DATA_DEFAULT, | ||
max_length: rawColumn.DATA_LENGTH, | ||
numeric_precision: rawColumn.DATA_PRECISION, | ||
numeric_scale: rawColumn.DATA_SCALE, | ||
is_nullable: rawColumn.NULLABLE === 'Y', | ||
is_unique: rawColumn.CONSTRAINT_TYPE === 'U', | ||
is_primary_key: rawColumn.CONSTRAINT_TYPE === 'P', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
}; | ||
})]; | ||
return [2 /*return*/, records.map(rawColumnToColumn)]; | ||
} | ||
@@ -239,3 +228,3 @@ }); | ||
.count({ count: '*' }) | ||
.from('USER_TAB_COLUMNS') | ||
.from('USER_TAB_COLS') | ||
.where({ | ||
@@ -242,0 +231,0 @@ TABLE_NAME: table, |
@@ -16,7 +16,2 @@ import { Knex } from 'knex'; | ||
/** | ||
* Converts Postgres default value to JS | ||
* Eg `'example'::character varying` => `example` | ||
*/ | ||
parseDefaultValue(type: string): any; | ||
/** | ||
* List all existing tables in the current schema/database | ||
@@ -23,0 +18,0 @@ */ |
@@ -39,2 +39,41 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function rawColumnToColumn(rawColumn) { | ||
return { | ||
name: rawColumn.column_name, | ||
table: rawColumn.table_name, | ||
data_type: rawColumn.data_type, | ||
default_value: parseDefaultValue(rawColumn.column_default) || | ||
parseDefaultValue(rawColumn.generation_expression), | ||
max_length: rawColumn.character_maximum_length, | ||
numeric_precision: rawColumn.numeric_precision, | ||
numeric_scale: rawColumn.numeric_scale, | ||
is_generated: rawColumn.is_generated === 'YES', | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
is_unique: rawColumn.is_unique, | ||
is_primary_key: rawColumn.is_primary, | ||
has_auto_increment: rawColumn.serial !== null || rawColumn.is_identity === 'YES', | ||
comment: rawColumn.column_comment, | ||
schema: rawColumn.table_schema, | ||
foreign_key_schema: rawColumn.foreign_key_schema, | ||
foreign_key_table: rawColumn.foreign_key_table, | ||
foreign_key_column: rawColumn.foreign_key_column, | ||
}; | ||
} | ||
/** | ||
* Converts Postgres default value to JS | ||
* Eg `'example'::character varying` => `example` | ||
*/ | ||
function parseDefaultValue(type) { | ||
if (!type) | ||
return null; | ||
if (type.startsWith('nextval(')) | ||
return type; | ||
var _a = type.split('::'), value = _a[0], cast = _a[1]; | ||
value = value.replace(/^\'(.*)\'$/, '$1'); | ||
if (/.*json.*/.test(cast)) | ||
return JSON.parse(value); | ||
if (/.*(char|text).*/.test(cast)) | ||
return String(value); | ||
return isNaN(value) ? value : Number(value); | ||
} | ||
var Postgres = /** @class */ (function () { | ||
@@ -67,24 +106,2 @@ function Postgres(knex) { | ||
}; | ||
/** | ||
* Converts Postgres default value to JS | ||
* Eg `'example'::character varying` => `example` | ||
*/ | ||
Postgres.prototype.parseDefaultValue = function (type) { | ||
if (!type) | ||
return null; | ||
if (type.startsWith('nextval(')) | ||
return type; | ||
var parts = type.split('::'); | ||
var value = parts[0]; | ||
if (value.startsWith("'") && value.endsWith("'")) { | ||
value = value.slice(1, -1); | ||
} | ||
if (parts[1] && parts[1].includes('json')) | ||
return JSON.parse(value); | ||
if (parts[1] && (parts[1].includes('char') || parts[1].includes('text'))) | ||
return String(value); | ||
if (Number.isNaN(Number(value))) | ||
return value; | ||
return Number(value); | ||
}; | ||
// Tables | ||
@@ -217,3 +234,2 @@ // =============================================================================================== | ||
var knex, query, rawColumn, records; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
@@ -224,28 +240,7 @@ switch (_a.label) { | ||
query = knex | ||
.select('c.column_name', 'c.table_name', 'c.data_type', 'c.column_default', 'c.character_maximum_length', 'c.is_nullable', 'c.numeric_precision', 'c.numeric_scale', 'c.table_schema', knex | ||
.select(knex.raw("'YES'")) | ||
.from('pg_index') | ||
.join('pg_attribute', function () { | ||
this.on('pg_attribute.attrelid', '=', 'pg_index.indrelid').andOn(knex.raw('pg_attribute.attnum = any(pg_index.indkey)')); | ||
}) | ||
.whereRaw('pg_index.indrelid = quote_ident(c.table_name)::regclass') | ||
.andWhere(knex.raw('pg_attribute.attname = c.column_name')) | ||
.andWhere(knex.raw('pg_index.indisprimary')) | ||
.as('is_primary'), knex | ||
.select(knex.raw("case when count(1) >0 then 'YES' else null end")) | ||
.from('pg_index') | ||
.join('pg_attribute', function () { | ||
this.on('pg_attribute.attrelid', '=', 'pg_index.indrelid').andOn(knex.raw('pg_attribute.attnum = any(pg_index.indkey)')); | ||
}) | ||
.whereRaw('pg_index.indrelid = quote_ident(c.table_name)::regclass') | ||
.andWhere(knex.raw('pg_attribute.attname = c.column_name')) | ||
.andWhere(knex.raw('pg_index.indisunique')) | ||
.as('is_unique'), knex | ||
.select(knex.raw('pg_catalog.col_description(pg_catalog.pg_class.oid, c.ordinal_position:: int)')) | ||
.from('pg_catalog.pg_class') | ||
.whereRaw("pg_catalog.pg_class.oid = (select quote_ident(c.table_name):: regclass:: oid)") | ||
.andWhere({ 'pg_catalog.pg_class.relname': 'c.table_name' }) | ||
.as('column_comment'), knex.raw('pg_get_serial_sequence(quote_ident(c.table_name), c.column_name) as serial'), 'ffk.referenced_table_schema', 'ffk.referenced_table_name', 'ffk.referenced_column_name') | ||
.select('c.column_name', 'c.table_name', 'c.data_type', 'c.column_default', 'c.character_maximum_length', 'c.is_generated', 'c.is_nullable', 'c.numeric_precision', 'c.numeric_scale', 'c.table_schema', 'c.is_identity', 'c.generation_expression', knex.raw('pg_get_serial_sequence(quote_ident(c.table_name), c.column_name) as serial'), knex.raw('pg_catalog.col_description(pg_class.oid, c.ordinal_position:: int) as column_comment'), knex.raw("COALESCE(pg.indisunique, false) as is_unique"), knex.raw("COALESCE(pg.indisprimary, false) as is_primary"), 'ffk.foreign_key_schema', 'ffk.foreign_key_table', 'ffk.foreign_key_column') | ||
.from(knex.raw('information_schema.columns c')) | ||
.joinRaw("\n LEFT JOIN (\n SELECT\n k1.table_schema,\n k1.table_name,\n k1.column_name,\n k2.table_schema AS referenced_table_schema,\n k2.table_name AS referenced_table_name,\n k2.column_name AS referenced_column_name\n FROM\n information_schema.key_column_usage k1\n JOIN information_schema.referential_constraints fk using (\n constraint_schema, constraint_name\n )\n JOIN information_schema.key_column_usage k2 ON k2.constraint_schema = fk.unique_constraint_schema\n AND k2.constraint_name = fk.unique_constraint_name\n AND k2.ordinal_position = k1.position_in_unique_constraint\n ) ffk ON ffk.table_name = c.table_name\n AND ffk.column_name = c.column_name\n ") | ||
.joinRaw("\n LEFT JOIN pg_catalog.pg_class\n ON pg_catalog.pg_class.oid = quote_ident(c.table_name):: regclass:: oid\n AND pg_catalog.pg_class.relname = c.table_name\n ") | ||
.joinRaw("\n LEFT JOIN LATERAL (\n SELECT\n pg_index.indisprimary,\n pg_index.indisunique\n FROM pg_index\n JOIN pg_attribute\n ON pg_attribute.attrelid = pg_index.indrelid\n AND pg_attribute.attnum = any(pg_index.indkey)\n WHERE pg_index.indrelid = quote_ident(c.table_name)::regclass\n AND pg_attribute.attname = c.column_name\n LIMIT 1\n ) pg ON true\n ") | ||
.joinRaw("\n LEFT JOIN LATERAL (\n SELECT\n k2.table_schema AS foreign_key_schema,\n k2.table_name AS foreign_key_table,\n k2.column_name AS foreign_key_column\n FROM\n information_schema.key_column_usage k1\n JOIN information_schema.referential_constraints fk using (\n constraint_schema, constraint_name\n )\n JOIN information_schema.key_column_usage k2\n ON k2.constraint_schema = fk.unique_constraint_schema\n AND k2.constraint_name = fk.unique_constraint_name\n AND k2.ordinal_position = k1.position_in_unique_constraint\n WHERE k1.table_name = c.table_name\n AND k1.column_name = c.column_name\n ) ffk ON TRUE\n ") | ||
.whereIn('c.table_schema', this.explodedSchema) | ||
@@ -262,47 +257,7 @@ .orderBy(['c.table_name', 'c.ordinal_position']); | ||
rawColumn = _a.sent(); | ||
return [2 /*return*/, { | ||
name: rawColumn.column_name, | ||
table: rawColumn.table_name, | ||
data_type: rawColumn.data_type, | ||
default_value: rawColumn.column_default | ||
? this.parseDefaultValue(rawColumn.column_default) | ||
: null, | ||
max_length: rawColumn.character_maximum_length, | ||
numeric_precision: rawColumn.numeric_precision, | ||
numeric_scale: rawColumn.numeric_scale, | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
is_unique: rawColumn.is_unique === 'YES', | ||
is_primary_key: rawColumn.is_primary === 'YES', | ||
has_auto_increment: rawColumn.serial !== null, | ||
foreign_key_column: rawColumn.referenced_column_name, | ||
foreign_key_table: rawColumn.referenced_table_name, | ||
comment: rawColumn.column_comment, | ||
schema: rawColumn.table_schema, | ||
foreign_key_schema: rawColumn.referenced_table_schema, | ||
}]; | ||
return [2 /*return*/, rawColumnToColumn(rawColumn)]; | ||
case 2: return [4 /*yield*/, query]; | ||
case 3: | ||
records = _a.sent(); | ||
return [2 /*return*/, records.map(function (rawColumn) { | ||
return { | ||
name: rawColumn.column_name, | ||
table: rawColumn.table_name, | ||
data_type: rawColumn.data_type, | ||
default_value: rawColumn.column_default | ||
? _this.parseDefaultValue(rawColumn.column_default) | ||
: null, | ||
max_length: rawColumn.character_maximum_length, | ||
numeric_precision: rawColumn.numeric_precision, | ||
numeric_scale: rawColumn.numeric_scale, | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
is_unique: rawColumn.is_unique === 'YES', | ||
is_primary_key: rawColumn.is_primary === 'YES', | ||
has_auto_increment: rawColumn.serial !== null, | ||
foreign_key_column: rawColumn.referenced_column_name, | ||
foreign_key_table: rawColumn.referenced_table_name, | ||
comment: rawColumn.column_comment, | ||
schema: rawColumn.table_schema, | ||
foreign_key_schema: rawColumn.referenced_table_schema, | ||
}; | ||
})]; | ||
return [2 /*return*/, records.map(rawColumnToColumn)]; | ||
} | ||
@@ -309,0 +264,0 @@ }); |
@@ -134,3 +134,3 @@ "use strict"; | ||
if (!table) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.knex.raw("PRAGMA table_info(??)", table)]; | ||
return [4 /*yield*/, this.knex.raw("PRAGMA table_xinfo(??)", table)]; | ||
case 1: | ||
@@ -178,3 +178,3 @@ columns = _a.sent(); | ||
}); | ||
return [4 /*yield*/, this.knex.raw("PRAGMA table_info(??)", table)]; | ||
return [4 /*yield*/, this.knex.raw("PRAGMA table_xinfo(??)", table)]; | ||
case 2: | ||
@@ -200,2 +200,3 @@ columns = _a.sent(); | ||
numeric_scale: null, | ||
is_generated: raw.hidden !== 0, | ||
is_nullable: raw.notnull === 0, | ||
@@ -248,3 +249,3 @@ is_unique: !!(index === null || index === void 0 ? void 0 : index.unique), | ||
isColumn = false; | ||
return [4 /*yield*/, this.knex.raw("SELECT COUNT(*) AS ct FROM pragma_table_info('" + table + "') WHERE name='" + column + "'")]; | ||
return [4 /*yield*/, this.knex.raw("SELECT COUNT(*) AS ct FROM pragma_table_xinfo('" + table + "') WHERE name='" + column + "'")]; | ||
case 1: | ||
@@ -269,3 +270,3 @@ results = _a.sent(); | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.knex.raw("PRAGMA table_info(??)", table)]; | ||
case 0: return [4 /*yield*/, this.knex.raw("PRAGMA table_xinfo(??)", table)]; | ||
case 1: | ||
@@ -272,0 +273,0 @@ columns = _a.sent(); |
@@ -12,3 +12,4 @@ export interface Column { | ||
is_primary_key: boolean; | ||
has_auto_increment?: boolean; | ||
is_generated: boolean; | ||
has_auto_increment: boolean; | ||
foreign_key_table: string | null; | ||
@@ -15,0 +16,0 @@ foreign_key_column: string | null; |
@@ -20,2 +20,3 @@ import { Knex } from 'knex'; | ||
numeric_scale: number | null; | ||
is_generated: boolean | null; | ||
is_nullable: 'YES' | 'NO'; | ||
@@ -28,4 +29,30 @@ default_value: string | null; | ||
foreign_key_column: string | null; | ||
generation_expression: string | null; | ||
}; | ||
function rawColumnToColumn(rawColumn: RawColumn): Column { | ||
return { | ||
...rawColumn, | ||
default_value: | ||
parseDefaultValue(rawColumn.default_value) || | ||
parseDefaultValue(rawColumn.generation_expression), | ||
is_generated: !!rawColumn.is_generated, | ||
is_unique: rawColumn.is_unique === 'YES', | ||
is_primary_key: rawColumn.is_primary_key === 'YES', | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
has_auto_increment: rawColumn.has_auto_increment === 'YES', | ||
numeric_precision: rawColumn.numeric_precision || null, | ||
numeric_scale: rawColumn.numeric_precision || null, | ||
}; | ||
} | ||
function parseDefaultValue(value: string | null) { | ||
if (!value) return null; | ||
value = value.replace(/^\((.*)\)$/, '$1'); | ||
value = value.replace(/^\'(.*)\'$/, '$1'); | ||
return isNaN(value as any) ? String(value) : Number(value); | ||
} | ||
export default class MSSQL implements SchemaInspector { | ||
@@ -58,18 +85,2 @@ knex: Knex; | ||
parseDefaultValue(value: string | null) { | ||
if (!value) return null; | ||
if (value.startsWith('(') && value.endsWith(')')) { | ||
value = value.slice(1, -1); | ||
} | ||
if (value.startsWith("'") && value.endsWith("'")) { | ||
value = value.slice(1, -1); | ||
} | ||
if (Number.isNaN(Number(value))) return String(value); | ||
return Number(value); | ||
} | ||
// Tables | ||
@@ -203,3 +214,6 @@ // =============================================================================================== | ||
END AS [is_nullable], | ||
object_definition ([c].[default_object_id]) AS default_value, | ||
COALESCE( | ||
object_definition ([c].[default_object_id]), | ||
[cc].[definition] | ||
) AS [default_value], | ||
CASE [i].[is_unique] | ||
@@ -225,3 +239,4 @@ WHEN 1 THEN | ||
COL_NAME ([fk].[referenced_object_id], | ||
[fk].[referenced_column_id]) AS [foreign_key_column]`) | ||
[fk].[referenced_column_id]) AS [foreign_key_column], | ||
[cc].[is_computed] as [is_generated]`) | ||
) | ||
@@ -235,2 +250,5 @@ .from(this.knex.raw(`??.[sys].[columns] [c]`, [dbName])) | ||
.joinRaw( | ||
`LEFT JOIN [sys].[computed_columns] AS [cc] ON [cc].[object_id] = [c].[object_id] AND [cc].[column_id] = [c].[column_id]` | ||
) | ||
.joinRaw( | ||
`LEFT JOIN [sys].[index_columns] [ic] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id]` | ||
@@ -255,12 +273,3 @@ ) | ||
return { | ||
...rawColumn, | ||
default_value: this.parseDefaultValue(rawColumn.default_value), | ||
is_unique: rawColumn.is_unique === 'YES', | ||
is_primary_key: rawColumn.is_primary_key === 'YES', | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
has_auto_increment: rawColumn.has_auto_increment === 'YES', | ||
numeric_precision: rawColumn.numeric_precision || null, | ||
numeric_scale: rawColumn.numeric_precision || null, | ||
} as Column; | ||
return rawColumnToColumn(rawColumn); | ||
} | ||
@@ -270,16 +279,3 @@ | ||
return records.map( | ||
(rawColumn): Column => { | ||
return { | ||
...rawColumn, | ||
default_value: this.parseDefaultValue(rawColumn.default_value), | ||
is_unique: rawColumn.is_unique === 'YES', | ||
is_primary_key: rawColumn.is_primary_key === 'YES', | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
has_auto_increment: rawColumn.has_auto_increment === 'YES', | ||
numeric_precision: rawColumn.numeric_precision || null, | ||
numeric_scale: rawColumn.numeric_precision || null, | ||
} as Column; | ||
} | ||
) as Column[]; | ||
return records.map(rawColumnToColumn); | ||
} | ||
@@ -286,0 +282,0 @@ |
@@ -31,6 +31,37 @@ import { Knex } from 'knex'; | ||
COLUMN_KEY: 'PRI' | 'UNI' | null; | ||
EXTRA: 'auto_increment' | null; | ||
EXTRA: 'auto_increment' | 'STORED GENERATED' | 'VIRTUAL GENERATED' | null; | ||
CONSTRAINT_NAME: 'PRIMARY' | null; | ||
GENERATION_EXPRESSION: string; | ||
}; | ||
function rawColumnToColumn(rawColumn: RawColumn): Column { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: | ||
parseDefaultValue(rawColumn.COLUMN_DEFAULT) ?? | ||
parseDefaultValue(rawColumn.GENERATION_EXPRESSION || null), | ||
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH, | ||
numeric_precision: rawColumn.NUMERIC_PRECISION, | ||
numeric_scale: rawColumn.NUMERIC_SCALE, | ||
is_generated: !!rawColumn.EXTRA?.endsWith('GENERATED'), | ||
is_nullable: rawColumn.IS_NULLABLE === 'YES', | ||
is_unique: rawColumn.COLUMN_KEY === 'UNI', | ||
is_primary_key: | ||
rawColumn.CONSTRAINT_NAME === 'PRIMARY' || rawColumn.COLUMN_KEY === 'PRI', | ||
has_auto_increment: rawColumn.EXTRA === 'auto_increment', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
// onDelete: rawColumn.DELETE_RULE, | ||
// onUpdate: rawColumn.UPDATE_RULE, | ||
}; | ||
} | ||
function parseDefaultValue(value: any) { | ||
// MariaDB returns string NULL for not-nullable varchar fields | ||
return /null|NULL/.test(value) ? null : value; | ||
} | ||
export default class MySQL implements SchemaInspector { | ||
@@ -173,2 +204,3 @@ knex: Knex; | ||
'c.NUMERIC_SCALE', | ||
'c.GENERATION_EXPRESSION', | ||
'fk.REFERENCED_TABLE_NAME', | ||
@@ -208,22 +240,3 @@ 'fk.REFERENCED_COLUMN_NAME', | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: parseDefault(rawColumn.COLUMN_DEFAULT), | ||
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH, | ||
numeric_precision: rawColumn.NUMERIC_PRECISION, | ||
numeric_scale: rawColumn.NUMERIC_SCALE, | ||
is_nullable: rawColumn.IS_NULLABLE === 'YES', | ||
is_unique: rawColumn.COLUMN_KEY === 'UNI', | ||
is_primary_key: | ||
rawColumn.CONSTRAINT_NAME === 'PRIMARY' || | ||
rawColumn.COLUMN_KEY === 'PRI', | ||
has_auto_increment: rawColumn.EXTRA === 'auto_increment', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
// onDelete: rawColumn.DELETE_RULE, | ||
// onUpdate: rawColumn.UPDATE_RULE, | ||
} as Column; | ||
return rawColumnToColumn(rawColumn); | ||
} | ||
@@ -233,32 +246,3 @@ | ||
return records.map( | ||
(rawColumn): Column => { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: parseDefault(rawColumn.COLUMN_DEFAULT), | ||
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH, | ||
numeric_precision: rawColumn.NUMERIC_PRECISION, | ||
numeric_scale: rawColumn.NUMERIC_SCALE, | ||
is_nullable: rawColumn.IS_NULLABLE === 'YES', | ||
is_unique: rawColumn.COLUMN_KEY === 'UNI', | ||
is_primary_key: | ||
rawColumn.CONSTRAINT_NAME === 'PRIMARY' || | ||
rawColumn.COLUMN_KEY === 'PRI', | ||
has_auto_increment: rawColumn.EXTRA === 'auto_increment', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
// onDelete: rawColumn.DELETE_RULE, | ||
// onUpdate: rawColumn.UPDATE_RULE, | ||
}; | ||
} | ||
) as Column[]; | ||
function parseDefault(value: any) { | ||
// MariaDB returns string NULL for not-nullable varchar fields | ||
if (value === 'NULL' || value === 'null') return null; | ||
return value; | ||
} | ||
return records.map(rawColumnToColumn); | ||
} | ||
@@ -265,0 +249,0 @@ |
@@ -24,4 +24,26 @@ import { Knex } from 'knex'; | ||
CONSTRAINT_TYPE: 'P' | 'U' | null; | ||
VIRTUAL_COLUMN: 'YES' | 'NO'; | ||
IDENTITY_COLUMN: 'YES' | 'NO'; | ||
}; | ||
function rawColumnToColumn(rawColumn: RawColumn): Column { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: rawColumn.DATA_DEFAULT, | ||
max_length: rawColumn.DATA_LENGTH, | ||
numeric_precision: rawColumn.DATA_PRECISION, | ||
numeric_scale: rawColumn.DATA_SCALE, | ||
is_generated: rawColumn.VIRTUAL_COLUMN === 'YES', | ||
is_nullable: rawColumn.NULLABLE === 'Y', | ||
is_unique: rawColumn.CONSTRAINT_TYPE === 'U', | ||
is_primary_key: rawColumn.CONSTRAINT_TYPE === 'P', | ||
has_auto_increment: rawColumn.IDENTITY_COLUMN === 'YES', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
}; | ||
} | ||
export default class oracleDB implements SchemaInspector { | ||
@@ -101,3 +123,3 @@ knex: Knex; | ||
) | ||
.from('USER_TAB_COLUMNS'); | ||
.from('USER_TAB_COLS'); | ||
@@ -133,2 +155,4 @@ if (table) { | ||
'c.NULLABLE', | ||
'c.IDENTITY_COLUMN', | ||
'c.VIRTUAL_COLUMN', | ||
'cm.COMMENTS as COLUMN_COMMENT', | ||
@@ -139,3 +163,3 @@ 'pk.CONSTRAINT_TYPE', | ||
) | ||
.from('USER_TAB_COLUMNS as c') | ||
.from('USER_TAB_COLS as c') | ||
.leftJoin('USER_COL_COMMENTS as cm', { | ||
@@ -198,17 +222,3 @@ 'c.TABLE_NAME': 'cm.TABLE_NAME', | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: rawColumn.DATA_DEFAULT, | ||
max_length: rawColumn.DATA_LENGTH, | ||
numeric_precision: rawColumn.DATA_PRECISION, | ||
numeric_scale: rawColumn.DATA_SCALE, | ||
is_nullable: rawColumn.NULLABLE === 'Y', | ||
is_unique: rawColumn.CONSTRAINT_TYPE === 'U', | ||
is_primary_key: rawColumn.CONSTRAINT_TYPE === 'P', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
} as Column; | ||
return rawColumnToColumn(rawColumn); | ||
} | ||
@@ -218,21 +228,3 @@ | ||
return records.map( | ||
(rawColumn): Column => { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: rawColumn.DATA_DEFAULT, | ||
max_length: rawColumn.DATA_LENGTH, | ||
numeric_precision: rawColumn.DATA_PRECISION, | ||
numeric_scale: rawColumn.DATA_SCALE, | ||
is_nullable: rawColumn.NULLABLE === 'Y', | ||
is_unique: rawColumn.CONSTRAINT_TYPE === 'U', | ||
is_primary_key: rawColumn.CONSTRAINT_TYPE === 'P', | ||
foreign_key_column: rawColumn.REFERENCED_COLUMN_NAME, | ||
foreign_key_table: rawColumn.REFERENCED_TABLE_NAME, | ||
comment: rawColumn.COLUMN_COMMENT, | ||
}; | ||
} | ||
) as Column[]; | ||
return records.map(rawColumnToColumn); | ||
} | ||
@@ -246,3 +238,3 @@ | ||
.count<{ count: 0 | 1 }>({ count: '*' }) | ||
.from('USER_TAB_COLUMNS') | ||
.from('USER_TAB_COLS') | ||
.where({ | ||
@@ -249,0 +241,0 @@ TABLE_NAME: table, |
@@ -20,5 +20,8 @@ import { Knex } from 'knex'; | ||
character_maximum_length: number | null; | ||
is_generated: 'YES' | 'NO'; | ||
is_nullable: 'YES' | 'NO'; | ||
is_unique: 'YES' | 'NO'; | ||
is_primary: null | 'YES'; | ||
is_unique: boolean; | ||
is_primary: boolean; | ||
is_identity: 'YES' | 'NO'; | ||
generation_expression: null | string; | ||
numeric_precision: null | number; | ||
@@ -28,7 +31,50 @@ numeric_scale: null | number; | ||
column_comment: string | null; | ||
referenced_table_schema: null | string; | ||
referenced_table_name: null | string; | ||
referenced_column_name: null | string; | ||
foreign_key_schema: null | string; | ||
foreign_key_table: null | string; | ||
foreign_key_column: null | string; | ||
}; | ||
function rawColumnToColumn(rawColumn: RawColumn): Column { | ||
return { | ||
name: rawColumn.column_name, | ||
table: rawColumn.table_name, | ||
data_type: rawColumn.data_type, | ||
default_value: | ||
parseDefaultValue(rawColumn.column_default) || | ||
parseDefaultValue(rawColumn.generation_expression), | ||
max_length: rawColumn.character_maximum_length, | ||
numeric_precision: rawColumn.numeric_precision, | ||
numeric_scale: rawColumn.numeric_scale, | ||
is_generated: rawColumn.is_generated === 'YES', | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
is_unique: rawColumn.is_unique, | ||
is_primary_key: rawColumn.is_primary, | ||
has_auto_increment: | ||
rawColumn.serial !== null || rawColumn.is_identity === 'YES', | ||
comment: rawColumn.column_comment, | ||
schema: rawColumn.table_schema, | ||
foreign_key_schema: rawColumn.foreign_key_schema, | ||
foreign_key_table: rawColumn.foreign_key_table, | ||
foreign_key_column: rawColumn.foreign_key_column, | ||
}; | ||
} | ||
/** | ||
* Converts Postgres default value to JS | ||
* Eg `'example'::character varying` => `example` | ||
*/ | ||
function parseDefaultValue(type: string | null) { | ||
if (!type) return null; | ||
if (type.startsWith('nextval(')) return type; | ||
let [value, cast] = type.split('::'); | ||
value = value.replace(/^\'(.*)\'$/, '$1'); | ||
if (/.*json.*/.test(cast)) return JSON.parse(value); | ||
if (/.*(char|text).*/.test(cast)) return String(value); | ||
return isNaN(value as any) ? value : Number(value); | ||
} | ||
export default class Postgres implements SchemaInspector { | ||
@@ -66,27 +112,2 @@ knex: Knex; | ||
/** | ||
* Converts Postgres default value to JS | ||
* Eg `'example'::character varying` => `example` | ||
*/ | ||
parseDefaultValue(type: string) { | ||
if (!type) return null; | ||
if (type.startsWith('nextval(')) return type; | ||
const parts = type.split('::'); | ||
let value = parts[0]; | ||
if (value.startsWith("'") && value.endsWith("'")) { | ||
value = value.slice(1, -1); | ||
} | ||
if (parts[1] && parts[1].includes('json')) return JSON.parse(value); | ||
if (parts[1] && (parts[1].includes('char') || parts[1].includes('text'))) | ||
return String(value); | ||
if (Number.isNaN(Number(value))) return value; | ||
return Number(value); | ||
} | ||
// Tables | ||
@@ -214,2 +235,3 @@ // =============================================================================================== | ||
'c.character_maximum_length', | ||
'c.is_generated', | ||
'c.is_nullable', | ||
@@ -219,42 +241,5 @@ 'c.numeric_precision', | ||
'c.table_schema', | ||
'c.is_identity', | ||
'c.generation_expression', | ||
knex | ||
.select(knex.raw(`'YES'`)) | ||
.from('pg_index') | ||
.join('pg_attribute', function () { | ||
this.on('pg_attribute.attrelid', '=', 'pg_index.indrelid').andOn( | ||
knex.raw('pg_attribute.attnum = any(pg_index.indkey)') | ||
); | ||
}) | ||
.whereRaw('pg_index.indrelid = quote_ident(c.table_name)::regclass') | ||
.andWhere(knex.raw('pg_attribute.attname = c.column_name')) | ||
.andWhere(knex.raw('pg_index.indisprimary')) | ||
.as('is_primary'), | ||
knex | ||
.select(knex.raw(`case when count(1) >0 then 'YES' else null end`)) | ||
.from('pg_index') | ||
.join('pg_attribute', function () { | ||
this.on('pg_attribute.attrelid', '=', 'pg_index.indrelid').andOn( | ||
knex.raw('pg_attribute.attnum = any(pg_index.indkey)') | ||
); | ||
}) | ||
.whereRaw('pg_index.indrelid = quote_ident(c.table_name)::regclass') | ||
.andWhere(knex.raw('pg_attribute.attname = c.column_name')) | ||
.andWhere(knex.raw('pg_index.indisunique')) | ||
.as('is_unique'), | ||
knex | ||
.select( | ||
knex.raw( | ||
'pg_catalog.col_description(pg_catalog.pg_class.oid, c.ordinal_position:: int)' | ||
) | ||
) | ||
.from('pg_catalog.pg_class') | ||
.whereRaw( | ||
`pg_catalog.pg_class.oid = (select quote_ident(c.table_name):: regclass:: oid)` | ||
) | ||
.andWhere({ 'pg_catalog.pg_class.relname': 'c.table_name' }) | ||
.as('column_comment'), | ||
knex.raw( | ||
@@ -264,5 +249,12 @@ 'pg_get_serial_sequence(quote_ident(c.table_name), c.column_name) as serial' | ||
'ffk.referenced_table_schema', | ||
'ffk.referenced_table_name', | ||
'ffk.referenced_column_name' | ||
knex.raw( | ||
'pg_catalog.col_description(pg_class.oid, c.ordinal_position:: int) as column_comment' | ||
), | ||
knex.raw(`COALESCE(pg.indisunique, false) as is_unique`), | ||
knex.raw(`COALESCE(pg.indisprimary, false) as is_primary`), | ||
'ffk.foreign_key_schema', | ||
'ffk.foreign_key_table', | ||
'ffk.foreign_key_column' | ||
) | ||
@@ -272,10 +264,30 @@ .from(knex.raw('information_schema.columns c')) | ||
` | ||
LEFT JOIN ( | ||
LEFT JOIN pg_catalog.pg_class | ||
ON pg_catalog.pg_class.oid = quote_ident(c.table_name):: regclass:: oid | ||
AND pg_catalog.pg_class.relname = c.table_name | ||
` | ||
) | ||
.joinRaw( | ||
` | ||
LEFT JOIN LATERAL ( | ||
SELECT | ||
k1.table_schema, | ||
k1.table_name, | ||
k1.column_name, | ||
k2.table_schema AS referenced_table_schema, | ||
k2.table_name AS referenced_table_name, | ||
k2.column_name AS referenced_column_name | ||
pg_index.indisprimary, | ||
pg_index.indisunique | ||
FROM pg_index | ||
JOIN pg_attribute | ||
ON pg_attribute.attrelid = pg_index.indrelid | ||
AND pg_attribute.attnum = any(pg_index.indkey) | ||
WHERE pg_index.indrelid = quote_ident(c.table_name)::regclass | ||
AND pg_attribute.attname = c.column_name | ||
LIMIT 1 | ||
) pg ON true | ||
` | ||
) | ||
.joinRaw( | ||
` | ||
LEFT JOIN LATERAL ( | ||
SELECT | ||
k2.table_schema AS foreign_key_schema, | ||
k2.table_name AS foreign_key_table, | ||
k2.column_name AS foreign_key_column | ||
FROM | ||
@@ -286,7 +298,9 @@ information_schema.key_column_usage k1 | ||
) | ||
JOIN information_schema.key_column_usage k2 ON k2.constraint_schema = fk.unique_constraint_schema | ||
AND k2.constraint_name = fk.unique_constraint_name | ||
AND k2.ordinal_position = k1.position_in_unique_constraint | ||
) ffk ON ffk.table_name = c.table_name | ||
AND ffk.column_name = c.column_name | ||
JOIN information_schema.key_column_usage k2 | ||
ON k2.constraint_schema = fk.unique_constraint_schema | ||
AND k2.constraint_name = fk.unique_constraint_name | ||
AND k2.ordinal_position = k1.position_in_unique_constraint | ||
WHERE k1.table_name = c.table_name | ||
AND k1.column_name = c.column_name | ||
) ffk ON TRUE | ||
` | ||
@@ -306,22 +320,3 @@ ) | ||
return { | ||
name: rawColumn.column_name, | ||
table: rawColumn.table_name, | ||
data_type: rawColumn.data_type, | ||
default_value: rawColumn.column_default | ||
? this.parseDefaultValue(rawColumn.column_default) | ||
: null, | ||
max_length: rawColumn.character_maximum_length, | ||
numeric_precision: rawColumn.numeric_precision, | ||
numeric_scale: rawColumn.numeric_scale, | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
is_unique: rawColumn.is_unique === 'YES', | ||
is_primary_key: rawColumn.is_primary === 'YES', | ||
has_auto_increment: rawColumn.serial !== null, | ||
foreign_key_column: rawColumn.referenced_column_name, | ||
foreign_key_table: rawColumn.referenced_table_name, | ||
comment: rawColumn.column_comment, | ||
schema: rawColumn.table_schema, | ||
foreign_key_schema: rawColumn.referenced_table_schema, | ||
} as T extends string ? Column : Column[]; | ||
return rawColumnToColumn(rawColumn); | ||
} | ||
@@ -331,27 +326,3 @@ | ||
return records.map( | ||
(rawColumn): Column => { | ||
return { | ||
name: rawColumn.column_name, | ||
table: rawColumn.table_name, | ||
data_type: rawColumn.data_type, | ||
default_value: rawColumn.column_default | ||
? this.parseDefaultValue(rawColumn.column_default) | ||
: null, | ||
max_length: rawColumn.character_maximum_length, | ||
numeric_precision: rawColumn.numeric_precision, | ||
numeric_scale: rawColumn.numeric_scale, | ||
is_nullable: rawColumn.is_nullable === 'YES', | ||
is_unique: rawColumn.is_unique === 'YES', | ||
is_primary_key: rawColumn.is_primary === 'YES', | ||
has_auto_increment: rawColumn.serial !== null, | ||
foreign_key_column: rawColumn.referenced_column_name, | ||
foreign_key_table: rawColumn.referenced_table_name, | ||
comment: rawColumn.column_comment, | ||
schema: rawColumn.table_schema, | ||
foreign_key_schema: rawColumn.referenced_table_schema, | ||
}; | ||
} | ||
) as T extends string ? Column : Column[]; | ||
return records.map(rawColumnToColumn); | ||
} | ||
@@ -358,0 +329,0 @@ |
@@ -18,2 +18,3 @@ import { Knex } from 'knex'; | ||
pk: 0 | 1; | ||
hidden: number; | ||
}; | ||
@@ -104,3 +105,3 @@ | ||
const columns = await this.knex.raw<RawColumn[]>( | ||
`PRAGMA table_info(??)`, | ||
`PRAGMA table_xinfo(??)`, | ||
table | ||
@@ -137,3 +138,3 @@ ); | ||
const columns: RawColumn[] = await this.knex.raw( | ||
`PRAGMA table_info(??)`, | ||
`PRAGMA table_xinfo(??)`, | ||
table | ||
@@ -164,2 +165,3 @@ ); | ||
numeric_scale: null, | ||
is_generated: raw.hidden !== 0, | ||
is_nullable: raw.notnull === 0, | ||
@@ -200,3 +202,3 @@ is_unique: !!index?.unique, | ||
const results = await this.knex.raw( | ||
`SELECT COUNT(*) AS ct FROM pragma_table_info('${table}') WHERE name='${column}'` | ||
`SELECT COUNT(*) AS ct FROM pragma_table_xinfo('${table}') WHERE name='${column}'` | ||
); | ||
@@ -215,3 +217,3 @@ const resultsVal = results[0]['ct']; | ||
const columns = await this.knex.raw<RawColumn[]>( | ||
`PRAGMA table_info(??)`, | ||
`PRAGMA table_xinfo(??)`, | ||
table | ||
@@ -218,0 +220,0 @@ ); |
@@ -13,4 +13,4 @@ export interface Column { | ||
is_primary_key: boolean; | ||
// Hard to discern in Oracle due to triggers | ||
has_auto_increment?: boolean; | ||
is_generated: boolean; | ||
has_auto_increment: boolean; | ||
foreign_key_table: string | null; | ||
@@ -17,0 +17,0 @@ foreign_key_column: string | null; |
{ | ||
"name": "knex-schema-inspector", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "Utility for extracting information about existing DB schema", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -31,3 +31,3 @@ import knex, { Knex } from 'knex'; | ||
it('returns tables', async () => { | ||
expect(await inspector.tables()).to.deep.equal([ | ||
expect(await inspector.tables()).to.have.deep.members([ | ||
'teams', | ||
@@ -42,3 +42,3 @@ 'users', | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.tableInfo()).to.deep.equal([ | ||
expect(await inspector.tableInfo()).to.have.deep.members([ | ||
{ | ||
@@ -80,3 +80,3 @@ name: 'teams', | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.columns()).to.deep.equal([ | ||
expect(await inspector.columns()).to.have.deep.members([ | ||
{ table: 'teams', column: 'activated_at' }, | ||
@@ -100,3 +100,3 @@ { table: 'teams', column: 'created_at' }, | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.columns('teams')).to.deep.equal([ | ||
expect(await inspector.columns('teams')).to.have.deep.members([ | ||
{ column: 'id', table: 'teams' }, | ||
@@ -115,3 +115,3 @@ { column: 'uuid', table: 'teams' }, | ||
it('returns information for all columns in all tables', async () => { | ||
expect(await inspector.columnInfo()).to.deep.equal([ | ||
expect(await inspector.columnInfo()).to.have.deep.members([ | ||
{ | ||
@@ -125,2 +125,3 @@ name: 'id', | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -141,2 +142,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -157,2 +159,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -173,2 +176,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -189,2 +193,3 @@ is_unique: false, | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -205,2 +210,3 @@ is_unique: false, | ||
numeric_scale: 19, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -221,2 +227,3 @@ is_unique: false, | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -237,2 +244,3 @@ is_unique: false, | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -253,2 +261,3 @@ is_unique: true, | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -269,2 +278,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -285,2 +295,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -301,2 +312,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -317,2 +329,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -333,2 +346,3 @@ is_unique: false, | ||
numeric_scale: 19, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -344,3 +358,3 @@ is_unique: false, | ||
it('returns information for all columns in specific table', async () => { | ||
expect(await inspector.columnInfo('teams')).to.deep.equal([ | ||
expect(await inspector.columnInfo('teams')).to.have.deep.members([ | ||
{ | ||
@@ -354,2 +368,3 @@ name: 'id', | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -370,2 +385,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -386,2 +402,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -402,2 +419,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -418,2 +436,3 @@ is_unique: false, | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -434,2 +453,3 @@ is_unique: false, | ||
numeric_scale: 19, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -450,2 +470,3 @@ is_unique: false, | ||
numeric_scale: 10, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -469,2 +490,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -471,0 +493,0 @@ is_unique: true, |
@@ -41,3 +41,3 @@ import knex, { Knex } from 'knex'; | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.tableInfo()).to.deep.equal([ | ||
expect(await inspector.tableInfo()).to.have.deep.members([ | ||
{ | ||
@@ -87,3 +87,3 @@ name: 'page_visits', | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.columns()).to.deep.equal([ | ||
expect(await inspector.columns()).to.have.deep.members([ | ||
{ table: 'page_visits', column: 'request_path' }, | ||
@@ -107,3 +107,3 @@ { table: 'page_visits', column: 'user_agent' }, | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.columns('teams')).to.deep.equal([ | ||
expect(await inspector.columns('teams')).to.have.deep.members([ | ||
{ column: 'id', table: 'teams' }, | ||
@@ -122,3 +122,3 @@ { column: 'uuid', table: 'teams' }, | ||
it('returns information for all columns in all tables', async () => { | ||
expect(await inspector.columnInfo()).to.deep.equal([ | ||
expect(await inspector.columnInfo()).to.deep.include.members([ | ||
{ | ||
@@ -132,2 +132,3 @@ name: 'team_id', | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -149,2 +150,3 @@ is_unique: false, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -166,2 +168,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -183,2 +186,3 @@ is_unique: true, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -200,2 +204,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -217,2 +222,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -234,2 +240,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -251,2 +258,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -268,2 +276,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -285,2 +294,3 @@ is_unique: false, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -302,2 +312,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -319,2 +330,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -336,2 +348,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -353,2 +366,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -366,3 +380,3 @@ is_unique: false, | ||
it('returns information for all columns in specific table', async () => { | ||
expect(await inspector.columnInfo('teams')).to.deep.equal([ | ||
expect(await inspector.columnInfo('teams')).to.deep.include.members([ | ||
{ | ||
@@ -376,2 +390,3 @@ name: 'id', | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -393,2 +408,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -410,2 +426,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -427,2 +444,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -444,2 +462,3 @@ is_unique: false, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -461,2 +480,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -478,2 +498,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -499,2 +520,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -501,0 +523,0 @@ is_unique: true, |
@@ -64,3 +64,3 @@ import knex, { Knex } from 'knex'; | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.columns()).to.deep.equal([ | ||
expect(await inspector.columns()).to.have.deep.members([ | ||
{ table: 'TEAMS', column: 'ID' }, | ||
@@ -84,3 +84,3 @@ { table: 'TEAMS', column: 'UUID' }, | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.columns('TEAMS')).to.deep.equal([ | ||
expect(await inspector.columns('TEAMS')).to.have.deep.members([ | ||
{ column: 'ID', table: 'TEAMS' }, | ||
@@ -99,14 +99,3 @@ { column: 'UUID', table: 'TEAMS' }, | ||
it('returns information for all columns in all tables', async () => { | ||
const columnInfo = await inspector.columnInfo(); | ||
const teamInfo = columnInfo | ||
.filter((column) => column.table == 'TEAMS') | ||
.sort((a, b) => a.name.localeCompare(b.name)); | ||
const usersInfo = columnInfo | ||
.filter((column) => column.table == 'USERS') | ||
.sort((a, b) => a.name.localeCompare(b.name)); | ||
const pageVisitsInfo = columnInfo | ||
.filter((column) => column.table == 'PAGE_VISITS') | ||
.sort((a, b) => a.name.localeCompare(b.name)); | ||
expect([...teamInfo, ...usersInfo, ...pageVisitsInfo]).to.deep.equal([ | ||
expect(await inspector.columnInfo()).to.have.deep.members([ | ||
{ | ||
@@ -120,5 +109,7 @@ name: 'ACTIVATED_AT', | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -136,5 +127,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -152,5 +145,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -168,5 +163,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -184,5 +181,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -200,5 +199,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -216,5 +217,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
is_unique: true, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -232,5 +235,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -248,5 +253,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -264,5 +271,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -280,5 +289,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: 'ID', | ||
@@ -296,5 +307,7 @@ foreign_key_table: 'TEAMS', | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -312,5 +325,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -328,5 +343,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -340,7 +357,3 @@ foreign_key_table: null, | ||
it('returns information for all columns in specific table', async () => { | ||
const columnInfo = await inspector.columnInfo('TEAMS'); | ||
expect( | ||
columnInfo.sort((a, b) => a.name.localeCompare(b.name)) | ||
).to.deep.equal([ | ||
expect(await inspector.columnInfo('TEAMS')).to.have.deep.members([ | ||
{ | ||
@@ -354,5 +367,7 @@ name: 'ACTIVATED_AT', | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -370,5 +385,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -386,5 +403,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -402,5 +421,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -418,5 +439,7 @@ foreign_key_table: null, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -434,5 +457,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -450,5 +475,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
is_unique: true, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -470,5 +497,7 @@ foreign_key_table: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
is_unique: true, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
@@ -475,0 +504,0 @@ foreign_key_table: null, |
@@ -31,3 +31,3 @@ import knex, { Knex } from 'knex'; | ||
it('returns tables', async () => { | ||
expect(await inspector.tables()).to.deep.equal([ | ||
expect(await inspector.tables()).to.have.deep.members([ | ||
'teams', | ||
@@ -43,3 +43,3 @@ 'users', | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.tableInfo()).to.deep.equal([ | ||
expect(await inspector.tableInfo()).to.have.deep.members([ | ||
{ name: 'camelCase', schema: 'public', comment: null }, | ||
@@ -71,3 +71,3 @@ { name: 'page_visits', schema: 'public', comment: null }, | ||
database.transaction(async (trx) => { | ||
expect(await schemaInspector(trx).columns()).to.deep.equal([ | ||
expect(await schemaInspector(trx).columns()).to.have.deep.members([ | ||
{ table: 'users', column: 'id' }, | ||
@@ -91,3 +91,3 @@ { table: 'page_visits', column: 'request_path' }, | ||
expect(await inspector.columns()).to.deep.equal([ | ||
expect(await inspector.columns()).to.have.deep.members([ | ||
{ table: 'users', column: 'id' }, | ||
@@ -112,3 +112,3 @@ { table: 'page_visits', column: 'request_path' }, | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.columns('teams')).to.deep.equal([ | ||
expect(await inspector.columns('teams')).to.have.deep.members([ | ||
{ table: 'teams', column: 'id' }, | ||
@@ -127,3 +127,3 @@ { table: 'teams', column: 'uuid' }, | ||
it('returns information for all columns in all tables', async () => { | ||
expect(await inspector.columnInfo()).to.deep.equal([ | ||
expect(await inspector.columnInfo()).to.have.deep.members([ | ||
{ | ||
@@ -137,2 +137,3 @@ name: 'primaryKey', | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -156,2 +157,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -175,2 +177,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -194,2 +197,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -213,2 +217,3 @@ is_unique: false, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -232,2 +237,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -251,2 +257,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -270,2 +277,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -289,2 +297,3 @@ is_unique: false, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -296,3 +305,3 @@ is_unique: false, | ||
foreign_key_table: null, | ||
comment: null, | ||
comment: 'Remaining usage credits', | ||
schema: 'public', | ||
@@ -309,2 +318,3 @@ foreign_key_schema: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -328,2 +338,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -347,2 +358,3 @@ is_unique: false, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -366,2 +378,3 @@ is_unique: true, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -385,2 +398,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -404,2 +418,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -419,3 +434,3 @@ is_unique: false, | ||
it('returns information for all columns in specific table', async () => { | ||
expect(await inspector.columnInfo('teams')).to.deep.equal([ | ||
expect(await inspector.columnInfo('teams')).to.have.deep.members([ | ||
{ | ||
@@ -429,2 +444,3 @@ name: 'id', | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -448,2 +464,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -467,2 +484,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -486,2 +504,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -505,2 +524,3 @@ is_unique: false, | ||
numeric_scale: 0, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -512,3 +532,3 @@ is_unique: false, | ||
foreign_key_table: null, | ||
comment: null, | ||
comment: 'Remaining usage credits', | ||
schema: 'public', | ||
@@ -525,2 +545,3 @@ foreign_key_schema: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -544,2 +565,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -568,2 +590,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -570,0 +593,0 @@ is_unique: true, |
@@ -104,3 +104,3 @@ import knex, { Knex } from 'knex'; | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.columns()).to.deep.equal([ | ||
expect(await inspector.columns()).to.have.deep.members([ | ||
{ table: 'page_visits', column: 'request_path' }, | ||
@@ -124,3 +124,3 @@ { table: 'page_visits', column: 'user_agent' }, | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.columns('teams')).to.deep.equal([ | ||
expect(await inspector.columns('teams')).to.have.deep.members([ | ||
{ column: 'id', table: 'teams' }, | ||
@@ -139,3 +139,3 @@ { column: 'uuid', table: 'teams' }, | ||
it('returns information for all columns in all tables', async () => { | ||
expect(await inspector.columnInfo()).to.deep.equal([ | ||
expect(await inspector.columnInfo()).to.have.deep.members([ | ||
{ | ||
@@ -149,2 +149,3 @@ name: 'request_path', | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -165,2 +166,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -181,2 +183,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -197,2 +200,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -213,2 +217,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -229,2 +234,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -245,2 +251,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -261,2 +268,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -277,2 +285,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -293,2 +302,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -309,2 +319,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -325,2 +336,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -341,2 +353,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -357,2 +370,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -369,3 +383,3 @@ is_unique: false, | ||
it('returns information for all columns in specific table', async () => { | ||
expect(await inspector.columnInfo('teams')).to.deep.equal([ | ||
expect(await inspector.columnInfo('teams')).to.have.deep.members([ | ||
{ | ||
@@ -379,2 +393,3 @@ name: 'id', | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -395,2 +410,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -411,2 +427,3 @@ is_unique: true, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -427,2 +444,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -443,2 +461,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -459,2 +478,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -475,2 +495,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: true, | ||
@@ -495,2 +516,3 @@ is_unique: false, | ||
numeric_scale: null, | ||
is_generated: false, | ||
is_nullable: false, | ||
@@ -497,0 +519,0 @@ is_unique: true, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
285047
6193