@langchain/langgraph-checkpoint-postgres
Version:
88 lines • 3.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.tableExistsSQL = exports.getSQLStatements = exports.getTablesWithSchema = void 0;
const langgraph_checkpoint_1 = require("@langchain/langgraph-checkpoint");
const getTablesWithSchema = (schema) => {
const tables = [
"checkpoints",
"checkpoint_blobs",
"checkpoint_migrations",
"checkpoint_writes",
];
return tables.reduce((acc, table) => {
acc[table] = `${schema}.${table}`;
return acc;
}, {});
};
exports.getTablesWithSchema = getTablesWithSchema;
const getSQLStatements = (schema) => {
const SCHEMA_TABLES = (0, exports.getTablesWithSchema)(schema);
return {
SELECT_SQL: `select
thread_id,
checkpoint,
checkpoint_ns,
checkpoint_id,
parent_checkpoint_id,
metadata,
(
select array_agg(array[bl.channel::bytea, bl.type::bytea, bl.blob])
from jsonb_each_text(checkpoint -> 'channel_versions')
inner join ${SCHEMA_TABLES.checkpoint_blobs} bl
on bl.thread_id = cp.thread_id
and bl.checkpoint_ns = cp.checkpoint_ns
and bl.channel = jsonb_each_text.key
and bl.version = jsonb_each_text.value
) as channel_values,
(
select
array_agg(array[cw.task_id::text::bytea, cw.channel::bytea, cw.type::bytea, cw.blob] order by cw.task_id, cw.idx)
from ${SCHEMA_TABLES.checkpoint_writes} cw
where cw.thread_id = cp.thread_id
and cw.checkpoint_ns = cp.checkpoint_ns
and cw.checkpoint_id = cp.checkpoint_id
) as pending_writes,
(
select array_agg(array[cw.type::bytea, cw.blob] order by cw.idx)
from ${SCHEMA_TABLES.checkpoint_writes} cw
where cw.thread_id = cp.thread_id
and cw.checkpoint_ns = cp.checkpoint_ns
and cw.checkpoint_id = cp.parent_checkpoint_id
and cw.channel = '${langgraph_checkpoint_1.TASKS}'
) as pending_sends
from ${SCHEMA_TABLES.checkpoints} cp `,
UPSERT_CHECKPOINT_BLOBS_SQL: `INSERT INTO ${SCHEMA_TABLES.checkpoint_blobs} (thread_id, checkpoint_ns, channel, version, type, blob)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (thread_id, checkpoint_ns, channel, version) DO NOTHING
`,
UPSERT_CHECKPOINTS_SQL: `INSERT INTO ${SCHEMA_TABLES.checkpoints} (thread_id, checkpoint_ns, checkpoint_id, parent_checkpoint_id, checkpoint, metadata)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (thread_id, checkpoint_ns, checkpoint_id)
DO UPDATE SET
checkpoint = EXCLUDED.checkpoint,
metadata = EXCLUDED.metadata;
`,
UPSERT_CHECKPOINT_WRITES_SQL: `INSERT INTO ${SCHEMA_TABLES.checkpoint_writes} (thread_id, checkpoint_ns, checkpoint_id, task_id, idx, channel, type, blob)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (thread_id, checkpoint_ns, checkpoint_id, task_id, idx) DO UPDATE SET
channel = EXCLUDED.channel,
type = EXCLUDED.type,
blob = EXCLUDED.blob;
`,
INSERT_CHECKPOINT_WRITES_SQL: `INSERT INTO ${SCHEMA_TABLES.checkpoint_writes} (thread_id, checkpoint_ns, checkpoint_id, task_id, idx, channel, type, blob)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (thread_id, checkpoint_ns, checkpoint_id, task_id, idx) DO NOTHING
`,
};
};
exports.getSQLStatements = getSQLStatements;
const tableExistsSQL = (schema, table) => {
const tableWithoutSchema = table.split(".")[1];
return `SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = '${schema}'
AND table_name = '${tableWithoutSchema}'
);`;
};
exports.tableExistsSQL = tableExistsSQL;
//# sourceMappingURL=sql.js.map
;