prisma-field-encryption
Advanced tools
Comparing version 1.2.0-beta.1 to 1.2.0-beta.2
@@ -46,11 +46,36 @@ "use strict"; | ||
export type MigrationReport = { | ||
${Object.keys(models) | ||
.map(modelName => ` ${modelName}: number`) | ||
.join(',\n')} | ||
} | ||
/** | ||
* Migrate models concurrently. | ||
* | ||
* Processed models: | ||
${Object.keys(models) | ||
.map(modelName => ` * - ${modelName}`) | ||
.join('\n')} | ||
* | ||
* @returns a dictionary of the number of processed records per model. | ||
*/ | ||
export async function migrate( | ||
client: PrismaClient, | ||
reportProgress: ProgressReportCallback = defaultProgressReport | ||
) { | ||
await Promise.all([ | ||
): Promise<MigrationReport> { | ||
const [ | ||
${Object.keys(models) | ||
.map(modelName => ` processed${modelName}`) | ||
.join(',\n')} | ||
] = await Promise.all([ | ||
${Object.keys(models) | ||
.map(modelName => ` migrate${modelName}(client, reportProgress)`) | ||
.join(',\n')} | ||
]) | ||
return { | ||
${Object.keys(models) | ||
.map(modelName => ` ${modelName}: processed${modelName}`) | ||
.join(',\n')} | ||
} | ||
} | ||
@@ -57,0 +82,0 @@ `; |
@@ -15,5 +15,9 @@ "use strict"; | ||
import type { PrismaClient, ${modelName} } from '${prismaClientModule}' | ||
import { ProgressReportCallback, defaultProgressReport } from './index' | ||
import { | ||
ProgressReportCallback, | ||
defaultProgressReport, | ||
visitRecords | ||
} from 'prisma-field-encryption/dist/generator/runtime' | ||
type Cursor = ${modelName}['${model.cursor}'] | undefined | ||
type Cursor = ${modelName}['${model.cursor}'] | ||
@@ -23,28 +27,13 @@ export async function migrate( | ||
reportProgress: ProgressReportCallback = defaultProgressReport | ||
) { | ||
const totalCount = await client.${interfaceName}.count() | ||
if (totalCount === 0) { | ||
return | ||
} | ||
let cursor: Cursor = undefined | ||
let processed = 0 | ||
while (true) { | ||
const tick = performance.now() | ||
const newCursor: Cursor = await migrateRecord(client, cursor) | ||
if (newCursor === cursor) { | ||
break // Reached the end | ||
} | ||
cursor = newCursor | ||
processed++ | ||
const tock = performance.now() | ||
reportProgress({ | ||
model: '${modelName}', | ||
processed, | ||
totalCount, | ||
performance: tock - tick | ||
}) | ||
} | ||
): Promise<number> { | ||
return visitRecords<Cursor>({ | ||
modelName: '${modelName}', | ||
client, | ||
getTotalCount: client.${interfaceName}.count, | ||
migrateRecord, | ||
reportProgress, | ||
}) | ||
} | ||
async function migrateRecord(client: PrismaClient, cursor: Cursor) { | ||
async function migrateRecord(client: PrismaClient, cursor: Cursor | undefined) { | ||
return await client.$transaction(async tx => { | ||
@@ -51,0 +40,0 @@ const record = await tx.${interfaceName}.findFirst({ |
@@ -33,3 +33,3 @@ #!/usr/bin/env node | ||
try { | ||
promises_1.default.mkdir(outputDir, { recursive: true }); | ||
await promises_1.default.mkdir(outputDir, { recursive: true }); | ||
} | ||
@@ -36,0 +36,0 @@ catch (_d) { } |
{ | ||
"name": "prisma-field-encryption", | ||
"version": "1.2.0-beta.1", | ||
"version": "1.2.0-beta.2", | ||
"description": "Transparent field-level encryption at rest for Prisma", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
43133
30
746