diff --git a/packages/code-gen/src/generator/sql/query-basics.js b/packages/code-gen/src/generator/sql/query-basics.js index c2bdd38787..579da0dbeb 100644 --- a/packages/code-gen/src/generator/sql/query-basics.js +++ b/packages/code-gen/src/generator/sql/query-basics.js @@ -218,17 +218,19 @@ function insertQuery(context, imports, type) { /** * @param {Postgres} sql * @param {${type.partial.insertType}|(${type.partial.insertType}[])} insert + * @param {{ withPrimaryKey: boolean }=} options * @returns {Promise<${type.uniqueName}[]>} */ - export function ${type.name}Insert(sql, insert) { + export function ${type.name}Insert(sql, insert, options = {}) { if (insert === undefined || insert.length === 0) { return []; } + options.withPrimaryKey = options.withPrimaryKey ?? false; return query\` INSERT INTO "${type.name}" ($\{${type.name}Fields( - "", { excludePrimaryKey: true })}) - VALUES $\{${type.name}InsertValues(insert)} + "", { excludePrimaryKey: !options.withPrimaryKey })}) + VALUES $\{${type.name}InsertValues(insert, { includePrimaryKey: options.withPrimaryKey })} RETURNING $\{${type.name}Fields("")} \`.exec(sql); } diff --git a/packages/code-gen/test/sql.test.js b/packages/code-gen/test/sql.test.js index 76da250e4e..33fbd70b09 100644 --- a/packages/code-gen/test/sql.test.js +++ b/packages/code-gen/test/sql.test.js @@ -194,6 +194,17 @@ test("code-gen/e2e/sql", async (t) => { } }); + t.test("Insert with primary key", async (t) => { + const id = uuid(); + const [category] = await client.queries.categoryInsert( + sql, + { id, label: "TestPK" }, + { withPrimaryKey: true }, + ); + + t.equal(category.id, id); + }); + t.test("deletedAt in the future", async (t) => { const future = new Date(); future.setUTCDate(future.getUTCDate() + 1); diff --git a/packages/store/src/generated/query-basics.js b/packages/store/src/generated/query-basics.js index 98a30721c8..5f99dc3b9e 100644 --- a/packages/store/src/generated/query-basics.js +++ b/packages/store/src/generated/query-basics.js @@ -68,15 +68,21 @@ WHERE ${fileWhere(where)} /** * @param {Postgres} sql * @param {StoreFileInsertPartial|(StoreFileInsertPartial[])} insert + * @param {{ withPrimaryKey: boolean }=} options * @returns {Promise} */ -export function fileInsert(sql, insert) { +export function fileInsert(sql, insert, options = {}) { if (insert === undefined || insert.length === 0) { return []; } + options.withPrimaryKey = options.withPrimaryKey ?? false; return query` -INSERT INTO "file" (${fileFields("", { excludePrimaryKey: true })}) -VALUES ${fileInsertValues(insert)} +INSERT INTO "file" (${fileFields("", { + excludePrimaryKey: !options.withPrimaryKey, + })}) +VALUES ${fileInsertValues(insert, { + includePrimaryKey: options.withPrimaryKey, + })} RETURNING ${fileFields("")} `.exec(sql); } @@ -154,15 +160,21 @@ WHERE ${fileGroupWhere(where)} /** * @param {Postgres} sql * @param {StoreFileGroupInsertPartial|(StoreFileGroupInsertPartial[])} insert + * @param {{ withPrimaryKey: boolean }=} options * @returns {Promise} */ -export function fileGroupInsert(sql, insert) { +export function fileGroupInsert(sql, insert, options = {}) { if (insert === undefined || insert.length === 0) { return []; } + options.withPrimaryKey = options.withPrimaryKey ?? false; return query` -INSERT INTO "fileGroup" (${fileGroupFields("", { excludePrimaryKey: true })}) -VALUES ${fileGroupInsertValues(insert)} +INSERT INTO "fileGroup" (${fileGroupFields("", { + excludePrimaryKey: !options.withPrimaryKey, + })}) +VALUES ${fileGroupInsertValues(insert, { + includePrimaryKey: options.withPrimaryKey, + })} RETURNING ${fileGroupFields("")} `.exec(sql); } @@ -279,15 +291,19 @@ WHERE ${jobWhere(where)} /** * @param {Postgres} sql * @param {StoreJobInsertPartial|(StoreJobInsertPartial[])} insert + * @param {{ withPrimaryKey: boolean }=} options * @returns {Promise} */ -export function jobInsert(sql, insert) { +export function jobInsert(sql, insert, options = {}) { if (insert === undefined || insert.length === 0) { return []; } + options.withPrimaryKey = options.withPrimaryKey ?? false; return query` -INSERT INTO "job" (${jobFields("", { excludePrimaryKey: true })}) -VALUES ${jobInsertValues(insert)} +INSERT INTO "job" (${jobFields("", { + excludePrimaryKey: !options.withPrimaryKey, + })}) +VALUES ${jobInsertValues(insert, { includePrimaryKey: options.withPrimaryKey })} RETURNING ${jobFields("")} `.exec(sql); } @@ -345,15 +361,21 @@ WHERE ${sessionWhere(where)} /** * @param {Postgres} sql * @param {StoreSessionInsertPartial|(StoreSessionInsertPartial[])} insert + * @param {{ withPrimaryKey: boolean }=} options * @returns {Promise} */ -export function sessionInsert(sql, insert) { +export function sessionInsert(sql, insert, options = {}) { if (insert === undefined || insert.length === 0) { return []; } + options.withPrimaryKey = options.withPrimaryKey ?? false; return query` -INSERT INTO "session" (${sessionFields("", { excludePrimaryKey: true })}) -VALUES ${sessionInsertValues(insert)} +INSERT INTO "session" (${sessionFields("", { + excludePrimaryKey: !options.withPrimaryKey, + })}) +VALUES ${sessionInsertValues(insert, { + includePrimaryKey: options.withPrimaryKey, + })} RETURNING ${sessionFields("")} `.exec(sql); }