Add ColumnOf definitions
This commit is contained in:
parent
59de642d31
commit
09d25c926c
4 changed files with 620 additions and 492 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@endeavorance/prequel",
|
||||
"version": "1.2.0",
|
||||
"version": "1.4.0",
|
||||
"exports": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
|
@ -10,7 +10,9 @@
|
|||
"keywords": [],
|
||||
"author": "Endeavorance <hello@endeavorance.camp> (https://endeavorance.camp)",
|
||||
"license": "CC BY-NC-SA 4.0",
|
||||
"files": ["dist"],
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
|
|
115
src/column-types.ts
Normal file
115
src/column-types.ts
Normal file
|
@ -0,0 +1,115 @@
|
|||
import type { ColumnShorthand } from "./columns";
|
||||
import type { Table } from "./table";
|
||||
|
||||
export const ColumnOf = {
|
||||
PrimaryKey: {
|
||||
type: "INTEGER",
|
||||
primary: true,
|
||||
autoincrement: true,
|
||||
},
|
||||
|
||||
Text: {
|
||||
type: "TEXT",
|
||||
nullable: false,
|
||||
},
|
||||
|
||||
Int: {
|
||||
type: "INTEGER",
|
||||
nullable: false,
|
||||
},
|
||||
|
||||
Real: {
|
||||
type: "REAL",
|
||||
nullable: false,
|
||||
},
|
||||
|
||||
ForeignKey(otherTable: Table<unknown>, cascade = false): ColumnShorthand {
|
||||
return {
|
||||
type: otherTable.primaryColumnType(),
|
||||
references: otherTable.reference(),
|
||||
nullable: false,
|
||||
cascade
|
||||
};
|
||||
},
|
||||
|
||||
Nullable: {
|
||||
Text: {
|
||||
type: "TEXT",
|
||||
nullable: true,
|
||||
|
||||
},
|
||||
|
||||
Int: {
|
||||
type: "INTEGER",
|
||||
nullable: true,
|
||||
},
|
||||
|
||||
Real: {
|
||||
type: "REAL",
|
||||
nullable: true,
|
||||
},
|
||||
|
||||
ForeignKey(otherTable: Table<unknown>, cascade = false): ColumnShorthand {
|
||||
return {
|
||||
type: otherTable.primaryColumnType(),
|
||||
references: otherTable.reference(),
|
||||
nullable: true,
|
||||
cascade,
|
||||
};
|
||||
},
|
||||
},
|
||||
|
||||
Unique: {
|
||||
Text: {
|
||||
type: "TEXT",
|
||||
nullable: false,
|
||||
unique: true,
|
||||
},
|
||||
|
||||
Int: {
|
||||
type: "INTEGER",
|
||||
nullable: false,
|
||||
unique: true,
|
||||
},
|
||||
|
||||
Real: {
|
||||
type: "REAL",
|
||||
nullable: false,
|
||||
unique: true,
|
||||
},
|
||||
|
||||
ForeignKey(otherTable: Table<unknown>, cascade = false): ColumnShorthand {
|
||||
return {
|
||||
type: otherTable.primaryColumnType(),
|
||||
references: otherTable.reference(),
|
||||
nullable: false,
|
||||
unique: true,
|
||||
cascade,
|
||||
};
|
||||
},
|
||||
},
|
||||
|
||||
Defaulted: {
|
||||
Text(defaultValue: string): ColumnShorthand {
|
||||
return {
|
||||
type: "TEXT",
|
||||
default: defaultValue,
|
||||
}
|
||||
},
|
||||
|
||||
Int(defaultValue: number): ColumnShorthand {
|
||||
return {
|
||||
type: "INTEGER",
|
||||
default: defaultValue,
|
||||
} as const;
|
||||
},
|
||||
|
||||
Real(defaultValue: number): ColumnShorthand {
|
||||
return {
|
||||
type: "REAL",
|
||||
default: defaultValue,
|
||||
} as const;
|
||||
}
|
||||
},
|
||||
|
||||
} as const;
|
|
@ -1,5 +1,6 @@
|
|||
import type { Column, ColumnShorthand, DataType } from "./columns";
|
||||
import { Prequel } from "./prequel";
|
||||
import { Table } from "./table";
|
||||
import { ColumnOf } from "./column-types";
|
||||
|
||||
export { Prequel, Table, type DataType, type Column, type ColumnShorthand };
|
||||
export { Prequel, Table, ColumnOf, type DataType, type Column, type ColumnShorthand };
|
||||
|
|
10
src/table.ts
10
src/table.ts
|
@ -2,6 +2,7 @@ import type { Database, Statement } from "bun:sqlite";
|
|||
import {
|
||||
type ColumnMap,
|
||||
type ColumnShorthandMap,
|
||||
type DataType,
|
||||
generateColumnDefinitionSQL,
|
||||
normalizeColumns,
|
||||
} from "./columns";
|
||||
|
@ -70,6 +71,7 @@ export class Table<RowShape> {
|
|||
private _truncateQuery: Statement<void, []>;
|
||||
|
||||
public query: typeof Database.prototype.query;
|
||||
public prepare: typeof Database.prototype.prepare;
|
||||
|
||||
constructor(
|
||||
db: Database | Prequel,
|
||||
|
@ -84,7 +86,10 @@ export class Table<RowShape> {
|
|||
this._name = name;
|
||||
this._columnDefinitions = normalizeColumns(cols);
|
||||
this._columnNames = Object.keys(this._columnDefinitions);
|
||||
|
||||
// Pass-thru functions
|
||||
this.query = this._db.query.bind(this._db);
|
||||
this.prepare = this._db.prepare.bind(this._db);
|
||||
|
||||
const columnSQLParts: string[] = [];
|
||||
const updateColumnSQLParts: string[] = [];
|
||||
|
@ -154,6 +159,11 @@ export class Table<RowShape> {
|
|||
return this._columnNames.includes(colName);
|
||||
}
|
||||
|
||||
public primaryColumnType(): DataType {
|
||||
const primary = this._primaryColumnName as keyof typeof this._columnDefinitions;
|
||||
return this._columnDefinitions[primary].type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a reference string for a column in the table.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue