graphile-i18n 2.20.14 → 2.20.15

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.
Files changed (3) hide show
  1. package/esm/plugin.js +15 -30
  2. package/package.json +2 -2
  3. package/plugin.js +15 -30
package/esm/plugin.js CHANGED
@@ -21,6 +21,7 @@ import 'graphile-build-pg';
21
21
  import { TYPES } from '@dataplan/pg';
22
22
  import { context as grafastContext, lambda, object } from 'grafast';
23
23
  import { withRequestPgClient } from 'graphile-plugin-utils';
24
+ import { sql } from 'pg-sql2';
24
25
  // ─── Helpers ─────────────────────────────────────────────────────────────────
25
26
  function hasI18nTag(codec) {
26
27
  const tags = codec.extensions?.tags;
@@ -74,17 +75,16 @@ export function createI18nPlugin(options = {}) {
74
75
  const translationTableName = hasI18nTag(c);
75
76
  if (!translationTableName)
76
77
  continue;
77
- // Get schema name from the codec's pg extensions
78
- let schemaName = c.extensions?.pg?.schemaName ?? 'public';
78
+ const pg = c.extensions?.pg;
79
+ if (!pg?.schemaName || !pg?.name)
80
+ continue;
81
+ const schemaName = pg.schemaName;
82
+ const baseTable = pg.name;
79
83
  let pkColumn = null;
80
84
  let pkType = 'text';
81
85
  for (const [, resource] of Object.entries(build.input.pgRegistry.pgResources)) {
82
86
  const r = resource;
83
87
  if (r.codec === c) {
84
- // Try multiple sources for schema name
85
- const rSchema = r.extensions?.pg?.schemaName ?? r.schemaName;
86
- if (rSchema)
87
- schemaName = rSchema;
88
88
  // Extract PK from the resource's uniques array
89
89
  const uniques = r.uniques;
90
90
  if (uniques) {
@@ -102,39 +102,22 @@ export function createI18nPlugin(options = {}) {
102
102
  }
103
103
  if (!pkColumn)
104
104
  continue;
105
- // Find the translation codec. The @i18n tag value is the SQL table name
106
- // (e.g. 'posts_translations'), but PostGraphile inflects codec names
107
- // to camelCase (e.g. 'postsTranslations'). Match via resource name.
108
105
  let translationCodec = null;
109
106
  for (const [, resource] of Object.entries(build.input.pgRegistry.pgResources)) {
110
107
  const r = resource;
111
108
  if (!r.codec?.attributes)
112
109
  continue;
113
- // Match by the resource's SQL name (which preserves snake_case)
114
- const sqlName = r.codec?.extensions?.pg?.name ?? r.name;
115
- if (sqlName === translationTableName) {
110
+ const translationPg = r.codec.extensions?.pg;
111
+ if (translationPg?.schemaName === schemaName && translationPg?.name === translationTableName) {
116
112
  translationCodec = r.codec;
117
113
  break;
118
114
  }
119
115
  }
120
- // Fallback: try matching the inflected codec name directly
121
- if (!translationCodec) {
122
- const inflectedName = build.inflection.camelCase(translationTableName);
123
- for (const [, tCodec] of Object.entries(build.input.pgRegistry.pgCodecs)) {
124
- const tc = tCodec;
125
- if (!tc.attributes)
126
- continue;
127
- if (tc.name === translationTableName || tc.name === inflectedName) {
128
- translationCodec = tc;
129
- break;
130
- }
131
- }
132
- }
133
116
  if (!translationCodec)
134
117
  continue;
135
118
  // Find FK column on translation table — convention first, then type match
136
119
  let fkColumn = null;
137
- const conventionalFk = `${c.name}_id`;
120
+ const conventionalFk = `${baseTable}_id`;
138
121
  if (translationCodec.attributes[conventionalFk]) {
139
122
  fkColumn = conventionalFk;
140
123
  }
@@ -173,8 +156,8 @@ export function createI18nPlugin(options = {}) {
173
156
  if (Object.keys(fields).length === 0)
174
157
  continue;
175
158
  i18nRegistry[c.name] = {
176
- baseTable: c.name,
177
- translationTable: translationTableName,
159
+ baseTable,
160
+ translationTable: translationCodec.extensions.pg.name,
178
161
  schemaName,
179
162
  fkColumn,
180
163
  pkColumn,
@@ -211,13 +194,15 @@ export function createI18nPlugin(options = {}) {
211
194
  }
212
195
  const localeType = localeTypeCache[localeTypeName];
213
196
  const { schemaName, baseTable, translationTable, fkColumn, pkColumn, pkType, fields: i18nFields } = info;
197
+ const baseTableSql = sql.compile(sql.identifier(schemaName, baseTable)).text;
198
+ const translationTableSql = sql.compile(sql.identifier(schemaName, translationTable)).text;
214
199
  const coalescedCols = Object.values(i18nFields)
215
200
  .map(f => `coalesce(v."${f.column}", b."${f.column}") as "${f.column}"`)
216
201
  .join(', ');
217
202
  // Build the SQL query template
218
203
  const sqlQuery = `SELECT v."${langCodeColumn}" AS "lang_code", ${coalescedCols}
219
- FROM "${schemaName}"."${baseTable}" b
220
- LEFT JOIN "${schemaName}"."${translationTable}" v
204
+ FROM ${baseTableSql} b
205
+ LEFT JOIN ${translationTableSql} v
221
206
  ON v."${fkColumn}" = b."${pkColumn}"
222
207
  AND array_position($2::text[], v."${langCodeColumn}") IS NOT NULL
223
208
  WHERE b."${pkColumn}" = $1::${pkType}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-i18n",
3
- "version": "2.20.14",
3
+ "version": "2.20.15",
4
4
  "description": "PostGraphile v5 i18n plugin — language-aware fields from @i18n translation tables with Accept-Language negotiation and fallback chains",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "homepage": "https://github.com/constructive-io/constructive",
@@ -64,5 +64,5 @@
64
64
  "accept-language",
65
65
  "locale"
66
66
  ],
67
- "gitHead": "bdfb8311c08b63ef0ed431dd4652c09e2805f37b"
67
+ "gitHead": "785caa052b125a3b4f42c4ff8a68f1be7fcede1a"
68
68
  }
package/plugin.js CHANGED
@@ -25,6 +25,7 @@ require("graphile-build-pg");
25
25
  const pg_1 = require("@dataplan/pg");
26
26
  const grafast_1 = require("grafast");
27
27
  const graphile_plugin_utils_1 = require("graphile-plugin-utils");
28
+ const pg_sql2_1 = require("pg-sql2");
28
29
  // ─── Helpers ─────────────────────────────────────────────────────────────────
29
30
  function hasI18nTag(codec) {
30
31
  const tags = codec.extensions?.tags;
@@ -78,17 +79,16 @@ function createI18nPlugin(options = {}) {
78
79
  const translationTableName = hasI18nTag(c);
79
80
  if (!translationTableName)
80
81
  continue;
81
- // Get schema name from the codec's pg extensions
82
- let schemaName = c.extensions?.pg?.schemaName ?? 'public';
82
+ const pg = c.extensions?.pg;
83
+ if (!pg?.schemaName || !pg?.name)
84
+ continue;
85
+ const schemaName = pg.schemaName;
86
+ const baseTable = pg.name;
83
87
  let pkColumn = null;
84
88
  let pkType = 'text';
85
89
  for (const [, resource] of Object.entries(build.input.pgRegistry.pgResources)) {
86
90
  const r = resource;
87
91
  if (r.codec === c) {
88
- // Try multiple sources for schema name
89
- const rSchema = r.extensions?.pg?.schemaName ?? r.schemaName;
90
- if (rSchema)
91
- schemaName = rSchema;
92
92
  // Extract PK from the resource's uniques array
93
93
  const uniques = r.uniques;
94
94
  if (uniques) {
@@ -106,39 +106,22 @@ function createI18nPlugin(options = {}) {
106
106
  }
107
107
  if (!pkColumn)
108
108
  continue;
109
- // Find the translation codec. The @i18n tag value is the SQL table name
110
- // (e.g. 'posts_translations'), but PostGraphile inflects codec names
111
- // to camelCase (e.g. 'postsTranslations'). Match via resource name.
112
109
  let translationCodec = null;
113
110
  for (const [, resource] of Object.entries(build.input.pgRegistry.pgResources)) {
114
111
  const r = resource;
115
112
  if (!r.codec?.attributes)
116
113
  continue;
117
- // Match by the resource's SQL name (which preserves snake_case)
118
- const sqlName = r.codec?.extensions?.pg?.name ?? r.name;
119
- if (sqlName === translationTableName) {
114
+ const translationPg = r.codec.extensions?.pg;
115
+ if (translationPg?.schemaName === schemaName && translationPg?.name === translationTableName) {
120
116
  translationCodec = r.codec;
121
117
  break;
122
118
  }
123
119
  }
124
- // Fallback: try matching the inflected codec name directly
125
- if (!translationCodec) {
126
- const inflectedName = build.inflection.camelCase(translationTableName);
127
- for (const [, tCodec] of Object.entries(build.input.pgRegistry.pgCodecs)) {
128
- const tc = tCodec;
129
- if (!tc.attributes)
130
- continue;
131
- if (tc.name === translationTableName || tc.name === inflectedName) {
132
- translationCodec = tc;
133
- break;
134
- }
135
- }
136
- }
137
120
  if (!translationCodec)
138
121
  continue;
139
122
  // Find FK column on translation table — convention first, then type match
140
123
  let fkColumn = null;
141
- const conventionalFk = `${c.name}_id`;
124
+ const conventionalFk = `${baseTable}_id`;
142
125
  if (translationCodec.attributes[conventionalFk]) {
143
126
  fkColumn = conventionalFk;
144
127
  }
@@ -177,8 +160,8 @@ function createI18nPlugin(options = {}) {
177
160
  if (Object.keys(fields).length === 0)
178
161
  continue;
179
162
  i18nRegistry[c.name] = {
180
- baseTable: c.name,
181
- translationTable: translationTableName,
163
+ baseTable,
164
+ translationTable: translationCodec.extensions.pg.name,
182
165
  schemaName,
183
166
  fkColumn,
184
167
  pkColumn,
@@ -215,13 +198,15 @@ function createI18nPlugin(options = {}) {
215
198
  }
216
199
  const localeType = localeTypeCache[localeTypeName];
217
200
  const { schemaName, baseTable, translationTable, fkColumn, pkColumn, pkType, fields: i18nFields } = info;
201
+ const baseTableSql = pg_sql2_1.sql.compile(pg_sql2_1.sql.identifier(schemaName, baseTable)).text;
202
+ const translationTableSql = pg_sql2_1.sql.compile(pg_sql2_1.sql.identifier(schemaName, translationTable)).text;
218
203
  const coalescedCols = Object.values(i18nFields)
219
204
  .map(f => `coalesce(v."${f.column}", b."${f.column}") as "${f.column}"`)
220
205
  .join(', ');
221
206
  // Build the SQL query template
222
207
  const sqlQuery = `SELECT v."${langCodeColumn}" AS "lang_code", ${coalescedCols}
223
- FROM "${schemaName}"."${baseTable}" b
224
- LEFT JOIN "${schemaName}"."${translationTable}" v
208
+ FROM ${baseTableSql} b
209
+ LEFT JOIN ${translationTableSql} v
225
210
  ON v."${fkColumn}" = b."${pkColumn}"
226
211
  AND array_position($2::text[], v."${langCodeColumn}") IS NOT NULL
227
212
  WHERE b."${pkColumn}" = $1::${pkType}