-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebe2c35
commit 6eafa6b
Showing
11 changed files
with
225 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Deno: create.test.ts" type="DenoConfigurationType" nameIsGenerated="true" inputPath="$PROJECT_DIR$/test/unit/schema/create.test.ts" programParameters="test -A" workingDirectory="$PROJECT_DIR$/test/unit/schema"> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
type ColumnDefinitionNative = { | ||
name: string; | ||
data_type: string; | ||
not_null?: boolean; | ||
default?: unknown; | ||
unique?: boolean; | ||
foreign_key?: { | ||
table: string; | ||
column: string; | ||
on_delete?: "NO ACTION" | "CASCADE" | "SET NULL" | "SET DEFAULT"; | ||
}; | ||
}; | ||
|
||
export type { ColumnDefinitionNative }; | ||
|
||
export class AlterQuery { | ||
readonly #tableName: string; | ||
|
||
#addColumns: ColumnDefinitionNative[] = []; | ||
|
||
#inherits?: string; | ||
|
||
constructor(nameWithSchema: string) { | ||
this.#tableName = nameWithSchema; | ||
} | ||
|
||
addColumn(column: ColumnDefinitionNative): AlterQuery { | ||
column = { not_null: false, unique: false, ...column }; | ||
this.#addColumns.push(column); | ||
return this; | ||
} | ||
|
||
|
||
buildQuery(): string { | ||
let query = `ALTER TABLE ${this.#tableName} \n\t`; | ||
query += this.#prepareColumns(); | ||
return query; | ||
} | ||
|
||
#prepareColumn(column: ColumnDefinitionNative): string { | ||
let query = `ADD COLUMN "${column.name}" ${column.data_type}`; | ||
if (column.foreign_key) { | ||
const onDelete = column.foreign_key.on_delete | ||
? ` ON DELETE ${column.foreign_key.on_delete}` | ||
: ""; | ||
query += ` REFERENCES ${column.foreign_key.table} ("${column.foreign_key.column}") ${onDelete}`; | ||
} else { | ||
if (column.not_null) query += ` NOT NULL`; | ||
if (column.unique) query += ` UNIQUE`; | ||
} | ||
return query; | ||
} | ||
|
||
#prepareColumns(): string { | ||
return this.#addColumns | ||
.map((column) => { | ||
return this.#prepareColumn(column); | ||
}) | ||
.join("\n\t"); | ||
} | ||
|
||
#prepareInherits(): string { | ||
if (!this.#inherits) return ""; | ||
return ` INHERITS (${this.#inherits})`; | ||
} | ||
|
||
#preparePrimaryKey(): string { | ||
if (this.#inherits) return ""; | ||
return `, PRIMARY KEY (id)`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.