@payloadcms/drizzle 3.94.0-internal.78c6d86 → 3.94.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exports/postgres.d.ts +1 -0
- package/dist/exports/postgres.d.ts.map +1 -1
- package/dist/exports/postgres.js +1 -0
- package/dist/exports/postgres.js.map +1 -1
- package/dist/postgres/findDocumentLocks.d.ts +4 -0
- package/dist/postgres/findDocumentLocks.d.ts.map +1 -0
- package/dist/postgres/findDocumentLocks.js +22 -0
- package/dist/postgres/findDocumentLocks.js.map +1 -0
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +14 -2
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/schema/build.js +9 -5
- package/dist/schema/build.js.map +1 -1
- package/dist/schema/buildRawSchema.spec.js +152 -4
- package/dist/schema/buildRawSchema.spec.js.map +1 -1
- package/dist/schema/traverseFields.d.ts.map +1 -1
- package/dist/schema/traverseFields.js +43 -29
- package/dist/schema/traverseFields.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 +5 -1
- 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 +27 -1
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/transform/write/types.d.ts +10 -0
- package/dist/transform/write/types.d.ts.map +1 -1
- package/dist/transform/write/types.js.map +1 -1
- package/dist/updateJobs.d.ts.map +1 -1
- package/dist/updateJobs.js +144 -3
- package/dist/updateJobs.js.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.d.ts +6 -1
- package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.js +21 -3
- package/dist/upsertRow/deleteExistingRowsByPath.js.map +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +34 -10
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/utilities/createSchemaGenerator.d.ts.map +1 -1
- package/dist/utilities/createSchemaGenerator.js +17 -1
- package/dist/utilities/createSchemaGenerator.js.map +1 -1
- package/dist/utilities/createSchemaGenerator.spec.js +172 -0
- package/dist/utilities/createSchemaGenerator.spec.js.map +1 -0
- package/dist/utilities/fitIdentifierLength.d.ts +25 -0
- package/dist/utilities/fitIdentifierLength.d.ts.map +1 -0
- package/dist/utilities/fitIdentifierLength.js +35 -0
- package/dist/utilities/fitIdentifierLength.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { mkdtemp, readFile, rm } from 'fs/promises';
|
|
2
|
+
import { tmpdir } from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { sanitizeConfig } from 'payload';
|
|
5
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
6
|
+
import { columnToCodeConverter } from '../postgres/columnToCodeConverter.js';
|
|
7
|
+
import { setColumnID } from '../postgres/schema/setColumnID.js';
|
|
8
|
+
import { buildRawSchema } from '../schema/buildRawSchema.js';
|
|
9
|
+
import { createSchemaGenerator } from './createSchemaGenerator.js';
|
|
10
|
+
const generateSchema = createSchemaGenerator({
|
|
11
|
+
columnToCodeConverter,
|
|
12
|
+
corePackageSuffix: 'pg-core',
|
|
13
|
+
defaultOutputFile: undefined,
|
|
14
|
+
enumImport: 'pgEnum',
|
|
15
|
+
schemaImport: 'pgSchema',
|
|
16
|
+
tableImport: 'pgTable'
|
|
17
|
+
});
|
|
18
|
+
const createAdapter = async (config)=>{
|
|
19
|
+
const sanitized = await sanitizeConfig(config);
|
|
20
|
+
return {
|
|
21
|
+
name: 'postgres',
|
|
22
|
+
blocksAsJSON: false,
|
|
23
|
+
fieldConstraints: {},
|
|
24
|
+
idType: 'serial',
|
|
25
|
+
localesSuffix: '_locales',
|
|
26
|
+
packageName: '@payloadcms/db-postgres',
|
|
27
|
+
payload: {
|
|
28
|
+
blocks: {},
|
|
29
|
+
collections: Object.fromEntries(sanitized.collections.map((collection)=>[
|
|
30
|
+
collection.slug,
|
|
31
|
+
{
|
|
32
|
+
config: collection,
|
|
33
|
+
customIDType: undefined
|
|
34
|
+
}
|
|
35
|
+
])),
|
|
36
|
+
config: sanitized,
|
|
37
|
+
logger: {
|
|
38
|
+
warn: ()=>{}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
rawRelations: {},
|
|
42
|
+
rawTables: {},
|
|
43
|
+
tableNameMap: new Map(),
|
|
44
|
+
versionsSuffix: '_versions'
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
let outputDir;
|
|
48
|
+
afterEach(async ()=>{
|
|
49
|
+
if (outputDir) {
|
|
50
|
+
await rm(outputDir, {
|
|
51
|
+
force: true,
|
|
52
|
+
recursive: true
|
|
53
|
+
});
|
|
54
|
+
outputDir = undefined;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
const generate = async (adapter)=>{
|
|
58
|
+
buildRawSchema({
|
|
59
|
+
adapter,
|
|
60
|
+
setColumnID
|
|
61
|
+
});
|
|
62
|
+
outputDir = await mkdtemp(path.join(tmpdir(), 'payload-schema-'));
|
|
63
|
+
const outputFile = path.join(outputDir, 'schema.ts');
|
|
64
|
+
await generateSchema.call(adapter, {
|
|
65
|
+
log: false,
|
|
66
|
+
outputFile,
|
|
67
|
+
prettify: false
|
|
68
|
+
});
|
|
69
|
+
return readFile(outputFile, 'utf8');
|
|
70
|
+
};
|
|
71
|
+
const longGroupName = 'some_long_group_name_that_pushes_the_fk_over_the_limit';
|
|
72
|
+
describe('createSchemaGenerator', ()=>{
|
|
73
|
+
it('should type the extras callback when a promoted foreign key references its own table', async ()=>{
|
|
74
|
+
const adapter = await createAdapter({
|
|
75
|
+
collections: [
|
|
76
|
+
{
|
|
77
|
+
slug: 'pages',
|
|
78
|
+
fields: [
|
|
79
|
+
{
|
|
80
|
+
name: longGroupName,
|
|
81
|
+
type: 'group',
|
|
82
|
+
fields: [
|
|
83
|
+
{
|
|
84
|
+
name: 'parent',
|
|
85
|
+
type: 'relationship',
|
|
86
|
+
relationTo: 'pages'
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
],
|
|
91
|
+
timestamps: false,
|
|
92
|
+
versions: false
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
});
|
|
96
|
+
const schema = await generate(adapter);
|
|
97
|
+
expect(adapter.rawTables.pages.foreignKeys).toBeDefined();
|
|
98
|
+
expect(schema).toContain('type PgTableExtraConfigValue');
|
|
99
|
+
expect(schema).toMatch(/\(columns\): PgTableExtraConfigValue\[\] => \[/);
|
|
100
|
+
});
|
|
101
|
+
it('should treat promoted foreign keys as edges when detecting circular references', async ()=>{
|
|
102
|
+
const adapter = await createAdapter({
|
|
103
|
+
collections: [
|
|
104
|
+
{
|
|
105
|
+
slug: 'members',
|
|
106
|
+
fields: [
|
|
107
|
+
{
|
|
108
|
+
name: longGroupName,
|
|
109
|
+
type: 'group',
|
|
110
|
+
fields: [
|
|
111
|
+
{
|
|
112
|
+
name: 'avatar',
|
|
113
|
+
type: 'upload',
|
|
114
|
+
relationTo: 'media'
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
}
|
|
118
|
+
],
|
|
119
|
+
timestamps: false,
|
|
120
|
+
versions: false
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
slug: 'media',
|
|
124
|
+
fields: [
|
|
125
|
+
{
|
|
126
|
+
name: 'owner',
|
|
127
|
+
type: 'relationship',
|
|
128
|
+
relationTo: 'members'
|
|
129
|
+
}
|
|
130
|
+
],
|
|
131
|
+
timestamps: false,
|
|
132
|
+
versions: false,
|
|
133
|
+
upload: true
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
});
|
|
137
|
+
const schema = await generate(adapter);
|
|
138
|
+
// `members` -> `media` is promoted to a table-level foreign key, `media` -> `members` stays
|
|
139
|
+
// inline. Both sides of the cycle need an explicit type.
|
|
140
|
+
expect(adapter.rawTables.members.columns[`${longGroupName}_avatar`].reference).toBeUndefined();
|
|
141
|
+
expect(schema).toMatch(/\(\): AnyPgColumn => members\.id/);
|
|
142
|
+
expect(schema).toMatch(/\(columns\): PgTableExtraConfigValue\[\] => \[/);
|
|
143
|
+
});
|
|
144
|
+
it('should leave the extras callback untyped when no foreign key points back at the table', async ()=>{
|
|
145
|
+
const adapter = await createAdapter({
|
|
146
|
+
collections: [
|
|
147
|
+
{
|
|
148
|
+
slug: 'posts',
|
|
149
|
+
fields: [
|
|
150
|
+
{
|
|
151
|
+
name: 'tags',
|
|
152
|
+
type: 'array',
|
|
153
|
+
fields: [
|
|
154
|
+
{
|
|
155
|
+
name: 'tag',
|
|
156
|
+
type: 'text'
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
}
|
|
160
|
+
],
|
|
161
|
+
timestamps: false,
|
|
162
|
+
versions: false
|
|
163
|
+
}
|
|
164
|
+
]
|
|
165
|
+
});
|
|
166
|
+
const schema = await generate(adapter);
|
|
167
|
+
expect(schema).not.toContain('PgTableExtraConfigValue');
|
|
168
|
+
expect(schema).toContain('(columns) => [');
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
//# sourceMappingURL=createSchemaGenerator.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/createSchemaGenerator.spec.ts"],"sourcesContent":["import type { Config } from 'payload'\n\nimport { mkdtemp, readFile, rm } from 'fs/promises'\nimport { tmpdir } from 'os'\nimport path from 'path'\nimport { sanitizeConfig } from 'payload'\nimport { afterEach, describe, expect, it } from 'vitest'\n\nimport type { DrizzleAdapter } from '../types.js'\n\nimport { columnToCodeConverter } from '../postgres/columnToCodeConverter.js'\nimport { setColumnID } from '../postgres/schema/setColumnID.js'\nimport { buildRawSchema } from '../schema/buildRawSchema.js'\nimport { createSchemaGenerator } from './createSchemaGenerator.js'\n\nconst generateSchema = createSchemaGenerator({\n columnToCodeConverter,\n corePackageSuffix: 'pg-core',\n defaultOutputFile: undefined,\n enumImport: 'pgEnum',\n schemaImport: 'pgSchema',\n tableImport: 'pgTable',\n})\n\nconst createAdapter = async (config: Config): Promise<DrizzleAdapter> => {\n const sanitized = await sanitizeConfig(config)\n\n return {\n name: 'postgres',\n blocksAsJSON: false,\n fieldConstraints: {},\n idType: 'serial',\n localesSuffix: '_locales',\n packageName: '@payloadcms/db-postgres',\n payload: {\n blocks: {},\n collections: Object.fromEntries(\n sanitized.collections.map((collection) => [\n collection.slug,\n { config: collection, customIDType: undefined },\n ]),\n ),\n config: sanitized,\n logger: { warn: () => {} },\n },\n rawRelations: {},\n rawTables: {},\n tableNameMap: new Map(),\n versionsSuffix: '_versions',\n } as unknown as DrizzleAdapter\n}\n\nlet outputDir: string | undefined\n\nafterEach(async () => {\n if (outputDir) {\n await rm(outputDir, { force: true, recursive: true })\n outputDir = undefined\n }\n})\n\nconst generate = async (adapter: DrizzleAdapter): Promise<string> => {\n buildRawSchema({ adapter, setColumnID })\n\n outputDir = await mkdtemp(path.join(tmpdir(), 'payload-schema-'))\n const outputFile = path.join(outputDir, 'schema.ts')\n\n await generateSchema.call(adapter, { log: false, outputFile, prettify: false })\n\n return readFile(outputFile, 'utf8')\n}\n\nconst longGroupName = 'some_long_group_name_that_pushes_the_fk_over_the_limit'\n\ndescribe('createSchemaGenerator', () => {\n it('should type the extras callback when a promoted foreign key references its own table', async () => {\n const adapter = await createAdapter({\n collections: [\n {\n slug: 'pages',\n fields: [\n {\n name: longGroupName,\n type: 'group',\n fields: [{ name: 'parent', type: 'relationship', relationTo: 'pages' }],\n },\n ],\n timestamps: false,\n versions: false,\n },\n ],\n } as Config)\n\n const schema = await generate(adapter)\n\n expect(adapter.rawTables.pages.foreignKeys).toBeDefined()\n expect(schema).toContain('type PgTableExtraConfigValue')\n expect(schema).toMatch(/\\(columns\\): PgTableExtraConfigValue\\[\\] => \\[/)\n })\n\n it('should treat promoted foreign keys as edges when detecting circular references', async () => {\n const adapter = await createAdapter({\n collections: [\n {\n slug: 'members',\n fields: [\n {\n name: longGroupName,\n type: 'group',\n fields: [{ name: 'avatar', type: 'upload', relationTo: 'media' }],\n },\n ],\n timestamps: false,\n versions: false,\n },\n {\n slug: 'media',\n fields: [{ name: 'owner', type: 'relationship', relationTo: 'members' }],\n timestamps: false,\n versions: false,\n upload: true,\n },\n ],\n } as Config)\n\n const schema = await generate(adapter)\n\n // `members` -> `media` is promoted to a table-level foreign key, `media` -> `members` stays\n // inline. Both sides of the cycle need an explicit type.\n expect(adapter.rawTables.members.columns[`${longGroupName}_avatar`].reference).toBeUndefined()\n expect(schema).toMatch(/\\(\\): AnyPgColumn => members\\.id/)\n expect(schema).toMatch(/\\(columns\\): PgTableExtraConfigValue\\[\\] => \\[/)\n })\n\n it('should leave the extras callback untyped when no foreign key points back at the table', async () => {\n const adapter = await createAdapter({\n collections: [\n {\n slug: 'posts',\n fields: [{ name: 'tags', type: 'array', fields: [{ name: 'tag', type: 'text' }] }],\n timestamps: false,\n versions: false,\n },\n ],\n } as Config)\n\n const schema = await generate(adapter)\n\n expect(schema).not.toContain('PgTableExtraConfigValue')\n expect(schema).toContain('(columns) => [')\n })\n})\n"],"names":["mkdtemp","readFile","rm","tmpdir","path","sanitizeConfig","afterEach","describe","expect","it","columnToCodeConverter","setColumnID","buildRawSchema","createSchemaGenerator","generateSchema","corePackageSuffix","defaultOutputFile","undefined","enumImport","schemaImport","tableImport","createAdapter","config","sanitized","name","blocksAsJSON","fieldConstraints","idType","localesSuffix","packageName","payload","blocks","collections","Object","fromEntries","map","collection","slug","customIDType","logger","warn","rawRelations","rawTables","tableNameMap","Map","versionsSuffix","outputDir","force","recursive","generate","adapter","join","outputFile","call","log","prettify","longGroupName","fields","type","relationTo","timestamps","versions","schema","pages","foreignKeys","toBeDefined","toContain","toMatch","upload","members","columns","reference","toBeUndefined","not"],"mappings":"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"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DrizzleAdapter } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns `<name><suffix>` for a generated index or foreign key name, shortened only when
|
|
4
|
+
* Postgres would otherwise truncate it.
|
|
5
|
+
*
|
|
6
|
+
* Postgres keeps at most 63 characters of an identifier, so a longer name never matches what
|
|
7
|
+
* push reads back from the database and the constraint is dropped and re-added on every run.
|
|
8
|
+
*
|
|
9
|
+
* - Other adapters (SQLite, D1) have no such limit and always get the name unchanged.
|
|
10
|
+
* - Names that already fit are unchanged, so existing databases keep their current names.
|
|
11
|
+
* - Longer names are cut and end with a hash of the full name. The result depends only on
|
|
12
|
+
* `name` and `suffix`, never on which other names exist in the config, so adding or
|
|
13
|
+
* reordering fields doesn't rename constraints on other tables.
|
|
14
|
+
*/
|
|
15
|
+
export declare const fitIdentifierLength: ({ name, adapter, suffix, }: {
|
|
16
|
+
adapter: Pick<DrizzleAdapter, "name">;
|
|
17
|
+
name: string;
|
|
18
|
+
suffix: string;
|
|
19
|
+
}) => string;
|
|
20
|
+
/** Whether Postgres would truncate `identifier`. Always `false` for non-Postgres adapters. */
|
|
21
|
+
export declare const exceedsIdentifierLength: ({ adapter, identifier, }: {
|
|
22
|
+
adapter: Pick<DrizzleAdapter, "name">;
|
|
23
|
+
identifier: string;
|
|
24
|
+
}) => boolean;
|
|
25
|
+
//# sourceMappingURL=fitIdentifierLength.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fitIdentifierLength.d.ts","sourceRoot":"","sources":["../../src/utilities/fitIdentifierLength.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAIjD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,mBAAmB,+BAI7B;IACD,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;IACrC,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;CACf,KAAG,MAUH,CAAA;AAED,8FAA8F;AAC9F,eAAO,MAAM,uBAAuB,6BAGjC;IACD,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;IACrC,UAAU,EAAE,MAAM,CAAA;CACnB,KAAG,OAAiF,CAAA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { maxIdentifierLength } from './validateIdentifierLength.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns `<name><suffix>` for a generated index or foreign key name, shortened only when
|
|
4
|
+
* Postgres would otherwise truncate it.
|
|
5
|
+
*
|
|
6
|
+
* Postgres keeps at most 63 characters of an identifier, so a longer name never matches what
|
|
7
|
+
* push reads back from the database and the constraint is dropped and re-added on every run.
|
|
8
|
+
*
|
|
9
|
+
* - Other adapters (SQLite, D1) have no such limit and always get the name unchanged.
|
|
10
|
+
* - Names that already fit are unchanged, so existing databases keep their current names.
|
|
11
|
+
* - Longer names are cut and end with a hash of the full name. The result depends only on
|
|
12
|
+
* `name` and `suffix`, never on which other names exist in the config, so adding or
|
|
13
|
+
* reordering fields doesn't rename constraints on other tables.
|
|
14
|
+
*/ export const fitIdentifierLength = ({ name, adapter, suffix })=>{
|
|
15
|
+
const identifier = `${name}${suffix}`;
|
|
16
|
+
if (!exceedsIdentifierLength({
|
|
17
|
+
adapter,
|
|
18
|
+
identifier
|
|
19
|
+
})) {
|
|
20
|
+
return identifier;
|
|
21
|
+
}
|
|
22
|
+
const tail = `_${hashIdentifier(identifier)}${suffix}`;
|
|
23
|
+
return `${name.slice(0, maxIdentifierLength - tail.length)}${tail}`;
|
|
24
|
+
};
|
|
25
|
+
/** Whether Postgres would truncate `identifier`. Always `false` for non-Postgres adapters. */ export const exceedsIdentifierLength = ({ adapter, identifier })=>adapter.name === 'postgres' && identifier.length > maxIdentifierLength;
|
|
26
|
+
/** 32-bit FNV-1a hash as 8 hex characters. Avoids `crypto` so it also runs on edge runtimes. */ const hashIdentifier = (value)=>{
|
|
27
|
+
let hash = 0x811c9dc5;
|
|
28
|
+
for(let i = 0; i < value.length; i++){
|
|
29
|
+
hash ^= value.charCodeAt(i);
|
|
30
|
+
hash = Math.imul(hash, 0x01000193);
|
|
31
|
+
}
|
|
32
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=fitIdentifierLength.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/fitIdentifierLength.ts"],"sourcesContent":["import type { DrizzleAdapter } from '../types.js'\n\nimport { maxIdentifierLength } from './validateIdentifierLength.js'\n\n/**\n * Returns `<name><suffix>` for a generated index or foreign key name, shortened only when\n * Postgres would otherwise truncate it.\n *\n * Postgres keeps at most 63 characters of an identifier, so a longer name never matches what\n * push reads back from the database and the constraint is dropped and re-added on every run.\n *\n * - Other adapters (SQLite, D1) have no such limit and always get the name unchanged.\n * - Names that already fit are unchanged, so existing databases keep their current names.\n * - Longer names are cut and end with a hash of the full name. The result depends only on\n * `name` and `suffix`, never on which other names exist in the config, so adding or\n * reordering fields doesn't rename constraints on other tables.\n */\nexport const fitIdentifierLength = ({\n name,\n adapter,\n suffix,\n}: {\n adapter: Pick<DrizzleAdapter, 'name'>\n name: string\n suffix: string\n}): string => {\n const identifier = `${name}${suffix}`\n\n if (!exceedsIdentifierLength({ adapter, identifier })) {\n return identifier\n }\n\n const tail = `_${hashIdentifier(identifier)}${suffix}`\n\n return `${name.slice(0, maxIdentifierLength - tail.length)}${tail}`\n}\n\n/** Whether Postgres would truncate `identifier`. Always `false` for non-Postgres adapters. */\nexport const exceedsIdentifierLength = ({\n adapter,\n identifier,\n}: {\n adapter: Pick<DrizzleAdapter, 'name'>\n identifier: string\n}): boolean => adapter.name === 'postgres' && identifier.length > maxIdentifierLength\n\n/** 32-bit FNV-1a hash as 8 hex characters. Avoids `crypto` so it also runs on edge runtimes. */\nconst hashIdentifier = (value: string): string => {\n let hash = 0x811c9dc5\n\n for (let i = 0; i < value.length; i++) {\n hash ^= value.charCodeAt(i)\n hash = Math.imul(hash, 0x01000193)\n }\n\n return (hash >>> 0).toString(16).padStart(8, '0')\n}\n"],"names":["maxIdentifierLength","fitIdentifierLength","name","adapter","suffix","identifier","exceedsIdentifierLength","tail","hashIdentifier","slice","length","value","hash","i","charCodeAt","Math","imul","toString","padStart"],"mappings":"AAEA,SAASA,mBAAmB,QAAQ,gCAA+B;AAEnE;;;;;;;;;;;;CAYC,GACD,OAAO,MAAMC,sBAAsB,CAAC,EAClCC,IAAI,EACJC,OAAO,EACPC,MAAM,EAKP;IACC,MAAMC,aAAa,GAAGH,OAAOE,QAAQ;IAErC,IAAI,CAACE,wBAAwB;QAAEH;QAASE;IAAW,IAAI;QACrD,OAAOA;IACT;IAEA,MAAME,OAAO,CAAC,CAAC,EAAEC,eAAeH,cAAcD,QAAQ;IAEtD,OAAO,GAAGF,KAAKO,KAAK,CAAC,GAAGT,sBAAsBO,KAAKG,MAAM,IAAIH,MAAM;AACrE,EAAC;AAED,4FAA4F,GAC5F,OAAO,MAAMD,0BAA0B,CAAC,EACtCH,OAAO,EACPE,UAAU,EAIX,GAAcF,QAAQD,IAAI,KAAK,cAAcG,WAAWK,MAAM,GAAGV,oBAAmB;AAErF,8FAA8F,GAC9F,MAAMQ,iBAAiB,CAACG;IACtB,IAAIC,OAAO;IAEX,IAAK,IAAIC,IAAI,GAAGA,IAAIF,MAAMD,MAAM,EAAEG,IAAK;QACrCD,QAAQD,MAAMG,UAAU,CAACD;QACzBD,OAAOG,KAAKC,IAAI,CAACJ,MAAM;IACzB;IAEA,OAAO,AAACA,CAAAA,SAAS,CAAA,EAAGK,QAAQ,CAAC,IAAIC,QAAQ,CAAC,GAAG;AAC/C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/drizzle",
|
|
3
|
-
"version": "3.94.
|
|
3
|
+
"version": "3.94.1",
|
|
4
4
|
"description": "A library of shared functions used by different payload database adapters",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -61,10 +61,10 @@
|
|
|
61
61
|
"@types/pg": "8.20.0",
|
|
62
62
|
"@types/to-snake-case": "1.0.0",
|
|
63
63
|
"esbuild": "0.27.1",
|
|
64
|
-
"payload": "3.94.
|
|
64
|
+
"payload": "3.94.1"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"payload": "3.94.
|
|
67
|
+
"payload": "3.94.1"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
70
|
"build": "pnpm build:swc && pnpm build:types",
|