MCPcopy Create free account
hub / github.com/subquery/subql / generateCreateTableQuery

Function generateCreateTableQuery

packages/node-core/src/db/sync-helper.ts:493–528  ·  view source on GitHub ↗
(
  model: ModelStatic<Model<any, any>>,
  schema: string,
  withoutForeignKey: boolean
)

Source from the content-addressed store, hash-verified

491}
492
493export function generateCreateTableQuery(
494 model: ModelStatic<Model<any, any>>,
495 schema: string,
496 withoutForeignKey: boolean
497): QueryString[] {
498 const tableName = model.tableName;
499
500 const attributes = model.getAttributes();
501 const columnDefinitions: string[] = [];
502 const primaryKeyColumns: string[] = [];
503 const comments: Query[] = [];
504
505 Object.keys(attributes).forEach((key) => {
506 const attr = attributes[key];
507
508 assert(attr.field, 'Expected field to be set on attribute');
509
510 const columnDefinition = `"${attr.field}" ${formatAttributes(attr, schema, withoutForeignKey)}`;
511
512 columnDefinitions.push(columnDefinition);
513 if (attr.comment) {
514 comments.push(commentColumnQuery(schema, tableName, attr.field, attr.comment));
515 }
516 if (attr.primaryKey) {
517 primaryKeyColumns.push(`"${attr.field}"`);
518 }
519 });
520
521 const primaryKeyDefinition = `, PRIMARY KEY (${primaryKeyColumns.join(', ')})`;
522
523 const tableQuery = `CREATE TABLE IF NOT EXISTS "${schema}"."${tableName}" (${columnDefinitions.join(
524 ',\n '
525 )}${primaryKeyDefinition});`;
526
527 return [tableQuery, ...comments];
528}
529
530export function generateCreateIndexQuery(
531 indexes: readonly ModelIndexesOptions[],

Callers 1

Calls 2

formatAttributesFunction · 0.90
commentColumnQueryFunction · 0.85

Tested by

no test coverage detected