@payloadcms/drizzle 3.0.0-beta.106 → 3.0.0-beta.108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/count.js +1 -1
- package/dist/count.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +4 -2
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +9 -7
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +5 -3
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.js +1 -1
- package/dist/deleteVersions.js.map +1 -1
- package/dist/find/buildFindManyArgs.d.ts +9 -3
- package/dist/find/buildFindManyArgs.d.ts.map +1 -1
- package/dist/find/buildFindManyArgs.js +4 -1
- package/dist/find/buildFindManyArgs.js.map +1 -1
- package/dist/find/findMany.d.ts +1 -1
- package/dist/find/findMany.d.ts.map +1 -1
- package/dist/find/findMany.js +6 -3
- package/dist/find/findMany.js.map +1 -1
- package/dist/find/traverseFields.d.ts +6 -3
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +69 -2
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +2 -1
- package/dist/find.js.map +1 -1
- package/dist/findGlobalVersions.js +1 -1
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +2 -1
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.js +1 -1
- package/dist/findVersions.js.map +1 -1
- package/dist/postgres/countDistinct.d.ts.map +1 -1
- package/dist/postgres/countDistinct.js +3 -3
- package/dist/postgres/countDistinct.js.map +1 -1
- package/dist/postgres/init.js +2 -2
- package/dist/postgres/init.js.map +1 -1
- package/dist/postgres/schema/build.js +4 -3
- package/dist/postgres/schema/build.js.map +1 -1
- package/dist/postgres/schema/traverseFields.d.ts.map +1 -1
- package/dist/postgres/schema/traverseFields.js +23 -2
- package/dist/postgres/schema/traverseFields.js.map +1 -1
- package/dist/postgres/types.d.ts +1 -0
- package/dist/postgres/types.d.ts.map +1 -1
- package/dist/postgres/types.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +1 -1
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +2 -2
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildOrderBy.d.ts +18 -0
- package/dist/queries/buildOrderBy.d.ts.map +1 -0
- package/dist/queries/buildOrderBy.js +51 -0
- package/dist/queries/buildOrderBy.js.map +1 -0
- package/dist/queries/buildQuery.d.ts +4 -4
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +12 -47
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +6 -2
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +45 -11
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/parseParams.d.ts +1 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +30 -4
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +11 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +54 -14
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queryDrafts.js +1 -1
- package/dist/queryDrafts.js.map +1 -1
- package/dist/transform/read/index.d.ts +3 -2
- package/dist/transform/read/index.d.ts.map +1 -1
- package/dist/transform/read/index.js +2 -1
- package/dist/transform/read/index.js.map +1 -1
- package/dist/transform/read/traverseFields.d.ts +6 -2
- package/dist/transform/read/traverseFields.d.ts.map +1 -1
- package/dist/transform/read/traverseFields.js +39 -2
- package/dist/transform/read/traverseFields.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +7 -1
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/update.d.ts.map +1 -1
- package/dist/update.js +3 -2
- package/dist/update.js.map +1 -1
- package/dist/updateGlobalVersion.js +2 -2
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateVersion.js +2 -2
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertRow/index.d.ts +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +4 -2
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/insertArrays.d.ts.map +1 -1
- package/dist/upsertRow/insertArrays.js +4 -1
- package/dist/upsertRow/insertArrays.js.map +1 -1
- package/dist/upsertRow/types.d.ts +4 -1
- package/dist/upsertRow/types.d.ts.map +1 -1
- package/dist/upsertRow/types.js.map +1 -1
- package/dist/utilities/getCollectionIdType.d.ts +7 -0
- package/dist/utilities/getCollectionIdType.d.ts.map +1 -0
- package/dist/utilities/getCollectionIdType.js +11 -0
- package/dist/utilities/getCollectionIdType.js.map +1 -0
- package/dist/utilities/isPolymorphicRelationship.d.ts +6 -0
- package/dist/utilities/isPolymorphicRelationship.d.ts.map +1 -0
- package/dist/utilities/isPolymorphicRelationship.js +5 -0
- package/dist/utilities/isPolymorphicRelationship.js.map +1 -0
- package/package.json +3 -3
package/dist/update.js
CHANGED
|
@@ -2,7 +2,7 @@ import toSnakeCase from 'to-snake-case';
|
|
|
2
2
|
import buildQuery from './queries/buildQuery.js';
|
|
3
3
|
import { selectDistinct } from './queries/selectDistinct.js';
|
|
4
4
|
import { upsertRow } from './upsertRow/index.js';
|
|
5
|
-
export const updateOne = async function updateOne({ id, collection: collectionSlug, data, draft, locale, req, where: whereArg }) {
|
|
5
|
+
export const updateOne = async function updateOne({ id, collection: collectionSlug, data, draft, joins: joinQuery, locale, req, where: whereArg }) {
|
|
6
6
|
const db = this.sessions[await req?.transactionID]?.db || this.drizzle;
|
|
7
7
|
const collection = this.payload.collections[collectionSlug].config;
|
|
8
8
|
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug));
|
|
@@ -12,7 +12,7 @@ export const updateOne = async function updateOne({ id, collection: collectionSl
|
|
|
12
12
|
}
|
|
13
13
|
};
|
|
14
14
|
let idToUpdate = id;
|
|
15
|
-
const { joins, selectFields, where } =
|
|
15
|
+
const { joins, selectFields, where } = buildQuery({
|
|
16
16
|
adapter: this,
|
|
17
17
|
fields: collection.fields,
|
|
18
18
|
locale,
|
|
@@ -44,6 +44,7 @@ export const updateOne = async function updateOne({ id, collection: collectionSl
|
|
|
44
44
|
data,
|
|
45
45
|
db,
|
|
46
46
|
fields: collection.fields,
|
|
47
|
+
joinQuery,
|
|
47
48
|
operation: 'update',
|
|
48
49
|
req,
|
|
49
50
|
tableName
|
package/dist/update.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/update.ts"],"sourcesContent":["import type { UpdateOne } from 'payload'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport buildQuery from './queries/buildQuery.js'\nimport { selectDistinct } from './queries/selectDistinct.js'\nimport { upsertRow } from './upsertRow/index.js'\n\nexport const updateOne: UpdateOne = async function updateOne(\n this: DrizzleAdapter,\n { id, collection: collectionSlug, data, draft, locale, req, where: whereArg },\n) {\n const db = this.sessions[await req?.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))\n const whereToUse = whereArg || { id: { equals: id } }\n let idToUpdate = id\n\n const { joins, selectFields, where } =
|
|
1
|
+
{"version":3,"sources":["../src/update.ts"],"sourcesContent":["import type { UpdateOne } from 'payload'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport buildQuery from './queries/buildQuery.js'\nimport { selectDistinct } from './queries/selectDistinct.js'\nimport { upsertRow } from './upsertRow/index.js'\n\nexport const updateOne: UpdateOne = async function updateOne(\n this: DrizzleAdapter,\n { id, collection: collectionSlug, data, draft, joins: joinQuery, locale, req, where: whereArg },\n) {\n const db = this.sessions[await req?.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))\n const whereToUse = whereArg || { id: { equals: id } }\n let idToUpdate = id\n\n const { joins, selectFields, where } = buildQuery({\n adapter: this,\n fields: collection.fields,\n locale,\n tableName,\n where: whereToUse,\n })\n\n const selectDistinctResult = await selectDistinct({\n adapter: this,\n chainedMethods: [{ args: [1], method: 'limit' }],\n db,\n joins,\n selectFields,\n tableName,\n where,\n })\n\n if (selectDistinctResult?.[0]?.id) {\n idToUpdate = selectDistinctResult?.[0]?.id\n }\n\n const result = await upsertRow({\n id: idToUpdate,\n adapter: this,\n data,\n db,\n fields: collection.fields,\n joinQuery,\n operation: 'update',\n req,\n tableName,\n })\n\n return result\n}\n"],"names":["toSnakeCase","buildQuery","selectDistinct","upsertRow","updateOne","id","collection","collectionSlug","data","draft","joins","joinQuery","locale","req","where","whereArg","db","sessions","transactionID","drizzle","payload","collections","config","tableName","tableNameMap","get","slug","whereToUse","equals","idToUpdate","selectFields","adapter","fields","selectDistinctResult","chainedMethods","args","method","result","operation"],"mappings":"AAEA,OAAOA,iBAAiB,gBAAe;AAIvC,OAAOC,gBAAgB,0BAAyB;AAChD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,MAAMC,YAAuB,eAAeA,UAEjD,EAAEC,EAAE,EAAEC,YAAYC,cAAc,EAAEC,IAAI,EAAEC,KAAK,EAAEC,OAAOC,SAAS,EAAEC,MAAM,EAAEC,GAAG,EAAEC,OAAOC,QAAQ,EAAE;IAE/F,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAAC,MAAMJ,KAAKK,cAAc,EAAEF,MAAM,IAAI,CAACG,OAAO;IACtE,MAAMb,aAAa,IAAI,CAACc,OAAO,CAACC,WAAW,CAACd,eAAe,CAACe,MAAM;IAClE,MAAMC,YAAY,IAAI,CAACC,YAAY,CAACC,GAAG,CAACzB,YAAYM,WAAWoB,IAAI;IACnE,MAAMC,aAAaZ,YAAY;QAAEV,IAAI;YAAEuB,QAAQvB;QAAG;IAAE;IACpD,IAAIwB,aAAaxB;IAEjB,MAAM,EAAEK,KAAK,EAAEoB,YAAY,EAAEhB,KAAK,EAAE,GAAGb,WAAW;QAChD8B,SAAS,IAAI;QACbC,QAAQ1B,WAAW0B,MAAM;QACzBpB;QACAW;QACAT,OAAOa;IACT;IAEA,MAAMM,uBAAuB,MAAM/B,eAAe;QAChD6B,SAAS,IAAI;QACbG,gBAAgB;YAAC;gBAAEC,MAAM;oBAAC;iBAAE;gBAAEC,QAAQ;YAAQ;SAAE;QAChDpB;QACAN;QACAoB;QACAP;QACAT;IACF;IAEA,IAAImB,sBAAsB,CAAC,EAAE,EAAE5B,IAAI;QACjCwB,aAAaI,sBAAsB,CAAC,EAAE,EAAE5B;IAC1C;IAEA,MAAMgC,SAAS,MAAMlC,UAAU;QAC7BE,IAAIwB;QACJE,SAAS,IAAI;QACbvB;QACAQ;QACAgB,QAAQ1B,WAAW0B,MAAM;QACzBrB;QACA2B,WAAW;QACXzB;QACAU;IACF;IAEA,OAAOc;AACT,EAAC"}
|
|
@@ -11,8 +11,8 @@ export async function updateGlobalVersion({ id, global, locale, req = {}, versio
|
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
13
|
const tableName = this.tableNameMap.get(`_${toSnakeCase(globalConfig.slug)}${this.versionsSuffix}`);
|
|
14
|
-
const fields = buildVersionGlobalFields(globalConfig);
|
|
15
|
-
const { where } =
|
|
14
|
+
const fields = buildVersionGlobalFields(this.payload.config, globalConfig);
|
|
15
|
+
const { where } = buildQuery({
|
|
16
16
|
adapter: this,
|
|
17
17
|
fields,
|
|
18
18
|
locale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateGlobalVersion.ts"],"sourcesContent":["import type {\n PayloadRequest,\n SanitizedGlobalConfig,\n TypeWithID,\n TypeWithVersion,\n UpdateGlobalVersionArgs,\n} from 'payload'\n\nimport { buildVersionGlobalFields } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport buildQuery from './queries/buildQuery.js'\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function updateGlobalVersion<T extends TypeWithID>(\n this: DrizzleAdapter,\n {\n id,\n global,\n locale,\n req = {} as PayloadRequest,\n versionData,\n where: whereArg,\n }: UpdateGlobalVersionArgs<T>,\n) {\n const db = this.sessions[await req?.transactionID]?.db || this.drizzle\n const globalConfig: SanitizedGlobalConfig = this.payload.globals.config.find(\n ({ slug }) => slug === global,\n )\n const whereToUse = whereArg || { id: { equals: id } }\n\n const tableName = this.tableNameMap.get(\n `_${toSnakeCase(globalConfig.slug)}${this.versionsSuffix}`,\n )\n\n const fields = buildVersionGlobalFields(globalConfig)\n\n const { where } =
|
|
1
|
+
{"version":3,"sources":["../src/updateGlobalVersion.ts"],"sourcesContent":["import type {\n PayloadRequest,\n SanitizedGlobalConfig,\n TypeWithID,\n TypeWithVersion,\n UpdateGlobalVersionArgs,\n} from 'payload'\n\nimport { buildVersionGlobalFields } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport buildQuery from './queries/buildQuery.js'\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function updateGlobalVersion<T extends TypeWithID>(\n this: DrizzleAdapter,\n {\n id,\n global,\n locale,\n req = {} as PayloadRequest,\n versionData,\n where: whereArg,\n }: UpdateGlobalVersionArgs<T>,\n) {\n const db = this.sessions[await req?.transactionID]?.db || this.drizzle\n const globalConfig: SanitizedGlobalConfig = this.payload.globals.config.find(\n ({ slug }) => slug === global,\n )\n const whereToUse = whereArg || { id: { equals: id } }\n\n const tableName = this.tableNameMap.get(\n `_${toSnakeCase(globalConfig.slug)}${this.versionsSuffix}`,\n )\n\n const fields = buildVersionGlobalFields(this.payload.config, globalConfig)\n\n const { where } = buildQuery({\n adapter: this,\n fields,\n locale,\n tableName,\n where: whereToUse,\n })\n\n const result = await upsertRow<TypeWithVersion<T>>({\n id,\n adapter: this,\n data: versionData,\n db,\n fields,\n operation: 'update',\n req,\n tableName,\n where,\n })\n\n return result\n}\n"],"names":["buildVersionGlobalFields","toSnakeCase","buildQuery","upsertRow","updateGlobalVersion","id","global","locale","req","versionData","where","whereArg","db","sessions","transactionID","drizzle","globalConfig","payload","globals","config","find","slug","whereToUse","equals","tableName","tableNameMap","get","versionsSuffix","fields","adapter","result","data","operation"],"mappings":"AAQA,SAASA,wBAAwB,QAAQ,UAAS;AAClD,OAAOC,iBAAiB,gBAAe;AAIvC,OAAOC,gBAAgB,0BAAyB;AAChD,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,oBAEpB,EACEC,EAAE,EACFC,MAAM,EACNC,MAAM,EACNC,MAAM,CAAC,CAAmB,EAC1BC,WAAW,EACXC,OAAOC,QAAQ,EACY;IAE7B,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAAC,MAAML,KAAKM,cAAc,EAAEF,MAAM,IAAI,CAACG,OAAO;IACtE,MAAMC,eAAsC,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAC1E,CAAC,EAAEC,IAAI,EAAE,GAAKA,SAASf;IAEzB,MAAMgB,aAAaX,YAAY;QAAEN,IAAI;YAAEkB,QAAQlB;QAAG;IAAE;IAEpD,MAAMmB,YAAY,IAAI,CAACC,YAAY,CAACC,GAAG,CACrC,CAAC,CAAC,EAAEzB,YAAYe,aAAaK,IAAI,EAAE,EAAE,IAAI,CAACM,cAAc,CAAC,CAAC;IAG5D,MAAMC,SAAS5B,yBAAyB,IAAI,CAACiB,OAAO,CAACE,MAAM,EAAEH;IAE7D,MAAM,EAAEN,KAAK,EAAE,GAAGR,WAAW;QAC3B2B,SAAS,IAAI;QACbD;QACArB;QACAiB;QACAd,OAAOY;IACT;IAEA,MAAMQ,SAAS,MAAM3B,UAA8B;QACjDE;QACAwB,SAAS,IAAI;QACbE,MAAMtB;QACNG;QACAgB;QACAI,WAAW;QACXxB;QACAgB;QACAd;IACF;IAEA,OAAOoB;AACT"}
|
package/dist/updateVersion.js
CHANGED
|
@@ -11,8 +11,8 @@ export async function updateVersion({ id, collection, locale, req = {}, versionD
|
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
13
|
const tableName = this.tableNameMap.get(`_${toSnakeCase(collectionConfig.slug)}${this.versionsSuffix}`);
|
|
14
|
-
const fields = buildVersionCollectionFields(collectionConfig);
|
|
15
|
-
const { where } =
|
|
14
|
+
const fields = buildVersionCollectionFields(this.payload.config, collectionConfig);
|
|
15
|
+
const { where } = buildQuery({
|
|
16
16
|
adapter: this,
|
|
17
17
|
fields,
|
|
18
18
|
locale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateVersion.ts"],"sourcesContent":["import type {\n PayloadRequest,\n SanitizedCollectionConfig,\n TypeWithID,\n TypeWithVersion,\n UpdateVersionArgs,\n} from 'payload'\n\nimport { buildVersionCollectionFields } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport buildQuery from './queries/buildQuery.js'\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function updateVersion<T extends TypeWithID>(\n this: DrizzleAdapter,\n {\n id,\n collection,\n locale,\n req = {} as PayloadRequest,\n versionData,\n where: whereArg,\n }: UpdateVersionArgs<T>,\n) {\n const db = this.sessions[await req?.transactionID]?.db || this.drizzle\n const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config\n const whereToUse = whereArg || { id: { equals: id } }\n const tableName = this.tableNameMap.get(\n `_${toSnakeCase(collectionConfig.slug)}${this.versionsSuffix}`,\n )\n\n const fields = buildVersionCollectionFields(collectionConfig)\n\n const { where } =
|
|
1
|
+
{"version":3,"sources":["../src/updateVersion.ts"],"sourcesContent":["import type {\n PayloadRequest,\n SanitizedCollectionConfig,\n TypeWithID,\n TypeWithVersion,\n UpdateVersionArgs,\n} from 'payload'\n\nimport { buildVersionCollectionFields } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport buildQuery from './queries/buildQuery.js'\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function updateVersion<T extends TypeWithID>(\n this: DrizzleAdapter,\n {\n id,\n collection,\n locale,\n req = {} as PayloadRequest,\n versionData,\n where: whereArg,\n }: UpdateVersionArgs<T>,\n) {\n const db = this.sessions[await req?.transactionID]?.db || this.drizzle\n const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config\n const whereToUse = whereArg || { id: { equals: id } }\n const tableName = this.tableNameMap.get(\n `_${toSnakeCase(collectionConfig.slug)}${this.versionsSuffix}`,\n )\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig)\n\n const { where } = buildQuery({\n adapter: this,\n fields,\n locale,\n tableName,\n where: whereToUse,\n })\n\n const result = await upsertRow<TypeWithVersion<T>>({\n id,\n adapter: this,\n data: versionData,\n db,\n fields,\n operation: 'update',\n req,\n tableName,\n where,\n })\n\n return result\n}\n"],"names":["buildVersionCollectionFields","toSnakeCase","buildQuery","upsertRow","updateVersion","id","collection","locale","req","versionData","where","whereArg","db","sessions","transactionID","drizzle","collectionConfig","payload","collections","config","whereToUse","equals","tableName","tableNameMap","get","slug","versionsSuffix","fields","adapter","result","data","operation"],"mappings":"AAQA,SAASA,4BAA4B,QAAQ,UAAS;AACtD,OAAOC,iBAAiB,gBAAe;AAIvC,OAAOC,gBAAgB,0BAAyB;AAChD,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,cAEpB,EACEC,EAAE,EACFC,UAAU,EACVC,MAAM,EACNC,MAAM,CAAC,CAAmB,EAC1BC,WAAW,EACXC,OAAOC,QAAQ,EACM;IAEvB,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAAC,MAAML,KAAKM,cAAc,EAAEF,MAAM,IAAI,CAACG,OAAO;IACtE,MAAMC,mBAA8C,IAAI,CAACC,OAAO,CAACC,WAAW,CAACZ,WAAW,CAACa,MAAM;IAC/F,MAAMC,aAAaT,YAAY;QAAEN,IAAI;YAAEgB,QAAQhB;QAAG;IAAE;IACpD,MAAMiB,YAAY,IAAI,CAACC,YAAY,CAACC,GAAG,CACrC,CAAC,CAAC,EAAEvB,YAAYe,iBAAiBS,IAAI,EAAE,EAAE,IAAI,CAACC,cAAc,CAAC,CAAC;IAGhE,MAAMC,SAAS3B,6BAA6B,IAAI,CAACiB,OAAO,CAACE,MAAM,EAAEH;IAEjE,MAAM,EAAEN,KAAK,EAAE,GAAGR,WAAW;QAC3B0B,SAAS,IAAI;QACbD;QACApB;QACAe;QACAZ,OAAOU;IACT;IAEA,MAAMS,SAAS,MAAM1B,UAA8B;QACjDE;QACAuB,SAAS,IAAI;QACbE,MAAMrB;QACNG;QACAe;QACAI,WAAW;QACXvB;QACAc;QACAZ;IACF;IAEA,OAAOmB;AACT"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { TypeWithID } from 'payload';
|
|
2
2
|
import type { Args } from './types.js';
|
|
3
|
-
export declare const upsertRow: <T extends Record<string, unknown> | TypeWithID>({ id, adapter, data, db, fields, ignoreResult, operation, path, req, tableName, upsertTarget, where, }: Args) => Promise<T>;
|
|
3
|
+
export declare const upsertRow: <T extends Record<string, unknown> | TypeWithID>({ id, adapter, data, db, fields, ignoreResult, joinQuery, operation, path, req, tableName, upsertTarget, where, }: Args) => Promise<T>;
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/upsertRow/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAMzC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAStC,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/upsertRow/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAMzC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAStC,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU,qHAc3E,IAAI,KAAG,OAAO,CAAC,CAAC,CAwZlB,CAAA"}
|
package/dist/upsertRow/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { transformForWrite } from '../transform/write/index.js';
|
|
|
6
6
|
import { deleteExistingArrayRows } from './deleteExistingArrayRows.js';
|
|
7
7
|
import { deleteExistingRowsByPath } from './deleteExistingRowsByPath.js';
|
|
8
8
|
import { insertArrays } from './insertArrays.js';
|
|
9
|
-
export const upsertRow = async ({ id, adapter, data, db, fields, ignoreResult, operation, path = '', req, tableName, upsertTarget, where })=>{
|
|
9
|
+
export const upsertRow = async ({ id, adapter, data, db, fields, ignoreResult, joinQuery, operation, path = '', req, tableName, upsertTarget, where })=>{
|
|
10
10
|
// Split out the incoming data into the corresponding:
|
|
11
11
|
// base row, locales, relationships, blocks, and arrays
|
|
12
12
|
const rowToInsert = transformForWrite({
|
|
@@ -353,6 +353,7 @@ export const upsertRow = async ({ id, adapter, data, db, fields, ignoreResult, o
|
|
|
353
353
|
adapter,
|
|
354
354
|
depth: 0,
|
|
355
355
|
fields,
|
|
356
|
+
joinQuery,
|
|
356
357
|
tableName
|
|
357
358
|
});
|
|
358
359
|
findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id);
|
|
@@ -364,7 +365,8 @@ export const upsertRow = async ({ id, adapter, data, db, fields, ignoreResult, o
|
|
|
364
365
|
adapter,
|
|
365
366
|
config: adapter.payload.config,
|
|
366
367
|
data: doc,
|
|
367
|
-
fields
|
|
368
|
+
fields,
|
|
369
|
+
joinQuery
|
|
368
370
|
});
|
|
369
371
|
return result;
|
|
370
372
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/upsertRow/index.ts"],"sourcesContent":["import type { TypeWithID } from 'payload'\n\nimport { eq } from 'drizzle-orm'\nimport { ValidationError } from 'payload'\n\nimport type { BlockRowToInsert } from '../transform/write/types.js'\nimport type { Args } from './types.js'\n\nimport { buildFindManyArgs } from '../find/buildFindManyArgs.js'\nimport { transform } from '../transform/read/index.js'\nimport { transformForWrite } from '../transform/write/index.js'\nimport { deleteExistingArrayRows } from './deleteExistingArrayRows.js'\nimport { deleteExistingRowsByPath } from './deleteExistingRowsByPath.js'\nimport { insertArrays } from './insertArrays.js'\n\nexport const upsertRow = async <T extends Record<string, unknown> | TypeWithID>({\n id,\n adapter,\n data,\n db,\n fields,\n ignoreResult,\n operation,\n path = '',\n req,\n tableName,\n upsertTarget,\n where,\n}: Args): Promise<T> => {\n // Split out the incoming data into the corresponding:\n // base row, locales, relationships, blocks, and arrays\n const rowToInsert = transformForWrite({\n adapter,\n data,\n fields,\n path,\n tableName,\n })\n\n // First, we insert the main row\n let insertedRow: Record<string, unknown>\n\n try {\n if (operation === 'update') {\n const target = upsertTarget || adapter.tables[tableName].id\n\n if (id) {\n rowToInsert.row.id = id\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target },\n tableName,\n values: rowToInsert.row,\n })\n } else {\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target, where },\n tableName,\n values: rowToInsert.row,\n })\n }\n } else {\n ;[insertedRow] = await adapter.insert({\n db,\n tableName,\n values: rowToInsert.row,\n })\n }\n\n const localesToInsert: Record<string, unknown>[] = []\n const relationsToInsert: Record<string, unknown>[] = []\n const textsToInsert: Record<string, unknown>[] = []\n const numbersToInsert: Record<string, unknown>[] = []\n const blocksToInsert: { [blockType: string]: BlockRowToInsert[] } = {}\n const selectsToInsert: { [selectTableName: string]: Record<string, unknown>[] } = {}\n\n // If there are locale rows with data, add the parent and locale to each\n if (Object.keys(rowToInsert.locales).length > 0) {\n Object.entries(rowToInsert.locales).forEach(([locale, localeRow]) => {\n localeRow._parentID = insertedRow.id\n localeRow._locale = locale\n localesToInsert.push(localeRow)\n })\n }\n\n // If there are relationships, add parent to each\n if (rowToInsert.relationships.length > 0) {\n rowToInsert.relationships.forEach((relation) => {\n relation.parent = insertedRow.id\n relationsToInsert.push(relation)\n })\n }\n\n // If there are texts, add parent to each\n if (rowToInsert.texts.length > 0) {\n rowToInsert.texts.forEach((textRow) => {\n textRow.parent = insertedRow.id\n textsToInsert.push(textRow)\n })\n }\n\n // If there are numbers, add parent to each\n if (rowToInsert.numbers.length > 0) {\n rowToInsert.numbers.forEach((numberRow) => {\n numberRow.parent = insertedRow.id\n numbersToInsert.push(numberRow)\n })\n }\n\n // If there are selects, add parent to each, and then\n // store by table name and rows\n if (Object.keys(rowToInsert.selects).length > 0) {\n Object.entries(rowToInsert.selects).forEach(([selectTableName, selectRows]) => {\n selectRows.forEach((row) => {\n if (typeof row.parent === 'undefined') {\n row.parent = insertedRow.id\n }\n if (!selectsToInsert[selectTableName]) {\n selectsToInsert[selectTableName] = []\n }\n selectsToInsert[selectTableName].push(row)\n })\n })\n }\n\n // If there are blocks, add parent to each, and then\n // store by table name and rows\n Object.keys(rowToInsert.blocks).forEach((blockName) => {\n rowToInsert.blocks[blockName].forEach((blockRow) => {\n blockRow.row._parentID = insertedRow.id\n if (!blocksToInsert[blockName]) {\n blocksToInsert[blockName] = []\n }\n if (blockRow.row.uuid) {\n delete blockRow.row.uuid\n }\n blocksToInsert[blockName].push(blockRow)\n })\n })\n\n // //////////////////////////////////\n // INSERT LOCALES\n // //////////////////////////////////\n\n if (localesToInsert.length > 0) {\n const localeTableName = `${tableName}${adapter.localesSuffix}`\n const localeTable = adapter.tables[`${tableName}${adapter.localesSuffix}`]\n\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: localeTableName,\n where: eq(localeTable._parentID, insertedRow.id),\n })\n }\n\n await adapter.insert({\n db,\n tableName: localeTableName,\n values: localesToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT RELATIONSHIPS\n // //////////////////////////////////\n\n const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: [...relationsToInsert, ...rowToInsert.relationshipsToDelete],\n tableName: relationshipsTableName,\n })\n }\n\n if (relationsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: relationshipsTableName,\n values: relationsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany TEXTS\n // //////////////////////////////////\n\n const textsTableName = `${tableName}_texts`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: textsToInsert,\n tableName: textsTableName,\n })\n }\n\n if (textsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: textsTableName,\n values: textsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany NUMBERS\n // //////////////////////////////////\n\n const numbersTableName = `${tableName}_numbers`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: numbersToInsert,\n tableName: numbersTableName,\n })\n }\n\n if (numbersToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: numbersTableName,\n values: numbersToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT BLOCKS\n // //////////////////////////////////\n\n const insertedBlockRows: Record<string, Record<string, unknown>[]> = {}\n\n if (operation === 'update') {\n for (const blockName of rowToInsert.blocksToDelete) {\n const blockTableName = adapter.tableNameMap.get(`${tableName}_blocks_${blockName}`)\n const blockTable = adapter.tables[blockTableName]\n await adapter.deleteWhere({\n db,\n tableName: blockTableName,\n where: eq(blockTable._parentID, insertedRow.id),\n })\n }\n }\n\n for (const [blockName, blockRows] of Object.entries(blocksToInsert)) {\n const blockTableName = adapter.tableNameMap.get(`${tableName}_blocks_${blockName}`)\n insertedBlockRows[blockName] = await adapter.insert({\n db,\n tableName: blockTableName,\n values: blockRows.map(({ row }) => row),\n })\n\n insertedBlockRows[blockName].forEach((row, i) => {\n blockRows[i].row = row\n })\n\n const blockLocaleIndexMap: number[] = []\n\n const blockLocaleRowsToInsert = blockRows.reduce((acc, blockRow, i) => {\n if (Object.entries(blockRow.locales).length > 0) {\n Object.entries(blockRow.locales).forEach(([blockLocale, blockLocaleData]) => {\n if (Object.keys(blockLocaleData).length > 0) {\n blockLocaleData._parentID = blockRow.row.id\n blockLocaleData._locale = blockLocale\n acc.push(blockLocaleData)\n blockLocaleIndexMap.push(i)\n }\n })\n }\n\n return acc\n }, [])\n\n if (blockLocaleRowsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: `${blockTableName}${adapter.localesSuffix}`,\n values: blockLocaleRowsToInsert,\n })\n }\n\n await insertArrays({\n adapter,\n arrays: blockRows.map(({ arrays }) => arrays),\n db,\n parentRows: insertedBlockRows[blockName],\n })\n }\n\n // //////////////////////////////////\n // INSERT ARRAYS RECURSIVELY\n // //////////////////////////////////\n\n if (operation === 'update') {\n for (const arrayTableName of Object.keys(rowToInsert.arrays)) {\n await deleteExistingArrayRows({\n adapter,\n db,\n parentID: insertedRow.id,\n tableName: arrayTableName,\n })\n }\n }\n\n await insertArrays({\n adapter,\n arrays: [rowToInsert.arrays],\n db,\n parentRows: [insertedRow],\n })\n\n // //////////////////////////////////\n // INSERT hasMany SELECTS\n // //////////////////////////////////\n\n for (const [selectTableName, tableRows] of Object.entries(selectsToInsert)) {\n const selectTable = adapter.tables[selectTableName]\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: selectTableName,\n where: eq(selectTable.parent, insertedRow.id),\n })\n }\n await adapter.insert({\n db,\n tableName: selectTableName,\n values: tableRows,\n })\n }\n\n // //////////////////////////////////\n // Error Handling\n // //////////////////////////////////\n } catch (error) {\n if (error.code === '23505') {\n let fieldName: null | string = null\n // We need to try and find the right constraint for the field but if we can't we fallback to a generic message\n if (adapter.fieldConstraints?.[tableName]) {\n if (adapter.fieldConstraints[tableName]?.[error.constraint]) {\n fieldName = adapter.fieldConstraints[tableName]?.[error.constraint]\n } else {\n const replacement = `${tableName}_`\n\n if (error.constraint.includes(replacement)) {\n const replacedConstraint = error.constraint.replace(replacement, '')\n\n if (replacedConstraint && adapter.fieldConstraints[tableName]?.[replacedConstraint]) {\n fieldName = adapter.fieldConstraints[tableName][replacedConstraint]\n }\n }\n }\n }\n\n if (!fieldName) {\n // Last case scenario we extract the key and value from the detail on the error\n const detail = error.detail\n const regex = /Key \\(([^)]+)\\)=\\(([^)]+)\\)/\n const match = detail.match(regex)\n\n if (match) {\n const key = match[1]\n\n fieldName = key\n }\n }\n\n throw new ValidationError(\n {\n id,\n errors: [\n {\n field: fieldName,\n message: req.t('error:valueMustBeUnique'),\n },\n ],\n },\n req.t,\n )\n } else {\n throw error\n }\n }\n\n if (ignoreResult) {\n return data as T\n }\n\n // //////////////////////////////////\n // RETRIEVE NEWLY UPDATED ROW\n // //////////////////////////////////\n\n const findManyArgs = buildFindManyArgs({\n adapter,\n depth: 0,\n fields,\n tableName,\n })\n\n findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id)\n\n const doc = await db.query[tableName].findFirst(findManyArgs)\n\n // //////////////////////////////////\n // TRANSFORM DATA\n // //////////////////////////////////\n\n const result = transform<T>({\n adapter,\n config: adapter.payload.config,\n data: doc,\n fields,\n })\n\n return result\n}\n"],"names":["eq","ValidationError","buildFindManyArgs","transform","transformForWrite","deleteExistingArrayRows","deleteExistingRowsByPath","insertArrays","upsertRow","id","adapter","data","db","fields","ignoreResult","operation","path","req","tableName","upsertTarget","where","rowToInsert","insertedRow","target","tables","row","insert","onConflictDoUpdate","set","values","localesToInsert","relationsToInsert","textsToInsert","numbersToInsert","blocksToInsert","selectsToInsert","Object","keys","locales","length","entries","forEach","locale","localeRow","_parentID","_locale","push","relationships","relation","parent","texts","textRow","numbers","numberRow","selects","selectTableName","selectRows","blocks","blockName","blockRow","uuid","localeTableName","localesSuffix","localeTable","deleteWhere","relationshipsTableName","relationshipsSuffix","localeColumnName","parentColumnName","parentID","pathColumnName","rows","relationshipsToDelete","textsTableName","numbersTableName","insertedBlockRows","blocksToDelete","blockTableName","tableNameMap","get","blockTable","blockRows","map","i","blockLocaleIndexMap","blockLocaleRowsToInsert","reduce","acc","blockLocale","blockLocaleData","arrays","parentRows","arrayTableName","tableRows","selectTable","error","code","fieldName","fieldConstraints","constraint","replacement","includes","replacedConstraint","replace","detail","regex","match","key","errors","field","message","t","findManyArgs","depth","doc","query","findFirst","result","config","payload"],"mappings":"AAEA,SAASA,EAAE,QAAQ,cAAa;AAChC,SAASC,eAAe,QAAQ,UAAS;AAKzC,SAASC,iBAAiB,QAAQ,+BAA8B;AAChE,SAASC,SAAS,QAAQ,6BAA4B;AACtD,SAASC,iBAAiB,QAAQ,8BAA6B;AAC/D,SAASC,uBAAuB,QAAQ,+BAA8B;AACtE,SAASC,wBAAwB,QAAQ,gCAA+B;AACxE,SAASC,YAAY,QAAQ,oBAAmB;AAEhD,OAAO,MAAMC,YAAY,OAAuD,EAC9EC,EAAE,EACFC,OAAO,EACPC,IAAI,EACJC,EAAE,EACFC,MAAM,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,EAAEa,YAAYb,EAAE;IAEpE,MAAMyG,MAAM,MAAMtG,GAAGuG,KAAK,CAACjG,UAAU,CAACkG,SAAS,CAACJ;IAEhD,qCAAqC;IACrC,iBAAiB;IACjB,qCAAqC;IAErC,MAAMK,SAASlH,UAAa;QAC1BO;QACA4G,QAAQ5G,QAAQ6G,OAAO,CAACD,MAAM;QAC9B3G,MAAMuG;QACNrG;IACF;IAEA,OAAOwG;AACT,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/upsertRow/index.ts"],"sourcesContent":["import type { TypeWithID } from 'payload'\n\nimport { eq } from 'drizzle-orm'\nimport { ValidationError } from 'payload'\n\nimport type { BlockRowToInsert } from '../transform/write/types.js'\nimport type { Args } from './types.js'\n\nimport { buildFindManyArgs } from '../find/buildFindManyArgs.js'\nimport { transform } from '../transform/read/index.js'\nimport { transformForWrite } from '../transform/write/index.js'\nimport { deleteExistingArrayRows } from './deleteExistingArrayRows.js'\nimport { deleteExistingRowsByPath } from './deleteExistingRowsByPath.js'\nimport { insertArrays } from './insertArrays.js'\n\nexport const upsertRow = async <T extends Record<string, unknown> | TypeWithID>({\n id,\n adapter,\n data,\n db,\n fields,\n ignoreResult,\n joinQuery,\n operation,\n path = '',\n req,\n tableName,\n upsertTarget,\n where,\n}: Args): Promise<T> => {\n // Split out the incoming data into the corresponding:\n // base row, locales, relationships, blocks, and arrays\n const rowToInsert = transformForWrite({\n adapter,\n data,\n fields,\n path,\n tableName,\n })\n\n // First, we insert the main row\n let insertedRow: Record<string, unknown>\n\n try {\n if (operation === 'update') {\n const target = upsertTarget || adapter.tables[tableName].id\n\n if (id) {\n rowToInsert.row.id = id\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target },\n tableName,\n values: rowToInsert.row,\n })\n } else {\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target, where },\n tableName,\n values: rowToInsert.row,\n })\n }\n } else {\n ;[insertedRow] = await adapter.insert({\n db,\n tableName,\n values: rowToInsert.row,\n })\n }\n\n const localesToInsert: Record<string, unknown>[] = []\n const relationsToInsert: Record<string, unknown>[] = []\n const textsToInsert: Record<string, unknown>[] = []\n const numbersToInsert: Record<string, unknown>[] = []\n const blocksToInsert: { [blockType: string]: BlockRowToInsert[] } = {}\n const selectsToInsert: { [selectTableName: string]: Record<string, unknown>[] } = {}\n\n // If there are locale rows with data, add the parent and locale to each\n if (Object.keys(rowToInsert.locales).length > 0) {\n Object.entries(rowToInsert.locales).forEach(([locale, localeRow]) => {\n localeRow._parentID = insertedRow.id\n localeRow._locale = locale\n localesToInsert.push(localeRow)\n })\n }\n\n // If there are relationships, add parent to each\n if (rowToInsert.relationships.length > 0) {\n rowToInsert.relationships.forEach((relation) => {\n relation.parent = insertedRow.id\n relationsToInsert.push(relation)\n })\n }\n\n // If there are texts, add parent to each\n if (rowToInsert.texts.length > 0) {\n rowToInsert.texts.forEach((textRow) => {\n textRow.parent = insertedRow.id\n textsToInsert.push(textRow)\n })\n }\n\n // If there are numbers, add parent to each\n if (rowToInsert.numbers.length > 0) {\n rowToInsert.numbers.forEach((numberRow) => {\n numberRow.parent = insertedRow.id\n numbersToInsert.push(numberRow)\n })\n }\n\n // If there are selects, add parent to each, and then\n // store by table name and rows\n if (Object.keys(rowToInsert.selects).length > 0) {\n Object.entries(rowToInsert.selects).forEach(([selectTableName, selectRows]) => {\n selectRows.forEach((row) => {\n if (typeof row.parent === 'undefined') {\n row.parent = insertedRow.id\n }\n if (!selectsToInsert[selectTableName]) {\n selectsToInsert[selectTableName] = []\n }\n selectsToInsert[selectTableName].push(row)\n })\n })\n }\n\n // If there are blocks, add parent to each, and then\n // store by table name and rows\n Object.keys(rowToInsert.blocks).forEach((blockName) => {\n rowToInsert.blocks[blockName].forEach((blockRow) => {\n blockRow.row._parentID = insertedRow.id\n if (!blocksToInsert[blockName]) {\n blocksToInsert[blockName] = []\n }\n if (blockRow.row.uuid) {\n delete blockRow.row.uuid\n }\n blocksToInsert[blockName].push(blockRow)\n })\n })\n\n // //////////////////////////////////\n // INSERT LOCALES\n // //////////////////////////////////\n\n if (localesToInsert.length > 0) {\n const localeTableName = `${tableName}${adapter.localesSuffix}`\n const localeTable = adapter.tables[`${tableName}${adapter.localesSuffix}`]\n\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: localeTableName,\n where: eq(localeTable._parentID, insertedRow.id),\n })\n }\n\n await adapter.insert({\n db,\n tableName: localeTableName,\n values: localesToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT RELATIONSHIPS\n // //////////////////////////////////\n\n const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: [...relationsToInsert, ...rowToInsert.relationshipsToDelete],\n tableName: relationshipsTableName,\n })\n }\n\n if (relationsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: relationshipsTableName,\n values: relationsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany TEXTS\n // //////////////////////////////////\n\n const textsTableName = `${tableName}_texts`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: textsToInsert,\n tableName: textsTableName,\n })\n }\n\n if (textsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: textsTableName,\n values: textsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany NUMBERS\n // //////////////////////////////////\n\n const numbersTableName = `${tableName}_numbers`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: numbersToInsert,\n tableName: numbersTableName,\n })\n }\n\n if (numbersToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: numbersTableName,\n values: numbersToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT BLOCKS\n // //////////////////////////////////\n\n const insertedBlockRows: Record<string, Record<string, unknown>[]> = {}\n\n if (operation === 'update') {\n for (const blockName of rowToInsert.blocksToDelete) {\n const blockTableName = adapter.tableNameMap.get(`${tableName}_blocks_${blockName}`)\n const blockTable = adapter.tables[blockTableName]\n await adapter.deleteWhere({\n db,\n tableName: blockTableName,\n where: eq(blockTable._parentID, insertedRow.id),\n })\n }\n }\n\n for (const [blockName, blockRows] of Object.entries(blocksToInsert)) {\n const blockTableName = adapter.tableNameMap.get(`${tableName}_blocks_${blockName}`)\n insertedBlockRows[blockName] = await adapter.insert({\n db,\n tableName: blockTableName,\n values: blockRows.map(({ row }) => row),\n })\n\n insertedBlockRows[blockName].forEach((row, i) => {\n blockRows[i].row = row\n })\n\n const blockLocaleIndexMap: number[] = []\n\n const blockLocaleRowsToInsert = blockRows.reduce((acc, blockRow, i) => {\n if (Object.entries(blockRow.locales).length > 0) {\n Object.entries(blockRow.locales).forEach(([blockLocale, blockLocaleData]) => {\n if (Object.keys(blockLocaleData).length > 0) {\n blockLocaleData._parentID = blockRow.row.id\n blockLocaleData._locale = blockLocale\n acc.push(blockLocaleData)\n blockLocaleIndexMap.push(i)\n }\n })\n }\n\n return acc\n }, [])\n\n if (blockLocaleRowsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: `${blockTableName}${adapter.localesSuffix}`,\n values: blockLocaleRowsToInsert,\n })\n }\n\n await insertArrays({\n adapter,\n arrays: blockRows.map(({ arrays }) => arrays),\n db,\n parentRows: insertedBlockRows[blockName],\n })\n }\n\n // //////////////////////////////////\n // INSERT ARRAYS RECURSIVELY\n // //////////////////////////////////\n\n if (operation === 'update') {\n for (const arrayTableName of Object.keys(rowToInsert.arrays)) {\n await deleteExistingArrayRows({\n adapter,\n db,\n parentID: insertedRow.id,\n tableName: arrayTableName,\n })\n }\n }\n\n await insertArrays({\n adapter,\n arrays: [rowToInsert.arrays],\n db,\n parentRows: [insertedRow],\n })\n\n // //////////////////////////////////\n // INSERT hasMany SELECTS\n // //////////////////////////////////\n\n for (const [selectTableName, tableRows] of Object.entries(selectsToInsert)) {\n const selectTable = adapter.tables[selectTableName]\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: selectTableName,\n where: eq(selectTable.parent, insertedRow.id),\n })\n }\n await adapter.insert({\n db,\n tableName: selectTableName,\n values: tableRows,\n })\n }\n\n // //////////////////////////////////\n // Error Handling\n // //////////////////////////////////\n } catch (error) {\n if (error.code === '23505') {\n let fieldName: null | string = null\n // We need to try and find the right constraint for the field but if we can't we fallback to a generic message\n if (adapter.fieldConstraints?.[tableName]) {\n if (adapter.fieldConstraints[tableName]?.[error.constraint]) {\n fieldName = adapter.fieldConstraints[tableName]?.[error.constraint]\n } else {\n const replacement = `${tableName}_`\n\n if (error.constraint.includes(replacement)) {\n const replacedConstraint = error.constraint.replace(replacement, '')\n\n if (replacedConstraint && adapter.fieldConstraints[tableName]?.[replacedConstraint]) {\n fieldName = adapter.fieldConstraints[tableName][replacedConstraint]\n }\n }\n }\n }\n\n if (!fieldName) {\n // Last case scenario we extract the key and value from the detail on the error\n const detail = error.detail\n const regex = /Key \\(([^)]+)\\)=\\(([^)]+)\\)/\n const match = detail.match(regex)\n\n if (match) {\n const key = match[1]\n\n fieldName = key\n }\n }\n\n throw new ValidationError(\n {\n id,\n errors: [\n {\n field: fieldName,\n message: req.t('error:valueMustBeUnique'),\n },\n ],\n },\n req.t,\n )\n } else {\n throw error\n }\n }\n\n if (ignoreResult) {\n return data as T\n }\n\n // //////////////////////////////////\n // RETRIEVE NEWLY UPDATED ROW\n // //////////////////////////////////\n\n const findManyArgs = buildFindManyArgs({\n adapter,\n depth: 0,\n fields,\n joinQuery,\n tableName,\n })\n\n findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id)\n\n const doc = await db.query[tableName].findFirst(findManyArgs)\n\n // //////////////////////////////////\n // TRANSFORM DATA\n // //////////////////////////////////\n\n const result = transform<T>({\n adapter,\n config: adapter.payload.config,\n data: doc,\n fields,\n joinQuery,\n })\n\n return result\n}\n"],"names":["eq","ValidationError","buildFindManyArgs","transform","transformForWrite","deleteExistingArrayRows","deleteExistingRowsByPath","insertArrays","upsertRow","id","adapter","data","db","fields","ignoreResult","joinQuery","operation","path","req","tableName","upsertTarget","where","rowToInsert","insertedRow","target","tables","row","insert","onConflictDoUpdate","set","values","localesToInsert","relationsToInsert","textsToInsert","numbersToInsert","blocksToInsert","selectsToInsert","Object","keys","locales","length","entries","forEach","locale","localeRow","_parentID","_locale","push","relationships","relation","parent","texts","textRow","numbers","numberRow","selects","selectTableName","selectRows","blocks","blockName","blockRow","uuid","localeTableName","localesSuffix","localeTable","deleteWhere","relationshipsTableName","relationshipsSuffix","localeColumnName","parentColumnName","parentID","pathColumnName","rows","relationshipsToDelete","textsTableName","numbersTableName","insertedBlockRows","blocksToDelete","blockTableName","tableNameMap","get","blockTable","blockRows","map","i","blockLocaleIndexMap","blockLocaleRowsToInsert","reduce","acc","blockLocale","blockLocaleData","arrays","parentRows","arrayTableName","tableRows","selectTable","error","code","fieldName","fieldConstraints","constraint","replacement","includes","replacedConstraint","replace","detail","regex","match","key","errors","field","message","t","findManyArgs","depth","doc","query","findFirst","result","config","payload"],"mappings":"AAEA,SAASA,EAAE,QAAQ,cAAa;AAChC,SAASC,eAAe,QAAQ,UAAS;AAKzC,SAASC,iBAAiB,QAAQ,+BAA8B;AAChE,SAASC,SAAS,QAAQ,6BAA4B;AACtD,SAASC,iBAAiB,QAAQ,8BAA6B;AAC/D,SAASC,uBAAuB,QAAQ,+BAA8B;AACtE,SAASC,wBAAwB,QAAQ,gCAA+B;AACxE,SAASC,YAAY,QAAQ,oBAAmB;AAEhD,OAAO,MAAMC,YAAY,OAAuD,EAC9EC,EAAE,EACFC,OAAO,EACPC,IAAI,EACJC,EAAE,EACFC,MAAM,EACNC,YAA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V,EAAE,EAAEc,YAAYd,EAAE;IAEpE,MAAM0G,MAAM,MAAMvG,GAAGwG,KAAK,CAACjG,UAAU,CAACkG,SAAS,CAACJ;IAEhD,qCAAqC;IACrC,iBAAiB;IACjB,qCAAqC;IAErC,MAAMK,SAASnH,UAAa;QAC1BO;QACA6G,QAAQ7G,QAAQ8G,OAAO,CAACD,MAAM;QAC9B5G,MAAMwG;QACNtG;QACAE;IACF;IAEA,OAAOuG;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insertArrays.d.ts","sourceRoot":"","sources":["../../src/upsertRow/insertArrays.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAErE,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,cAAc,CAAA;IACvB,MAAM,EAAE;QACN,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAAA;KACxC,EAAE,CAAA;IACH,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC,GAAG,kBAAkB,CAAA;IAClD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CACtC,CAAA;AAYD,eAAO,MAAM,YAAY,wCAA+C,IAAI,KAAG,OAAO,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"insertArrays.d.ts","sourceRoot":"","sources":["../../src/upsertRow/insertArrays.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAErE,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,cAAc,CAAA;IACvB,MAAM,EAAE;QACN,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAAA;KACxC,EAAE,CAAA;IACH,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC,GAAG,kBAAkB,CAAA;IAClD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CACtC,CAAA;AAYD,eAAO,MAAM,YAAY,wCAA+C,IAAI,KAAG,OAAO,CAAC,IAAI,CAoF1F,CAAA"}
|
|
@@ -26,7 +26,10 @@ export const insertArrays = async ({ adapter, arrays, db, parentRows })=>{
|
|
|
26
26
|
arrayRowLocaleData._locale = arrayRowLocale;
|
|
27
27
|
rowsByTable[tableName].locales.push(arrayRowLocaleData);
|
|
28
28
|
if (!arrayRow.row.id) {
|
|
29
|
-
arrayRowLocaleData._getParentID = (rows)=>
|
|
29
|
+
arrayRowLocaleData._getParentID = (rows)=>{
|
|
30
|
+
const { id } = rows.find((each)=>each._uuid === arrayRow.row._uuid);
|
|
31
|
+
return id;
|
|
32
|
+
};
|
|
30
33
|
}
|
|
31
34
|
});
|
|
32
35
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/upsertRow/insertArrays.ts"],"sourcesContent":["import type { ArrayRowToInsert } from '../transform/write/types.js'\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n parentRows: Record<string, unknown>[]\n}\n\ntype RowsByTable = {\n [tableName: string]: {\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n locales: Record<string, unknown>[]\n rows: Record<string, unknown>[]\n }\n}\n\nexport const insertArrays = async ({ adapter, arrays, db, parentRows }: Args): Promise<void> => {\n // Maintain a map of flattened rows by table\n const rowsByTable: RowsByTable = {}\n\n arrays.forEach((arraysByTable, parentRowIndex) => {\n Object.entries(arraysByTable).forEach(([tableName, arrayRows]) => {\n // If the table doesn't exist in map, initialize it\n if (!rowsByTable[tableName]) {\n rowsByTable[tableName] = {\n arrays: [],\n locales: [],\n rows: [],\n }\n }\n\n const parentID = parentRows[parentRowIndex].id\n\n // Add any sub arrays that need to be created\n // We will call this recursively below\n arrayRows.forEach((arrayRow, i) => {\n if (Object.keys(arrayRow.arrays).length > 0) {\n rowsByTable[tableName].arrays.push(arrayRow.arrays)\n }\n\n // Set up parent IDs for both row and locale row\n arrayRow.row._parentID = parentID\n rowsByTable[tableName].rows.push(arrayRow.row)\n\n Object.entries(arrayRow.locales).forEach(([arrayRowLocale, arrayRowLocaleData]) => {\n arrayRowLocaleData._parentID = arrayRow.row.id\n arrayRowLocaleData._locale = arrayRowLocale\n rowsByTable[tableName].locales.push(arrayRowLocaleData)\n if (!arrayRow.row.id) {\n arrayRowLocaleData._getParentID = (rows) => rows
|
|
1
|
+
{"version":3,"sources":["../../src/upsertRow/insertArrays.ts"],"sourcesContent":["import type { ArrayRowToInsert } from '../transform/write/types.js'\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n parentRows: Record<string, unknown>[]\n}\n\ntype RowsByTable = {\n [tableName: string]: {\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n locales: Record<string, unknown>[]\n rows: Record<string, unknown>[]\n }\n}\n\nexport const insertArrays = async ({ adapter, arrays, db, parentRows }: Args): Promise<void> => {\n // Maintain a map of flattened rows by table\n const rowsByTable: RowsByTable = {}\n\n arrays.forEach((arraysByTable, parentRowIndex) => {\n Object.entries(arraysByTable).forEach(([tableName, arrayRows]) => {\n // If the table doesn't exist in map, initialize it\n if (!rowsByTable[tableName]) {\n rowsByTable[tableName] = {\n arrays: [],\n locales: [],\n rows: [],\n }\n }\n\n const parentID = parentRows[parentRowIndex].id\n\n // Add any sub arrays that need to be created\n // We will call this recursively below\n arrayRows.forEach((arrayRow, i) => {\n if (Object.keys(arrayRow.arrays).length > 0) {\n rowsByTable[tableName].arrays.push(arrayRow.arrays)\n }\n\n // Set up parent IDs for both row and locale row\n arrayRow.row._parentID = parentID\n rowsByTable[tableName].rows.push(arrayRow.row)\n\n Object.entries(arrayRow.locales).forEach(([arrayRowLocale, arrayRowLocaleData]) => {\n arrayRowLocaleData._parentID = arrayRow.row.id\n arrayRowLocaleData._locale = arrayRowLocale\n rowsByTable[tableName].locales.push(arrayRowLocaleData)\n if (!arrayRow.row.id) {\n arrayRowLocaleData._getParentID = (rows: { _uuid: string; id: number }[]) => {\n const { id } = rows.find((each) => each._uuid === arrayRow.row._uuid)\n return id\n }\n }\n })\n })\n })\n })\n\n // Insert all corresponding arrays\n // (one insert per array table)\n for (const [tableName, row] of Object.entries(rowsByTable)) {\n // the nested arrays need the ID for the parentID foreign key\n let insertedRows: Args['parentRows']\n if (row.rows.length > 0) {\n insertedRows = await adapter.insert({\n db,\n tableName,\n values: row.rows,\n })\n }\n\n // Insert locale rows\n if (adapter.tables[`${tableName}${adapter.localesSuffix}`] && row.locales.length > 0) {\n if (!row.locales[0]._parentID) {\n row.locales = row.locales.map((localeRow) => {\n if (typeof localeRow._getParentID === 'function') {\n localeRow._parentID = localeRow._getParentID(insertedRows)\n delete localeRow._getParentID\n }\n return localeRow\n })\n }\n await adapter.insert({\n db,\n tableName: `${tableName}${adapter.localesSuffix}`,\n values: row.locales,\n })\n }\n\n // If there are sub arrays, call this function recursively\n if (row.arrays.length > 0) {\n await insertArrays({\n adapter,\n arrays: row.arrays,\n db,\n parentRows: insertedRows,\n })\n }\n }\n}\n"],"names":["insertArrays","adapter","arrays","db","parentRows","rowsByTable","forEach","arraysByTable","parentRowIndex","Object","entries","tableName","arrayRows","locales","rows","parentID","id","arrayRow","i","keys","length","push","row","_parentID","arrayRowLocale","arrayRowLocaleData","_locale","_getParentID","find","each","_uuid","insertedRows","insert","values","tables","localesSuffix","map","localeRow"],"mappings":"AAsBA,OAAO,MAAMA,eAAe,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4BACpE,OAAOd;wBACT;oBACF;gBACF;YACF;QACF;IACF;IAEA,kCAAkC;IAClC,+BAA+B;IAC/B,KAAK,MAAM,CAACL,WAAWW,IAAI,IAAIb,OAAOC,OAAO,CAACL,aAAc;QAC1D,6DAA6D;QAC7D,IAAI0B;QACJ,IAAIT,IAAIR,IAAI,CAACM,MAAM,GAAG,GAAG;YACvBW,eAAe,MAAM9B,QAAQ+B,MAAM,CAAC;gBAClC7B;gBACAQ;gBACAsB,QAAQX,IAAIR,IAAI;YAClB;QACF;QAEA,qBAAqB;QACrB,IAAIb,QAAQiC,MAAM,CAAC,CAAC,EAAEvB,UAAU,EAAEV,QAAQkC,aAAa,CAAC,CAAC,CAAC,IAAIb,IAAIT,OAAO,CAACO,MAAM,GAAG,GAAG;YACpF,IAAI,CAACE,IAAIT,OAAO,CAAC,EAAE,CAACU,SAAS,EAAE;gBAC7BD,IAAIT,OAAO,GAAGS,IAAIT,OAAO,CAACuB,GAAG,CAAC,CAACC;oBAC7B,IAAI,OAAOA,UAAUV,YAAY,KAAK,YAAY;wBAChDU,UAAUd,SAAS,GAAGc,UAAUV,YAAY,CAACI;wBAC7C,OAAOM,UAAUV,YAAY;oBAC/B;oBACA,OAAOU;gBACT;YACF;YACA,MAAMpC,QAAQ+B,MAAM,CAAC;gBACnB7B;gBACAQ,WAAW,CAAC,EAAEA,UAAU,EAAEV,QAAQkC,aAAa,CAAC,CAAC;gBACjDF,QAAQX,IAAIT,OAAO;YACrB;QACF;QAEA,0DAA0D;QAC1D,IAAIS,IAAIpB,MAAM,CAACkB,MAAM,GAAG,GAAG;YACzB,MAAMpB,aAAa;gBACjBC;gBACAC,QAAQoB,IAAIpB,MAAM;gBAClBC;gBACAC,YAAY2B;YACd;QACF;IACF;AACF,EAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SQL } from 'drizzle-orm';
|
|
2
|
-
import type { Field, PayloadRequest } from 'payload';
|
|
2
|
+
import type { Field, JoinQuery, PayloadRequest } from 'payload';
|
|
3
3
|
import type { DrizzleAdapter, DrizzleTransaction, GenericColumn } from '../types.js';
|
|
4
4
|
type BaseArgs = {
|
|
5
5
|
adapter: DrizzleAdapter;
|
|
@@ -11,18 +11,21 @@ type BaseArgs = {
|
|
|
11
11
|
* @default false
|
|
12
12
|
*/
|
|
13
13
|
ignoreResult?: boolean;
|
|
14
|
+
joinQuery?: JoinQuery;
|
|
14
15
|
path?: string;
|
|
15
16
|
req: PayloadRequest;
|
|
16
17
|
tableName: string;
|
|
17
18
|
};
|
|
18
19
|
type CreateArgs = {
|
|
19
20
|
id?: never;
|
|
21
|
+
joinQuery?: never;
|
|
20
22
|
operation: 'create';
|
|
21
23
|
upsertTarget?: never;
|
|
22
24
|
where?: never;
|
|
23
25
|
} & BaseArgs;
|
|
24
26
|
type UpdateArgs = {
|
|
25
27
|
id?: number | string;
|
|
28
|
+
joinQuery?: JoinQuery;
|
|
26
29
|
operation: 'update';
|
|
27
30
|
upsertTarget?: GenericColumn;
|
|
28
31
|
where?: SQL<unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/upsertRow/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/upsertRow/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAEpF,KAAK,QAAQ,GAAG;IACd,OAAO,EAAE,cAAc,CAAA;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC,GAAG,kBAAkB,CAAA;IAClD,MAAM,EAAE,KAAK,EAAE,CAAA;IACf;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,cAAc,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,KAAK,UAAU,GAAG;IAChB,EAAE,CAAC,EAAE,KAAK,CAAA;IACV,SAAS,CAAC,EAAE,KAAK,CAAA;IACjB,SAAS,EAAE,QAAQ,CAAA;IACnB,YAAY,CAAC,EAAE,KAAK,CAAA;IACpB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,GAAG,QAAQ,CAAA;AAEZ,KAAK,UAAU,GAAG;IAChB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACpB,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,SAAS,EAAE,QAAQ,CAAA;IACnB,YAAY,CAAC,EAAE,aAAa,CAAA;IAC5B,KAAK,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;CACrB,GAAG,QAAQ,CAAA;AAEZ,MAAM,MAAM,IAAI,GAAG,UAAU,GAAG,UAAU,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/upsertRow/types.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { Field, PayloadRequest } from 'payload'\n\nimport type { DrizzleAdapter, DrizzleTransaction, GenericColumn } from '../types.js'\n\ntype BaseArgs = {\n adapter: DrizzleAdapter\n data: Record<string, unknown>\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n fields: Field[]\n /**\n * When true, skips reading the data back from the database and returns the input data\n * @default false\n */\n ignoreResult?: boolean\n path?: string\n req: PayloadRequest\n tableName: string\n}\n\ntype CreateArgs = {\n id?: never\n operation: 'create'\n upsertTarget?: never\n where?: never\n} & BaseArgs\n\ntype UpdateArgs = {\n id?: number | string\n operation: 'update'\n upsertTarget?: GenericColumn\n where?: SQL<unknown>\n} & BaseArgs\n\nexport type Args = CreateArgs | UpdateArgs\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/upsertRow/types.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { Field, JoinQuery, PayloadRequest } from 'payload'\n\nimport type { DrizzleAdapter, DrizzleTransaction, GenericColumn } from '../types.js'\n\ntype BaseArgs = {\n adapter: DrizzleAdapter\n data: Record<string, unknown>\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n fields: Field[]\n /**\n * When true, skips reading the data back from the database and returns the input data\n * @default false\n */\n ignoreResult?: boolean\n joinQuery?: JoinQuery\n path?: string\n req: PayloadRequest\n tableName: string\n}\n\ntype CreateArgs = {\n id?: never\n joinQuery?: never\n operation: 'create'\n upsertTarget?: never\n where?: never\n} & BaseArgs\n\ntype UpdateArgs = {\n id?: number | string\n joinQuery?: JoinQuery\n operation: 'update'\n upsertTarget?: GenericColumn\n where?: SQL<unknown>\n} & BaseArgs\n\nexport type Args = CreateArgs | UpdateArgs\n"],"names":[],"mappings":"AAqCA,WAA0C"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Collection } from 'payload';
|
|
2
|
+
import type { DrizzleAdapter } from '../types.js';
|
|
3
|
+
export declare const getCollectionIdType: ({ adapter, collection, }: {
|
|
4
|
+
adapter: DrizzleAdapter;
|
|
5
|
+
collection: Collection;
|
|
6
|
+
}) => "number" | "text";
|
|
7
|
+
//# sourceMappingURL=getCollectionIdType.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCollectionIdType.d.ts","sourceRoot":"","sources":["../../src/utilities/getCollectionIdType.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEzC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AASjD,eAAO,MAAM,mBAAmB,6BAG7B;IACD,OAAO,EAAE,cAAc,CAAA;IACvB,UAAU,EAAE,UAAU,CAAA;CACvB,sBAEA,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const typeMap = {
|
|
2
|
+
number: 'number',
|
|
3
|
+
serial: 'number',
|
|
4
|
+
text: 'text',
|
|
5
|
+
uuid: 'text'
|
|
6
|
+
};
|
|
7
|
+
export const getCollectionIdType = ({ adapter, collection })=>{
|
|
8
|
+
return collection.customIDType ?? typeMap[adapter.idType];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=getCollectionIdType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/getCollectionIdType.ts"],"sourcesContent":["import type { Collection } from 'payload'\n\nimport type { DrizzleAdapter } from '../types.js'\n\nconst typeMap: Record<string, 'number' | 'text'> = {\n number: 'number',\n serial: 'number',\n text: 'text',\n uuid: 'text',\n}\n\nexport const getCollectionIdType = ({\n adapter,\n collection,\n}: {\n adapter: DrizzleAdapter\n collection: Collection\n}) => {\n return collection.customIDType ?? typeMap[adapter.idType]\n}\n"],"names":["typeMap","number","serial","text","uuid","getCollectionIdType","adapter","collection","customIDType","idType"],"mappings":"AAIA,MAAMA,UAA6C;IACjDC,QAAQ;IACRC,QAAQ;IACRC,MAAM;IACNC,MAAM;AACR;AAEA,OAAO,MAAMC,sBAAsB,CAAC,EAClCC,OAAO,EACPC,UAAU,EAIX;IACC,OAAOA,WAAWC,YAAY,IAAIR,OAAO,CAACM,QAAQG,MAAM,CAAC;AAC3D,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPolymorphicRelationship.d.ts","sourceRoot":"","sources":["../../src/utilities/isPolymorphicRelationship.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7C,eAAO,MAAM,yBAAyB,UAC7B,OAAO,KACb,KAAK,IAAI;IACV,UAAU,EAAE,cAAc,CAAA;IAC1B,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;CASvB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/isPolymorphicRelationship.ts"],"sourcesContent":["import type { CollectionSlug } from 'payload'\n\nexport const isPolymorphicRelationship = (\n value: unknown,\n): value is {\n relationTo: CollectionSlug\n value: number | string\n} => {\n return (\n value &&\n typeof value === 'object' &&\n 'relationTo' in value &&\n typeof value.relationTo === 'string' &&\n 'value' in value\n )\n}\n"],"names":["isPolymorphicRelationship","value","relationTo"],"mappings":"AAEA,OAAO,MAAMA,4BAA4B,CACvCC;IAKA,OACEA,SACA,OAAOA,UAAU,YACjB,gBAAgBA,SAChB,OAAOA,MAAMC,UAAU,KAAK,YAC5B,WAAWD;AAEf,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/drizzle",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.108",
|
|
4
4
|
"description": "A library of shared functions used by different payload database adapters",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"@types/pg": "8.10.2",
|
|
47
47
|
"@types/to-snake-case": "1.0.0",
|
|
48
48
|
"@payloadcms/eslint-config": "3.0.0-beta.97",
|
|
49
|
-
"payload": "3.0.0-beta.
|
|
49
|
+
"payload": "3.0.0-beta.108"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"payload": "3.0.0-beta.
|
|
52
|
+
"payload": "3.0.0-beta.108"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"build": "pnpm build:swc && pnpm build:types",
|