diff --git a/src/index.ts b/src/index.ts index c328c4e..5f1859f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,9 @@ export * from './core/Column.ts' +export * from './core/Constraint.ts' export * from './core/Database.ts' export * from './core/Driver.ts' export * from './core/Expr.ts' +export * from './core/Index.ts' export * from './core/Query.ts' export * from './core/Selection.ts' export * from './core/Sql.ts' diff --git a/src/sqlite/SqliteDialect.ts b/src/sqlite/SqliteDialect.ts index ec1463e..6891efd 100644 --- a/src/sqlite/SqliteDialect.ts +++ b/src/sqlite/SqliteDialect.ts @@ -45,7 +45,7 @@ class SqliteEmitter extends Emitter { ) } emitIdColumn() { - this.sql += 'integer primary key autoincrement' + this.sql += 'integer' } } diff --git a/src/universal/UniversalColumns.ts b/src/universal/UniversalColumns.ts index 50fb87d..d643978 100644 --- a/src/universal/UniversalColumns.ts +++ b/src/universal/UniversalColumns.ts @@ -4,7 +4,8 @@ import {sql} from '../core/Sql.ts' export function id(name?: string): Column { return new Column({ name, - type: sql.chunk('emitIdColumn', undefined) + type: sql.chunk('emitIdColumn', undefined), + primary: true }) } diff --git a/test/TestDriver.ts b/test/TestDriver.ts index 5e40b48..de971a5 100644 --- a/test/TestDriver.ts +++ b/test/TestDriver.ts @@ -5,7 +5,7 @@ import { type SyncDatabase } from '../src/core/Database.ts' import {table} from '../src/core/Table.ts' -import {eq, sql} from '../src/index.ts' +import {eq, foreignKey, primaryKey, sql, unique} from '../src/index.ts' import {boolean, id, int, json, text} from '../src/universal.ts' const Node = table('Node', { @@ -25,6 +25,27 @@ const Post = table('Post', { title: text().notNull() }) +const TableA = table('TableA', { + id: id() +}) + +const TableB = table( + 'TableB', + { + isUnique: int().unique(), + hasRef: int().references(TableA.id), + colA: int(), + colB: int().unique() + }, + TableB => { + return { + uniqueA: unique().on(TableB.colA), + multiPk: primaryKey(TableB.colA, TableB.colB), + multiRef: foreignKey(TableB.colA).references(TableA.id) + } + } +) + export async function testDriver( name: string, createDb: () => Promise @@ -198,5 +219,29 @@ export async function testDriver( } } }) + + Test.it('constraints and indexes', async () => { + //try { + await db.createTable(TableA) + await db.createTable(TableB) + await db.insert(TableA).values({}) + await db.insert(TableB).values({ + isUnique: 1, + hasRef: 1, + colA: 1, + colB: 1 + }) + const [row] = await db.select().from(TableB) + Assert.isEqual(row, { + isUnique: 1, + hasRef: 1, + colA: 1, + colB: 1 + }) + /*} finally { + await db.dropTable(TableB) + await db.dropTable(TableA) + }*/ + }) }) }