Skip to content

Commit

Permalink
add the schema structure
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 16, 2024
1 parent 8993ee3 commit fae614b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/connections/sqlite/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Database, Table, TableColumn } from 'src/models/database';
import { Connection, SqlConnection } from '..';
import { SqlConnection } from '..';
import { AbstractDialect } from 'src/query-builder';
import { SqliteDialect } from 'src/query-builder/dialects/sqlite-dialect';
export abstract class SqliteBaseConnection extends SqlConnection {
dialect: AbstractDialect = new SqliteDialect();

public async fetchDatabaseSchema(): Promise<Database> {
const { data: tableList } = await this.query<{
type: string;
Expand Down
5 changes: 5 additions & 0 deletions src/query-builder/dialects/sqlite-dialect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AbstractDialect } from '..';

export class SqliteDialect extends AbstractDialect {
protected AUTO_INCREMENT_KEYWORD = 'AUTOINCREMENT';
}
38 changes: 37 additions & 1 deletion src/query-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Dialect {
select(builder: QueryBuilderInternal): Query;
insert(builder: QueryBuilderInternal): Query;
update(builder: QueryBuilderInternal): Query;
// delete(builder: QueryBuilderInternal): Query;
delete(builder: QueryBuilderInternal): Query;
createTable(builder: QueryBuilderInternal): Query;
dropTable(builder: QueryBuilderInternal): Query;
renameColumn(builder: QueryBuilderInternal): Query;
Expand All @@ -32,6 +32,10 @@ export enum ColumnDataType {
}

export abstract class AbstractDialect implements Dialect {
// Various flags to help customize the dialect
protected AUTO_INCREMENT_KEYWORD = 'AUTO_INCREMENT';
protected SUPPORT_COLUMN_COMMENT = true;

escapeId(identifier: string): string {
return identifier
.split('.')
Expand Down Expand Up @@ -189,12 +193,44 @@ export abstract class AbstractDialect implements Dialect {
];
}

// This is generic implementation of column definition,
protected buildColumnDefinition(def: TableColumnDefinition): string {
const referencePart = def.references
? [
`REFERENCES ${this.escapeId(def.references.table)}(${def.references.column.map((c) => this.escapeId(c)).join(', ')})`,
def.references.match ? `MATCH ${def.references.match}` : '',
def.references.onDelete
? `ON DELETE ${def.references.onDelete}`
: '',
def.references.onUpdate
? `ON UPDATE ${def.references.onUpdate}`
: '',
]
.filter(Boolean)
.join(' ')
: '';

return [
def.type,
def.nullable === false ? 'NOT NULL' : '',
def.invisible ? 'INVISIBLE' : '', // This is for MySQL case
def.primaryKey ? 'PRIMARY KEY' : '',
def.unique ? 'UNIQUE' : '',
def.default ? `DEFAULT ${def.default}` : '',
def.defaultExpression ? `DEFAULT (${def.defaultExpression})` : '',
def.autoIncrement ? this.AUTO_INCREMENT_KEYWORD : '',
def.collate ? `COLLATE ${def.collate}` : '',
def.generatedExpression
? `GENERATED ALWAYS AS (${def.generatedExpression})`
: '',
def.generatedExpression && def.generatedType
? def.generatedType
: '',
def.checkExpression ? `CHECK (${def.checkExpression})` : '',
referencePart,
def.comment && this.SUPPORT_COLUMN_COMMENT
? `COMMENT '${def.comment}'`
: '',
]
.filter(Boolean)
.join(' ');
Expand Down
14 changes: 8 additions & 6 deletions tests/connections/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ describe('Database Connection', () => {
test('Create table', async () => {
// Create testing table
await db.createTable(DEFAULT_SCHEMA, 'persons', [
{ name: 'id', type: 'INTEGER' },
{ name: 'id', definition: { type: 'INTEGER' } },
{
name: 'name',
type:
process.env.CONNECTION_TYPE === 'bigquery'
? 'STRING'
: 'VARCHAR(255)',
definition: {
type:
process.env.CONNECTION_TYPE === 'bigquery'
? 'STRING'
: 'VARCHAR(255)',
},
},
{ name: 'age', type: 'INTEGER' },
{ name: 'age', definition: { type: 'INTEGER' } },
]);
});

Expand Down

0 comments on commit fae614b

Please sign in to comment.