@secondlayer/subgraphs 3.24.0 → 3.24.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/src/index.js CHANGED
@@ -226,6 +226,11 @@ import { createHash } from "node:crypto";
226
226
 
227
227
  // src/schema/utils.ts
228
228
  import { pgSchemaName } from "@secondlayer/shared/db/queries/subgraphs";
229
+ function quotePgIdent(name) {
230
+ if (/^[a-z_][a-z0-9_]*$/i.test(name))
231
+ return name;
232
+ return `"${name.replace(/"/g, '""')}"`;
233
+ }
229
234
 
230
235
  // src/schema/generator.ts
231
236
  var TYPE_MAP = {
@@ -250,7 +255,7 @@ function tableNeedsTrgm(tableDef) {
250
255
  return Object.values(tableDef.columns).some((col) => col.search);
251
256
  }
252
257
  function emitTableDDL(schemaName, tableName, tableDef) {
253
- const qualifiedName = `${schemaName}.${tableName}`;
258
+ const qualifiedName = `${quotePgIdent(schemaName)}.${quotePgIdent(tableName)}`;
254
259
  const statements = [];
255
260
  const columnDefs = [
256
261
  "_id BIGSERIAL PRIMARY KEY",
@@ -261,12 +266,12 @@ function emitTableDDL(schemaName, tableName, tableDef) {
261
266
  for (const [colName, col] of Object.entries(tableDef.columns)) {
262
267
  const sqlType = TYPE_MAP[col.type];
263
268
  const nullable = col.nullable ? "" : " NOT NULL";
264
- let colDef = `${colName} ${sqlType}${nullable}`;
269
+ let colDef = `${quotePgIdent(colName)} ${sqlType}${nullable}`;
265
270
  if (col.default !== undefined) {
266
271
  colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
267
272
  }
268
273
  if (col.type === "uint") {
269
- colDef += ` CHECK (${colName} >= 0)`;
274
+ colDef += ` CHECK (${quotePgIdent(colName)} >= 0)`;
270
275
  }
271
276
  columnDefs.push(colDef);
272
277
  }
@@ -274,30 +279,30 @@ function emitTableDDL(schemaName, tableName, tableDef) {
274
279
  ${columnDefs.join(`,
275
280
  `)}
276
281
  )`);
277
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_block_height ON ${qualifiedName} (_block_height)`);
278
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_tx_id ON ${qualifiedName} (_tx_id)`);
282
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_block_height`)} ON ${qualifiedName} (_block_height)`);
283
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_tx_id`)} ON ${qualifiedName} (_tx_id)`);
279
284
  for (const [colName, col] of Object.entries(tableDef.columns)) {
280
285
  if (col.indexed) {
281
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName} ON ${qualifiedName} (${colName})`);
286
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_${colName}`)} ON ${qualifiedName} (${quotePgIdent(colName)})`);
282
287
  }
283
288
  }
284
289
  for (const [colName, col] of Object.entries(tableDef.columns)) {
285
290
  if (col.search) {
286
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName}_trgm ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`);
291
+ statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_${colName}_trgm`)} ON ${qualifiedName} USING gin (${quotePgIdent(colName)} gin_trgm_ops)`);
287
292
  }
288
293
  }
289
294
  if (tableDef.indexes) {
290
295
  for (let i = 0;i < tableDef.indexes.length; i++) {
291
296
  const cols = tableDef.indexes[i];
292
- const idxName = `idx_${schemaName}_${tableName}_composite_${i}`;
293
- statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.join(", ")})`);
297
+ const idxName = quotePgIdent(`idx_${schemaName}_${tableName}_composite_${i}`);
298
+ statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.map(quotePgIdent).join(", ")})`);
294
299
  }
295
300
  }
296
301
  if (tableDef.uniqueKeys) {
297
302
  for (let i = 0;i < tableDef.uniqueKeys.length; i++) {
298
303
  const cols = tableDef.uniqueKeys[i];
299
- const constraintName = `uq_${schemaName}_${tableName}_${cols.join("_")}`;
300
- statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.join(", ")})`);
304
+ const constraintName = quotePgIdent(`uq_${schemaName}_${tableName}_${cols.join("_")}`);
305
+ statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.map(quotePgIdent).join(", ")})`);
301
306
  }
302
307
  }
303
308
  return statements;
@@ -329,8 +334,8 @@ function emitFactoryDDL(schemaName) {
329
334
  }
330
335
  function emitForeignKeyDDL(schemaName, tableName, tableDef) {
331
336
  return (tableDef.relations ?? []).map((rel) => {
332
- const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
333
- return `ALTER TABLE ${schemaName}.${tableName} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.join(", ")}) ` + `REFERENCES ${schemaName}.${rel.references} (${rel.referencedColumns.join(", ")})`;
337
+ const constraintName = quotePgIdent(`fk_${schemaName}_${tableName}_${rel.name}`);
338
+ return `ALTER TABLE ${quotePgIdent(schemaName)}.${quotePgIdent(tableName)} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.map(quotePgIdent).join(", ")}) ` + `REFERENCES ${quotePgIdent(schemaName)}.${quotePgIdent(rel.references)} (${rel.referencedColumns.map(quotePgIdent).join(", ")})`;
334
339
  });
335
340
  }
336
341
  function generateSubgraphSQL(def, schemaNameOverride) {
@@ -4736,5 +4741,5 @@ export {
4736
4741
  ByoBreakingChangeError
4737
4742
  };
4738
4743
 
4739
- //# debugId=0DAD1DFA856B057164756E2164756E21
4744
+ //# debugId=CE8974B47137FD3C64756E2164756E21
4740
4745
  //# sourceMappingURL=index.js.map