flint-orm 0.1.0 → 0.2.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.
@@ -1,4 +0,0 @@
1
- export { text, integer, boolean, json, date, real } from '../schema/columns';
2
- export type { ColumnDef, IntegerColumnDef, DateColumnDef, DateColumnDefWithDefault } from '../schema/columns';
3
- export { table, snakeCase } from '../schema/table';
4
- export type { InferRow, InsertRow, TableDef, AnyTable } from '../schema/table';
@@ -1,302 +0,0 @@
1
- // @bun
2
- // src/schema/columns.ts
3
- function nullCast() {
4
- return null;
5
- }
6
- function makeColumn(config) {
7
- const base = {
8
- name: config.name,
9
- __internal: {
10
- _type: undefined,
11
- sqlType: config.sqlType,
12
- isPrimaryKey: false,
13
- isNotNull: false,
14
- isUnique: false,
15
- hasDefault: false,
16
- defaultValue: undefined,
17
- defaultFn: undefined,
18
- isAutoIncrement: false,
19
- hasDefaultNow: false,
20
- hasOnUpdate: false,
21
- referencesTable: null,
22
- referencesColumn: null,
23
- encode: config.encode,
24
- decode: config.decode,
25
- tableName: null
26
- }
27
- };
28
- const col = {
29
- ...base,
30
- primaryKey() {
31
- return { ...this, __internal: { ...this.__internal, isPrimaryKey: true } };
32
- },
33
- notNull() {
34
- return { ...this, __internal: { ...this.__internal, isNotNull: true } };
35
- },
36
- unique() {
37
- return { ...this, __internal: { ...this.__internal, isUnique: true } };
38
- },
39
- default(value) {
40
- return { ...this, __internal: { ...this.__internal, hasDefault: true, defaultValue: value } };
41
- },
42
- defaultFn(fn) {
43
- return { ...this, __internal: { ...this.__internal, hasDefault: true, defaultFn: fn } };
44
- },
45
- references(target) {
46
- return {
47
- ...this,
48
- __internal: {
49
- ...this.__internal,
50
- referencesTable: target.__internal.tableName,
51
- referencesColumn: target.name
52
- }
53
- };
54
- }
55
- };
56
- return col;
57
- }
58
- function text(name) {
59
- return makeColumn({
60
- name: name ?? "",
61
- sqlType: "text",
62
- encode: (v) => v,
63
- decode: (v) => v == null ? nullCast() : v
64
- });
65
- }
66
- function integer(name) {
67
- const base = makeColumn({
68
- name: name ?? "",
69
- sqlType: "integer",
70
- encode: (v) => v,
71
- decode: (v) => v == null ? nullCast() : Number(v)
72
- });
73
- const intCol = {
74
- ...base,
75
- primaryKey() {
76
- return {
77
- ...this,
78
- __internal: { ...this.__internal, isPrimaryKey: true }
79
- };
80
- },
81
- notNull() {
82
- return {
83
- ...this,
84
- __internal: { ...this.__internal, isNotNull: true }
85
- };
86
- },
87
- unique() {
88
- return {
89
- ...this,
90
- __internal: { ...this.__internal, isUnique: true }
91
- };
92
- },
93
- default(value) {
94
- return {
95
- ...this,
96
- __internal: { ...this.__internal, hasDefault: true, defaultValue: value }
97
- };
98
- },
99
- defaultFn(fn) {
100
- return {
101
- ...this,
102
- __internal: { ...this.__internal, hasDefault: true, defaultFn: fn }
103
- };
104
- },
105
- autoIncrement() {
106
- return {
107
- ...this,
108
- __internal: { ...this.__internal, isAutoIncrement: true }
109
- };
110
- }
111
- };
112
- return intCol;
113
- }
114
- function boolean(name) {
115
- return makeColumn({
116
- name: name ?? "",
117
- sqlType: "integer",
118
- encode: (v) => v ? 1 : 0,
119
- decode: (v) => v == null ? nullCast() : Boolean(v)
120
- });
121
- }
122
- function json(name) {
123
- return makeColumn({
124
- name: name ?? "",
125
- sqlType: "text",
126
- encode: (v) => v == null ? null : JSON.stringify(v),
127
- decode: (v) => v == null ? nullCast() : JSON.parse(v)
128
- });
129
- }
130
- function date(name) {
131
- const base = makeColumn({
132
- name: name ?? "",
133
- sqlType: "integer",
134
- encode: (v) => v == null ? null : v.getTime(),
135
- decode: (v) => v == null ? nullCast() : new Date(v)
136
- });
137
- const dateCol = {
138
- ...base,
139
- primaryKey() {
140
- return { ...this, __internal: { ...this.__internal, isPrimaryKey: true } };
141
- },
142
- notNull() {
143
- return { ...this, __internal: { ...this.__internal, isNotNull: true } };
144
- },
145
- unique() {
146
- return { ...this, __internal: { ...this.__internal, isUnique: true } };
147
- },
148
- default(value) {
149
- return { ...this, __internal: { ...this.__internal, hasDefault: true, defaultValue: value } };
150
- },
151
- defaultFn(fn) {
152
- return { ...this, __internal: { ...this.__internal, hasDefault: true, defaultFn: fn } };
153
- },
154
- defaultNow() {
155
- return { ...this, __internal: { ...this.__internal, hasDefaultNow: true } };
156
- },
157
- onUpdate() {
158
- return { ...this, __internal: { ...this.__internal, hasOnUpdate: true } };
159
- }
160
- };
161
- return dateCol;
162
- }
163
- function real(name) {
164
- return makeColumn({
165
- name: name ?? "",
166
- sqlType: "real",
167
- encode: (v) => v,
168
- decode: (v) => v == null ? nullCast() : Number(v)
169
- });
170
- }
171
- // src/schema/table.ts
172
- function table(name, columns) {
173
- const stamped = Object.create(null);
174
- for (const [key, col] of Object.entries(columns)) {
175
- const columnName = col.name || key;
176
- const stampedInternal = { ...col.__internal, name: columnName, tableName: name };
177
- const stampedCol = {
178
- name: columnName,
179
- __internal: stampedInternal,
180
- primaryKey() {
181
- return { ...this, __internal: { ...stampedInternal, isPrimaryKey: true } };
182
- },
183
- notNull() {
184
- return { ...this, __internal: { ...stampedInternal, isNotNull: true } };
185
- },
186
- unique() {
187
- return { ...this, __internal: { ...stampedInternal, isUnique: true } };
188
- },
189
- default(value) {
190
- return {
191
- ...this,
192
- __internal: { ...stampedInternal, hasDefault: true, defaultValue: value }
193
- };
194
- },
195
- defaultFn(fn) {
196
- return { ...this, __internal: { ...stampedInternal, hasDefault: true, defaultFn: fn } };
197
- },
198
- references(target) {
199
- return {
200
- ...this,
201
- __internal: {
202
- ...stampedInternal,
203
- referencesTable: target.__internal.tableName,
204
- referencesColumn: target.name
205
- }
206
- };
207
- }
208
- };
209
- if ("autoIncrement" in col) {
210
- stampedCol.autoIncrement = function() {
211
- return { ...this, __internal: { ...stampedInternal, isAutoIncrement: true } };
212
- };
213
- }
214
- if ("defaultNow" in col) {
215
- stampedCol.defaultNow = function() {
216
- return { ...this, __internal: { ...stampedInternal, hasDefaultNow: true } };
217
- };
218
- }
219
- if ("onUpdate" in col) {
220
- stampedCol.onUpdate = function() {
221
- return { ...this, __internal: { ...stampedInternal, hasOnUpdate: true } };
222
- };
223
- }
224
- stamped[key] = stampedCol;
225
- }
226
- return Object.assign(stamped, {
227
- _: { name }
228
- });
229
- }
230
- function toSnakeCase(str) {
231
- return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
232
- }
233
- function snakeCaseTable(tableName, columns) {
234
- const converted = {};
235
- for (const [key, col] of Object.entries(columns)) {
236
- const sqlName = toSnakeCase(key);
237
- const columnName = col.name || sqlName;
238
- const stampedInternal = { ...col.__internal, name: columnName, tableName };
239
- const stampedCol = {
240
- name: columnName,
241
- __internal: stampedInternal,
242
- primaryKey() {
243
- return { ...this, __internal: { ...stampedInternal, isPrimaryKey: true } };
244
- },
245
- notNull() {
246
- return { ...this, __internal: { ...stampedInternal, isNotNull: true } };
247
- },
248
- unique() {
249
- return { ...this, __internal: { ...stampedInternal, isUnique: true } };
250
- },
251
- default(value) {
252
- return {
253
- ...this,
254
- __internal: { ...stampedInternal, hasDefault: true, defaultValue: value }
255
- };
256
- },
257
- defaultFn(fn) {
258
- return { ...this, __internal: { ...stampedInternal, hasDefault: true, defaultFn: fn } };
259
- },
260
- references(target) {
261
- return {
262
- ...this,
263
- __internal: {
264
- ...stampedInternal,
265
- referencesTable: target.__internal.tableName,
266
- referencesColumn: target.name
267
- }
268
- };
269
- }
270
- };
271
- if ("autoIncrement" in col) {
272
- stampedCol.autoIncrement = function() {
273
- return { ...this, __internal: { ...stampedInternal, isAutoIncrement: true } };
274
- };
275
- }
276
- if ("defaultNow" in col) {
277
- stampedCol.defaultNow = function() {
278
- return { ...this, __internal: { ...stampedInternal, hasDefaultNow: true } };
279
- };
280
- }
281
- if ("onUpdate" in col) {
282
- stampedCol.onUpdate = function() {
283
- return { ...this, __internal: { ...stampedInternal, hasOnUpdate: true } };
284
- };
285
- }
286
- converted[key] = stampedCol;
287
- }
288
- return table(tableName, converted);
289
- }
290
- var snakeCase = {
291
- table: snakeCaseTable
292
- };
293
- export {
294
- text,
295
- table,
296
- snakeCase,
297
- real,
298
- json,
299
- integer,
300
- date,
301
- boolean
302
- };
package/dist/errors.d.ts DELETED
@@ -1,13 +0,0 @@
1
- /** Base error class for all flint-orm errors. */
2
- export declare class FlintError extends Error {
3
- constructor(message: string);
4
- }
5
- /** Thrown when a value violates a column constraint or a validation rule fails. */
6
- export declare class FlintValidationError extends FlintError {
7
- constructor(message: string);
8
- }
9
- /** Thrown when a SQL query fails to execute. */
10
- export declare class FlintQueryError extends FlintError {
11
- readonly originalError?: Error;
12
- constructor(message: string, originalError?: Error);
13
- }
package/dist/flint.d.ts DELETED
@@ -1,128 +0,0 @@
1
- import { Database } from 'bun:sqlite';
2
- import { DeleteBuilder } from './query/builder';
3
- import type { Executable, SelectStage1, InsertStage1, UpdateStage1, JoinSelectStage1 } from './query/builder';
4
- import type { AnyTable } from './schema/table';
5
- import type { Condition } from './query/conditions';
6
- import type { ColumnDef } from './schema/columns';
7
- export type { Executable, SelectStage1, InsertStage1, UpdateStage1, JoinSelectStage1, JoinBuilder, SingleJoinBuilder } from './query/builder';
8
- export type { JoinResult } from './query/builder';
9
- export interface ConnectionDetails {
10
- /** Connection details for the SQLite database. */
11
- url: string;
12
- }
13
- /**
14
- * Create a flint database client.
15
- *
16
- * @example
17
- * const db = flint({ url: "app.db" });
18
- */
19
- export declare function flint(details: ConnectionDetails): {
20
- /**
21
- * Start a SELECT query — call `.from(table)` next.
22
- *
23
- * @example
24
- * const rows = db.select().from(users).execute()
25
- */
26
- select: () => SelectStage1;
27
- /**
28
- * Start an INSERT — call `.values(row)` next.
29
- *
30
- * @example
31
- * db.insert(users).values({ id: "u1", name: "Alice" }).execute()
32
- */
33
- insert: <T extends AnyTable>(table: T) => InsertStage1<T>;
34
- /**
35
- * Start an UPDATE — call `.set(partial)` next.
36
- *
37
- * @example
38
- * db.update(users).set({ name: "Bob" }).where(eq(users.id, "u1")).execute()
39
- */
40
- update: <T extends AnyTable>(table: T) => UpdateStage1<T>;
41
- /**
42
- * Start a DELETE — call `.where(condition)` next.
43
- *
44
- * @example
45
- * db.delete(users).where(eq(users.id, "u1")).execute()
46
- */
47
- delete: <T extends AnyTable>(table: T) => DeleteBuilder<T, false, keyof import(".").InferRow<T>>;
48
- /**
49
- * Start a LEFT JOIN — call `.on(child)` next.
50
- *
51
- * @example
52
- * db.leftJoin(users).on(posts).execute()
53
- */
54
- leftJoin: <Parent extends AnyTable>(parent: Parent) => JoinSelectStage1<Parent>;
55
- /**
56
- * Start an INNER JOIN — call `.on(child)` next.
57
- *
58
- * @example
59
- * db.innerJoin(users).on(posts).execute()
60
- */
61
- innerJoin: <Parent extends AnyTable>(parent: Parent) => JoinSelectStage1<Parent>;
62
- /**
63
- * Run multiple queries atomically in a single transaction.
64
- *
65
- * @example
66
- * db.batch([
67
- * db.insert(users).values({ id: "u1", name: "Alice" }),
68
- * db.insert(posts).values({ id: "p1", userId: "u1", title: "Hello" }),
69
- * ])
70
- */
71
- batch: (queries: Executable[]) => void;
72
- /**
73
- * Count all rows in a table, optionally filtered by a condition.
74
- *
75
- * @example
76
- * db.count(users)
77
- */
78
- count: <T extends AnyTable>(table: T, condition?: Condition) => number;
79
- /**
80
- * Count non-null values of a column, optionally filtered by a condition.
81
- *
82
- * @example
83
- * db.countColumn(users, users.email)
84
- */
85
- countColumn: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => number;
86
- /**
87
- * Sum non-null values of a column, optionally filtered by a condition.
88
- *
89
- * @example
90
- * db.sum(orders, orders.amount)
91
- */
92
- sum: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => number | null;
93
- /**
94
- * Average non-null values of a column, optionally filtered by a condition.
95
- *
96
- * @example
97
- * db.avg(users, users.age)
98
- */
99
- avg: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => number | null;
100
- /**
101
- * Find the minimum non-null value of a column, optionally filtered by a condition.
102
- *
103
- * @example
104
- * db.min(users, users.age)
105
- */
106
- min: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => number | null;
107
- /**
108
- * Find the maximum non-null value of a column, optionally filtered by a condition.
109
- *
110
- * @example
111
- * db.max(users, users.age)
112
- */
113
- max: <T extends AnyTable, C extends ColumnDef<any, any>>(table: T, column: C, condition?: Condition) => number | null;
114
- /** Direct access to the underlying `bun:sqlite` client. */
115
- $client: Database;
116
- };
117
- /** A raw SQL expression with parameters. */
118
- export interface SQLExpression {
119
- sql: string;
120
- params: unknown[];
121
- }
122
- /**
123
- * Tagged template for building parameterized SQL expressions.
124
- *
125
- * @example
126
- * const expr = sql`name = ${"Alice"} AND age > ${18}`
127
- */
128
- export declare function sql(strings: TemplateStringsArray, ...values: unknown[]): SQLExpression;
package/dist/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- export { flint, sql } from './flint';
2
- export type { ConnectionDetails, SQLExpression, Executable, SelectStage1, InsertStage1, UpdateStage1 } from './flint';
3
- export { text, integer, boolean, json, date, real } from './schema/columns';
4
- export type { ColumnDef, IntegerColumnDef, DateColumnDef, DateColumnDefWithDefault } from './schema/columns';
5
- export { table, snakeCase } from './schema/table';
6
- export type { InferRow, InsertRow, TableDef, AnyTable } from './schema/table';
7
- export { eq, and, or, gt, gte, lt, lte, neq, isIn, isNotIn, isNull, isNotNull, like, glob, between } from './query/conditions';
8
- export type { Condition } from './query/conditions';
9
- export { count, countColumn, sum, avg, min, max } from './query/aggregates';
@@ -1,47 +0,0 @@
1
- import type { Database } from 'bun:sqlite';
2
- import type { ColumnDef } from '../schema/columns';
3
- import type { AnyTable } from '../schema/table';
4
- import type { Condition } from './conditions';
5
- /**
6
- * Count all rows in a table, optionally filtered by a condition.
7
- *
8
- * @example
9
- * const total = db.count(users);
10
- * const active = db.count(users, eq(users.active, true));
11
- */
12
- export declare function count<T extends AnyTable>(client: Database, table: T, condition?: Condition): number;
13
- /**
14
- * Count non-null values of a column, optionally filtered by a condition.
15
- *
16
- * @example
17
- * const count = db.countColumn(users, users.email);
18
- */
19
- export declare function countColumn<T extends AnyTable, C extends ColumnDef<any, any>>(client: Database, table: T, column: C, condition?: Condition): number;
20
- /**
21
- * Sum non-null values of a column, optionally filtered by a condition.
22
- *
23
- * @example
24
- * const total = db.sum(orders, orders.amount);
25
- */
26
- export declare function sum<T extends AnyTable, C extends ColumnDef<any, any>>(client: Database, table: T, column: C, condition?: Condition): number | null;
27
- /**
28
- * Average non-null values of a column, optionally filtered by a condition.
29
- *
30
- * @example
31
- * const avgAge = db.avg(users, users.age);
32
- */
33
- export declare function avg<T extends AnyTable, C extends ColumnDef<any, any>>(client: Database, table: T, column: C, condition?: Condition): number | null;
34
- /**
35
- * Find the minimum non-null value of a column, optionally filtered by a condition.
36
- *
37
- * @example
38
- * const minAge = db.min(users, users.age);
39
- */
40
- export declare function min<T extends AnyTable, C extends ColumnDef<any, any>>(client: Database, table: T, column: C, condition?: Condition): number | null;
41
- /**
42
- * Find the maximum non-null value of a column, optionally filtered by a condition.
43
- *
44
- * @example
45
- * const maxAge = db.max(users, users.age);
46
- */
47
- export declare function max<T extends AnyTable, C extends ColumnDef<any, any>>(client: Database, table: T, column: C, condition?: Condition): number | null;