-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsql-source.ts
115 lines (95 loc) · 3.04 KB
/
sql-source.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { Assertion } from '@orbit/core';
import { queryable, updatable, RequestOptions } from '@orbit/data';
import {
RecordSourceQueryOptions,
RecordSchema,
RecordSourceSettings,
RecordQueryable,
RecordUpdatable,
RecordSource,
RecordTransform,
RecordQuery,
} from '@orbit/records';
import Knex from 'knex';
import { Processor, ProcessorSettings } from './processor';
export interface SQLQueryOptions extends RecordSourceQueryOptions {}
export interface SQLTransformOptions extends RequestOptions {}
export interface SQLSourceSettings
extends RecordSourceSettings<SQLQueryOptions, SQLTransformOptions> {
knex?: Knex.Config;
autoMigrate?: boolean;
}
export interface SQLSource
extends RecordSource<SQLQueryOptions, SQLTransformOptions>,
RecordQueryable<unknown>,
RecordUpdatable<unknown> {}
/**
* Source for storing data in SQL database.
*/
@queryable
@updatable
export class SQLSource extends RecordSource<
SQLQueryOptions,
SQLTransformOptions
> {
protected _processor: Processor;
constructor(settings: SQLSourceSettings) {
settings.name = settings.name || 'sql';
if (!settings.schema) {
new Assertion(
"SQLSource's `schema` must be specified in `settings.schema` constructor argument"
);
}
if (!settings.knex) {
new Assertion(
"SQLSource's `knex` must be specified in `settings.knex` constructor argument"
);
}
const autoActivate = settings.autoActivate;
settings.autoActivate = false;
super(settings);
let processorSettings: ProcessorSettings = {
knex: settings.knex as Knex.Config,
schema: settings.schema as RecordSchema,
autoMigrate: settings.autoMigrate,
};
this._processor = new Processor(processorSettings);
if (autoActivate !== false) {
this.activate();
}
}
async _activate() {
await super._activate();
await this._processor.openDB();
}
async deactivate() {
await super.deactivate();
return this._processor.closeDB();
}
/////////////////////////////////////////////////////////////////////////////
// Updatable interface implementation
/////////////////////////////////////////////////////////////////////////////
async _update(transform: RecordTransform): Promise<any> {
if (!this.transformLog.contains(transform.id)) {
const operations = Array.isArray(transform.operations)
? transform.operations
: [transform.operations];
const data = await this._processor.patch(operations);
await this.transformed([transform]);
return {
transform: [transform],
data: Array.isArray(transform.operations) ? data : data[0],
};
}
}
/////////////////////////////////////////////////////////////////////////////
// Queryable interface implementation
/////////////////////////////////////////////////////////////////////////////
async _query(query: RecordQuery): Promise<any> {
const data = await this._processor.query(query);
return {
transform: [],
data: Array.isArray(data) ? data[0] : data,
};
}
}