@uql/core 3.1.0 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +134 -176
- package/README.md +413 -0
- package/package.json +31 -26
- package/dist/package.json +0 -131
- package/src/@types/index.d.ts +0 -1
- package/src/@types/jest.d.ts +0 -6
- package/src/browser/http/bus.spec.ts +0 -22
- package/src/browser/http/bus.ts +0 -17
- package/src/browser/http/http.spec.ts +0 -70
- package/src/browser/http/http.ts +0 -55
- package/src/browser/http/index.ts +0 -2
- package/src/browser/index.ts +0 -4
- package/src/browser/options.spec.ts +0 -37
- package/src/browser/options.ts +0 -18
- package/src/browser/querier/genericClientRepository.spec.ts +0 -105
- package/src/browser/querier/genericClientRepository.ts +0 -49
- package/src/browser/querier/httpQuerier.ts +0 -82
- package/src/browser/querier/index.ts +0 -3
- package/src/browser/querier/querier.util.spec.ts +0 -35
- package/src/browser/querier/querier.util.ts +0 -18
- package/src/browser/type/clientQuerier.ts +0 -45
- package/src/browser/type/clientQuerierPool.ts +0 -5
- package/src/browser/type/clientRepository.ts +0 -22
- package/src/browser/type/index.ts +0 -4
- package/src/browser/type/request.ts +0 -25
- package/src/dialect/abstractDialect.ts +0 -28
- package/src/dialect/abstractSqlDialect-spec.ts +0 -1309
- package/src/dialect/abstractSqlDialect.ts +0 -805
- package/src/dialect/index.ts +0 -3
- package/src/dialect/namingStrategy.spec.ts +0 -52
- package/src/dialect/queryContext.ts +0 -69
- package/src/entity/decorator/definition.spec.ts +0 -736
- package/src/entity/decorator/definition.ts +0 -265
- package/src/entity/decorator/entity.ts +0 -8
- package/src/entity/decorator/field.ts +0 -9
- package/src/entity/decorator/id.ts +0 -9
- package/src/entity/decorator/index.ts +0 -5
- package/src/entity/decorator/relation.spec.ts +0 -41
- package/src/entity/decorator/relation.ts +0 -34
- package/src/entity/index.ts +0 -1
- package/src/express/@types/express.d.ts +0 -8
- package/src/express/@types/index.d.ts +0 -1
- package/src/express/index.ts +0 -2
- package/src/express/querierMiddleware.ts +0 -217
- package/src/express/query.util.spec.ts +0 -40
- package/src/express/query.util.ts +0 -21
- package/src/index.ts +0 -9
- package/src/maria/index.ts +0 -3
- package/src/maria/mariaDialect.spec.ts +0 -207
- package/src/maria/mariaDialect.ts +0 -42
- package/src/maria/mariaQuerierPool.test.ts +0 -23
- package/src/maria/mariadbQuerier.test.ts +0 -23
- package/src/maria/mariadbQuerier.ts +0 -45
- package/src/maria/mariadbQuerierPool.ts +0 -21
- package/src/migrate/cli.ts +0 -301
- package/src/migrate/generator/index.ts +0 -4
- package/src/migrate/generator/mongoSchemaGenerator.spec.ts +0 -112
- package/src/migrate/generator/mongoSchemaGenerator.ts +0 -115
- package/src/migrate/generator/mysqlSchemaGenerator.spec.ts +0 -34
- package/src/migrate/generator/mysqlSchemaGenerator.ts +0 -92
- package/src/migrate/generator/postgresSchemaGenerator.spec.ts +0 -44
- package/src/migrate/generator/postgresSchemaGenerator.ts +0 -127
- package/src/migrate/generator/sqliteSchemaGenerator.spec.ts +0 -33
- package/src/migrate/generator/sqliteSchemaGenerator.ts +0 -81
- package/src/migrate/index.ts +0 -41
- package/src/migrate/introspection/index.ts +0 -4
- package/src/migrate/introspection/mongoIntrospector.spec.ts +0 -75
- package/src/migrate/introspection/mongoIntrospector.ts +0 -47
- package/src/migrate/introspection/mysqlIntrospector.spec.ts +0 -113
- package/src/migrate/introspection/mysqlIntrospector.ts +0 -278
- package/src/migrate/introspection/postgresIntrospector.spec.ts +0 -112
- package/src/migrate/introspection/postgresIntrospector.ts +0 -329
- package/src/migrate/introspection/sqliteIntrospector.spec.ts +0 -112
- package/src/migrate/introspection/sqliteIntrospector.ts +0 -296
- package/src/migrate/migrator-mongo.test.ts +0 -54
- package/src/migrate/migrator.spec.ts +0 -255
- package/src/migrate/migrator.test.ts +0 -94
- package/src/migrate/migrator.ts +0 -719
- package/src/migrate/namingStrategy.spec.ts +0 -22
- package/src/migrate/schemaGenerator-advanced.spec.ts +0 -138
- package/src/migrate/schemaGenerator.spec.ts +0 -190
- package/src/migrate/schemaGenerator.ts +0 -478
- package/src/migrate/storage/databaseStorage.spec.ts +0 -69
- package/src/migrate/storage/databaseStorage.ts +0 -100
- package/src/migrate/storage/index.ts +0 -2
- package/src/migrate/storage/jsonStorage.ts +0 -58
- package/src/migrate/type.ts +0 -1
- package/src/mongo/index.ts +0 -3
- package/src/mongo/mongoDialect.spec.ts +0 -251
- package/src/mongo/mongoDialect.ts +0 -238
- package/src/mongo/mongodbQuerier.test.ts +0 -45
- package/src/mongo/mongodbQuerier.ts +0 -256
- package/src/mongo/mongodbQuerierPool.test.ts +0 -25
- package/src/mongo/mongodbQuerierPool.ts +0 -24
- package/src/mysql/index.ts +0 -3
- package/src/mysql/mysql2Querier.test.ts +0 -20
- package/src/mysql/mysql2Querier.ts +0 -49
- package/src/mysql/mysql2QuerierPool.test.ts +0 -20
- package/src/mysql/mysql2QuerierPool.ts +0 -21
- package/src/mysql/mysqlDialect.spec.ts +0 -20
- package/src/mysql/mysqlDialect.ts +0 -16
- package/src/namingStrategy/defaultNamingStrategy.ts +0 -18
- package/src/namingStrategy/index.spec.ts +0 -36
- package/src/namingStrategy/index.ts +0 -2
- package/src/namingStrategy/snakeCaseNamingStrategy.ts +0 -15
- package/src/options.spec.ts +0 -41
- package/src/options.ts +0 -18
- package/src/postgres/index.ts +0 -3
- package/src/postgres/manual-types.d.ts +0 -4
- package/src/postgres/pgQuerier.test.ts +0 -25
- package/src/postgres/pgQuerier.ts +0 -45
- package/src/postgres/pgQuerierPool.test.ts +0 -28
- package/src/postgres/pgQuerierPool.ts +0 -21
- package/src/postgres/postgresDialect.spec.ts +0 -428
- package/src/postgres/postgresDialect.ts +0 -144
- package/src/querier/abstractQuerier-test.ts +0 -584
- package/src/querier/abstractQuerier.ts +0 -353
- package/src/querier/abstractQuerierPool-test.ts +0 -20
- package/src/querier/abstractQuerierPool.ts +0 -18
- package/src/querier/abstractSqlQuerier-spec.ts +0 -979
- package/src/querier/abstractSqlQuerier-test.ts +0 -21
- package/src/querier/abstractSqlQuerier.ts +0 -138
- package/src/querier/decorator/index.ts +0 -3
- package/src/querier/decorator/injectQuerier.spec.ts +0 -74
- package/src/querier/decorator/injectQuerier.ts +0 -45
- package/src/querier/decorator/serialized.spec.ts +0 -98
- package/src/querier/decorator/serialized.ts +0 -13
- package/src/querier/decorator/transactional.spec.ts +0 -240
- package/src/querier/decorator/transactional.ts +0 -56
- package/src/querier/index.ts +0 -4
- package/src/repository/genericRepository.spec.ts +0 -111
- package/src/repository/genericRepository.ts +0 -74
- package/src/repository/index.ts +0 -1
- package/src/sqlite/index.ts +0 -3
- package/src/sqlite/manual-types.d.ts +0 -4
- package/src/sqlite/sqliteDialect.spec.ts +0 -155
- package/src/sqlite/sqliteDialect.ts +0 -76
- package/src/sqlite/sqliteQuerier.spec.ts +0 -36
- package/src/sqlite/sqliteQuerier.test.ts +0 -21
- package/src/sqlite/sqliteQuerier.ts +0 -37
- package/src/sqlite/sqliteQuerierPool.test.ts +0 -12
- package/src/sqlite/sqliteQuerierPool.ts +0 -38
- package/src/test/entityMock.ts +0 -375
- package/src/test/index.ts +0 -3
- package/src/test/it.util.ts +0 -69
- package/src/test/spec.util.ts +0 -57
- package/src/type/entity.ts +0 -218
- package/src/type/index.ts +0 -9
- package/src/type/migration.ts +0 -241
- package/src/type/namingStrategy.ts +0 -17
- package/src/type/querier.ts +0 -143
- package/src/type/querierPool.ts +0 -26
- package/src/type/query.ts +0 -506
- package/src/type/repository.ts +0 -142
- package/src/type/universalQuerier.ts +0 -133
- package/src/type/utility.ts +0 -21
- package/src/util/dialect.util-extra.spec.ts +0 -96
- package/src/util/dialect.util.spec.ts +0 -23
- package/src/util/dialect.util.ts +0 -134
- package/src/util/index.ts +0 -5
- package/src/util/object.util.spec.ts +0 -29
- package/src/util/object.util.ts +0 -27
- package/src/util/raw.ts +0 -11
- package/src/util/sql.util-extra.spec.ts +0 -17
- package/src/util/sql.util.spec.ts +0 -208
- package/src/util/sql.util.ts +0 -104
- package/src/util/string.util.spec.ts +0 -46
- package/src/util/string.util.ts +0 -35
- package/tsconfig.build.json +0 -5
- package/tsconfig.json +0 -8
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, jest } from 'bun:test';
|
|
2
|
-
import type { MongoQuerier, QuerierPool } from '../../type/index.js';
|
|
3
|
-
import { MongoSchemaIntrospector } from './mongoIntrospector.js';
|
|
4
|
-
|
|
5
|
-
describe('MongoSchemaIntrospector', () => {
|
|
6
|
-
let introspector: MongoSchemaIntrospector;
|
|
7
|
-
let pool: QuerierPool;
|
|
8
|
-
let querier: MongoQuerier;
|
|
9
|
-
let db: any;
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
db = {
|
|
13
|
-
listCollections: jest.fn<any>().mockReturnValue({
|
|
14
|
-
toArray: jest.fn<any>().mockResolvedValue([]),
|
|
15
|
-
}),
|
|
16
|
-
collection: jest.fn<any>().mockReturnValue({
|
|
17
|
-
indexes: jest.fn<any>().mockResolvedValue([]),
|
|
18
|
-
}),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
querier = {
|
|
22
|
-
db,
|
|
23
|
-
release: jest.fn<any>().mockResolvedValue(undefined) as any,
|
|
24
|
-
} as any;
|
|
25
|
-
|
|
26
|
-
pool = {
|
|
27
|
-
getQuerier: jest.fn<any>().mockResolvedValue(querier) as any,
|
|
28
|
-
} as any;
|
|
29
|
-
|
|
30
|
-
introspector = new MongoSchemaIntrospector(pool);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('getTableNames should return collection names', async () => {
|
|
34
|
-
db.listCollections.mockReturnValueOnce({
|
|
35
|
-
toArray: jest.fn<any>().mockResolvedValue([{ name: 'users' }, { name: 'posts' }]),
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const names = await introspector.getTableNames();
|
|
39
|
-
|
|
40
|
-
expect(names).toEqual(['users', 'posts']);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('getTableSchema should return collection details', async () => {
|
|
44
|
-
db.listCollections.mockReturnValueOnce({
|
|
45
|
-
toArray: jest.fn<any>().mockResolvedValue([{ name: 'users' }]),
|
|
46
|
-
});
|
|
47
|
-
db.collection.mockReturnValueOnce({
|
|
48
|
-
indexes: jest.fn<any>().mockResolvedValue([
|
|
49
|
-
{ name: '_id_', key: { _id: 1 } },
|
|
50
|
-
{ name: 'idx_username', key: { username: 1 }, unique: true },
|
|
51
|
-
]),
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const schema = await introspector.getTableSchema('users');
|
|
55
|
-
|
|
56
|
-
expect(schema).toBeDefined();
|
|
57
|
-
expect(schema?.name).toBe('users');
|
|
58
|
-
expect(schema?.indexes).toHaveLength(2);
|
|
59
|
-
expect(schema?.indexes?.[1]).toMatchObject({
|
|
60
|
-
name: 'idx_username',
|
|
61
|
-
columns: ['username'],
|
|
62
|
-
unique: true,
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('getTableSchema should return undefined for non-existent collection', async () => {
|
|
67
|
-
db.listCollections.mockReturnValueOnce({
|
|
68
|
-
toArray: jest.fn<any>().mockResolvedValue([]),
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
const schema = await introspector.getTableSchema('non_existent');
|
|
72
|
-
|
|
73
|
-
expect(schema).toBeUndefined();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { MongoQuerier, QuerierPool, SchemaIntrospector, TableSchema } from '../../type/index.js';
|
|
2
|
-
|
|
3
|
-
export class MongoSchemaIntrospector implements SchemaIntrospector {
|
|
4
|
-
constructor(private readonly querierPool: QuerierPool) {}
|
|
5
|
-
|
|
6
|
-
async getTableSchema(tableName: string): Promise<TableSchema | undefined> {
|
|
7
|
-
const querier = await this.querierPool.getQuerier();
|
|
8
|
-
try {
|
|
9
|
-
const { db } = querier as MongoQuerier;
|
|
10
|
-
const collections = await db.listCollections({ name: tableName }).toArray();
|
|
11
|
-
if (collections.length === 0) {
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// MongoDB doesn't have a fixed schema, but we can look at the indexes
|
|
16
|
-
const indexes = await db.collection(tableName).indexes();
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
name: tableName,
|
|
20
|
-
columns: [], // We don't have columns in Mongo
|
|
21
|
-
indexes: indexes.map((idx: any) => ({
|
|
22
|
-
name: idx.name,
|
|
23
|
-
columns: Object.keys(idx.key),
|
|
24
|
-
unique: !!idx.unique,
|
|
25
|
-
})),
|
|
26
|
-
};
|
|
27
|
-
} finally {
|
|
28
|
-
await querier.release();
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async getTableNames(): Promise<string[]> {
|
|
33
|
-
const querier = await this.querierPool.getQuerier();
|
|
34
|
-
try {
|
|
35
|
-
const { db } = querier as MongoQuerier;
|
|
36
|
-
const collections = await db.listCollections().toArray();
|
|
37
|
-
return collections.map((c: any) => c.name);
|
|
38
|
-
} finally {
|
|
39
|
-
await querier.release();
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async tableExists(tableName: string): Promise<boolean> {
|
|
44
|
-
const names = await this.getTableNames();
|
|
45
|
-
return names.includes(tableName);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, jest } from 'bun:test';
|
|
2
|
-
import type { QuerierPool, SqlQuerier } from '../../type/index.js';
|
|
3
|
-
import { MysqlSchemaIntrospector } from './mysqlIntrospector.js';
|
|
4
|
-
|
|
5
|
-
describe('MysqlSchemaIntrospector', () => {
|
|
6
|
-
let introspector: MysqlSchemaIntrospector;
|
|
7
|
-
let pool: QuerierPool;
|
|
8
|
-
let querier: SqlQuerier;
|
|
9
|
-
|
|
10
|
-
let mockAll: ReturnType<typeof jest.fn>;
|
|
11
|
-
let mockRun: ReturnType<typeof jest.fn>;
|
|
12
|
-
let mockRelease: ReturnType<typeof jest.fn>;
|
|
13
|
-
let mockGetQuerier: ReturnType<typeof jest.fn>;
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
mockAll = jest.fn<any>().mockResolvedValue([]);
|
|
17
|
-
mockRun = jest.fn<any>().mockResolvedValue({});
|
|
18
|
-
mockRelease = jest.fn<any>().mockResolvedValue(undefined);
|
|
19
|
-
|
|
20
|
-
querier = {
|
|
21
|
-
all: mockAll,
|
|
22
|
-
run: mockRun,
|
|
23
|
-
release: mockRelease,
|
|
24
|
-
dialect: { escapeIdChar: '`' },
|
|
25
|
-
} as any;
|
|
26
|
-
|
|
27
|
-
mockGetQuerier = jest.fn<any>().mockResolvedValue(querier);
|
|
28
|
-
pool = {
|
|
29
|
-
getQuerier: mockGetQuerier,
|
|
30
|
-
} as any;
|
|
31
|
-
|
|
32
|
-
introspector = new MysqlSchemaIntrospector(pool);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('getTableNames should return a list of tables', async () => {
|
|
36
|
-
mockAll.mockResolvedValueOnce([{ table_name: 'users' }, { table_name: 'posts' }]);
|
|
37
|
-
|
|
38
|
-
const names = await introspector.getTableNames();
|
|
39
|
-
|
|
40
|
-
expect(names).toEqual(['users', 'posts']);
|
|
41
|
-
expect(querier.all).toHaveBeenCalledWith(expect.stringContaining('information_schema.TABLES'));
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('getTableSchema should return table details', async () => {
|
|
45
|
-
// 1. tableExists check
|
|
46
|
-
mockAll.mockResolvedValueOnce([{ count: 1 }]);
|
|
47
|
-
// 2. getColumns
|
|
48
|
-
mockAll.mockResolvedValueOnce([
|
|
49
|
-
{
|
|
50
|
-
column_name: 'id',
|
|
51
|
-
data_type: 'int',
|
|
52
|
-
column_type: 'int(11)',
|
|
53
|
-
is_nullable: 'NO',
|
|
54
|
-
column_default: null,
|
|
55
|
-
character_maximum_length: null,
|
|
56
|
-
numeric_precision: 10,
|
|
57
|
-
numeric_scale: 0,
|
|
58
|
-
column_key: 'PRI',
|
|
59
|
-
extra: 'auto_increment',
|
|
60
|
-
column_comment: 'User ID',
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
column_name: 'email',
|
|
64
|
-
data_type: 'varchar',
|
|
65
|
-
column_type: 'varchar(255)',
|
|
66
|
-
is_nullable: 'YES',
|
|
67
|
-
column_default: null,
|
|
68
|
-
character_maximum_length: 255,
|
|
69
|
-
numeric_precision: null,
|
|
70
|
-
numeric_scale: null,
|
|
71
|
-
column_key: 'UNI',
|
|
72
|
-
extra: '',
|
|
73
|
-
column_comment: null,
|
|
74
|
-
},
|
|
75
|
-
]);
|
|
76
|
-
// 3. getIndexes
|
|
77
|
-
mockAll.mockResolvedValueOnce([
|
|
78
|
-
{
|
|
79
|
-
index_name: 'email_unique',
|
|
80
|
-
columns: 'email',
|
|
81
|
-
is_unique: 1,
|
|
82
|
-
},
|
|
83
|
-
]);
|
|
84
|
-
// 4. getForeignKeys
|
|
85
|
-
mockAll.mockResolvedValueOnce([]);
|
|
86
|
-
// 5. getPrimaryKey
|
|
87
|
-
mockAll.mockResolvedValueOnce([{ column_name: 'id' }]);
|
|
88
|
-
|
|
89
|
-
const schema = await introspector.getTableSchema('users');
|
|
90
|
-
|
|
91
|
-
expect(schema).toBeDefined();
|
|
92
|
-
expect(schema?.name).toBe('users');
|
|
93
|
-
expect(schema?.columns).toHaveLength(2);
|
|
94
|
-
expect(schema?.columns[0]).toMatchObject({
|
|
95
|
-
name: 'id',
|
|
96
|
-
type: 'INT',
|
|
97
|
-
nullable: false,
|
|
98
|
-
isAutoIncrement: true,
|
|
99
|
-
isPrimaryKey: true,
|
|
100
|
-
});
|
|
101
|
-
expect(schema?.indexes).toHaveLength(1);
|
|
102
|
-
expect(schema?.indexes?.[0].name).toBe('email_unique');
|
|
103
|
-
expect(schema?.indexes?.[0].unique).toBe(true);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('getTableSchema should return undefined for non-existent table', async () => {
|
|
107
|
-
mockAll.mockResolvedValueOnce([{ count: 0 }]);
|
|
108
|
-
|
|
109
|
-
const schema = await introspector.getTableSchema('non_existent');
|
|
110
|
-
|
|
111
|
-
expect(schema).toBeUndefined();
|
|
112
|
-
});
|
|
113
|
-
});
|
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ColumnSchema,
|
|
3
|
-
ForeignKeySchema,
|
|
4
|
-
IndexSchema,
|
|
5
|
-
QuerierPool,
|
|
6
|
-
SchemaIntrospector,
|
|
7
|
-
SqlQuerier,
|
|
8
|
-
TableSchema,
|
|
9
|
-
} from '../../type/index.js';
|
|
10
|
-
import { isSqlQuerier } from '../../type/index.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* MySQL/MariaDB schema introspector.
|
|
14
|
-
* Works with both MySQL and MariaDB as they share the same information_schema structure.
|
|
15
|
-
*/
|
|
16
|
-
export class MysqlSchemaIntrospector implements SchemaIntrospector {
|
|
17
|
-
constructor(private readonly querierPool: QuerierPool) {}
|
|
18
|
-
|
|
19
|
-
async getTableSchema(tableName: string): Promise<TableSchema | undefined> {
|
|
20
|
-
const querier = await this.getQuerier();
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
const exists = await this.tableExistsInternal(querier, tableName);
|
|
24
|
-
if (!exists) {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const [columns, indexes, foreignKeys, primaryKey] = await Promise.all([
|
|
29
|
-
this.getColumns(querier, tableName),
|
|
30
|
-
this.getIndexes(querier, tableName),
|
|
31
|
-
this.getForeignKeys(querier, tableName),
|
|
32
|
-
this.getPrimaryKey(querier, tableName),
|
|
33
|
-
]);
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
name: tableName,
|
|
37
|
-
columns,
|
|
38
|
-
primaryKey,
|
|
39
|
-
indexes,
|
|
40
|
-
foreignKeys,
|
|
41
|
-
};
|
|
42
|
-
} finally {
|
|
43
|
-
await querier.release();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async getTableNames(): Promise<string[]> {
|
|
48
|
-
const querier = await this.getQuerier();
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
const sql = `
|
|
52
|
-
SELECT TABLE_NAME as table_name
|
|
53
|
-
FROM information_schema.TABLES
|
|
54
|
-
WHERE TABLE_SCHEMA = DATABASE()
|
|
55
|
-
AND TABLE_TYPE = 'BASE TABLE'
|
|
56
|
-
ORDER BY TABLE_NAME
|
|
57
|
-
`;
|
|
58
|
-
|
|
59
|
-
const results = await querier.all<{ table_name: string }>(sql);
|
|
60
|
-
return results.map((r: any) => r.table_name);
|
|
61
|
-
} finally {
|
|
62
|
-
await querier.release();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async tableExists(tableName: string): Promise<boolean> {
|
|
67
|
-
const querier = await this.getQuerier();
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
return this.tableExistsInternal(querier, tableName);
|
|
71
|
-
} finally {
|
|
72
|
-
await querier.release();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
private async tableExistsInternal(querier: SqlQuerier, tableName: string): Promise<boolean> {
|
|
77
|
-
const sql = `
|
|
78
|
-
SELECT COUNT(*) as count
|
|
79
|
-
FROM information_schema.TABLES
|
|
80
|
-
WHERE TABLE_SCHEMA = DATABASE()
|
|
81
|
-
AND TABLE_NAME = ?
|
|
82
|
-
`;
|
|
83
|
-
|
|
84
|
-
const results = await querier.all<{ count: number }>(sql, [tableName]);
|
|
85
|
-
return (results[0]?.count ?? 0) > 0;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
private async getQuerier(): Promise<SqlQuerier> {
|
|
89
|
-
const querier = await this.querierPool.getQuerier();
|
|
90
|
-
|
|
91
|
-
if (!isSqlQuerier(querier)) {
|
|
92
|
-
await querier.release();
|
|
93
|
-
throw new Error('MysqlSchemaIntrospector requires a SQL-based querier');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return querier;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private async getColumns(querier: SqlQuerier, tableName: string): Promise<ColumnSchema[]> {
|
|
100
|
-
const sql = `
|
|
101
|
-
SELECT
|
|
102
|
-
COLUMN_NAME as column_name,
|
|
103
|
-
DATA_TYPE as data_type,
|
|
104
|
-
COLUMN_TYPE as column_type,
|
|
105
|
-
IS_NULLABLE as is_nullable,
|
|
106
|
-
COLUMN_DEFAULT as column_default,
|
|
107
|
-
CHARACTER_MAXIMUM_LENGTH as character_maximum_length,
|
|
108
|
-
NUMERIC_PRECISION as numeric_precision,
|
|
109
|
-
NUMERIC_SCALE as numeric_scale,
|
|
110
|
-
COLUMN_KEY as column_key,
|
|
111
|
-
EXTRA as extra,
|
|
112
|
-
COLUMN_COMMENT as column_comment
|
|
113
|
-
FROM information_schema.COLUMNS
|
|
114
|
-
WHERE TABLE_SCHEMA = DATABASE()
|
|
115
|
-
AND TABLE_NAME = ?
|
|
116
|
-
ORDER BY ORDINAL_POSITION
|
|
117
|
-
`;
|
|
118
|
-
|
|
119
|
-
const results = await querier.all<{
|
|
120
|
-
column_name: string;
|
|
121
|
-
data_type: string;
|
|
122
|
-
column_type: string;
|
|
123
|
-
is_nullable: string;
|
|
124
|
-
column_default: string | null;
|
|
125
|
-
character_maximum_length: number | null;
|
|
126
|
-
numeric_precision: number | null;
|
|
127
|
-
numeric_scale: number | null;
|
|
128
|
-
column_key: string;
|
|
129
|
-
extra: string;
|
|
130
|
-
column_comment: string | null;
|
|
131
|
-
}>(sql, [tableName]);
|
|
132
|
-
|
|
133
|
-
return results.map((row: any) => ({
|
|
134
|
-
name: row.column_name,
|
|
135
|
-
type: row.data_type.toUpperCase(),
|
|
136
|
-
nullable: row.is_nullable === 'YES',
|
|
137
|
-
defaultValue: this.parseDefaultValue(row.column_default),
|
|
138
|
-
isPrimaryKey: row.column_key === 'PRI',
|
|
139
|
-
isAutoIncrement: row.extra.toLowerCase().includes('auto_increment'),
|
|
140
|
-
isUnique: row.column_key === 'UNI',
|
|
141
|
-
length: row.character_maximum_length ?? undefined,
|
|
142
|
-
precision: row.numeric_precision ?? undefined,
|
|
143
|
-
scale: row.numeric_scale ?? undefined,
|
|
144
|
-
comment: row.column_comment || undefined,
|
|
145
|
-
}));
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
private async getIndexes(querier: SqlQuerier, tableName: string): Promise<IndexSchema[]> {
|
|
149
|
-
const sql = `
|
|
150
|
-
SELECT
|
|
151
|
-
INDEX_NAME as index_name,
|
|
152
|
-
GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) as columns,
|
|
153
|
-
NOT NON_UNIQUE as is_unique
|
|
154
|
-
FROM information_schema.STATISTICS
|
|
155
|
-
WHERE TABLE_SCHEMA = DATABASE()
|
|
156
|
-
AND TABLE_NAME = ?
|
|
157
|
-
AND INDEX_NAME != 'PRIMARY'
|
|
158
|
-
GROUP BY INDEX_NAME, NON_UNIQUE
|
|
159
|
-
ORDER BY INDEX_NAME
|
|
160
|
-
`;
|
|
161
|
-
|
|
162
|
-
const results = await querier.all<{
|
|
163
|
-
index_name: string;
|
|
164
|
-
columns: string;
|
|
165
|
-
is_unique: number;
|
|
166
|
-
}>(sql, [tableName]);
|
|
167
|
-
|
|
168
|
-
return results.map((row: any) => ({
|
|
169
|
-
name: row.index_name,
|
|
170
|
-
columns: row.columns.split(','),
|
|
171
|
-
unique: Boolean(row.is_unique),
|
|
172
|
-
}));
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
private async getForeignKeys(querier: SqlQuerier, tableName: string): Promise<ForeignKeySchema[]> {
|
|
176
|
-
const sql = `
|
|
177
|
-
SELECT
|
|
178
|
-
kcu.CONSTRAINT_NAME as constraint_name,
|
|
179
|
-
GROUP_CONCAT(kcu.COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) as columns,
|
|
180
|
-
kcu.REFERENCED_TABLE_NAME as referenced_table,
|
|
181
|
-
GROUP_CONCAT(kcu.REFERENCED_COLUMN_NAME ORDER BY kcu.ORDINAL_POSITION) as referenced_columns,
|
|
182
|
-
rc.DELETE_RULE as delete_rule,
|
|
183
|
-
rc.UPDATE_RULE as update_rule
|
|
184
|
-
FROM information_schema.KEY_COLUMN_USAGE kcu
|
|
185
|
-
JOIN information_schema.REFERENTIAL_CONSTRAINTS rc
|
|
186
|
-
ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
|
|
187
|
-
AND kcu.TABLE_SCHEMA = rc.CONSTRAINT_SCHEMA
|
|
188
|
-
WHERE kcu.TABLE_SCHEMA = DATABASE()
|
|
189
|
-
AND kcu.TABLE_NAME = ?
|
|
190
|
-
AND kcu.REFERENCED_TABLE_NAME IS NOT NULL
|
|
191
|
-
GROUP BY kcu.CONSTRAINT_NAME, kcu.REFERENCED_TABLE_NAME, rc.DELETE_RULE, rc.UPDATE_RULE
|
|
192
|
-
ORDER BY kcu.CONSTRAINT_NAME
|
|
193
|
-
`;
|
|
194
|
-
|
|
195
|
-
const results = await querier.all<{
|
|
196
|
-
constraint_name: string;
|
|
197
|
-
columns: string;
|
|
198
|
-
referenced_table: string;
|
|
199
|
-
referenced_columns: string;
|
|
200
|
-
delete_rule: string;
|
|
201
|
-
update_rule: string;
|
|
202
|
-
}>(sql, [tableName]);
|
|
203
|
-
|
|
204
|
-
return results.map((row: any) => ({
|
|
205
|
-
name: row.constraint_name,
|
|
206
|
-
columns: row.columns.split(','),
|
|
207
|
-
referencedTable: row.referenced_table,
|
|
208
|
-
referencedColumns: row.referenced_columns.split(','),
|
|
209
|
-
onDelete: this.normalizeReferentialAction(row.delete_rule),
|
|
210
|
-
onUpdate: this.normalizeReferentialAction(row.update_rule),
|
|
211
|
-
}));
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
private async getPrimaryKey(querier: SqlQuerier, tableName: string): Promise<string[] | undefined> {
|
|
215
|
-
const sql = `
|
|
216
|
-
SELECT COLUMN_NAME as column_name
|
|
217
|
-
FROM information_schema.KEY_COLUMN_USAGE
|
|
218
|
-
WHERE TABLE_SCHEMA = DATABASE()
|
|
219
|
-
AND TABLE_NAME = ?
|
|
220
|
-
AND CONSTRAINT_NAME = 'PRIMARY'
|
|
221
|
-
ORDER BY ORDINAL_POSITION
|
|
222
|
-
`;
|
|
223
|
-
|
|
224
|
-
const results = await querier.all<{ column_name: string }>(sql, [tableName]);
|
|
225
|
-
|
|
226
|
-
if (results.length === 0) {
|
|
227
|
-
return undefined;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return results.map((r: any) => r.column_name);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
private parseDefaultValue(defaultValue: string | null): unknown {
|
|
234
|
-
if (defaultValue === null) {
|
|
235
|
-
return undefined;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Check for common patterns
|
|
239
|
-
if (defaultValue === 'NULL') {
|
|
240
|
-
return null;
|
|
241
|
-
}
|
|
242
|
-
if (defaultValue === 'CURRENT_TIMESTAMP') {
|
|
243
|
-
return defaultValue;
|
|
244
|
-
}
|
|
245
|
-
if (/^'.*'$/.test(defaultValue)) {
|
|
246
|
-
return defaultValue.slice(1, -1);
|
|
247
|
-
}
|
|
248
|
-
if (/^-?\d+$/.test(defaultValue)) {
|
|
249
|
-
return Number.parseInt(defaultValue, 10);
|
|
250
|
-
}
|
|
251
|
-
if (/^-?\d+\.\d+$/.test(defaultValue)) {
|
|
252
|
-
return Number.parseFloat(defaultValue);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return defaultValue;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
private normalizeReferentialAction(action: string): 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION' | undefined {
|
|
259
|
-
switch (action.toUpperCase()) {
|
|
260
|
-
case 'CASCADE':
|
|
261
|
-
return 'CASCADE';
|
|
262
|
-
case 'SET NULL':
|
|
263
|
-
return 'SET NULL';
|
|
264
|
-
case 'RESTRICT':
|
|
265
|
-
return 'RESTRICT';
|
|
266
|
-
case 'NO ACTION':
|
|
267
|
-
return 'NO ACTION';
|
|
268
|
-
default:
|
|
269
|
-
return undefined;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Alias for MysqlSchemaIntrospector.
|
|
276
|
-
* MariaDB uses the same information_schema structure as MySQL.
|
|
277
|
-
*/
|
|
278
|
-
export const MariadbSchemaIntrospector = MysqlSchemaIntrospector;
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, jest } from 'bun:test';
|
|
2
|
-
import type { QuerierPool, SqlQuerier } from '../../type/index.js';
|
|
3
|
-
import { PostgresSchemaIntrospector } from './postgresIntrospector.js';
|
|
4
|
-
|
|
5
|
-
describe('PostgresSchemaIntrospector', () => {
|
|
6
|
-
let introspector: PostgresSchemaIntrospector;
|
|
7
|
-
let pool: QuerierPool;
|
|
8
|
-
let querier: SqlQuerier;
|
|
9
|
-
|
|
10
|
-
let mockAll: ReturnType<typeof jest.fn>;
|
|
11
|
-
let mockRun: ReturnType<typeof jest.fn>;
|
|
12
|
-
let mockRelease: ReturnType<typeof jest.fn>;
|
|
13
|
-
let mockGetQuerier: ReturnType<typeof jest.fn>;
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
mockAll = jest.fn<any>().mockResolvedValue([]);
|
|
17
|
-
mockRun = jest.fn<any>().mockResolvedValue({});
|
|
18
|
-
mockRelease = jest.fn<any>().mockResolvedValue(undefined);
|
|
19
|
-
|
|
20
|
-
querier = {
|
|
21
|
-
all: mockAll,
|
|
22
|
-
run: mockRun,
|
|
23
|
-
release: mockRelease,
|
|
24
|
-
dialect: { escapeIdChar: '"' },
|
|
25
|
-
} as any;
|
|
26
|
-
|
|
27
|
-
mockGetQuerier = jest.fn<any>().mockResolvedValue(querier);
|
|
28
|
-
pool = {
|
|
29
|
-
getQuerier: mockGetQuerier,
|
|
30
|
-
} as any;
|
|
31
|
-
|
|
32
|
-
introspector = new PostgresSchemaIntrospector(pool);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('getTableNames should return a list of tables', async () => {
|
|
36
|
-
mockAll.mockResolvedValueOnce([{ table_name: 'users' }, { table_name: 'posts' }]);
|
|
37
|
-
|
|
38
|
-
const names = await introspector.getTableNames();
|
|
39
|
-
|
|
40
|
-
expect(names).toEqual(['users', 'posts']);
|
|
41
|
-
expect(querier.all).toHaveBeenCalledWith(expect.stringContaining('information_schema.tables'));
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('getTableSchema should return table details', async () => {
|
|
45
|
-
// 1. tableExists check
|
|
46
|
-
mockAll.mockResolvedValueOnce([{ exists: true }]);
|
|
47
|
-
// 2. getColumns
|
|
48
|
-
mockAll.mockResolvedValueOnce([
|
|
49
|
-
{
|
|
50
|
-
column_name: 'id',
|
|
51
|
-
data_type: 'integer',
|
|
52
|
-
udt_name: 'int4',
|
|
53
|
-
is_nullable: 'NO',
|
|
54
|
-
column_default: "nextval('users_id_seq'::regclass)",
|
|
55
|
-
character_maximum_length: null,
|
|
56
|
-
numeric_precision: 32,
|
|
57
|
-
numeric_scale: 0,
|
|
58
|
-
is_primary_key: true,
|
|
59
|
-
is_unique: false,
|
|
60
|
-
column_comment: 'User ID',
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
column_name: 'name',
|
|
64
|
-
data_type: 'character varying',
|
|
65
|
-
udt_name: 'varchar',
|
|
66
|
-
is_nullable: 'YES',
|
|
67
|
-
column_default: null,
|
|
68
|
-
character_maximum_length: 255,
|
|
69
|
-
numeric_precision: null,
|
|
70
|
-
numeric_scale: null,
|
|
71
|
-
is_primary_key: false,
|
|
72
|
-
is_unique: false,
|
|
73
|
-
column_comment: null,
|
|
74
|
-
},
|
|
75
|
-
]);
|
|
76
|
-
// 3. getIndexes
|
|
77
|
-
mockAll.mockResolvedValueOnce([
|
|
78
|
-
{
|
|
79
|
-
index_name: 'idx_users_name',
|
|
80
|
-
columns: ['name'],
|
|
81
|
-
is_unique: false,
|
|
82
|
-
},
|
|
83
|
-
]);
|
|
84
|
-
// 4. getForeignKeys
|
|
85
|
-
mockAll.mockResolvedValueOnce([]);
|
|
86
|
-
// 5. getPrimaryKey
|
|
87
|
-
mockAll.mockResolvedValueOnce([{ column_name: 'id' }]);
|
|
88
|
-
|
|
89
|
-
const schema = await introspector.getTableSchema('users');
|
|
90
|
-
|
|
91
|
-
expect(schema).toBeDefined();
|
|
92
|
-
expect(schema?.name).toBe('users');
|
|
93
|
-
expect(schema?.columns).toHaveLength(2);
|
|
94
|
-
expect(schema?.columns[0]).toMatchObject({
|
|
95
|
-
name: 'id',
|
|
96
|
-
type: 'INTEGER',
|
|
97
|
-
nullable: false,
|
|
98
|
-
isAutoIncrement: true,
|
|
99
|
-
isPrimaryKey: true,
|
|
100
|
-
});
|
|
101
|
-
expect(schema?.indexes).toHaveLength(1);
|
|
102
|
-
expect(schema?.indexes?.[0].name).toBe('idx_users_name');
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('getTableSchema should return undefined for non-existent table', async () => {
|
|
106
|
-
mockAll.mockResolvedValueOnce([{ exists: false }]);
|
|
107
|
-
|
|
108
|
-
const schema = await introspector.getTableSchema('non_existent');
|
|
109
|
-
|
|
110
|
-
expect(schema).toBeUndefined();
|
|
111
|
-
});
|
|
112
|
-
});
|