@payloadcms/drizzle 4.0.0-internal.e7d4ebc → 4.0.0-internal.ea6e8f8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +25 -3
- package/dist/count.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +28 -4
- package/dist/countVersions.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +15 -2
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createTableName.d.ts.map +1 -1
- package/dist/createTableName.js +2 -7
- package/dist/createTableName.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +12 -1
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteBranchGlobal.d.ts +10 -0
- package/dist/deleteBranchGlobal.d.ts.map +1 -0
- package/dist/deleteBranchGlobal.js +27 -0
- package/dist/deleteBranchGlobal.js.map +1 -0
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +18 -1
- package/dist/deleteOne.js.map +1 -1
- package/dist/exports/postgres.d.ts +3 -0
- package/dist/exports/postgres.d.ts.map +1 -1
- package/dist/exports/postgres.js +3 -0
- package/dist/exports/postgres.js.map +1 -1
- package/dist/exports/sqlite.d.ts +1 -0
- package/dist/exports/sqlite.d.ts.map +1 -1
- package/dist/exports/sqlite.js +1 -0
- package/dist/exports/sqlite.js.map +1 -1
- package/dist/find/buildFindManyArgs.d.ts +3 -2
- package/dist/find/buildFindManyArgs.d.ts.map +1 -1
- package/dist/find/buildFindManyArgs.js +2 -1
- package/dist/find/buildFindManyArgs.js.map +1 -1
- package/dist/find/buildPolymorphicJoinQuery.d.ts +39 -0
- package/dist/find/buildPolymorphicJoinQuery.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinQuery.js +152 -0
- package/dist/find/buildPolymorphicJoinQuery.js.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts +20 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.js +301 -0
- package/dist/find/buildPolymorphicJoinWhere.js.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts +37 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.js +191 -0
- package/dist/find/createPolymorphicJoinWherePlan.js.map +1 -0
- package/dist/find/findMany.d.ts +6 -1
- package/dist/find/findMany.d.ts.map +1 -1
- package/dist/find/findMany.js +16 -3
- package/dist/find/findMany.js.map +1 -1
- package/dist/find/traverseFields.d.ts +3 -2
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +72 -197
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +32 -4
- package/dist/find.js.map +1 -1
- package/dist/findDistinct.d.ts.map +1 -1
- package/dist/findDistinct.js +107 -8
- package/dist/findDistinct.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +22 -5
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.js +8 -3
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +30 -3
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +32 -5
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +41 -15
- package/dist/migrate.js.map +1 -1
- package/dist/migrateDown.d.ts +2 -1
- package/dist/migrateDown.d.ts.map +1 -1
- package/dist/migrateDown.js +17 -4
- package/dist/migrateDown.js.map +1 -1
- package/dist/migrateFresh.d.ts +4 -2
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +26 -9
- package/dist/migrateFresh.js.map +1 -1
- package/dist/migrateRefresh.d.ts +2 -1
- package/dist/migrateRefresh.d.ts.map +1 -1
- package/dist/migrateRefresh.js +20 -5
- package/dist/migrateRefresh.js.map +1 -1
- package/dist/migrateReset.d.ts +2 -1
- package/dist/migrateReset.d.ts.map +1 -1
- package/dist/migrateReset.js +18 -4
- package/dist/migrateReset.js.map +1 -1
- package/dist/migrateStatus.d.ts +2 -1
- package/dist/migrateStatus.d.ts.map +1 -1
- package/dist/migrateStatus.js +11 -6
- package/dist/migrateStatus.js.map +1 -1
- package/dist/postgres/createJSONQuery/index.js +3 -3
- package/dist/postgres/createJSONQuery/index.js.map +1 -1
- package/dist/postgres/init.d.ts.map +1 -1
- package/dist/postgres/init.js +6 -0
- package/dist/postgres/init.js.map +1 -1
- package/dist/postgres/insert.d.ts.map +1 -1
- package/dist/postgres/insert.js +4 -2
- package/dist/postgres/insert.js.map +1 -1
- package/dist/postgres/predefinedMigrations/sizes-to-variants/index.d.ts +35 -0
- package/dist/postgres/predefinedMigrations/sizes-to-variants/index.d.ts.map +1 -0
- package/dist/postgres/predefinedMigrations/sizes-to-variants/index.js +185 -0
- package/dist/postgres/predefinedMigrations/sizes-to-variants/index.js.map +1 -0
- package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.js +8 -4
- package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.js.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/index.js +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/index.js.map +1 -1
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.d.ts +15 -0
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.d.ts.map +1 -0
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.js +25 -0
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.js.map +1 -0
- package/dist/postgres/query/postgresUnaccent.d.ts +13 -0
- package/dist/postgres/query/postgresUnaccent.d.ts.map +1 -0
- package/dist/postgres/query/postgresUnaccent.js +40 -0
- package/dist/postgres/query/postgresUnaccent.js.map +1 -0
- package/dist/postgres/types.d.ts +17 -2
- package/dist/postgres/types.d.ts.map +1 -1
- package/dist/postgres/types.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +3 -2
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +2 -1
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildBranchVisibility.d.ts +11 -0
- package/dist/queries/buildBranchVisibility.d.ts.map +1 -0
- package/dist/queries/buildBranchVisibility.js +48 -0
- package/dist/queries/buildBranchVisibility.js.map +1 -0
- package/dist/queries/buildOperatorConstraint.d.ts +22 -0
- package/dist/queries/buildOperatorConstraint.d.ts.map +1 -0
- package/dist/queries/buildOperatorConstraint.js +63 -0
- package/dist/queries/buildOperatorConstraint.js.map +1 -0
- package/dist/queries/buildOrderBy.d.ts +3 -2
- package/dist/queries/buildOrderBy.d.ts.map +1 -1
- package/dist/queries/buildOrderBy.js +2 -1
- package/dist/queries/buildOrderBy.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +3 -2
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +4 -2
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +15 -5
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +225 -54
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/operatorMap.d.ts +7 -3
- package/dist/queries/operatorMap.d.ts.map +1 -1
- package/dist/queries/operatorMap.js.map +1 -1
- package/dist/queries/parseParams.d.ts +3 -2
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +266 -11
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/resolveRelationshipPath.d.ts +36 -0
- package/dist/queries/resolveRelationshipPath.d.ts.map +1 -0
- package/dist/queries/resolveRelationshipPath.js +66 -0
- package/dist/queries/resolveRelationshipPath.js.map +1 -0
- package/dist/queries/validateOperatorHandlers.d.ts +8 -0
- package/dist/queries/validateOperatorHandlers.d.ts.map +1 -0
- package/dist/queries/validateOperatorHandlers.js +30 -0
- package/dist/queries/validateOperatorHandlers.js.map +1 -0
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +23 -4
- package/dist/queryDrafts.js.map +1 -1
- package/dist/schema/build.d.ts.map +1 -1
- package/dist/schema/build.js +20 -17
- package/dist/schema/build.js.map +1 -1
- package/dist/schema/traverseFields.js +5 -5
- package/dist/schema/traverseFields.js.map +1 -1
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js +3 -2
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js.map +1 -1
- package/dist/sqlite/createJSONQuery/index.d.ts.map +1 -1
- package/dist/sqlite/createJSONQuery/index.js +6 -5
- package/dist/sqlite/createJSONQuery/index.js.map +1 -1
- package/dist/sqlite/insert.d.ts.map +1 -1
- package/dist/sqlite/insert.js +9 -4
- package/dist/sqlite/insert.js.map +1 -1
- package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.d.ts +15 -0
- package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.d.ts.map +1 -0
- package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.js +114 -0
- package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.js.map +1 -0
- package/dist/sqlite/types.d.ts +4 -0
- package/dist/sqlite/types.d.ts.map +1 -1
- package/dist/sqlite/types.js.map +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +13 -5
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +1 -5
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transform/write/array.d.ts +3 -2
- package/dist/transform/write/array.d.ts.map +1 -1
- package/dist/transform/write/array.js +2 -1
- package/dist/transform/write/array.js.map +1 -1
- package/dist/transform/write/blocks.d.ts +3 -2
- package/dist/transform/write/blocks.d.ts.map +1 -1
- package/dist/transform/write/blocks.js +2 -1
- package/dist/transform/write/blocks.js.map +1 -1
- package/dist/transform/write/index.d.ts.map +1 -1
- package/dist/transform/write/index.js +2 -0
- package/dist/transform/write/index.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts +2 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +40 -20
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/transform/write/types.d.ts +7 -0
- package/dist/transform/write/types.d.ts.map +1 -1
- package/dist/transform/write/types.js.map +1 -1
- package/dist/types.d.ts +51 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/updateGlobal.d.ts +5 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +96 -4
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateJobs.d.ts.map +1 -1
- package/dist/updateJobs.js +37 -0
- package/dist/updateJobs.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +86 -4
- package/dist/updateOne.js.map +1 -1
- package/dist/upsertBranchGlobalChange.d.ts +3 -0
- package/dist/upsertBranchGlobalChange.d.ts.map +1 -0
- package/dist/upsertBranchGlobalChange.js +35 -0
- package/dist/upsertBranchGlobalChange.js.map +1 -0
- package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.js +17 -12
- package/dist/upsertRow/deleteExistingRowsByPath.js.map +1 -1
- package/dist/upsertRow/index.d.ts +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +156 -46
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/types.d.ts +3 -0
- package/dist/upsertRow/types.d.ts.map +1 -1
- package/dist/upsertRow/types.js.map +1 -1
- package/dist/utilities/buildCreateMigration.d.ts.map +1 -1
- package/dist/utilities/buildCreateMigration.js +12 -8
- package/dist/utilities/buildCreateMigration.js.map +1 -1
- package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.d.ts +15 -0
- package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.d.ts.map +1 -0
- package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js +110 -0
- package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js.map +1 -0
- package/dist/utilities/buildForeignKeyName.d.ts.map +1 -1
- package/dist/utilities/buildForeignKeyName.js +3 -2
- package/dist/utilities/buildForeignKeyName.js.map +1 -1
- package/dist/utilities/buildIndexName.d.ts.map +1 -1
- package/dist/utilities/buildIndexName.js +3 -2
- package/dist/utilities/buildIndexName.js.map +1 -1
- package/dist/utilities/checkTruncatedIdentifiers.d.ts +13 -0
- package/dist/utilities/checkTruncatedIdentifiers.d.ts.map +1 -0
- package/dist/utilities/checkTruncatedIdentifiers.js +56 -0
- package/dist/utilities/checkTruncatedIdentifiers.js.map +1 -0
- package/dist/utilities/createSchemaGenerator.d.ts.map +1 -1
- package/dist/utilities/createSchemaGenerator.js +3 -0
- package/dist/utilities/createSchemaGenerator.js.map +1 -1
- package/dist/utilities/getSizesToVariantsRenames.d.ts +54 -0
- package/dist/utilities/getSizesToVariantsRenames.d.ts.map +1 -0
- package/dist/utilities/getSizesToVariantsRenames.js +251 -0
- package/dist/utilities/getSizesToVariantsRenames.js.map +1 -0
- package/dist/utilities/parseJSONPathSegment.d.ts +9 -0
- package/dist/utilities/parseJSONPathSegment.d.ts.map +1 -0
- package/dist/utilities/parseJSONPathSegment.js +17 -0
- package/dist/utilities/parseJSONPathSegment.js.map +1 -0
- package/dist/utilities/validateExistingBlockIsIdentical.d.ts +1 -5
- package/dist/utilities/validateExistingBlockIsIdentical.d.ts.map +1 -1
- package/dist/utilities/validateExistingBlockIsIdentical.js.map +1 -1
- package/dist/utilities/validateIdentifierLength.d.ts +12 -0
- package/dist/utilities/validateIdentifierLength.d.ts.map +1 -0
- package/dist/utilities/validateIdentifierLength.js +18 -0
- package/dist/utilities/validateIdentifierLength.js.map +1 -0
- package/package.json +3 -3
- package/dist/upsertRow/handleUpsertError.spec.js +0 -42
- package/dist/upsertRow/handleUpsertError.spec.js.map +0 -1
- package/dist/utilities/isValidStringID.spec.js +0 -15
- package/dist/utilities/isValidStringID.spec.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { and, eq, inArray } from 'drizzle-orm';
|
|
2
2
|
export const deleteExistingRowsByPath = async ({ adapter, db, localeColumnName = '_locale', parentColumnName = '_parentID', parentID, pathColumnName = '_path', rows, tableName })=>{
|
|
3
|
-
const localizedPathsToDelete = new
|
|
3
|
+
const localizedPathsToDelete = new Map();
|
|
4
4
|
const pathsToDelete = new Set();
|
|
5
5
|
const table = adapter.tables[tableName];
|
|
6
6
|
rows.forEach((row)=>{
|
|
@@ -8,24 +8,29 @@ export const deleteExistingRowsByPath = async ({ adapter, db, localeColumnName =
|
|
|
8
8
|
const localeData = row[localeColumnName];
|
|
9
9
|
if (typeof path === 'string') {
|
|
10
10
|
if (typeof localeData === 'string') {
|
|
11
|
-
localizedPathsToDelete.
|
|
11
|
+
const localePaths = localizedPathsToDelete.get(localeData) ?? new Set();
|
|
12
|
+
localePaths.add(path);
|
|
13
|
+
localizedPathsToDelete.set(localeData, localePaths);
|
|
12
14
|
} else {
|
|
13
15
|
pathsToDelete.add(path);
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
});
|
|
17
19
|
if (localizedPathsToDelete.size > 0) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
for (const [locale, localePaths] of localizedPathsToDelete){
|
|
21
|
+
const whereConstraints = [
|
|
22
|
+
eq(table[parentColumnName], parentID)
|
|
23
|
+
];
|
|
24
|
+
if (pathColumnName) {
|
|
25
|
+
whereConstraints.push(inArray(table[pathColumnName], Array.from(localePaths)));
|
|
26
|
+
}
|
|
27
|
+
whereConstraints.push(eq(table[localeColumnName], locale));
|
|
28
|
+
await adapter.deleteWhere({
|
|
29
|
+
db,
|
|
30
|
+
tableName,
|
|
31
|
+
where: and(...whereConstraints)
|
|
32
|
+
});
|
|
23
33
|
}
|
|
24
|
-
await adapter.deleteWhere({
|
|
25
|
-
db,
|
|
26
|
-
tableName,
|
|
27
|
-
where: and(...whereConstraints)
|
|
28
|
-
});
|
|
29
34
|
}
|
|
30
35
|
if (pathsToDelete.size > 0) {
|
|
31
36
|
const whereConstraints = [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/upsertRow/deleteExistingRowsByPath.ts"],"sourcesContent":["import { and, eq, inArray } from 'drizzle-orm'\n\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n localeColumnName?: string\n parentColumnName?: string\n parentID: unknown\n pathColumnName?: string\n rows: Record<string, unknown>[]\n tableName: string\n}\n\nexport const deleteExistingRowsByPath = async ({\n adapter,\n db,\n localeColumnName = '_locale',\n parentColumnName = '_parentID',\n parentID,\n pathColumnName = '_path',\n rows,\n tableName,\n}: Args): Promise<void> => {\n const localizedPathsToDelete = new Set<string
|
|
1
|
+
{"version":3,"sources":["../../src/upsertRow/deleteExistingRowsByPath.ts"],"sourcesContent":["import { and, eq, inArray } from 'drizzle-orm'\n\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n localeColumnName?: string\n parentColumnName?: string\n parentID: unknown\n pathColumnName?: string\n rows: Record<string, unknown>[]\n tableName: string\n}\n\nexport const deleteExistingRowsByPath = async ({\n adapter,\n db,\n localeColumnName = '_locale',\n parentColumnName = '_parentID',\n parentID,\n pathColumnName = '_path',\n rows,\n tableName,\n}: Args): Promise<void> => {\n const localizedPathsToDelete = new Map<string, Set<string>>()\n const pathsToDelete = new Set<string>()\n const table = adapter.tables[tableName]\n\n rows.forEach((row) => {\n const path = row[pathColumnName]\n const localeData = row[localeColumnName]\n if (typeof path === 'string') {\n if (typeof localeData === 'string') {\n const localePaths = localizedPathsToDelete.get(localeData) ?? new Set<string>()\n\n localePaths.add(path)\n localizedPathsToDelete.set(localeData, localePaths)\n } else {\n pathsToDelete.add(path)\n }\n }\n })\n\n if (localizedPathsToDelete.size > 0) {\n for (const [locale, localePaths] of localizedPathsToDelete) {\n const whereConstraints = [eq(table[parentColumnName], parentID)]\n\n if (pathColumnName) {\n whereConstraints.push(inArray(table[pathColumnName], Array.from(localePaths)))\n }\n\n whereConstraints.push(eq(table[localeColumnName], locale))\n\n await adapter.deleteWhere({\n db,\n tableName,\n where: and(...whereConstraints),\n })\n }\n }\n\n if (pathsToDelete.size > 0) {\n const whereConstraints = [eq(table[parentColumnName], parentID)]\n\n if (pathColumnName) {\n whereConstraints.push(inArray(table[pathColumnName], Array.from(pathsToDelete)))\n }\n\n await adapter.deleteWhere({\n db,\n tableName,\n where: and(...whereConstraints),\n })\n }\n}\n"],"names":["and","eq","inArray","deleteExistingRowsByPath","adapter","db","localeColumnName","parentColumnName","parentID","pathColumnName","rows","tableName","localizedPathsToDelete","Map","pathsToDelete","Set","table","tables","forEach","row","path","localeData","localePaths","get","add","set","size","locale","whereConstraints","push","Array","from","deleteWhere","where"],"mappings":"AAAA,SAASA,GAAG,EAAEC,EAAE,EAAEC,OAAO,QAAQ,cAAa;AAe9C,OAAO,MAAMC,2BAA2B,OAAO,EAC7CC,OAAO,EACPC,EAAE,EACFC,mBAAmB,SAAS,EAC5BC,mBAAmB,WAAW,EAC9BC,QAAQ,EACRC,iBAAiB,OAAO,EACxBC,IAAI,EACJC,SAAS,EACJ;IACL,MAAMC,yBAAyB,IAAIC;IACnC,MAAMC,gBAAgB,IAAIC;IAC1B,MAAMC,QAAQZ,QAAQa,MAAM,CAACN,UAAU;IAEvCD,KAAKQ,OAAO,CAAC,CAACC;QACZ,MAAMC,OAAOD,GAAG,CAACV,eAAe;QAChC,MAAMY,aAAaF,GAAG,CAACb,iBAAiB;QACxC,IAAI,OAAOc,SAAS,UAAU;YAC5B,IAAI,OAAOC,eAAe,UAAU;gBAClC,MAAMC,cAAcV,uBAAuBW,GAAG,CAACF,eAAe,IAAIN;gBAElEO,YAAYE,GAAG,CAACJ;gBAChBR,uBAAuBa,GAAG,CAACJ,YAAYC;YACzC,OAAO;gBACLR,cAAcU,GAAG,CAACJ;YACpB;QACF;IACF;IAEA,IAAIR,uBAAuBc,IAAI,GAAG,GAAG;QACnC,KAAK,MAAM,CAACC,QAAQL,YAAY,IAAIV,uBAAwB;YAC1D,MAAMgB,mBAAmB;gBAAC3B,GAAGe,KAAK,CAACT,iBAAiB,EAAEC;aAAU;YAEhE,IAAIC,gBAAgB;gBAClBmB,iBAAiBC,IAAI,CAAC3B,QAAQc,KAAK,CAACP,eAAe,EAAEqB,MAAMC,IAAI,CAACT;YAClE;YAEAM,iBAAiBC,IAAI,CAAC5B,GAAGe,KAAK,CAACV,iBAAiB,EAAEqB;YAElD,MAAMvB,QAAQ4B,WAAW,CAAC;gBACxB3B;gBACAM;gBACAsB,OAAOjC,OAAO4B;YAChB;QACF;IACF;IAEA,IAAId,cAAcY,IAAI,GAAG,GAAG;QAC1B,MAAME,mBAAmB;YAAC3B,GAAGe,KAAK,CAACT,iBAAiB,EAAEC;SAAU;QAEhE,IAAIC,gBAAgB;YAClBmB,iBAAiBC,IAAI,CAAC3B,QAAQc,KAAK,CAACP,eAAe,EAAEqB,MAAMC,IAAI,CAACjB;QAClE;QAEA,MAAMV,QAAQ4B,WAAW,CAAC;YACxB3B;YACAM;YACAsB,OAAOjC,OAAO4B;QAChB;IACF;AACF,EAAC"}
|
|
@@ -7,5 +7,5 @@ import type { Args } from './types.js';
|
|
|
7
7
|
*
|
|
8
8
|
* adapter function replaces the entire row and does not support partial updates.
|
|
9
9
|
*/
|
|
10
|
-
export declare const upsertRow: <T extends Record<string, unknown> | TypeWithID>({ id, adapter, collectionSlug, data, db, fields, globalSlug, ignoreResult, customID, joinQuery: _joinQuery, operation, path, req, select, tableName, upsertTarget, where, }: Args) => Promise<T>;
|
|
10
|
+
export declare const upsertRow: <T extends Record<string, unknown> | TypeWithID>({ id, adapter, collectionSlug, data, db, fields, globalSlug, ignoreResult, customID, joinQuery: _joinQuery, operation, path, req, select, tableName, upsertConflictData, upsertTarget, where, }: Args) => Promise<null | T>;
|
|
11
11
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/upsertRow/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/upsertRow/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAMzC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAqBtC;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU,mMAsB3E,IAAI,KAAG,OAAO,CAAC,IAAI,GAAG,CAAC,CA01BzB,CAAA"}
|
package/dist/upsertRow/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { and, desc, eq, isNull, or } from 'drizzle-orm';
|
|
2
|
+
import { branchField, MAIN_BRANCH } from 'payload';
|
|
2
3
|
import { buildFindManyArgs } from '../find/buildFindManyArgs.js';
|
|
3
4
|
import { transform } from '../transform/read/index.js';
|
|
4
5
|
import { transformForWrite } from '../transform/write/index.js';
|
|
@@ -17,7 +18,7 @@ import { shouldUseOptimizedUpsertRow } from './shouldUseOptimizedUpsertRow.js';
|
|
|
17
18
|
*/ export const upsertRow = async ({ id, adapter, collectionSlug, data, db, fields, globalSlug, ignoreResult, // TODO:
|
|
18
19
|
// When we support joins for write operations (create/update) - pass collectionSlug to the buildFindManyArgs
|
|
19
20
|
// Make a new argument in upsertRow.ts and pass the slug from every operation.
|
|
20
|
-
customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, upsertTarget, where })=>{
|
|
21
|
+
customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, upsertConflictData, upsertTarget, where })=>{
|
|
21
22
|
if (operation === 'create' && !data.createdAt) {
|
|
22
23
|
data.createdAt = new Date().toISOString();
|
|
23
24
|
}
|
|
@@ -40,6 +41,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
40
41
|
const { row } = transformedForWrite;
|
|
41
42
|
const { arraysToPush } = transformedForWrite;
|
|
42
43
|
const drizzle = db;
|
|
44
|
+
const updateWhere = where ? and(eq(adapter.tables[tableName].id, id), where) : eq(adapter.tables[tableName].id, id);
|
|
43
45
|
// First, handle $push arrays
|
|
44
46
|
if (arraysToPush && Object.keys(arraysToPush)?.length) {
|
|
45
47
|
await insertArrays({
|
|
@@ -65,7 +67,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
65
67
|
if (hasDataToUpdate) {
|
|
66
68
|
// Only update row if there is something to update.
|
|
67
69
|
// Example: if the data only consists of a single $push, calling insertArrays is enough - we don't need to update the row.
|
|
68
|
-
await drizzle.update(adapter.tables[tableName]).set(row).where(
|
|
70
|
+
await drizzle.update(adapter.tables[tableName]).set(row).where(updateWhere);
|
|
69
71
|
}
|
|
70
72
|
return ignoreResult === 'idOnly' ? {
|
|
71
73
|
id
|
|
@@ -83,8 +85,11 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
83
85
|
const hasOnlyColumns = Object.keys(findManyArgs.columns || {}).length > 0;
|
|
84
86
|
if (!hasDataToUpdate) {
|
|
85
87
|
// Nothing to update => just fetch current row and return
|
|
86
|
-
findManyArgs.where =
|
|
88
|
+
findManyArgs.where = updateWhere;
|
|
87
89
|
const doc = await db.query[tableName].findFirst(findManyArgs);
|
|
90
|
+
if (!doc) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
88
93
|
return transform({
|
|
89
94
|
adapter,
|
|
90
95
|
config: adapter.payload.config,
|
|
@@ -105,7 +110,10 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
105
110
|
}
|
|
106
111
|
}
|
|
107
112
|
}
|
|
108
|
-
const docs = await drizzle.update(adapter.tables[tableName]).set(row).where(
|
|
113
|
+
const docs = await drizzle.update(adapter.tables[tableName]).set(row).where(updateWhere).returning(Object.keys(selectedFields).length ? selectedFields : undefined);
|
|
114
|
+
if (!docs[0]) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
109
117
|
return transform({
|
|
110
118
|
adapter,
|
|
111
119
|
config: adapter.payload.config,
|
|
@@ -116,7 +124,13 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
116
124
|
});
|
|
117
125
|
}
|
|
118
126
|
// DB Update that needs the result, potentially with joins => need to update first, then find. returning() does not work with joins.
|
|
119
|
-
await drizzle.update(adapter.tables[tableName]).set(row).where(
|
|
127
|
+
const docs = await drizzle.update(adapter.tables[tableName]).set(row).where(updateWhere).returning({
|
|
128
|
+
id: adapter.tables[tableName].id
|
|
129
|
+
});
|
|
130
|
+
if (!docs[0]) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
insertedRow = docs[0];
|
|
120
134
|
findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id);
|
|
121
135
|
const doc = await db.query[tableName].findFirst(findManyArgs);
|
|
122
136
|
return transform({
|
|
@@ -149,6 +163,16 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
149
163
|
path,
|
|
150
164
|
tableName
|
|
151
165
|
});
|
|
166
|
+
const conflictWrite = upsertConflictData ? transformForWrite({
|
|
167
|
+
adapter,
|
|
168
|
+
data: upsertConflictData,
|
|
169
|
+
enableAtomicWrites: false,
|
|
170
|
+
fields,
|
|
171
|
+
path,
|
|
172
|
+
tableName
|
|
173
|
+
}) : rowToInsert;
|
|
174
|
+
let nestedWrite = rowToInsert;
|
|
175
|
+
let isUpdatingAfterInsertConflict = false;
|
|
152
176
|
if (customID) {
|
|
153
177
|
rowToInsert.row.id = customID;
|
|
154
178
|
}
|
|
@@ -168,19 +192,59 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
168
192
|
rowToInsert.row.id = id;
|
|
169
193
|
[insertedRow] = await adapter.insert({
|
|
170
194
|
db,
|
|
171
|
-
onConflictDoUpdate: {
|
|
172
|
-
set:
|
|
195
|
+
onConflictDoUpdate: where ? {
|
|
196
|
+
set: conflictWrite.row,
|
|
197
|
+
target,
|
|
198
|
+
where
|
|
199
|
+
} : {
|
|
200
|
+
set: conflictWrite.row,
|
|
173
201
|
target
|
|
174
202
|
},
|
|
175
203
|
tableName,
|
|
176
204
|
values: rowToInsert.row
|
|
177
205
|
});
|
|
206
|
+
} else if (upsertConflictData && upsertTarget) {
|
|
207
|
+
// The parent conflict is atomic. Child-table replacements are serialized only
|
|
208
|
+
// when the caller's transaction spans this complete upsert.
|
|
209
|
+
const targetKey = Object.entries(adapter.tables[tableName]).find(([, column])=>column === target)?.[0];
|
|
210
|
+
const targetValue = targetKey ? rowToInsert.row[targetKey] : undefined;
|
|
211
|
+
if (!targetKey || typeof targetValue === 'undefined') {
|
|
212
|
+
throw new Error(`Could not resolve the upsert target value for table "${tableName}".`);
|
|
213
|
+
}
|
|
214
|
+
for(let attempt = 0; attempt < 10; attempt++){
|
|
215
|
+
;
|
|
216
|
+
[insertedRow] = await adapter.insert({
|
|
217
|
+
db,
|
|
218
|
+
onConflictDoNothing: {
|
|
219
|
+
target
|
|
220
|
+
},
|
|
221
|
+
tableName,
|
|
222
|
+
values: rowToInsert.row
|
|
223
|
+
});
|
|
224
|
+
if (insertedRow) {
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
const updateWhere = where ? and(eq(target, targetValue), where) : eq(target, targetValue);
|
|
228
|
+
const updatedRows = await db.update(adapter.tables[tableName]).set(conflictWrite.row).where(updateWhere).returning();
|
|
229
|
+
if (updatedRows[0]) {
|
|
230
|
+
insertedRow = updatedRows[0];
|
|
231
|
+
nestedWrite = conflictWrite;
|
|
232
|
+
isUpdatingAfterInsertConflict = true;
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
if (where) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (!insertedRow) {
|
|
240
|
+
throw new Error(`Could not upsert a stable row in table "${tableName}".`);
|
|
241
|
+
}
|
|
178
242
|
} else {
|
|
179
243
|
;
|
|
180
244
|
[insertedRow] = await adapter.insert({
|
|
181
245
|
db,
|
|
182
246
|
onConflictDoUpdate: {
|
|
183
|
-
set:
|
|
247
|
+
set: conflictWrite.row,
|
|
184
248
|
target,
|
|
185
249
|
where
|
|
186
250
|
},
|
|
@@ -188,6 +252,9 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
188
252
|
values: rowToInsert.row
|
|
189
253
|
});
|
|
190
254
|
}
|
|
255
|
+
if (!insertedRow) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
191
258
|
} else {
|
|
192
259
|
// No main row data to update, just use the existing ID
|
|
193
260
|
insertedRow = {
|
|
@@ -211,39 +278,44 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
211
278
|
const numbersToInsert = [];
|
|
212
279
|
const blocksToInsert = {};
|
|
213
280
|
const selectsToInsert = {};
|
|
281
|
+
const localeTableName = `${tableName}${adapter.localesSuffix}`;
|
|
282
|
+
const shouldStampLocaleBranch = Boolean(adapter.rawTables[localeTableName]?.columns[branchField]);
|
|
214
283
|
// If there are locale rows with data, add the parent and locale to each
|
|
215
|
-
if (Object.keys(
|
|
216
|
-
Object.entries(
|
|
284
|
+
if (Object.keys(nestedWrite.locales).length > 0) {
|
|
285
|
+
Object.entries(nestedWrite.locales).forEach(([locale, localeRow])=>{
|
|
217
286
|
localeRow._parentID = insertedRow.id;
|
|
218
287
|
localeRow._locale = locale;
|
|
288
|
+
if (shouldStampLocaleBranch) {
|
|
289
|
+
localeRow[branchField] = insertedRow[branchField] ?? data[branchField] ?? MAIN_BRANCH;
|
|
290
|
+
}
|
|
219
291
|
localesToInsert.push(localeRow);
|
|
220
292
|
});
|
|
221
293
|
}
|
|
222
294
|
// If there are relationships, add parent to each
|
|
223
|
-
if (
|
|
224
|
-
|
|
295
|
+
if (nestedWrite.relationships.length > 0) {
|
|
296
|
+
nestedWrite.relationships.forEach((relation)=>{
|
|
225
297
|
relation.parent = insertedRow.id;
|
|
226
298
|
relationsToInsert.push(relation);
|
|
227
299
|
});
|
|
228
300
|
}
|
|
229
301
|
// If there are texts, add parent to each
|
|
230
|
-
if (
|
|
231
|
-
|
|
302
|
+
if (nestedWrite.texts.length > 0) {
|
|
303
|
+
nestedWrite.texts.forEach((textRow)=>{
|
|
232
304
|
textRow.parent = insertedRow.id;
|
|
233
305
|
textsToInsert.push(textRow);
|
|
234
306
|
});
|
|
235
307
|
}
|
|
236
308
|
// If there are numbers, add parent to each
|
|
237
|
-
if (
|
|
238
|
-
|
|
309
|
+
if (nestedWrite.numbers.length > 0) {
|
|
310
|
+
nestedWrite.numbers.forEach((numberRow)=>{
|
|
239
311
|
numberRow.parent = insertedRow.id;
|
|
240
312
|
numbersToInsert.push(numberRow);
|
|
241
313
|
});
|
|
242
314
|
}
|
|
243
315
|
// If there are selects, add parent to each, and then
|
|
244
316
|
// store by table name and rows
|
|
245
|
-
if (Object.keys(
|
|
246
|
-
Object.entries(
|
|
317
|
+
if (Object.keys(nestedWrite.selects).length > 0) {
|
|
318
|
+
Object.entries(nestedWrite.selects).forEach(([selectTableName, selectRows])=>{
|
|
247
319
|
selectsToInsert[selectTableName] = [];
|
|
248
320
|
selectRows.forEach((row)=>{
|
|
249
321
|
if (typeof row.parent === 'undefined') {
|
|
@@ -255,8 +327,8 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
255
327
|
}
|
|
256
328
|
// If there are blocks, add parent to each, and then
|
|
257
329
|
// store by table name and rows
|
|
258
|
-
Object.keys(
|
|
259
|
-
|
|
330
|
+
Object.keys(nestedWrite.blocks).forEach((tableName)=>{
|
|
331
|
+
nestedWrite.blocks[tableName].forEach((blockRow)=>{
|
|
260
332
|
blockRow.row._parentID = insertedRow.id;
|
|
261
333
|
if (!blocksToInsert[tableName]) {
|
|
262
334
|
blocksToInsert[tableName] = [];
|
|
@@ -271,20 +343,36 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
271
343
|
// INSERT LOCALES
|
|
272
344
|
// //////////////////////////////////
|
|
273
345
|
if (localesToInsert.length > 0) {
|
|
274
|
-
const localeTableName = `${tableName}${adapter.localesSuffix}`;
|
|
275
346
|
const localeTable = adapter.tables[`${tableName}${adapter.localesSuffix}`];
|
|
276
|
-
if (operation === 'update') {
|
|
277
|
-
|
|
347
|
+
if (operation === 'update' && isUpdatingAfterInsertConflict) {
|
|
348
|
+
for (const localeRow of localesToInsert){
|
|
349
|
+
await adapter.insert({
|
|
350
|
+
db,
|
|
351
|
+
onConflictDoUpdate: {
|
|
352
|
+
set: localeRow,
|
|
353
|
+
target: [
|
|
354
|
+
localeTable._locale,
|
|
355
|
+
localeTable._parentID
|
|
356
|
+
]
|
|
357
|
+
},
|
|
358
|
+
tableName: localeTableName,
|
|
359
|
+
values: localeRow
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
if (operation === 'update') {
|
|
364
|
+
await adapter.deleteWhere({
|
|
365
|
+
db,
|
|
366
|
+
tableName: localeTableName,
|
|
367
|
+
where: eq(localeTable._parentID, insertedRow.id)
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
await adapter.insert({
|
|
278
371
|
db,
|
|
279
372
|
tableName: localeTableName,
|
|
280
|
-
|
|
373
|
+
values: localesToInsert
|
|
281
374
|
});
|
|
282
375
|
}
|
|
283
|
-
await adapter.insert({
|
|
284
|
-
db,
|
|
285
|
-
tableName: localeTableName,
|
|
286
|
-
values: localesToInsert
|
|
287
|
-
});
|
|
288
376
|
}
|
|
289
377
|
// //////////////////////////////////
|
|
290
378
|
// INSERT RELATIONSHIPS
|
|
@@ -292,7 +380,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
292
380
|
const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`;
|
|
293
381
|
if (operation === 'update') {
|
|
294
382
|
// Filter out specific item deletions (those with itemToRemove) from general path deletions
|
|
295
|
-
const generalRelationshipDeletes =
|
|
383
|
+
const generalRelationshipDeletes = nestedWrite.relationshipsToDelete.filter((rel)=>!('itemToRemove' in rel));
|
|
296
384
|
await deleteExistingRowsByPath({
|
|
297
385
|
adapter,
|
|
298
386
|
db,
|
|
@@ -317,9 +405,9 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
317
405
|
// //////////////////////////////////
|
|
318
406
|
// HANDLE RELATIONSHIP $push OPERATIONS
|
|
319
407
|
// //////////////////////////////////
|
|
320
|
-
if (
|
|
408
|
+
if (nestedWrite.relationshipsToAppend.length > 0) {
|
|
321
409
|
// Prepare all relationships for batch insert (order will be set after max query)
|
|
322
|
-
const relationshipsToInsert =
|
|
410
|
+
const relationshipsToInsert = nestedWrite.relationshipsToAppend.map((rel)=>{
|
|
323
411
|
const parentId = id || insertedRow.id;
|
|
324
412
|
const row = {
|
|
325
413
|
parent: parentId,
|
|
@@ -417,10 +505,10 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
417
505
|
// //////////////////////////////////
|
|
418
506
|
// HANDLE RELATIONSHIP $remove OPERATIONS
|
|
419
507
|
// //////////////////////////////////
|
|
420
|
-
if (
|
|
508
|
+
if (nestedWrite.relationshipsToDelete.some((rel)=>'itemToRemove' in rel)) {
|
|
421
509
|
const relationshipTable = adapter.tables[relationshipsTableName];
|
|
422
510
|
if (relationshipTable) {
|
|
423
|
-
for (const relToDelete of
|
|
511
|
+
for (const relToDelete of nestedWrite.relationshipsToDelete){
|
|
424
512
|
if ('itemToRemove' in relToDelete && relToDelete.itemToRemove) {
|
|
425
513
|
const item = relToDelete.itemToRemove;
|
|
426
514
|
const parentId = id || insertedRow.id;
|
|
@@ -474,7 +562,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
474
562
|
pathColumnName: 'path',
|
|
475
563
|
rows: [
|
|
476
564
|
...textsToInsert,
|
|
477
|
-
...
|
|
565
|
+
...nestedWrite.textsToDelete
|
|
478
566
|
],
|
|
479
567
|
tableName: textsTableName
|
|
480
568
|
});
|
|
@@ -500,7 +588,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
500
588
|
pathColumnName: 'path',
|
|
501
589
|
rows: [
|
|
502
590
|
...numbersToInsert,
|
|
503
|
-
...
|
|
591
|
+
...nestedWrite.numbersToDelete
|
|
504
592
|
],
|
|
505
593
|
tableName: numbersTableName
|
|
506
594
|
});
|
|
@@ -517,7 +605,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
517
605
|
// //////////////////////////////////
|
|
518
606
|
const insertedBlockRows = {};
|
|
519
607
|
if (operation === 'update') {
|
|
520
|
-
for (const tableName of
|
|
608
|
+
for (const tableName of nestedWrite.blocksToDelete){
|
|
521
609
|
const blockTable = adapter.tables[tableName];
|
|
522
610
|
await adapter.deleteWhere({
|
|
523
611
|
db,
|
|
@@ -573,7 +661,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
573
661
|
// INSERT ARRAYS RECURSIVELY
|
|
574
662
|
// //////////////////////////////////
|
|
575
663
|
if (operation === 'update') {
|
|
576
|
-
for (const arrayTableName of Object.keys(
|
|
664
|
+
for (const arrayTableName of Object.keys(nestedWrite.arrays)){
|
|
577
665
|
await deleteExistingArrayRows({
|
|
578
666
|
adapter,
|
|
579
667
|
db,
|
|
@@ -585,8 +673,8 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
585
673
|
await insertArrays({
|
|
586
674
|
adapter,
|
|
587
675
|
arrays: [
|
|
588
|
-
|
|
589
|
-
|
|
676
|
+
nestedWrite.arrays,
|
|
677
|
+
nestedWrite.arraysToPush
|
|
590
678
|
],
|
|
591
679
|
db,
|
|
592
680
|
parentRows: [
|
|
@@ -598,14 +686,36 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
|
|
|
598
686
|
// //////////////////////////////////
|
|
599
687
|
// INSERT hasMany SELECTS
|
|
600
688
|
// //////////////////////////////////
|
|
601
|
-
|
|
689
|
+
const selectTableNames = new Set([
|
|
690
|
+
...Object.keys(nestedWrite.selectsToDelete),
|
|
691
|
+
...Object.keys(selectsToInsert)
|
|
692
|
+
]);
|
|
693
|
+
for (const selectTableName of selectTableNames){
|
|
694
|
+
const tableRows = selectsToInsert[selectTableName] ?? [];
|
|
602
695
|
const selectTable = adapter.tables[selectTableName];
|
|
603
696
|
if (operation === 'update') {
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
697
|
+
for (const selectToDelete of nestedWrite.selectsToDelete[selectTableName] ?? []){
|
|
698
|
+
let selectParent = selectToDelete.parent ?? insertedRow.id;
|
|
699
|
+
if ((typeof selectParent === 'number' || typeof selectParent === 'string') && selectParent in arraysBlocksUUIDMap) {
|
|
700
|
+
selectParent = arraysBlocksUUIDMap[selectParent];
|
|
701
|
+
}
|
|
702
|
+
const deleteConstraints = [
|
|
703
|
+
eq(selectTable.parent, selectParent)
|
|
704
|
+
];
|
|
705
|
+
if (typeof selectToDelete.locale === 'string') {
|
|
706
|
+
if (!selectTable.locale) {
|
|
707
|
+
throw new Error(`Could not limit the select replacement for table "${selectTableName}" to locale "${selectToDelete.locale}".`);
|
|
708
|
+
}
|
|
709
|
+
deleteConstraints.push(eq(selectTable.locale, selectToDelete.locale));
|
|
710
|
+
} else if (selectTable.locale) {
|
|
711
|
+
deleteConstraints.push(isNull(selectTable.locale));
|
|
712
|
+
}
|
|
713
|
+
await adapter.deleteWhere({
|
|
714
|
+
db,
|
|
715
|
+
tableName: selectTableName,
|
|
716
|
+
where: and(...deleteConstraints)
|
|
717
|
+
});
|
|
718
|
+
}
|
|
609
719
|
}
|
|
610
720
|
if (Object.keys(arraysBlocksUUIDMap).length > 0) {
|
|
611
721
|
tableRows.forEach((row)=>{
|