knex-schema-inspector
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -5,2 +5,3 @@ import { Knex } from 'knex'; | ||
import { Column } from '../types/column'; | ||
import { ForeignKey } from '../types/foreign-key'; | ||
declare type RawColumn = { | ||
@@ -66,13 +67,4 @@ TABLE_NAME: string; | ||
primary(table: string): Promise<string | null>; | ||
foreignKeys(table?: string): Promise<{ | ||
table: string; | ||
column: string; | ||
foreign_key_table: string; | ||
foreign_key_column: string; | ||
foreign_key_schema?: string | undefined; | ||
constraint_name: string | null; | ||
on_update: "NO ACTION" | "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT" | null; | ||
on_delete: "NO ACTION" | "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT" | null; | ||
}[]>; | ||
foreignKeys(table?: string): Promise<ForeignKey[]>; | ||
} | ||
export {}; |
"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) { | ||
@@ -306,15 +295,23 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
MySQL.prototype.foreignKeys = function (table) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, this.knex.raw("\n SELECT DISTINCT\n rc.TABLE_NAME AS 'table',\n kcu.COLUMN_NAME AS 'column',\n rc.REFERENCED_TABLE_NAME AS 'foreign_key_table',\n kcu.REFERENCED_COLUMN_NAME AS 'foreign_key_column',\n rc.CONSTRAINT_NAME AS 'constraint_name',\n rc.UPDATE_RULE AS on_update,\n rc.DELETE_RULE AS on_delete\n FROM\n information_schema.referential_constraints AS rc\n JOIN information_schema.key_column_usage AS kcu ON\n rc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME\n AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA\n WHERE\n rc.CONSTRAINT_SCHEMA = ?;\n ", [this.knex.client.database()])]; | ||
case 1: | ||
result = _b.sent(); | ||
// Mapping casts "RowDataPacket" object from mysql to plain JS object | ||
var query, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
query = this.knex | ||
.select("rc.TABLE_NAME AS table", "kcu.COLUMN_NAME AS column", "rc.REFERENCED_TABLE_NAME AS foreign_key_table", "kcu.REFERENCED_COLUMN_NAME AS foreign_key_column", "rc.CONSTRAINT_NAME AS constraint_name", "rc.UPDATE_RULE AS on_update", "rc.DELETE_RULE AS on_delete") | ||
.from("information_schema.referential_constraints AS rc") | ||
.leftJoin("information_schema.key_column_usage AS kcu ", function () { | ||
this.on("rc.CONSTRAINT_NAME", "=", "kcu.CONSTRAINT_NAME").andOn("kcu.CONSTRAINT_SCHEMA", "=", "rc.CONSTRAINT_SCHEMA"); | ||
}) | ||
.where({ | ||
'rc.CONSTRAINT_SCHEMA': this.knex.client.database(), | ||
}); | ||
if (table) { | ||
return [2 /*return*/, (_a = result === null || result === void 0 ? void 0 : result[0]) === null || _a === void 0 ? void 0 : _a.filter(function (row) { return row.table === table; }).map(function (row) { return (__assign({}, row)); })]; | ||
query.andWhere({ 'rc.TABLE_NAME': table }); | ||
} | ||
return [2 /*return*/, result === null || result === void 0 ? void 0 : result[0].map(function (row) { return (__assign({}, row)); })]; | ||
return [4 /*yield*/, query]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, result]; | ||
} | ||
@@ -321,0 +318,0 @@ }); |
@@ -180,3 +180,3 @@ "use strict"; | ||
.from('sqlite_master') | ||
.whereRaw("sql LIKE \"%AUTOINCREMENT%\"")]; | ||
.whereRaw("sql LIKE '%AUTOINCREMENT%'")]; | ||
case 1: | ||
@@ -183,0 +183,0 @@ tablesWithAutoIncrementPrimaryKeys = (_a.sent()).map(function (_a) { |
@@ -18,2 +18,3 @@ "use strict"; | ||
case 'Client_SQLite3': | ||
case 'Client_BetterSQLite3': | ||
constructor = require('./dialects/sqlite').default; | ||
@@ -20,0 +21,0 @@ break; |
@@ -292,33 +292,29 @@ import { Knex } from 'knex'; | ||
async foreignKeys(table?: string) { | ||
const result = await this.knex.raw<[ForeignKey[]]>( | ||
` | ||
SELECT DISTINCT | ||
rc.TABLE_NAME AS 'table', | ||
kcu.COLUMN_NAME AS 'column', | ||
rc.REFERENCED_TABLE_NAME AS 'foreign_key_table', | ||
kcu.REFERENCED_COLUMN_NAME AS 'foreign_key_column', | ||
rc.CONSTRAINT_NAME AS 'constraint_name', | ||
rc.UPDATE_RULE AS on_update, | ||
rc.DELETE_RULE AS on_delete | ||
FROM | ||
information_schema.referential_constraints AS rc | ||
JOIN information_schema.key_column_usage AS kcu ON | ||
rc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME | ||
AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA | ||
WHERE | ||
rc.CONSTRAINT_SCHEMA = ?; | ||
`, | ||
[this.knex.client.database()] | ||
); | ||
// Mapping casts "RowDataPacket" object from mysql to plain JS object | ||
const query = this.knex | ||
.select( | ||
`rc.TABLE_NAME AS table`, | ||
`kcu.COLUMN_NAME AS column`, | ||
`rc.REFERENCED_TABLE_NAME AS foreign_key_table`, | ||
`kcu.REFERENCED_COLUMN_NAME AS foreign_key_column`, | ||
`rc.CONSTRAINT_NAME AS constraint_name`, | ||
`rc.UPDATE_RULE AS on_update`, | ||
`rc.DELETE_RULE AS on_delete` | ||
) | ||
.from(`information_schema.referential_constraints AS rc`) | ||
.leftJoin(`information_schema.key_column_usage AS kcu `, function () { | ||
this.on(`rc.CONSTRAINT_NAME`, `=`, `kcu.CONSTRAINT_NAME`).andOn( | ||
`kcu.CONSTRAINT_SCHEMA`, | ||
`=`, | ||
`rc.CONSTRAINT_SCHEMA` | ||
); | ||
}) | ||
.where({ | ||
'rc.CONSTRAINT_SCHEMA': this.knex.client.database(), | ||
}); | ||
if (table) { | ||
return result?.[0] | ||
?.filter((row) => row.table === table) | ||
.map((row) => ({ ...row })); | ||
query.andWhere({ 'rc.TABLE_NAME': table }); | ||
} | ||
return result?.[0].map((row) => ({ ...row })); | ||
const result: ForeignKey[] = await query; | ||
return result; | ||
} | ||
} |
@@ -139,3 +139,3 @@ import { Knex } from 'knex'; | ||
.from('sqlite_master') | ||
.whereRaw(`sql LIKE "%AUTOINCREMENT%"`) | ||
.whereRaw(`sql LIKE '%AUTOINCREMENT%'`) | ||
).map(({ name }) => name); | ||
@@ -142,0 +142,0 @@ |
@@ -19,2 +19,3 @@ import { Knex } from 'knex'; | ||
case 'Client_SQLite3': | ||
case 'Client_BetterSQLite3': | ||
constructor = require('./dialects/sqlite').default; | ||
@@ -21,0 +22,0 @@ break; |
{ | ||
"name": "knex-schema-inspector", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Utility for extracting information about existing DB schema", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"prepare": "npm run build", | ||
"lint": "prettier --check .", | ||
"test": "npm run lint && ts-mocha test/**/*.spec.ts --timeout 10000" | ||
}, | ||
"repository": { | ||
@@ -38,2 +44,3 @@ "type": "git", | ||
"@types/node": "^18.11.2", | ||
"better-sqlite3": "^8.6.0", | ||
"chai": "^4.3.6", | ||
@@ -66,8 +73,3 @@ "husky": "^8.0.1", | ||
"lodash.isnil": "^4.0.0" | ||
}, | ||
"scripts": { | ||
"build": "tsc --build", | ||
"lint": "prettier --check .", | ||
"test": "npm run lint && ts-mocha test/**/*.spec.ts --timeout 10000" | ||
} | ||
} | ||
} |
@@ -612,3 +612,17 @@ import knex, { Knex } from 'knex'; | ||
}); | ||
it('filters valid tables based on param', async () => { | ||
expect(await inspector.foreignKeys('users')).to.deep.equal([ | ||
{ | ||
table: 'users', | ||
column: 'team_id', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: 'id', | ||
constraint_name: 'fk_team_id', | ||
on_delete: 'CASCADE', | ||
on_update: 'CASCADE', | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
@@ -6,57 +6,75 @@ import knex, { Knex } from 'knex'; | ||
describe('sqlite', () => { | ||
let database: Knex; | ||
let inspector: SchemaInspector; | ||
for (const sqliteClientName of ['sqlite3', 'better-sqlite3']) { | ||
describe(sqliteClientName, () => { | ||
let database: Knex; | ||
let inspector: SchemaInspector; | ||
before(() => { | ||
database = knex({ | ||
client: 'sqlite3', | ||
connection: { | ||
filename: './test/db/sqlite.db', | ||
}, | ||
before(() => { | ||
database = knex({ | ||
client: sqliteClientName, | ||
connection: { | ||
filename: './test/db/sqlite.db', | ||
}, | ||
}); | ||
inspector = schemaInspector(database); | ||
}); | ||
inspector = schemaInspector(database); | ||
}); | ||
after(async () => { | ||
await database.destroy(); | ||
}); | ||
after(async () => { | ||
await database.destroy(); | ||
}); | ||
describe('.tables', () => { | ||
it('returns tables', async () => { | ||
expect(await inspector.tables()).to.deep.equal([ | ||
'page_visits', | ||
'users', | ||
'teams', | ||
]); | ||
describe('.tables', () => { | ||
it('returns tables', async () => { | ||
expect(await inspector.tables()).to.deep.equal([ | ||
'page_visits', | ||
'users', | ||
'teams', | ||
]); | ||
}); | ||
}); | ||
}); | ||
describe('.tableInfo', () => { | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.tableInfo()).to.deep.equal([ | ||
{ | ||
name: 'page_visits', | ||
sql: | ||
'CREATE TABLE page_visits (\n' + | ||
' request_path varchar(100)\n' + | ||
', user_agent varchar(200)\n' + | ||
', created_at datetime\n' + | ||
')', | ||
}, | ||
{ | ||
name: 'users', | ||
sql: | ||
'CREATE TABLE "users" (\n' + | ||
'\t"id"\tINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n' + | ||
'\t"team_id"\tinteger NOT NULL,\n' + | ||
'\t"email"\tvarchar(100),\n' + | ||
'\t"password"\tvarchar(60),\n' + | ||
`\t"status"\tvarchar(60) DEFAULT 'active',\n` + | ||
'\tFOREIGN KEY("team_id") REFERENCES "teams"("id") ' + | ||
'ON UPDATE CASCADE ' + | ||
'ON DELETE CASCADE\n' + | ||
')', | ||
}, | ||
{ | ||
describe('.tableInfo', () => { | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.tableInfo()).to.deep.equal([ | ||
{ | ||
name: 'page_visits', | ||
sql: | ||
'CREATE TABLE page_visits (\n' + | ||
' request_path varchar(100)\n' + | ||
', user_agent varchar(200)\n' + | ||
', created_at datetime\n' + | ||
')', | ||
}, | ||
{ | ||
name: 'users', | ||
sql: | ||
'CREATE TABLE "users" (\n' + | ||
'\t"id"\tINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n' + | ||
'\t"team_id"\tinteger NOT NULL,\n' + | ||
'\t"email"\tvarchar(100),\n' + | ||
'\t"password"\tvarchar(60),\n' + | ||
`\t"status"\tvarchar(60) DEFAULT 'active',\n` + | ||
'\tFOREIGN KEY("team_id") REFERENCES "teams"("id") ' + | ||
'ON UPDATE CASCADE ' + | ||
'ON DELETE CASCADE\n' + | ||
')', | ||
}, | ||
{ | ||
name: 'teams', | ||
sql: | ||
'CREATE TABLE "teams" (\n' + | ||
'\t"id"\tINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n' + | ||
'\t"uuid"\tvarchar(36) NOT NULL UNIQUE,\n' + | ||
'\t"name"\tvarchar(100) DEFAULT NULL,\n' + | ||
'\t"description"\ttext,\n' + | ||
'\t"credits"\tinteger,\n' + | ||
'\t"created_at"\tdatetime,\n' + | ||
'\t"activated_at"\tdate\n' + | ||
')', | ||
}, | ||
]); | ||
}); | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.tableInfo('teams')).to.deep.equal({ | ||
name: 'teams', | ||
@@ -73,345 +91,434 @@ sql: | ||
')', | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.tableInfo('teams')).to.deep.equal({ | ||
name: 'teams', | ||
sql: | ||
'CREATE TABLE "teams" (\n' + | ||
'\t"id"\tINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n' + | ||
'\t"uuid"\tvarchar(36) NOT NULL UNIQUE,\n' + | ||
'\t"name"\tvarchar(100) DEFAULT NULL,\n' + | ||
'\t"description"\ttext,\n' + | ||
'\t"credits"\tinteger,\n' + | ||
'\t"created_at"\tdatetime,\n' + | ||
'\t"activated_at"\tdate\n' + | ||
')', | ||
describe('.hasTable', () => { | ||
it('returns if table exists or not', async () => { | ||
expect(await inspector.hasTable('teams')).to.equal(true); | ||
expect(await inspector.hasTable('foobar')).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
describe('.hasTable', () => { | ||
it('returns if table exists or not', async () => { | ||
expect(await inspector.hasTable('teams')).to.equal(true); | ||
expect(await inspector.hasTable('foobar')).to.equal(false); | ||
}); | ||
}); | ||
describe('.columns', () => { | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.columns()).to.have.deep.members([ | ||
{ table: 'page_visits', column: 'request_path' }, | ||
{ table: 'page_visits', column: 'user_agent' }, | ||
{ table: 'page_visits', column: 'created_at' }, | ||
{ table: 'teams', column: 'id' }, | ||
{ table: 'teams', column: 'uuid' }, | ||
{ table: 'teams', column: 'name' }, | ||
{ table: 'teams', column: 'description' }, | ||
{ table: 'teams', column: 'credits' }, | ||
{ table: 'teams', column: 'created_at' }, | ||
{ table: 'teams', column: 'activated_at' }, | ||
{ table: 'users', column: 'id' }, | ||
{ table: 'users', column: 'team_id' }, | ||
{ table: 'users', column: 'email' }, | ||
{ table: 'users', column: 'password' }, | ||
{ table: 'users', column: 'status' }, | ||
]); | ||
}); | ||
describe('.columns', () => { | ||
it('returns information for all tables', async () => { | ||
expect(await inspector.columns()).to.have.deep.members([ | ||
{ table: 'page_visits', column: 'request_path' }, | ||
{ table: 'page_visits', column: 'user_agent' }, | ||
{ table: 'page_visits', column: 'created_at' }, | ||
{ table: 'teams', column: 'id' }, | ||
{ table: 'teams', column: 'uuid' }, | ||
{ table: 'teams', column: 'name' }, | ||
{ table: 'teams', column: 'description' }, | ||
{ table: 'teams', column: 'credits' }, | ||
{ table: 'teams', column: 'created_at' }, | ||
{ table: 'teams', column: 'activated_at' }, | ||
{ table: 'users', column: 'id' }, | ||
{ table: 'users', column: 'team_id' }, | ||
{ table: 'users', column: 'email' }, | ||
{ table: 'users', column: 'password' }, | ||
{ table: 'users', column: 'status' }, | ||
]); | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.columns('teams')).to.have.deep.members([ | ||
{ column: 'id', table: 'teams' }, | ||
{ column: 'uuid', table: 'teams' }, | ||
{ column: 'name', table: 'teams' }, | ||
{ column: 'description', table: 'teams' }, | ||
{ column: 'credits', table: 'teams' }, | ||
{ column: 'created_at', table: 'teams' }, | ||
{ column: 'activated_at', table: 'teams' }, | ||
]); | ||
}); | ||
}); | ||
it('returns information for specific table', async () => { | ||
expect(await inspector.columns('teams')).to.have.deep.members([ | ||
{ column: 'id', table: 'teams' }, | ||
{ column: 'uuid', table: 'teams' }, | ||
{ column: 'name', table: 'teams' }, | ||
{ column: 'description', table: 'teams' }, | ||
{ column: 'credits', table: 'teams' }, | ||
{ column: 'created_at', table: 'teams' }, | ||
{ column: 'activated_at', table: 'teams' }, | ||
]); | ||
}); | ||
}); | ||
describe('.columnInfo', () => { | ||
it('returns information for all columns in all tables', async () => { | ||
expect(await inspector.columnInfo()).to.have.deep.members([ | ||
{ | ||
name: 'request_path', | ||
table: 'page_visits', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'user_agent', | ||
table: 'page_visits', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 200, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'created_at', | ||
table: 'page_visits', | ||
data_type: 'datetime', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'id', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'uuid', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 36, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: true, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'name', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'description', | ||
table: 'teams', | ||
data_type: 'text', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'credits', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'created_at', | ||
table: 'teams', | ||
data_type: 'datetime', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'activated_at', | ||
table: 'teams', | ||
data_type: 'date', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'id', | ||
table: 'users', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'team_id', | ||
table: 'users', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: 'id', | ||
foreign_key_table: 'teams', | ||
}, | ||
{ | ||
name: 'email', | ||
table: 'users', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'password', | ||
table: 'users', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 60, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'status', | ||
table: 'users', | ||
data_type: 'varchar', | ||
default_value: 'active', | ||
max_length: 60, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
]); | ||
}); | ||
describe('.columnInfo', () => { | ||
it('returns information for all columns in all tables', async () => { | ||
expect(await inspector.columnInfo()).to.have.deep.members([ | ||
{ | ||
name: 'request_path', | ||
table: 'page_visits', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'user_agent', | ||
table: 'page_visits', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 200, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'created_at', | ||
table: 'page_visits', | ||
data_type: 'datetime', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'id', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'uuid', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 36, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: true, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'name', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'description', | ||
table: 'teams', | ||
data_type: 'text', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'credits', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'created_at', | ||
table: 'teams', | ||
data_type: 'datetime', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'activated_at', | ||
table: 'teams', | ||
data_type: 'date', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'id', | ||
table: 'users', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'team_id', | ||
table: 'users', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: 'id', | ||
foreign_key_table: 'teams', | ||
}, | ||
{ | ||
name: 'email', | ||
table: 'users', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'password', | ||
table: 'users', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 60, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'status', | ||
table: 'users', | ||
data_type: 'varchar', | ||
default_value: 'active', | ||
max_length: 60, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
]); | ||
}); | ||
it('returns information for all columns in specific table', async () => { | ||
expect(await inspector.columnInfo('teams')).to.have.deep.members([ | ||
{ | ||
name: 'id', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'uuid', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 36, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: true, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'name', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'description', | ||
table: 'teams', | ||
data_type: 'text', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'credits', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'created_at', | ||
table: 'teams', | ||
data_type: 'datetime', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'activated_at', | ||
table: 'teams', | ||
data_type: 'date', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
]); | ||
}); | ||
it('returns information for all columns in specific table', async () => { | ||
expect(await inspector.columnInfo('teams')).to.have.deep.members([ | ||
{ | ||
name: 'id', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: false, | ||
is_primary_key: true, | ||
has_auto_increment: true, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
it('returns information for a specific column in a specific table', async () => { | ||
expect(await inspector.columnInfo('teams', 'uuid')).to.deep.equal({ | ||
name: 'uuid', | ||
@@ -432,138 +539,33 @@ table: 'teams', | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'name', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 100, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'description', | ||
table: 'teams', | ||
data_type: 'text', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'credits', | ||
table: 'teams', | ||
data_type: 'integer', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'created_at', | ||
table: 'teams', | ||
data_type: 'datetime', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
{ | ||
name: 'activated_at', | ||
table: 'teams', | ||
data_type: 'date', | ||
default_value: null, | ||
max_length: null, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: true, | ||
is_unique: false, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); | ||
it('returns information for a specific column in a specific table', async () => { | ||
expect(await inspector.columnInfo('teams', 'uuid')).to.deep.equal({ | ||
name: 'uuid', | ||
table: 'teams', | ||
data_type: 'varchar', | ||
default_value: null, | ||
max_length: 36, | ||
numeric_precision: null, | ||
numeric_scale: null, | ||
is_generated: false, | ||
generation_expression: null, | ||
is_nullable: false, | ||
is_unique: true, | ||
is_primary_key: false, | ||
has_auto_increment: false, | ||
foreign_key_column: null, | ||
foreign_key_table: null, | ||
describe('.primary', () => { | ||
it('returns primary key for a table', async () => { | ||
expect(await inspector.primary('teams')).to.equal('id'); | ||
expect(await inspector.primary('page_visits')).to.equal(null); | ||
}); | ||
}); | ||
}); | ||
describe('.primary', () => { | ||
it('returns primary key for a table', async () => { | ||
expect(await inspector.primary('teams')).to.equal('id'); | ||
expect(await inspector.primary('page_visits')).to.equal(null); | ||
}); | ||
}); | ||
describe('.foreignKeys', () => { | ||
it('returns foreign keys for all tables', async () => { | ||
expect(await inspector.foreignKeys()).to.deep.equal([ | ||
{ | ||
table: 'users', | ||
column: 'team_id', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: 'id', | ||
constraint_name: null, | ||
on_delete: 'CASCADE', | ||
on_update: 'CASCADE', | ||
}, | ||
]); | ||
}); | ||
describe('.foreignKeys', () => { | ||
it('returns foreign keys for all tables', async () => { | ||
expect(await inspector.foreignKeys()).to.deep.equal([ | ||
{ | ||
table: 'users', | ||
column: 'team_id', | ||
foreign_key_table: 'teams', | ||
foreign_key_column: 'id', | ||
constraint_name: null, | ||
on_delete: 'CASCADE', | ||
on_update: 'CASCADE', | ||
}, | ||
]); | ||
it('filters based on table param', async () => { | ||
expect(await inspector.foreignKeys('teams')).to.deep.equal([]); | ||
}); | ||
}); | ||
it('filters based on table param', async () => { | ||
expect(await inspector.foreignKeys('teams')).to.deep.equal([]); | ||
}); | ||
}); | ||
}); | ||
} |
409337
9096
19