query-core 0.0.4
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/README.md +1 -0
- package/lib/batch.js +262 -0
- package/lib/build.js +784 -0
- package/lib/index.js +7 -0
- package/package.json +27 -0
- package/src/batch.ts +218 -0
- package/src/build.ts +683 -0
- package/src/index.ts +17 -0
- package/src/metadata.ts +73 -0
- package/tsconfig.json +26 -0
package/lib/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "query-core",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "query",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build:lib": "tsc",
|
|
9
|
+
"clean:lib": "rimraf lib"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"tslint": "5.10.0",
|
|
13
|
+
"typescript": "^3.3.3333"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"registry": "https://registry.npmjs.org/"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git@github.com/core-ts/query"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"query",
|
|
24
|
+
"query-core",
|
|
25
|
+
"sql"
|
|
26
|
+
]
|
|
27
|
+
}
|
package/src/batch.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { buildToInsert, buildToInsertBatch, buildToUpdate, buildToUpdateBatch, version } from './build';
|
|
2
|
+
import { Attributes, Statement } from './metadata';
|
|
3
|
+
|
|
4
|
+
export class SqlInserter<T> {
|
|
5
|
+
version?: string;
|
|
6
|
+
constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public map?: (v: T) => T) {
|
|
7
|
+
this.write = this.write.bind(this);
|
|
8
|
+
const x = version(attributes);
|
|
9
|
+
if (x) {
|
|
10
|
+
this.version = x.name;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
write(obj: T): Promise<number> {
|
|
14
|
+
if (!obj) {
|
|
15
|
+
return Promise.resolve(0);
|
|
16
|
+
}
|
|
17
|
+
let obj2: NonNullable<T> | T = obj;
|
|
18
|
+
if (this.map) {
|
|
19
|
+
obj2 = this.map(obj);
|
|
20
|
+
}
|
|
21
|
+
const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
|
|
22
|
+
if (stmt) {
|
|
23
|
+
return this.exec(stmt.query, stmt.params);
|
|
24
|
+
} else {
|
|
25
|
+
return Promise.resolve(0);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
30
|
+
export class SqlUpdater<T> {
|
|
31
|
+
version?: string;
|
|
32
|
+
constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, public map?: (v: T) => T) {
|
|
33
|
+
this.write = this.write.bind(this);
|
|
34
|
+
const x = version(attributes);
|
|
35
|
+
if (x) {
|
|
36
|
+
this.version = x.name;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
write(obj: T): Promise<number> {
|
|
40
|
+
if (!obj) {
|
|
41
|
+
return Promise.resolve(0);
|
|
42
|
+
}
|
|
43
|
+
let obj2: NonNullable<T> | T = obj;
|
|
44
|
+
if (this.map) {
|
|
45
|
+
obj2 = this.map(obj);
|
|
46
|
+
}
|
|
47
|
+
const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
|
|
48
|
+
if (stmt) {
|
|
49
|
+
return this.exec(stmt.query, stmt.params);
|
|
50
|
+
} else {
|
|
51
|
+
return Promise.resolve(0);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
56
|
+
export class SqlBatchInserter<T> {
|
|
57
|
+
version?: string;
|
|
58
|
+
constructor(public exec: (sql: string, args?: any[]) => Promise<number>, public table: string, public attributes: Attributes, public param: ((i: number) => string) | boolean, public map?: (v: T) => T) {
|
|
59
|
+
this.write = this.write.bind(this);
|
|
60
|
+
const x = version(attributes);
|
|
61
|
+
if (x) {
|
|
62
|
+
this.version = x.name;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
write(objs: T[]): Promise<number> {
|
|
66
|
+
if (!objs || objs.length === 0) {
|
|
67
|
+
return Promise.resolve(0);
|
|
68
|
+
}
|
|
69
|
+
let list = objs;
|
|
70
|
+
if (this.map) {
|
|
71
|
+
list = [];
|
|
72
|
+
for (const obj of objs) {
|
|
73
|
+
const obj2 = this.map(obj);
|
|
74
|
+
list.push(obj2);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const stmt = buildToInsertBatch(list, this.table, this.attributes, this.param, this.version);
|
|
78
|
+
if (stmt) {
|
|
79
|
+
return this.exec(stmt.query, stmt.params);
|
|
80
|
+
} else {
|
|
81
|
+
return Promise.resolve(0);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
86
|
+
export class SqlBatchUpdater<T> {
|
|
87
|
+
version?: string;
|
|
88
|
+
constructor(public execBatch: (statements: Statement[]) => Promise<number>, public table: string, public attributes: Attributes, public param: (i: number) => string, protected notSkipInvalid?: boolean, public map?: (v: T) => T) {
|
|
89
|
+
this.write = this.write.bind(this);
|
|
90
|
+
const x = version(attributes);
|
|
91
|
+
if (x) {
|
|
92
|
+
this.version = x.name;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
write(objs: T[]): Promise<number> {
|
|
96
|
+
if (!objs || objs.length === 0) {
|
|
97
|
+
return Promise.resolve(0);
|
|
98
|
+
}
|
|
99
|
+
let list = objs;
|
|
100
|
+
if (this.map) {
|
|
101
|
+
list = [];
|
|
102
|
+
for (const obj of objs) {
|
|
103
|
+
const obj2 = this.map(obj);
|
|
104
|
+
list.push(obj2);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const stmts = buildToUpdateBatch(list, this.table, this.attributes, this.param, this.notSkipInvalid);
|
|
108
|
+
if (stmts && stmts.length > 0) {
|
|
109
|
+
return this.execBatch(stmts);
|
|
110
|
+
} else {
|
|
111
|
+
return Promise.resolve(0);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
116
|
+
export class StreamInserter<T> {
|
|
117
|
+
list: T[] = [];
|
|
118
|
+
size: number = 0;
|
|
119
|
+
version?: string;
|
|
120
|
+
map?: (v: T) => T;
|
|
121
|
+
constructor(public exec: ((sql: string, args?: any[]) => Promise<number>), public table: string, public attributes: Attributes, public param: (i: number) => string, size?: number, toDB?: (v: T) => T) {
|
|
122
|
+
this.write = this.write.bind(this);
|
|
123
|
+
this.flush = this.flush.bind(this);
|
|
124
|
+
this.map = toDB;
|
|
125
|
+
const x = version(attributes);
|
|
126
|
+
if (x) {
|
|
127
|
+
this.version = x.name;
|
|
128
|
+
}
|
|
129
|
+
if (size !== undefined && size > 0) {
|
|
130
|
+
this.size = size;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
write(obj: T): Promise<number> {
|
|
134
|
+
if (!obj) {
|
|
135
|
+
return Promise.resolve(0);
|
|
136
|
+
}
|
|
137
|
+
let obj2: NonNullable<T> | T = obj;
|
|
138
|
+
if (this.map) {
|
|
139
|
+
obj2 = this.map(obj);
|
|
140
|
+
this.list.push(obj2);
|
|
141
|
+
} else {
|
|
142
|
+
this.list.push(obj);
|
|
143
|
+
}
|
|
144
|
+
if (this.list.length < this.size) {
|
|
145
|
+
return Promise.resolve(0);
|
|
146
|
+
} else {
|
|
147
|
+
return this.flush();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
flush(): Promise<number> {
|
|
151
|
+
if (!this.list || this.list.length === 0) {
|
|
152
|
+
return Promise.resolve(0);
|
|
153
|
+
} else {
|
|
154
|
+
const total = this.list.length;
|
|
155
|
+
const stmt = buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version);
|
|
156
|
+
if (stmt) {
|
|
157
|
+
return this.exec(stmt.query, stmt.params).then(r => {
|
|
158
|
+
this.list = [];
|
|
159
|
+
return total;
|
|
160
|
+
});
|
|
161
|
+
} else {
|
|
162
|
+
return Promise.resolve(0);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
168
|
+
export class StreamUpdater<T> {
|
|
169
|
+
list: T[] = [];
|
|
170
|
+
size: number = 0;
|
|
171
|
+
version?: string;
|
|
172
|
+
map?: (v: T) => T;
|
|
173
|
+
constructor(public execBatch: ((statements: Statement[]) => Promise<number>), public table: string, public attributes: Attributes, public param: (i: number) => string, size?: number, toDB?: (v: T) => T) {
|
|
174
|
+
this.write = this.write.bind(this);
|
|
175
|
+
this.flush = this.flush.bind(this);
|
|
176
|
+
this.map = toDB;
|
|
177
|
+
const x = version(attributes);
|
|
178
|
+
if (x) {
|
|
179
|
+
this.version = x.name;
|
|
180
|
+
}
|
|
181
|
+
if (size !== undefined && size > 0) {
|
|
182
|
+
this.size = size;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
write(obj: T): Promise<number> {
|
|
186
|
+
if (!obj) {
|
|
187
|
+
return Promise.resolve(0);
|
|
188
|
+
}
|
|
189
|
+
let obj2: NonNullable<T> | T = obj;
|
|
190
|
+
if (this.map) {
|
|
191
|
+
obj2 = this.map(obj);
|
|
192
|
+
this.list.push(obj2);
|
|
193
|
+
} else {
|
|
194
|
+
this.list.push(obj);
|
|
195
|
+
}
|
|
196
|
+
if (this.list.length < this.size) {
|
|
197
|
+
return Promise.resolve(0);
|
|
198
|
+
} else {
|
|
199
|
+
return this.flush();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
flush(): Promise<number> {
|
|
203
|
+
if (!this.list || this.list.length === 0) {
|
|
204
|
+
return Promise.resolve(0);
|
|
205
|
+
} else {
|
|
206
|
+
const total = this.list.length;
|
|
207
|
+
const stmt = buildToUpdateBatch(this.list, this.table, this.attributes, this.param);
|
|
208
|
+
if (stmt) {
|
|
209
|
+
return this.execBatch(stmt).then(r => {
|
|
210
|
+
this.list = [];
|
|
211
|
+
return total;
|
|
212
|
+
});
|
|
213
|
+
} else {
|
|
214
|
+
return Promise.resolve(0);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|