export async function importContacts(db: Kysely<Tables>, contacts: Contact[]) {
// Let rejection escape the callback so Kysely rolls back the whole batch.
await db.transaction().execute(async transaction => {
for (const contact of contacts) {
await transaction.insertInto('contact').values(contact).execute()
}
})
}
Authored implementation, informed by the retrieved declarations.
Read the captured fixture ↗
↳
HOW THE AGENT USES IT
Every insert goes through transaction.
The promise rejects on a duplicate name. The error is not swallowed, so the
transaction can roll back.
That is the implementation decision. The assertions below are the execution
evidence.
Before validation: workspace_context resolved Kysely 0.28.8 in the installed project. Artifact match: unknown.
Inspect the context request and evidence
{
"name": "workspace_context",
"arguments": {
"config": "$WORKSPACE/tsconfig.json",
"file": "$WORKSPACE/batch.ts",
"importSpecifier": "kysely",
"symbol": "Kysely",
"expectedVersion": "0.28.8"
}
}
{
"retrieved": {
"status": "not-run",
"reason": "No discovery question supplied"
},
"resolved": {
"status": "pass",
"reason": "Import resolved by the project compiler"
},
"versionMatch": {
"status": "pass",
"reason": "Resolved kysely@0.28.8; expected 0.28.8"
},
"artifactMatch": {
"status": "unknown",
"reason": "Installed package contents have not been compared with the registry artifact"
},
"typechecked": {
"status": "not-run",
"reason": "Use workspace validation for project diagnostics"
},
"tested": {
"status": "not-run",
"reason": "No test command executed"
}
}
Full request and response ↗
workspace_validateACTUAL REQUEST
{
"config": "$WORKSPACE/tsconfig.json",
"workspaceRoot": "$WORKSPACE",
"timeoutMs": 60000,
"record": true,
"queryId": "9bfdae25-f3f2-4110-8b5f-c39d045bdafd",
"testCommand": [
"npm",
"test"
]
}
$WORKSPACE replaces the private absolute fixture path. Tests run only
because testCommand was explicitly supplied.
RETURNED EVIDENCE
- TypeScript compilation
- pass
- Explicit test command
- pass
- Recorded inputs unchanged
- pass
Inspect the validation result ↗
For this fixture and these assertions. Not a general correctness guarantee.
What actually happened
commits the whole valid batch and binds a quote as data pass
Executed assertion source from batch.test.ts:
test('commits the whole valid batch and binds a quote as data', async () => {
const db = await createDatabase()
try {
const contacts = [{ id: 1, name: "O'Reilly" }, { id: 2, name: "Robert'); DROP TABLE contact;--" }]
const compiled = db.insertInto('contact').values(contacts[0]!).compile()
assert.equal(compiled.sql, 'insert into "contact" ("id", "name") values (?, ?)')
assert.deepEqual(compiled.parameters, [1, "O'Reilly"])
await importContacts(db, contacts)
assert.deepEqual(await db.selectFrom('contact').selectAll().orderBy('id').execute(), contacts)
console.log(JSON.stringify({ assertion: 'parameter binding and exact round trip', sql: compiled.sql, parameters: compiled.parameters, rows: contacts }))
} finally {
await db.destroy()
}
})
Exact recorded stdout excerpt:
# {"assertion":"parameter binding and exact round trip","sql":"insert into \\"contact\\" (\\"id\\", \\"name\\") values (?, ?)","parameters":[1,"O'Reilly"],"rows":[{"id":1,"name":"O'Reilly"},{"id":2,"name":"Robert'); DROP TABLE contact;--"}]}
ok 1 - commits the whole valid batch and binds a quote as data
Complete validation request and response ↗UNIQUE failure rolls back an earlier insert in the same batch pass
Executed assertion source from batch.test.ts:
test('UNIQUE failure rolls back an earlier insert in the same batch', async () => {
const db = await createDatabase()
try {
await importContacts(db, [{ id: 1, name: 'existing' }])
await assert.rejects(importContacts(db, [{ id: 2, name: 'must roll back' }, { id: 3, name: 'existing' }]), { code: 'SQLITE_CONSTRAINT_UNIQUE' })
const rows = await db.selectFrom('contact').selectAll().execute()
assert.deepEqual(rows, [{ id: 1, name: 'existing' }])
console.log(JSON.stringify({ assertion: 'whole batch rollback', rejectedCode: 'SQLITE_CONSTRAINT_UNIQUE', rows }))
} finally {
await db.destroy()
}
})
Exact recorded stdout excerpt:
# {"assertion":"whole batch rollback","rejectedCode":"SQLITE_CONSTRAINT_UNIQUE","rows":[{"id":1,"name":"existing"}]}
ok 2 - UNIQUE failure rolls back an earlier insert in the same batch
Complete validation request and response ↗Exact test names from the recorded output. Open either assertion to inspect its source and result. Exit code 0.
Inspect complete execution output
> test
> node --experimental-strip-types --test --test-reporter=tap batch.test.ts
TAP version 13
# {"assertion":"parameter binding and exact round trip","sql":"insert into \\"contact\\" (\\"id\\", \\"name\\") values (?, ?)","parameters":[1,"O'Reilly"],"rows":[{"id":1,"name":"O'Reilly"},{"id":2,"name":"Robert'); DROP TABLE contact;--"}]}
# {"assertion":"whole batch rollback","rejectedCode":"SQLITE_CONSTRAINT_UNIQUE","rows":[{"id":1,"name":"existing"}]}
# Subtest: commits the whole valid batch and binds a quote as data
ok 1 - commits the whole valid batch and binds a quote as data
---
duration_ms: 252.026792
type: 'test'
...
# Subtest: UNIQUE failure rolls back an earlier insert in the same batch
ok 2 - UNIQUE failure rolls back an earlier insert in the same batch
---
duration_ms: 1.616292
type: 'test'
...
1..2
# tests 2
# suites 0
# pass 2
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 747.708542