@contember/engine-http 1.2.0-alpha.10 → 1.2.0-alpha.11
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/dist/src/transfer/ContentSchemaTransferMappingFactory.d.ts.map +1 -1
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.js +10 -8
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.js.map +1 -1
- package/dist/src/transfer/ExportExecutor.d.ts +4 -2
- package/dist/src/transfer/ExportExecutor.d.ts.map +1 -1
- package/dist/src/transfer/ExportExecutor.js +11 -6
- package/dist/src/transfer/ExportExecutor.js.map +1 -1
- package/dist/src/transfer/ImportApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/transfer/ImportApiMiddlewareFactory.js +18 -6
- package/dist/src/transfer/ImportApiMiddlewareFactory.js.map +1 -1
- package/dist/src/transfer/ImportExecutor.d.ts +1 -8
- package/dist/src/transfer/ImportExecutor.d.ts.map +1 -1
- package/dist/src/transfer/ImportExecutor.js +29 -52
- package/dist/src/transfer/ImportExecutor.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +10 -10
- package/src/transfer/ContentSchemaTransferMappingFactory.ts +9 -7
- package/src/transfer/ExportExecutor.ts +16 -9
- package/src/transfer/ImportApiMiddlewareFactory.ts +19 -9
- package/src/transfer/ImportExecutor.ts +31 -63
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.11",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@apollographql/graphql-playground-html": "^1.6.24",
|
|
12
|
-
"@contember/database": "^1.2.0-alpha.
|
|
13
|
-
"@contember/engine-content-api": "^1.2.0-alpha.
|
|
14
|
-
"@contember/engine-plugins": "^1.2.0-alpha.
|
|
15
|
-
"@contember/engine-system-api": "^1.2.0-alpha.
|
|
16
|
-
"@contember/engine-tenant-api": "^1.2.0-alpha.
|
|
17
|
-
"@contember/graphql-utils": "^1.2.0-alpha.
|
|
18
|
-
"@contember/schema": "^1.2.0-alpha.
|
|
19
|
-
"@contember/schema-utils": "^1.2.0-alpha.
|
|
20
|
-
"@contember/typesafe": "^1.2.0-alpha.
|
|
12
|
+
"@contember/database": "^1.2.0-alpha.11",
|
|
13
|
+
"@contember/engine-content-api": "^1.2.0-alpha.11",
|
|
14
|
+
"@contember/engine-plugins": "^1.2.0-alpha.11",
|
|
15
|
+
"@contember/engine-system-api": "^1.2.0-alpha.11",
|
|
16
|
+
"@contember/engine-tenant-api": "^1.2.0-alpha.11",
|
|
17
|
+
"@contember/graphql-utils": "^1.2.0-alpha.11",
|
|
18
|
+
"@contember/schema": "^1.2.0-alpha.11",
|
|
19
|
+
"@contember/schema-utils": "^1.2.0-alpha.11",
|
|
20
|
+
"@contember/typesafe": "^1.2.0-alpha.11",
|
|
21
21
|
"@graphql-tools/schema": "^8.3.5",
|
|
22
22
|
"@types/lru-cache": "^7.10.10",
|
|
23
23
|
"accepts": "^1.3.7",
|
|
@@ -8,9 +8,11 @@ export class ContentSchemaTransferMappingFactory {
|
|
|
8
8
|
const tables: Record<string, TransferTableMapping> = {}
|
|
9
9
|
|
|
10
10
|
for (const entity of Object.values(contentSchema.model.entities)) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
if (entity.view === undefined) {
|
|
12
|
+
tables[entity.tableName] = {
|
|
13
|
+
name: entity.tableName,
|
|
14
|
+
columns: this.buildDbColumnSchemaMap(contentSchema.model, entity),
|
|
15
|
+
}
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -28,7 +30,7 @@ export class ContentSchemaTransferMappingFactory {
|
|
|
28
30
|
|
|
29
31
|
for (const field of Object.values(entity.fields)) {
|
|
30
32
|
const column = acceptFieldVisitor<DbColumnSchema | null>(schema, entity, field, {
|
|
31
|
-
visitColumn: (
|
|
33
|
+
visitColumn: ({ column }) => {
|
|
32
34
|
if (column.type === Model.ColumnType.Enum) {
|
|
33
35
|
return {
|
|
34
36
|
name: column.columnName,
|
|
@@ -53,12 +55,12 @@ export class ContentSchemaTransferMappingFactory {
|
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
},
|
|
56
|
-
visitOneHasOneOwning: (
|
|
58
|
+
visitOneHasOneOwning: ({ relation, targetEntity }) => ({
|
|
57
59
|
name: relation.joiningColumn.columnName,
|
|
58
60
|
type: targetEntity.fields[targetEntity.primary].type as (Model.ColumnType.Uuid | Model.ColumnType.Int),
|
|
59
61
|
nullable: relation.nullable,
|
|
60
62
|
}),
|
|
61
|
-
visitManyHasOne: (
|
|
63
|
+
visitManyHasOne: ({ relation, targetEntity }) => ({
|
|
62
64
|
name: relation.joiningColumn.columnName,
|
|
63
65
|
type: targetEntity.fields[targetEntity.primary].type as (Model.ColumnType.Uuid | Model.ColumnType.Int),
|
|
64
66
|
nullable: relation.nullable,
|
|
@@ -85,7 +87,7 @@ export class ContentSchemaTransferMappingFactory {
|
|
|
85
87
|
visitColumn: () => null,
|
|
86
88
|
visitOneHasOneOwning: () => null,
|
|
87
89
|
visitManyHasOne: () => null,
|
|
88
|
-
visitManyHasManyOwning: (entity, relation, targetEntity) => ({
|
|
90
|
+
visitManyHasManyOwning: ({ entity, relation, targetEntity }) => ({
|
|
89
91
|
name: relation.joiningTable.tableName,
|
|
90
92
|
columns: {
|
|
91
93
|
[relation.joiningTable.joiningColumn.columnName]: {
|
|
@@ -11,10 +11,15 @@ import { ProjectContainer } from '../ProjectContainer'
|
|
|
11
11
|
export type ExportRequest = ReturnType<typeof ExportRequest>
|
|
12
12
|
export const ExportRequest = Typesafe.object({
|
|
13
13
|
// tenant: Typesafe.boolean,
|
|
14
|
-
projects: Typesafe.array(Typesafe.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
projects: Typesafe.array(Typesafe.intersection(
|
|
15
|
+
Typesafe.object({
|
|
16
|
+
slug: Typesafe.string,
|
|
17
|
+
system: Typesafe.boolean,
|
|
18
|
+
}),
|
|
19
|
+
Typesafe.partial({
|
|
20
|
+
targetSlug: Typesafe.string,
|
|
21
|
+
}),
|
|
22
|
+
)),
|
|
18
23
|
})
|
|
19
24
|
|
|
20
25
|
export class ExportExecutor {
|
|
@@ -31,7 +36,7 @@ export class ExportExecutor {
|
|
|
31
36
|
|
|
32
37
|
if (project.system) {
|
|
33
38
|
const systemMapping = this.systemSchemaTransferMappingFactory.build()
|
|
34
|
-
yield ['importSystemSchemaBegin', { project: project.slug, tables: Object.keys(systemMapping.tables) }]
|
|
39
|
+
yield ['importSystemSchemaBegin', { project: project.targetSlug ?? project.slug, tables: Object.keys(systemMapping.tables) }]
|
|
35
40
|
yield* this.exportSchema(systemContext.client, systemMapping)
|
|
36
41
|
}
|
|
37
42
|
|
|
@@ -42,7 +47,7 @@ export class ExportExecutor {
|
|
|
42
47
|
|
|
43
48
|
yield [
|
|
44
49
|
'importContentSchemaBegin', {
|
|
45
|
-
project: project.slug,
|
|
50
|
+
project: project.targetSlug ?? project.slug,
|
|
46
51
|
stage: stage.slug,
|
|
47
52
|
schemaVersion: contentSchema.version,
|
|
48
53
|
tables: Object.keys(contentMapping.tables),
|
|
@@ -69,12 +74,14 @@ export class ExportExecutor {
|
|
|
69
74
|
private async* exportSequences(db: Client<Connection.TransactionLike>, table: TransferTableMapping): AsyncIterable<Command> {
|
|
70
75
|
for (const column of Object.values(table.columns)) {
|
|
71
76
|
if (column.type === Model.ColumnType.Int && column.sequence) {
|
|
72
|
-
const
|
|
73
|
-
'SELECT
|
|
77
|
+
const seqNameResult = await db.query<{ name: string }>(
|
|
78
|
+
'SELECT pg_get_serial_sequence(?, ?) AS name',
|
|
74
79
|
[`${db.schema}.${table.name}`, column.name],
|
|
75
80
|
)
|
|
76
81
|
|
|
77
|
-
const
|
|
82
|
+
const seqNameQuoted = seqNameResult.rows[0].name.split('.').map(it => wrapIdentifier(it)).join('.')
|
|
83
|
+
const seqValueResult = await db.query<{ last_value: number }>(`SELECT last_value FROM ${seqNameQuoted}`)
|
|
84
|
+
const seqValue = seqValueResult.rows[0].last_value
|
|
78
85
|
yield ['importSequence', { table: table.name, column: column.name, value: seqValue }]
|
|
79
86
|
}
|
|
80
87
|
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { KoaMiddleware, KoaRequestState } from '../koa'
|
|
2
2
|
import { AuthResult, HttpError, TimerMiddlewareState } from '../common'
|
|
3
3
|
import { ProjectGroupResolver, ProjectInfoMiddlewareState } from '../project-common'
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { ImportExecutor } from './ImportExecutor'
|
|
4
|
+
import { fromBuffer } from './CommandStream'
|
|
5
|
+
import { ImportError, ImportExecutor } from './ImportExecutor'
|
|
7
6
|
import { createGunzip } from 'zlib'
|
|
8
|
-
import { Readable } from 'stream'
|
|
9
7
|
|
|
10
8
|
type ImportApiMiddlewareState =
|
|
11
9
|
& TimerMiddlewareState
|
|
@@ -38,12 +36,24 @@ export class ImportApiMiddlewareFactory {
|
|
|
38
36
|
throw new HttpError(`Unsupported content encoding`, 415)
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
|
|
42
|
-
const commands = fromBuffer(isGzip ? request.req.pipe(createGunzip()) : request.req)
|
|
39
|
+
response.headers['Content-Type'] = 'application/json'
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
try {
|
|
42
|
+
const isGzip = request.headers['content-encoding'] === 'gzip'
|
|
43
|
+
const commands = fromBuffer(isGzip ? request.req.pipe(createGunzip()) : request.req)
|
|
44
|
+
await this.importExecutor.import(groupContainer, authResult, commands)
|
|
45
|
+
response.status = 200
|
|
46
|
+
response.body = { ok: true }
|
|
47
|
+
|
|
48
|
+
} catch (e) {
|
|
49
|
+
if (e instanceof ImportError) {
|
|
50
|
+
response.status = 400
|
|
51
|
+
response.body = { ok: false, error: e.message }
|
|
52
|
+
|
|
53
|
+
} else {
|
|
54
|
+
throw e
|
|
55
|
+
}
|
|
56
|
+
}
|
|
47
57
|
}
|
|
48
58
|
}
|
|
49
59
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Client, Connection, ConstraintHelper, wrapIdentifier } from '@contember/database'
|
|
2
2
|
import { Model, Schema } from '@contember/schema'
|
|
3
3
|
import * as Typesafe from '@contember/typesafe'
|
|
4
4
|
import { ParseError } from '@contember/typesafe'
|
|
@@ -26,7 +26,7 @@ type InsertContext = {
|
|
|
26
26
|
export class ImportError extends Error {
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
type CommandProcessor<T extends CommandName> = (it: CommandIterator<T>) =>
|
|
29
|
+
type CommandProcessor<T extends CommandName> = (it: CommandIterator<T>) => Promise<CommandIterator | null>
|
|
30
30
|
type CommandProcessorMap<T extends CommandName> = { [N in CommandName]?: N extends T ? CommandProcessor<N> : undefined }
|
|
31
31
|
|
|
32
32
|
class CommandIterator<T extends CommandName = CommandName> {
|
|
@@ -61,12 +61,6 @@ class CommandIterator<T extends CommandName = CommandName> {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
export type ImportProgress =
|
|
65
|
-
| [kind: 'error', message: string]
|
|
66
|
-
| [kind: 'insertRow', details: { table: string; insertedRowCount: number }]
|
|
67
|
-
| [kind: 'insertEnd', details: { table: string; insertedRowCount: number }]
|
|
68
|
-
|
|
69
|
-
|
|
70
64
|
export class ImportExecutor {
|
|
71
65
|
constructor(
|
|
72
66
|
private readonly contentSchemaTransferMappingFactory: ContentSchemaTransferMappingFactory,
|
|
@@ -75,37 +69,27 @@ export class ImportExecutor {
|
|
|
75
69
|
) {
|
|
76
70
|
}
|
|
77
71
|
|
|
78
|
-
async
|
|
79
|
-
|
|
80
|
-
const it = await CommandIterator.create(commands)
|
|
81
|
-
|
|
82
|
-
const final = yield* this.match(it, {
|
|
83
|
-
importContentSchemaBegin: it => this.importContentSchemaBegin(groupContainer, authResult, it),
|
|
84
|
-
importSystemSchemaBegin: it => this.importSystemSchemaBegin(groupContainer, authResult, it),
|
|
85
|
-
})
|
|
72
|
+
async import(groupContainer: ProjectGroupContainer, authResult: AuthResult, commands: AsyncIterable<Command>) {
|
|
73
|
+
const it = await CommandIterator.create(commands)
|
|
86
74
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
} catch (e) {
|
|
92
|
-
if (e instanceof ImportError) {
|
|
93
|
-
yield ['error', e.message]
|
|
75
|
+
const final = await this.match(it, {
|
|
76
|
+
importContentSchemaBegin: it => this.importContentSchemaBegin(groupContainer, authResult, it),
|
|
77
|
+
importSystemSchemaBegin: it => this.importSystemSchemaBegin(groupContainer, authResult, it),
|
|
78
|
+
})
|
|
94
79
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
80
|
+
if (final !== null) {
|
|
81
|
+
throw new ImportError(`Unexpected ${final.commandName}`)
|
|
98
82
|
}
|
|
99
83
|
}
|
|
100
84
|
|
|
101
|
-
private async
|
|
85
|
+
private async match<T extends CommandName>(it: CommandIterator<T> | null, processors: CommandProcessorMap<T>) {
|
|
102
86
|
let next: CommandIterator | null = it
|
|
103
87
|
|
|
104
88
|
while (next !== null) {
|
|
105
89
|
const processor = processors[next.commandName]
|
|
106
90
|
|
|
107
91
|
if (processor) {
|
|
108
|
-
next =
|
|
92
|
+
next = await processor(next as any)
|
|
109
93
|
|
|
110
94
|
} else {
|
|
111
95
|
break
|
|
@@ -115,7 +99,7 @@ export class ImportExecutor {
|
|
|
115
99
|
return next
|
|
116
100
|
}
|
|
117
101
|
|
|
118
|
-
private async
|
|
102
|
+
private async importContentSchemaBegin(groupContainer: ProjectGroupContainer, authResult: AuthResult, it: CommandIterator<'importContentSchemaBegin'>) {
|
|
119
103
|
const [options] = it.commandArgs
|
|
120
104
|
const projectContainer = await this.createProjectContainer(groupContainer, options.project)
|
|
121
105
|
const systemDatabaseContext = projectContainer.systemDatabaseContextFactory.create()
|
|
@@ -131,29 +115,27 @@ export class ImportExecutor {
|
|
|
131
115
|
const contentDatabaseClient = projectContainer.connection.createClient(stage.schema, {})
|
|
132
116
|
const mapping = this.contentSchemaTransferMappingFactory.createContentSchemaMapping(contentSchema)
|
|
133
117
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
await
|
|
138
|
-
await that.lockTables(db, options.tables)
|
|
139
|
-
await that.truncateTables(db, options.tables)
|
|
118
|
+
return await contentDatabaseClient.transaction(async db => {
|
|
119
|
+
await this.disableTriggers(db, options.tables)
|
|
120
|
+
await this.lockTables(db, options.tables)
|
|
121
|
+
await this.truncateTables(db, options.tables)
|
|
140
122
|
|
|
141
123
|
const constraintHelper = new ConstraintHelper(db)
|
|
142
124
|
await constraintHelper.setFkConstraintsDeferred()
|
|
143
125
|
|
|
144
|
-
const result =
|
|
145
|
-
importSequence: it =>
|
|
146
|
-
insertBegin: it =>
|
|
126
|
+
const result = await this.match(await it.next(), {
|
|
127
|
+
importSequence: it => this.importSequence(db, mapping, it),
|
|
128
|
+
insertBegin: it => this.insertBegin(db, mapping, it),
|
|
147
129
|
})
|
|
148
130
|
|
|
149
131
|
await constraintHelper.setFkConstraintsImmediate()
|
|
150
|
-
await
|
|
132
|
+
await this.enableTriggers(db, options.tables)
|
|
151
133
|
|
|
152
134
|
return result
|
|
153
135
|
})
|
|
154
136
|
}
|
|
155
137
|
|
|
156
|
-
private async
|
|
138
|
+
private async importSystemSchemaBegin(groupContainer: ProjectGroupContainer, authResult: AuthResult, it: CommandIterator<'importSystemSchemaBegin'>) {
|
|
157
139
|
const [options] = it.commandArgs
|
|
158
140
|
const projectContainer = await this.createProjectContainer(groupContainer, options.project)
|
|
159
141
|
const schema = await groupContainer.projectSchemaResolver.getSchema(options.project)
|
|
@@ -166,21 +148,19 @@ export class ImportExecutor {
|
|
|
166
148
|
const systemDatabaseContext = projectContainer.systemDatabaseContextFactory.create()
|
|
167
149
|
const mapping = this.systemSchemaTransferMappingFactory.build()
|
|
168
150
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return yield* asyncIterableTransaction(systemDatabaseContext.client, async function* (db) {
|
|
172
|
-
await that.truncateTables(db, options.tables)
|
|
151
|
+
return await systemDatabaseContext.client.transaction(async db => {
|
|
152
|
+
await this.truncateTables(db, options.tables)
|
|
173
153
|
|
|
174
154
|
const constraintHelper = new ConstraintHelper(db)
|
|
175
155
|
await constraintHelper.setFkConstraintsDeferred()
|
|
176
156
|
|
|
177
|
-
return
|
|
178
|
-
insertBegin: it =>
|
|
157
|
+
return await this.match(await it.next(), {
|
|
158
|
+
insertBegin: it => this.insertBegin(db, mapping, it),
|
|
179
159
|
})
|
|
180
160
|
})
|
|
181
161
|
}
|
|
182
162
|
|
|
183
|
-
private async
|
|
163
|
+
private async importSequence(db: Client<Connection.TransactionLike>, mapping: TransferMapping, it: CommandIterator<'importSequence'>): Promise<CommandIterator | null> {
|
|
184
164
|
const [options] = it.commandArgs
|
|
185
165
|
|
|
186
166
|
await db.query(
|
|
@@ -191,16 +171,13 @@ export class ImportExecutor {
|
|
|
191
171
|
return it.next()
|
|
192
172
|
}
|
|
193
173
|
|
|
194
|
-
private async
|
|
174
|
+
private async insertBegin(db: Client<Connection.TransactionLike>, mapping: TransferMapping, it: CommandIterator<'insertBegin'>): Promise<CommandIterator | null> {
|
|
195
175
|
const [options] = it.commandArgs
|
|
196
176
|
const insertContext = await this.buildInsertContext(db, mapping, db.schema, options.table, options.columns)
|
|
197
177
|
let insertRowsPromise: Promise<Connection.Result> | null = null
|
|
198
|
-
let lastEmitAt = process.hrtime.bigint()
|
|
199
178
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
const result = yield* this.match(await it.next(), {
|
|
203
|
-
insertRow: async function* (it) {
|
|
179
|
+
const result = await this.match(await it.next(), {
|
|
180
|
+
insertRow: async it => {
|
|
204
181
|
const [values] = it.commandArgs
|
|
205
182
|
|
|
206
183
|
try {
|
|
@@ -217,16 +194,9 @@ export class ImportExecutor {
|
|
|
217
194
|
|
|
218
195
|
if (insertContext.rows.length === 1000) {
|
|
219
196
|
await insertRowsPromise
|
|
220
|
-
insertRowsPromise =
|
|
197
|
+
insertRowsPromise = this.insertRows(db, insertContext)
|
|
221
198
|
insertContext.insertedRowCount += insertContext.rows.length
|
|
222
199
|
insertContext.rows = []
|
|
223
|
-
|
|
224
|
-
const now = process.hrtime.bigint()
|
|
225
|
-
|
|
226
|
-
if (now - lastEmitAt > 1e9) {
|
|
227
|
-
yield ['insertRow', { table: insertContext.tableName, insertedRowCount: insertContext.insertedRowCount }]
|
|
228
|
-
lastEmitAt = now
|
|
229
|
-
}
|
|
230
200
|
}
|
|
231
201
|
|
|
232
202
|
return await it.next()
|
|
@@ -238,8 +208,6 @@ export class ImportExecutor {
|
|
|
238
208
|
await this.insertRows(db, insertContext)
|
|
239
209
|
insertContext.insertedRowCount += insertContext.rows.length
|
|
240
210
|
insertContext.rows = []
|
|
241
|
-
|
|
242
|
-
yield ['insertEnd', { table: insertContext.tableName, insertedRowCount: insertContext.insertedRowCount }]
|
|
243
211
|
}
|
|
244
212
|
|
|
245
213
|
if (result === null || result.commandName !== 'insertEnd') {
|