I am using this code to save the yjs update into PostgreSQL:
const pgPut = async (
db: pg.Pool,
key: Map<string, string>,
val: Uint8Array,
source: string,
keys: any[]
) => {
try {
const query = `INSERT INTO tex_sync (key, value, plain_value, version, content_type, doc_name, clock, source)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) `;
const decoder = new TextDecoder("utf-8");
let text: string = decoder.decode(val);
let version = key.get("version") || "default";
let contentType = key.get("contentType") || "default";
let docName = key.get("docName") ? key.get("docName") : "default";
let clock = key.get("clock") ? key.get("clock") : -1;
// https://stackoverflow.com/questions/1347646/postgres-error-on-insert-error-invalid-byte-sequence-for-encoding-utf8-0x0
let replacedText = text
.replaceAll("", "")
.replaceAll("0x00", "")
.replaceAll(/\u0000/g, "");
const values = [
JSON.stringify(keys),
Buffer.from(val),
replacedText,
version,
contentType,
docName,
clock,
source,
];
const res: pg.QueryResult<any> = await db.query(query, values);
} catch (err: any) {
logger.error(
"Insert tex sync record error:" + JSON.stringify(keys) + ",val:" + val,
err.stack
);
}
};
I added uniq key constains for the key. Now I found the uniq key will conflict. Seems a massive update request arrived in the same time that query the postgresql get the same clock, is it ok to override the content when conflict happen?