diff --git a/lib/index.d.ts b/lib/index.d.ts index 044d2ff..669a025 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -14,7 +14,9 @@ type RemoveFirstParam = T extends (first: any, ...rest: infer P) => infer R ? export interface Relation { type: "REL"; table: keyof Tables; + default?: string; nullable?: boolean; + unique?: boolean; } export interface Column { type: DataType; diff --git a/lib/index.js b/lib/index.js index 5ef442a..f547df5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,25 +5,20 @@ export class BunORM { tables; db; createTable = (table, opts) => (typeof this.db.run(`CREATE TABLE IF NOT EXISTS ${table} ('id' INTEGER PRIMARY KEY AUTOINCREMENT,${Object.entries(opts.columns) - .filter(([_, opts]) => opts.type !== "REL") .map(([col, opts]) => [ `'${col}'`, - opts.type === "JSON" ? "TEXT" : opts.type, + opts.type === "JSON" + ? "TEXT" + : opts.type === "REL" + ? "INTEGER" + : opts.type, opts.default && `DEFAULT ${opts.default}`, opts.unique && "UNIQUE", !opts.nullable && "NOT NULL", ] .filter((x) => !!x) .join(" ")) - .join()}${Object.entries(opts.columns).filter(([_, opts]) => opts.type === "REL").length - ? "," + - Object.entries(opts.columns) - .filter(([_, opts]) => opts.type === "REL") - .map(([col, opts]) => [`'${col}' INTEGER`, !opts.nullable && "NOT NULL"] - .filter((x) => !!x) - .join(" ")) - .join() - : ""},'updatedAt' DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,'createdAt' DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL${Object.entries(opts.columns).filter(([_, opts]) => opts.type === "REL").length + .join()},'updatedAt' DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,'createdAt' DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL${Object.entries(opts.columns).filter(([_, opts]) => opts.type === "REL").length ? "," + Object.entries(opts.columns) .filter(([_, opts]) => opts.type === "REL")