forj 0.1.3 → 0.1.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 +1 -1
- package/src/migrations/schema.ts +46 -31
package/package.json
CHANGED
package/src/migrations/schema.ts
CHANGED
|
@@ -31,7 +31,7 @@ export class Schema {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
// mudei tudo this.#executeSql -> this.#addStatement
|
|
34
|
-
// static
|
|
34
|
+
// static #executeSql(...sql: string[] | string[][]) {
|
|
35
35
|
// sql = sql.flat(Infinity) as string[]
|
|
36
36
|
// // if (this.#c) {
|
|
37
37
|
// // for (const statement of sql) {
|
|
@@ -41,50 +41,65 @@ export class Schema {
|
|
|
41
41
|
// this.#addStatement(...sql)
|
|
42
42
|
// }
|
|
43
43
|
|
|
44
|
-
static
|
|
44
|
+
static #blueprint(table: string, ...fns: BlueprintFn[]) {
|
|
45
45
|
const blueprint = new Blueprint(table)
|
|
46
|
-
fn(blueprint)
|
|
47
|
-
|
|
46
|
+
fns.flatMap(fn => fn && fn(blueprint))
|
|
47
|
+
return blueprint
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
static
|
|
50
|
+
static create(table: string, fn: BlueprintFn, exist: boolean = false) {
|
|
51
|
+
this.#addStatement(Builder.create(this.#blueprint(table, fn), exist))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static createIfNotExists(table: string, fn: BlueprintFn) {
|
|
51
55
|
this.create(table, fn, true)
|
|
52
56
|
}
|
|
53
57
|
|
|
54
|
-
static
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
static createPivot(table: string, fn: BlueprintFn): void
|
|
59
|
+
static createPivot(table: string, columns: string[], fn: BlueprintFn): void
|
|
60
|
+
static createPivot(table: string, columns: string[] | BlueprintFn, fn?: BlueprintFn) {
|
|
61
|
+
const hasColumn = Array.isArray(columns)
|
|
62
|
+
columns = hasColumn ? columns as string[] : [] // @ts-ignore
|
|
63
|
+
fn = hasColumn ? fn : columns
|
|
64
|
+
|
|
65
|
+
this.#addStatement(Builder.create(this.#blueprint(table, fn as BlueprintFn, (table: Blueprint) => {
|
|
66
|
+
columns.forEach(column => table.foreignId(column))
|
|
67
|
+
table.primary(table.columns.map(c => c.name))
|
|
68
|
+
})).slice(0, -1) + ' WITHOUT ROWID;')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static table(table: string, fn: BlueprintFn) {
|
|
72
|
+
this.#addStatement(Builder.alter(this.#blueprint(table, fn)))
|
|
58
73
|
}
|
|
59
74
|
|
|
60
|
-
static
|
|
61
|
-
|
|
75
|
+
static drop(table: string) {
|
|
76
|
+
this.#addStatement(Builder.drop(table))
|
|
62
77
|
}
|
|
63
78
|
|
|
64
|
-
static
|
|
65
|
-
|
|
79
|
+
static dropIfExists(table: string) {
|
|
80
|
+
this.#addStatement(Builder.dropIfExists(table))
|
|
66
81
|
}
|
|
67
82
|
|
|
68
|
-
static
|
|
69
|
-
|
|
83
|
+
static rename(from: string, to: string) {
|
|
84
|
+
this.#addStatement(Builder.rename(from, to))
|
|
70
85
|
}
|
|
71
86
|
|
|
72
|
-
static
|
|
73
|
-
|
|
87
|
+
static dropView(view: string) {
|
|
88
|
+
this.#addStatement(Builder.dropView(view))
|
|
74
89
|
}
|
|
75
90
|
|
|
76
|
-
static
|
|
77
|
-
|
|
91
|
+
static dropViewIfExists(view: string) {
|
|
92
|
+
this.#addStatement(Builder.dropViewIfExists(view))
|
|
78
93
|
}
|
|
79
94
|
|
|
80
|
-
static
|
|
95
|
+
static disableForeignKeyConstraints() {
|
|
81
96
|
this.#foreignKeyConstraintsEnabled = false
|
|
82
|
-
|
|
97
|
+
this.#addStatement('PRAGMA foreign_keys = OFF;')
|
|
83
98
|
}
|
|
84
99
|
|
|
85
|
-
static
|
|
100
|
+
static enableForeignKeyConstraints() {
|
|
86
101
|
this.#foreignKeyConstraintsEnabled = true
|
|
87
|
-
|
|
102
|
+
this.#addStatement('PRAGMA foreign_keys = ON;')
|
|
88
103
|
}
|
|
89
104
|
|
|
90
105
|
static isForeignKeyConstraintsEnabled(): boolean {
|
|
@@ -98,13 +113,13 @@ export class Schema {
|
|
|
98
113
|
return this.sql
|
|
99
114
|
}
|
|
100
115
|
|
|
101
|
-
// static
|
|
116
|
+
// static dropAllTables() {
|
|
102
117
|
// const tables = await this.getAllTables()
|
|
103
118
|
// const sql = Builder.dropAllTables(tables)
|
|
104
|
-
//
|
|
119
|
+
// this.#addStatement(sql)
|
|
105
120
|
// }
|
|
106
121
|
|
|
107
|
-
// static
|
|
122
|
+
// static hasTable(table: string): Promise<boolean> {
|
|
108
123
|
// if (!this.#c)
|
|
109
124
|
// throw new Error('Database connection not set')
|
|
110
125
|
|
|
@@ -113,7 +128,7 @@ export class Schema {
|
|
|
113
128
|
// return result.length > 0
|
|
114
129
|
// }
|
|
115
130
|
|
|
116
|
-
// static
|
|
131
|
+
// static hasColumn(table: string, columnName: string): Promise<boolean> {
|
|
117
132
|
// if (!this.#c)
|
|
118
133
|
// throw new Error('Database connection not set')
|
|
119
134
|
|
|
@@ -122,7 +137,7 @@ export class Schema {
|
|
|
122
137
|
// return result.some((row: any) => row.name === columnName)
|
|
123
138
|
// }
|
|
124
139
|
|
|
125
|
-
// static
|
|
140
|
+
// static hasColumns(table: string, ...columnNames: string[]): Promise<boolean> {
|
|
126
141
|
// if (!this.#c)
|
|
127
142
|
// throw new Error('Database connection not set')
|
|
128
143
|
|
|
@@ -133,7 +148,7 @@ export class Schema {
|
|
|
133
148
|
// return columnNames.every(col => existingColumns.includes(col))
|
|
134
149
|
// }
|
|
135
150
|
|
|
136
|
-
// static
|
|
151
|
+
// static getAllTables(): Promise<string[]> {
|
|
137
152
|
// if (!this.#c)
|
|
138
153
|
// throw new Error('Database connection not set')
|
|
139
154
|
|
|
@@ -142,7 +157,7 @@ export class Schema {
|
|
|
142
157
|
// return result.map((row: any) => row.name)
|
|
143
158
|
// }
|
|
144
159
|
|
|
145
|
-
// static
|
|
160
|
+
// static getColumns(table: string): Promise<any[]> {
|
|
146
161
|
// if (!this.#c)
|
|
147
162
|
// throw new Error('Database connection not set')
|
|
148
163
|
|
|
@@ -150,7 +165,7 @@ export class Schema {
|
|
|
150
165
|
// return await this.#c.query(sql)
|
|
151
166
|
// }
|
|
152
167
|
|
|
153
|
-
// static
|
|
168
|
+
// static getColumnType(table: string, columnName: string): Promise<string | null> {
|
|
154
169
|
// if (!this.#c) {
|
|
155
170
|
// throw new Error('Database connection not set')
|
|
156
171
|
// }
|