@proteinjs/db 1.0.14 → 1.0.16
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/.eslintrc.js +20 -0
- package/.prettierignore +3 -0
- package/.prettierrc +8 -0
- package/CHANGELOG.md +14 -24
- package/LICENSE +21 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/src/Columns.d.ts.map +1 -1
- package/dist/src/Columns.js +34 -17
- package/dist/src/Columns.js.map +1 -1
- package/dist/src/Db.d.ts.map +1 -1
- package/dist/src/Db.js +45 -18
- package/dist/src/Db.js.map +1 -1
- package/dist/src/MigrationRunner.d.ts.map +1 -1
- package/dist/src/MigrationRunner.js +15 -7
- package/dist/src/MigrationRunner.js.map +1 -1
- package/dist/src/QueryBuilderFactory.d.ts.map +1 -1
- package/dist/src/QueryBuilderFactory.js +5 -7
- package/dist/src/QueryBuilderFactory.js.map +1 -1
- package/dist/src/Record.d.ts.map +1 -1
- package/dist/src/Record.js +4 -2
- package/dist/src/Record.js.map +1 -1
- package/dist/src/StatementConfigFactory.d.ts.map +1 -1
- package/dist/src/StatementConfigFactory.js +3 -2
- package/dist/src/StatementConfigFactory.js.map +1 -1
- package/dist/src/Table.d.ts.map +1 -1
- package/dist/src/Table.js +6 -3
- package/dist/src/Table.js.map +1 -1
- package/dist/src/auth/TableAuth.d.ts.map +1 -1
- package/dist/src/auth/TableAuth.js +16 -11
- package/dist/src/auth/TableAuth.js.map +1 -1
- package/dist/src/auth/TableServiceAuth.d.ts.map +1 -1
- package/dist/src/auth/TableServiceAuth.js +12 -6
- package/dist/src/auth/TableServiceAuth.js.map +1 -1
- package/dist/src/reference/Reference.d.ts.map +1 -1
- package/dist/src/reference/Reference.js.map +1 -1
- package/dist/src/reference/ReferenceArray.d.ts.map +1 -1
- package/dist/src/reference/ReferenceArray.js.map +1 -1
- package/dist/src/schema/SchemaMetadata.d.ts.map +1 -1
- package/dist/src/schema/SchemaMetadata.js +18 -15
- package/dist/src/schema/SchemaMetadata.js.map +1 -1
- package/dist/src/schema/SchemaOperations.d.ts.map +1 -1
- package/dist/src/schema/TableManager.d.ts.map +1 -1
- package/dist/src/schema/TableManager.js +24 -9
- package/dist/src/schema/TableManager.js.map +1 -1
- package/dist/src/serializers/GraphSerializer.d.ts.map +1 -1
- package/dist/src/serializers/QueryBuilderSerializer.d.ts.map +1 -1
- package/dist/src/serializers/ReferenceArraySerializer.d.ts.map +1 -1
- package/dist/src/serializers/ReferenceArraySerializer.js.map +1 -1
- package/dist/src/serializers/ReferenceSerializer.d.ts.map +1 -1
- package/dist/src/serializers/TableSerializer.d.ts.map +1 -1
- package/dist/src/services/DbService.d.ts.map +1 -1
- package/dist/src/source/SourceRecord.d.ts.map +1 -1
- package/dist/src/source/SourceRecord.js +7 -3
- package/dist/src/source/SourceRecord.js.map +1 -1
- package/dist/src/source/SourceRecordLoader.d.ts.map +1 -1
- package/dist/src/source/SourceRecordLoader.js +11 -4
- package/dist/src/source/SourceRecordLoader.js.map +1 -1
- package/dist/src/source/SourceRecordRepo.d.ts.map +1 -1
- package/dist/src/source/SourceRecordRepo.js.map +1 -1
- package/dist/src/tables/MigrationTable.d.ts.map +1 -1
- package/dist/src/tables/MigrationTable.js.map +1 -1
- package/dist/src/tables/tables.d.ts.map +1 -1
- package/dist/src/tables/tables.js.map +1 -1
- package/dist/test/CrudTests.d.ts +2 -0
- package/dist/test/CrudTests.d.ts.map +1 -1
- package/dist/test/CrudTests.js +19 -4
- package/dist/test/CrudTests.js.map +1 -1
- package/dist/test/TableManagerTests.d.ts.map +1 -1
- package/dist/test/TableManagerTests.js +17 -9
- package/dist/test/TableManagerTests.js.map +1 -1
- package/generated/index.ts +15 -18
- package/index.ts +1 -1
- package/jest.config.js +8 -17
- package/package.json +49 -43
- package/src/Columns.ts +284 -241
- package/src/Db.ts +188 -147
- package/src/MigrationRunner.ts +24 -10
- package/src/QueryBuilderFactory.ts +6 -9
- package/src/Record.ts +35 -29
- package/src/StatementConfigFactory.ts +14 -16
- package/src/Table.ts +91 -88
- package/src/auth/TableAuth.ts +37 -27
- package/src/auth/TableServiceAuth.ts +11 -9
- package/src/opt/moment.ts +1 -1
- package/src/reference/Reference.ts +7 -7
- package/src/reference/ReferenceArray.ts +8 -8
- package/src/schema/SchemaMetadata.ts +84 -64
- package/src/schema/SchemaOperations.ts +2 -2
- package/src/schema/TableManager.ts +178 -168
- package/src/serializers/GraphSerializer.ts +4 -4
- package/src/serializers/QueryBuilderSerializer.ts +11 -11
- package/src/serializers/ReferenceArraySerializer.ts +12 -8
- package/src/serializers/ReferenceSerializer.ts +7 -7
- package/src/serializers/TableSerializer.ts +5 -5
- package/src/services/DbService.ts +3 -3
- package/src/services/MigrationRunnerService.ts +1 -1
- package/src/source/SourceRecord.ts +43 -24
- package/src/source/SourceRecordLoader.ts +29 -18
- package/src/source/SourceRecordRepo.ts +3 -3
- package/src/tables/MigrationTable.ts +5 -5
- package/src/tables/tables.ts +1 -1
- package/test/CrudTests.ts +46 -26
- package/test/TableManagerTests.ts +329 -154
- package/tsconfig.json +18 -22
|
@@ -3,24 +3,27 @@ import { Columns, Table, getTables } from '../Table';
|
|
|
3
3
|
import { Record as DbRecord, withRecordColumns } from '../Record';
|
|
4
4
|
import { BooleanColumn } from '../Columns';
|
|
5
5
|
|
|
6
|
-
export const getSourceRecordLoaders = <T extends SourceRecord = SourceRecord>() =>
|
|
6
|
+
export const getSourceRecordLoaders = <T extends SourceRecord = SourceRecord>() =>
|
|
7
|
+
SourceRepository.get().objects<SourceRecordLoader<T>>('@proteinjs/db/SourceRecordLoader');
|
|
7
8
|
|
|
8
9
|
export function getSourceRecordTables() {
|
|
9
10
|
const tables = getTables();
|
|
10
11
|
const sourceRecordTables: Table<any>[] = [];
|
|
11
|
-
for (
|
|
12
|
-
if (isSourceRecordTable(table))
|
|
12
|
+
for (const table of tables) {
|
|
13
|
+
if (isSourceRecordTable(table)) {
|
|
13
14
|
sourceRecordTables.push(table);
|
|
15
|
+
}
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
return sourceRecordTables;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export function isSourceRecordTable(table: Table<any>) {
|
|
20
|
-
for (
|
|
22
|
+
for (const columnPropertyName in table.columns) {
|
|
21
23
|
const column = table.columns[columnPropertyName];
|
|
22
|
-
if (column.name == getSourceRecordColumns().isLoadedFromSource.name)
|
|
24
|
+
if (column.name == getSourceRecordColumns().isLoadedFromSource.name) {
|
|
23
25
|
return true;
|
|
26
|
+
}
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
return false;
|
|
@@ -34,42 +37,58 @@ const getSourceRecordColumns = (hideFromUi = true) => {
|
|
|
34
37
|
return {
|
|
35
38
|
isLoadedFromSource: new BooleanColumn('is_loaded_from_source', { ui: { hidden: hideFromUi } }),
|
|
36
39
|
};
|
|
37
|
-
}
|
|
40
|
+
};
|
|
38
41
|
|
|
39
42
|
/**
|
|
40
43
|
* Wrapper function to add default Record and SourceRecord columns to your table's columns.
|
|
41
|
-
*
|
|
44
|
+
*
|
|
42
45
|
* Note: using this requires an explicit dependency on moment@2.29.4 in your package (since transient dependencies are brittle by typescript's standards)
|
|
43
|
-
*
|
|
46
|
+
*
|
|
44
47
|
* @param columns your columns
|
|
45
48
|
* @param hideFromUi if true, source record columns are hidden from RecordTable and RecordForm
|
|
46
49
|
* @returns recordColumns & sourceRecordColumns & your columns
|
|
47
50
|
*/
|
|
48
|
-
export function withSourceRecordColumns<T extends SourceRecord>(
|
|
49
|
-
|
|
51
|
+
export function withSourceRecordColumns<T extends SourceRecord>(
|
|
52
|
+
columns: Columns<Omit<T, keyof SourceRecord>>,
|
|
53
|
+
hideFromUi?: boolean
|
|
54
|
+
): Columns<SourceRecord> & Columns<Omit<T, keyof SourceRecord>> {
|
|
55
|
+
return Object.assign(
|
|
56
|
+
Object.assign({}, getSourceRecordColumns(hideFromUi)),
|
|
57
|
+
withRecordColumns<DbRecord>(columns) as any
|
|
58
|
+
);
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
type InferRecordFromTable<T> = T extends Table<infer R> ? R : never;
|
|
53
|
-
type InferRecordWithoutTimestamps<T> = Omit<InferRecordFromTable<T>, 'created'|'updated'>;
|
|
54
|
-
type RequiredProperties<T> = Pick<
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
[
|
|
59
|
-
|
|
62
|
+
type InferRecordWithoutTimestamps<T> = Omit<InferRecordFromTable<T>, 'created' | 'updated'>;
|
|
63
|
+
type RequiredProperties<T> = Pick<
|
|
64
|
+
T,
|
|
65
|
+
{
|
|
66
|
+
[K in keyof T]: T extends Record<K, T[K]> ? K : never;
|
|
67
|
+
}[keyof T]
|
|
68
|
+
>;
|
|
69
|
+
type OptionalProperties<T> = Pick<
|
|
70
|
+
T,
|
|
71
|
+
{
|
|
72
|
+
[K in keyof T]: T extends Record<K, T[K]> ? never : K;
|
|
73
|
+
}[keyof T]
|
|
74
|
+
>;
|
|
60
75
|
|
|
61
76
|
/**
|
|
62
77
|
* Use this to load a record from source into the db.
|
|
63
|
-
*
|
|
78
|
+
*
|
|
64
79
|
* On Db.init, the record will be inserted if it doesn't exist, and updated if it does exist to mirror what is in source.
|
|
65
|
-
*
|
|
80
|
+
*
|
|
66
81
|
* If the SourceRecordLoader is deleted from source, the record will be deleted from the db on server startup. This will also be the behavior if id is changed - the record with the old id will be deleted.
|
|
67
82
|
*/
|
|
68
83
|
export interface SourceRecordLoader<T extends SourceRecord> extends Loadable {
|
|
69
|
-
table: Table<T
|
|
84
|
+
table: Table<T>;
|
|
70
85
|
record: {
|
|
71
|
-
[P in keyof RequiredProperties<InferRecordWithoutTimestamps<Table<T>>>]: RequiredProperties<
|
|
86
|
+
[P in keyof RequiredProperties<InferRecordWithoutTimestamps<Table<T>>>]: RequiredProperties<
|
|
87
|
+
InferRecordWithoutTimestamps<Table<T>>
|
|
88
|
+
>[P];
|
|
72
89
|
} & {
|
|
73
|
-
[P in keyof OptionalProperties<InferRecordWithoutTimestamps<Table<T>>>]?: OptionalProperties<
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
[P in keyof OptionalProperties<InferRecordWithoutTimestamps<Table<T>>>]?: OptionalProperties<
|
|
91
|
+
InferRecordWithoutTimestamps<Table<T>>
|
|
92
|
+
>[P];
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -5,7 +5,9 @@ import { Table } from '../Table';
|
|
|
5
5
|
import { getSystemDb } from '../Db';
|
|
6
6
|
import { SourceRecordRepo } from './SourceRecordRepo';
|
|
7
7
|
|
|
8
|
-
type SourceRecordsMap = {
|
|
8
|
+
type SourceRecordsMap = {
|
|
9
|
+
[tableName: string]: { table: Table<any>; records: Omit<SourceRecord, 'created' | 'updated'>[]; recordIds: string[] };
|
|
10
|
+
};
|
|
9
11
|
|
|
10
12
|
export class SourceRecordLoader {
|
|
11
13
|
private logger = new Logger(this.constructor.name);
|
|
@@ -13,7 +15,7 @@ export class SourceRecordLoader {
|
|
|
13
15
|
async load() {
|
|
14
16
|
const sourceRecordsMap = await this.getSourceRecordsMap();
|
|
15
17
|
const db = getSystemDb();
|
|
16
|
-
for (
|
|
18
|
+
for (const tableName in sourceRecordsMap) {
|
|
17
19
|
let insertCount = 0;
|
|
18
20
|
let updateCount = 0;
|
|
19
21
|
let deleteCount = 0;
|
|
@@ -21,12 +23,13 @@ export class SourceRecordLoader {
|
|
|
21
23
|
const sourceRecordIds = sourceRecordsMap[tableName].recordIds;
|
|
22
24
|
if (!table.sourceRecordOptions.doNotDeleteSourceRecordsFromDb) {
|
|
23
25
|
const qb = QueryBuilder.fromObject<SourceRecord>({ isLoadedFromSource: true }, table.name);
|
|
24
|
-
if (sourceRecordIds.length > 0)
|
|
26
|
+
if (sourceRecordIds.length > 0) {
|
|
25
27
|
qb.condition({ field: 'id', operator: 'NOT IN', value: sourceRecordIds });
|
|
26
|
-
|
|
28
|
+
}
|
|
29
|
+
|
|
27
30
|
deleteCount = await db.delete(table, qb);
|
|
28
31
|
}
|
|
29
|
-
|
|
32
|
+
|
|
30
33
|
const sourceRecords = sourceRecordsMap[tableName].records;
|
|
31
34
|
for (let sourceRecord of sourceRecords) {
|
|
32
35
|
sourceRecord.isLoadedFromSource = true;
|
|
@@ -38,31 +41,39 @@ export class SourceRecordLoader {
|
|
|
38
41
|
sourceRecord = { ...sourceRecord, ...dbSourceRecord };
|
|
39
42
|
insertCount += 1;
|
|
40
43
|
}
|
|
41
|
-
|
|
42
|
-
new SourceRecordRepo().loadSourceRecord(table.name, sourceRecord as any)
|
|
44
|
+
|
|
45
|
+
new SourceRecordRepo().loadSourceRecord(table.name, sourceRecord as any);
|
|
43
46
|
}
|
|
44
|
-
|
|
45
|
-
this.logger.info(
|
|
47
|
+
|
|
48
|
+
this.logger.info(
|
|
49
|
+
`(${table.name}) Loaded ${sourceRecords.length} ${sourceRecords.length == 1 ? 'record' : 'records'} from source (inserts: ${insertCount}, updates: ${updateCount}, deletes: ${deleteCount})`
|
|
50
|
+
);
|
|
46
51
|
}
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
private async getSourceRecordsMap() {
|
|
50
55
|
const sourceRecordsMap: SourceRecordsMap = {};
|
|
51
56
|
const sourceRecordTables = getSourceRecordTables();
|
|
52
|
-
for (
|
|
53
|
-
if (!sourceRecordsMap[sourceRecordTable.name])
|
|
54
|
-
sourceRecordsMap[sourceRecordTable.name] = { table: sourceRecordTable, records: [], recordIds: []};
|
|
57
|
+
for (const sourceRecordTable of sourceRecordTables) {
|
|
58
|
+
if (!sourceRecordsMap[sourceRecordTable.name]) {
|
|
59
|
+
sourceRecordsMap[sourceRecordTable.name] = { table: sourceRecordTable, records: [], recordIds: [] };
|
|
60
|
+
}
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
const sourceRecordLoaders = getSourceRecordLoaders();
|
|
58
|
-
for (
|
|
59
|
-
if (!sourceRecordsMap[sourceRecordLoader.table.name])
|
|
60
|
-
sourceRecordsMap[sourceRecordLoader.table.name] = {
|
|
61
|
-
|
|
64
|
+
for (const sourceRecordLoader of sourceRecordLoaders) {
|
|
65
|
+
if (!sourceRecordsMap[sourceRecordLoader.table.name]) {
|
|
66
|
+
sourceRecordsMap[sourceRecordLoader.table.name] = {
|
|
67
|
+
table: sourceRecordLoader.table,
|
|
68
|
+
records: [],
|
|
69
|
+
recordIds: [],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
62
73
|
sourceRecordsMap[sourceRecordLoader.table.name].records.push(sourceRecordLoader.record);
|
|
63
74
|
sourceRecordsMap[sourceRecordLoader.table.name].recordIds.push(sourceRecordLoader.record.id);
|
|
64
75
|
}
|
|
65
|
-
|
|
76
|
+
|
|
66
77
|
return sourceRecordsMap;
|
|
67
78
|
}
|
|
68
|
-
}
|
|
79
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { SourceRecord } from './SourceRecord';
|
|
2
2
|
|
|
3
3
|
export class SourceRecordRepo {
|
|
4
|
-
private static SOURCE_RECORD_MAP: {[key: string]: SourceRecord} = {};
|
|
4
|
+
private static SOURCE_RECORD_MAP: { [key: string]: SourceRecord } = {};
|
|
5
5
|
|
|
6
6
|
private getKey(tableName: string, recordId: string) {
|
|
7
7
|
return `${tableName}:${recordId}`;
|
|
@@ -11,7 +11,7 @@ export class SourceRecordRepo {
|
|
|
11
11
|
SourceRecordRepo.SOURCE_RECORD_MAP[this.getKey(tableName, sourceRecord.id)] = sourceRecord;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
getSourceRecord<T extends SourceRecord = SourceRecord>(tableName: string, sourceRecordId: string): T|undefined {
|
|
14
|
+
getSourceRecord<T extends SourceRecord = SourceRecord>(tableName: string, sourceRecordId: string): T | undefined {
|
|
15
15
|
return SourceRecordRepo.SOURCE_RECORD_MAP[this.getKey(tableName, sourceRecordId)] as T;
|
|
16
16
|
}
|
|
17
|
-
}
|
|
17
|
+
}
|
|
@@ -5,19 +5,19 @@ import { SourceRecord, withSourceRecordColumns } from '../source/SourceRecord';
|
|
|
5
5
|
|
|
6
6
|
export interface Migration extends SourceRecord {
|
|
7
7
|
description: string;
|
|
8
|
-
status?: 'proposed'|'running'|'success'|'failure';
|
|
8
|
+
status?: 'proposed' | 'running' | 'success' | 'failure';
|
|
9
9
|
failureMessage?: string;
|
|
10
10
|
failureStack?: string;
|
|
11
11
|
startTime?: Moment;
|
|
12
12
|
endTime?: Moment;
|
|
13
13
|
duration?: string;
|
|
14
14
|
output?: any;
|
|
15
|
-
run: () => Promise<any|void>;
|
|
15
|
+
run: () => Promise<any | void>;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export class MigrationTable extends Table<Migration> {
|
|
19
19
|
public name = 'migration';
|
|
20
|
-
|
|
20
|
+
public columns = withSourceRecordColumns<Migration>({
|
|
21
21
|
description: new StringColumn('description', {}, 4000),
|
|
22
22
|
status: new StringColumn('status', { defaultValue: async () => 'proposed' }),
|
|
23
23
|
failureMessage: new StringColumn('failure_message', {}, 4000),
|
|
@@ -26,11 +26,11 @@ export class MigrationTable extends Table<Migration> {
|
|
|
26
26
|
endTime: new DateTimeColumn('end_time'),
|
|
27
27
|
duration: new StringColumn('duration'),
|
|
28
28
|
output: new ObjectColumn('output'),
|
|
29
|
-
|
|
29
|
+
});
|
|
30
30
|
public sourceRecordOptions = {
|
|
31
31
|
doNotDeleteSourceRecordsFromDb: true,
|
|
32
32
|
ui: {
|
|
33
33
|
hideColumns: true,
|
|
34
34
|
},
|
|
35
35
|
};
|
|
36
|
-
}
|
|
36
|
+
}
|
package/src/tables/tables.ts
CHANGED
package/test/CrudTests.ts
CHANGED
|
@@ -5,46 +5,48 @@ import { StringColumn } from '../src/Columns';
|
|
|
5
5
|
import { Table } from '../src/Table';
|
|
6
6
|
|
|
7
7
|
export interface Employee extends Record {
|
|
8
|
-
|
|
8
|
+
name: string;
|
|
9
9
|
department?: string;
|
|
10
|
+
object?: string;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export class EmployeeTable extends Table<Employee> {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
name = 'db_test_employee';
|
|
15
|
+
columns = withRecordColumns<Employee>({
|
|
16
|
+
name: new StringColumn('name'),
|
|
16
17
|
department: new StringColumn('department'),
|
|
17
|
-
|
|
18
|
+
object: new StringColumn('object'),
|
|
19
|
+
});
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
export const crudTests = (
|
|
21
|
-
driver: DbDriver,
|
|
22
|
-
dropTable: (table: Table<any>) => Promise<void>,
|
|
23
|
-
) => {
|
|
22
|
+
export const crudTests = (driver: DbDriver, dropTable: (table: Table<any>) => Promise<void>) => {
|
|
24
23
|
return () => {
|
|
25
24
|
const getTable = (tableName: string) => {
|
|
26
25
|
const employeeTable = new EmployeeTable();
|
|
27
|
-
if (employeeTable.name == tableName)
|
|
26
|
+
if (employeeTable.name == tableName) {
|
|
28
27
|
return employeeTable;
|
|
29
|
-
|
|
28
|
+
}
|
|
29
|
+
|
|
30
30
|
throw new Error('Cannot find test table');
|
|
31
31
|
};
|
|
32
32
|
const db = new Db(driver, getTable);
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
beforeAll(async () => {
|
|
35
|
-
if (driver.start)
|
|
35
|
+
if (driver.start) {
|
|
36
36
|
await driver.start();
|
|
37
|
+
}
|
|
37
38
|
|
|
38
39
|
await driver.getTableManager().loadTable(new EmployeeTable());
|
|
39
|
-
})
|
|
40
|
-
|
|
40
|
+
});
|
|
41
|
+
|
|
41
42
|
afterAll(async () => {
|
|
42
43
|
await dropTable(new EmployeeTable());
|
|
43
|
-
|
|
44
|
-
if (driver.stop)
|
|
44
|
+
|
|
45
|
+
if (driver.stop) {
|
|
45
46
|
await driver.stop();
|
|
47
|
+
}
|
|
46
48
|
});
|
|
47
|
-
|
|
49
|
+
|
|
48
50
|
test('Insert', async () => {
|
|
49
51
|
const testEmployee: Omit<Employee, keyof Record> = { name: 'Veronica' };
|
|
50
52
|
const emplyeeTable: Table<Employee> = new EmployeeTable();
|
|
@@ -53,18 +55,28 @@ export const crudTests = (
|
|
|
53
55
|
expect(fetchedEmployee).toBeTruthy();
|
|
54
56
|
await db.delete(emplyeeTable, { id: fetchedEmployee.id });
|
|
55
57
|
});
|
|
56
|
-
|
|
58
|
+
|
|
57
59
|
test('Update', async () => {
|
|
58
60
|
const testEmployee: Omit<Employee, keyof Record> = { name: 'Veronica' };
|
|
59
61
|
const emplyeeTable: Table<Employee> = new EmployeeTable();
|
|
60
62
|
const insertedEmployee = await db.insert(emplyeeTable, testEmployee);
|
|
61
|
-
const updateCount = await db.update(
|
|
63
|
+
const updateCount = await db.update(
|
|
64
|
+
emplyeeTable,
|
|
65
|
+
{
|
|
66
|
+
name: 'Veronican',
|
|
67
|
+
department: 'Cake Factory',
|
|
68
|
+
object:
|
|
69
|
+
'{"cookie":{"originalMaxAge":5184000000,"expires":"2024-07-08T06:16:07.134Z","httpOnly":true,"path":"/"},"passport":{"user":"brent@n3xa.io"}}',
|
|
70
|
+
},
|
|
71
|
+
{ id: insertedEmployee.id }
|
|
72
|
+
);
|
|
62
73
|
expect(updateCount).toBe(1);
|
|
63
74
|
const fetchedEmployee = await db.get(emplyeeTable, { id: insertedEmployee.id });
|
|
75
|
+
expect(fetchedEmployee.name).toBe('Veronican');
|
|
64
76
|
expect(fetchedEmployee.department).toBe('Cake Factory');
|
|
65
77
|
await db.delete(emplyeeTable, { id: fetchedEmployee.id });
|
|
66
78
|
});
|
|
67
|
-
|
|
79
|
+
|
|
68
80
|
test('Delete', async () => {
|
|
69
81
|
const testEmployee: Omit<Employee, keyof Record> = { name: 'Veronica' };
|
|
70
82
|
const emplyeeTable: Table<Employee> = new EmployeeTable();
|
|
@@ -76,7 +88,7 @@ export const crudTests = (
|
|
|
76
88
|
fetchedEmployee = await db.get(emplyeeTable, { id: insertedEmployee.id });
|
|
77
89
|
expect(fetchedEmployee).toBeFalsy();
|
|
78
90
|
});
|
|
79
|
-
|
|
91
|
+
|
|
80
92
|
test('Query', async () => {
|
|
81
93
|
const testEmployee1: Omit<Employee, keyof Record> = { name: 'Veronica', department: 'Cake Factory' };
|
|
82
94
|
const testEmployee2: Omit<Employee, keyof Record> = { name: 'Brent', department: 'Cake Factory' };
|
|
@@ -89,10 +101,14 @@ export const crudTests = (
|
|
|
89
101
|
expect(fetchedEmployees.length).toBe(2);
|
|
90
102
|
expect(fetchedEmployees[0].name).toBe('Veronica');
|
|
91
103
|
expect(fetchedEmployees[1].name).toBe('Brent');
|
|
92
|
-
const qb = new QueryBuilder<Employee>(emplyeeTable.name).condition({
|
|
104
|
+
const qb = new QueryBuilder<Employee>(emplyeeTable.name).condition({
|
|
105
|
+
field: 'id',
|
|
106
|
+
operator: 'IN',
|
|
107
|
+
value: [fetchedEmployee1.id, fetchedEmployee2.id, fetchedEmployee3.id],
|
|
108
|
+
});
|
|
93
109
|
await db.delete(emplyeeTable, qb);
|
|
94
110
|
});
|
|
95
|
-
|
|
111
|
+
|
|
96
112
|
test('Get row count', async () => {
|
|
97
113
|
const testEmployee1: Omit<Employee, keyof Record> = { name: 'Veronica', department: 'Cake Factory' };
|
|
98
114
|
const testEmployee2: Omit<Employee, keyof Record> = { name: 'Brent', department: 'Cake Factory' };
|
|
@@ -103,8 +119,12 @@ export const crudTests = (
|
|
|
103
119
|
const fetchedEmployee3 = await db.insert(emplyeeTable, testEmployee3);
|
|
104
120
|
const rowCount = await db.getRowCount(emplyeeTable, { department: 'Cake Factory' });
|
|
105
121
|
expect(rowCount).toBe(2);
|
|
106
|
-
const qb = new QueryBuilder<Employee>(emplyeeTable.name).condition({
|
|
122
|
+
const qb = new QueryBuilder<Employee>(emplyeeTable.name).condition({
|
|
123
|
+
field: 'id',
|
|
124
|
+
operator: 'IN',
|
|
125
|
+
value: [fetchedEmployee1.id, fetchedEmployee2.id, fetchedEmployee3.id],
|
|
126
|
+
});
|
|
107
127
|
await db.delete(emplyeeTable, qb);
|
|
108
128
|
});
|
|
109
129
|
};
|
|
110
|
-
}
|
|
130
|
+
};
|