knex-schema-inspector
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -7,5 +7,12 @@ import { Knex } from 'knex'; | ||
knex: Knex; | ||
_schema?: string; | ||
constructor(knex: Knex); | ||
parseDefaultValue(value: string): string | number | null; | ||
/** | ||
* Set the schema to be used in other methods | ||
*/ | ||
withSchema(schema: string): this; | ||
get schema(): string; | ||
set schema(value: string); | ||
parseDefaultValue(value: string | null): string | number | null; | ||
/** | ||
* List all existing tables in the current schema/database | ||
@@ -12,0 +19,0 @@ */ |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -43,2 +54,21 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
} | ||
// MS SQL specific | ||
// =============================================================================================== | ||
/** | ||
* Set the schema to be used in other methods | ||
*/ | ||
MSSQL.prototype.withSchema = function (schema) { | ||
this.schema = schema; | ||
return this; | ||
}; | ||
Object.defineProperty(MSSQL.prototype, "schema", { | ||
get: function () { | ||
return this._schema || 'dbo'; | ||
}, | ||
set: function (value) { | ||
this._schema = value; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
MSSQL.prototype.parseDefaultValue = function (value) { | ||
@@ -73,2 +103,3 @@ if (!value) | ||
TABLE_CATALOG: this.knex.client.database(), | ||
TABLE_SCHEMA: this.schema, | ||
})]; | ||
@@ -97,2 +128,3 @@ case 1: | ||
TABLE_TYPE: 'BASE TABLE', | ||
TABLE_SCHEMA: this.schema, | ||
}); | ||
@@ -138,2 +170,3 @@ if (!table) return [3 /*break*/, 2]; | ||
table_name: table, | ||
TABLE_SCHEMA: this.schema, | ||
}) | ||
@@ -162,3 +195,6 @@ .first()]; | ||
.from('INFORMATION_SCHEMA.COLUMNS') | ||
.where({ TABLE_CATALOG: this.knex.client.database() }); | ||
.where({ | ||
TABLE_CATALOG: this.knex.client.database(), | ||
TABLE_SCHEMA: this.schema, | ||
}); | ||
if (table) { | ||
@@ -190,35 +226,21 @@ query.andWhere({ TABLE_NAME: table }); | ||
query = this.knex | ||
.select('c.TABLE_NAME', 'c.COLUMN_NAME', 'c.COLUMN_DEFAULT', 'c.DATA_TYPE', 'c.CHARACTER_MAXIMUM_LENGTH', 'c.NUMERIC_PRECISION', 'c.NUMERIC_SCALE', 'c.IS_NULLABLE', 'c.COLLATION_NAME', 'pk.CONSTRAINT_TABLE_NAME', 'pk.CONSTRAINT_COLUMN_NAME', 'pk.CONSTRAINT_NAME', 'pk.PK_SET', 'rc.UPDATE_RULE', 'rc.DELETE_RULE', 'rc.MATCH_OPTION', 'cu.IS_UNIQUE') | ||
.from(dbName + '.INFORMATION_SCHEMA.COLUMNS AS c') | ||
.joinRaw("\n LEFT JOIN (\n SELECT\n CONSTRAINT_NAME AS CONSTRAINT_NAME,\n TABLE_NAME AS CONSTRAINT_TABLE_NAME,\n COLUMN_NAME AS CONSTRAINT_COLUMN_NAME,\n CONSTRAINT_CATALOG,\n CONSTRAINT_SCHEMA,\n PK_SET = CASE WHEN CONSTRAINT_NAME LIKE '%pk%'\n THEN 'PRIMARY'\n ELSE NULL\n END\n FROM " + dbName + ".INFORMATION_SCHEMA.KEY_COLUMN_USAGE\n ) as pk\n ON [c].[TABLE_NAME] = [pk].[CONSTRAINT_TABLE_NAME]\n AND [c].[TABLE_CATALOG] = [pk].[CONSTRAINT_CATALOG]\n AND [c].[COLUMN_NAME] = [pk].[CONSTRAINT_COLUMN_NAME]\n ") | ||
.joinRaw("\n LEFT JOIN (\n SELECT\n CONSTRAINT_NAME,\n CONSTRAINT_CATALOG,\n CONSTRAINT_SCHEMA,\n MATCH_OPTION,\n DELETE_RULE,\n UPDATE_RULE\n FROM " + dbName + ".INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS\n ) AS rc\n ON [pk].[CONSTRAINT_NAME] = [rc].[CONSTRAINT_NAME]\n AND [pk].[CONSTRAINT_CATALOG] = [rc].[CONSTRAINT_CATALOG]\n AND [pk].[CONSTRAINT_SCHEMA] = [rc].[CONSTRAINT_SCHEMA]\n ") | ||
.joinRaw("\n LEFT JOIN (\n SELECT\n COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') AS EXTRA,\n TABLE_NAME,\n COLUMN_NAME,\n TABLE_CATALOG\n FROM\n INFORMATION_SCHEMA.COLUMNS\n WHERE\n COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1) AS ac\n ON [c].[TABLE_NAME] = [ac].[TABLE_NAME]\n AND [c].[TABLE_CATALOG] = [ac].[TABLE_CATALOG]\n AND [c].[COLUMN_NAME] = [ac].[COLUMN_NAME]\n ") | ||
.joinRaw("\n LEFT JOIN (\n SELECT\n Tab.*,\n IS_UNIQUE = CASE\n WHEN CONSTRAINT_TYPE = 'UNIQUE'\n THEN 'YES'\n ELSE NULL\n END\n FROM\n INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,\n INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col\n WHERE\n Col.Constraint_Name = Tab.Constraint_Name\n AND Col.Table_Name = Tab.Table_Name\n AND Tab.CONSTRAINT_TYPE = 'UNIQUE'\n ) AS cu\n ON [c].[TABLE_NAME] = [cu].[Table_Name]\n AND [c].[COLUMN_NAME] = [cu].[Constraint_Name]\n AND [c].[TABLE_CATALOG] =[cu].[TABLE_CATALOG]\n ") | ||
.where({ | ||
'c.TABLE_CATALOG': this.knex.client.database(), | ||
}); | ||
.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]")) | ||
.from(this.knex.raw("??.[sys].[columns] [c]", [dbName])) | ||
.joinRaw("JOIN [sys].[types] [t] ON [c].[user_type_id] = [t].[user_type_id]") | ||
.joinRaw("JOIN [sys].[tables] [o] ON [o].[object_id] = [c].[object_id]") | ||
.joinRaw("JOIN [sys].[schemas] [s] ON [s].[schema_id] = [o].[schema_id]") | ||
.joinRaw("LEFT JOIN [sys].[index_columns] [ic] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id]") | ||
.joinRaw("LEFT JOIN [sys].[indexes] AS [i] ON [i].[object_id] = [c].[object_id] AND [i].[index_id] = [ic].[index_id]") | ||
.joinRaw("LEFT JOIN [sys].[foreign_key_columns] AS [fk] ON [fk].[parent_object_id] = [c].[object_id] AND [fk].[parent_column_id] = [c].[column_id]") | ||
.where({ 's.name': this.schema }); | ||
if (table) { | ||
query.andWhere({ 'c.TABLE_NAME': table }); | ||
query.andWhere({ 'o.name': table }); | ||
} | ||
if (!column) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, query | ||
.andWhere({ 'c.column_name': column }) | ||
.andWhere({ 'c.name': column }) | ||
.first()]; | ||
case 1: | ||
rawColumn = _a.sent(); | ||
return [2 /*return*/, { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: this.parseDefaultValue(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.IS_UNIQUE === 'YES', | ||
is_primary_key: rawColumn.PK_SET === 'PRIMARY', | ||
has_auto_increment: rawColumn.PK_SET === 'PRIMARY', | ||
foreign_key_column: rawColumn.CONSTRAINT_COLUMN_NAME, | ||
foreign_key_table: rawColumn.CONSTRAINT_TABLE_NAME, | ||
}]; | ||
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 })]; | ||
case 2: return [4 /*yield*/, query]; | ||
@@ -228,17 +250,3 @@ case 3: | ||
return [2 /*return*/, records.map(function (rawColumn) { | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: _this.parseDefaultValue(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.IS_UNIQUE === 'YES', | ||
is_primary_key: rawColumn.PK_SET === 'PRIMARY', | ||
has_auto_increment: rawColumn.PK_SET === 'PRIMARY', | ||
foreign_key_column: rawColumn.CONSTRAINT_COLUMN_NAME, | ||
foreign_key_table: rawColumn.CONSTRAINT_TABLE_NAME, | ||
}; | ||
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 }); | ||
})]; | ||
@@ -263,2 +271,3 @@ } | ||
COLUMN_NAME: column, | ||
TABLE_SCHEMA: this.schema, | ||
}) | ||
@@ -278,3 +287,3 @@ .first().count; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.knex.raw("SELECT\n Col.Column_Name\n FROM\n INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,\n INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col\n WHERE\n Col.Constraint_Name = Tab.Constraint_Name\n AND Col.Table_Name = Tab.Table_Name\n AND Constraint_Type = 'PRIMARY KEY'\n AND Col.Table_Name = '" + table + "'")]; | ||
case 0: return [4 /*yield*/, this.knex.raw("SELECT\n Col.Column_Name\n FROM\n INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,\n INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col\n WHERE\n Col.Constraint_Name = Tab.Constraint_Name\n AND Col.Table_Name = Tab.Table_Name\n AND Constraint_Type = 'PRIMARY KEY'\n AND Col.Table_Name = ?\n AND Tab.CONSTRAINT_SCHEMA = ?", [table, this.schema])]; | ||
case 1: | ||
@@ -281,0 +290,0 @@ results = _a.sent(); |
@@ -13,21 +13,15 @@ import { Knex } from 'knex'; | ||
type RawColumn = { | ||
TABLE_NAME: string; | ||
COLUMN_NAME: string; | ||
COLUMN_DEFAULT: any | null; | ||
DATA_TYPE: string; | ||
CHARACTER_MAXIMUM_LENGTH: number | null; | ||
NUMERIC_PRECISION: number | null; | ||
NUMERIC_SCALE: number | null; | ||
IS_NULLABLE: 'YES' | 'NO'; | ||
IS_UNIQUE: 'YES' | 'NO'; | ||
COLLATION_NAME: string | null; | ||
CONSTRAINT_TABLE_NAME: string | null; | ||
CONSTRAINT_COLUMN_NAME: string | null; | ||
EXTRA: number | null; | ||
UPDATE_RULE: string | null; | ||
DELETE_RULE: string | null; | ||
/** @TODO Extend with other possible values */ | ||
COLUMN_KEY: 'PRI' | null; | ||
PK_SET: 'PRIMARY' | null; | ||
table: string; | ||
name: string; | ||
data_type: string; | ||
max_length: number | null; | ||
numeric_precision: number | null; | ||
numeric_scale: number | null; | ||
is_nullable: 'YES' | 'NO'; | ||
default_value: string | null; | ||
is_unique: 'YES' | 'NO'; | ||
is_primary_key: 'YES' | 'NO'; | ||
has_auto_increment: 'YES' | 'NO'; | ||
foreign_key_table: string | null; | ||
foreign_key_column: string | null; | ||
}; | ||
@@ -37,2 +31,3 @@ | ||
knex: Knex; | ||
_schema?: string; | ||
@@ -43,3 +38,22 @@ constructor(knex: Knex) { | ||
parseDefaultValue(value: string) { | ||
// MS SQL specific | ||
// =============================================================================================== | ||
/** | ||
* Set the schema to be used in other methods | ||
*/ | ||
withSchema(schema: string) { | ||
this.schema = schema; | ||
return this; | ||
} | ||
get schema() { | ||
return this._schema || 'dbo'; | ||
} | ||
set schema(value: string) { | ||
this._schema = value; | ||
} | ||
parseDefaultValue(value: string | null) { | ||
if (!value) return null; | ||
@@ -73,2 +87,3 @@ | ||
TABLE_CATALOG: this.knex.client.database(), | ||
TABLE_SCHEMA: this.schema, | ||
}); | ||
@@ -91,2 +106,3 @@ return records.map(({ TABLE_NAME }) => TABLE_NAME); | ||
TABLE_TYPE: 'BASE TABLE', | ||
TABLE_SCHEMA: this.schema, | ||
}); | ||
@@ -129,2 +145,3 @@ | ||
table_name: table, | ||
TABLE_SCHEMA: this.schema, | ||
}) | ||
@@ -148,3 +165,6 @@ .first(); | ||
.from('INFORMATION_SCHEMA.COLUMNS') | ||
.where({ TABLE_CATALOG: this.knex.client.database() }); | ||
.where({ | ||
TABLE_CATALOG: this.knex.client.database(), | ||
TABLE_SCHEMA: this.schema, | ||
}); | ||
@@ -171,106 +191,59 @@ if (table) { | ||
const dbName = this.knex.client.database(); | ||
const query = this.knex | ||
.select( | ||
'c.TABLE_NAME', | ||
'c.COLUMN_NAME', | ||
'c.COLUMN_DEFAULT', | ||
'c.DATA_TYPE', | ||
'c.CHARACTER_MAXIMUM_LENGTH', | ||
'c.NUMERIC_PRECISION', | ||
'c.NUMERIC_SCALE', | ||
'c.IS_NULLABLE', | ||
'c.COLLATION_NAME', | ||
'pk.CONSTRAINT_TABLE_NAME', | ||
'pk.CONSTRAINT_COLUMN_NAME', | ||
'pk.CONSTRAINT_NAME', | ||
'pk.PK_SET', | ||
'rc.UPDATE_RULE', | ||
'rc.DELETE_RULE', | ||
'rc.MATCH_OPTION', | ||
'cu.IS_UNIQUE' | ||
this.knex.raw(` | ||
[o].[name] AS [table], | ||
[c].[name] AS [name], | ||
[t].[name] AS [data_type], | ||
[c].[max_length] AS [max_length], | ||
[c].[precision] AS [numeric_precision], | ||
[c].[scale] AS [numeric_scale], | ||
CASE WHEN [c].[is_nullable] = 0 THEN | ||
'NO' | ||
ELSE | ||
'YES' | ||
END AS [is_nullable], | ||
object_definition ([c].[default_object_id]) AS default_value, | ||
CASE [i].[is_unique] | ||
WHEN 1 THEN | ||
'YES' | ||
ELSE | ||
'NO' | ||
END AS [is_unique], | ||
CASE [i].[is_primary_key] | ||
WHEN 1 THEN | ||
'YES' | ||
ELSE | ||
'NO' | ||
END AS [is_primary_key], | ||
CASE [c].[is_identity] | ||
WHEN 1 THEN | ||
'YES' | ||
ELSE | ||
'NO' | ||
END AS [has_auto_increment], | ||
OBJECT_NAME ([fk].[referenced_object_id]) AS [foreign_key_table], | ||
COL_NAME ([fk].[referenced_object_id], | ||
[fk].[referenced_column_id]) AS [foreign_key_column]`) | ||
) | ||
.from(dbName + '.INFORMATION_SCHEMA.COLUMNS AS c') | ||
.from(this.knex.raw(`??.[sys].[columns] [c]`, [dbName])) | ||
.joinRaw( | ||
` | ||
LEFT JOIN ( | ||
SELECT | ||
CONSTRAINT_NAME AS CONSTRAINT_NAME, | ||
TABLE_NAME AS CONSTRAINT_TABLE_NAME, | ||
COLUMN_NAME AS CONSTRAINT_COLUMN_NAME, | ||
CONSTRAINT_CATALOG, | ||
CONSTRAINT_SCHEMA, | ||
PK_SET = CASE WHEN CONSTRAINT_NAME LIKE '%pk%' | ||
THEN 'PRIMARY' | ||
ELSE NULL | ||
END | ||
FROM ${dbName}.INFORMATION_SCHEMA.KEY_COLUMN_USAGE | ||
) as pk | ||
ON [c].[TABLE_NAME] = [pk].[CONSTRAINT_TABLE_NAME] | ||
AND [c].[TABLE_CATALOG] = [pk].[CONSTRAINT_CATALOG] | ||
AND [c].[COLUMN_NAME] = [pk].[CONSTRAINT_COLUMN_NAME] | ||
` | ||
`JOIN [sys].[types] [t] ON [c].[user_type_id] = [t].[user_type_id]` | ||
) | ||
.joinRaw(`JOIN [sys].[tables] [o] ON [o].[object_id] = [c].[object_id]`) | ||
.joinRaw(`JOIN [sys].[schemas] [s] ON [s].[schema_id] = [o].[schema_id]`) | ||
.joinRaw( | ||
` | ||
LEFT JOIN ( | ||
SELECT | ||
CONSTRAINT_NAME, | ||
CONSTRAINT_CATALOG, | ||
CONSTRAINT_SCHEMA, | ||
MATCH_OPTION, | ||
DELETE_RULE, | ||
UPDATE_RULE | ||
FROM ${dbName}.INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS | ||
) AS rc | ||
ON [pk].[CONSTRAINT_NAME] = [rc].[CONSTRAINT_NAME] | ||
AND [pk].[CONSTRAINT_CATALOG] = [rc].[CONSTRAINT_CATALOG] | ||
AND [pk].[CONSTRAINT_SCHEMA] = [rc].[CONSTRAINT_SCHEMA] | ||
` | ||
`LEFT JOIN [sys].[index_columns] [ic] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id]` | ||
) | ||
.joinRaw( | ||
` | ||
LEFT JOIN ( | ||
SELECT | ||
COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') AS EXTRA, | ||
TABLE_NAME, | ||
COLUMN_NAME, | ||
TABLE_CATALOG | ||
FROM | ||
INFORMATION_SCHEMA.COLUMNS | ||
WHERE | ||
COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1) AS ac | ||
ON [c].[TABLE_NAME] = [ac].[TABLE_NAME] | ||
AND [c].[TABLE_CATALOG] = [ac].[TABLE_CATALOG] | ||
AND [c].[COLUMN_NAME] = [ac].[COLUMN_NAME] | ||
` | ||
`LEFT JOIN [sys].[indexes] AS [i] ON [i].[object_id] = [c].[object_id] AND [i].[index_id] = [ic].[index_id]` | ||
) | ||
.joinRaw( | ||
` | ||
LEFT JOIN ( | ||
SELECT | ||
Tab.*, | ||
IS_UNIQUE = CASE | ||
WHEN CONSTRAINT_TYPE = 'UNIQUE' | ||
THEN 'YES' | ||
ELSE NULL | ||
END | ||
FROM | ||
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, | ||
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col | ||
WHERE | ||
Col.Constraint_Name = Tab.Constraint_Name | ||
AND Col.Table_Name = Tab.Table_Name | ||
AND Tab.CONSTRAINT_TYPE = 'UNIQUE' | ||
) AS cu | ||
ON [c].[TABLE_NAME] = [cu].[Table_Name] | ||
AND [c].[COLUMN_NAME] = [cu].[Constraint_Name] | ||
AND [c].[TABLE_CATALOG] =[cu].[TABLE_CATALOG] | ||
` | ||
`LEFT JOIN [sys].[foreign_key_columns] AS [fk] ON [fk].[parent_object_id] = [c].[object_id] AND [fk].[parent_column_id] = [c].[column_id]` | ||
) | ||
.where({ | ||
'c.TABLE_CATALOG': this.knex.client.database(), | ||
}); | ||
.where({ 's.name': this.schema }); | ||
if (table) { | ||
query.andWhere({ 'c.TABLE_NAME': table }); | ||
query.andWhere({ 'o.name': table }); | ||
} | ||
@@ -280,19 +253,14 @@ | ||
const rawColumn: RawColumn = await query | ||
.andWhere({ 'c.column_name': column }) | ||
.andWhere({ 'c.name': column }) | ||
.first(); | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: this.parseDefaultValue(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.IS_UNIQUE === 'YES', | ||
is_primary_key: rawColumn.PK_SET === 'PRIMARY', | ||
has_auto_increment: rawColumn.PK_SET === 'PRIMARY', | ||
foreign_key_column: rawColumn.CONSTRAINT_COLUMN_NAME, | ||
foreign_key_table: rawColumn.CONSTRAINT_TABLE_NAME, | ||
...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; | ||
@@ -306,16 +274,11 @@ } | ||
return { | ||
name: rawColumn.COLUMN_NAME, | ||
table: rawColumn.TABLE_NAME, | ||
data_type: rawColumn.DATA_TYPE, | ||
default_value: this.parseDefaultValue(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.IS_UNIQUE === 'YES', | ||
is_primary_key: rawColumn.PK_SET === 'PRIMARY', | ||
has_auto_increment: rawColumn.PK_SET === 'PRIMARY', | ||
foreign_key_column: rawColumn.CONSTRAINT_COLUMN_NAME, | ||
foreign_key_table: rawColumn.CONSTRAINT_TABLE_NAME, | ||
}; | ||
...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; | ||
} | ||
@@ -336,2 +299,3 @@ ) as Column[]; | ||
COLUMN_NAME: column, | ||
TABLE_SCHEMA: this.schema, | ||
}) | ||
@@ -356,4 +320,7 @@ .first(); | ||
AND Constraint_Type = 'PRIMARY KEY' | ||
AND Col.Table_Name = '${table}'` | ||
AND Col.Table_Name = ? | ||
AND Tab.CONSTRAINT_SCHEMA = ?`, | ||
[table, this.schema] | ||
); | ||
const columnName = results.length > 0 ? results[0]['Column_Name'] : null; | ||
@@ -360,0 +327,0 @@ return columnName as string; |
{ | ||
"name": "knex-schema-inspector", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Utility for extracting information about existing DB schema", | ||
@@ -5,0 +5,0 @@ "main": "dist/lib/index.js", |
@@ -117,11 +117,11 @@ import knex, { Knex } from 'knex'; | ||
default_value: null, | ||
max_length: null, | ||
max_length: 4, | ||
numeric_precision: 10, | ||
numeric_scale: 0, | ||
numeric_scale: 10, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_unique: true, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: 'id', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
@@ -140,4 +140,4 @@ { | ||
has_auto_increment: false, | ||
foreign_key_column: 'uuid', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
@@ -179,5 +179,5 @@ { | ||
default_value: null, | ||
max_length: null, | ||
max_length: 4, | ||
numeric_precision: 10, | ||
numeric_scale: 0, | ||
numeric_scale: 10, | ||
is_nullable: true, | ||
@@ -195,5 +195,5 @@ is_unique: false, | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
max_length: 6, | ||
numeric_precision: 19, | ||
numeric_scale: 19, | ||
is_nullable: true, | ||
@@ -211,5 +211,5 @@ is_unique: false, | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
max_length: 3, | ||
numeric_precision: 10, | ||
numeric_scale: 10, | ||
is_nullable: true, | ||
@@ -227,11 +227,11 @@ is_unique: false, | ||
default_value: null, | ||
max_length: null, | ||
max_length: 4, | ||
numeric_precision: 10, | ||
numeric_scale: 0, | ||
numeric_scale: 10, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_unique: true, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: 'id', | ||
foreign_key_table: 'users', | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
@@ -243,5 +243,5 @@ { | ||
default_value: null, | ||
max_length: null, | ||
max_length: 4, | ||
numeric_precision: 10, | ||
numeric_scale: 0, | ||
numeric_scale: 10, | ||
is_nullable: false, | ||
@@ -251,4 +251,4 @@ is_unique: false, | ||
has_auto_increment: false, | ||
foreign_key_column: 'team_id', | ||
foreign_key_table: 'users', | ||
foreign_key_column: 'id', | ||
foreign_key_table: 'teams', | ||
}, | ||
@@ -320,5 +320,5 @@ { | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
max_length: 6, | ||
numeric_precision: 19, | ||
numeric_scale: 19, | ||
is_nullable: true, | ||
@@ -340,11 +340,11 @@ is_unique: false, | ||
default_value: null, | ||
max_length: null, | ||
max_length: 4, | ||
numeric_precision: 10, | ||
numeric_scale: 0, | ||
numeric_scale: 10, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_unique: true, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: 'id', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
@@ -363,4 +363,4 @@ { | ||
has_auto_increment: false, | ||
foreign_key_column: 'uuid', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
@@ -402,5 +402,5 @@ { | ||
default_value: null, | ||
max_length: null, | ||
max_length: 4, | ||
numeric_precision: 10, | ||
numeric_scale: 0, | ||
numeric_scale: 10, | ||
is_nullable: true, | ||
@@ -418,5 +418,5 @@ is_unique: false, | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
max_length: 6, | ||
numeric_precision: 19, | ||
numeric_scale: 19, | ||
is_nullable: true, | ||
@@ -434,5 +434,5 @@ is_unique: false, | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
max_length: 3, | ||
numeric_precision: 10, | ||
numeric_scale: 10, | ||
is_nullable: true, | ||
@@ -460,4 +460,4 @@ is_unique: false, | ||
has_auto_increment: false, | ||
foreign_key_column: 'uuid', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}); | ||
@@ -464,0 +464,0 @@ }); |
471666
9664