@sqldoc/ns-deprecated 0.0.2 → 0.0.4
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/package.json +10 -9
- package/src/index.ts +61 -53
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@sqldoc/ns-deprecated",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"description": "Deprecation namespace for sqldoc -- marks SQL objects as deprecated with COMMENT ON statements",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
@@ -17,15 +17,16 @@
|
|
|
17
17
|
"!src/__tests__",
|
|
18
18
|
"package.json"
|
|
19
19
|
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "node --test 'src/__tests__/**/*.test.ts'",
|
|
22
|
+
"typecheck": "tsgo -noEmit"
|
|
23
|
+
},
|
|
20
24
|
"peerDependencies": {
|
|
21
|
-
"@sqldoc/core": "0.0.
|
|
25
|
+
"@sqldoc/core": "0.0.3"
|
|
22
26
|
},
|
|
23
27
|
"devDependencies": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"@sqldoc/
|
|
27
|
-
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"test": "vitest run"
|
|
28
|
+
"@sqldoc/core": "0.0.3",
|
|
29
|
+
"@typescript/native-preview": "latest",
|
|
30
|
+
"@sqldoc/test-utils": "0.0.1"
|
|
30
31
|
}
|
|
31
|
-
}
|
|
32
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1,5 @@
|
|
|
1
1
|
import type { NamespacePlugin, TagContext, TagOutput } from '@sqldoc/core'
|
|
2
|
-
|
|
3
|
-
function commentOnSql(target: string, qualifiedName: string, message: string): string {
|
|
4
|
-
return `COMMENT ON ${target} ${qualifiedName} IS '${message}';`
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function targetKeyword(target: string): string {
|
|
8
|
-
switch (target) {
|
|
9
|
-
case 'table':
|
|
10
|
-
return 'TABLE'
|
|
11
|
-
case 'column':
|
|
12
|
-
return 'COLUMN'
|
|
13
|
-
case 'view':
|
|
14
|
-
return 'VIEW'
|
|
15
|
-
case 'function':
|
|
16
|
-
return 'FUNCTION'
|
|
17
|
-
case 'type':
|
|
18
|
-
return 'TYPE'
|
|
19
|
-
default:
|
|
20
|
-
return target.toUpperCase()
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function qualifiedName(ctx: TagContext): string {
|
|
25
|
-
if (ctx.target === 'column') {
|
|
26
|
-
return `"${ctx.objectName}"."${ctx.columnName}"`
|
|
27
|
-
}
|
|
28
|
-
return `"${ctx.objectName}"`
|
|
29
|
-
}
|
|
2
|
+
import { escapeString, quoteIdentifier } from '@sqldoc/core'
|
|
30
3
|
|
|
31
4
|
const plugin: NamespacePlugin = {
|
|
32
5
|
apiVersion: 1,
|
|
@@ -49,45 +22,80 @@ const plugin: NamespacePlugin = {
|
|
|
49
22
|
},
|
|
50
23
|
|
|
51
24
|
onTag(ctx: TagContext): TagOutput | undefined {
|
|
52
|
-
const { tag } = ctx
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
25
|
+
const { tag, target, objectName, columnName, dialect } = ctx
|
|
26
|
+
const q = (name: string) => quoteIdentifier(name, dialect)
|
|
27
|
+
const esc = (s: string) => escapeString(s, dialect)
|
|
28
|
+
|
|
29
|
+
const docTarget = target === 'column' ? { object: objectName, column: columnName } : { object: objectName }
|
|
57
30
|
|
|
31
|
+
// Determine the deprecation message and docs value
|
|
32
|
+
let message: string
|
|
33
|
+
let docsValue: string
|
|
58
34
|
switch (tag.name) {
|
|
59
35
|
case '$self':
|
|
60
|
-
case null:
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
columns: [{ header: 'Status', ...docTarget, value: 'Deprecated' }],
|
|
65
|
-
},
|
|
66
|
-
}
|
|
67
|
-
}
|
|
36
|
+
case null:
|
|
37
|
+
message = 'DEPRECATED'
|
|
38
|
+
docsValue = 'Deprecated'
|
|
39
|
+
break
|
|
68
40
|
case 'replace': {
|
|
69
41
|
const args = tag.args as unknown[]
|
|
70
42
|
const newName = args[0] as string
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
columns: [{ header: 'Status', ...docTarget, value: `Deprecated → ${newName}` }],
|
|
75
|
-
},
|
|
76
|
-
}
|
|
43
|
+
message = `DEPRECATED: use ${newName} instead`
|
|
44
|
+
docsValue = `Deprecated \u2192 ${newName}`
|
|
45
|
+
break
|
|
77
46
|
}
|
|
78
47
|
case 'remove': {
|
|
79
48
|
const args = tag.args as unknown[]
|
|
80
49
|
const date = args[0] as string
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
columns: [{ header: 'Status', ...docTarget, value: `Remove after ${date}` }],
|
|
85
|
-
},
|
|
86
|
-
}
|
|
50
|
+
message = `DEPRECATED: scheduled for removal after ${date}`
|
|
51
|
+
docsValue = `Remove after ${date}`
|
|
52
|
+
break
|
|
87
53
|
}
|
|
88
54
|
default:
|
|
89
55
|
return undefined
|
|
90
56
|
}
|
|
57
|
+
|
|
58
|
+
const docs = {
|
|
59
|
+
columns: [{ header: 'Status', ...docTarget, value: docsValue }],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Generate dialect-specific SQL
|
|
63
|
+
switch (dialect) {
|
|
64
|
+
case 'postgres': {
|
|
65
|
+
const keyword = target.toUpperCase()
|
|
66
|
+
const name = target === 'column' ? `${q(objectName)}.${q(columnName!)}` : q(objectName)
|
|
67
|
+
return {
|
|
68
|
+
sql: [{ sql: `COMMENT ON ${keyword} ${name} IS ${esc(message)};` }],
|
|
69
|
+
docs,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
case 'mysql': {
|
|
74
|
+
if (target === 'table') {
|
|
75
|
+
return {
|
|
76
|
+
sql: [{ sql: `ALTER TABLE ${q(objectName)} COMMENT = ${esc(message)};` }],
|
|
77
|
+
docs,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (target === 'column') {
|
|
81
|
+
const colType = ctx.columnType ?? 'TEXT'
|
|
82
|
+
return {
|
|
83
|
+
sql: [
|
|
84
|
+
{
|
|
85
|
+
sql: `ALTER TABLE ${q(objectName)} MODIFY COLUMN ${q(columnName!)} ${colType} COMMENT ${esc(message)};`,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
docs,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// MySQL cannot comment on views, functions, or types -- docs-only output
|
|
92
|
+
return { sql: [], docs }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
case 'sqlite':
|
|
96
|
+
// SQLite has no comment support -- docs-only output
|
|
97
|
+
return { sql: [], docs }
|
|
98
|
+
}
|
|
91
99
|
},
|
|
92
100
|
}
|
|
93
101
|
|