@payloadcms/drizzle 3.0.0-canary.0cf6f1e → 3.0.0-canary.1a675ae
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/exports/postgres.d.ts +14 -0
- package/dist/exports/postgres.d.ts.map +1 -0
- package/dist/exports/postgres.js +15 -0
- package/dist/exports/postgres.js.map +1 -0
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +2 -2
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/migrate.d.ts +1 -1
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +4 -10
- package/dist/migrate.js.map +1 -1
- package/dist/migrateFresh.js.map +1 -1
- package/dist/postgres/countDistinct.d.ts +3 -0
- package/dist/postgres/countDistinct.d.ts.map +1 -0
- package/dist/postgres/countDistinct.js +24 -0
- package/dist/postgres/countDistinct.js.map +1 -0
- package/dist/postgres/createJSONQuery/convertPathToJSONTraversal.d.ts +2 -0
- package/dist/postgres/createJSONQuery/convertPathToJSONTraversal.d.ts.map +1 -0
- package/dist/postgres/createJSONQuery/convertPathToJSONTraversal.js +14 -0
- package/dist/postgres/createJSONQuery/convertPathToJSONTraversal.js.map +1 -0
- package/dist/postgres/createJSONQuery/formatJSONPathSegment.d.ts +2 -0
- package/dist/postgres/createJSONQuery/formatJSONPathSegment.d.ts.map +1 -0
- package/dist/postgres/createJSONQuery/formatJSONPathSegment.js +5 -0
- package/dist/postgres/createJSONQuery/formatJSONPathSegment.js.map +1 -0
- package/dist/postgres/createJSONQuery/index.d.ts +10 -0
- package/dist/postgres/createJSONQuery/index.d.ts.map +1 -0
- package/dist/postgres/createJSONQuery/index.js +54 -0
- package/dist/postgres/createJSONQuery/index.js.map +1 -0
- package/dist/postgres/createMigration.d.ts +3 -0
- package/dist/postgres/createMigration.d.ts.map +1 -0
- package/dist/postgres/createMigration.js +92 -0
- package/dist/postgres/createMigration.js.map +1 -0
- package/dist/postgres/defaultSnapshot.d.ts +3 -0
- package/dist/postgres/defaultSnapshot.d.ts.map +1 -0
- package/dist/postgres/defaultSnapshot.js +17 -0
- package/dist/postgres/defaultSnapshot.js.map +1 -0
- package/dist/postgres/deleteWhere.d.ts +3 -0
- package/dist/postgres/deleteWhere.d.ts.map +1 -0
- package/dist/postgres/deleteWhere.js +6 -0
- package/dist/postgres/deleteWhere.js.map +1 -0
- package/dist/postgres/dropDatabase.d.ts +3 -0
- package/dist/postgres/dropDatabase.d.ts.map +1 -0
- package/dist/postgres/dropDatabase.js +9 -0
- package/dist/postgres/dropDatabase.js.map +1 -0
- package/dist/postgres/execute.d.ts +3 -0
- package/dist/postgres/execute.d.ts.map +1 -0
- package/dist/postgres/execute.js +11 -0
- package/dist/postgres/execute.js.map +1 -0
- package/dist/postgres/getMigrationTemplate.d.ts +4 -0
- package/dist/postgres/getMigrationTemplate.d.ts.map +1 -0
- package/dist/postgres/getMigrationTemplate.js +13 -0
- package/dist/postgres/getMigrationTemplate.js.map +1 -0
- package/dist/postgres/init.d.ts +3 -0
- package/dist/postgres/init.d.ts.map +1 -0
- package/dist/postgres/init.js +95 -0
- package/dist/postgres/init.js.map +1 -0
- package/dist/postgres/insert.d.ts +3 -0
- package/dist/postgres/insert.d.ts.map +1 -0
- package/dist/postgres/insert.js +12 -0
- package/dist/postgres/insert.js.map +1 -0
- package/dist/postgres/requireDrizzleKit.d.ts +3 -0
- package/dist/postgres/requireDrizzleKit.d.ts.map +1 -0
- package/dist/postgres/requireDrizzleKit.js +5 -0
- package/dist/postgres/requireDrizzleKit.js.map +1 -0
- package/dist/postgres/schema/build.d.ts +40 -0
- package/dist/postgres/schema/build.d.ts.map +1 -0
- package/dist/postgres/schema/build.js +373 -0
- package/dist/postgres/schema/build.js.map +1 -0
- package/dist/postgres/schema/createIndex.d.ts +12 -0
- package/dist/postgres/schema/createIndex.d.ts.map +1 -0
- package/dist/postgres/schema/createIndex.js +18 -0
- package/dist/postgres/schema/createIndex.js.map +1 -0
- package/dist/postgres/schema/idToUUID.d.ts +3 -0
- package/dist/postgres/schema/idToUUID.d.ts.map +1 -0
- package/dist/postgres/schema/idToUUID.js +11 -0
- package/dist/postgres/schema/idToUUID.js.map +1 -0
- package/dist/postgres/schema/parentIDColumnMap.d.ts +4 -0
- package/dist/postgres/schema/parentIDColumnMap.d.ts.map +1 -0
- package/dist/postgres/schema/parentIDColumnMap.js +9 -0
- package/dist/postgres/schema/parentIDColumnMap.js.map +1 -0
- package/dist/postgres/schema/setColumnID.d.ts +11 -0
- package/dist/postgres/schema/setColumnID.d.ts.map +1 -0
- package/dist/postgres/schema/setColumnID.js +24 -0
- package/dist/postgres/schema/setColumnID.js.map +1 -0
- package/dist/postgres/schema/traverseFields.d.ts +40 -0
- package/dist/postgres/schema/traverseFields.d.ts.map +1 -0
- package/dist/postgres/schema/traverseFields.js +644 -0
- package/dist/postgres/schema/traverseFields.js.map +1 -0
- package/dist/postgres/schema/withDefault.d.ts +4 -0
- package/dist/postgres/schema/withDefault.d.ts.map +1 -0
- package/dist/postgres/schema/withDefault.js +10 -0
- package/dist/postgres/schema/withDefault.js.map +1 -0
- package/dist/postgres/types.d.ts +112 -0
- package/dist/postgres/types.d.ts.map +1 -0
- package/dist/postgres/types.js +3 -0
- package/dist/postgres/types.js.map +1 -0
- package/dist/queries/getTableColumnFromPath.js +1 -1
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/transform/read/hasManyNumber.d.ts +2 -1
- package/dist/transform/read/hasManyNumber.d.ts.map +1 -1
- package/dist/transform/read/hasManyNumber.js +12 -2
- package/dist/transform/read/hasManyNumber.js.map +1 -1
- package/dist/transform/read/hasManyText.d.ts +2 -1
- package/dist/transform/read/hasManyText.d.ts.map +1 -1
- package/dist/transform/read/hasManyText.js +12 -2
- package/dist/transform/read/hasManyText.js.map +1 -1
- package/dist/transform/read/relationship.d.ts +2 -1
- package/dist/transform/read/relationship.d.ts.map +1 -1
- package/dist/transform/read/relationship.js +15 -6
- package/dist/transform/read/relationship.js.map +1 -1
- package/dist/transform/read/traverseFields.d.ts +5 -1
- package/dist/transform/read/traverseFields.d.ts.map +1 -1
- package/dist/transform/read/traverseFields.js +45 -27
- package/dist/transform/read/traverseFields.js.map +1 -1
- package/dist/transform/write/array.d.ts +6 -1
- package/dist/transform/write/array.d.ts.map +1 -1
- package/dist/transform/write/array.js +7 -3
- package/dist/transform/write/array.js.map +1 -1
- package/dist/transform/write/blocks.d.ts +6 -1
- package/dist/transform/write/blocks.d.ts.map +1 -1
- package/dist/transform/write/blocks.js +5 -3
- package/dist/transform/write/blocks.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts +6 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +26 -12
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +1 -0
- package/dist/upsertRow/index.js.map +1 -1
- package/package.json +12 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execute.d.ts","sourceRoot":"","sources":["../../src/postgres/execute.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC,eAAO,MAAM,OAAO,EAAE,OAAO,CAAC,GAAG,CAQhC,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { sql } from 'drizzle-orm';
|
|
2
|
+
export const execute = function execute({ db, drizzle, raw, sql: statement }) {
|
|
3
|
+
const executeFrom = db ?? drizzle;
|
|
4
|
+
if (raw) {
|
|
5
|
+
return executeFrom.execute(sql.raw(raw));
|
|
6
|
+
} else {
|
|
7
|
+
return executeFrom.execute(sql`${statement}`);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=execute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/postgres/execute.ts"],"sourcesContent":["import { sql } from 'drizzle-orm'\n\nimport type { Execute } from './types.js'\n\nexport const execute: Execute<any> = function execute({ db, drizzle, raw, sql: statement }) {\n const executeFrom = db ?? drizzle\n\n if (raw) {\n return executeFrom.execute(sql.raw(raw))\n } else {\n return executeFrom.execute(sql`${statement}`)\n }\n}\n"],"names":["sql","execute","db","drizzle","raw","statement","executeFrom"],"mappings":"AAAA,SAASA,GAAG,QAAQ,cAAa;AAIjC,OAAO,MAAMC,UAAwB,SAASA,QAAQ,EAAEC,EAAE,EAAEC,OAAO,EAAEC,GAAG,EAAEJ,KAAKK,SAAS,EAAE;IACxF,MAAMC,cAAcJ,MAAMC;IAE1B,IAAIC,KAAK;QACP,OAAOE,YAAYL,OAAO,CAACD,IAAII,GAAG,CAACA;IACrC,OAAO;QACL,OAAOE,YAAYL,OAAO,CAACD,GAAG,CAAC,EAAEK,UAAU,CAAC;IAC9C;AACF,EAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { MigrationTemplateArgs } from 'payload';
|
|
2
|
+
export declare const indent: (text: string) => string;
|
|
3
|
+
export declare const getMigrationTemplate: ({ downSQL, imports, packageName, upSQL, }: MigrationTemplateArgs) => string;
|
|
4
|
+
//# sourceMappingURL=getMigrationTemplate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMigrationTemplate.d.ts","sourceRoot":"","sources":["../../src/postgres/getMigrationTemplate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAEpD,eAAO,MAAM,MAAM,SAAU,MAAM,WAIpB,CAAA;AAEf,eAAO,MAAM,oBAAoB,8CAK9B,qBAAqB,KAAG,MAS1B,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const indent = (text)=>text.split('\n').map((line)=>` ${line}`).join('\n');
|
|
2
|
+
export const getMigrationTemplate = ({ downSQL, imports, packageName, upSQL })=>`import { MigrateUpArgs, MigrateDownArgs, sql } from '${packageName}'
|
|
3
|
+
${imports ? `${imports}\n` : ''}
|
|
4
|
+
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
|
|
5
|
+
${indent(upSQL)}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function down({ payload, req }: MigrateDownArgs): Promise<void> {
|
|
9
|
+
${indent(downSQL)}
|
|
10
|
+
}
|
|
11
|
+
`;
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=getMigrationTemplate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/postgres/getMigrationTemplate.ts"],"sourcesContent":["import type { MigrationTemplateArgs } from 'payload'\n\nexport const indent = (text: string) =>\n text\n .split('\\n')\n .map((line) => ` ${line}`)\n .join('\\n')\n\nexport const getMigrationTemplate = ({\n downSQL,\n imports,\n packageName,\n upSQL,\n}: MigrationTemplateArgs): string => `import { MigrateUpArgs, MigrateDownArgs, sql } from '${packageName}'\n${imports ? `${imports}\\n` : ''}\nexport async function up({ payload, req }: MigrateUpArgs): Promise<void> {\n${indent(upSQL)}\n}\n\nexport async function down({ payload, req }: MigrateDownArgs): Promise<void> {\n${indent(downSQL)}\n}\n`\n"],"names":["indent","text","split","map","line","join","getMigrationTemplate","downSQL","imports","packageName","upSQL"],"mappings":"AAEA,OAAO,MAAMA,SAAS,CAACC,OACrBA,KACGC,KAAK,CAAC,MACNC,GAAG,CAAC,CAACC,OAAS,CAAC,EAAE,EAAEA,KAAK,CAAC,EACzBC,IAAI,CAAC,MAAK;AAEf,OAAO,MAAMC,uBAAuB,CAAC,EACnCC,OAAO,EACPC,OAAO,EACPC,WAAW,EACXC,KAAK,EACiB,GAAa,CAAC,qDAAqD,EAAED,YAAY;AACzG,EAAED,UAAU,CAAC,EAAEA,QAAQ,EAAE,CAAC,GAAG,GAAG;;AAEhC,EAAER,OAAOU,OAAO;;;;AAIhB,EAAEV,OAAOO,SAAS;;AAElB,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/postgres/init.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAA6B,MAAM,SAAS,CAAA;AAW9D,eAAO,MAAM,IAAI,EAAE,IAqGlB,CAAA"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { uniqueIndex } from 'drizzle-orm/pg-core';
|
|
2
|
+
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload';
|
|
3
|
+
import toSnakeCase from 'to-snake-case';
|
|
4
|
+
import { createTableName } from '../createTableName.js';
|
|
5
|
+
import { buildTable } from './schema/build.js';
|
|
6
|
+
export const init = function init() {
|
|
7
|
+
if (this.payload.config.localization) {
|
|
8
|
+
this.enums.enum__locales = this.pgSchema.enum('_locales', this.payload.config.localization.locales.map(({ code })=>code));
|
|
9
|
+
}
|
|
10
|
+
this.payload.config.collections.forEach((collection)=>{
|
|
11
|
+
createTableName({
|
|
12
|
+
adapter: this,
|
|
13
|
+
config: collection
|
|
14
|
+
});
|
|
15
|
+
if (collection.versions) {
|
|
16
|
+
createTableName({
|
|
17
|
+
adapter: this,
|
|
18
|
+
config: collection,
|
|
19
|
+
versions: true,
|
|
20
|
+
versionsCustomName: true
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
this.payload.config.collections.forEach((collection)=>{
|
|
25
|
+
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug));
|
|
26
|
+
const baseExtraConfig = {};
|
|
27
|
+
if (collection.upload.filenameCompoundIndex) {
|
|
28
|
+
const indexName = `${tableName}_filename_compound_idx`;
|
|
29
|
+
baseExtraConfig.filename_compound_index = (cols)=>{
|
|
30
|
+
const colsConstraint = collection.upload.filenameCompoundIndex.map((f)=>{
|
|
31
|
+
return cols[f];
|
|
32
|
+
});
|
|
33
|
+
return uniqueIndex(indexName).on(colsConstraint[0], ...colsConstraint.slice(1));
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
buildTable({
|
|
37
|
+
adapter: this,
|
|
38
|
+
baseExtraConfig,
|
|
39
|
+
disableNotNull: !!collection?.versions?.drafts,
|
|
40
|
+
disableUnique: false,
|
|
41
|
+
fields: collection.fields,
|
|
42
|
+
tableName,
|
|
43
|
+
timestamps: collection.timestamps,
|
|
44
|
+
versions: false
|
|
45
|
+
});
|
|
46
|
+
if (collection.versions) {
|
|
47
|
+
const versionsTableName = this.tableNameMap.get(`_${toSnakeCase(collection.slug)}${this.versionsSuffix}`);
|
|
48
|
+
const versionFields = buildVersionCollectionFields(collection);
|
|
49
|
+
buildTable({
|
|
50
|
+
adapter: this,
|
|
51
|
+
disableNotNull: !!collection.versions?.drafts,
|
|
52
|
+
disableUnique: true,
|
|
53
|
+
fields: versionFields,
|
|
54
|
+
tableName: versionsTableName,
|
|
55
|
+
timestamps: true,
|
|
56
|
+
versions: true
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
this.payload.config.globals.forEach((global)=>{
|
|
61
|
+
const tableName = createTableName({
|
|
62
|
+
adapter: this,
|
|
63
|
+
config: global
|
|
64
|
+
});
|
|
65
|
+
buildTable({
|
|
66
|
+
adapter: this,
|
|
67
|
+
disableNotNull: !!global?.versions?.drafts,
|
|
68
|
+
disableUnique: false,
|
|
69
|
+
fields: global.fields,
|
|
70
|
+
tableName,
|
|
71
|
+
timestamps: false,
|
|
72
|
+
versions: false
|
|
73
|
+
});
|
|
74
|
+
if (global.versions) {
|
|
75
|
+
const versionsTableName = createTableName({
|
|
76
|
+
adapter: this,
|
|
77
|
+
config: global,
|
|
78
|
+
versions: true,
|
|
79
|
+
versionsCustomName: true
|
|
80
|
+
});
|
|
81
|
+
const versionFields = buildVersionGlobalFields(global);
|
|
82
|
+
buildTable({
|
|
83
|
+
adapter: this,
|
|
84
|
+
disableNotNull: !!global.versions?.drafts,
|
|
85
|
+
disableUnique: true,
|
|
86
|
+
fields: versionFields,
|
|
87
|
+
tableName: versionsTableName,
|
|
88
|
+
timestamps: true,
|
|
89
|
+
versions: true
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/postgres/init.ts"],"sourcesContent":["import type { Init, SanitizedCollectionConfig } from 'payload'\n\nimport { uniqueIndex } from 'drizzle-orm/pg-core'\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { BaseExtraConfig, BasePostgresAdapter } from './types.js'\n\nimport { createTableName } from '../createTableName.js'\nimport { buildTable } from './schema/build.js'\n\nexport const init: Init = function init(this: BasePostgresAdapter) {\n if (this.payload.config.localization) {\n this.enums.enum__locales = this.pgSchema.enum(\n '_locales',\n this.payload.config.localization.locales.map(({ code }) => code) as [string, ...string[]],\n )\n }\n\n this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {\n createTableName({\n adapter: this,\n config: collection,\n })\n\n if (collection.versions) {\n createTableName({\n adapter: this,\n config: collection,\n versions: true,\n versionsCustomName: true,\n })\n }\n })\n this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {\n const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))\n\n const baseExtraConfig: BaseExtraConfig = {}\n\n if (collection.upload.filenameCompoundIndex) {\n const indexName = `${tableName}_filename_compound_idx`\n\n baseExtraConfig.filename_compound_index = (cols) => {\n const colsConstraint = collection.upload.filenameCompoundIndex.map((f) => {\n return cols[f]\n })\n return uniqueIndex(indexName).on(colsConstraint[0], ...colsConstraint.slice(1))\n }\n }\n\n buildTable({\n adapter: this,\n baseExtraConfig,\n disableNotNull: !!collection?.versions?.drafts,\n disableUnique: false,\n fields: collection.fields,\n tableName,\n timestamps: collection.timestamps,\n versions: false,\n })\n\n if (collection.versions) {\n const versionsTableName = this.tableNameMap.get(\n `_${toSnakeCase(collection.slug)}${this.versionsSuffix}`,\n )\n const versionFields = buildVersionCollectionFields(collection)\n\n buildTable({\n adapter: this,\n disableNotNull: !!collection.versions?.drafts,\n disableUnique: true,\n fields: versionFields,\n tableName: versionsTableName,\n timestamps: true,\n versions: true,\n })\n }\n })\n\n this.payload.config.globals.forEach((global) => {\n const tableName = createTableName({ adapter: this, config: global })\n\n buildTable({\n adapter: this,\n disableNotNull: !!global?.versions?.drafts,\n disableUnique: false,\n fields: global.fields,\n tableName,\n timestamps: false,\n versions: false,\n })\n\n if (global.versions) {\n const versionsTableName = createTableName({\n adapter: this,\n config: global,\n versions: true,\n versionsCustomName: true,\n })\n const versionFields = buildVersionGlobalFields(global)\n\n buildTable({\n adapter: this,\n disableNotNull: !!global.versions?.drafts,\n disableUnique: true,\n fields: versionFields,\n tableName: versionsTableName,\n timestamps: true,\n versions: true,\n })\n }\n })\n}\n"],"names":["uniqueIndex","buildVersionCollectionFields","buildVersionGlobalFields","toSnakeCase","createTableName","buildTable","init","payload","config","localization","enums","enum__locales","pgSchema","enum","locales","map","code","collections","forEach","collection","adapter","versions","versionsCustomName","tableName","tableNameMap","get","slug","baseExtraConfig","upload","filenameCompoundIndex","indexName","filename_compound_index","cols","colsConstraint","f","on","slice","disableNotNull","drafts","disableUnique","fields","timestamps","versionsTableName","versionsSuffix","versionFields","globals","global"],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["../../src/postgres/insert.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC,eAAO,MAAM,MAAM,EAAE,MAoBpB,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const insert = async function insert({ db, onConflictDoUpdate, tableName, values }) {
|
|
2
|
+
const table = this.tables[tableName];
|
|
3
|
+
let result;
|
|
4
|
+
if (onConflictDoUpdate) {
|
|
5
|
+
result = await db.insert(table).values(values).onConflictDoUpdate(onConflictDoUpdate).returning();
|
|
6
|
+
} else {
|
|
7
|
+
result = await db.insert(table).values(values).returning();
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=insert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/postgres/insert.ts"],"sourcesContent":["import type { TransactionPg } from '../types.js'\nimport type { Insert } from './types.js'\n\nexport const insert: Insert = async function insert({\n db,\n onConflictDoUpdate,\n tableName,\n values,\n}): Promise<Record<string, unknown>[]> {\n const table = this.tables[tableName]\n let result\n\n if (onConflictDoUpdate) {\n result = await (db as TransactionPg)\n .insert(table)\n .values(values)\n .onConflictDoUpdate(onConflictDoUpdate)\n .returning()\n } else {\n result = await (db as TransactionPg).insert(table).values(values).returning()\n }\n\n return result\n}\n"],"names":["insert","db","onConflictDoUpdate","tableName","values","table","tables","result","returning"],"mappings":"AAGA,OAAO,MAAMA,SAAiB,eAAeA,OAAO,EAClDC,EAAE,EACFC,kBAAkB,EAClBC,SAAS,EACTC,MAAM,EACP;IACC,MAAMC,QAAQ,IAAI,CAACC,MAAM,CAACH,UAAU;IACpC,IAAII;IAEJ,IAAIL,oBAAoB;QACtBK,SAAS,MAAM,AAACN,GACbD,MAAM,CAACK,OACPD,MAAM,CAACA,QACPF,kBAAkB,CAACA,oBACnBM,SAAS;IACd,OAAO;QACLD,SAAS,MAAM,AAACN,GAAqBD,MAAM,CAACK,OAAOD,MAAM,CAACA,QAAQI,SAAS;IAC7E;IAEA,OAAOD;AACT,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requireDrizzleKit.d.ts","sourceRoot":"","sources":["../../src/postgres/requireDrizzleKit.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAGpD,eAAO,MAAM,iBAAiB,EAAE,iBAAoD,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/postgres/requireDrizzleKit.ts"],"sourcesContent":["import { createRequire } from 'module'\n\nimport type { RequireDrizzleKit } from '../types.js'\n\nconst require = createRequire(import.meta.url)\nexport const requireDrizzleKit: RequireDrizzleKit = () => require('drizzle-kit/api')\n"],"names":["createRequire","require","url","requireDrizzleKit"],"mappings":"AAAA,SAASA,aAAa,QAAQ,SAAQ;AAItC,MAAMC,UAAUD,cAAc,YAAYE,GAAG;AAC7C,OAAO,MAAMC,oBAAuC,IAAMF,QAAQ,mBAAkB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { PgColumnBuilder } from 'drizzle-orm/pg-core';
|
|
2
|
+
import type { Field } from 'payload';
|
|
3
|
+
import type { BaseExtraConfig, BasePostgresAdapter, RelationMap } from '../types.js';
|
|
4
|
+
type Args = {
|
|
5
|
+
adapter: BasePostgresAdapter;
|
|
6
|
+
baseColumns?: Record<string, PgColumnBuilder>;
|
|
7
|
+
/**
|
|
8
|
+
* After table is created, run these functions to add extra config to the table
|
|
9
|
+
* ie. indexes, multiple columns, etc
|
|
10
|
+
*/
|
|
11
|
+
baseExtraConfig?: BaseExtraConfig;
|
|
12
|
+
buildNumbers?: boolean;
|
|
13
|
+
buildRelationships?: boolean;
|
|
14
|
+
disableNotNull: boolean;
|
|
15
|
+
disableUnique: boolean;
|
|
16
|
+
fields: Field[];
|
|
17
|
+
rootRelationsToBuild?: RelationMap;
|
|
18
|
+
rootRelationships?: Set<string>;
|
|
19
|
+
rootTableIDColType?: string;
|
|
20
|
+
rootTableName?: string;
|
|
21
|
+
tableName: string;
|
|
22
|
+
timestamps?: boolean;
|
|
23
|
+
versions: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Tracks whether or not this table is built
|
|
26
|
+
* from the result of a localized array or block field at some point
|
|
27
|
+
*/
|
|
28
|
+
withinLocalizedArrayOrBlock?: boolean;
|
|
29
|
+
};
|
|
30
|
+
type Result = {
|
|
31
|
+
hasLocalizedManyNumberField: boolean;
|
|
32
|
+
hasLocalizedManyTextField: boolean;
|
|
33
|
+
hasLocalizedRelationshipField: boolean;
|
|
34
|
+
hasManyNumberField: 'index' | boolean;
|
|
35
|
+
hasManyTextField: 'index' | boolean;
|
|
36
|
+
relationsToBuild: RelationMap;
|
|
37
|
+
};
|
|
38
|
+
export declare const buildTable: ({ adapter, baseColumns, baseExtraConfig, disableNotNull, disableUnique, fields, rootRelationsToBuild, rootRelationships, rootTableIDColType, rootTableName: incomingRootTableName, tableName, timestamps, versions, withinLocalizedArrayOrBlock, }: Args) => Result;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=build.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../../../src/postgres/schema/build.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,eAAe,EAEhB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAepC,OAAO,KAAK,EACV,eAAe,EACf,mBAAmB,EAInB,WAAW,EACZ,MAAM,aAAa,CAAA;AAOpB,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,mBAAmB,CAAA;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC7C;;;OAGG;IACH,eAAe,CAAC,EAAE,eAAe,CAAA;IACjC,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAC5B,cAAc,EAAE,OAAO,CAAA;IACvB,aAAa,EAAE,OAAO,CAAA;IACtB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,oBAAoB,CAAC,EAAE,WAAW,CAAA;IAClC,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC/B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,QAAQ,EAAE,OAAO,CAAA;IACjB;;;OAGG;IACH,2BAA2B,CAAC,EAAE,OAAO,CAAA;CACtC,CAAA;AAED,KAAK,MAAM,GAAG;IACZ,2BAA2B,EAAE,OAAO,CAAA;IACpC,yBAAyB,EAAE,OAAO,CAAA;IAClC,6BAA6B,EAAE,OAAO,CAAA;IACtC,kBAAkB,EAAE,OAAO,GAAG,OAAO,CAAA;IACrC,gBAAgB,EAAE,OAAO,GAAG,OAAO,CAAA;IACnC,gBAAgB,EAAE,WAAW,CAAA;CAC9B,CAAA;AAED,eAAO,MAAM,UAAU,uPAepB,IAAI,KAAG,MA6YT,CAAA"}
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { relations } from 'drizzle-orm';
|
|
2
|
+
import { foreignKey, index, integer, numeric, serial, timestamp, unique, varchar } from 'drizzle-orm/pg-core';
|
|
3
|
+
import toSnakeCase from 'to-snake-case';
|
|
4
|
+
import { createTableName } from '../../createTableName.js';
|
|
5
|
+
import { parentIDColumnMap } from './parentIDColumnMap.js';
|
|
6
|
+
import { setColumnID } from './setColumnID.js';
|
|
7
|
+
import { traverseFields } from './traverseFields.js';
|
|
8
|
+
export const buildTable = ({ adapter, baseColumns = {}, baseExtraConfig = {}, disableNotNull, disableUnique = false, fields, rootRelationsToBuild, rootRelationships, rootTableIDColType, rootTableName: incomingRootTableName, tableName, timestamps, versions, withinLocalizedArrayOrBlock })=>{
|
|
9
|
+
const isRoot = !incomingRootTableName;
|
|
10
|
+
const rootTableName = incomingRootTableName || tableName;
|
|
11
|
+
const columns = baseColumns;
|
|
12
|
+
const indexes = {};
|
|
13
|
+
const localesColumns = {};
|
|
14
|
+
const localesIndexes = {};
|
|
15
|
+
let localesTable;
|
|
16
|
+
let textsTable;
|
|
17
|
+
let numbersTable;
|
|
18
|
+
// Relationships to the base collection
|
|
19
|
+
const relationships = rootRelationships || new Set();
|
|
20
|
+
let relationshipsTable;
|
|
21
|
+
// Drizzle relations
|
|
22
|
+
const relationsToBuild = new Map();
|
|
23
|
+
const idColType = setColumnID({
|
|
24
|
+
adapter,
|
|
25
|
+
columns,
|
|
26
|
+
fields
|
|
27
|
+
});
|
|
28
|
+
const { hasLocalizedField, hasLocalizedManyNumberField, hasLocalizedManyTextField, hasLocalizedRelationshipField, hasManyNumberField, hasManyTextField } = traverseFields({
|
|
29
|
+
adapter,
|
|
30
|
+
columns,
|
|
31
|
+
disableNotNull,
|
|
32
|
+
disableUnique,
|
|
33
|
+
fields,
|
|
34
|
+
indexes,
|
|
35
|
+
localesColumns,
|
|
36
|
+
localesIndexes,
|
|
37
|
+
newTableName: tableName,
|
|
38
|
+
parentTableName: tableName,
|
|
39
|
+
relationsToBuild,
|
|
40
|
+
relationships,
|
|
41
|
+
rootRelationsToBuild: rootRelationsToBuild || relationsToBuild,
|
|
42
|
+
rootTableIDColType: rootTableIDColType || idColType,
|
|
43
|
+
rootTableName,
|
|
44
|
+
versions,
|
|
45
|
+
withinLocalizedArrayOrBlock
|
|
46
|
+
});
|
|
47
|
+
// split the relationsToBuild by localized and non-localized
|
|
48
|
+
const localizedRelations = new Map();
|
|
49
|
+
const nonLocalizedRelations = new Map();
|
|
50
|
+
relationsToBuild.forEach(({ type, localized, target }, key)=>{
|
|
51
|
+
const map = localized ? localizedRelations : nonLocalizedRelations;
|
|
52
|
+
map.set(key, {
|
|
53
|
+
type,
|
|
54
|
+
target
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
if (timestamps) {
|
|
58
|
+
columns.createdAt = timestamp('created_at', {
|
|
59
|
+
mode: 'string',
|
|
60
|
+
precision: 3,
|
|
61
|
+
withTimezone: true
|
|
62
|
+
}).defaultNow().notNull();
|
|
63
|
+
columns.updatedAt = timestamp('updated_at', {
|
|
64
|
+
mode: 'string',
|
|
65
|
+
precision: 3,
|
|
66
|
+
withTimezone: true
|
|
67
|
+
}).defaultNow().notNull();
|
|
68
|
+
}
|
|
69
|
+
const table = adapter.pgSchema.table(tableName, columns, (cols)=>{
|
|
70
|
+
const extraConfig = Object.entries(baseExtraConfig).reduce((config, [key, func])=>{
|
|
71
|
+
config[key] = func(cols);
|
|
72
|
+
return config;
|
|
73
|
+
}, {});
|
|
74
|
+
const result = Object.entries(indexes).reduce((acc, [colName, func])=>{
|
|
75
|
+
acc[colName] = func(cols);
|
|
76
|
+
return acc;
|
|
77
|
+
}, extraConfig);
|
|
78
|
+
return result;
|
|
79
|
+
});
|
|
80
|
+
adapter.tables[tableName] = table;
|
|
81
|
+
if (hasLocalizedField || localizedRelations.size) {
|
|
82
|
+
const localeTableName = `${tableName}${adapter.localesSuffix}`;
|
|
83
|
+
localesColumns.id = serial('id').primaryKey();
|
|
84
|
+
localesColumns._locale = adapter.enums.enum__locales('_locale').notNull();
|
|
85
|
+
localesColumns._parentID = parentIDColumnMap[idColType]('_parent_id').notNull();
|
|
86
|
+
localesTable = adapter.pgSchema.table(localeTableName, localesColumns, (cols)=>{
|
|
87
|
+
return Object.entries(localesIndexes).reduce((acc, [colName, func])=>{
|
|
88
|
+
acc[colName] = func(cols);
|
|
89
|
+
return acc;
|
|
90
|
+
}, {
|
|
91
|
+
_localeParent: unique(`${localeTableName}_locale_parent_id_unique`).on(cols._locale, cols._parentID),
|
|
92
|
+
_parentIdFk: foreignKey({
|
|
93
|
+
name: `${localeTableName}_parent_id_fk`,
|
|
94
|
+
columns: [
|
|
95
|
+
cols._parentID
|
|
96
|
+
],
|
|
97
|
+
foreignColumns: [
|
|
98
|
+
table.id
|
|
99
|
+
]
|
|
100
|
+
}).onDelete('cascade')
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
adapter.tables[localeTableName] = localesTable;
|
|
104
|
+
adapter.relations[`relations_${localeTableName}`] = relations(localesTable, ({ many, one })=>{
|
|
105
|
+
const result = {};
|
|
106
|
+
result._parentID = one(table, {
|
|
107
|
+
fields: [
|
|
108
|
+
localesTable._parentID
|
|
109
|
+
],
|
|
110
|
+
references: [
|
|
111
|
+
table.id
|
|
112
|
+
],
|
|
113
|
+
// name the relationship by what the many() relationName is
|
|
114
|
+
relationName: '_locales'
|
|
115
|
+
});
|
|
116
|
+
localizedRelations.forEach(({ type, target }, key)=>{
|
|
117
|
+
if (type === 'one') {
|
|
118
|
+
result[key] = one(adapter.tables[target], {
|
|
119
|
+
fields: [
|
|
120
|
+
localesTable[key]
|
|
121
|
+
],
|
|
122
|
+
references: [
|
|
123
|
+
adapter.tables[target].id
|
|
124
|
+
],
|
|
125
|
+
relationName: key
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (type === 'many') {
|
|
129
|
+
result[key] = many(adapter.tables[target], {
|
|
130
|
+
relationName: key
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
return result;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (isRoot) {
|
|
138
|
+
if (hasManyTextField) {
|
|
139
|
+
const textsTableName = `${rootTableName}_texts`;
|
|
140
|
+
const columns = {
|
|
141
|
+
id: serial('id').primaryKey(),
|
|
142
|
+
order: integer('order').notNull(),
|
|
143
|
+
parent: parentIDColumnMap[idColType]('parent_id').notNull(),
|
|
144
|
+
path: varchar('path').notNull(),
|
|
145
|
+
text: varchar('text')
|
|
146
|
+
};
|
|
147
|
+
if (hasLocalizedManyTextField) {
|
|
148
|
+
columns.locale = adapter.enums.enum__locales('locale');
|
|
149
|
+
}
|
|
150
|
+
textsTable = adapter.pgSchema.table(textsTableName, columns, (cols)=>{
|
|
151
|
+
const config = {
|
|
152
|
+
orderParentIdx: index(`${textsTableName}_order_parent_idx`).on(cols.order, cols.parent),
|
|
153
|
+
parentFk: foreignKey({
|
|
154
|
+
name: `${textsTableName}_parent_fk`,
|
|
155
|
+
columns: [
|
|
156
|
+
cols.parent
|
|
157
|
+
],
|
|
158
|
+
foreignColumns: [
|
|
159
|
+
table.id
|
|
160
|
+
]
|
|
161
|
+
}).onDelete('cascade')
|
|
162
|
+
};
|
|
163
|
+
if (hasManyTextField === 'index') {
|
|
164
|
+
config.text_idx = index(`${textsTableName}_text_idx`).on(cols.text);
|
|
165
|
+
}
|
|
166
|
+
if (hasLocalizedManyTextField) {
|
|
167
|
+
config.localeParent = index(`${textsTableName}_locale_parent`).on(cols.locale, cols.parent);
|
|
168
|
+
}
|
|
169
|
+
return config;
|
|
170
|
+
});
|
|
171
|
+
adapter.tables[textsTableName] = textsTable;
|
|
172
|
+
adapter.relations[`relations_${textsTableName}`] = relations(textsTable, ({ one })=>({
|
|
173
|
+
parent: one(table, {
|
|
174
|
+
fields: [
|
|
175
|
+
textsTable.parent
|
|
176
|
+
],
|
|
177
|
+
references: [
|
|
178
|
+
table.id
|
|
179
|
+
],
|
|
180
|
+
relationName: '_texts'
|
|
181
|
+
})
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
if (hasManyNumberField) {
|
|
185
|
+
const numbersTableName = `${rootTableName}_numbers`;
|
|
186
|
+
const columns = {
|
|
187
|
+
id: serial('id').primaryKey(),
|
|
188
|
+
number: numeric('number'),
|
|
189
|
+
order: integer('order').notNull(),
|
|
190
|
+
parent: parentIDColumnMap[idColType]('parent_id').notNull(),
|
|
191
|
+
path: varchar('path').notNull()
|
|
192
|
+
};
|
|
193
|
+
if (hasLocalizedManyNumberField) {
|
|
194
|
+
columns.locale = adapter.enums.enum__locales('locale');
|
|
195
|
+
}
|
|
196
|
+
numbersTable = adapter.pgSchema.table(numbersTableName, columns, (cols)=>{
|
|
197
|
+
const config = {
|
|
198
|
+
orderParentIdx: index(`${numbersTableName}_order_parent_idx`).on(cols.order, cols.parent),
|
|
199
|
+
parentFk: foreignKey({
|
|
200
|
+
name: `${numbersTableName}_parent_fk`,
|
|
201
|
+
columns: [
|
|
202
|
+
cols.parent
|
|
203
|
+
],
|
|
204
|
+
foreignColumns: [
|
|
205
|
+
table.id
|
|
206
|
+
]
|
|
207
|
+
}).onDelete('cascade')
|
|
208
|
+
};
|
|
209
|
+
if (hasManyNumberField === 'index') {
|
|
210
|
+
config.numberIdx = index(`${numbersTableName}_number_idx`).on(cols.number);
|
|
211
|
+
}
|
|
212
|
+
if (hasLocalizedManyNumberField) {
|
|
213
|
+
config.localeParent = index(`${numbersTableName}_locale_parent`).on(cols.locale, cols.parent);
|
|
214
|
+
}
|
|
215
|
+
return config;
|
|
216
|
+
});
|
|
217
|
+
adapter.tables[numbersTableName] = numbersTable;
|
|
218
|
+
adapter.relations[`relations_${numbersTableName}`] = relations(numbersTable, ({ one })=>({
|
|
219
|
+
parent: one(table, {
|
|
220
|
+
fields: [
|
|
221
|
+
numbersTable.parent
|
|
222
|
+
],
|
|
223
|
+
references: [
|
|
224
|
+
table.id
|
|
225
|
+
],
|
|
226
|
+
relationName: '_numbers'
|
|
227
|
+
})
|
|
228
|
+
}));
|
|
229
|
+
}
|
|
230
|
+
if (relationships.size) {
|
|
231
|
+
const relationshipColumns = {
|
|
232
|
+
id: serial('id').primaryKey(),
|
|
233
|
+
order: integer('order'),
|
|
234
|
+
parent: parentIDColumnMap[idColType]('parent_id').notNull(),
|
|
235
|
+
path: varchar('path').notNull()
|
|
236
|
+
};
|
|
237
|
+
if (hasLocalizedRelationshipField) {
|
|
238
|
+
relationshipColumns.locale = adapter.enums.enum__locales('locale');
|
|
239
|
+
}
|
|
240
|
+
const relationExtraConfig = {};
|
|
241
|
+
const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`;
|
|
242
|
+
relationships.forEach((relationTo)=>{
|
|
243
|
+
const relationshipConfig = adapter.payload.collections[relationTo].config;
|
|
244
|
+
const formattedRelationTo = createTableName({
|
|
245
|
+
adapter,
|
|
246
|
+
config: relationshipConfig,
|
|
247
|
+
throwValidationError: true
|
|
248
|
+
});
|
|
249
|
+
let colType = adapter.idType === 'uuid' ? 'uuid' : 'integer';
|
|
250
|
+
const relatedCollectionCustomIDType = adapter.payload.collections[relationshipConfig.slug]?.customIDType;
|
|
251
|
+
if (relatedCollectionCustomIDType === 'number') colType = 'numeric';
|
|
252
|
+
if (relatedCollectionCustomIDType === 'text') colType = 'varchar';
|
|
253
|
+
relationshipColumns[`${relationTo}ID`] = parentIDColumnMap[colType](`${formattedRelationTo}_id`);
|
|
254
|
+
relationExtraConfig[`${relationTo}IdFk`] = (cols)=>foreignKey({
|
|
255
|
+
name: `${relationshipsTableName}_${toSnakeCase(relationTo)}_fk`,
|
|
256
|
+
columns: [
|
|
257
|
+
cols[`${relationTo}ID`]
|
|
258
|
+
],
|
|
259
|
+
foreignColumns: [
|
|
260
|
+
adapter.tables[formattedRelationTo].id
|
|
261
|
+
]
|
|
262
|
+
}).onDelete('cascade');
|
|
263
|
+
});
|
|
264
|
+
relationshipsTable = adapter.pgSchema.table(relationshipsTableName, relationshipColumns, (cols)=>{
|
|
265
|
+
const result = Object.entries(relationExtraConfig).reduce((config, [key, func])=>{
|
|
266
|
+
config[key] = func(cols);
|
|
267
|
+
return config;
|
|
268
|
+
}, {
|
|
269
|
+
order: index(`${relationshipsTableName}_order_idx`).on(cols.order),
|
|
270
|
+
parentFk: foreignKey({
|
|
271
|
+
name: `${relationshipsTableName}_parent_fk`,
|
|
272
|
+
columns: [
|
|
273
|
+
cols.parent
|
|
274
|
+
],
|
|
275
|
+
foreignColumns: [
|
|
276
|
+
table.id
|
|
277
|
+
]
|
|
278
|
+
}).onDelete('cascade'),
|
|
279
|
+
parentIdx: index(`${relationshipsTableName}_parent_idx`).on(cols.parent),
|
|
280
|
+
pathIdx: index(`${relationshipsTableName}_path_idx`).on(cols.path)
|
|
281
|
+
});
|
|
282
|
+
if (hasLocalizedRelationshipField) {
|
|
283
|
+
result.localeIdx = index(`${relationshipsTableName}_locale_idx`).on(cols.locale);
|
|
284
|
+
}
|
|
285
|
+
return result;
|
|
286
|
+
});
|
|
287
|
+
adapter.tables[relationshipsTableName] = relationshipsTable;
|
|
288
|
+
adapter.relations[`relations_${relationshipsTableName}`] = relations(relationshipsTable, ({ one })=>{
|
|
289
|
+
const result = {
|
|
290
|
+
parent: one(table, {
|
|
291
|
+
fields: [
|
|
292
|
+
relationshipsTable.parent
|
|
293
|
+
],
|
|
294
|
+
references: [
|
|
295
|
+
table.id
|
|
296
|
+
],
|
|
297
|
+
relationName: '_rels'
|
|
298
|
+
})
|
|
299
|
+
};
|
|
300
|
+
relationships.forEach((relationTo)=>{
|
|
301
|
+
const relatedTableName = createTableName({
|
|
302
|
+
adapter,
|
|
303
|
+
config: adapter.payload.collections[relationTo].config,
|
|
304
|
+
throwValidationError: true
|
|
305
|
+
});
|
|
306
|
+
const idColumnName = `${relationTo}ID`;
|
|
307
|
+
result[idColumnName] = one(adapter.tables[relatedTableName], {
|
|
308
|
+
fields: [
|
|
309
|
+
relationshipsTable[idColumnName]
|
|
310
|
+
],
|
|
311
|
+
references: [
|
|
312
|
+
adapter.tables[relatedTableName].id
|
|
313
|
+
],
|
|
314
|
+
relationName: relationTo
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
return result;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
adapter.relations[`relations_${tableName}`] = relations(table, ({ many, one })=>{
|
|
322
|
+
const result = {};
|
|
323
|
+
nonLocalizedRelations.forEach(({ type, target }, key)=>{
|
|
324
|
+
if (type === 'one') {
|
|
325
|
+
result[key] = one(adapter.tables[target], {
|
|
326
|
+
fields: [
|
|
327
|
+
table[key]
|
|
328
|
+
],
|
|
329
|
+
references: [
|
|
330
|
+
adapter.tables[target].id
|
|
331
|
+
],
|
|
332
|
+
relationName: key
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
if (type === 'many') {
|
|
336
|
+
result[key] = many(adapter.tables[target], {
|
|
337
|
+
relationName: key
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
if (hasLocalizedField) {
|
|
342
|
+
result._locales = many(localesTable, {
|
|
343
|
+
relationName: '_locales'
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
if (hasManyTextField) {
|
|
347
|
+
result._texts = many(textsTable, {
|
|
348
|
+
relationName: '_texts'
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
if (hasManyNumberField) {
|
|
352
|
+
result._numbers = many(numbersTable, {
|
|
353
|
+
relationName: '_numbers'
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
if (relationships.size && relationshipsTable) {
|
|
357
|
+
result._rels = many(relationshipsTable, {
|
|
358
|
+
relationName: '_rels'
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
return result;
|
|
362
|
+
});
|
|
363
|
+
return {
|
|
364
|
+
hasLocalizedManyNumberField,
|
|
365
|
+
hasLocalizedManyTextField,
|
|
366
|
+
hasLocalizedRelationshipField,
|
|
367
|
+
hasManyNumberField,
|
|
368
|
+
hasManyTextField,
|
|
369
|
+
relationsToBuild
|
|
370
|
+
};
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
//# sourceMappingURL=build.js.map
|