flint-orm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API.md +620 -0
- package/README.md +0 -0
- package/dist/entries/expressions.d.ts +5 -0
- package/dist/entries/expressions.js +1184 -0
- package/dist/entries/table.d.ts +4 -0
- package/dist/entries/table.js +302 -0
- package/dist/errors.d.ts +13 -0
- package/dist/flint.d.ts +128 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +1485 -0
- package/dist/query/aggregates.d.ts +47 -0
- package/dist/query/builder.d.ts +256 -0
- package/dist/query/conditions.d.ts +176 -0
- package/dist/schema/columns.d.ts +137 -0
- package/dist/schema/table.d.ts +44 -0
- package/dist/src/entries/expressions.d.ts +5 -0
- package/dist/src/entries/table.d.ts +4 -0
- package/dist/src/errors.d.ts +13 -0
- package/dist/src/flint.d.ts +128 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/query/aggregates.d.ts +47 -0
- package/dist/src/query/builder.d.ts +256 -0
- package/dist/src/query/conditions.d.ts +176 -0
- package/dist/src/schema/columns.d.ts +137 -0
- package/dist/src/schema/table.d.ts +44 -0
- package/package.json +40 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A column definition with chainable modifiers.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const name = text("name").notNull().default("unnamed");
|
|
6
|
+
*/
|
|
7
|
+
export interface ColumnDef<T, S extends string = string> {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
primaryKey(): ColumnDef<T, S>;
|
|
10
|
+
notNull(): ColumnDef<T, S>;
|
|
11
|
+
unique(): ColumnDef<T, S>;
|
|
12
|
+
/** Set a static default value used when the value is omitted during insert. */
|
|
13
|
+
default(value: T): ColumnDef<T, S>;
|
|
14
|
+
/** Set a dynamic default function called when the value is omitted during insert. */
|
|
15
|
+
defaultFn(fn: () => T): ColumnDef<T, S>;
|
|
16
|
+
/**
|
|
17
|
+
* Define a foreign key reference to another table's column.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* text("userId").references(users.id)
|
|
21
|
+
*/
|
|
22
|
+
references(target: ColumnDef<any, any>): ColumnDef<T, S>;
|
|
23
|
+
readonly __internal: {
|
|
24
|
+
/** Phantom — exists only at the type level, never accessed at runtime. */
|
|
25
|
+
readonly _type: T;
|
|
26
|
+
/** SQLite storage class: "text" | "integer" | "real" | "blob". */
|
|
27
|
+
readonly sqlType: S;
|
|
28
|
+
/** Column constraint flags — set via modifiers, read by migration generator. */
|
|
29
|
+
readonly isPrimaryKey: boolean;
|
|
30
|
+
readonly isNotNull: boolean;
|
|
31
|
+
readonly isUnique: boolean;
|
|
32
|
+
readonly hasDefault: boolean;
|
|
33
|
+
readonly defaultValue: T | undefined;
|
|
34
|
+
readonly defaultFn: (() => T) | undefined;
|
|
35
|
+
readonly isAutoIncrement: boolean;
|
|
36
|
+
readonly hasDefaultNow: boolean;
|
|
37
|
+
readonly hasOnUpdate: boolean;
|
|
38
|
+
/** Foreign key reference — table and column names. */
|
|
39
|
+
readonly referencesTable: string | null;
|
|
40
|
+
readonly referencesColumn: string | null;
|
|
41
|
+
/** Converts logical value → storage value. Called by the builder. */
|
|
42
|
+
readonly encode: (value: T) => unknown;
|
|
43
|
+
/** Converts storage value → logical value. Called by the builder. */
|
|
44
|
+
readonly decode: (value: unknown) => T;
|
|
45
|
+
/** Table name — set by table() when the column is attached. Used for join disambiguation. */
|
|
46
|
+
readonly tableName: string | null;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** An integer column definition with auto-increment support. */
|
|
50
|
+
export interface IntegerColumnDef<S extends string = 'integer'> extends ColumnDef<number, S> {
|
|
51
|
+
primaryKey(): IntegerColumnDef<S>;
|
|
52
|
+
notNull(): IntegerColumnDef<S>;
|
|
53
|
+
unique(): IntegerColumnDef<S>;
|
|
54
|
+
default(value: number): IntegerColumnDef<S>;
|
|
55
|
+
defaultFn(fn: () => number): IntegerColumnDef<S>;
|
|
56
|
+
/**
|
|
57
|
+
* Mark as auto-increment (SQLite ROWID alias).
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* const id = integer("id").primaryKey().autoIncrement();
|
|
61
|
+
*/
|
|
62
|
+
autoIncrement(): IntegerColumnDef<S>;
|
|
63
|
+
}
|
|
64
|
+
/** A date column that stores a unix timestamp (milliseconds) in SQLite. Nullable in results unless `.defaultNow()` is called. */
|
|
65
|
+
export interface DateColumnDef extends ColumnDef<Date, 'integer'> {
|
|
66
|
+
primaryKey(): DateColumnDef;
|
|
67
|
+
notNull(): DateColumnDef;
|
|
68
|
+
unique(): DateColumnDef;
|
|
69
|
+
default(value: Date): DateColumnDef;
|
|
70
|
+
defaultFn(fn: () => Date): DateColumnDef;
|
|
71
|
+
/**
|
|
72
|
+
* Use `Date.now()` as the default when the value is omitted during insert.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* const createdAt = date("created_at").defaultNow();
|
|
76
|
+
*/
|
|
77
|
+
defaultNow(): DateColumnDefWithDefault;
|
|
78
|
+
/**
|
|
79
|
+
* Always set to `Date.now()` on update, regardless of the provided value.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const updatedAt = date("updated_at").onUpdate();
|
|
83
|
+
*/
|
|
84
|
+
onUpdate(): DateColumnDef;
|
|
85
|
+
}
|
|
86
|
+
/** A date column with a guaranteed default value, making it non-nullable in query results. */
|
|
87
|
+
export interface DateColumnDefWithDefault extends DateColumnDef {
|
|
88
|
+
primaryKey(): DateColumnDefWithDefault;
|
|
89
|
+
notNull(): DateColumnDefWithDefault;
|
|
90
|
+
unique(): DateColumnDefWithDefault;
|
|
91
|
+
default(value: Date): DateColumnDefWithDefault;
|
|
92
|
+
defaultFn(fn: () => Date): DateColumnDefWithDefault;
|
|
93
|
+
defaultNow(): DateColumnDefWithDefault;
|
|
94
|
+
onUpdate(): DateColumnDefWithDefault;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create a text column.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* const name = text("name").notNull();
|
|
101
|
+
*/
|
|
102
|
+
export declare function text(name?: string): ColumnDef<string, 'text'>;
|
|
103
|
+
/**
|
|
104
|
+
* Create an integer column.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const id = integer("id").primaryKey().autoIncrement();
|
|
108
|
+
*/
|
|
109
|
+
export declare function integer(name?: string): IntegerColumnDef<'integer'>;
|
|
110
|
+
/**
|
|
111
|
+
* Create a boolean column. Stores `0`/`1` in SQLite, exposed as `boolean` in TypeScript.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* const active = boolean("active").notNull().default(true);
|
|
115
|
+
*/
|
|
116
|
+
export declare function boolean(name?: string): ColumnDef<boolean, 'integer'>;
|
|
117
|
+
/**
|
|
118
|
+
* Create a JSON column. Stores JSON text in SQLite, exposed as `T` in TypeScript.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* const meta = json<Record<string, unknown>>("meta").default({});
|
|
122
|
+
*/
|
|
123
|
+
export declare function json<T>(name?: string): ColumnDef<T, 'text'>;
|
|
124
|
+
/**
|
|
125
|
+
* Create a date column. Stores a unix timestamp (milliseconds) in SQLite, exposed as `Date` in TypeScript.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const createdAt = date("created_at").defaultNow().notNull();
|
|
129
|
+
*/
|
|
130
|
+
export declare function date(name?: string): DateColumnDef;
|
|
131
|
+
/**
|
|
132
|
+
* Create a real (floating-point) column.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* const price = real("price").notNull();
|
|
136
|
+
*/
|
|
137
|
+
export declare function real(name?: string): ColumnDef<number, 'real'>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ColumnDef, DateColumnDef, DateColumnDefWithDefault } from './columns';
|
|
2
|
+
/** A table definition mapping column names to their `ColumnDef`s. */
|
|
3
|
+
export type TableDef<T> = T & {
|
|
4
|
+
readonly _: {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export declare function table<T extends Record<string, ColumnDef<any, any>>>(name: string, columns: T): TableDef<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Derive the row shape from a table's column definitions.
|
|
11
|
+
* DateColumnDef → Date | null (nullable unless defaultNow() was called)
|
|
12
|
+
* DateColumnDefWithDefault → Date (non-nullable, has a guaranteed default)
|
|
13
|
+
* All other columns → their _type as-is.
|
|
14
|
+
*
|
|
15
|
+
* Uses `infer C` to extract the column record from TableDef<T> so TypeScript
|
|
16
|
+
* evaluates the mapped type against the concrete columns, not the intersection.
|
|
17
|
+
* This produces cleaner hover info: { id: string; name: string } instead of
|
|
18
|
+
* Pick<InferRow<TableDef<{...}>>, ...>.
|
|
19
|
+
*/
|
|
20
|
+
export type InferRow<T extends TableDef<any>> = T extends TableDef<infer C> ? {
|
|
21
|
+
[K in keyof Omit<C, '_'>]: C[K] extends DateColumnDefWithDefault ? Date : C[K] extends DateColumnDef ? Date | null : C[K] extends ColumnDef<any, any> ? C[K]['__internal']['_type'] : never;
|
|
22
|
+
} : never;
|
|
23
|
+
/** The row type for inserts — columns with auto-defaults (integer, date) are optional. */
|
|
24
|
+
export type InsertRow<T extends TableDef<any>> = {
|
|
25
|
+
[K in keyof Omit<T, '_'> as HasAutoDefault<T[K]> extends true ? never : K]: T[K] extends ColumnDef<any, any> ? T[K]['__internal']['_type'] : never;
|
|
26
|
+
} & {
|
|
27
|
+
[K in keyof Omit<T, '_'> as HasAutoDefault<T[K]> extends true ? K : never]?: T[K] extends ColumnDef<any, any> ? T[K]['__internal']['_type'] : never;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Define a table with auto snake_case column names.
|
|
31
|
+
* Column names are inferred from object keys and converted to snake_case.
|
|
32
|
+
* Column constructors can be called without a name.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const users = snakeCase.table("users", {
|
|
36
|
+
* id: text().primaryKey(),
|
|
37
|
+
* firstName: text(),
|
|
38
|
+
* });
|
|
39
|
+
*/
|
|
40
|
+
export declare function snakeCaseTable<T extends Record<string, ColumnDef<any, any>>>(tableName: string, columns: T): TableDef<T>;
|
|
41
|
+
/** Provides `snakeCase.table()` for auto snake_case column naming. */
|
|
42
|
+
export declare const snakeCase: {
|
|
43
|
+
table: typeof snakeCaseTable;
|
|
44
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "flint-orm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"API.md",
|
|
7
|
+
"README.md"
|
|
8
|
+
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./table": {
|
|
18
|
+
"import": "./dist/entries/table.js",
|
|
19
|
+
"types": "./dist/entries/table.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./expressions": {
|
|
22
|
+
"import": "./dist/entries/expressions.js",
|
|
23
|
+
"types": "./dist/entries/expressions.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "bun build ./src/index.ts ./src/entries/table.ts ./src/entries/expressions.ts --outdir ./dist --target bun && tsc --project tsconfig.build.json",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"lint": "oxlint",
|
|
30
|
+
"format": "oxfmt"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/bun": "1.3.14",
|
|
34
|
+
"oxfmt": "0.57.0",
|
|
35
|
+
"oxlint": "1.72.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"typescript": "7.0.1-rc"
|
|
39
|
+
}
|
|
40
|
+
}
|