@payloadcms/db-postgres 3.0.0-alpha.49 → 3.0.0-alpha.50

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- {"version":3,"file":"deleteOne.d.ts","sourceRoot":"","sources":["../src/deleteOne.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAWjD,eAAO,MAAM,SAAS,EAAE,SAmCvB,CAAA"}
1
+ {"version":3,"file":"deleteOne.d.ts","sourceRoot":"","sources":["../src/deleteOne.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAajD,eAAO,MAAM,SAAS,EAAE,SAsDvB,CAAA"}
package/dist/deleteOne.js CHANGED
@@ -1,31 +1,58 @@
1
+ import { eq } from 'drizzle-orm';
1
2
  import toSnakeCase from 'to-snake-case';
2
3
  import { buildFindManyArgs } from './find/buildFindManyArgs.js';
3
4
  import buildQuery from './queries/buildQuery.js';
5
+ import { selectDistinct } from './queries/selectDistinct.js';
4
6
  import { transform } from './transform/read/index.js';
5
- export const deleteOne = async function deleteOne({ collection, req = {}, where: incomingWhere }) {
7
+ export const deleteOne = async function deleteOne({ collection: collectionSlug, req = {}, where: whereArg }) {
6
8
  const db = this.sessions[req.transactionID]?.db || this.drizzle;
7
- const collectionConfig = this.payload.collections[collection].config;
8
- const tableName = toSnakeCase(collection);
9
- const { where } = await buildQuery({
9
+ const collection = this.payload.collections[collectionSlug].config;
10
+ const tableName = toSnakeCase(collectionSlug);
11
+ let docToDelete;
12
+ const { joinAliases, joins, selectFields, where } = await buildQuery({
10
13
  adapter: this,
11
- fields: collectionConfig.fields,
14
+ fields: collection.fields,
15
+ locale: req.locale,
12
16
  tableName,
13
- where: incomingWhere
17
+ where: whereArg
14
18
  });
15
- const findManyArgs = buildFindManyArgs({
19
+ const selectDistinctResult = await selectDistinct({
16
20
  adapter: this,
17
- depth: 0,
18
- fields: collectionConfig.fields,
19
- tableName
21
+ chainedMethods: [
22
+ {
23
+ args: [
24
+ 1
25
+ ],
26
+ method: 'limit'
27
+ }
28
+ ],
29
+ db,
30
+ joinAliases,
31
+ joins,
32
+ selectFields,
33
+ tableName,
34
+ where
20
35
  });
21
- findManyArgs.where = where;
22
- const docToDelete = await db.query[tableName].findFirst(findManyArgs);
36
+ if (selectDistinctResult?.[0]?.id) {
37
+ docToDelete = await db.query[tableName].findFirst({
38
+ where: eq(this.tables[tableName].id, selectDistinctResult[0].id)
39
+ });
40
+ } else {
41
+ const findManyArgs = buildFindManyArgs({
42
+ adapter: this,
43
+ depth: 0,
44
+ fields: collection.fields,
45
+ tableName
46
+ });
47
+ findManyArgs.where = where;
48
+ docToDelete = await db.query[tableName].findFirst(findManyArgs);
49
+ }
23
50
  const result = transform({
24
51
  config: this.payload.config,
25
52
  data: docToDelete,
26
- fields: collectionConfig.fields
53
+ fields: collection.fields
27
54
  });
28
- await db.delete(this.tables[tableName]).where(where);
55
+ await db.delete(this.tables[tableName]).where(eq(this.tables[tableName].id, docToDelete.id));
29
56
  return result;
30
57
  };
31
58
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/deleteOne.ts"],"sourcesContent":["import type { DeleteOne } from 'payload/database'\nimport type { PayloadRequest } from 'payload/types'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { buildFindManyArgs } from './find/buildFindManyArgs.js'\nimport buildQuery from './queries/buildQuery.js'\nimport { transform } from './transform/read/index.js'\n\nexport const deleteOne: DeleteOne = async function deleteOne(\n this: PostgresAdapter,\n { collection, req = {} as PayloadRequest, where: incomingWhere },\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collectionConfig = this.payload.collections[collection].config\n const tableName = toSnakeCase(collection)\n\n const { where } = await buildQuery({\n adapter: this,\n fields: collectionConfig.fields,\n tableName,\n where: incomingWhere,\n })\n\n const findManyArgs = buildFindManyArgs({\n adapter: this,\n depth: 0,\n fields: collectionConfig.fields,\n tableName,\n })\n\n findManyArgs.where = where\n\n const docToDelete = await db.query[tableName].findFirst(findManyArgs)\n\n const result = transform({\n config: this.payload.config,\n data: docToDelete,\n fields: collectionConfig.fields,\n })\n\n await db.delete(this.tables[tableName]).where(where)\n\n return result\n}\n"],"names":["toSnakeCase","buildFindManyArgs","buildQuery","transform","deleteOne","collection","req","where","incomingWhere","db","sessions","transactionID","drizzle","collectionConfig","payload","collections","config","tableName","adapter","fields","findManyArgs","depth","docToDelete","query","findFirst","result","data","delete","tables"],"mappings":"AAGA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,iBAAiB,QAAQ,8BAA6B;AAC/D,OAAOC,gBAAgB,0BAAyB;AAChD,SAASC,SAAS,QAAQ,4BAA2B;AAErD,OAAO,MAAMC,YAAuB,eAAeA,UAEjD,EAAEC,UAAU,EAAEC,MAAM,CAAC,CAAmB,EAAEC,OAAOC,aAAa,EAAE;IAEhE,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACJ,IAAIK,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMC,mBAAmB,IAAI,CAACC,OAAO,CAACC,WAAW,CAACV,WAAW,CAACW,MAAM;IACpE,MAAMC,YAAYjB,YAAYK;IAE9B,MAAM,EAAEE,KAAK,EAAE,GAAG,MAAML,WAAW;QACjCgB,SAAS,IAAI;QACbC,QAAQN,iBAAiBM,MAAM;QAC/BF;QACAV,OAAOC;IACT;IAEA,MAAMY,eAAenB,kBAAkB;QACrCiB,SAAS,IAAI;QACbG,OAAO;QACPF,QAAQN,iBAAiBM,MAAM;QAC/BF;IACF;IAEAG,aAAab,KAAK,GAAGA;IAErB,MAAMe,cAAc,MAAMb,GAAGc,KAAK,CAACN,UAAU,CAACO,SAAS,CAACJ;IAExD,MAAMK,SAAStB,UAAU;QACvBa,QAAQ,IAAI,CAACF,OAAO,CAACE,MAAM;QAC3BU,MAAMJ;QACNH,QAAQN,iBAAiBM,MAAM;IACjC;IAEA,MAAMV,GAAGkB,MAAM,CAAC,IAAI,CAACC,MAAM,CAACX,UAAU,EAAEV,KAAK,CAACA;IAE9C,OAAOkB;AACT,EAAC"}
1
+ {"version":3,"sources":["../src/deleteOne.ts"],"sourcesContent":["import type { DeleteOne } from 'payload/database'\nimport type { PayloadRequest } from 'payload/types'\n\nimport { eq } from 'drizzle-orm'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { buildFindManyArgs } from './find/buildFindManyArgs.js'\nimport buildQuery from './queries/buildQuery.js'\nimport { selectDistinct } from './queries/selectDistinct.js'\nimport { transform } from './transform/read/index.js'\n\nexport const deleteOne: DeleteOne = async function deleteOne(\n this: PostgresAdapter,\n { collection: collectionSlug, req = {} as PayloadRequest, where: whereArg },\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n const tableName = toSnakeCase(collectionSlug)\n let docToDelete: Record<string, unknown>\n\n const { joinAliases, joins, selectFields, where } = await buildQuery({\n adapter: this,\n fields: collection.fields,\n locale: req.locale,\n tableName,\n where: whereArg,\n })\n\n const selectDistinctResult = await selectDistinct({\n adapter: this,\n chainedMethods: [{ args: [1], method: 'limit' }],\n db,\n joinAliases,\n joins,\n selectFields,\n tableName,\n where,\n })\n\n if (selectDistinctResult?.[0]?.id) {\n docToDelete = await db.query[tableName].findFirst({\n where: eq(this.tables[tableName].id, selectDistinctResult[0].id),\n })\n } else {\n const findManyArgs = buildFindManyArgs({\n adapter: this,\n depth: 0,\n fields: collection.fields,\n tableName,\n })\n\n findManyArgs.where = where\n\n docToDelete = await db.query[tableName].findFirst(findManyArgs)\n }\n\n const result = transform({\n config: this.payload.config,\n data: docToDelete,\n fields: collection.fields,\n })\n\n await db.delete(this.tables[tableName]).where(eq(this.tables[tableName].id, docToDelete.id))\n\n return result\n}\n"],"names":["eq","toSnakeCase","buildFindManyArgs","buildQuery","selectDistinct","transform","deleteOne","collection","collectionSlug","req","where","whereArg","db","sessions","transactionID","drizzle","payload","collections","config","tableName","docToDelete","joinAliases","joins","selectFields","adapter","fields","locale","selectDistinctResult","chainedMethods","args","method","id","query","findFirst","tables","findManyArgs","depth","result","data","delete"],"mappings":"AAGA,SAASA,EAAE,QAAQ,cAAa;AAChC,OAAOC,iBAAiB,gBAAe;AAIvC,SAASC,iBAAiB,QAAQ,8BAA6B;AAC/D,OAAOC,gBAAgB,0BAAyB;AAChD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,SAAS,QAAQ,4BAA2B;AAErD,OAAO,MAAMC,YAAuB,eAAeA,UAEjD,EAAEC,YAAYC,cAAc,EAAEC,MAAM,CAAC,CAAmB,EAAEC,OAAOC,QAAQ,EAAE;IAE3E,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACJ,IAAIK,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMR,aAAa,IAAI,CAACS,OAAO,CAACC,WAAW,CAACT,eAAe,CAACU,MAAM;IAClE,MAAMC,YAAYlB,YAAYO;IAC9B,IAAIY;IAEJ,MAAM,EAAEC,WAAW,EAAEC,KAAK,EAAEC,YAAY,EAAEb,KAAK,EAAE,GAAG,MAAMP,WAAW;QACnEqB,SAAS,IAAI;QACbC,QAAQlB,WAAWkB,MAAM;QACzBC,QAAQjB,IAAIiB,MAAM;QAClBP;QACAT,OAAOC;IACT;IAEA,MAAMgB,uBAAuB,MAAMvB,eAAe;QAChDoB,SAAS,IAAI;QACbI,gBAAgB;YAAC;gBAAEC,MAAM;oBAAC;iBAAE;gBAAEC,QAAQ;YAAQ;SAAE;QAChDlB;QACAS;QACAC;QACAC;QACAJ;QACAT;IACF;IAEA,IAAIiB,sBAAsB,CAAC,EAAE,EAAEI,IAAI;QACjCX,cAAc,MAAMR,GAAGoB,KAAK,CAACb,UAAU,CAACc,SAAS,CAAC;YAChDvB,OAAOV,GAAG,IAAI,CAACkC,MAAM,CAACf,UAAU,CAACY,EAAE,EAAEJ,oBAAoB,CAAC,EAAE,CAACI,EAAE;QACjE;IACF,OAAO;QACL,MAAMI,eAAejC,kBAAkB;YACrCsB,SAAS,IAAI;YACbY,OAAO;YACPX,QAAQlB,WAAWkB,MAAM;YACzBN;QACF;QAEAgB,aAAazB,KAAK,GAAGA;QAErBU,cAAc,MAAMR,GAAGoB,KAAK,CAACb,UAAU,CAACc,SAAS,CAACE;IACpD;IAEA,MAAME,SAAShC,UAAU;QACvBa,QAAQ,IAAI,CAACF,OAAO,CAACE,MAAM;QAC3BoB,MAAMlB;QACNK,QAAQlB,WAAWkB,MAAM;IAC3B;IAEA,MAAMb,GAAG2B,MAAM,CAAC,IAAI,CAACL,MAAM,CAACf,UAAU,EAAET,KAAK,CAACV,GAAG,IAAI,CAACkC,MAAM,CAACf,UAAU,CAACY,EAAE,EAAEX,YAAYW,EAAE;IAE1F,OAAOM;AACT,EAAC"}
@@ -1,3 +1,4 @@
1
+ import type { QueryPromise } from 'drizzle-orm';
1
2
  export type ChainedMethods = {
2
3
  args: unknown[];
3
4
  method: string;
@@ -7,9 +8,9 @@ export type ChainedMethods = {
7
8
  * @param methods
8
9
  * @param query
9
10
  */
10
- declare const chainMethods: ({ methods, query }: {
11
+ declare const chainMethods: <T>({ methods, query }: {
11
12
  methods: any;
12
13
  query: any;
13
- }) => Promise<unknown>;
14
+ }) => QueryPromise<T>;
14
15
  export { chainMethods };
15
16
  //# sourceMappingURL=chainMethods.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"chainMethods.d.ts","sourceRoot":"","sources":["../../src/find/chainMethods.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,OAAO,EAAE,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;CACf,EAAE,CAAA;AAEH;;;;GAIG;AACH,QAAA,MAAM,YAAY;;;MAAyB,QAAQ,OAAO,CAIzD,CAAA;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
1
+ {"version":3,"file":"chainMethods.d.ts","sourceRoot":"","sources":["../../src/find/chainMethods.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,OAAO,EAAE,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;CACf,EAAE,CAAA;AAEH;;;;GAIG;AACH,QAAA,MAAM,YAAY;;;MAA4B,aAAa,CAAC,CAI3D,CAAA;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/find/chainMethods.ts"],"sourcesContent":["export type ChainedMethods = {\n args: unknown[]\n method: string\n}[]\n\n/**\n * Call and returning methods that would normally be chained together but cannot be because of control logic\n * @param methods\n * @param query\n */\nconst chainMethods = ({ methods, query }): Promise<unknown> => {\n return methods.reduce((query, { args, method }) => {\n return query[method](...args)\n }, query)\n}\n\nexport { chainMethods }\n"],"names":["chainMethods","methods","query","reduce","args","method"],"mappings":"AAKA;;;;CAIC,GACD,MAAMA,eAAe,CAAC,EAAEC,OAAO,EAAEC,KAAK,EAAE;IACtC,OAAOD,QAAQE,MAAM,CAAC,CAACD,OAAO,EAAEE,IAAI,EAAEC,MAAM,EAAE;QAC5C,OAAOH,KAAK,CAACG,OAAO,IAAID;IAC1B,GAAGF;AACL;AAEA,SAASF,YAAY,GAAE"}
1
+ {"version":3,"sources":["../../src/find/chainMethods.ts"],"sourcesContent":["import type { QueryPromise } from 'drizzle-orm'\n\nexport type ChainedMethods = {\n args: unknown[]\n method: string\n}[]\n\n/**\n * Call and returning methods that would normally be chained together but cannot be because of control logic\n * @param methods\n * @param query\n */\nconst chainMethods = <T>({ methods, query }): QueryPromise<T> => {\n return methods.reduce((query, { args, method }) => {\n return query[method](...args)\n }, query)\n}\n\nexport { chainMethods }\n"],"names":["chainMethods","methods","query","reduce","args","method"],"mappings":"AAOA;;;;CAIC,GACD,MAAMA,eAAe,CAAI,EAAEC,OAAO,EAAEC,KAAK,EAAE;IACzC,OAAOD,QAAQE,MAAM,CAAC,CAACD,OAAO,EAAEE,IAAI,EAAEC,MAAM,EAAE;QAC5C,OAAOH,KAAK,CAACG,OAAO,IAAID;IAC1B,GAAGF;AACL;AAEA,SAASF,YAAY,GAAE"}
@@ -1 +1 @@
1
- {"version":3,"file":"findMany.d.ts","sourceRoot":"","sources":["../../src/find/findMany.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAChD,OAAO,KAAK,EAAE,KAAK,EAA8B,MAAM,eAAe,CAAA;AAItE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAQlD,KAAK,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,YAAY,CAAC,GAAG;IACzC,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,QAAQ,iHAYlB,IAAI;;;;;;;;;;;EA+KN,CAAA"}
1
+ {"version":3,"file":"findMany.d.ts","sourceRoot":"","sources":["../../src/find/findMany.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAChD,OAAO,KAAK,EAAE,KAAK,EAA8B,MAAM,eAAe,CAAA;AAItE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AASlD,KAAK,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,YAAY,CAAC,GAAG;IACzC,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,QAAQ,iHAYlB,IAAI;;;;;;;;;;;EAgKN,CAAA"}
@@ -1,5 +1,6 @@
1
1
  import { inArray, sql } from 'drizzle-orm';
2
2
  import buildQuery from '../queries/buildQuery.js';
3
+ import { selectDistinct } from '../queries/selectDistinct.js';
3
4
  import { transform } from '../transform/read/index.js';
4
5
  import { buildFindManyArgs } from './buildFindManyArgs.js';
5
6
  import { chainMethods } from './chainMethods.js';
@@ -12,7 +13,6 @@ export const findMany = async function find({ adapter, fields, limit: limitArg,
12
13
  let hasPrevPage;
13
14
  let hasNextPage;
14
15
  let pagingCounter;
15
- let selectDistinctResult;
16
16
  const { joinAliases, joins, orderBy, selectFields, where } = await buildQuery({
17
17
  adapter,
18
18
  fields,
@@ -38,52 +38,29 @@ export const findMany = async function find({ adapter, fields, limit: limitArg,
38
38
  fields,
39
39
  tableName
40
40
  });
41
- // only fetch IDs when a sort or where query is used that needs to be done on join tables, otherwise these can be done directly on the table in findMany
42
- if (Object.keys(joins).length > 0 || joinAliases.length > 0) {
43
- if (where) {
44
- selectDistinctMethods.push({
45
- args: [
46
- where
47
- ],
48
- method: 'where'
49
- });
50
- }
51
- joinAliases.forEach(({ condition, table })=>{
52
- selectDistinctMethods.push({
53
- args: [
54
- table,
55
- condition
56
- ],
57
- method: 'leftJoin'
58
- });
59
- });
60
- Object.entries(joins).forEach(([joinTable, condition])=>{
61
- if (joinTable) {
62
- selectDistinctMethods.push({
63
- args: [
64
- adapter.tables[joinTable],
65
- condition
66
- ],
67
- method: 'leftJoin'
68
- });
69
- }
70
- });
71
- selectDistinctMethods.push({
72
- args: [
73
- skip || (page - 1) * limit
74
- ],
75
- method: 'offset'
76
- });
77
- selectDistinctMethods.push({
78
- args: [
79
- limit === 0 ? undefined : limit
80
- ],
81
- method: 'limit'
82
- });
83
- selectDistinctResult = await chainMethods({
84
- methods: selectDistinctMethods,
85
- query: db.selectDistinct(selectFields).from(table)
86
- });
41
+ selectDistinctMethods.push({
42
+ args: [
43
+ skip || (page - 1) * limit
44
+ ],
45
+ method: 'offset'
46
+ });
47
+ selectDistinctMethods.push({
48
+ args: [
49
+ limit === 0 ? undefined : limit
50
+ ],
51
+ method: 'limit'
52
+ });
53
+ const selectDistinctResult = await selectDistinct({
54
+ adapter,
55
+ chainedMethods: selectDistinctMethods,
56
+ db,
57
+ joinAliases,
58
+ joins,
59
+ selectFields,
60
+ tableName,
61
+ where
62
+ });
63
+ if (selectDistinctResult) {
87
64
  if (selectDistinctResult.length === 0) {
88
65
  return {
89
66
  docs: [],
@@ -97,13 +74,14 @@ export const findMany = async function find({ adapter, fields, limit: limitArg,
97
74
  totalDocs: 0,
98
75
  totalPages: 0
99
76
  };
77
+ } else {
78
+ // set the id in an object for sorting later
79
+ selectDistinctResult.forEach(({ id }, i)=>{
80
+ orderedIDMap[id] = i;
81
+ });
82
+ orderedIDs = Object.keys(orderedIDMap);
83
+ findManyArgs.where = inArray(adapter.tables[tableName].id, orderedIDs);
100
84
  }
101
- // set the id in an object for sorting later
102
- selectDistinctResult.forEach(({ id }, i)=>{
103
- orderedIDMap[id] = i;
104
- });
105
- orderedIDs = Object.keys(orderedIDMap);
106
- findManyArgs.where = inArray(adapter.tables[tableName].id, orderedIDs);
107
85
  } else {
108
86
  findManyArgs.limit = limitArg === 0 ? undefined : limitArg;
109
87
  const offset = skip || (page - 1) * limitArg;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/find/findMany.ts"],"sourcesContent":["import type { FindArgs } from 'payload/database'\nimport type { Field, PayloadRequest, TypeWithID } from 'payload/types'\n\nimport { inArray, sql } from 'drizzle-orm'\n\nimport type { PostgresAdapter } from '../types.js'\nimport type { ChainedMethods } from './chainMethods.js'\n\nimport buildQuery from '../queries/buildQuery.js'\nimport { transform } from '../transform/read/index.js'\nimport { buildFindManyArgs } from './buildFindManyArgs.js'\nimport { chainMethods } from './chainMethods.js'\n\ntype Args = Omit<FindArgs, 'collection'> & {\n adapter: PostgresAdapter\n fields: Field[]\n tableName: string\n}\n\nexport const findMany = async function find({\n adapter,\n fields,\n limit: limitArg,\n locale,\n page = 1,\n pagination,\n req = {} as PayloadRequest,\n skip,\n sort,\n tableName,\n where: whereArg,\n}: Args) {\n const db = adapter.sessions[req.transactionID]?.db || adapter.drizzle\n const table = adapter.tables[tableName]\n\n const limit = limitArg ?? 10\n let totalDocs: number\n let totalPages: number\n let hasPrevPage: boolean\n let hasNextPage: boolean\n let pagingCounter: number\n let selectDistinctResult\n\n const { joinAliases, joins, orderBy, selectFields, where } = await buildQuery({\n adapter,\n fields,\n locale,\n sort,\n tableName,\n where: whereArg,\n })\n\n const orderedIDMap: Record<number | string, number> = {}\n let orderedIDs: (number | string)[]\n\n const selectDistinctMethods: ChainedMethods = []\n\n if (orderBy?.order && orderBy?.column) {\n selectDistinctMethods.push({\n args: [orderBy.order(orderBy.column)],\n method: 'orderBy',\n })\n }\n\n const findManyArgs = buildFindManyArgs({\n adapter,\n depth: 0,\n fields,\n tableName,\n })\n\n // only fetch IDs when a sort or where query is used that needs to be done on join tables, otherwise these can be done directly on the table in findMany\n if (Object.keys(joins).length > 0 || joinAliases.length > 0) {\n if (where) {\n selectDistinctMethods.push({ args: [where], method: 'where' })\n }\n\n joinAliases.forEach(({ condition, table }) => {\n selectDistinctMethods.push({\n args: [table, condition],\n method: 'leftJoin',\n })\n })\n\n Object.entries(joins).forEach(([joinTable, condition]) => {\n if (joinTable) {\n selectDistinctMethods.push({\n args: [adapter.tables[joinTable], condition],\n method: 'leftJoin',\n })\n }\n })\n\n selectDistinctMethods.push({ args: [skip || (page - 1) * limit], method: 'offset' })\n selectDistinctMethods.push({ args: [limit === 0 ? undefined : limit], method: 'limit' })\n\n selectDistinctResult = await chainMethods({\n methods: selectDistinctMethods,\n query: db.selectDistinct(selectFields).from(table),\n })\n\n if (selectDistinctResult.length === 0) {\n return {\n docs: [],\n hasNextPage: false,\n hasPrevPage: false,\n limit,\n nextPage: null,\n page: 1,\n pagingCounter: 0,\n prevPage: null,\n totalDocs: 0,\n totalPages: 0,\n }\n }\n // set the id in an object for sorting later\n selectDistinctResult.forEach(({ id }, i) => {\n orderedIDMap[id as number | string] = i\n })\n orderedIDs = Object.keys(orderedIDMap)\n findManyArgs.where = inArray(adapter.tables[tableName].id, orderedIDs)\n } else {\n findManyArgs.limit = limitArg === 0 ? undefined : limitArg\n\n const offset = skip || (page - 1) * limitArg\n\n if (!Number.isNaN(offset)) findManyArgs.offset = offset\n\n if (where) {\n findManyArgs.where = where\n }\n findManyArgs.orderBy = orderBy.order(orderBy.column)\n }\n\n const findPromise = db.query[tableName].findMany(findManyArgs)\n\n if (pagination !== false && (orderedIDs ? orderedIDs?.length >= limit : true)) {\n const selectCountMethods: ChainedMethods = []\n\n joinAliases.forEach(({ condition, table }) => {\n selectCountMethods.push({\n args: [table, condition],\n method: 'leftJoin',\n })\n })\n\n Object.entries(joins).forEach(([joinTable, condition]) => {\n if (joinTable) {\n selectCountMethods.push({\n args: [adapter.tables[joinTable], condition],\n method: 'leftJoin',\n })\n }\n })\n\n const countResult = await chainMethods({\n methods: selectCountMethods,\n query: db\n .select({\n count: sql<number>`count\n (DISTINCT ${adapter.tables[tableName].id})`,\n })\n .from(table)\n .where(where),\n })\n totalDocs = Number(countResult[0].count)\n totalPages = typeof limit === 'number' && limit !== 0 ? Math.ceil(totalDocs / limit) : 1\n hasPrevPage = page > 1\n hasNextPage = totalPages > page\n pagingCounter = (page - 1) * limit + 1\n }\n\n const rawDocs = await findPromise\n // sort rawDocs from selectQuery\n if (Object.keys(orderedIDMap).length > 0) {\n rawDocs.sort((a, b) => orderedIDMap[a.id] - orderedIDMap[b.id])\n }\n\n if (pagination === false || !totalDocs) {\n totalDocs = rawDocs.length\n totalPages = 1\n pagingCounter = 1\n hasPrevPage = false\n hasNextPage = false\n }\n\n const docs = rawDocs.map((data: TypeWithID) => {\n return transform({\n config: adapter.payload.config,\n data,\n fields,\n })\n })\n\n return {\n docs,\n hasNextPage,\n hasPrevPage,\n limit,\n nextPage: hasNextPage ? page + 1 : null,\n page,\n pagingCounter,\n prevPage: hasPrevPage ? page - 1 : null,\n totalDocs,\n totalPages,\n }\n}\n"],"names":["inArray","sql","buildQuery","transform","buildFindManyArgs","chainMethods","findMany","find","adapter","fields","limit","limitArg","locale","page","pagination","req","skip","sort","tableName","where","whereArg","db","sessions","transactionID","drizzle","table","tables","totalDocs","totalPages","hasPrevPage","hasNextPage","pagingCounter","selectDistinctResult","joinAliases","joins","orderBy","selectFields","orderedIDMap","orderedIDs","selectDistinctMethods","order","column","push","args","method","findManyArgs","depth","Object","keys","length","forEach","condition","entries","joinTable","undefined","methods","query","selectDistinct","from","docs","nextPage","prevPage","id","i","offset","Number","isNaN","findPromise","selectCountMethods","countResult","select","count","Math","ceil","rawDocs","a","b","map","data","config","payload"],"mappings":"AAGA,SAASA,OAAO,EAAEC,GAAG,QAAQ,cAAa;AAK1C,OAAOC,gBAAgB,2BAA0B;AACjD,SAASC,SAAS,QAAQ,6BAA4B;AACtD,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,YAAY,QAAQ,oBAAmB;AAQhD,OAAO,MAAMC,WAAW,eAAeC,KAAK,EAC1CC,OAAO,EACPC,MAAM,EACNC,OAAOC,QAAQ,EACfC,MAAM,EACNC,OAAO,CAAC,EACRC,UAAU,EACVC,MAAM,CAAC,CAAmB,EAC1BC,IAAI,EACJC,IAAI,EACJC,SAAS,EACTC,OAAOC,QAAQ,EACV;IACL,MAAMC,KAAKb,QAAQc,QAAQ,CAACP,IAAIQ,aAAa,CAAC,EAAEF,MAAMb,QAAQgB,OAAO;IACrE,MAAMC,QAAQjB,QAAQkB,MAAM,CAACR,UAAU;IAEvC,MAAMR,QAAQC,YAAY;IAC1B,IAAIgB;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IAEJ,MAAM,EAAEC,WAAW,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAEjB,KAAK,EAAE,GAAG,MAAMjB,WAAW;QAC5EM;QACAC;QACAG;QACAK;QACAC;QACAC,OAAOC;IACT;IAEA,MAAMiB,eAAgD,CAAC;IACvD,IAAIC;IAEJ,MAAMC,wBAAwC,EAAE;IAEhD,I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;YACF;QACF;QAEA,MAAMyB,cAAc,MAAMhE,aAAa;YACrCkD,SAASa;YACTZ,OAAOnC,GACJiD,MAAM,CAAC;gBACNC,OAAOtE,GAAW,CAAC;wBACL,EAAEO,QAAQkB,MAAM,CAACR,UAAU,CAAC4C,EAAE,CAAC,CAAC,CAAC;YACjD,GACCJ,IAAI,CAACjC,OACLN,KAAK,CAACA;QACX;QACAQ,YAAYsC,OAAOI,WAAW,CAAC,EAAE,CAACE,KAAK;QACvC3C,aAAa,OAAOlB,UAAU,YAAYA,UAAU,IAAI8D,KAAKC,IAAI,CAAC9C,YAAYjB,SAAS;QACvFmB,cAAchB,OAAO;QACrBiB,cAAcF,aAAaf;QAC3BkB,gBAAgB,AAAClB,CAAAA,OAAO,CAAA,IAAKH,QAAQ;IACvC;IAEA,MAAMgE,UAAU,MAAMP;IACtB,gCAAgC;IAChC,IAAIpB,OAAOC,IAAI,CAACX,cAAcY,MAAM,GAAG,GAAG;QACxCyB,QAAQzD,IAAI,CAAC,CAAC0D,GAAGC,IAAMvC,YAAY,CAACsC,EAAEb,EAAE,CAAC,GAAGzB,YAAY,CAACuC,EAAEd,EAAE,CAAC;IAChE;IAEA,IAAIhD,eAAe,SAAS,CAACa,WAAW;QACtCA,YAAY+C,QAAQzB,MAAM;QAC1BrB,aAAa;QACbG,gBAAgB;QAChBF,cAAc;QACdC,cAAc;IAChB;IAEA,MAAM6B,OAAOe,QAAQG,GAAG,CAAC,CAACC;QACxB,OAAO3E,UAAU;YACf4E,QAAQvE,QAAQwE,OAAO,CAACD,MAAM;YAC9BD;YACArE;QACF;IACF;IAEA,OAAO;QACLkD;QACA7B;QACAD;QACAnB;QACAkD,UAAU9B,cAAcjB,OAAO,IAAI;QACnCA;QACAkB;QACA8B,UAAUhC,cAAchB,OAAO,IAAI;QACnCc;QACAC;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/find/findMany.ts"],"sourcesContent":["import type { FindArgs } from 'payload/database'\nimport type { Field, PayloadRequest, TypeWithID } from 'payload/types'\n\nimport { inArray, sql } from 'drizzle-orm'\n\nimport type { PostgresAdapter } from '../types.js'\nimport type { ChainedMethods } from './chainMethods.js'\n\nimport buildQuery from '../queries/buildQuery.js'\nimport { selectDistinct } from '../queries/selectDistinct.js'\nimport { transform } from '../transform/read/index.js'\nimport { buildFindManyArgs } from './buildFindManyArgs.js'\nimport { chainMethods } from './chainMethods.js'\n\ntype Args = Omit<FindArgs, 'collection'> & {\n adapter: PostgresAdapter\n fields: Field[]\n tableName: string\n}\n\nexport const findMany = async function find({\n adapter,\n fields,\n limit: limitArg,\n locale,\n page = 1,\n pagination,\n req = {} as PayloadRequest,\n skip,\n sort,\n tableName,\n where: whereArg,\n}: Args) {\n const db = adapter.sessions[req.transactionID]?.db || adapter.drizzle\n const table = adapter.tables[tableName]\n\n const limit = limitArg ?? 10\n let totalDocs: number\n let totalPages: number\n let hasPrevPage: boolean\n let hasNextPage: boolean\n let pagingCounter: number\n\n const { joinAliases, joins, orderBy, selectFields, where } = await buildQuery({\n adapter,\n fields,\n locale,\n sort,\n tableName,\n where: whereArg,\n })\n\n const orderedIDMap: Record<number | string, number> = {}\n let orderedIDs: (number | string)[]\n\n const selectDistinctMethods: ChainedMethods = []\n\n if (orderBy?.order && orderBy?.column) {\n selectDistinctMethods.push({\n args: [orderBy.order(orderBy.column)],\n method: 'orderBy',\n })\n }\n\n const findManyArgs = buildFindManyArgs({\n adapter,\n depth: 0,\n fields,\n tableName,\n })\n\n selectDistinctMethods.push({ args: [skip || (page - 1) * limit], method: 'offset' })\n selectDistinctMethods.push({ args: [limit === 0 ? undefined : limit], method: 'limit' })\n\n const selectDistinctResult = await selectDistinct({\n adapter,\n chainedMethods: selectDistinctMethods,\n db,\n joinAliases,\n joins,\n selectFields,\n tableName,\n where,\n })\n\n if (selectDistinctResult) {\n if (selectDistinctResult.length === 0) {\n return {\n docs: [],\n hasNextPage: false,\n hasPrevPage: false,\n limit,\n nextPage: null,\n page: 1,\n pagingCounter: 0,\n prevPage: null,\n totalDocs: 0,\n totalPages: 0,\n }\n } else {\n // set the id in an object for sorting later\n selectDistinctResult.forEach(({ id }, i) => {\n orderedIDMap[id] = i\n })\n orderedIDs = Object.keys(orderedIDMap)\n findManyArgs.where = inArray(adapter.tables[tableName].id, orderedIDs)\n }\n } else {\n findManyArgs.limit = limitArg === 0 ? undefined : limitArg\n\n const offset = skip || (page - 1) * limitArg\n\n if (!Number.isNaN(offset)) findManyArgs.offset = offset\n\n if (where) {\n findManyArgs.where = where\n }\n findManyArgs.orderBy = orderBy.order(orderBy.column)\n }\n\n const findPromise = db.query[tableName].findMany(findManyArgs)\n\n if (pagination !== false && (orderedIDs ? orderedIDs?.length >= limit : true)) {\n const selectCountMethods: ChainedMethods = []\n\n joinAliases.forEach(({ condition, table }) => {\n selectCountMethods.push({\n args: [table, condition],\n method: 'leftJoin',\n })\n })\n\n Object.entries(joins).forEach(([joinTable, condition]) => {\n if (joinTable) {\n selectCountMethods.push({\n args: [adapter.tables[joinTable], condition],\n method: 'leftJoin',\n })\n }\n })\n\n const countResult = await chainMethods({\n methods: selectCountMethods,\n query: db\n .select({\n count: sql<number>`count\n (DISTINCT ${adapter.tables[tableName].id})`,\n })\n .from(table)\n .where(where),\n })\n totalDocs = Number(countResult[0].count)\n totalPages = typeof limit === 'number' && limit !== 0 ? Math.ceil(totalDocs / limit) : 1\n hasPrevPage = page > 1\n hasNextPage = totalPages > page\n pagingCounter = (page - 1) * limit + 1\n }\n\n const rawDocs = await findPromise\n // sort rawDocs from selectQuery\n if (Object.keys(orderedIDMap).length > 0) {\n rawDocs.sort((a, b) => orderedIDMap[a.id] - orderedIDMap[b.id])\n }\n\n if (pagination === false || !totalDocs) {\n totalDocs = rawDocs.length\n totalPages = 1\n pagingCounter = 1\n hasPrevPage = false\n hasNextPage = false\n }\n\n const docs = rawDocs.map((data: TypeWithID) => {\n return transform({\n config: adapter.payload.config,\n data,\n fields,\n })\n })\n\n return {\n docs,\n hasNextPage,\n hasPrevPage,\n limit,\n nextPage: hasNextPage ? page + 1 : null,\n page,\n pagingCounter,\n prevPage: hasPrevPage ? page - 1 : null,\n totalDocs,\n totalPages,\n }\n}\n"],"names":["inArray","sql","buildQuery","selectDistinct","transform","buildFindManyArgs","chainMethods","findMany","find","adapter","fields","limit","limitArg","locale","page","pagination","req","skip","sort","tableName","where","whereArg","db","sessions","transactionID","drizzle","table","tables","totalDocs","totalPages","hasPrevPage","hasNextPage","pagingCounter","joinAliases","joins","orderBy","selectFields","orderedIDMap","orderedIDs","selectDistinctMethods","order","column","push","args","method","findManyArgs","depth","undefined","selectDistinctResult","chainedMethods","length","docs","nextPage","prevPage","forEach","id","i","Object","keys","offset","Number","isNaN","findPromise","query","selectCountMethods","condition","entries","joinTable","countResult","methods","select","count","from","Math","ceil","rawDocs","a","b","map","data","config","payload"],"mappings":"AAGA,SAASA,OAAO,EAAEC,GAAG,QAAQ,cAAa;AAK1C,OAAOC,gBAAgB,2BAA0B;AACjD,SAASC,cAAc,QAAQ,+BAA8B;AAC7D,SAASC,SAAS,QAAQ,6BAA4B;AACtD,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,YAAY,QAAQ,oBAAmB;AAQhD,OAAO,MAAMC,WAAW,eAAeC,KAAK,EAC1CC,OAAO,EACPC,MAAM,EACNC,OAAOC,QAAQ,EACfC,MAAM,EACNC,OAAO,CAAC,EACRC,UAAU,EACVC,MAAM,CAAC,CAAmB,EAC1BC,IAAI,EACJC,IAAI,EACJC,SAAS,EACTC,OAAOC,QAAQ,EACV;IACL,MAAMC,KAAKb,QAAQc,QAAQ,CAACP,IAAIQ,aAAa,CAAC,EAAEF,MAAMb,QAAQgB,OAAO;IACrE,MAAMC,QAAQjB,QAAQkB,MAAM,CAACR,UAAU;IAEvC,MAAMR,QAAQC,YAAY;IAC1B,IAAIgB;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IAEJ,MAAM,EAAEC,WAAW,EAAEC,KAAK,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AS;QACvFmB,cAAchB,OAAO;QACrBiB,cAAcF,aAAaf;QAC3BkB,gBAAgB,AAAClB,CAAAA,OAAO,CAAA,IAAKH,QAAQ;IACvC;IAEA,MAAMgE,UAAU,MAAMb;IACtB,gCAAgC;IAChC,IAAIL,OAAOC,IAAI,CAACrB,cAAca,MAAM,GAAG,GAAG;QACxCyB,QAAQzD,IAAI,CAAC,CAAC0D,GAAGC,IAAMxC,YAAY,CAACuC,EAAErB,EAAE,CAAC,GAAGlB,YAAY,CAACwC,EAAEtB,EAAE,CAAC;IAChE;IAEA,IAAIxC,eAAe,SAAS,CAACa,WAAW;QACtCA,YAAY+C,QAAQzB,MAAM;QAC1BrB,aAAa;QACbG,gBAAgB;QAChBF,cAAc;QACdC,cAAc;IAChB;IAEA,MAAMoB,OAAOwB,QAAQG,GAAG,CAAC,CAACC;QACxB,OAAO3E,UAAU;YACf4E,QAAQvE,QAAQwE,OAAO,CAACD,MAAM;YAC9BD;YACArE;QACF;IACF;IAEA,OAAO;QACLyC;QACApB;QACAD;QACAnB;QACAyC,UAAUrB,cAAcjB,OAAO,IAAI;QACnCA;QACAkB;QACAqB,UAAUvB,cAAchB,OAAO,IAAI;QACnCc;QACAC;IACF;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"sanitizeQueryValue.d.ts","sourceRoot":"","sources":["../../src/queries/sanitizeQueryValue.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,UAAU,EAAoB,MAAM,eAAe,CAAA;AAG7E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAElD,KAAK,sBAAsB,GAAG;IAC5B,OAAO,EAAE,eAAe,CAAA;IACxB,KAAK,EAAE,KAAK,GAAG,UAAU,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;IACtB,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,eAAO,MAAM,kBAAkB,oEAM5B,sBAAsB;cAAe,MAAM;WAAS,OAAO;CAqF7D,CAAA"}
1
+ {"version":3,"file":"sanitizeQueryValue.d.ts","sourceRoot":"","sources":["../../src/queries/sanitizeQueryValue.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,UAAU,EAAoB,MAAM,eAAe,CAAA;AAG7E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAElD,KAAK,sBAAsB,GAAG;IAC5B,OAAO,EAAE,eAAe,CAAA;IACxB,KAAK,EAAE,KAAK,GAAG,UAAU,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;IACtB,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,eAAO,MAAM,kBAAkB,oEAM5B,sBAAsB;cAAe,MAAM;WAAS,OAAO;CAyF7D,CAAA"}
@@ -44,7 +44,7 @@ export const sanitizeQueryValue = ({ adapter, field, operator: operatorArg, rela
44
44
  if (field.type === 'number' && typeof formattedValue === 'string') {
45
45
  formattedValue = Number(val);
46
46
  }
47
- if (field.type === 'date') {
47
+ if (field.type === 'date' && operator !== 'exists') {
48
48
  if (typeof val === 'string') {
49
49
  formattedValue = new Date(val);
50
50
  if (Number.isNaN(Date.parse(formattedValue))) {
@@ -66,6 +66,9 @@ export const sanitizeQueryValue = ({ adapter, field, operator: operatorArg, rela
66
66
  formattedValue = null;
67
67
  }
68
68
  }
69
+ if ('hasMany' in field && field.hasMany && operator === 'contains') {
70
+ operator = 'equals';
71
+ }
69
72
  if (operator === 'near' || operator === 'within' || operator === 'intersects') {
70
73
  throw new APIError(`Querying with '${operator}' is not supported with the postgres database adapter.`);
71
74
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/queries/sanitizeQueryValue.ts"],"sourcesContent":["import { APIError } from 'payload/errors'\nimport { type Field, type TabAsField, fieldAffectsData } from 'payload/types'\nimport { createArrayFromCommaDelineated } from 'payload/utilities'\n\nimport type { PostgresAdapter } from '../types.js'\n\ntype SanitizeQueryValueArgs = {\n adapter: PostgresAdapter\n field: Field | TabAsField\n operator: string\n relationOrPath: string\n val: any\n}\n\nexport const sanitizeQueryValue = ({\n adapter,\n field,\n operator: operatorArg,\n relationOrPath,\n val,\n}: SanitizeQueryValueArgs): { operator: string; value: unknown } => {\n let operator = operatorArg\n let formattedValue = val\n\n if (!fieldAffectsData(field)) return { operator, value: formattedValue }\n\n if (\n (field.type === 'relationship' || field.type === 'upload') &&\n !relationOrPath.endsWith('relationTo') &&\n Array.isArray(formattedValue)\n ) {\n const allPossibleIDTypes: (number | string)[] = []\n formattedValue.forEach((val) => {\n if (adapter.idType !== 'uuid' && typeof val === 'string') {\n allPossibleIDTypes.push(val, parseInt(val))\n } else if (typeof val === 'string') {\n allPossibleIDTypes.push(val)\n } else {\n allPossibleIDTypes.push(val, String(val))\n }\n })\n formattedValue = allPossibleIDTypes\n }\n\n // Cast incoming values as proper searchable types\n if (field.type === 'checkbox' && typeof val === 'string') {\n if (val.toLowerCase() === 'true') formattedValue = true\n if (val.toLowerCase() === 'false') formattedValue = false\n }\n\n if (['all', 'in', 'not_in'].includes(operator)) {\n if (typeof formattedValue === 'string') {\n formattedValue = createArrayFromCommaDelineated(formattedValue)\n\n if (field.type === 'number') {\n formattedValue = formattedValue.map((arrayVal) => parseFloat(arrayVal))\n }\n }\n\n if (!Array.isArray(formattedValue) || formattedValue.length === 0) {\n return null\n }\n }\n\n if (field.type === 'number' && typeof formattedValue === 'string') {\n formattedValue = Number(val)\n }\n\n if (field.type === 'date') {\n if (typeof val === 'string') {\n formattedValue = new Date(val)\n if (Number.isNaN(Date.parse(formattedValue))) {\n return { operator, value: undefined }\n }\n }\n\n if (typeof val === 'number') {\n formattedValue = new Date(val)\n }\n }\n\n if (['relationship', 'upload'].includes(field.type)) {\n if (val === 'null') {\n formattedValue = null\n }\n }\n\n if (operator === 'near' || operator === 'within' || operator === 'intersects') {\n throw new APIError(\n `Querying with '${operator}' is not supported with the postgres database adapter.`,\n )\n }\n\n if (operator === 'contains') {\n formattedValue = `%${formattedValue}%`\n }\n\n if (operator === 'exists') {\n formattedValue = formattedValue === 'true' || formattedValue === true\n if (formattedValue === false) {\n operator = 'isNull'\n }\n }\n\n return { operator, value: formattedValue }\n}\n"],"names":["APIError","fieldAffectsData","createArrayFromCommaDelineated","sanitizeQueryValue","adapter","field","operator","operatorArg","relationOrPath","val","formattedValue","value","type","endsWith","Array","isArray","allPossibleIDTypes","forEach","idType","push","parseInt","String","toLowerCase","includes","map","arrayVal","parseFloat","length","Number","Date","isNaN","parse","undefined"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAAsCC,gBAAgB,QAAQ,gBAAe;AAC7E,SAASC,8BAA8B,QAAQ,oBAAmB;AAYlE,OAAO,MAAMC,qBAAqB,CAAC,EACjCC,OAAO,EACPC,KAAK,EACLC,UAAUC,WAAW,EACrBC,cAAc,EACdC,GAAG,EACoB;IACvB,IAAIH,WAAWC;IACf,IAAIG,iBAAiBD;IAErB,IAAI,CAACR,iBAAiBI,QAAQ,OAAO;QAAEC;QAAUK,OAAOD;IAAe;IAEvE,IACE,AAACL,CAAAA,MAAMO,IAAI,KAAK,kBAAkBP,MAAMO,IAAI,KAAK,QAAO,KACxD,CAACJ,eAAeK,QAAQ,CAAC,iBACzBC,MAAMC,OAAO,CAACL,iBACd;QACA,MAAMM,qBAA0C,EAAE;QAClDN,eAAeO,OAAO,CAAC,CAACR;YACtB,IAAIL,QAAQc,MAAM,KAAK,UAAU,OAAOT,QAAQ,UAAU;gBACxDO,mBAAmBG,IAAI,CAACV,KAAKW,SAASX;YACxC,OAAO,IAAI,OAAOA,QAAQ,UAAU;gBAClCO,mBAAmBG,IAAI,CAACV;YAC1B,OAAO;gBACLO,mBAAmBG,IAAI,CAACV,KAAKY,OAAOZ;YACtC;QACF;QACAC,iBAAiBM;IACnB;IAEA,kDAAkD;IAClD,IAAIX,MAAMO,IAAI,KAAK,c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BA,mBAAmB,UAAUA,mBAAmB;QACjE,IAAIA,mBAAmB,OAAO;YAC5BJ,WAAW;QACb;IACF;IAEA,OAAO;QAAEA;QAAUK,OAAOD;IAAe;AAC3C,EAAC"}
1
+ {"version":3,"sources":["../../src/queries/sanitizeQueryValue.ts"],"sourcesContent":["import { APIError } from 'payload/errors'\nimport { type Field, type TabAsField, fieldAffectsData } from 'payload/types'\nimport { createArrayFromCommaDelineated } from 'payload/utilities'\n\nimport type { PostgresAdapter } from '../types.js'\n\ntype SanitizeQueryValueArgs = {\n adapter: PostgresAdapter\n field: Field | TabAsField\n operator: string\n relationOrPath: string\n val: any\n}\n\nexport const sanitizeQueryValue = ({\n adapter,\n field,\n operator: operatorArg,\n relationOrPath,\n val,\n}: SanitizeQueryValueArgs): { operator: string; value: unknown } => {\n let operator = operatorArg\n let formattedValue = val\n\n if (!fieldAffectsData(field)) return { operator, value: formattedValue }\n\n if (\n (field.type === 'relationship' || field.type === 'upload') &&\n !relationOrPath.endsWith('relationTo') &&\n Array.isArray(formattedValue)\n ) {\n const allPossibleIDTypes: (number | string)[] = []\n formattedValue.forEach((val) => {\n if (adapter.idType !== 'uuid' && typeof val === 'string') {\n allPossibleIDTypes.push(val, parseInt(val))\n } else if (typeof val === 'string') {\n allPossibleIDTypes.push(val)\n } else {\n allPossibleIDTypes.push(val, String(val))\n }\n })\n formattedValue = allPossibleIDTypes\n }\n\n // Cast incoming values as proper searchable types\n if (field.type === 'checkbox' && typeof val === 'string') {\n if (val.toLowerCase() === 'true') formattedValue = true\n if (val.toLowerCase() === 'false') formattedValue = false\n }\n\n if (['all', 'in', 'not_in'].includes(operator)) {\n if (typeof formattedValue === 'string') {\n formattedValue = createArrayFromCommaDelineated(formattedValue)\n\n if (field.type === 'number') {\n formattedValue = formattedValue.map((arrayVal) => parseFloat(arrayVal))\n }\n }\n\n if (!Array.isArray(formattedValue) || formattedValue.length === 0) {\n return null\n }\n }\n\n if (field.type === 'number' && typeof formattedValue === 'string') {\n formattedValue = Number(val)\n }\n\n if (field.type === 'date' && operator !== 'exists') {\n if (typeof val === 'string') {\n formattedValue = new Date(val)\n if (Number.isNaN(Date.parse(formattedValue))) {\n return { operator, value: undefined }\n }\n }\n\n if (typeof val === 'number') {\n formattedValue = new Date(val)\n }\n }\n\n if (['relationship', 'upload'].includes(field.type)) {\n if (val === 'null') {\n formattedValue = null\n }\n }\n\n if ('hasMany' in field && field.hasMany && operator === 'contains') {\n operator = 'equals'\n }\n\n if (operator === 'near' || operator === 'within' || operator === 'intersects') {\n throw new APIError(\n `Querying with '${operator}' is not supported with the postgres database adapter.`,\n )\n }\n\n if (operator === 'contains') {\n formattedValue = `%${formattedValue}%`\n }\n\n if (operator === 'exists') {\n formattedValue = formattedValue === 'true' || formattedValue === true\n if (formattedValue === false) {\n operator = 'isNull'\n }\n }\n\n return { operator, value: formattedValue }\n}\n"],"names":["APIError","fieldAffectsData","createArrayFromCommaDelineated","sanitizeQueryValue","adapter","field","operator","operatorArg","relationOrPath","val","formattedValue","value","type","endsWith","Array","isArray","allPossibleIDTypes","forEach","idType","push","parseInt","String","toLowerCase","includes","map","arrayVal","parseFloat","length","Number","Date","isNaN","parse","undefined","hasMany"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAAsCC,gBAAgB,QAAQ,gBAAe;AAC7E,SAASC,8BAA8B,QAAQ,oBAAmB;AAYlE,OAAO,MAAMC,qBAAqB,CAAC,EACjCC,OAAO,EACPC,KAAK,EACLC,UAAUC,WAAW,EACrBC,cAAc,EACdC,GAAG,EACoB;IACvB,IAAIH,WAAWC;IACf,IAAIG,iBAAiBD;IAErB,IAAI,CAACR,iBAAiBI,QAAQ,OAAO;QAAEC;QAAUK,OAAOD;IAAe;IAEvE,IACE,AAACL,CAAAA,MAAMO,IAAI,KAAK,kBAAkBP,MAAMO,IAAI,KAAK,QAAO,KACxD,CAACJ,eAAeK,QAAQ,CAAC,iBACzBC,MAAMC,OAAO,CAACL,iBACd;QACA,M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AEA,IAAI;QAAC;QAAgB;KAAS,CAACc,QAAQ,CAAClB,MAAMO,IAAI,GAAG;QACnD,IAAIH,QAAQ,QAAQ;YAClBC,iBAAiB;QACnB;IACF;IAEA,IAAI,aAAaL,SAASA,MAAM4B,OAAO,IAAI3B,aAAa,YAAY;QAClEA,WAAW;IACb;IAEA,IAAIA,aAAa,UAAUA,aAAa,YAAYA,aAAa,cAAc;QAC7E,MAAM,IAAIN,SACR,CAAC,eAAe,EAAEM,SAAS,sDAAsD,CAAC;IAEtF;IAEA,IAAIA,aAAa,YAAY;QAC3BI,iBAAiB,CAAC,CAAC,EAAEA,eAAe,CAAC,CAAC;IACxC;IAEA,IAAIJ,aAAa,UAAU;QACzBI,iBAAiBA,mBAAmB,UAAUA,mBAAmB;QACjE,IAAIA,mBAAmB,OAAO;YAC5BJ,WAAW;QACb;IACF;IAEA,OAAO;QAAEA;QAAUK,OAAOD;IAAe;AAC3C,EAAC"}
@@ -0,0 +1,23 @@
1
+ import type { QueryPromise, SQL } from 'drizzle-orm';
2
+ import type { ChainedMethods } from '../find/chainMethods.js';
3
+ import type { DrizzleDB, PostgresAdapter } from '../types.js';
4
+ import type { BuildQueryJoinAliases, BuildQueryJoins } from './buildQuery.js';
5
+ import { type GenericColumn } from '../types.js';
6
+ type Args = {
7
+ adapter: PostgresAdapter;
8
+ chainedMethods?: ChainedMethods;
9
+ db: DrizzleDB;
10
+ joinAliases: BuildQueryJoinAliases;
11
+ joins: BuildQueryJoins;
12
+ selectFields: Record<string, GenericColumn>;
13
+ tableName: string;
14
+ where: SQL;
15
+ };
16
+ /**
17
+ * Selects distinct records from a table only if there are joins that need to be used, otherwise return null
18
+ */
19
+ export declare const selectDistinct: ({ adapter, chainedMethods, db, joinAliases, joins, selectFields, tableName, where, }: Args) => QueryPromise<Record<string, GenericColumn> & {
20
+ id: number | string;
21
+ }[]>;
22
+ export {};
23
+ //# sourceMappingURL=selectDistinct.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selectDistinct.d.ts","sourceRoot":"","sources":["../../src/queries/selectDistinct.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAEpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7D,OAAO,KAAK,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAG7E,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAA;AAEhD,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,eAAe,CAAA;IACxB,cAAc,CAAC,EAAE,cAAc,CAAA;IAC/B,EAAE,EAAE,SAAS,CAAA;IACb,WAAW,EAAE,qBAAqB,CAAA;IAClC,KAAK,EAAE,eAAe,CAAA;IACtB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IAC3C,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,EAAE,GAAG,CAAA;CACX,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,yFASxB,IAAI,KAAG,aAAa,OAAO,MAAM,EAAE,aAAa,CAAC,GAAG;IAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,EAAE,CA2B/E,CAAA"}
@@ -0,0 +1,41 @@
1
+ import { chainMethods } from '../find/chainMethods.js';
2
+ /**
3
+ * Selects distinct records from a table only if there are joins that need to be used, otherwise return null
4
+ */ export const selectDistinct = ({ adapter, chainedMethods = [], db, joinAliases, joins, selectFields, tableName, where })=>{
5
+ if (Object.keys(joins).length > 0 || joinAliases.length > 0) {
6
+ if (where) {
7
+ chainedMethods.push({
8
+ args: [
9
+ where
10
+ ],
11
+ method: 'where'
12
+ });
13
+ }
14
+ joinAliases.forEach(({ condition, table })=>{
15
+ chainedMethods.push({
16
+ args: [
17
+ table,
18
+ condition
19
+ ],
20
+ method: 'leftJoin'
21
+ });
22
+ });
23
+ Object.entries(joins).forEach(([joinTable, condition])=>{
24
+ if (joinTable) {
25
+ chainedMethods.push({
26
+ args: [
27
+ adapter.tables[joinTable],
28
+ condition
29
+ ],
30
+ method: 'leftJoin'
31
+ });
32
+ }
33
+ });
34
+ return chainMethods({
35
+ methods: chainedMethods,
36
+ query: db.selectDistinct(selectFields).from(adapter.tables[tableName])
37
+ });
38
+ }
39
+ };
40
+
41
+ //# sourceMappingURL=selectDistinct.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/queries/selectDistinct.ts"],"sourcesContent":["import type { QueryPromise, SQL } from 'drizzle-orm'\n\nimport type { ChainedMethods } from '../find/chainMethods.js'\nimport type { DrizzleDB, PostgresAdapter } from '../types.js'\nimport type { BuildQueryJoinAliases, BuildQueryJoins } from './buildQuery.js'\n\nimport { chainMethods } from '../find/chainMethods.js'\nimport { type GenericColumn } from '../types.js'\n\ntype Args = {\n adapter: PostgresAdapter\n chainedMethods?: ChainedMethods\n db: DrizzleDB\n joinAliases: BuildQueryJoinAliases\n joins: BuildQueryJoins\n selectFields: Record<string, GenericColumn>\n tableName: string\n where: SQL\n}\n\n/**\n * Selects distinct records from a table only if there are joins that need to be used, otherwise return null\n */\nexport const selectDistinct = ({\n adapter,\n chainedMethods = [],\n db,\n joinAliases,\n joins,\n selectFields,\n tableName,\n where,\n}: Args): QueryPromise<Record<string, GenericColumn> & { id: number | string }[]> => {\n if (Object.keys(joins).length > 0 || joinAliases.length > 0) {\n if (where) {\n chainedMethods.push({ args: [where], method: 'where' })\n }\n\n joinAliases.forEach(({ condition, table }) => {\n chainedMethods.push({\n args: [table, condition],\n method: 'leftJoin',\n })\n })\n\n Object.entries(joins).forEach(([joinTable, condition]) => {\n if (joinTable) {\n chainedMethods.push({\n args: [adapter.tables[joinTable], condition],\n method: 'leftJoin',\n })\n }\n })\n\n return chainMethods({\n methods: chainedMethods,\n query: db.selectDistinct(selectFields).from(adapter.tables[tableName]),\n })\n }\n}\n"],"names":["chainMethods","selectDistinct","adapter","chainedMethods","db","joinAliases","joins","selectFields","tableName","where","Object","keys","length","push","args","method","forEach","condition","table","entries","joinTable","tables","methods","query","from"],"mappings":"AAMA,SAASA,YAAY,QAAQ,0BAAyB;AActD;;CAEC,GACD,OAAO,MAAMC,iBAAiB,CAAC,EAC7BC,OAAO,EACPC,iBAAiB,EAAE,EACnBC,EAAE,EACFC,WAAW,EACXC,KAAK,EACLC,YAAY,EACZC,SAAS,EACTC,KAAK,EACA;IACL,IAAIC,OAAOC,IAAI,CAACL,OAAOM,MAAM,GAAG,KAAKP,YAAYO,MAAM,GAAG,GAAG;QAC3D,IAAIH,OAAO;YACTN,eAAeU,IAAI,CAAC;gBAAEC,MAAM;oBAACL;iBAAM;gBAAEM,QAAQ;YAAQ;QACvD;QAEAV,YAAYW,OAAO,CAAC,CAAC,EAAEC,SAAS,EAAEC,KAAK,EAAE;YACvCf,eAAeU,IAAI,CAAC;gBAClBC,MAAM;oBAACI;oBAAOD;iBAAU;gBACxBF,QAAQ;YACV;QACF;QAEAL,OAAOS,OAAO,CAACb,OAAOU,OAAO,CAAC,CAAC,CAACI,WAAWH,UAAU;YACnD,IAAIG,WAAW;gBACbjB,eAAeU,IAAI,CAAC;oBAClBC,MAAM;wBAACZ,QAAQmB,MAAM,CAACD,UAAU;wBAAEH;qBAAU;oBAC5CF,QAAQ;gBACV;YACF;QACF;QAEA,OAAOf,aAAa;YAClBsB,SAASnB;YACToB,OAAOnB,GAAGH,cAAc,CAACM,cAAciB,IAAI,CAACtB,QAAQmB,MAAM,CAACb,UAAU;QACvE;IACF;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAWjD,eAAO,MAAM,SAAS,EAAE,SAmEvB,CAAA"}
1
+ {"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAUjD,eAAO,MAAM,SAAS,EAAE,SA6CvB,CAAA"}
package/dist/update.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import toSnakeCase from 'to-snake-case';
2
- import { chainMethods } from './find/chainMethods.js';
3
2
  import buildQuery from './queries/buildQuery.js';
3
+ import { selectDistinct } from './queries/selectDistinct.js';
4
4
  import { upsertRow } from './upsertRow/index.js';
5
5
  export const updateOne = async function updateOne({ id, collection: collectionSlug, data, draft, locale, req, where: whereArg }) {
6
6
  const db = this.sessions[req.transactionID]?.db || this.drizzle;
@@ -11,6 +11,7 @@ export const updateOne = async function updateOne({ id, collection: collectionSl
11
11
  equals: id
12
12
  }
13
13
  };
14
+ let idToUpdate = id;
14
15
  const { joinAliases, joins, selectFields, where } = await buildQuery({
15
16
  adapter: this,
16
17
  fields: collection.fields,
@@ -18,51 +19,25 @@ export const updateOne = async function updateOne({ id, collection: collectionSl
18
19
  tableName,
19
20
  where: whereToUse
20
21
  });
21
- let idToUpdate = id;
22
- // only fetch IDs when a sort or where query is used that needs to be done on join tables, otherwise these can be done directly on the table in findMany
23
- if (Object.keys(joins).length > 0 || joinAliases.length > 0) {
24
- const selectDistinctMethods = [];
25
- if (where) {
26
- selectDistinctMethods.push({
27
- args: [
28
- where
29
- ],
30
- method: 'where'
31
- });
32
- }
33
- joinAliases.forEach(({ condition, table })=>{
34
- selectDistinctMethods.push({
22
+ const selectDistinctResult = await selectDistinct({
23
+ adapter: this,
24
+ chainedMethods: [
25
+ {
35
26
  args: [
36
- table,
37
- condition
27
+ 1
38
28
  ],
39
- method: 'leftJoin'
40
- });
41
- });
42
- Object.entries(joins).forEach(([joinTable, condition])=>{
43
- if (joinTable) {
44
- selectDistinctMethods.push({
45
- args: [
46
- this.tables[joinTable],
47
- condition
48
- ],
49
- method: 'leftJoin'
50
- });
29
+ method: 'limit'
51
30
  }
52
- });
53
- selectDistinctMethods.push({
54
- args: [
55
- 1
56
- ],
57
- method: 'limit'
58
- });
59
- const selectDistinctResult = await chainMethods({
60
- methods: selectDistinctMethods,
61
- query: db.selectDistinct(selectFields).from(this.tables[tableName])
62
- });
63
- if (selectDistinctResult?.[0]?.id) {
64
- idToUpdate = selectDistinctResult?.[0]?.id;
65
- }
31
+ ],
32
+ db,
33
+ joinAliases,
34
+ joins,
35
+ selectFields,
36
+ tableName,
37
+ where
38
+ });
39
+ if (selectDistinctResult?.[0]?.id) {
40
+ idToUpdate = selectDistinctResult?.[0]?.id;
66
41
  }
67
42
  const result = await upsertRow({
68
43
  id: idToUpdate,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/update.ts"],"sourcesContent":["import type { UpdateOne } from 'payload/database'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { ChainedMethods } from './find/chainMethods.js'\nimport type { PostgresAdapter } from './types.js'\n\nimport { chainMethods } from './find/chainMethods.js'\nimport buildQuery from './queries/buildQuery.js'\nimport { upsertRow } from './upsertRow/index.js'\n\nexport const updateOne: UpdateOne = async function updateOne(\n this: PostgresAdapter,\n { id, collection: collectionSlug, data, draft, locale, req, where: whereArg },\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n const tableName = toSnakeCase(collectionSlug)\n const whereToUse = whereArg || { id: { equals: id } }\n\n const { joinAliases, joins, selectFields, where } = await buildQuery({\n adapter: this,\n fields: collection.fields,\n locale,\n tableName,\n where: whereToUse,\n })\n\n let idToUpdate = id\n\n // only fetch IDs when a sort or where query is used that needs to be done on join tables, otherwise these can be done directly on the table in findMany\n if (Object.keys(joins).length > 0 || joinAliases.length > 0) {\n const selectDistinctMethods: ChainedMethods = []\n\n if (where) {\n selectDistinctMethods.push({ args: [where], method: 'where' })\n }\n\n joinAliases.forEach(({ condition, table }) => {\n selectDistinctMethods.push({\n args: [table, condition],\n method: 'leftJoin',\n })\n })\n\n Object.entries(joins).forEach(([joinTable, condition]) => {\n if (joinTable) {\n selectDistinctMethods.push({\n args: [this.tables[joinTable], condition],\n method: 'leftJoin',\n })\n }\n })\n\n selectDistinctMethods.push({ args: [1], method: 'limit' })\n\n const selectDistinctResult = await chainMethods({\n methods: selectDistinctMethods,\n query: db.selectDistinct(selectFields).from(this.tables[tableName]),\n })\n\n if (selectDistinctResult?.[0]?.id) {\n idToUpdate = selectDistinctResult?.[0]?.id\n }\n }\n\n const result = await upsertRow({\n id: idToUpdate,\n adapter: this,\n data,\n db,\n fields: collection.fields,\n operation: 'update',\n req,\n tableName: toSnakeCase(collectionSlug),\n })\n\n return result\n}\n"],"names":["toSnakeCase","chainMethods","buildQuery","upsertRow","updateOne","id","collection","collectionSlug","data","draft","locale","req","where","whereArg","db","sessions","transactionID","drizzle","payload","collections","config","tableName","whereToUse","equals","joinAliases","joins","selectFields","adapter","fields","idToUpdate","Object","keys","length","selectDistinctMethods","push","args","method","forEach","condition","table","entries","joinTable","tables","selectDistinctResult","methods","query","selectDistinct","from","result","operation"],"mappings":"AAEA,OAAOA,iBAAiB,gBAAe;AAKvC,SAASC,YAAY,QAAQ,yBAAwB;AACrD,OAAOC,gBAAgB,0BAAyB;AAChD,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,MAAMC,YAAuB,eAAeA,UAEjD,EAAEC,EAAE,EAAEC,YAAYC,cAAc,EAAEC,IAAI,EAAEC,KAAK,EAAEC,MAAM,EAAEC,GAAG,EAAEC,OAAOC,QAAQ,EAAE;IAE7E,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACJ,IAAIK,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMX,aAAa,IAAI,CAACY,OAAO,CAACC,WAAW,CAACZ,eAAe,CAACa,MAAM;IAClE,MAAMC,YAAYrB,YAAYO;IAC9B,MAAMe,aAAaT,YAAY;QAAER,IAAI;YAAEkB,QAAQlB;QAAG;IAAE;I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QAAQtB,WAAWsB,MAAM;QACzBqB,WAAW;QACXtC;QACAU,WAAWrB,YAAYO;IACzB;IAEA,OAAOyC;AACT,EAAC"}
1
+ {"version":3,"sources":["../src/update.ts"],"sourcesContent":["import type { UpdateOne } from 'payload/database'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } 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: PostgresAdapter,\n { id, collection: collectionSlug, data, draft, locale, req, where: whereArg },\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n const tableName = toSnakeCase(collectionSlug)\n const whereToUse = whereArg || { id: { equals: id } }\n let idToUpdate = id\n\n const { joinAliases, joins, selectFields, where } = await 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 joinAliases,\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 operation: 'update',\n req,\n tableName: toSnakeCase(collectionSlug),\n })\n\n return result\n}\n"],"names":["toSnakeCase","buildQuery","selectDistinct","upsertRow","updateOne","id","collection","collectionSlug","data","draft","locale","req","where","whereArg","db","sessions","transactionID","drizzle","payload","collections","config","tableName","whereToUse","equals","idToUpdate","joinAliases","joins","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,MAAM,EAAEC,GAAG,EAAEC,OAAOC,QAAQ,EAAE;IAE7E,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACJ,IAAIK,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMX,aAAa,IAAI,CAACY,OAAO,CAACC,WAAW,CAACZ,eAAe,CAACa,MAAM;IAClE,MAAMC,YAAYrB,YAAYO;IAC9B,MAAMe,aAAaT,YAAY;QAAER,IAAI;YAAEkB,QAAQlB;QAAG;IAAE;IACpD,IAAImB,aAAanB;IAEjB,MAAM,EAAEoB,WAAW,EAAEC,KAAK,EAAEC,YAAY,EAAEf,KAAK,EAAE,GAAG,MAAMX,WAAW;QACnE2B,SAAS,IAAI;QACbC,QAAQvB,WAAWuB,MAAM;QACzBnB;QACAW;QACAT,OAAOU;IACT;IAEA,MAAMQ,uBAAuB,MAAM5B,eAAe;QAChD0B,SAAS,IAAI;QACbG,gBAAgB;YAAC;gBAAEC,MAAM;oBAAC;iBAAE;gBAAEC,QAAQ;YAAQ;SAAE;QAChDnB;QACAW;QACAC;QACAC;QACAN;QACAT;IACF;IAEA,IAAIkB,sBAAsB,CAAC,EAAE,EAAEzB,IAAI;QACjCmB,aAAaM,sBAAsB,CAAC,EAAE,EAAEzB;IAC1C;IAEA,MAAM6B,SAAS,MAAM/B,UAAU;QAC7BE,IAAImB;QACJI,SAAS,IAAI;QACbpB;QACAM;QACAe,QAAQvB,WAAWuB,MAAM;QACzBM,WAAW;QACXxB;QACAU,WAAWrB,YAAYO;IACzB;IAEA,OAAO2B;AACT,EAAC"}
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@payloadcms/db-postgres",
3
- "version": "3.0.0-alpha.49",
3
+ "version": "3.0.0-alpha.50",
4
4
  "description": "The officially supported Postgres database adapter for Payload",
5
- "repository": "https://github.com/payloadcms/payload",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/payloadcms/payload.git",
8
+ "directory": "packages/db-postgres"
9
+ },
6
10
  "license": "MIT",
7
11
  "homepage": "https://payloadcms.com",
8
12
  "type": "module",
@@ -26,11 +30,11 @@
26
30
  "devDependencies": {
27
31
  "@types/pg": "8.10.2",
28
32
  "@types/to-snake-case": "1.0.0",
29
- "payload": "3.0.0-alpha.49",
33
+ "payload": "3.0.0-alpha.50",
30
34
  "@payloadcms/eslint-config": "1.1.1"
31
35
  },
32
36
  "peerDependencies": {
33
- "payload": "3.0.0-alpha.49"
37
+ "payload": "3.0.0-alpha.50"
34
38
  },
35
39
  "exports": {
36
40
  ".": {