@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,EAAE,MAAM,kBAAkB,CAAA;AAoBvD,eAAO,MAAM,eAAe,EAAE,eAsD7B,CAAA"}
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"}
@@ -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 = (upSQL, downSQL)=>`import {
5
+ const migrationTemplate = ({ downSQL, imports, upSQL })=>`import {
5
6
  MigrateUpArgs,
6
7
  MigrateDownArgs,
7
- } from "@payloadcms/db-mongodb";
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
- let migrationFileContent;
25
- // Check for predefined migration.
26
- // Either passed in via --file or prefixed with @payloadcms/db-mongodb/
27
- if (file || migrationName?.startsWith('@payloadcms/db-mongodb/')) {
28
- if (!file) file = migrationName;
29
- const predefinedMigrationName = file.replace('@payloadcms/db-mongodb/', '');
30
- migrationName = predefinedMigrationName;
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 = (upSQL?: string, downSQL?: string) => `import {\n MigrateUpArgs,\n MigrateDownArgs,\n} from \"@payloadcms/db-mongodb\";\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\n let migrationFileContent: string | undefined\n\n // Check for predefined migration.\n // Either passed in via --file or prefixed with @payloadcms/db-mongodb/\n if (file || migrationName?.startsWith('@payloadcms/db-mongodb/')) {\n if (!file) file = migrationName\n\n const predefinedMigrationName = file.replace('@payloadcms/db-mongodb/', '')\n migrationName = predefinedMigrationName\n const cleanPath = path.join(dirname, `../predefinedMigrations/${predefinedMigrationName}.js`)\n\n // Check if predefined migration exists\n if (fs.existsSync(cleanPath)) {\n let migration = await eval(\n `${typeof require === 'function' ? 'require' : 'import'}(${cleanPath})`,\n )\n if ('default' in migration) migration = migration.default\n const { down, up } = migration\n\n migrationFileContent = migrationTemplate(up, down)\n } else {\n payload.logger.error({\n msg: `Canned migration ${predefinedMigrationName} not found.`,\n })\n process.exit(1)\n }\n } else {\n migrationFileContent = migrationTemplate()\n }\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","fileURLToPath","migrationTemplate","upSQL","downSQL","createMigration","file","migrationName","payload","filename","url","dirname","dir","db","migrationDir","existsSync","mkdirSync","migrationFileContent","startsWith","predefinedMigrationName","replace","cleanPath","join","migration","eval","require","default","down","up","logger","error","msg","process","exit","yyymmdd","hhmmss","Date","toISOString","split","formattedDate","formattedTime","timestamp","formattedName","fileName","filePath","writeFileSync","info"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,yDAAyD,GAGzD,OAAOA,QAAQ,KAAI;AACnB,OA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IACF,OAAO;QACLhB,uBAAuBf;IACzB;IAEA,MAAM,CAACgC,SAASC,OAAO,GAAG,IAAIC,OAAOC,WAAW,GAAGC,KAAK,CAAC;IACzD,MAAMC,gBAAgBL,QAAQd,OAAO,CAAC,OAAO;IAC7C,MAAMoB,gBAAgBL,OAAOG,KAAK,CAAC,IAAI,CAAC,EAAE,CAAClB,OAAO,CAAC,OAAO;IAE1D,MAAMqB,YAAY,CAAC,EAAEF,cAAc,CAAC,EAAEC,cAAc,CAAC;IAErD,MAAME,gBAAgBnC,eAAea,QAAQ,OAAO;IACpD,MAAMuB,WAAWpC,gBAAgB,CAAC,EAAEkC,UAAU,CAAC,EAAEC,cAAc,GAAG,CAAC,GAAG,CAAC,EAAED,UAAU,aAAa,CAAC;IACjG,MAAMG,WAAW,CAAC,EAAEhC,IAAI,CAAC,EAAE+B,SAAS,CAAC;IACrC5C,GAAG8C,aAAa,CAACD,UAAU3B;IAC3BT,QAAQqB,MAAM,CAACiB,IAAI,CAAC;QAAEf,KAAK,CAAC,qBAAqB,EAAEa,SAAS,CAAC;IAAC;AAChE,EAAC"}
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,CAAC;;;;AAI3F,EAAED,QAAQ;;;AAGV,EAAEC,SAAS,CAAC,mBAAmB,CAAC,CAAC;;;;AAIjC,EAAEF,WAAW,CAAC,mBAAmB,CAAC,CAAC;;AAEnC,CAAC;AAED,OAAO,MAAMG,kBAAmC,eAAeA,gBAAgB,EAC7EC,IAAI,EACJC,aAAa,EACbC,OAAO,EACR;IACC,MAAMC,WAAWT,cAAc,YAAYU,GAAG;IAC9C,MAAMC,UAAUb,KAAKa,OAAO,CAACF;IAE7B,MAAMG,MAAMJ,QAAQK,EAAE,CAACC,YAAY;IACnC,IAAI,CAACjB,GAAGkB,UAAU,CAACH,MAAM;QACvBf,GAAGmB,SAAS,CAACJ;IACf;IACA,MAAMK,sBAAsB,MAAMlB,uBAAuB;QACvDY;QACAL;QACAC;QACAC;IACF;IAEA,MAAMU,uBAAuBjB,kBAAkBgB;IAE/C,MAAM,CAACE,SAASC,OAAO,GAAG,IAAIC,OAAOC,WAAW,GAAGC,KAAK,CAAC;IACzD,MAAMC,gBAAgBL,QAAQM,OAAO,CAAC,OAAO;IAC7C,MAAMC,gBAAgBN,OAAOG,KAAK,CAAC,IAAI,CAAC,EAAE,CAACE,OAAO,CAAC,OAAO;IAE1D,MAAME,YAAY,CAAC,EAAEH,cAAc,CAAC,EAAEE,cAAc,CAAC;IAErD,MAAME,gBAAgBrB,eAAekB,QAAQ,OAAO;IACpD,MAAMI,WAAWtB,gBAAgB,CAAC,EAAEoB,UAAU,CAAC,EAAEC,cAAc,GAAG,CAAC,GAAG,CAAC,EAAED,UAAU,aAAa,CAAC;IACjG,MAAMG,WAAW,CAAC,EAAElB,IAAI,CAAC,EAAEiB,SAAS,CAAC;IACrChC,GAAGkC,aAAa,CAACD,UAAUZ;IAC3BV,QAAQwB,MAAM,CAACC,IAAI,CAAC;QAAEC,KAAK,CAAC,qBAAqB,EAAEJ,SAAS,CAAC;IAAC;AAChE,EAAC"}
@@ -93,4 +93,6 @@ module.exports.up = ` async function migrateCollectionDocs(slug: string, docsAt
93
93
  }
94
94
  }),
95
95
  )
96
- `
96
+ `;
97
+
98
+ //# sourceMappingURL=versions-v1-v2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/predefinedMigrations/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.38",
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.38"
43
+ "payload": "3.0.0-beta.39"
44
44
  },
45
45
  "peerDependencies": {
46
- "payload": "3.0.0-beta.38"
46
+ "payload": "3.0.0-beta.39"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "pnpm build:swc && pnpm build:types",