@proteinjs/db 1.0.26 → 1.0.28
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 +23 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/src/Columns.d.ts +12 -12
- package/dist/src/Columns.d.ts.map +1 -1
- package/dist/src/Columns.js +28 -17
- package/dist/src/Columns.js.map +1 -1
- package/dist/src/Record.d.ts.map +1 -1
- package/dist/src/Record.js +3 -0
- package/dist/src/Record.js.map +1 -1
- package/dist/src/Table.d.ts +2 -2
- package/dist/src/Table.d.ts.map +1 -1
- package/dist/src/schema/SchemaMetadata.js +5 -22
- package/dist/src/schema/SchemaMetadata.js.map +1 -1
- package/dist/test/ColumnTypesTests.d.ts +53 -0
- package/dist/test/ColumnTypesTests.d.ts.map +1 -0
- package/dist/test/ColumnTypesTests.js +209 -0
- package/dist/test/ColumnTypesTests.js.map +1 -0
- package/dist/test/CrudTests.d.ts.map +1 -1
- package/dist/test/CrudTests.js +57 -10
- package/dist/test/CrudTests.js.map +1 -1
- package/index.ts +1 -0
- package/package.json +3 -3
- package/src/Columns.ts +44 -31
- package/src/Record.ts +3 -0
- package/src/Table.ts +2 -2
- package/src/schema/SchemaMetadata.ts +5 -5
- package/test/ColumnTypesTests.ts +153 -0
- package/test/CrudTests.ts +43 -10
|
@@ -20,9 +20,9 @@ export class SchemaMetadata {
|
|
|
20
20
|
async columnExists(columnName: string, table: Table<any>): Promise<boolean> {
|
|
21
21
|
const qb = QueryBuilder.fromObject(
|
|
22
22
|
{
|
|
23
|
-
TABLE_SCHEMA: this.multiDbSupported ? this.dbDriver.getDbName() : undefined,
|
|
24
23
|
TABLE_NAME: table.name,
|
|
25
24
|
COLUMN_NAME: columnName,
|
|
25
|
+
...(this.multiDbSupported ? { TABLE_SCHEMA: this.dbDriver.getDbName() } : {}),
|
|
26
26
|
},
|
|
27
27
|
'COLUMNS'
|
|
28
28
|
);
|
|
@@ -34,8 +34,8 @@ export class SchemaMetadata {
|
|
|
34
34
|
async getColumnMetadata(table: Table<any>): Promise<{ [columnName: string]: { type: string; isNullable: boolean } }> {
|
|
35
35
|
const qb = QueryBuilder.fromObject(
|
|
36
36
|
{
|
|
37
|
-
TABLE_SCHEMA: this.multiDbSupported ? this.dbDriver.getDbName() : undefined,
|
|
38
37
|
TABLE_NAME: table.name,
|
|
38
|
+
...(this.multiDbSupported ? { TABLE_SCHEMA: this.dbDriver.getDbName() } : {}),
|
|
39
39
|
},
|
|
40
40
|
'COLUMNS'
|
|
41
41
|
);
|
|
@@ -52,9 +52,9 @@ export class SchemaMetadata {
|
|
|
52
52
|
async getPrimaryKey(table: Table<any>): Promise<string[]> {
|
|
53
53
|
const qb = QueryBuilder.fromObject(
|
|
54
54
|
{
|
|
55
|
-
TABLE_SCHEMA: this.multiDbSupported ? this.dbDriver.getDbName() : undefined,
|
|
56
55
|
TABLE_NAME: table.name,
|
|
57
56
|
CONSTRAINT_NAME: 'PRIMARY',
|
|
57
|
+
...(this.multiDbSupported ? { TABLE_SCHEMA: this.dbDriver.getDbName() } : {}),
|
|
58
58
|
},
|
|
59
59
|
'KEY_COLUMN_USAGE'
|
|
60
60
|
);
|
|
@@ -73,8 +73,8 @@ export class SchemaMetadata {
|
|
|
73
73
|
): Promise<{ [columnName: string]: { referencedTableName: string; referencedColumnName: string } }> {
|
|
74
74
|
const qb = QueryBuilder.fromObject(
|
|
75
75
|
{
|
|
76
|
-
TABLE_SCHEMA: this.multiDbSupported ? this.dbDriver.getDbName() : undefined,
|
|
77
76
|
TABLE_NAME: table.name,
|
|
77
|
+
...(this.multiDbSupported ? { TABLE_SCHEMA: this.dbDriver.getDbName() } : {}),
|
|
78
78
|
},
|
|
79
79
|
'KEY_COLUMN_USAGE'
|
|
80
80
|
);
|
|
@@ -98,8 +98,8 @@ export class SchemaMetadata {
|
|
|
98
98
|
async getUniqueColumns(table: Table<any>): Promise<string[]> {
|
|
99
99
|
const qb = QueryBuilder.fromObject(
|
|
100
100
|
{
|
|
101
|
-
TABLE_SCHEMA: this.multiDbSupported ? this.dbDriver.getDbName() : undefined,
|
|
102
101
|
TABLE_NAME: table.name,
|
|
102
|
+
...(this.multiDbSupported ? { TABLE_SCHEMA: this.dbDriver.getDbName() } : {}),
|
|
103
103
|
},
|
|
104
104
|
'KEY_COLUMN_USAGE'
|
|
105
105
|
);
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { Db, DbDriver } from '../src/Db';
|
|
2
|
+
import { Moment } from 'moment';
|
|
3
|
+
import { ReferenceArray } from '../src/reference/ReferenceArray';
|
|
4
|
+
import { Reference } from '../src/reference/Reference';
|
|
5
|
+
import {
|
|
6
|
+
IntegerColumn,
|
|
7
|
+
StringColumn,
|
|
8
|
+
FloatColumn,
|
|
9
|
+
DecimalColumn,
|
|
10
|
+
BooleanColumn,
|
|
11
|
+
DateColumn,
|
|
12
|
+
DateTimeColumn,
|
|
13
|
+
BinaryColumn,
|
|
14
|
+
UuidColumn,
|
|
15
|
+
PasswordColumn,
|
|
16
|
+
ObjectColumn,
|
|
17
|
+
ArrayColumn,
|
|
18
|
+
ReferenceArrayColumn,
|
|
19
|
+
ReferenceColumn,
|
|
20
|
+
} from '../src/Columns';
|
|
21
|
+
import { withRecordColumns, Record } from '../src/Record';
|
|
22
|
+
import { Table } from '../src/Table';
|
|
23
|
+
|
|
24
|
+
export interface TestRecord extends Record {
|
|
25
|
+
integerColumn?: number | null;
|
|
26
|
+
stringColumn?: string | null;
|
|
27
|
+
floatColumn?: number | null;
|
|
28
|
+
decimalColumn?: number | null;
|
|
29
|
+
booleanColumn?: boolean | null;
|
|
30
|
+
dateColumn?: Date | null;
|
|
31
|
+
dateTimeColumn?: Moment | null;
|
|
32
|
+
binaryColumn?: number | null;
|
|
33
|
+
uuidColumn?: string | null;
|
|
34
|
+
passwordColumn?: string | null;
|
|
35
|
+
objectColumn?: any | null;
|
|
36
|
+
arrayColumn?: any[] | null;
|
|
37
|
+
referenceArrayColumn?: ReferenceArray<any> | null;
|
|
38
|
+
referenceColumn?: Reference<any> | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class TestTable extends Table<TestRecord> {
|
|
42
|
+
public name = 'db_test_table';
|
|
43
|
+
public columns = withRecordColumns<TestRecord>({
|
|
44
|
+
integerColumn: new IntegerColumn('integer_column'),
|
|
45
|
+
stringColumn: new StringColumn('string_column', {}, 255),
|
|
46
|
+
floatColumn: new FloatColumn('float_column'),
|
|
47
|
+
decimalColumn: new DecimalColumn('decimal_column'),
|
|
48
|
+
booleanColumn: new BooleanColumn('boolean_column'),
|
|
49
|
+
dateColumn: new DateColumn('date_column'),
|
|
50
|
+
dateTimeColumn: new DateTimeColumn('date_time_column'),
|
|
51
|
+
binaryColumn: new BinaryColumn('binary_column'),
|
|
52
|
+
uuidColumn: new UuidColumn('uuid_column'),
|
|
53
|
+
passwordColumn: new PasswordColumn('password_column'),
|
|
54
|
+
objectColumn: new ObjectColumn('object_column'),
|
|
55
|
+
arrayColumn: new ArrayColumn('array_column'),
|
|
56
|
+
referenceArrayColumn: new ReferenceArrayColumn('reference_array_column', 'reference_table', false),
|
|
57
|
+
referenceColumn: new ReferenceColumn('reference_column', 'reference_table', false),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Used for testing purposes only.
|
|
63
|
+
* */
|
|
64
|
+
export const getColumnTypeTestTable = (tableName: string) => {
|
|
65
|
+
const testTable = new TestTable();
|
|
66
|
+
if (testTable.name === tableName) {
|
|
67
|
+
return new TestTable();
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`Cannot find test table: ${tableName}`);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const columnTypeTests = (driver: DbDriver, dropTable: (table: Table<any>) => Promise<void>) => {
|
|
73
|
+
return () => {
|
|
74
|
+
const db = new Db(driver, getColumnTypeTestTable);
|
|
75
|
+
|
|
76
|
+
beforeAll(async () => {
|
|
77
|
+
if (driver.start) {
|
|
78
|
+
await driver.start();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await driver.getTableManager().loadTable(new TestTable());
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
afterAll(async () => {
|
|
85
|
+
await dropTable(new TestTable());
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('Insert record with all null values', async () => {
|
|
89
|
+
const testRecord: Omit<TestRecord, keyof Record> = {
|
|
90
|
+
integerColumn: null,
|
|
91
|
+
stringColumn: null,
|
|
92
|
+
floatColumn: null,
|
|
93
|
+
decimalColumn: null,
|
|
94
|
+
booleanColumn: null,
|
|
95
|
+
dateColumn: null,
|
|
96
|
+
dateTimeColumn: null,
|
|
97
|
+
binaryColumn: null,
|
|
98
|
+
uuidColumn: null,
|
|
99
|
+
passwordColumn: null,
|
|
100
|
+
objectColumn: null,
|
|
101
|
+
arrayColumn: null,
|
|
102
|
+
referenceArrayColumn: null,
|
|
103
|
+
referenceColumn: null,
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const testTable: Table<TestRecord> = new TestTable();
|
|
107
|
+
const insertedRecord = await db.insert(testTable, testRecord);
|
|
108
|
+
const fetchedRecord = await db.get(testTable, { id: insertedRecord.id });
|
|
109
|
+
|
|
110
|
+
expect(fetchedRecord).toBeTruthy();
|
|
111
|
+
expect(fetchedRecord.integerColumn).toBeNull();
|
|
112
|
+
expect(fetchedRecord.stringColumn).toBeNull();
|
|
113
|
+
expect(fetchedRecord.floatColumn).toBeNull();
|
|
114
|
+
expect(fetchedRecord.decimalColumn).toBeNull();
|
|
115
|
+
expect(fetchedRecord.booleanColumn).toBeNull();
|
|
116
|
+
expect(fetchedRecord.dateColumn).toBeNull();
|
|
117
|
+
expect(fetchedRecord.dateTimeColumn).toBeNull();
|
|
118
|
+
expect(fetchedRecord.binaryColumn).toBeNull();
|
|
119
|
+
expect(fetchedRecord.uuidColumn).toBeNull();
|
|
120
|
+
expect(fetchedRecord.passwordColumn).toBeNull();
|
|
121
|
+
expect(fetchedRecord.objectColumn).toBeNull();
|
|
122
|
+
expect(fetchedRecord.arrayColumn).toBeNull();
|
|
123
|
+
expect(fetchedRecord.referenceColumn).toBeNull();
|
|
124
|
+
expect(fetchedRecord.referenceArrayColumn).toHaveProperty('_ids', []);
|
|
125
|
+
|
|
126
|
+
// Clean up
|
|
127
|
+
await db.delete(testTable, { id: fetchedRecord.id });
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('Insert record with all undefined values', async () => {
|
|
131
|
+
const testRecord: Omit<TestRecord, keyof Record> = {
|
|
132
|
+
integerColumn: undefined,
|
|
133
|
+
stringColumn: undefined,
|
|
134
|
+
floatColumn: undefined,
|
|
135
|
+
decimalColumn: undefined,
|
|
136
|
+
booleanColumn: undefined,
|
|
137
|
+
dateColumn: undefined,
|
|
138
|
+
dateTimeColumn: undefined,
|
|
139
|
+
binaryColumn: undefined,
|
|
140
|
+
uuidColumn: undefined,
|
|
141
|
+
passwordColumn: undefined,
|
|
142
|
+
objectColumn: undefined,
|
|
143
|
+
arrayColumn: undefined,
|
|
144
|
+
referenceArrayColumn: undefined,
|
|
145
|
+
referenceColumn: undefined,
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const testTable: Table<TestRecord> = new TestTable();
|
|
149
|
+
|
|
150
|
+
await expect(db.insert(testTable, testRecord)).rejects.toThrow();
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
};
|
package/test/CrudTests.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { DbDriver, Db } from '../src/Db';
|
|
|
3
3
|
import { withRecordColumns, Record } from '../src/Record';
|
|
4
4
|
import { BooleanColumn, DateColumn, StringColumn } from '../src/Columns';
|
|
5
5
|
import { Table } from '../src/Table';
|
|
6
|
-
import { log } from 'console';
|
|
7
6
|
|
|
8
7
|
export interface Employee extends Record {
|
|
9
8
|
name: string;
|
|
@@ -32,7 +31,7 @@ export class EmployeeTable extends Table<Employee> {
|
|
|
32
31
|
export const getTestTable = (tableName: string) => {
|
|
33
32
|
const employeeTable = new EmployeeTable();
|
|
34
33
|
if (employeeTable.name == tableName) {
|
|
35
|
-
return
|
|
34
|
+
return new EmployeeTable();
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
throw new Error(`Cannot find test table: ${tableName}`);
|
|
@@ -280,16 +279,16 @@ export const crudTests = (driver: DbDriver, dropTable: (table: Table<any>) => Pr
|
|
|
280
279
|
name: 'Cassidy',
|
|
281
280
|
jobTitle: 'Cowboy',
|
|
282
281
|
};
|
|
283
|
-
const
|
|
282
|
+
const emplyeeTable: Table<Employee> = new EmployeeTable();
|
|
284
283
|
|
|
285
|
-
const insertedEmployee1 = await db.insert(
|
|
286
|
-
const insertedEmployee2 = await db.insert(
|
|
287
|
-
const insertedEmployee3 = await db.insert(
|
|
284
|
+
const insertedEmployee1 = await db.insert(emplyeeTable, testEmployee1);
|
|
285
|
+
const insertedEmployee2 = await db.insert(emplyeeTable, testEmployee2);
|
|
286
|
+
const insertedEmployee3 = await db.insert(emplyeeTable, testEmployee3);
|
|
288
287
|
|
|
289
|
-
const sortQuery = new QueryBuilder<Employee>(
|
|
288
|
+
const sortQuery = new QueryBuilder<Employee>(emplyeeTable.name).sort([
|
|
290
289
|
{ field: 'name', byValues: ['Cassidy', 'Veronica', 'Kiriko'] },
|
|
291
290
|
]);
|
|
292
|
-
const sortResults = await db.query(
|
|
291
|
+
const sortResults = await db.query(emplyeeTable, sortQuery);
|
|
293
292
|
|
|
294
293
|
// Assertions
|
|
295
294
|
expect(sortResults.length).toBe(3);
|
|
@@ -298,12 +297,46 @@ export const crudTests = (driver: DbDriver, dropTable: (table: Table<any>) => Pr
|
|
|
298
297
|
expect(sortResults[2].name).toBe('Kiriko');
|
|
299
298
|
|
|
300
299
|
// Clean up
|
|
301
|
-
const qb = new QueryBuilder<Employee>(
|
|
300
|
+
const qb = new QueryBuilder<Employee>(emplyeeTable.name).condition({
|
|
302
301
|
field: 'id',
|
|
303
302
|
operator: 'IN',
|
|
304
303
|
value: [insertedEmployee1.id, insertedEmployee2.id, insertedEmployee3.id],
|
|
305
304
|
});
|
|
306
|
-
await db.delete(
|
|
305
|
+
await db.delete(emplyeeTable, qb);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('CRUD operations with undefined values', async () => {
|
|
309
|
+
const testEmployee1: Omit<Employee, keyof Record> = { name: 'Veronica', jobTitle: 'Software Engineer' };
|
|
310
|
+
const testEmployee2: Omit<Employee, keyof Record> = { name: 'Brent', jobTitle: undefined };
|
|
311
|
+
const emplyeeTable: Table<Employee> = new EmployeeTable();
|
|
312
|
+
|
|
313
|
+
// Insert operation with undefined values
|
|
314
|
+
await expect(db.insert(emplyeeTable, testEmployee2)).rejects.toThrow();
|
|
315
|
+
|
|
316
|
+
// Insert valid employee
|
|
317
|
+
const insertedEmployee1 = await db.insert(emplyeeTable, testEmployee1);
|
|
318
|
+
|
|
319
|
+
// Attempt to build query with undefined value
|
|
320
|
+
expect(() => {
|
|
321
|
+
new QueryBuilder<Employee>(emplyeeTable.name).condition({ field: 'jobTitle', operator: '=', value: undefined });
|
|
322
|
+
}).toThrow();
|
|
323
|
+
|
|
324
|
+
// Attempt to query
|
|
325
|
+
await expect(db.query(emplyeeTable, { id: insertedEmployee1, jobTitle: undefined })).rejects.toThrow();
|
|
326
|
+
|
|
327
|
+
// Update operation with undefined values
|
|
328
|
+
await expect(db.update(emplyeeTable, { id: insertedEmployee1.id, jobTitle: undefined })).rejects.toThrow();
|
|
329
|
+
|
|
330
|
+
// Attempt to delete
|
|
331
|
+
await expect(db.delete(emplyeeTable, { id: insertedEmployee1, jobTitle: undefined })).rejects.toThrow();
|
|
332
|
+
|
|
333
|
+
// Clean up
|
|
334
|
+
const deleteValidQuery = new QueryBuilder<Employee>(emplyeeTable.name).condition({
|
|
335
|
+
field: 'id',
|
|
336
|
+
operator: 'IN',
|
|
337
|
+
value: [insertedEmployee1.id],
|
|
338
|
+
});
|
|
339
|
+
await db.delete(emplyeeTable, deleteValidQuery);
|
|
307
340
|
});
|
|
308
341
|
};
|
|
309
342
|
};
|