@payloadcms/db-mongodb 3.0.0-beta.38 → 3.0.0-beta.39
Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"createMigration.d.ts","sourceRoot":"","sources":["../src/createMigration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,
|
1
|
+
{"version":3,"file":"createMigration.d.ts","sourceRoot":"","sources":["../src/createMigration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAyB,MAAM,kBAAkB,CAAA;AAsB9E,eAAO,MAAM,eAAe,EAAE,eAgC7B,CAAA"}
|
package/dist/createMigration.js
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
/* eslint-disable no-restricted-syntax, no-await-in-loop */ import fs from 'fs';
|
2
2
|
import path from 'path';
|
3
|
+
import { getPredefinedMigration } from 'payload/database';
|
3
4
|
import { fileURLToPath } from 'url';
|
4
|
-
const migrationTemplate = (
|
5
|
+
const migrationTemplate = ({ downSQL, imports, upSQL })=>`import {
|
5
6
|
MigrateUpArgs,
|
6
7
|
MigrateDownArgs,
|
7
|
-
} from
|
8
|
+
} from '@payloadcms/db-mongodb'
|
9
|
+
${imports}
|
8
10
|
|
9
11
|
export async function up({ payload }: MigrateUpArgs): Promise<void> {
|
10
12
|
${upSQL ?? ` // Migration code`}
|
11
|
-
}
|
13
|
+
}
|
12
14
|
|
13
15
|
export async function down({ payload }: MigrateDownArgs): Promise<void> {
|
14
16
|
${downSQL ?? ` // Migration code`}
|
15
|
-
}
|
17
|
+
}
|
16
18
|
`;
|
17
19
|
export const createMigration = async function createMigration({ file, migrationName, payload }) {
|
18
20
|
const filename = fileURLToPath(import.meta.url);
|
@@ -21,29 +23,13 @@ export const createMigration = async function createMigration({ file, migrationN
|
|
21
23
|
if (!fs.existsSync(dir)) {
|
22
24
|
fs.mkdirSync(dir);
|
23
25
|
}
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
const cleanPath = path.join(dirname, `../predefinedMigrations/${predefinedMigrationName}.js`);
|
32
|
-
// Check if predefined migration exists
|
33
|
-
if (fs.existsSync(cleanPath)) {
|
34
|
-
let migration = await eval(`${typeof require === 'function' ? 'require' : 'import'}(${cleanPath})`);
|
35
|
-
if ('default' in migration) migration = migration.default;
|
36
|
-
const { down, up } = migration;
|
37
|
-
migrationFileContent = migrationTemplate(up, down);
|
38
|
-
} else {
|
39
|
-
payload.logger.error({
|
40
|
-
msg: `Canned migration ${predefinedMigrationName} not found.`
|
41
|
-
});
|
42
|
-
process.exit(1);
|
43
|
-
}
|
44
|
-
} else {
|
45
|
-
migrationFileContent = migrationTemplate();
|
46
|
-
}
|
26
|
+
const predefinedMigration = await getPredefinedMigration({
|
27
|
+
dirname,
|
28
|
+
file,
|
29
|
+
migrationName,
|
30
|
+
payload
|
31
|
+
});
|
32
|
+
const migrationFileContent = migrationTemplate(predefinedMigration);
|
47
33
|
const [yyymmdd, hhmmss] = new Date().toISOString().split('T');
|
48
34
|
const formattedDate = yyymmdd.replace(/\D/g, '');
|
49
35
|
const formattedTime = hhmmss.split('.')[0].replace(/\D/g, '');
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/createMigration.ts"],"sourcesContent":["/* eslint-disable no-restricted-syntax, no-await-in-loop */\nimport type { CreateMigration } from 'payload/database'\n\nimport fs from 'fs'\nimport path from 'path'\nimport { fileURLToPath } from 'url'\n\nconst migrationTemplate = (
|
1
|
+
{"version":3,"sources":["../src/createMigration.ts"],"sourcesContent":["/* eslint-disable no-restricted-syntax, no-await-in-loop */\nimport type { CreateMigration, MigrationTemplateArgs } from 'payload/database'\n\nimport fs from 'fs'\nimport path from 'path'\nimport { getPredefinedMigration } from 'payload/database'\nimport { fileURLToPath } from 'url'\n\nconst migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {\n MigrateUpArgs,\n MigrateDownArgs,\n} from '@payloadcms/db-mongodb'\n${imports}\n\nexport async function up({ payload }: MigrateUpArgs): Promise<void> {\n${upSQL ?? ` // Migration code`}\n}\n\nexport async function down({ payload }: MigrateDownArgs): Promise<void> {\n${downSQL ?? ` // Migration code`}\n}\n`\n\nexport const createMigration: CreateMigration = async function createMigration({\n file,\n migrationName,\n payload,\n}) {\n const filename = fileURLToPath(import.meta.url)\n const dirname = path.dirname(filename)\n\n const dir = payload.db.migrationDir\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir)\n }\n const predefinedMigration = await getPredefinedMigration({\n dirname,\n file,\n migrationName,\n payload,\n })\n\n const migrationFileContent = migrationTemplate(predefinedMigration)\n\n const [yyymmdd, hhmmss] = new Date().toISOString().split('T')\n const formattedDate = yyymmdd.replace(/\\D/g, '')\n const formattedTime = hhmmss.split('.')[0].replace(/\\D/g, '')\n\n const timestamp = `${formattedDate}_${formattedTime}`\n\n const formattedName = migrationName?.replace(/\\W/g, '_')\n const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`\n const filePath = `${dir}/${fileName}`\n fs.writeFileSync(filePath, migrationFileContent)\n payload.logger.info({ msg: `Migration created at ${filePath}` })\n}\n"],"names":["fs","path","getPredefinedMigration","fileURLToPath","migrationTemplate","downSQL","imports","upSQL","createMigration","file","migrationName","payload","filename","url","dirname","dir","db","migrationDir","existsSync","mkdirSync","predefinedMigration","migrationFileContent","yyymmdd","hhmmss","Date","toISOString","split","formattedDate","replace","formattedTime","timestamp","formattedName","fileName","filePath","writeFileSync","logger","info","msg"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,yDAAyD,GAGzD,OAAOA,QAAQ,KAAI;AACnB,OAAOC,UAAU,OAAM;AACvB,SAASC,sBAAsB,QAAQ,mBAAkB;AACzD,SAASC,aAAa,QAAQ,MAAK;AAEnC,MAAMC,oBAAoB,CAAC,EAAEC,OAAO,EAAEC,OAAO,EAAEC,KAAK,EAAyB,GAAa,CA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efinedMigrations/versions-v1-v2.js"],"sourcesContent":["module.exports.up = ` async function migrateCollectionDocs(slug: string, docsAtATime = 100) {\n const VersionsModel = payload.db.versions[slug]\n const remainingDocs = await VersionsModel.aggregate(\n [\n // Sort so that newest are first\n {\n $sort: {\n updatedAt: -1,\n },\n },\n // Group by parent ID\n // take the $first of each\n {\n $group: {\n _id: '$parent',\n _versionID: { $first: '$_id' },\n createdAt: { $first: '$createdAt' },\n latest: { $first: '$latest' },\n updatedAt: { $first: '$updatedAt' },\n version: { $first: '$version' },\n },\n },\n {\n $match: {\n latest: { $eq: null },\n },\n },\n {\n $limit: docsAtATime,\n },\n ],\n {\n allowDiskUse: true,\n },\n ).exec()\n\n if (!remainingDocs || remainingDocs.length === 0) {\n const newVersions = await VersionsModel.find({\n latest: {\n $eq: true,\n },\n })\n\n if (newVersions?.length) {\n payload.logger.info(\n \\`Migrated \\${newVersions.length} documents in the \"\\${slug}\" versions collection.\\`,\n )\n }\n\n return\n }\n\n const remainingDocIds = remainingDocs.map((doc) => doc._versionID)\n\n await VersionsModel.updateMany(\n {\n _id: {\n $in: remainingDocIds,\n },\n },\n {\n latest: true,\n },\n )\n\n await migrateCollectionDocs(slug)\n }\n\n // For each collection\n await Promise.all(\n payload.config.collections.map(async ({ slug, versions }) => {\n if (versions?.drafts) {\n return migrateCollectionDocs(slug)\n }\n }),\n )\n\n // For each global\n await Promise.all(\n payload.config.globals.map(async ({ slug, versions }) => {\n if (versions) {\n const VersionsModel = payload.db.versions[slug]\n\n await VersionsModel.findOneAndUpdate(\n {},\n { latest: true },\n {\n sort: { updatedAt: -1 },\n },\n ).exec()\n\n payload.logger.info(\\`Migrated the \"\\${slug}\" global.\\`)\n }\n }),\n )\n`\n"],"names":["module","exports","up"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAAA,OAAOC,OAAO,CAACC,EAAE,GAAG,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+FrB,CAAC"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@payloadcms/db-mongodb",
|
3
|
-
"version": "3.0.0-beta.
|
3
|
+
"version": "3.0.0-beta.39",
|
4
4
|
"description": "The officially supported MongoDB database adapter for Payload",
|
5
5
|
"homepage": "https://payloadcms.com",
|
6
6
|
"repository": {
|
@@ -40,10 +40,10 @@
|
|
40
40
|
"mongodb": "4.17.1",
|
41
41
|
"mongodb-memory-server": "^9",
|
42
42
|
"@payloadcms/eslint-config": "1.1.1",
|
43
|
-
"payload": "3.0.0-beta.
|
43
|
+
"payload": "3.0.0-beta.39"
|
44
44
|
},
|
45
45
|
"peerDependencies": {
|
46
|
-
"payload": "3.0.0-beta.
|
46
|
+
"payload": "3.0.0-beta.39"
|
47
47
|
},
|
48
48
|
"scripts": {
|
49
49
|
"build": "pnpm build:swc && pnpm build:types",
|