-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
identifierNaming.js
32 lines (29 loc) · 1.2 KB
/
identifierNaming.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// This example rule will enforce primary key columns be named like this: tablename_id
/** @type {import('schemalint').Rule} */
const identifierNaming = {
name: 'identifier-naming',
docs: {
description: 'Identifier columns should follow "tablename_id" convention',
url: '...',
},
process({ schemaObject, report }) {
const validator = ({ tags, columns, name: tableName }) => {
const idColumns = columns.filter((c) => c.isPrimaryKey);
// There might be 2 or more, or there might be 0. In both cases, skip this table.
if (idColumns.length === 1) {
const [idColumn] = idColumns;
const expectedName = `${tableName}_id`;
if (idColumn.name !== expectedName) {
report({
rule: this.name,
identifier: `${schemaObject.name}.${tableName}`,
message: `The primary column on ${tableName} is called ${idColumn.name} which doesn't follow the convention. Expected name was: ${expectedName}`,
suggestedMigration: `ALTER TABLE "${tableName}" RENAME COLUMN "${idColumn.name}" TO "${expectedName}";`,
});
}
}
};
schemaObject.tables.forEach(validator);
},
};
module.exports = identifierNaming;