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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. package/README.md +1 -3
  2. package/dist/create.js +2 -2
  3. package/dist/create.js.map +1 -1
  4. package/dist/createGlobal.d.ts +1 -1
  5. package/dist/createGlobal.d.ts.map +1 -1
  6. package/dist/createGlobal.js +3 -3
  7. package/dist/createGlobal.js.map +1 -1
  8. package/dist/createGlobalVersion.d.ts +1 -1
  9. package/dist/createGlobalVersion.d.ts.map +1 -1
  10. package/dist/createGlobalVersion.js +2 -2
  11. package/dist/createGlobalVersion.js.map +1 -1
  12. package/dist/createVersion.js +2 -2
  13. package/dist/createVersion.js.map +1 -1
  14. package/dist/deleteOne.d.ts.map +1 -1
  15. package/dist/deleteOne.js +41 -14
  16. package/dist/deleteOne.js.map +1 -1
  17. package/dist/find/chainMethods.d.ts +3 -2
  18. package/dist/find/chainMethods.d.ts.map +1 -1
  19. package/dist/find/chainMethods.js.map +1 -1
  20. package/dist/find/findMany.d.ts.map +1 -1
  21. package/dist/find/findMany.js +31 -53
  22. package/dist/find/findMany.js.map +1 -1
  23. package/dist/findGlobal.js +1 -1
  24. package/dist/findGlobal.js.map +1 -1
  25. package/dist/queries/getTableColumnFromPath.js.map +1 -1
  26. package/dist/queries/parseParams.js +1 -1
  27. package/dist/queries/parseParams.js.map +1 -1
  28. package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
  29. package/dist/queries/sanitizeQueryValue.js +4 -1
  30. package/dist/queries/sanitizeQueryValue.js.map +1 -1
  31. package/dist/queries/selectDistinct.d.ts +23 -0
  32. package/dist/queries/selectDistinct.d.ts.map +1 -0
  33. package/dist/queries/selectDistinct.js +41 -0
  34. package/dist/queries/selectDistinct.js.map +1 -0
  35. package/dist/transform/read/hasManyText.d.ts +2 -2
  36. package/dist/transform/read/hasManyText.d.ts.map +1 -1
  37. package/dist/transform/read/hasManyText.js +1 -1
  38. package/dist/transform/read/hasManyText.js.map +1 -1
  39. package/dist/transform/read/index.d.ts.map +1 -1
  40. package/dist/transform/read/index.js +2 -2
  41. package/dist/transform/read/index.js.map +1 -1
  42. package/dist/transform/read/traverseFields.d.ts.map +1 -1
  43. package/dist/transform/write/array.d.ts +2 -2
  44. package/dist/transform/write/array.d.ts.map +1 -1
  45. package/dist/transform/write/array.js +3 -3
  46. package/dist/transform/write/array.js.map +1 -1
  47. package/dist/transform/write/blocks.d.ts +2 -2
  48. package/dist/transform/write/blocks.d.ts.map +1 -1
  49. package/dist/transform/write/blocks.js +3 -3
  50. package/dist/transform/write/blocks.js.map +1 -1
  51. package/dist/transform/write/index.js +4 -4
  52. package/dist/transform/write/index.js.map +1 -1
  53. package/dist/transform/write/texts.js +2 -2
  54. package/dist/transform/write/texts.js.map +1 -1
  55. package/dist/transform/write/traverseFields.d.ts +2 -2
  56. package/dist/transform/write/traverseFields.d.ts.map +1 -1
  57. package/dist/transform/write/traverseFields.js +21 -21
  58. package/dist/transform/write/traverseFields.js.map +1 -1
  59. package/dist/transform/write/types.d.ts +1 -1
  60. package/dist/transform/write/types.d.ts.map +1 -1
  61. package/dist/transform/write/types.js.map +1 -1
  62. package/dist/update.d.ts.map +1 -1
  63. package/dist/update.js +20 -45
  64. package/dist/update.js.map +1 -1
  65. package/dist/updateGlobal.d.ts +1 -1
  66. package/dist/updateGlobal.d.ts.map +1 -1
  67. package/dist/updateGlobal.js +3 -3
  68. package/dist/updateGlobal.js.map +1 -1
  69. package/dist/updateGlobalVersion.js +2 -2
  70. package/dist/updateGlobalVersion.js.map +1 -1
  71. package/dist/updateVersion.js +2 -2
  72. package/dist/updateVersion.js.map +1 -1
  73. package/dist/upsertRow/index.d.ts.map +1 -1
  74. package/dist/upsertRow/types.d.ts +1 -1
  75. package/dist/upsertRow/types.d.ts.map +1 -1
  76. package/dist/upsertRow/types.js.map +1 -1
  77. package/dist/utilities/appendPrefixToKeys.d.ts.map +1 -1
  78. package/package.json +20 -7
  79. package/src/index.ts +150 -0
package/README.md CHANGED
@@ -21,12 +21,10 @@ export default buildConfig({
21
21
  db: postgresAdapter({
22
22
  pool: {
23
23
  connectionString: process.env.DATABASE_URI,
24
- }
24
+ },
25
25
  }),
26
26
  // ...rest of config
27
27
  })
28
-
29
28
  ```
30
29
 
31
30
  More detailed usage can be found in the [Payload Docs](https://payloadcms.com/docs/configuration/overview).
32
-
package/dist/create.js CHANGED
@@ -9,8 +9,8 @@ export const create = async function create({ collection: collectionSlug, data,
9
9
  db,
10
10
  fields: collection.fields,
11
11
  operation: 'create',
12
- tableName: toSnakeCase(collectionSlug),
13
- req
12
+ req,
13
+ tableName: toSnakeCase(collectionSlug)
14
14
  });
15
15
  return result;
16
16
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/create.ts"],"sourcesContent":["import type { Create } from 'payload/database'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport const create: Create = async function create(\n this: PostgresAdapter,\n { collection: collectionSlug, data, req },\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n\n const result = await upsertRow({\n adapter: this,\n data,\n db,\n fields: collection.fields,\n operation: 'create',\n tableName: toSnakeCase(collectionSlug),\n req,\n })\n\n return result\n}\n"],"names":["toSnakeCase","upsertRow","create","collection","collectionSlug","data","req","db","sessions","transactionID","drizzle","payload","collections","config","result","adapter","fields","operation","tableName"],"mappings":"AAEA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,MAAMC,SAAiB,eAAeA,OAE3C,EAAEC,YAAYC,cAAc,EAAEC,IAAI,EAAEC,GAAG,EAAE;IAEzC,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACF,IAAIG,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMP,aAAa,IAAI,CAACQ,OAAO,CAACC,WAAW,CAACR,eAAe,CAACS,MAAM;IAElE,MAAMC,SAAS,MAAMb,UAAU;QAC7Bc,SAAS,IAAI;QACbV;QACAE;QACAS,QAAQb,WAAWa,MAAM;QACzBC,WAAW;QACXC,WAAWlB,YAAYI;QACvBE;IACF;IAEA,OAAOQ;AACT,EAAC"}
1
+ {"version":3,"sources":["../src/create.ts"],"sourcesContent":["import type { Create } from 'payload/database'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport const create: Create = async function create(\n this: PostgresAdapter,\n { collection: collectionSlug, data, req },\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n\n const result = await upsertRow({\n adapter: this,\n data,\n db,\n fields: collection.fields,\n operation: 'create',\n req,\n tableName: toSnakeCase(collectionSlug),\n })\n\n return result\n}\n"],"names":["toSnakeCase","upsertRow","create","collection","collectionSlug","data","req","db","sessions","transactionID","drizzle","payload","collections","config","result","adapter","fields","operation","tableName"],"mappings":"AAEA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,MAAMC,SAAiB,eAAeA,OAE3C,EAAEC,YAAYC,cAAc,EAAEC,IAAI,EAAEC,GAAG,EAAE;IAEzC,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACF,IAAIG,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMP,aAAa,IAAI,CAACQ,OAAO,CAACC,WAAW,CAACR,eAAe,CAACS,MAAM;IAElE,MAAMC,SAAS,MAAMb,UAAU;QAC7Bc,SAAS,IAAI;QACbV;QACAE;QACAS,QAAQb,WAAWa,MAAM;QACzBC,WAAW;QACXX;QACAY,WAAWlB,YAAYI;IACzB;IAEA,OAAOU;AACT,EAAC"}
@@ -1,5 +1,5 @@
1
1
  import type { CreateGlobalArgs } from 'payload/database';
2
2
  import type { TypeWithID } from 'payload/types';
3
3
  import type { PostgresAdapter } from './types.js';
4
- export declare function createGlobal<T extends TypeWithID>(this: PostgresAdapter, { data, req, slug }: CreateGlobalArgs): Promise<T>;
4
+ export declare function createGlobal<T extends TypeWithID>(this: PostgresAdapter, { slug, data, req }: CreateGlobalArgs): Promise<T>;
5
5
  //# sourceMappingURL=createGlobal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createGlobal.d.ts","sourceRoot":"","sources":["../src/createGlobal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACxD,OAAO,KAAK,EAAkB,UAAU,EAAE,MAAM,eAAe,CAAA;AAI/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAIjD,wBAAsB,YAAY,CAAC,CAAC,SAAS,UAAU,EACrD,IAAI,EAAE,eAAe,EACrB,EAAE,IAAI,EAAE,GAA0B,EAAE,IAAI,EAAE,EAAE,gBAAgB,GAC3D,OAAO,CAAC,CAAC,CAAC,CAeZ"}
1
+ {"version":3,"file":"createGlobal.d.ts","sourceRoot":"","sources":["../src/createGlobal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACxD,OAAO,KAAK,EAAkB,UAAU,EAAE,MAAM,eAAe,CAAA;AAI/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAIjD,wBAAsB,YAAY,CAAC,CAAC,SAAS,UAAU,EACrD,IAAI,EAAE,eAAe,EACrB,EAAE,IAAI,EAAE,IAAI,EAAE,GAA0B,EAAE,EAAE,gBAAgB,GAC3D,OAAO,CAAC,CAAC,CAAC,CAeZ"}
@@ -1,6 +1,6 @@
1
1
  import toSnakeCase from 'to-snake-case';
2
2
  import { upsertRow } from './upsertRow/index.js';
3
- export async function createGlobal({ data, req = {}, slug }) {
3
+ export async function createGlobal({ slug, data, req = {} }) {
4
4
  const db = this.sessions[req.transactionID]?.db || this.drizzle;
5
5
  const globalConfig = this.payload.globals.config.find((config)=>config.slug === slug);
6
6
  const result = await upsertRow({
@@ -9,8 +9,8 @@ export async function createGlobal({ data, req = {}, slug }) {
9
9
  db,
10
10
  fields: globalConfig.fields,
11
11
  operation: 'create',
12
- tableName: toSnakeCase(slug),
13
- req
12
+ req,
13
+ tableName: toSnakeCase(slug)
14
14
  });
15
15
  return result;
16
16
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/createGlobal.ts"],"sourcesContent":["import type { CreateGlobalArgs } from 'payload/database'\nimport type { PayloadRequest, TypeWithID } from 'payload/types'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function createGlobal<T extends TypeWithID>(\n this: PostgresAdapter,\n { data, req = {} as PayloadRequest, slug }: CreateGlobalArgs,\n): Promise<T> {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)\n\n const result = await upsertRow<T>({\n adapter: this,\n data,\n db,\n fields: globalConfig.fields,\n operation: 'create',\n tableName: toSnakeCase(slug),\n req,\n })\n\n return result\n}\n"],"names":["toSnakeCase","upsertRow","createGlobal","data","req","slug","db","sessions","transactionID","drizzle","globalConfig","payload","globals","config","find","result","adapter","fields","operation","tableName"],"mappings":"AAGA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,aAEpB,EAAEC,IAAI,EAAEC,MAAM,CAAC,CAAmB,EAAEC,IAAI,EAAoB;IAE5D,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACH,IAAII,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMC,eAAe,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAACD,SAAWA,OAAOR,IAAI,KAAKA;IAElF,MAAMU,SAAS,MAAMd,UAAa;QAChCe,SAAS,IAAI;QACbb;QACAG;QACAW,QAAQP,aAAaO,MAAM;QAC3BC,WAAW;QACXC,WAAWnB,YAAYK;QACvBD;IACF;IAEA,OAAOW;AACT"}
1
+ {"version":3,"sources":["../src/createGlobal.ts"],"sourcesContent":["import type { CreateGlobalArgs } from 'payload/database'\nimport type { PayloadRequest, TypeWithID } from 'payload/types'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function createGlobal<T extends TypeWithID>(\n this: PostgresAdapter,\n { slug, data, req = {} as PayloadRequest }: CreateGlobalArgs,\n): Promise<T> {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)\n\n const result = await upsertRow<T>({\n adapter: this,\n data,\n db,\n fields: globalConfig.fields,\n operation: 'create',\n req,\n tableName: toSnakeCase(slug),\n })\n\n return result\n}\n"],"names":["toSnakeCase","upsertRow","createGlobal","slug","data","req","db","sessions","transactionID","drizzle","globalConfig","payload","globals","config","find","result","adapter","fields","operation","tableName"],"mappings":"AAGA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,aAEpB,EAAEC,IAAI,EAAEC,IAAI,EAAEC,MAAM,CAAC,CAAmB,EAAoB;IAE5D,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACF,IAAIG,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMC,eAAe,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAACD,SAAWA,OAAOV,IAAI,KAAKA;IAElF,MAAMY,SAAS,MAAMd,UAAa;QAChCe,SAAS,IAAI;QACbZ;QACAE;QACAW,QAAQP,aAAaO,MAAM;QAC3BC,WAAW;QACXb;QACAc,WAAWnB,YAAYG;IACzB;IAEA,OAAOY;AACT"}
@@ -1,6 +1,6 @@
1
1
  import type { TypeWithVersion } from 'payload/database';
2
- import { type CreateGlobalVersionArgs } from 'payload/database';
3
2
  import type { TypeWithID } from 'payload/types';
3
+ import { type CreateGlobalVersionArgs } from 'payload/database';
4
4
  import type { PostgresAdapter } from './types.js';
5
5
  export declare function createGlobalVersion<T extends TypeWithID>(this: PostgresAdapter, { autosave, globalSlug, req, versionData }: CreateGlobalVersionArgs): Promise<TypeWithVersion<T>>;
6
6
  //# sourceMappingURL=createGlobalVersion.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createGlobalVersion.d.ts","sourceRoot":"","sources":["../src/createGlobalVersion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAC/D,OAAO,KAAK,EAAkB,UAAU,EAAE,MAAM,eAAe,CAAA;AAM/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAIjD,wBAAsB,mBAAmB,CAAC,CAAC,SAAS,UAAU,EAC5D,IAAI,EAAE,eAAe,EACrB,EAAE,QAAQ,EAAE,UAAU,EAAE,GAA0B,EAAE,WAAW,EAAE,EAAE,uBAAuB,+BAgC3F"}
1
+ {"version":3,"file":"createGlobalVersion.d.ts","sourceRoot":"","sources":["../src/createGlobalVersion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,KAAK,EAAkB,UAAU,EAAE,MAAM,eAAe,CAAA;AAG/D,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAI/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAIjD,wBAAsB,mBAAmB,CAAC,CAAC,SAAS,UAAU,EAC5D,IAAI,EAAE,eAAe,EACrB,EAAE,QAAQ,EAAE,UAAU,EAAE,GAA0B,EAAE,WAAW,EAAE,EAAE,uBAAuB,+BAgC3F"}
@@ -17,8 +17,8 @@ export async function createGlobalVersion({ autosave, globalSlug, req = {}, vers
17
17
  db,
18
18
  fields: buildVersionGlobalFields(global),
19
19
  operation: 'create',
20
- tableName,
21
- req
20
+ req,
21
+ tableName
22
22
  });
23
23
  const table = this.tables[tableName];
24
24
  if (global.versions.drafts) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/createGlobalVersion.ts"],"sourcesContent":["import type { TypeWithVersion } from 'payload/database'\nimport { type CreateGlobalVersionArgs } from 'payload/database'\nimport type { PayloadRequest, TypeWithID } from 'payload/types'\n\nimport { sql } from 'drizzle-orm'\nimport { buildVersionGlobalFields } from 'payload/versions'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function createGlobalVersion<T extends TypeWithID>(\n this: PostgresAdapter,\n { autosave, globalSlug, req = {} as PayloadRequest, versionData }: CreateGlobalVersionArgs,\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const global = this.payload.globals.config.find(({ slug }) => slug === globalSlug)\n const globalTableName = toSnakeCase(globalSlug)\n const tableName = `_${globalTableName}_v`\n\n const result = await upsertRow<TypeWithVersion<T>>({\n adapter: this,\n data: {\n autosave,\n latest: true,\n version: versionData,\n },\n db,\n fields: buildVersionGlobalFields(global),\n operation: 'create',\n tableName,\n req,\n })\n\n const table = this.tables[tableName]\n\n if (global.versions.drafts) {\n await db.execute(sql`\n UPDATE ${table}\n SET latest = false\n WHERE ${table.id} != ${result.id};\n `)\n }\n\n return result\n}\n"],"names":["sql","buildVersionGlobalFields","toSnakeCase","upsertRow","createGlobalVersion","autosave","globalSlug","req","versionData","db","sessions","transactionID","drizzle","global","payload","globals","config","find","slug","globalTableName","tableName","result","adapter","data","latest","version","fields","operation","table","tables","versions","drafts","execute","id"],"mappings":"AAIA,SAASA,GAAG,QAAQ,cAAa;AACjC,SAASC,wBAAwB,QAAQ,mBAAkB;AAC3D,OAAOC,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,oBAEpB,EAAEC,QAAQ,EAAEC,UAAU,EAAEC,MAAM,CAAC,CAAmB,EAAEC,WAAW,EAA2B;IAE1F,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACH,IAAII,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMC,SAAS,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAAC,EAAEC,IAAI,EAAE,GAAKA,SAASZ;IACvE,MAAMa,kBAAkBjB,YAAYI;IACpC,MAAMc,YAAY,CAAC,CAAC,EAAED,gBAAgB,EAAE,CAAC;IAEzC,MAAME,SAAS,MAAMlB,UAA8B;QACjDmB,SAAS,IAAI;QACbC,MAAM;YACJlB;YACAmB,QAAQ;YACRC,SAASjB;QACX;QACAC;QACAiB,QAAQzB,yBAAyBY;QACjCc,WAAW;QACXP;QACAb;IACF;IAEA,MAAMqB,QAAQ,IAAI,CAACC,MAAM,CAACT,UAAU;IAEpC,IAAIP,OAAOiB,QAAQ,CAACC,MAAM,EAAE;QAC1B,MAAMtB,GAAGuB,OAAO,CAAChC,GAAG,CAAC;aACZ,EAAE4B,MAAM;;YAET,EAAEA,MAAMK,EAAE,CAAC,IAAI,EAAEZ,OAAOY,EAAE,CAAC;IACnC,CAAC;IACH;IAEA,OAAOZ;AACT"}
1
+ {"version":3,"sources":["../src/createGlobalVersion.ts"],"sourcesContent":["import type { TypeWithVersion } from 'payload/database'\nimport type { PayloadRequest, TypeWithID } from 'payload/types'\n\nimport { sql } from 'drizzle-orm'\nimport { type CreateGlobalVersionArgs } from 'payload/database'\nimport { buildVersionGlobalFields } from 'payload/versions'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function createGlobalVersion<T extends TypeWithID>(\n this: PostgresAdapter,\n { autosave, globalSlug, req = {} as PayloadRequest, versionData }: CreateGlobalVersionArgs,\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const global = this.payload.globals.config.find(({ slug }) => slug === globalSlug)\n const globalTableName = toSnakeCase(globalSlug)\n const tableName = `_${globalTableName}_v`\n\n const result = await upsertRow<TypeWithVersion<T>>({\n adapter: this,\n data: {\n autosave,\n latest: true,\n version: versionData,\n },\n db,\n fields: buildVersionGlobalFields(global),\n operation: 'create',\n req,\n tableName,\n })\n\n const table = this.tables[tableName]\n\n if (global.versions.drafts) {\n await db.execute(sql`\n UPDATE ${table}\n SET latest = false\n WHERE ${table.id} != ${result.id};\n `)\n }\n\n return result\n}\n"],"names":["sql","buildVersionGlobalFields","toSnakeCase","upsertRow","createGlobalVersion","autosave","globalSlug","req","versionData","db","sessions","transactionID","drizzle","global","payload","globals","config","find","slug","globalTableName","tableName","result","adapter","data","latest","version","fields","operation","table","tables","versions","drafts","execute","id"],"mappings":"AAGA,SAASA,GAAG,QAAQ,cAAa;AAEjC,SAASC,wBAAwB,QAAQ,mBAAkB;AAC3D,OAAOC,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,oBAEpB,EAAEC,QAAQ,EAAEC,UAAU,EAAEC,MAAM,CAAC,CAAmB,EAAEC,WAAW,EAA2B;IAE1F,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACH,IAAII,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMC,SAAS,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAAC,EAAEC,IAAI,EAAE,GAAKA,SAASZ;IACvE,MAAMa,kBAAkBjB,YAAYI;IACpC,MAAMc,YAAY,CAAC,CAAC,EAAED,gBAAgB,EAAE,CAAC;IAEzC,MAAME,SAAS,MAAMlB,UAA8B;QACjDmB,SAAS,IAAI;QACbC,MAAM;YACJlB;YACAmB,QAAQ;YACRC,SAASjB;QACX;QACAC;QACAiB,QAAQzB,yBAAyBY;QACjCc,WAAW;QACXpB;QACAa;IACF;IAEA,MAAMQ,QAAQ,IAAI,CAACC,MAAM,CAACT,UAAU;IAEpC,IAAIP,OAAOiB,QAAQ,CAACC,MAAM,EAAE;QAC1B,MAAMtB,GAAGuB,OAAO,CAAChC,GAAG,CAAC;aACZ,EAAE4B,MAAM;;YAET,EAAEA,MAAMK,EAAE,CAAC,IAAI,EAAEZ,OAAOY,EAAE,CAAC;IACnC,CAAC;IACH;IAEA,OAAOZ;AACT"}
@@ -18,8 +18,8 @@ export async function createVersion({ autosave, collectionSlug, parent, req = {}
18
18
  db,
19
19
  fields: buildVersionCollectionFields(collection),
20
20
  operation: 'create',
21
- tableName,
22
- req
21
+ req,
22
+ tableName
23
23
  });
24
24
  const table = this.tables[tableName];
25
25
  const relationshipsTable = this.tables[`${tableName}_rels`];
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/createVersion.ts"],"sourcesContent":["import type { CreateVersionArgs, TypeWithVersion } from 'payload/database'\nimport type { PayloadRequest, TypeWithID } from 'payload/types'\n\nimport { sql } from 'drizzle-orm'\nimport { buildVersionCollectionFields } from 'payload/versions'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function createVersion<T extends TypeWithID>(\n this: PostgresAdapter,\n {\n autosave,\n collectionSlug,\n parent,\n req = {} as PayloadRequest,\n versionData,\n }: CreateVersionArgs<T>,\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n const collectionTableName = toSnakeCase(collectionSlug)\n const tableName = `_${collectionTableName}_v`\n\n const result = await upsertRow<TypeWithVersion<T>>({\n adapter: this,\n data: {\n autosave,\n latest: true,\n parent,\n version: versionData,\n },\n db,\n fields: buildVersionCollectionFields(collection),\n operation: 'create',\n tableName,\n req,\n })\n\n const table = this.tables[tableName]\n const relationshipsTable = this.tables[`${tableName}_rels`]\n\n if (collection.versions.drafts) {\n await db.execute(sql`\n UPDATE ${table}\n SET latest = false\n FROM ${relationshipsTable}\n WHERE ${table.id} = ${relationshipsTable.parent}\n AND ${relationshipsTable.path} = ${'parent'}\n AND ${relationshipsTable[`${collectionSlug}ID`]} = ${parent}\n AND ${table.id} != ${result.id};\n `)\n }\n\n return result\n}\n"],"names":["sql","buildVersionCollectionFields","toSnakeCase","upsertRow","createVersion","autosave","collectionSlug","parent","req","versionData","db","sessions","transactionID","drizzle","collection","payload","collections","config","collectionTableName","tableName","result","adapter","data","latest","version","fields","operation","table","tables","relationshipsTable","versions","drafts","execute","id","path"],"mappings":"AAGA,SAASA,GAAG,QAAQ,cAAa;AACjC,SAASC,4BAA4B,QAAQ,mBAAkB;AAC/D,OAAOC,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,cAEpB,EACEC,QAAQ,EACRC,cAAc,EACdC,MAAM,EACNC,MAAM,CAAC,CAAmB,EAC1BC,WAAW,EACU;IAEvB,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACH,IAAII,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMC,aAAa,IAAI,CAACC,OAAO,CAACC,WAAW,CAACV,eAAe,CAACW,MAAM;IAClE,MAAMC,sBAAsBhB,YAAYI;IACxC,MAAMa,YAAY,CAAC,CAAC,EAAED,oBAAoB,EAAE,CAAC;IAE7C,MAAME,SAAS,MAAMjB,UAA8B;QACjDkB,SAAS,IAAI;QACbC,MAAM;YACJjB;YACAkB,QAAQ;YACRhB;YACAiB,SAASf;QACX;QACAC;QACAe,QAAQxB,6BAA6Ba;QACrCY,WAAW;QACXP;QACAX;IACF;IAEA,MAAMmB,QAAQ,IAAI,CAACC,MAAM,CAACT,UAAU;IACpC,MAAMU,qBAAqB,IAAI,CAACD,MAAM,CAAC,CAAC,EAAET,UAAU,KAAK,CAAC,CAAC;IAE3D,IAAIL,WAAWgB,QAAQ,CAACC,MAAM,EAAE;QAC9B,MAAMrB,GAAGsB,OAAO,CAAChC,GAAG,CAAC;aACZ,EAAE2B,MAAM;;WAEV,EAAEE,mBAAmB;YACpB,EAAEF,MAAMM,EAAE,CAAC,GAAG,EAAEJ,mBAAmBtB,MAAM,CAAC;YAC1C,EAAEsB,mBAAmBK,IAAI,CAAC,GAAG,EAAE,SAAS;YACxC,EAAEL,kBAAkB,CAAC,CAAC,EAAEvB,eAAe,EAAE,CAAC,CAAC,CAAC,GAAG,EAAEC,OAAO;YACxD,EAAEoB,MAAMM,EAAE,CAAC,IAAI,EAAEb,OAAOa,EAAE,CAAC;IACnC,CAAC;IACH;IAEA,OAAOb;AACT"}
1
+ {"version":3,"sources":["../src/createVersion.ts"],"sourcesContent":["import type { CreateVersionArgs, TypeWithVersion } from 'payload/database'\nimport type { PayloadRequest, TypeWithID } from 'payload/types'\n\nimport { sql } from 'drizzle-orm'\nimport { buildVersionCollectionFields } from 'payload/versions'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { upsertRow } from './upsertRow/index.js'\n\nexport async function createVersion<T extends TypeWithID>(\n this: PostgresAdapter,\n {\n autosave,\n collectionSlug,\n parent,\n req = {} as PayloadRequest,\n versionData,\n }: CreateVersionArgs<T>,\n) {\n const db = this.sessions[req.transactionID]?.db || this.drizzle\n const collection = this.payload.collections[collectionSlug].config\n const collectionTableName = toSnakeCase(collectionSlug)\n const tableName = `_${collectionTableName}_v`\n\n const result = await upsertRow<TypeWithVersion<T>>({\n adapter: this,\n data: {\n autosave,\n latest: true,\n parent,\n version: versionData,\n },\n db,\n fields: buildVersionCollectionFields(collection),\n operation: 'create',\n req,\n tableName,\n })\n\n const table = this.tables[tableName]\n const relationshipsTable = this.tables[`${tableName}_rels`]\n\n if (collection.versions.drafts) {\n await db.execute(sql`\n UPDATE ${table}\n SET latest = false\n FROM ${relationshipsTable}\n WHERE ${table.id} = ${relationshipsTable.parent}\n AND ${relationshipsTable.path} = ${'parent'}\n AND ${relationshipsTable[`${collectionSlug}ID`]} = ${parent}\n AND ${table.id} != ${result.id};\n `)\n }\n\n return result\n}\n"],"names":["sql","buildVersionCollectionFields","toSnakeCase","upsertRow","createVersion","autosave","collectionSlug","parent","req","versionData","db","sessions","transactionID","drizzle","collection","payload","collections","config","collectionTableName","tableName","result","adapter","data","latest","version","fields","operation","table","tables","relationshipsTable","versions","drafts","execute","id","path"],"mappings":"AAGA,SAASA,GAAG,QAAQ,cAAa;AACjC,SAASC,4BAA4B,QAAQ,mBAAkB;AAC/D,OAAOC,iBAAiB,gBAAe;AAIvC,SAASC,SAAS,QAAQ,uBAAsB;AAEhD,OAAO,eAAeC,cAEpB,EACEC,QAAQ,EACRC,cAAc,EACdC,MAAM,EACNC,MAAM,CAAC,CAAmB,EAC1BC,WAAW,EACU;IAEvB,MAAMC,KAAK,IAAI,CAACC,QAAQ,CAACH,IAAII,aAAa,CAAC,EAAEF,MAAM,IAAI,CAACG,OAAO;IAC/D,MAAMC,aAAa,IAAI,CAACC,OAAO,CAACC,WAAW,CAACV,eAAe,CAACW,MAAM;IAClE,MAAMC,sBAAsBhB,YAAYI;IACxC,MAAMa,YAAY,CAAC,CAAC,EAAED,oBAAoB,EAAE,CAAC;IAE7C,MAAME,SAAS,MAAMjB,UAA8B;QACjDkB,SAAS,IAAI;QACbC,MAAM;YACJjB;YACAkB,QAAQ;YACRhB;YACAiB,SAASf;QACX;QACAC;QACAe,QAAQxB,6BAA6Ba;QACrCY,WAAW;QACXlB;QACAW;IACF;IAEA,MAAMQ,QAAQ,IAAI,CAACC,MAAM,CAACT,UAAU;IACpC,MAAMU,qBAAqB,IAAI,CAACD,MAAM,CAAC,CAAC,EAAET,UAAU,KAAK,CAAC,CAAC;IAE3D,IAAIL,WAAWgB,QAAQ,CAACC,MAAM,EAAE;QAC9B,MAAMrB,GAAGsB,OAAO,CAAChC,GAAG,CAAC;aACZ,EAAE2B,MAAM;;WAEV,EAAEE,mBAAmB;YACpB,EAAEF,MAAMM,EAAE,CAAC,GAAG,EAAEJ,mBAAmBtB,MAAM,CAAC;YAC1C,EAAEsB,mBAAmBK,IAAI,CAAC,GAAG,EAAE,SAAS;YACxC,EAAEL,kBAAkB,CAAC,CAAC,EAAEvB,eAAe,EAAE,CAAC,CAAC,CAAC,GAAG,EAAEC,OAAO;YACxD,EAAEoB,MAAMM,EAAE,CAAC,IAAI,EAAEb,OAAOa,EAAE,CAAC;IACnC,CAAC;IACH;IAEA,OAAOb;AACT"}
@@ -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,QAAQ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xB,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,IA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AASL;YACTD,OAAOzC,GACJgD,MAAM,CAAC;gBACNC,OAAOtE,GAAW,CAAC;wBACL,EAAEQ,QAAQkB,MAAM,CAACR,UAAU,CAACoC,EAAE,CAAC,CAAC,CAAC;YACjD,GACCiB,IAAI,CAAC9C,OACLN,KAAK,CAACA;QACX;QACAQ,YAAYgC,OAAOQ,WAAW,CAAC,EAAE,CAACG,KAAK;QACvC1C,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,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,6 +1,6 @@
1
1
  import toSnakeCase from 'to-snake-case';
2
2
  import { findMany } from './find/findMany.js';
3
- export const findGlobal = async function findGlobal({ locale, req, slug, where }) {
3
+ export const findGlobal = async function findGlobal({ slug, locale, req, where }) {
4
4
  const globalConfig = this.payload.globals.config.find((config)=>config.slug === slug);
5
5
  const tableName = toSnakeCase(slug);
6
6
  const { docs: [doc] } = await findMany({
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/findGlobal.ts"],"sourcesContent":["import type { FindGlobal } from 'payload/database'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { findMany } from './find/findMany.js'\n\nexport const findGlobal: FindGlobal = async function findGlobal(\n this: PostgresAdapter,\n { locale, req, slug, where },\n) {\n const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)\n const tableName = toSnakeCase(slug)\n\n const {\n docs: [doc],\n } = await findMany({\n adapter: this,\n fields: globalConfig.fields,\n limit: 1,\n locale,\n pagination: false,\n req,\n tableName,\n where,\n })\n\n if (doc) {\n doc.globalType = slug\n return doc\n }\n\n return {}\n}\n"],"names":["toSnakeCase","findMany","findGlobal","locale","req","slug","where","globalConfig","payload","globals","config","find","tableName","docs","doc","adapter","fields","limit","pagination","globalType"],"mappings":"AAEA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,QAAQ,QAAQ,qBAAoB;AAE7C,OAAO,MAAMC,aAAyB,eAAeA,WAEnD,EAAEC,MAAM,EAAEC,GAAG,EAAEC,IAAI,EAAEC,KAAK,EAAE;IAE5B,MAAMC,eAAe,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAACD,SAAWA,OAAOL,IAAI,KAAKA;IAClF,MAAMO,YAAYZ,YAAYK;IAE9B,MAAM,EACJQ,MAAM,CAACC,IAAI,EACZ,GAAG,MAAMb,SAAS;QACjBc,SAAS,IAAI;QACbC,QAAQT,aAAaS,MAAM;QAC3BC,OAAO;QACPd;QACAe,YAAY;QACZd;QACAQ;QACAN;IACF;IAEA,IAAIQ,KAAK;QACPA,IAAIK,UAAU,GAAGd;QACjB,OAAOS;IACT;IAEA,OAAO,CAAC;AACV,EAAC"}
1
+ {"version":3,"sources":["../src/findGlobal.ts"],"sourcesContent":["import type { FindGlobal } from 'payload/database'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from './types.js'\n\nimport { findMany } from './find/findMany.js'\n\nexport const findGlobal: FindGlobal = async function findGlobal(\n this: PostgresAdapter,\n { slug, locale, req, where },\n) {\n const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)\n const tableName = toSnakeCase(slug)\n\n const {\n docs: [doc],\n } = await findMany({\n adapter: this,\n fields: globalConfig.fields,\n limit: 1,\n locale,\n pagination: false,\n req,\n tableName,\n where,\n })\n\n if (doc) {\n doc.globalType = slug\n return doc\n }\n\n return {}\n}\n"],"names":["toSnakeCase","findMany","findGlobal","slug","locale","req","where","globalConfig","payload","globals","config","find","tableName","docs","doc","adapter","fields","limit","pagination","globalType"],"mappings":"AAEA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,QAAQ,QAAQ,qBAAoB;AAE7C,OAAO,MAAMC,aAAyB,eAAeA,WAEnD,EAAEC,IAAI,EAAEC,MAAM,EAAEC,GAAG,EAAEC,KAAK,EAAE;IAE5B,MAAMC,eAAe,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAACD,SAAWA,OAAOP,IAAI,KAAKA;IAClF,MAAMS,YAAYZ,YAAYG;IAE9B,MAAM,EACJU,MAAM,CAACC,IAAI,EACZ,GAAG,MAAMb,SAAS;QACjBc,SAAS,IAAI;QACbC,QAAQT,aAAaS,MAAM;QAC3BC,OAAO;QACPb;QACAc,YAAY;QACZb;QACAO;QACAN;IACF;IAEA,IAAIQ,KAAK;QACPA,IAAIK,UAAU,GAAGhB;QACjB,OAAOW;IACT;IAEA,OAAO,CAAC;AACV,EAAC"}