@proteinjs/db-driver-knex 1.0.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 +16 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/generated/index.d.ts +11 -0
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/index.js +39 -0
- package/dist/generated/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/src/KnexColumnTypeFactory.d.ts +5 -0
- package/dist/src/KnexColumnTypeFactory.d.ts.map +1 -0
- package/dist/src/KnexColumnTypeFactory.js +33 -0
- package/dist/src/KnexColumnTypeFactory.js.map +1 -0
- package/dist/src/KnexConfig.d.ts +19 -0
- package/dist/src/KnexConfig.d.ts.map +1 -0
- package/dist/src/KnexConfig.js +15 -0
- package/dist/src/KnexConfig.js.map +1 -0
- package/dist/src/KnexDriver.d.ts +22 -0
- package/dist/src/KnexDriver.d.ts.map +1 -0
- package/dist/src/KnexDriver.js +187 -0
- package/dist/src/KnexDriver.js.map +1 -0
- package/dist/src/KnexSchemaOperations.d.ts +11 -0
- package/dist/src/KnexSchemaOperations.d.ts.map +1 -0
- package/dist/src/KnexSchemaOperations.js +174 -0
- package/dist/src/KnexSchemaOperations.js.map +1 -0
- package/dist/src/getColumnFactory.d.ts +34 -0
- package/dist/src/getColumnFactory.d.ts.map +1 -0
- package/dist/src/getColumnFactory.js +111 -0
- package/dist/src/getColumnFactory.js.map +1 -0
- package/dist/test/Crud.test.d.ts +2 -0
- package/dist/test/Crud.test.d.ts.map +1 -0
- package/dist/test/Crud.test.js +62 -0
- package/dist/test/Crud.test.js.map +1 -0
- package/dist/test/TableManager.test.d.ts +2 -0
- package/dist/test/TableManager.test.d.ts.map +1 -0
- package/dist/test/TableManager.test.js +63 -0
- package/dist/test/TableManager.test.js.map +1 -0
- package/generated/index.ts +35 -0
- package/index.ts +2 -0
- package/jest.config.js +18 -0
- package/package.json +47 -0
- package/src/KnexColumnTypeFactory.ts +27 -0
- package/src/KnexConfig.ts +18 -0
- package/src/KnexDriver.ts +90 -0
- package/src/KnexSchemaOperations.ts +130 -0
- package/src/getColumnFactory.ts +86 -0
- package/test/Crud.test.ts +21 -0
- package/test/TableManager.test.ts +23 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BinaryColumn, BooleanColumn, Column, DateColumn, DateTimeColumn, DecimalColumn, FloatColumn, IntegerColumn, StringColumn, UuidColumn } from '@proteinjs/db';
|
|
2
|
+
|
|
3
|
+
// note: this might be specific to maria db
|
|
4
|
+
export class KnexColumnTypeFactory {
|
|
5
|
+
getType(column: Column<any, any>): string {
|
|
6
|
+
if (column instanceof IntegerColumn)
|
|
7
|
+
return column.large ? 'bigint' : 'int';
|
|
8
|
+
else if (column instanceof UuidColumn)
|
|
9
|
+
return 'char';
|
|
10
|
+
else if (column instanceof StringColumn)
|
|
11
|
+
return column.maxLength === 'MAX' ? 'longtext' : 'varchar';
|
|
12
|
+
else if (column instanceof FloatColumn)
|
|
13
|
+
return 'float';
|
|
14
|
+
else if (column instanceof DecimalColumn)
|
|
15
|
+
return 'decimal';
|
|
16
|
+
else if (column instanceof BooleanColumn)
|
|
17
|
+
return 'tinyint';
|
|
18
|
+
else if (column instanceof DateColumn)
|
|
19
|
+
return 'date';
|
|
20
|
+
else if (column instanceof DateTimeColumn)
|
|
21
|
+
return 'datetime';
|
|
22
|
+
else if (column instanceof BinaryColumn)
|
|
23
|
+
return (column as BinaryColumn).maxLength === 'MAX' ? 'longblob' : 'blob';
|
|
24
|
+
|
|
25
|
+
throw new Error(`Invalid column type: ${column.constructor.name}, must extend a base column`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Loadable, SourceRepository } from '@proteinjs/reflection';
|
|
2
|
+
|
|
3
|
+
export const getKnexConfig = () => {
|
|
4
|
+
const config = SourceRepository.get().object<KnexConfig>('@proteinjs/db-driver-knex/KnexConfig');
|
|
5
|
+
return Object.assign({
|
|
6
|
+
host: process.env.DB_HOST ? process.env.DB_HOST : 'localhost',
|
|
7
|
+
user: process.env.DB_USER ? process.env.DB_USER : 'root',
|
|
8
|
+
password: process.env.DB_PASSWORD ? process.env.DB_PASSWORD : '',
|
|
9
|
+
dbName: process.env.DB_NAME ? process.env.DB_NAME : 'dev',
|
|
10
|
+
}, config);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type KnexConfig = Loadable & {
|
|
14
|
+
host?: string,
|
|
15
|
+
user?: string,
|
|
16
|
+
password?: string,
|
|
17
|
+
dbName?: string,
|
|
18
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import knex from 'knex';
|
|
2
|
+
import { DbDriver, DbDriverStatementConfig, SerializedRecord, TableManager } from '@proteinjs/db';
|
|
3
|
+
import { KnexConfig, getKnexConfig } from './KnexConfig';
|
|
4
|
+
import { Logger } from '@proteinjs/util';
|
|
5
|
+
import { Statement } from '@proteinjs/db-query';
|
|
6
|
+
import { KnexSchemaOperations } from './KnexSchemaOperations';
|
|
7
|
+
import { KnexColumnTypeFactory } from './KnexColumnTypeFactory';
|
|
8
|
+
|
|
9
|
+
export class KnexDriver implements DbDriver {
|
|
10
|
+
private static KNEX: knex;
|
|
11
|
+
private logger = new Logger(this.constructor.name);
|
|
12
|
+
private config: KnexConfig;
|
|
13
|
+
private knexConfig: any;
|
|
14
|
+
|
|
15
|
+
constructor(config?: KnexConfig) {
|
|
16
|
+
this.config = config ? config : getKnexConfig();
|
|
17
|
+
this.knexConfig = {
|
|
18
|
+
client: 'mysql',
|
|
19
|
+
connection: {
|
|
20
|
+
host: this.config.host,
|
|
21
|
+
user: this.config.user,
|
|
22
|
+
password: this.config.password,
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getKnex(): knex {
|
|
28
|
+
if (!KnexDriver.KNEX)
|
|
29
|
+
KnexDriver.KNEX = knex(this.knexConfig);
|
|
30
|
+
|
|
31
|
+
return KnexDriver.KNEX;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getDbName() {
|
|
35
|
+
return this.config.dbName as string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async createDbIfNotExists(): Promise<void> {
|
|
39
|
+
if (await this.dbExists(this.getDbName()))
|
|
40
|
+
return;
|
|
41
|
+
|
|
42
|
+
await this.getKnex().raw(`CREATE DATABASE ${this.getDbName()};`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private async dbExists(databaseName: string): Promise<boolean> {
|
|
46
|
+
const result: any = await this.getKnex().raw('SHOW DATABASES;');
|
|
47
|
+
for (const existingDatabase of result[0]) {
|
|
48
|
+
if (existingDatabase['Database'] == databaseName)
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async start() {
|
|
56
|
+
await this.setMaxAllowedPacketSize();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async stop() {
|
|
60
|
+
await this.getKnex().destroy();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async setMaxAllowedPacketSize(): Promise<void> {
|
|
64
|
+
await this.getKnex().raw('SET GLOBAL max_allowed_packet=1073741824;');
|
|
65
|
+
await this.getKnex().destroy();
|
|
66
|
+
KnexDriver.KNEX = knex(this.knexConfig);
|
|
67
|
+
this.logger.info('Set global max_allowed_packet size to 1gb');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getTableManager(): TableManager {
|
|
71
|
+
const columnTypeFactory = new KnexColumnTypeFactory();
|
|
72
|
+
const schemaOperations = new KnexSchemaOperations(this);
|
|
73
|
+
return new TableManager(this, columnTypeFactory, schemaOperations);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async runQuery(generateStatement: (config: DbDriverStatementConfig) => Statement): Promise<SerializedRecord[]> {
|
|
77
|
+
const { sql, params } = generateStatement({ useParams: true, prefixTablesWithDb: true });
|
|
78
|
+
try {
|
|
79
|
+
return (await this.getKnex().raw(sql, params as any))[0]; // returns 2 arrays, first is records, second is metadata per record
|
|
80
|
+
} catch(error: any) {
|
|
81
|
+
this.logger.error(`Failed when executing query: ${sql}`);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async runDml(generateStatement: (config: DbDriverStatementConfig) => Statement): Promise<number> {
|
|
87
|
+
const { affectedRows } = (await this.runQuery(generateStatement) as any);
|
|
88
|
+
return affectedRows;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import * as knex from 'knex';
|
|
2
|
+
import { Logger } from '@proteinjs/util';
|
|
3
|
+
import { Column, Table, SchemaOperations, TableChanges } from '@proteinjs/db';
|
|
4
|
+
import { KnexDriver } from './KnexDriver';
|
|
5
|
+
import { getColumnFactory } from './getColumnFactory';
|
|
6
|
+
|
|
7
|
+
export class KnexSchemaOperations implements SchemaOperations {
|
|
8
|
+
private logger = new Logger(this.constructor.name);
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
private knexDriver: KnexDriver
|
|
12
|
+
){}
|
|
13
|
+
|
|
14
|
+
async createTable(table: Table<any>) {
|
|
15
|
+
let resolve: any;
|
|
16
|
+
let reject: any;
|
|
17
|
+
const p = new Promise<void>((rs, rj) => {
|
|
18
|
+
resolve = rs;
|
|
19
|
+
reject = rj;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
await this.knexDriver.getKnex().schema.withSchema(this.knexDriver.getDbName()).createTable(table.name, (tableBuilder: knex.TableBuilder) => {
|
|
23
|
+
for (const columnPropertyName in table.columns) {
|
|
24
|
+
const column = table.columns[columnPropertyName];
|
|
25
|
+
this.createColumn(column, table, tableBuilder);
|
|
26
|
+
this.logger.info(`[${table.name}] Creating column: ${column.name}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
tableBuilder.primary(['id']);
|
|
30
|
+
this.logger.info(`[${table.name}] Creating primary key: id`);
|
|
31
|
+
|
|
32
|
+
if (table.indexes) {
|
|
33
|
+
for (const index of table.indexes) {
|
|
34
|
+
const columnNames = index.columns.map((columnPropertyName) => table.columns[columnPropertyName as string].name)
|
|
35
|
+
tableBuilder.index(columnNames, index.name);
|
|
36
|
+
this.logger.info(`[${table.name}] Creating index: ${columnNames}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}).catch((reason: any) => {
|
|
40
|
+
reject(`Failed to create table: ${table.name}. reason: ${reason}`);
|
|
41
|
+
}).then(() => {
|
|
42
|
+
resolve();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return p;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async alterTable(table: Table<any>, tableChanges: TableChanges) {
|
|
49
|
+
let resolve: any;
|
|
50
|
+
let reject: any;
|
|
51
|
+
const p = new Promise<void>((rs, rj) => {
|
|
52
|
+
resolve = rs;
|
|
53
|
+
reject = rj;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await this.knexDriver.getKnex().schema.withSchema(this.knexDriver.getDbName()).table(table.name, (tableBuilder: knex.TableBuilder) => {
|
|
57
|
+
for (const columnPropertyName of tableChanges.columnsToCreate) {
|
|
58
|
+
const column = table.columns[columnPropertyName];
|
|
59
|
+
this.logger.info(`[${table.name}] Creating column: ${column.name}`);
|
|
60
|
+
this.createColumn(column, table, tableBuilder, tableChanges);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const column of tableChanges.columnsWithUniqueConstraintsToDrop) {
|
|
64
|
+
tableBuilder.dropUnique([column]);
|
|
65
|
+
this.logger.info(`[${table.name}.${column}] Dropping unique constraint`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
for (const column of tableChanges.columnsWithForeignKeysToDrop) {
|
|
69
|
+
tableBuilder.dropForeign([column]);
|
|
70
|
+
this.logger.info(`[${table.name}.${column}] Dropping foreign key`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const index of tableChanges.indexesToDrop) {
|
|
74
|
+
tableBuilder.dropIndex(index.columns);
|
|
75
|
+
this.logger.info(`[${table.name}] Dropping index: ${JSON.stringify(index)}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for (const columnPropertyName of tableChanges.columnsToAlter) {
|
|
79
|
+
const column = table.columns[columnPropertyName];
|
|
80
|
+
this.logger.info(`[${table.name}.${column.name}] Altering column type to: ${column.constructor.name}`);
|
|
81
|
+
this.createColumn(column, table, tableBuilder, tableChanges).alter();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (const columnPropertyName of tableChanges.columnsToRename) {
|
|
85
|
+
const column = table.columns[columnPropertyName];
|
|
86
|
+
if (!column.oldName)
|
|
87
|
+
continue;
|
|
88
|
+
|
|
89
|
+
tableBuilder.renameColumn(column.oldName, column.name);
|
|
90
|
+
this.logger.info(`[${table.name}] Renaming column: ${column.oldName} -> ${column.name}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (const index of tableChanges.indexesToCreate) {
|
|
94
|
+
tableBuilder.index(index.columns);
|
|
95
|
+
this.logger.info(`[${table.name}] Creating index: ${JSON.stringify(index)}`);
|
|
96
|
+
}
|
|
97
|
+
}).catch((reason: any) => {
|
|
98
|
+
reject(`Failed to alter table: ${table.name}. reason: ${reason}`);
|
|
99
|
+
}).then(() => {
|
|
100
|
+
resolve();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return p;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private createColumn(column: Column<any, any>, table: Table<any>, tableBuilder: knex.TableBuilder, tableChanges?: TableChanges) {
|
|
107
|
+
const columnFactory = getColumnFactory(column);
|
|
108
|
+
const columnBuilder = columnFactory.create(column, tableBuilder);
|
|
109
|
+
if (column.options?.unique?.unique && (!tableChanges || tableChanges.columnsWithUniqueConstraintsToCreate.includes(column.name))) {
|
|
110
|
+
columnBuilder.unique(column.options.unique.indexName);
|
|
111
|
+
this.logger.info(`[${table.name}.${column.name}] Adding unique constraint`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (column.options?.references && (!tableChanges || tableChanges.columnsWithForeignKeysToCreate.includes(column.name))) {
|
|
115
|
+
columnBuilder.references('id').inTable(`${this.knexDriver.getDbName()}.${column.options.references.table}`);
|
|
116
|
+
this.logger.info(`[${table.name}.${column.name}] Adding foreign key -> ${column.options.references.table}.id`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (typeof column.options?.nullable !== 'undefined') {
|
|
120
|
+
if (column.options?.nullable)
|
|
121
|
+
columnBuilder.nullable();
|
|
122
|
+
else
|
|
123
|
+
columnBuilder.notNullable();
|
|
124
|
+
|
|
125
|
+
this.logger.info(`[${table.name}.${column.name}] Adding constraint nullable: ${column.options?.nullable}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return columnBuilder;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import knex from 'knex';
|
|
2
|
+
import { BinaryColumn, BooleanColumn, Column, DateColumn, DateTimeColumn, DecimalColumn, FloatColumn, IntegerColumn, StringColumn, UuidColumn } from '@proteinjs/db';
|
|
3
|
+
|
|
4
|
+
export const getColumnFactory = (column: Column<any, any>): ColumnFactory => {
|
|
5
|
+
if (column instanceof IntegerColumn)
|
|
6
|
+
return new IntegerColumnFactory();
|
|
7
|
+
else if (column instanceof UuidColumn)
|
|
8
|
+
return new UuidColumnFactory();
|
|
9
|
+
else if (column instanceof StringColumn)
|
|
10
|
+
return new StringColumnFactory();
|
|
11
|
+
else if (column instanceof FloatColumn)
|
|
12
|
+
return new FloatColumnFactory();
|
|
13
|
+
else if (column instanceof DecimalColumn)
|
|
14
|
+
return new DecimalColumnFactory();
|
|
15
|
+
else if (column instanceof BooleanColumn)
|
|
16
|
+
return new BooleanColumnFactory();
|
|
17
|
+
else if (column instanceof DateColumn)
|
|
18
|
+
return new DateColumnFactory();
|
|
19
|
+
else if (column instanceof DateTimeColumn)
|
|
20
|
+
return new DateTimeColumnFactory();
|
|
21
|
+
else if (column instanceof BinaryColumn)
|
|
22
|
+
return new BinaryColumnFactory();
|
|
23
|
+
|
|
24
|
+
throw new Error(`Invalid column type: ${column.constructor.name}, must extend a base column`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ColumnFactory {
|
|
28
|
+
create(column: Column<any, any>, tableBuilder: knex.TableBuilder): knex.ColumnBuilder;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class IntegerColumnFactory implements ColumnFactory {
|
|
32
|
+
create(integerColumn: IntegerColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
33
|
+
return integerColumn.large ? tableBuilder.bigInteger(integerColumn.name) : tableBuilder.integer(integerColumn.name);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// max length of longtext is 4,294,967,295 bytes (~4 GiB)
|
|
38
|
+
export class StringColumnFactory implements ColumnFactory {
|
|
39
|
+
create(stringColumn: StringColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
40
|
+
return stringColumn.maxLength === 'MAX' ? tableBuilder.text(stringColumn.name, 'longtext') : tableBuilder.string(stringColumn.name, stringColumn.maxLength);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class FloatColumnFactory implements ColumnFactory {
|
|
45
|
+
create(floatColumn: FloatColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
46
|
+
return tableBuilder.float(floatColumn.name);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class DecimalColumnFactory implements ColumnFactory {
|
|
51
|
+
create(decimalColumn: DecimalColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
52
|
+
return decimalColumn.large ? tableBuilder.decimal(decimalColumn.name, 38, 20) : tableBuilder.decimal(decimalColumn.name);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class BooleanColumnFactory implements ColumnFactory {
|
|
57
|
+
create(booleanColumn: BooleanColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
58
|
+
return tableBuilder.boolean(booleanColumn.name);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class DateColumnFactory implements ColumnFactory {
|
|
63
|
+
create(dateColumn: DateColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
64
|
+
return tableBuilder.date(dateColumn.name);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class DateTimeColumnFactory implements ColumnFactory {
|
|
69
|
+
create(dateTimeColumn: DateTimeColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
70
|
+
return tableBuilder.dateTime(dateTimeColumn.name);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// max length of longblob is 4,294,967,295 bytes (~4 GiB)
|
|
75
|
+
// max length when undefined (aka blob) is 65,535 bytes (~64 KiB)
|
|
76
|
+
export class BinaryColumnFactory implements ColumnFactory {
|
|
77
|
+
create(binaryColumn: BinaryColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
78
|
+
return binaryColumn.maxLength == 'MAX' ? tableBuilder.specificType(binaryColumn.name, 'longblob') : tableBuilder.binary(binaryColumn.name, binaryColumn.maxLength);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class UuidColumnFactory implements ColumnFactory {
|
|
83
|
+
create(uuidColumn: UuidColumn, tableBuilder: knex.TableBuilder): knex.ColumnBuilder {
|
|
84
|
+
return tableBuilder.uuid(uuidColumn.name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Table, crudTests } from '@proteinjs/db'
|
|
2
|
+
import { KnexDriver } from '../src/KnexDriver'
|
|
3
|
+
|
|
4
|
+
const knexDriver = new KnexDriver({
|
|
5
|
+
host: 'localhost',
|
|
6
|
+
user: 'root',
|
|
7
|
+
password: '',
|
|
8
|
+
dbName: 'test',
|
|
9
|
+
});
|
|
10
|
+
const dropTable = async (table: Table<any>) => {
|
|
11
|
+
if (await knexDriver.getKnex().schema.withSchema(knexDriver.getDbName()).hasTable(table.name))
|
|
12
|
+
await knexDriver.getKnex().schema.withSchema(knexDriver.getDbName()).dropTable(table.name);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe(
|
|
16
|
+
'CRUD Tests',
|
|
17
|
+
crudTests(
|
|
18
|
+
knexDriver,
|
|
19
|
+
dropTable
|
|
20
|
+
)
|
|
21
|
+
);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Table, tableManagerTests } from '@proteinjs/db'
|
|
2
|
+
import { KnexDriver } from '../src/KnexDriver'
|
|
3
|
+
import { KnexColumnTypeFactory } from '../src/KnexColumnTypeFactory';
|
|
4
|
+
|
|
5
|
+
const knexDriver = new KnexDriver({
|
|
6
|
+
host: 'localhost',
|
|
7
|
+
user: 'root',
|
|
8
|
+
password: '',
|
|
9
|
+
dbName: 'test',
|
|
10
|
+
});
|
|
11
|
+
const dropTable = async (table: Table<any>) => {
|
|
12
|
+
if (await knexDriver.getKnex().schema.withSchema(knexDriver.getDbName()).hasTable(table.name))
|
|
13
|
+
await knexDriver.getKnex().schema.withSchema(knexDriver.getDbName()).dropTable(table.name);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe(
|
|
17
|
+
'Table Manager Tests',
|
|
18
|
+
tableManagerTests(
|
|
19
|
+
knexDriver,
|
|
20
|
+
dropTable,
|
|
21
|
+
new KnexColumnTypeFactory().getType
|
|
22
|
+
)
|
|
23
|
+
);
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "./",
|
|
4
|
+
"target": "es5",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"outDir": "./dist/",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"noImplicitAny": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"typeRoots": [
|
|
17
|
+
"./node_modules/@types"
|
|
18
|
+
],
|
|
19
|
+
"types": [
|
|
20
|
+
"node", "jest"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
}
|