@payloadcms/drizzle 3.49.0-canary.8 → 3.49.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"deleteMany.d.ts","sourceRoot":"","sources":["../src/deleteMany.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAUzC,eAAO,MAAM,UAAU,EAAE,UAmCxB,CAAA"}
1
+ {"version":3,"file":"deleteMany.d.ts","sourceRoot":"","sources":["../src/deleteMany.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAWzC,eAAO,MAAM,UAAU,EAAE,UAmDxB,CAAA"}
@@ -1,34 +1,46 @@
1
1
  import { inArray } from 'drizzle-orm';
2
2
  import toSnakeCase from 'to-snake-case';
3
3
  import { findMany } from './find/findMany.js';
4
+ import { buildQuery } from './queries/buildQuery.js';
4
5
  import { getTransaction } from './utilities/getTransaction.js';
5
- export const deleteMany = async function deleteMany({ collection, req, where }) {
6
+ export const deleteMany = async function deleteMany({ collection, req, where: whereArg }) {
6
7
  const db = await getTransaction(this, req);
7
8
  const collectionConfig = this.payload.collections[collection].config;
8
9
  const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug));
9
- const result = await findMany({
10
+ const table = this.tables[tableName];
11
+ const { joins, where } = buildQuery({
10
12
  adapter: this,
11
13
  fields: collectionConfig.flattenedFields,
12
- joins: false,
13
- limit: 0,
14
14
  locale: req?.locale,
15
- page: 1,
16
- pagination: false,
17
- req,
18
15
  tableName,
19
- where
16
+ where: whereArg
20
17
  });
21
- const ids = [];
22
- result.docs.forEach((data)=>{
23
- ids.push(data.id);
24
- });
25
- if (ids.length > 0) {
26
- await this.deleteWhere({
27
- db,
18
+ let whereToUse = where;
19
+ if (joins?.length) {
20
+ // Difficult to support joins (through where referencing other tables) in deleteMany. => 2 separate queries.
21
+ // We can look into supporting this using one single query (through a subquery) in the future, though that's difficult to do in a generic way.
22
+ const result = await findMany({
23
+ adapter: this,
24
+ fields: collectionConfig.flattenedFields,
25
+ joins: false,
26
+ limit: 0,
27
+ locale: req?.locale,
28
+ page: 1,
29
+ pagination: false,
30
+ req,
31
+ select: {
32
+ id: true
33
+ },
28
34
  tableName,
29
- where: inArray(this.tables[tableName].id, ids)
35
+ where: whereArg
30
36
  });
37
+ whereToUse = inArray(table.id, result.docs.map((doc)=>doc.id));
31
38
  }
39
+ await this.deleteWhere({
40
+ db,
41
+ tableName,
42
+ where: whereToUse
43
+ });
32
44
  };
33
45
 
34
46
  //# sourceMappingURL=deleteMany.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/deleteMany.ts"],"sourcesContent":["import type { DeleteMany } from 'payload'\n\nimport { inArray } from 'drizzle-orm'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport { findMany } from './find/findMany.js'\nimport { getTransaction } from './utilities/getTransaction.js'\n\nexport const deleteMany: DeleteMany = async function deleteMany(\n this: DrizzleAdapter,\n { collection, req, where },\n) {\n const db = await getTransaction(this, req)\n const collectionConfig = this.payload.collections[collection].config\n\n const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug))\n\n const result = await findMany({\n adapter: this,\n fields: collectionConfig.flattenedFields,\n joins: false,\n limit: 0,\n locale: req?.locale,\n page: 1,\n pagination: false,\n req,\n tableName,\n where,\n })\n\n const ids = []\n\n result.docs.forEach((data) => {\n ids.push(data.id)\n })\n\n if (ids.length > 0) {\n await this.deleteWhere({\n db,\n tableName,\n where: inArray(this.tables[tableName].id, ids),\n })\n }\n}\n"],"names":["inArray","toSnakeCase","findMany","getTransaction","deleteMany","collection","req","where","db","collectionConfig","payload","collections","config","tableName","tableNameMap","get","slug","result","adapter","fields","flattenedFields","joins","limit","locale","page","pagination","ids","docs","forEach","data","push","id","length","deleteWhere","tables"],"mappings":"AAEA,SAASA,OAAO,QAAQ,cAAa;AACrC,OAAOC,iBAAiB,gBAAe;AAIvC,SAASC,QAAQ,QAAQ,qBAAoB;AAC7C,SAASC,cAAc,QAAQ,gCAA+B;AAE9D,OAAO,MAAMC,aAAyB,eAAeA,WAEnD,EAAEC,UAAU,EAAEC,GAAG,EAAEC,KAAK,EAAE;IAE1B,MAAMC,KAAK,MAAML,eAAe,IAAI,EAAEG;IACtC,MAAMG,mBAAmB,IAAI,CAACC,OAAO,CAACC,WAAW,CAACN,WAAW,CAACO,MAAM;IAEpE,MAAMC,YAAY,IAAI,CAACC,YAAY,CAACC,GAAG,CAACd,YAAYQ,iBAAiBO,IAAI;IAEzE,MAAMC,SAAS,MAAMf,SAAS;QAC5BgB,SAAS,IAAI;QACbC,QAAQV,iBAAiBW,eAAe;QACxCC,OAAO;QACPC,OAAO;QACPC,QAAQjB,KAAKiB;QACbC,MAAM;QACNC,YAAY;QACZnB;QACAO;QACAN;IACF;IAEA,MAAMmB,MAAM,EAAE;IAEdT,OAAOU,IAAI,CAACC,OAAO,CAAC,CAACC;QACnBH,IAAII,IAAI,CAACD,KAAKE,EAAE;IAClB;IAEA,IAAIL,IAAIM,MAAM,GAAG,GAAG;QAClB,MAAM,IAAI,CAACC,WAAW,CAAC;YACrBzB;YACAK;YACAN,OAAOP,QAAQ,IAAI,CAACkC,MAAM,CAACrB,UAAU,CAACkB,EAAE,EAAEL;QAC5C;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../src/deleteMany.ts"],"sourcesContent":["import type { DeleteMany } from 'payload'\n\nimport { inArray } from 'drizzle-orm'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport { findMany } from './find/findMany.js'\nimport { buildQuery } from './queries/buildQuery.js'\nimport { getTransaction } from './utilities/getTransaction.js'\n\nexport const deleteMany: DeleteMany = async function deleteMany(\n this: DrizzleAdapter,\n { collection, req, where: whereArg },\n) {\n const db = await getTransaction(this, req)\n const collectionConfig = this.payload.collections[collection].config\n\n const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug))\n\n const table = this.tables[tableName]\n\n const { joins, where } = buildQuery({\n adapter: this,\n fields: collectionConfig.flattenedFields,\n locale: req?.locale,\n tableName,\n where: whereArg,\n })\n\n let whereToUse = where\n\n if (joins?.length) {\n // Difficult to support joins (through where referencing other tables) in deleteMany. => 2 separate queries.\n // We can look into supporting this using one single query (through a subquery) in the future, though that's difficult to do in a generic way.\n const result = await findMany({\n adapter: this,\n fields: collectionConfig.flattenedFields,\n joins: false,\n limit: 0,\n locale: req?.locale,\n page: 1,\n pagination: false,\n req,\n select: {\n id: true,\n },\n tableName,\n where: whereArg,\n })\n\n whereToUse = inArray(\n table.id,\n result.docs.map((doc) => doc.id),\n )\n }\n\n await this.deleteWhere({\n db,\n tableName,\n where: whereToUse,\n })\n}\n"],"names":["inArray","toSnakeCase","findMany","buildQuery","getTransaction","deleteMany","collection","req","where","whereArg","db","collectionConfig","payload","collections","config","tableName","tableNameMap","get","slug","table","tables","joins","adapter","fields","flattenedFields","locale","whereToUse","length","result","limit","page","pagination","select","id","docs","map","doc","deleteWhere"],"mappings":"AAEA,SAASA,OAAO,QAAQ,cAAa;AACrC,OAAOC,iBAAiB,gBAAe;AAIvC,SAASC,QAAQ,QAAQ,qBAAoB;AAC7C,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,gCAA+B;AAE9D,OAAO,MAAMC,aAAyB,eAAeA,WAEnD,EAAEC,UAAU,EAAEC,GAAG,EAAEC,OAAOC,QAAQ,EAAE;IAEpC,MAAMC,KAAK,MAAMN,eAAe,IAAI,EAAEG;IACtC,MAAMI,mBAAmB,IAAI,CAACC,OAAO,CAACC,WAAW,CAACP,WAAW,CAACQ,MAAM;IAEpE,MAAMC,YAAY,IAAI,CAACC,YAAY,CAACC,GAAG,CAAChB,YAAYU,iBAAiBO,IAAI;IAEzE,MAAMC,QAAQ,IAAI,CAACC,MAAM,CAACL,UAAU;IAEpC,MAAM,EAAEM,KAAK,EAAEb,KAAK,EAAE,GAAGL,WAAW;QAClCmB,SAAS,IAAI;QACbC,QAAQZ,iBAAiBa,eAAe;QACxCC,QAAQlB,KAAKkB;QACbV;QACAP,OAAOC;IACT;IAEA,IAAIiB,aAAalB;IAEjB,IAAIa,OAAOM,QAAQ;QACjB,4GAA4G;QAC5G,8IAA8I;QAC9I,MAAMC,SAAS,MAAM1B,SAAS;YAC5BoB,SAAS,IAAI;YACbC,QAAQZ,iBAAiBa,eAAe;YACxCH,OAAO;YACPQ,OAAO;YACPJ,QAAQlB,KAAKkB;YACbK,MAAM;YACNC,YAAY;YACZxB;YACAyB,QAAQ;gBACNC,IAAI;YACN;YACAlB;YACAP,OAAOC;QACT;QAEAiB,aAAa1B,QACXmB,MAAMc,EAAE,EACRL,OAAOM,IAAI,CAACC,GAAG,CAAC,CAACC,MAAQA,IAAIH,EAAE;IAEnC;IAEA,MAAM,IAAI,CAACI,WAAW,CAAC;QACrB3B;QACAK;QACAP,OAAOkB;IACT;AACF,EAAC"}
@@ -10,8 +10,8 @@ export const countDistinct = async function countDistinct({ column, db, joins, t
10
10
  let query = db.select({
11
11
  count: sql`COUNT(1) OVER()`
12
12
  }).from(this.tables[tableName]).where(where).groupBy(column || this.tables[tableName].id).limit(1).$dynamic();
13
- joins.forEach(({ condition, table })=>{
14
- query = query.leftJoin(table, condition);
13
+ joins.forEach(({ type, condition, table })=>{
14
+ query = query[type ?? 'leftJoin'](table, condition);
15
15
  });
16
16
  // When we have any joins, we need to count each individual ID only once.
17
17
  // COUNT(*) doesn't work for this well in this case, as it also counts joined tables.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/postgres/countDistinct.ts"],"sourcesContent":["import type { PgTableWithColumns } from 'drizzle-orm/pg-core'\n\nimport { count, sql } from 'drizzle-orm'\n\nimport type { BasePostgresAdapter, CountDistinct } from './types.js'\n\nexport const countDistinct: CountDistinct = async function countDistinct(\n this: BasePostgresAdapter,\n { column, db, joins, tableName, where },\n) {\n // When we don't have any joins - use a simple COUNT(*) query.\n if (joins.length === 0) {\n const countResult = await db\n .select({\n count: column ? count(sql`DISTINCT ${column}`) : count(),\n })\n .from(this.tables[tableName])\n .where(where)\n\n return Number(countResult?.[0]?.count ?? 0)\n }\n\n let query = db\n .select({\n count: sql`COUNT(1) OVER()`,\n })\n .from(this.tables[tableName])\n .where(where)\n .groupBy(column || this.tables[tableName].id)\n .limit(1)\n .$dynamic()\n\n joins.forEach(({ condition, table }) => {\n query = query.leftJoin(table as PgTableWithColumns<any>, condition)\n })\n\n // When we have any joins, we need to count each individual ID only once.\n // COUNT(*) doesn't work for this well in this case, as it also counts joined tables.\n // SELECT (COUNT DISTINCT id) has a very slow performance on large tables.\n // Instead, COUNT (GROUP BY id) can be used which is still slower than COUNT(*) but acceptable.\n const countResult = await query\n\n return Number(countResult?.[0]?.count ?? 0)\n}\n"],"names":["count","sql","countDistinct","column","db","joins","tableName","where","length","countResult","select","from","tables","Number","query","groupBy","id","limit","$dynamic","forEach","condition","table","leftJoin"],"mappings":"AAEA,SAASA,KAAK,EAAEC,GAAG,QAAQ,cAAa;AAIxC,OAAO,MAAMC,gBAA+B,eAAeA,cAEzD,EAAEC,MAAM,EAAEC,EAAE,EAAEC,KAAK,EAAEC,SAAS,EAAEC,KAAK,EAAE;IAEvC,8DAA8D;IAC9D,IAAIF,MAAMG,MAAM,KAAK,GAAG;QACtB,MAAMC,cAAc,MAAML,GACvBM,MAAM,CAAC;YACNV,OAAOG,SAASH,MAAMC,GAAG,CAAC,SAAS,EAAEE,OAAO,CAAC,IAAIH;QACnD,GACCW,IAAI,CAAC,IAAI,CAACC,MAAM,CAACN,UAAU,EAC3BC,KAAK,CAACA;QAET,OAAOM,OAAOJ,aAAa,CAAC,EAAE,EAAET,SAAS;IAC3C;IAEA,IAAIc,QAAQV,GACTM,MAAM,CAAC;QACNV,OAAOC,GAAG,CAAC,eAAe,CAAC;IAC7B,GACCU,IAAI,CAAC,IAAI,CAACC,MAAM,CAACN,UAAU,EAC3BC,KAAK,CAACA,OACNQ,OAAO,CAACZ,UAAU,IAAI,CAACS,MAAM,CAACN,UAAU,CAACU,EAAE,EAC3CC,KAAK,CAAC,GACNC,QAAQ;IAEXb,MAAMc,OAAO,CAAC,CAAC,EAAEC,SAAS,EAAEC,KAAK,EAAE;QACjCP,QAAQA,MAAMQ,QAAQ,CAACD,OAAkCD;IAC3D;IAEA,yEAAyE;IACzE,qFAAqF;IACrF,0EAA0E;IAC1E,+FAA+F;IAC/F,MAAMX,cAAc,MAAMK;IAE1B,OAAOD,OAAOJ,aAAa,CAAC,EAAE,EAAET,SAAS;AAC3C,EAAC"}
1
+ {"version":3,"sources":["../../src/postgres/countDistinct.ts"],"sourcesContent":["import type { PgTableWithColumns } from 'drizzle-orm/pg-core'\n\nimport { count, sql } from 'drizzle-orm'\n\nimport type { BasePostgresAdapter, CountDistinct } from './types.js'\n\nexport const countDistinct: CountDistinct = async function countDistinct(\n this: BasePostgresAdapter,\n { column, db, joins, tableName, where },\n) {\n // When we don't have any joins - use a simple COUNT(*) query.\n if (joins.length === 0) {\n const countResult = await db\n .select({\n count: column ? count(sql`DISTINCT ${column}`) : count(),\n })\n .from(this.tables[tableName])\n .where(where)\n\n return Number(countResult?.[0]?.count ?? 0)\n }\n\n let query = db\n .select({\n count: sql`COUNT(1) OVER()`,\n })\n .from(this.tables[tableName])\n .where(where)\n .groupBy(column || this.tables[tableName].id)\n .limit(1)\n .$dynamic()\n\n joins.forEach(({ type, condition, table }) => {\n query = query[type ?? 'leftJoin'](table as PgTableWithColumns<any>, condition)\n })\n\n // When we have any joins, we need to count each individual ID only once.\n // COUNT(*) doesn't work for this well in this case, as it also counts joined tables.\n // SELECT (COUNT DISTINCT id) has a very slow performance on large tables.\n // Instead, COUNT (GROUP BY id) can be used which is still slower than COUNT(*) but acceptable.\n const countResult = await query\n\n return Number(countResult?.[0]?.count ?? 0)\n}\n"],"names":["count","sql","countDistinct","column","db","joins","tableName","where","length","countResult","select","from","tables","Number","query","groupBy","id","limit","$dynamic","forEach","type","condition","table"],"mappings":"AAEA,SAASA,KAAK,EAAEC,GAAG,QAAQ,cAAa;AAIxC,OAAO,MAAMC,gBAA+B,eAAeA,cAEzD,EAAEC,MAAM,EAAEC,EAAE,EAAEC,KAAK,EAAEC,SAAS,EAAEC,KAAK,EAAE;IAEvC,8DAA8D;IAC9D,IAAIF,MAAMG,MAAM,KAAK,GAAG;QACtB,MAAMC,cAAc,MAAML,GACvBM,MAAM,CAAC;YACNV,OAAOG,SAASH,MAAMC,GAAG,CAAC,SAAS,EAAEE,OAAO,CAAC,IAAIH;QACnD,GACCW,IAAI,CAAC,IAAI,CAACC,MAAM,CAACN,UAAU,EAC3BC,KAAK,CAACA;QAET,OAAOM,OAAOJ,aAAa,CAAC,EAAE,EAAET,SAAS;IAC3C;IAEA,IAAIc,QAAQV,GACTM,MAAM,CAAC;QACNV,OAAOC,GAAG,CAAC,eAAe,CAAC;IAC7B,GACCU,IAAI,CAAC,IAAI,CAACC,MAAM,CAACN,UAAU,EAC3BC,KAAK,CAACA,OACNQ,OAAO,CAACZ,UAAU,IAAI,CAACS,MAAM,CAACN,UAAU,CAACU,EAAE,EAC3CC,KAAK,CAAC,GACNC,QAAQ;IAEXb,MAAMc,OAAO,CAAC,CAAC,EAAEC,IAAI,EAAEC,SAAS,EAAEC,KAAK,EAAE;QACvCR,QAAQA,KAAK,CAACM,QAAQ,WAAW,CAACE,OAAkCD;IACtE;IAEA,yEAAyE;IACzE,qFAAqF;IACrF,0EAA0E;IAC1E,+FAA+F;IAC/F,MAAMZ,cAAc,MAAMK;IAE1B,OAAOD,OAAOJ,aAAa,CAAC,EAAE,EAAET,SAAS;AAC3C,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"sanitizeQueryValue.d.ts","sourceRoot":"","sources":["../../src/queries/sanitizeQueryValue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAEtC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AAC,CAAA;KACxB,EAAE,CAAA;IACH,KAAK,EAAE,KAAK,GAAG,UAAU,CAAA;IACzB,MAAM,EAAE,OAAO,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;IACtB,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,KAAK,eAAe,GAAG;IACrB,SAAS,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;IACvB,KAAK,EAAE,OAAO,CAAA;CACf,CAAA;AAED,eAAO,MAAM,kBAAkB,qFAQ5B,sBAAsB,KAAG;IAC1B,OAAO,CAAC,EAAE,eAAe,EAAE,CAAA;IAC3B,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,OAAO,CAAA;CAuNf,CAAA"}
@@ -74,12 +74,17 @@ export const sanitizeQueryValue = ({ adapter, columns, field, isUUID, operator:
74
74
  }
75
75
  if (field.type === 'date' && operator !== 'exists') {
76
76
  if (typeof val === 'string') {
77
- formattedValue = new Date(val).toISOString();
78
- if (Number.isNaN(Date.parse(formattedValue))) {
79
- return {
80
- operator,
81
- value: undefined
82
- };
77
+ if (val === 'null' || val === '') {
78
+ formattedValue = null;
79
+ } else {
80
+ const date = new Date(val);
81
+ if (Number.isNaN(date.getTime())) {
82
+ return {
83
+ operator,
84
+ value: undefined
85
+ };
86
+ }
87
+ formattedValue = date.toISOString();
83
88
  }
84
89
  } else if (typeof val === 'number') {
85
90
  formattedValue = new Date(val).toISOString();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/queries/sanitizeQueryValue.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\n\nimport { APIError, createArrayFromCommaDelineated, type Field, type TabAsField } from 'payload'\nimport { fieldAffectsData } from 'payload/shared'\nimport { validate as uuidValidate } from 'uuid'\n\nimport type { DrizzleAdapter } from '../types.js'\n\nimport { getCollectionIdType } from '../utilities/getCollectionIdType.js'\nimport { isPolymorphicRelationship } from '../utilities/isPolymorphicRelationship.js'\nimport { isRawConstraint } from '../utilities/rawConstraint.js'\n\ntype SanitizeQueryValueArgs = {\n adapter: DrizzleAdapter\n columns?: {\n idType: 'number' | 'text' | 'uuid'\n rawColumn: SQL<unknown>\n }[]\n field: Field | TabAsField\n isUUID: boolean\n operator: string\n relationOrPath: string\n val: any\n}\n\ntype SanitizedColumn = {\n rawColumn: SQL<unknown>\n value: unknown\n}\n\nexport const sanitizeQueryValue = ({\n adapter,\n columns,\n field,\n isUUID,\n operator: operatorArg,\n relationOrPath,\n val,\n}: SanitizeQueryValueArgs): {\n columns?: SanitizedColumn[]\n operator: string\n value: unknown\n} => {\n let operator = operatorArg\n let formattedValue = val\n let formattedColumns: SanitizedColumn[]\n\n if (!fieldAffectsData(field)) {\n return { operator, value: formattedValue }\n }\n\n if (isRawConstraint(val)) {\n return { operator, value: val.value }\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') {\n formattedValue = true\n }\n if (val.toLowerCase() === 'false') {\n formattedValue = false\n }\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 } else if (typeof formattedValue === 'number') {\n formattedValue = [formattedValue]\n }\n\n if (!Array.isArray(formattedValue)) {\n return null\n }\n }\n\n if (field.type === 'number' && typeof formattedValue === 'string') {\n formattedValue = Number(val)\n\n if (Number.isNaN(formattedValue)) {\n formattedValue = null\n }\n }\n\n if (isUUID && typeof formattedValue === 'string') {\n if (!uuidValidate(val)) {\n formattedValue = null\n }\n }\n\n if (field.type === 'date' && operator !== 'exists') {\n if (typeof val === 'string') {\n formattedValue = new Date(val).toISOString()\n if (Number.isNaN(Date.parse(formattedValue))) {\n return { operator, value: undefined }\n }\n } else if (typeof val === 'number') {\n formattedValue = new Date(val).toISOString()\n }\n }\n\n if (field.type === 'relationship' || field.type === 'upload') {\n if (val === 'null') {\n formattedValue = null\n } else if (!(formattedValue === null || typeof formattedValue === 'boolean')) {\n // convert the value to the idType of the relationship\n let idType: 'number' | 'text'\n if (typeof field.relationTo === 'string') {\n idType = getCollectionIdType({\n adapter,\n collection: adapter.payload.collections[field.relationTo],\n })\n } else {\n if (isPolymorphicRelationship(val)) {\n if (operator !== 'equals') {\n throw new APIError(\n `Only 'equals' operator is supported for polymorphic relationship object notation. Given - ${operator}`,\n )\n }\n idType = getCollectionIdType({\n adapter,\n collection: adapter.payload.collections[val.relationTo],\n })\n\n if (isRawConstraint(val.value)) {\n return {\n operator,\n value: val.value.value,\n }\n }\n return {\n operator,\n value: idType === 'number' ? Number(val.value) : String(val.value),\n }\n }\n\n formattedColumns = columns\n .map(({ idType, rawColumn }) => {\n let formattedValue: number | number[] | string | string[]\n\n if (Array.isArray(val)) {\n formattedValue = val\n .map((eachVal) => {\n let formattedValue: number | string\n\n if (idType === 'number') {\n formattedValue = Number(eachVal)\n\n if (Number.isNaN(formattedValue)) {\n return null\n }\n } else {\n if (idType === 'uuid' && !uuidValidate(eachVal)) {\n return null\n }\n\n formattedValue = String(eachVal)\n }\n\n return formattedValue\n })\n .filter(Boolean) as number[] | string[]\n } else if (idType === 'number') {\n formattedValue = Number(val)\n\n if (Number.isNaN(formattedValue)) {\n return null\n }\n } else {\n formattedValue = String(val)\n }\n\n return {\n rawColumn,\n value: formattedValue,\n }\n })\n .filter(Boolean)\n }\n if (Array.isArray(formattedValue)) {\n formattedValue = formattedValue.map((value) => {\n if (idType === 'number') {\n return Number(value)\n }\n if (idType === 'text') {\n return String(value)\n }\n return value\n })\n } else {\n if (idType === 'number') {\n formattedValue = Number(val)\n }\n if (idType === 'text') {\n formattedValue = String(val)\n }\n }\n }\n }\n\n if ('hasMany' in field && field.hasMany && operator === 'contains') {\n operator = 'equals'\n }\n\n if (operator === 'near' && field.type === 'point' && typeof formattedValue === 'string') {\n const [lng, lat, maxDistance, minDistance] = formattedValue.split(',')\n\n formattedValue = [Number(lng), Number(lat), Number(maxDistance), Number(minDistance)]\n }\n\n if (operator === 'contains') {\n formattedValue = `%${formattedValue}%`\n }\n\n if (operator === 'exists') {\n formattedValue = val === 'true' || val === true\n\n if (formattedValue) {\n operator = 'exists'\n } else {\n operator = 'isNull'\n }\n }\n\n return {\n columns: formattedColumns,\n operator,\n value: formattedValue,\n }\n}\n"],"names":["APIError","createArrayFromCommaDelineated","fieldAffectsData","validate","uuidValidate","getCollectionIdType","isPolymorphicRelationship","isRawConstraint","sanitizeQueryValue","adapter","columns","field","isUUID","operator","operatorArg","relationOrPath","val","formattedValue","formattedColumns","value","type","endsWith","Array","isArray","allPossibleIDTypes","forEach","idType","push","parseInt","String","toLowerCase","includes","map","arrayVal","parseFloat","Number","isNaN","Date","toISOString","parse","undefined","relationTo","collection","payload","collections","rawColumn","eachVal","filter","Boolean","hasMany","lng","lat","maxDistance","minDistance","split"],"mappings":"AAEA,SAASA,QAAQ,EAAEC,8BAA8B,QAAqC,UAAS;AAC/F,SAASC,gBAAgB,QAAQ,iBAAgB;AACjD,SAASC,YAAYC,YAAY,QAAQ,OAAM;AAI/C,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,yBAAyB,QAAQ,4CAA2C;AACrF,SAASC,eAAe,QAAQ,gCAA+B;AAoB/D,OAAO,MAAMC,qBAAqB,CAAC,EACjCC,OAAO,EACPC,OAAO,EACPC,KAAK,EACLC,MAAM,EACNC,UAAUC,WAAW,EAC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WAAW,UAAU;oBACvBT,iBAAiBkB,OAAOnB;gBAC1B;gBACA,IAAIU,WAAW,QAAQ;oBACrBT,iBAAiBY,OAAOb;gBAC1B;YACF;QACF;IACF;IAEA,IAAI,aAAaL,SAASA,MAAMsC,OAAO,IAAIpC,aAAa,YAAY;QAClEA,WAAW;IACb;IAEA,IAAIA,aAAa,UAAUF,MAAMS,IAAI,KAAK,WAAW,OAAOH,mBAAmB,UAAU;QACvF,MAAM,CAACiC,KAAKC,KAAKC,aAAaC,YAAY,GAAGpC,eAAeqC,KAAK,CAAC;QAElErC,iBAAiB;YAACkB,OAAOe;YAAMf,OAAOgB;YAAMhB,OAAOiB;YAAcjB,OAAOkB;SAAa;IACvF;IAEA,IAAIxC,aAAa,YAAY;QAC3BI,iBAAiB,CAAC,CAAC,EAAEA,eAAe,CAAC,CAAC;IACxC;IAEA,IAAIJ,aAAa,UAAU;QACzBI,iBAAiBD,QAAQ,UAAUA,QAAQ;QAE3C,IAAIC,gBAAgB;YAClBJ,WAAW;QACb,OAAO;YACLA,WAAW;QACb;IACF;IAEA,OAAO;QACLH,SAASQ;QACTL;QACAM,OAAOF;IACT;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/queries/sanitizeQueryValue.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\n\nimport { APIError, createArrayFromCommaDelineated, type Field, type TabAsField } from 'payload'\nimport { fieldAffectsData } from 'payload/shared'\nimport { validate as uuidValidate } from 'uuid'\n\nimport type { DrizzleAdapter } from '../types.js'\n\nimport { getCollectionIdType } from '../utilities/getCollectionIdType.js'\nimport { isPolymorphicRelationship } from '../utilities/isPolymorphicRelationship.js'\nimport { isRawConstraint } from '../utilities/rawConstraint.js'\n\ntype SanitizeQueryValueArgs = {\n adapter: DrizzleAdapter\n columns?: {\n idType: 'number' | 'text' | 'uuid'\n rawColumn: SQL<unknown>\n }[]\n field: Field | TabAsField\n isUUID: boolean\n operator: string\n relationOrPath: string\n val: any\n}\n\ntype SanitizedColumn = {\n rawColumn: SQL<unknown>\n value: unknown\n}\n\nexport const sanitizeQueryValue = ({\n adapter,\n columns,\n field,\n isUUID,\n operator: operatorArg,\n relationOrPath,\n val,\n}: SanitizeQueryValueArgs): {\n columns?: SanitizedColumn[]\n operator: string\n value: unknown\n} => {\n let operator = operatorArg\n let formattedValue = val\n let formattedColumns: SanitizedColumn[]\n\n if (!fieldAffectsData(field)) {\n return { operator, value: formattedValue }\n }\n\n if (isRawConstraint(val)) {\n return { operator, value: val.value }\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') {\n formattedValue = true\n }\n if (val.toLowerCase() === 'false') {\n formattedValue = false\n }\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 } else if (typeof formattedValue === 'number') {\n formattedValue = [formattedValue]\n }\n\n if (!Array.isArray(formattedValue)) {\n return null\n }\n }\n\n if (field.type === 'number' && typeof formattedValue === 'string') {\n formattedValue = Number(val)\n\n if (Number.isNaN(formattedValue)) {\n formattedValue = null\n }\n }\n\n if (isUUID && typeof formattedValue === 'string') {\n if (!uuidValidate(val)) {\n formattedValue = null\n }\n }\n\n if (field.type === 'date' && operator !== 'exists') {\n if (typeof val === 'string') {\n if (val === 'null' || val === '') {\n formattedValue = null\n } else {\n const date = new Date(val)\n if (Number.isNaN(date.getTime())) {\n return { operator, value: undefined }\n }\n formattedValue = date.toISOString()\n }\n } else if (typeof val === 'number') {\n formattedValue = new Date(val).toISOString()\n }\n }\n\n if (field.type === 'relationship' || field.type === 'upload') {\n if (val === 'null') {\n formattedValue = null\n } else if (!(formattedValue === null || typeof formattedValue === 'boolean')) {\n // convert the value to the idType of the relationship\n let idType: 'number' | 'text'\n if (typeof field.relationTo === 'string') {\n idType = getCollectionIdType({\n adapter,\n collection: adapter.payload.collections[field.relationTo],\n })\n } else {\n if (isPolymorphicRelationship(val)) {\n if (operator !== 'equals') {\n throw new APIError(\n `Only 'equals' operator is supported for polymorphic relationship object notation. Given - ${operator}`,\n )\n }\n idType = getCollectionIdType({\n adapter,\n collection: adapter.payload.collections[val.relationTo],\n })\n\n if (isRawConstraint(val.value)) {\n return {\n operator,\n value: val.value.value,\n }\n }\n return {\n operator,\n value: idType === 'number' ? Number(val.value) : String(val.value),\n }\n }\n\n formattedColumns = columns\n .map(({ idType, rawColumn }) => {\n let formattedValue: number | number[] | string | string[]\n\n if (Array.isArray(val)) {\n formattedValue = val\n .map((eachVal) => {\n let formattedValue: number | string\n\n if (idType === 'number') {\n formattedValue = Number(eachVal)\n\n if (Number.isNaN(formattedValue)) {\n return null\n }\n } else {\n if (idType === 'uuid' && !uuidValidate(eachVal)) {\n return null\n }\n\n formattedValue = String(eachVal)\n }\n\n return formattedValue\n })\n .filter(Boolean) as number[] | string[]\n } else if (idType === 'number') {\n formattedValue = Number(val)\n\n if (Number.isNaN(formattedValue)) {\n return null\n }\n } else {\n formattedValue = String(val)\n }\n\n return {\n rawColumn,\n value: formattedValue,\n }\n })\n .filter(Boolean)\n }\n if (Array.isArray(formattedValue)) {\n formattedValue = formattedValue.map((value) => {\n if (idType === 'number') {\n return Number(value)\n }\n if (idType === 'text') {\n return String(value)\n }\n return value\n })\n } else {\n if (idType === 'number') {\n formattedValue = Number(val)\n }\n if (idType === 'text') {\n formattedValue = String(val)\n }\n }\n }\n }\n\n if ('hasMany' in field && field.hasMany && operator === 'contains') {\n operator = 'equals'\n }\n\n if (operator === 'near' && field.type === 'point' && typeof formattedValue === 'string') {\n const [lng, lat, maxDistance, minDistance] = formattedValue.split(',')\n\n formattedValue = [Number(lng), Number(lat), Number(maxDistance), Number(minDistance)]\n }\n\n if (operator === 'contains') {\n formattedValue = `%${formattedValue}%`\n }\n\n if (operator === 'exists') {\n formattedValue = val === 'true' || val === true\n\n if (formattedValue) {\n operator = 'exists'\n } else {\n operator = 'isNull'\n }\n }\n\n return {\n columns: formattedColumns,\n operator,\n value: formattedValue,\n }\n}\n"],"names":["APIError","createArrayFromCommaDelineated","fieldAffectsData","validate","uuidValidate","getCollectionIdType","isPolymorphicRelationship","isRawConstraint","sanitizeQueryValue","adapter","columns","field","isUUID","operator","operatorArg","relationOrPath","val","formattedValue","formattedColumns","value","type","endsWith","Array","isArray","allPossibleIDTypes","forEach","idType","push","parseInt","String","toLowerCase","includes","map","arrayVal","parseFloat","Number","isNaN","date","Date","getTime","undefined","toISOString","relationTo","collection","payload","collections","rawColumn","eachVal","filter","Boolean","hasMany","lng","lat","maxDistance","minDistance","split"],"mappings":"AAEA,SAASA,QAAQ,EAAEC,8BAA8B,QAAqC,UAAS;AAC/F,SAASC,gBAAgB,QAAQ,iBAAgB;AACjD,SAASC,YAAYC,YAAY,QAAQ,OAAM;AAI/C,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,yBAAyB,QAAQ,4CAA2C;AACrF,SAASC,eAAe,QAAQ,gCAA+B;AAoB/D,OAAO,MAAMC,qBAAqB,CAAC,EACjCC,OAAO,EACPC,OAAO,EACPC,KAAK,EACLC,MAAM,EACNC,UAAUC,WAAW,EACrBC,cAAc,EACdC,GAAG,EACoB;IAKvB,IAAIH,WAAWC;IACf,IAAIG,iBAAiBD;IACrB,IAAIE;IAEJ,IAAI,CAAChB,iBAAiBS,QAAQ;QAC5B,OAAO;YAAEE;YAAUM,OAAOF;QA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AQ;oBACrBT,iBAAiBY,OAAOb;gBAC1B;YACF;QACF;IACF;IAEA,IAAI,aAAaL,SAASA,MAAMuC,OAAO,IAAIrC,aAAa,YAAY;QAClEA,WAAW;IACb;IAEA,IAAIA,aAAa,UAAUF,MAAMS,IAAI,KAAK,WAAW,OAAOH,mBAAmB,UAAU;QACvF,MAAM,CAACkC,KAAKC,KAAKC,aAAaC,YAAY,GAAGrC,eAAesC,KAAK,CAAC;QAElEtC,iBAAiB;YAACkB,OAAOgB;YAAMhB,OAAOiB;YAAMjB,OAAOkB;YAAclB,OAAOmB;SAAa;IACvF;IAEA,IAAIzC,aAAa,YAAY;QAC3BI,iBAAiB,CAAC,CAAC,EAAEA,eAAe,CAAC,CAAC;IACxC;IAEA,IAAIJ,aAAa,UAAU;QACzBI,iBAAiBD,QAAQ,UAAUA,QAAQ;QAE3C,IAAIC,gBAAgB;YAClBJ,WAAW;QACb,OAAO;YACLA,WAAW;QACb;IACF;IAEA,OAAO;QACLH,SAASQ;QACTL;QACAM,OAAOF;IACT;AACF,EAAC"}
@@ -13,8 +13,8 @@
13
13
  if (where) {
14
14
  query = query.where(where);
15
15
  }
16
- joins.forEach(({ condition, table })=>{
17
- query = query.leftJoin(table, condition);
16
+ joins.forEach(({ type, condition, table })=>{
17
+ query = query[type ?? 'leftJoin'](table, condition);
18
18
  });
19
19
  return queryModifier({
20
20
  query
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/queries/selectDistinct.ts"],"sourcesContent":["import type { QueryPromise, SQL } from 'drizzle-orm'\nimport type { SQLiteColumn, SQLiteSelect } from 'drizzle-orm/sqlite-core'\n\nimport type {\n DrizzleAdapter,\n DrizzleTransaction,\n GenericColumn,\n GenericPgColumn,\n TransactionPg,\n TransactionSQLite,\n} from '../types.js'\nimport type { BuildQueryJoinAliases } from './buildQuery.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n forceRun?: boolean\n joins: BuildQueryJoinAliases\n query?: (args: { query: SQLiteSelect }) => SQLiteSelect\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 db,\n forceRun,\n joins,\n query: queryModifier = ({ query }) => query,\n selectFields,\n tableName,\n where,\n}: Args): QueryPromise<{ id: number | string }[] & Record<string, GenericColumn>> => {\n if (forceRun || Object.keys(joins).length > 0) {\n let query: SQLiteSelect\n const table = adapter.tables[tableName]\n\n if (adapter.name === 'postgres') {\n query = (db as TransactionPg)\n .selectDistinct(selectFields as Record<string, GenericPgColumn>)\n .from(table)\n .$dynamic() as unknown as SQLiteSelect\n }\n if (adapter.name === 'sqlite') {\n query = (db as TransactionSQLite)\n .selectDistinct(selectFields as Record<string, SQLiteColumn>)\n .from(table)\n .$dynamic()\n }\n\n if (where) {\n query = query.where(where)\n }\n\n joins.forEach(({ condition, table }) => {\n query = query.leftJoin(table, condition)\n })\n\n return queryModifier({\n query,\n }) as unknown as QueryPromise<{ id: number | string }[] & Record<string, GenericColumn>>\n }\n}\n"],"names":["selectDistinct","adapter","db","forceRun","joins","query","queryModifier","selectFields","tableName","where","Object","keys","length","table","tables","name","from","$dynamic","forEach","condition","leftJoin"],"mappings":"AAwBA;;CAEC,GACD,OAAO,MAAMA,iBAAiB,CAAC,EAC7BC,OAAO,EACPC,EAAE,EACFC,QAAQ,EACRC,KAAK,EACLC,OAAOC,gBAAgB,CAAC,EAAED,KAAK,EAAE,GAAKA,KAAK,EAC3CE,YAAY,EACZC,SAAS,EACTC,KAAK,EACA;IACL,IAAIN,YAAYO,OAAOC,IAAI,CAACP,OAAOQ,MAAM,GAAG,GAAG;QAC7C,IAAIP;QACJ,MAAMQ,QAAQZ,QAAQa,MAAM,CAACN,UAAU;QAEvC,IAAIP,QAAQc,IAAI,KAAK,YAAY;YAC/BV,QAAQ,AAACH,GACNF,cAAc,CAACO,cACfS,IAAI,CAACH,OACLI,QAAQ;QACb;QACA,IAAIhB,QAAQc,IAAI,KAAK,UAAU;YAC7BV,QAAQ,AAACH,GACNF,cAAc,CAACO,cACfS,IAAI,CAACH,OACLI,QAAQ;QACb;QAEA,IAAIR,OAAO;YACTJ,QAAQA,MAAMI,KAAK,CAACA;QACtB;QAEAL,MAAMc,OAAO,CAAC,CAAC,EAAEC,SAAS,EAAEN,KAAK,EAAE;YACjCR,QAAQA,MAAMe,QAAQ,CAACP,OAAOM;QAChC;QAEA,OAAOb,cAAc;YACnBD;QACF;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/queries/selectDistinct.ts"],"sourcesContent":["import type { QueryPromise, SQL } from 'drizzle-orm'\nimport type { SQLiteColumn, SQLiteSelect } from 'drizzle-orm/sqlite-core'\n\nimport type {\n DrizzleAdapter,\n DrizzleTransaction,\n GenericColumn,\n GenericPgColumn,\n TransactionPg,\n TransactionSQLite,\n} from '../types.js'\nimport type { BuildQueryJoinAliases } from './buildQuery.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n forceRun?: boolean\n joins: BuildQueryJoinAliases\n query?: (args: { query: SQLiteSelect }) => SQLiteSelect\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 db,\n forceRun,\n joins,\n query: queryModifier = ({ query }) => query,\n selectFields,\n tableName,\n where,\n}: Args): QueryPromise<{ id: number | string }[] & Record<string, GenericColumn>> => {\n if (forceRun || Object.keys(joins).length > 0) {\n let query: SQLiteSelect\n const table = adapter.tables[tableName]\n\n if (adapter.name === 'postgres') {\n query = (db as TransactionPg)\n .selectDistinct(selectFields as Record<string, GenericPgColumn>)\n .from(table)\n .$dynamic() as unknown as SQLiteSelect\n }\n if (adapter.name === 'sqlite') {\n query = (db as TransactionSQLite)\n .selectDistinct(selectFields as Record<string, SQLiteColumn>)\n .from(table)\n .$dynamic()\n }\n\n if (where) {\n query = query.where(where)\n }\n\n joins.forEach(({ type, condition, table }) => {\n query = query[type ?? 'leftJoin'](table, condition)\n })\n\n return queryModifier({\n query,\n }) as unknown as QueryPromise<{ id: number | string }[] & Record<string, GenericColumn>>\n }\n}\n"],"names":["selectDistinct","adapter","db","forceRun","joins","query","queryModifier","selectFields","tableName","where","Object","keys","length","table","tables","name","from","$dynamic","forEach","type","condition"],"mappings":"AAwBA;;CAEC,GACD,OAAO,MAAMA,iBAAiB,CAAC,EAC7BC,OAAO,EACPC,EAAE,EACFC,QAAQ,EACRC,KAAK,EACLC,OAAOC,gBAAgB,CAAC,EAAED,KAAK,EAAE,GAAKA,KAAK,EAC3CE,YAAY,EACZC,SAAS,EACTC,KAAK,EACA;IACL,IAAIN,YAAYO,OAAOC,IAAI,CAACP,OAAOQ,MAAM,GAAG,GAAG;QAC7C,IAAIP;QACJ,MAAMQ,QAAQZ,QAAQa,MAAM,CAACN,UAAU;QAEvC,IAAIP,QAAQc,IAAI,KAAK,YAAY;YAC/BV,QAAQ,AAACH,GACNF,cAAc,CAACO,cACfS,IAAI,CAACH,OACLI,QAAQ;QACb;QACA,IAAIhB,QAAQc,IAAI,KAAK,UAAU;YAC7BV,QAAQ,AAACH,GACNF,cAAc,CAACO,cACfS,IAAI,CAACH,OACLI,QAAQ;QACb;QAEA,IAAIR,OAAO;YACTJ,QAAQA,MAAMI,KAAK,CAACA;QACtB;QAEAL,MAAMc,OAAO,CAAC,CAAC,EAAEC,IAAI,EAAEC,SAAS,EAAEP,KAAK,EAAE;YACvCR,QAAQA,KAAK,CAACc,QAAQ,WAAW,CAACN,OAAOO;QAC3C;QAEA,OAAOd,cAAc;YACnBD;QACF;IACF;AACF,EAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payloadcms/drizzle",
3
- "version": "3.49.0-canary.8",
3
+ "version": "3.49.1",
4
4
  "description": "A library of shared functions used by different payload database adapters",
5
5
  "homepage": "https://payloadcms.com",
6
6
  "repository": {
@@ -55,10 +55,10 @@
55
55
  "@types/pg": "8.10.2",
56
56
  "@types/to-snake-case": "1.0.0",
57
57
  "@payloadcms/eslint-config": "3.28.0",
58
- "payload": "3.49.0-canary.8"
58
+ "payload": "3.49.1"
59
59
  },
60
60
  "peerDependencies": {
61
- "payload": "3.49.0-canary.8"
61
+ "payload": "3.49.1"
62
62
  },
63
63
  "scripts": {
64
64
  "build": "pnpm build:swc && pnpm build:types",