11import { Table } from "../src/schema" ;
2- import { integer , serial , timestamp , uuid , varchar } from "../src/types/column-type" ;
2+ import { integer , serial , text , timestamp , uuid , varchar } from "../src/types/column-type" ;
33
44// Define test interfaces
55interface User {
@@ -70,8 +70,14 @@ describe("Schema and Table", () => {
7070 table . column ( "id" , uuid ( ) ) . primaryKey ( ) . $defaultUUID ( ) ;
7171 table . column ( "createdAt" , timestamp ( ) ) . default ( "now()" ) ;
7272
73+ // CREATE TABLE IF NOT EXISTS users (
74+ // "id" UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
75+ // "createdAt" TIMESTAMP DEFAULT 'now()'
76+ // );
77+
7378 const sql = table . createTableSql ( ) ;
74- expect ( sql ) . toContain ( '"createdAt" TIMESTAMP DEFAULT now()' ) ;
79+ expect ( sql ) . toContain ( `"id" UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid()` ) ;
80+ expect ( sql ) . toContain ( `"createdAt" TIMESTAMP DEFAULT 'now()'` ) ;
7581 } ) ;
7682
7783 it ( "should create a table with foreign key references" , ( ) => {
@@ -82,65 +88,65 @@ describe("Schema and Table", () => {
8288 const sql = table . createTableSql ( ) ;
8389 expect ( sql ) . toContain ( '"authorId" UUID REFERENCES users(id)' ) ;
8490 } ) ;
85- //
86- // it("should create a table with all constraints combined", () => {
87- // const table = new Table<User>("users");
88- // table.column("id", "UUID") .primaryKey();
89- // table.column("name", "VARCHAR(255)" ).notNull();
90- // table.column("email", "VARCHAR(255)" ).unique().notNull();
91- // table.column("age", "INTEGER" );
92- // table
93- // .column("createdAt", "TIMESTAMP" )
94- // .default("CURRENT_TIMESTAMP")
95- // .notNull();
96- //
97- // const sql = table.toString();
98- // expect(sql).toContain(' "id" UUID PRIMARY KEY');
99- // expect(sql).toContain(' "name" VARCHAR(255) NOT NULL');
100- // expect(sql).toContain(' "email" VARCHAR(255) UNIQUE NOT NULL');
101- // expect(sql).toContain(' "age" INTEGER');
102- // expect(sql).toContain(
103- // '"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL'
104- // );
105- // } );
106- //
107- // it("should handle complex table relationships", () => {
108- // const usersTable = new Table<User>("users" );
109- // usersTable.column("id", "UUID").primaryKey( );
110- // usersTable.column("name", "VARCHAR(255)").notNull( );
111- //
112- // const postsTable = new Table<Post>("posts");
113- // postsTable.column("id", "UUID").primaryKey();
114- // postsTable.column("title", "VARCHAR(255)").notNull( );
115- // postsTable .column("content ", "TEXT" );
116- // postsTable .column("authorId ", "UUID") .notNull().references("users", "id" );
117- // postsTable
118- // .column("createdAt", "TIMESTAMP")
119- // .default("CURRENT_TIMESTAMP")
120- // .notNull();
121- //
122- // const postsSql = postsTable.toString();
123- // expect(postsSql).toContain(
124- // '"authorId" UUID NOT NULL REFERENCES users(id)'
125- // );
126- // expect(postsSql).toContain(
127- // '"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL'
128- // );
129- // });
130- //
131- // it("should handle different default value types", () => {
132- // const table = new Table<any>("test" );
133- // table.column("id", "UUID").primaryKey();
134- // table.column("stringDefault", "VARCHAR(255)").default("default value");
135- // table.column("numberDefault", "INTEGER").default(42);
136- // table.column("nullDefault", " VARCHAR(255)").default(null);
137- //
138- // const sql = table.toString();
139- // expect(sql).toContain(
140- // "\"stringDefault\" VARCHAR(255) DEFAULT 'default value'"
141- // );
142- // expect(sql ).toContain('"numberDefault" INTEGER DEFAULT 42' );
143- // expect(sql ).toContain('"nullDefault" VARCHAR(255) DEFAULT NULL' );
144- // });
91+
92+ it ( "should create a table with all constraints combined" , ( ) => {
93+ const table = new Table < User > ( "users" ) ;
94+ table . column ( "id" , uuid ( ) ) . primaryKey ( ) . $defaultUUID ( ) ;
95+ table . column ( "name" , varchar ( ) ) . notNull ( ) ;
96+ table . column ( "email" , varchar ( ) ) . unique ( ) . notNull ( ) ;
97+ table . column ( "age" , integer ( ) ) ;
98+ table
99+ . column ( "createdAt" , timestamp ( ) )
100+ . default ( "CURRENT_TIMESTAMP" )
101+ . notNull ( ) ;
102+
103+ // CREATE TABLE IF NOT EXISTS users (
104+ // "id" UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
105+ // "name" VARCHAR(255) NOT NULL,
106+ // "email" VARCHAR(255) NOT NULL UNIQUE,
107+ // "age" INTEGER,
108+ // "createdAt" TIMESTAMP NOT NULL DEFAULT 'CURRENT_TIMESTAMP'
109+ // );
110+ const sql = table . createTableSql ( ) ;
111+ expect ( sql ) . toContain ( `"id" UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid()` ) ;
112+ expect ( sql ) . toContain ( `"name" VARCHAR(255) NOT NULL` ) ;
113+ expect ( sql ) . toContain ( `"email" VARCHAR(255) NOT NULL UNIQUE` ) ;
114+ expect ( sql ) . toContain ( `"age" INTEGER` ) ;
115+ expect ( sql ) . toContain ( `"createdAt" TIMESTAMP NOT NULL DEFAULT 'CURRENT_TIMESTAMP'` ) ;
116+ } ) ;
117+
118+
119+ it ( "should handle complex table relationships" , ( ) => {
120+ const usersTable = new Table < User > ( "users" ) ;
121+ usersTable . column ( "id " , uuid ( ) ) . primaryKey ( ) ;
122+ usersTable . column ( "name " , varchar ( ) ) . notNull ( ) ;
123+
124+ const postsTable = new Table < Post > ( "posts" ) ;
125+ postsTable . column ( "id" , uuid ( ) ) . primaryKey ( ) ;
126+ postsTable . column ( "title" , varchar ( ) ) . notNull ( ) ;
127+ postsTable . column ( "content" , text ( ) ) ;
128+ postsTable . column ( "authorId" , uuid ( ) ) . notNull ( ) . references ( {
129+ table : "users" ,
130+ column : "id" ,
131+ onDelete : 'CASCADE'
132+ } ) ;
133+ postsTable
134+ . column ( "createdAt" , timestamp ( ) )
135+ . $defaultNOW ( )
136+ . notNull ( ) ;
137+
138+ const postsSql = postsTable . createTableSql ( ) ;
139+
140+ // CREATE TABLE IF NOT EXISTS posts (
141+ // "id" UUID PRIMARY KEY NOT NULL,
142+ // "title" VARCHAR(255) NOT NULL,
143+ // "content" TEXT,
144+ // "authorId" UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
145+ // "createdAt" TIMESTAMP NOT NULL DEFAULT 'now()'
146+ // );
147+
148+ expect ( postsSql ) . toContain ( `"authorId" UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE` ) ;
149+ expect ( postsSql ) . toContain ( `"createdAt" TIMESTAMP NOT NULL DEFAULT 'now()'` ) ;
150+ } ) ;
145151 } ) ;
146152} ) ;
0 commit comments