@rwillians/qx 0.1.10 → 0.1.12
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/README.md +5 -5
- package/dist/cjs/bun-sqlite.js +8 -1
- package/dist/cjs/console-logger.js +16 -0
- package/dist/cjs/experimental-migration.js +6 -5
- package/dist/cjs/index.js +14 -9
- package/dist/cjs/standard-schema.js +3 -0
- package/dist/esm/bun-sqlite.js +7 -1
- package/dist/esm/console-logger.js +13 -0
- package/dist/esm/experimental-migration.js +6 -5
- package/dist/esm/index.js +14 -9
- package/dist/esm/standard-schema.js +4 -2
- package/dist/types/bun-sqlite.d.ts +7 -1
- package/dist/types/{pretty-logger.d.ts → console-logger.d.ts} +2 -2
- package/dist/types/index.d.ts +36 -28
- package/dist/types/standard-schema.d.ts +49 -17
- package/package.json +1 -5
- package/dist/cjs/pretty-logger.js +0 -16
- package/dist/esm/pretty-logger.js +0 -13
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
# qx
|
|
1
|
+
# qx
|
|
2
2
|
|
|
3
|
-
A teeny tiny ORM for TypeScript and
|
|
4
|
-
|
|
3
|
+
A teeny tiny, type-safe and dependency-free ORM for TypeScript and
|
|
4
|
+
JavaScript.
|
|
5
5
|
|
|
6
6
|
Built for you who wants a simple, small ORM that just works.
|
|
7
7
|
|
|
@@ -10,10 +10,10 @@ import * as sqlite from '@rwillians/qx/bun-sqlite';
|
|
|
10
10
|
import { create, from, into, table } from '@rwillians/qx';
|
|
11
11
|
|
|
12
12
|
const users = table('users', t => ({
|
|
13
|
-
id: t.integer(
|
|
13
|
+
id: t.integer().autoincrement().primaryKey(),
|
|
14
14
|
name: t.string(),
|
|
15
15
|
email: t.string(),
|
|
16
|
-
createdAt: t.
|
|
16
|
+
createdAt: t.datetime(),
|
|
17
17
|
}));
|
|
18
18
|
|
|
19
19
|
// ...
|
package/dist/cjs/bun-sqlite.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.connect = void 0;
|
|
3
|
+
exports.inmemory = exports.connect = void 0;
|
|
4
4
|
const bun_sqlite_1 = require("bun:sqlite");
|
|
5
5
|
const node_util_1 = require("node:util");
|
|
6
6
|
const u = require("./utils");
|
|
@@ -505,3 +505,10 @@ class BunSQLite {
|
|
|
505
505
|
*/
|
|
506
506
|
const connect = (...args) => new BunSQLite(new bun_sqlite_1.Database(...args));
|
|
507
507
|
exports.connect = connect;
|
|
508
|
+
/**
|
|
509
|
+
* @public An in-memory database connection.
|
|
510
|
+
* @since 0.1.12
|
|
511
|
+
* @version 1
|
|
512
|
+
*/
|
|
513
|
+
const inmemory = connect(':memory:');
|
|
514
|
+
exports.inmemory = inmemory;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createConsoleLogger = void 0;
|
|
4
|
+
const node_util_1 = require("node:util");
|
|
5
|
+
/**
|
|
6
|
+
* @public Creates a basic console logger that logs all queries.
|
|
7
|
+
* @since 0.1.0
|
|
8
|
+
* @version 2
|
|
9
|
+
*/
|
|
10
|
+
const createConsoleLogger = () => ({
|
|
11
|
+
query: {
|
|
12
|
+
debug: (sql, params) => { process.stdout.write(sql + ' ' + (0, node_util_1.inspect)(params, false, null, true) + '\n'); },
|
|
13
|
+
error: (sql, params) => { process.stderr.write(sql + ' ' + (0, node_util_1.inspect)(params, false, null, true) + '\n'); },
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
exports.createConsoleLogger = createConsoleLogger;
|
|
@@ -14,15 +14,16 @@ const migrations = (0, index_1.table)('schema_migrations', t => ({
|
|
|
14
14
|
*/
|
|
15
15
|
const defineMigrations = (migs) => async (db) => {
|
|
16
16
|
await index_1.create.table(migrations, { ifNotExists: true }).onto(db);
|
|
17
|
+
const { mostRecentId } = await (0, index_1.from)(migrations.as('m'))
|
|
18
|
+
.orderBy(({ m }) => [[m.timestamp, 'DESC']])
|
|
19
|
+
.select(({ m }) => ({ mostRecentId: m.id }))
|
|
20
|
+
.one(db) || { mostRecentId: '' };
|
|
17
21
|
for (const [id, migrate] of Object.entries(migs)) {
|
|
18
|
-
|
|
19
|
-
.where(({ m }) => index_1.expr.eq(m.id, id))
|
|
20
|
-
.exists(db);
|
|
21
|
-
if (alreadyMigrated)
|
|
22
|
+
if (id < mostRecentId)
|
|
22
23
|
continue;
|
|
23
24
|
await (0, index_1.transaction)(db, async () => {
|
|
24
25
|
await migrate(db);
|
|
25
|
-
await (0, index_1.into)(migrations).
|
|
26
|
+
await (0, index_1.into)(migrations).values({ id, timestamp: new Date() }).insert(db);
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
29
|
};
|
package/dist/cjs/index.js
CHANGED
|
@@ -21,7 +21,7 @@ const TABLE_ALIAS = Symbol.for('~exto.table-alias');
|
|
|
21
21
|
/**
|
|
22
22
|
* @private A builder for column properties.
|
|
23
23
|
* @since 0.1.0
|
|
24
|
-
* @version
|
|
24
|
+
* @version 2
|
|
25
25
|
*/
|
|
26
26
|
class ColumnPropsBuilder {
|
|
27
27
|
props;
|
|
@@ -39,9 +39,12 @@ class ColumnPropsBuilder {
|
|
|
39
39
|
return new ColumnPropsBuilder({ ...this.props, autoincrement: true });
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
* @public Sets a default value for the column.
|
|
42
|
+
* @public Sets a default value for the column. This is not the
|
|
43
|
+
* table's default value for the column, but rather a
|
|
44
|
+
* fallback value that qx will use when inserting a new row
|
|
45
|
+
* where this column is nullish.
|
|
43
46
|
* @since 0.1.0
|
|
44
|
-
* @version
|
|
47
|
+
* @version 2
|
|
45
48
|
*/
|
|
46
49
|
default(value) {
|
|
47
50
|
return new ColumnPropsBuilder({ ...this.props, default: value });
|
|
@@ -49,12 +52,12 @@ class ColumnPropsBuilder {
|
|
|
49
52
|
/**
|
|
50
53
|
* @public Marks the column as nullable.
|
|
51
54
|
* @since 0.1.0
|
|
52
|
-
* @version
|
|
55
|
+
* @version 2
|
|
53
56
|
* @throws {Error} if the column is a primary key.
|
|
54
57
|
*/
|
|
55
58
|
nullable() {
|
|
56
59
|
if (this.props.primaryKey)
|
|
57
|
-
throw new Error('Cannot
|
|
60
|
+
throw new Error('Cannot make a primary key column nullable');
|
|
58
61
|
const { schema, ...props } = this.props;
|
|
59
62
|
return new ColumnPropsBuilder({ ...props, nullable: true, schema: std.nullable(schema) });
|
|
60
63
|
}
|
|
@@ -72,9 +75,11 @@ class ColumnPropsBuilder {
|
|
|
72
75
|
/**
|
|
73
76
|
* @public Marks the column as unique.
|
|
74
77
|
* @since 0.1.0
|
|
75
|
-
* @version
|
|
78
|
+
* @version 2
|
|
76
79
|
*/
|
|
77
80
|
unique() {
|
|
81
|
+
if (this.props.primaryKey)
|
|
82
|
+
throw new Error('Primary key columns are already unique');
|
|
78
83
|
return new ColumnPropsBuilder({ ...this.props, unique: true });
|
|
79
84
|
}
|
|
80
85
|
}
|
|
@@ -538,9 +543,9 @@ class InsertBuilder {
|
|
|
538
543
|
/**
|
|
539
544
|
* @public Adds one or more rows to be inserted.
|
|
540
545
|
* @since 0.1.0
|
|
541
|
-
* @version
|
|
546
|
+
* @version 2
|
|
542
547
|
*/
|
|
543
|
-
|
|
548
|
+
values(rows) {
|
|
544
549
|
this.rows.push(...(u.wrap(rows)));
|
|
545
550
|
return this;
|
|
546
551
|
}
|
|
@@ -549,7 +554,7 @@ class InsertBuilder {
|
|
|
549
554
|
* @since 0.1.0
|
|
550
555
|
* @version 1
|
|
551
556
|
*/
|
|
552
|
-
async
|
|
557
|
+
async insert(db) {
|
|
553
558
|
if (this.rows.length === 0)
|
|
554
559
|
return [];
|
|
555
560
|
const insertShape = getInsertShape(this.table);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
3
|
+
// STANDARD SCHEMA SPEC //
|
|
4
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.string = exports.strictObject = exports.number = exports.nullable = exports.integer = exports.instanceOf = exports.date = exports.boolean = exports.array = exports.parse = void 0;
|
|
4
7
|
/**
|
package/dist/esm/bun-sqlite.js
CHANGED
|
@@ -501,7 +501,13 @@ class BunSQLite {
|
|
|
501
501
|
* @version 1
|
|
502
502
|
*/
|
|
503
503
|
const connect = (...args) => new BunSQLite(new Database(...args));
|
|
504
|
+
/**
|
|
505
|
+
* @public An in-memory database connection.
|
|
506
|
+
* @since 0.1.12
|
|
507
|
+
* @version 1
|
|
508
|
+
*/
|
|
509
|
+
const inmemory = connect(':memory:');
|
|
504
510
|
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
505
511
|
// EXPORTS //
|
|
506
512
|
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
507
|
-
export { connect, };
|
|
513
|
+
export { connect, inmemory, };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { inspect } from 'node:util';
|
|
2
|
+
import {} from './index';
|
|
3
|
+
/**
|
|
4
|
+
* @public Creates a basic console logger that logs all queries.
|
|
5
|
+
* @since 0.1.0
|
|
6
|
+
* @version 2
|
|
7
|
+
*/
|
|
8
|
+
export const createConsoleLogger = () => ({
|
|
9
|
+
query: {
|
|
10
|
+
debug: (sql, params) => { process.stdout.write(sql + ' ' + inspect(params, false, null, true) + '\n'); },
|
|
11
|
+
error: (sql, params) => { process.stderr.write(sql + ' ' + inspect(params, false, null, true) + '\n'); },
|
|
12
|
+
},
|
|
13
|
+
});
|
|
@@ -11,15 +11,16 @@ const migrations = table('schema_migrations', t => ({
|
|
|
11
11
|
*/
|
|
12
12
|
export const defineMigrations = (migs) => async (db) => {
|
|
13
13
|
await create.table(migrations, { ifNotExists: true }).onto(db);
|
|
14
|
+
const { mostRecentId } = await from(migrations.as('m'))
|
|
15
|
+
.orderBy(({ m }) => [[m.timestamp, 'DESC']])
|
|
16
|
+
.select(({ m }) => ({ mostRecentId: m.id }))
|
|
17
|
+
.one(db) || { mostRecentId: '' };
|
|
14
18
|
for (const [id, migrate] of Object.entries(migs)) {
|
|
15
|
-
|
|
16
|
-
.where(({ m }) => expr.eq(m.id, id))
|
|
17
|
-
.exists(db);
|
|
18
|
-
if (alreadyMigrated)
|
|
19
|
+
if (id < mostRecentId)
|
|
19
20
|
continue;
|
|
20
21
|
await transaction(db, async () => {
|
|
21
22
|
await migrate(db);
|
|
22
|
-
await into(migrations).
|
|
23
|
+
await into(migrations).values({ id, timestamp: new Date() }).insert(db);
|
|
23
24
|
});
|
|
24
25
|
}
|
|
25
26
|
};
|
package/dist/esm/index.js
CHANGED
|
@@ -18,7 +18,7 @@ const TABLE_ALIAS = Symbol.for('~exto.table-alias');
|
|
|
18
18
|
/**
|
|
19
19
|
* @private A builder for column properties.
|
|
20
20
|
* @since 0.1.0
|
|
21
|
-
* @version
|
|
21
|
+
* @version 2
|
|
22
22
|
*/
|
|
23
23
|
class ColumnPropsBuilder {
|
|
24
24
|
props;
|
|
@@ -36,9 +36,12 @@ class ColumnPropsBuilder {
|
|
|
36
36
|
return new ColumnPropsBuilder({ ...this.props, autoincrement: true });
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
* @public Sets a default value for the column.
|
|
39
|
+
* @public Sets a default value for the column. This is not the
|
|
40
|
+
* table's default value for the column, but rather a
|
|
41
|
+
* fallback value that qx will use when inserting a new row
|
|
42
|
+
* where this column is nullish.
|
|
40
43
|
* @since 0.1.0
|
|
41
|
-
* @version
|
|
44
|
+
* @version 2
|
|
42
45
|
*/
|
|
43
46
|
default(value) {
|
|
44
47
|
return new ColumnPropsBuilder({ ...this.props, default: value });
|
|
@@ -46,12 +49,12 @@ class ColumnPropsBuilder {
|
|
|
46
49
|
/**
|
|
47
50
|
* @public Marks the column as nullable.
|
|
48
51
|
* @since 0.1.0
|
|
49
|
-
* @version
|
|
52
|
+
* @version 2
|
|
50
53
|
* @throws {Error} if the column is a primary key.
|
|
51
54
|
*/
|
|
52
55
|
nullable() {
|
|
53
56
|
if (this.props.primaryKey)
|
|
54
|
-
throw new Error('Cannot
|
|
57
|
+
throw new Error('Cannot make a primary key column nullable');
|
|
55
58
|
const { schema, ...props } = this.props;
|
|
56
59
|
return new ColumnPropsBuilder({ ...props, nullable: true, schema: std.nullable(schema) });
|
|
57
60
|
}
|
|
@@ -69,9 +72,11 @@ class ColumnPropsBuilder {
|
|
|
69
72
|
/**
|
|
70
73
|
* @public Marks the column as unique.
|
|
71
74
|
* @since 0.1.0
|
|
72
|
-
* @version
|
|
75
|
+
* @version 2
|
|
73
76
|
*/
|
|
74
77
|
unique() {
|
|
78
|
+
if (this.props.primaryKey)
|
|
79
|
+
throw new Error('Primary key columns are already unique');
|
|
75
80
|
return new ColumnPropsBuilder({ ...this.props, unique: true });
|
|
76
81
|
}
|
|
77
82
|
}
|
|
@@ -529,9 +534,9 @@ class InsertBuilder {
|
|
|
529
534
|
/**
|
|
530
535
|
* @public Adds one or more rows to be inserted.
|
|
531
536
|
* @since 0.1.0
|
|
532
|
-
* @version
|
|
537
|
+
* @version 2
|
|
533
538
|
*/
|
|
534
|
-
|
|
539
|
+
values(rows) {
|
|
535
540
|
this.rows.push(...(u.wrap(rows)));
|
|
536
541
|
return this;
|
|
537
542
|
}
|
|
@@ -540,7 +545,7 @@ class InsertBuilder {
|
|
|
540
545
|
* @since 0.1.0
|
|
541
546
|
* @version 1
|
|
542
547
|
*/
|
|
543
|
-
async
|
|
548
|
+
async insert(db) {
|
|
544
549
|
if (this.rows.length === 0)
|
|
545
550
|
return [];
|
|
546
551
|
const insertShape = getInsertShape(this.table);
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
2
|
+
// STANDARD SCHEMA SPEC //
|
|
3
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
2
4
|
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
3
5
|
// STANDARD SCHEMA API //
|
|
4
6
|
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
@@ -7,7 +9,7 @@ import * as std from '@standard-schema/spec';
|
|
|
7
9
|
* @since 0.1.0
|
|
8
10
|
* @version 1
|
|
9
11
|
*/
|
|
10
|
-
export {}
|
|
12
|
+
export {};
|
|
11
13
|
/**
|
|
12
14
|
* @public Use any standard schema to parse a value.
|
|
13
15
|
* @since 0.1.0
|
|
@@ -50,4 +50,10 @@ declare class BunSQLite implements IDatabase {
|
|
|
50
50
|
* @version 1
|
|
51
51
|
*/
|
|
52
52
|
declare const connect: (...args: ConstructorParameters<typeof Database>) => BunSQLite;
|
|
53
|
-
|
|
53
|
+
/**
|
|
54
|
+
* @public An in-memory database connection.
|
|
55
|
+
* @since 0.1.12
|
|
56
|
+
* @version 1
|
|
57
|
+
*/
|
|
58
|
+
declare const inmemory: BunSQLite;
|
|
59
|
+
export { connect, inmemory, };
|
|
@@ -2,6 +2,6 @@ import { type ILogger } from './index';
|
|
|
2
2
|
/**
|
|
3
3
|
* @public Creates a basic console logger that logs all queries.
|
|
4
4
|
* @since 0.1.0
|
|
5
|
-
* @version
|
|
5
|
+
* @version 2
|
|
6
6
|
*/
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const createConsoleLogger: () => ILogger;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -40,10 +40,9 @@ type ColumnProps<T extends std.Schema = std.Schema> = {
|
|
|
40
40
|
*/
|
|
41
41
|
autoincrement?: true;
|
|
42
42
|
/**
|
|
43
|
-
* @public The default value for the column
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* once for each row right before its insertion.
|
|
43
|
+
* @public The default value for the column. If a column has
|
|
44
|
+
* nullish value in an insert, then this value will be used
|
|
45
|
+
* instead.
|
|
47
46
|
* @since 0.1.0
|
|
48
47
|
* @version 1
|
|
49
48
|
*/
|
|
@@ -92,7 +91,7 @@ type ColumnProps<T extends std.Schema = std.Schema> = {
|
|
|
92
91
|
/**
|
|
93
92
|
* @public A column definition.
|
|
94
93
|
* @since 0.1.0
|
|
95
|
-
* @version
|
|
94
|
+
* @version 2
|
|
96
95
|
*/
|
|
97
96
|
type Column<S extends string = string, T extends ColumnProps = ColumnProps> = Expand<T & {
|
|
98
97
|
/**
|
|
@@ -111,15 +110,15 @@ type Column<S extends string = string, T extends ColumnProps = ColumnProps> = Ex
|
|
|
111
110
|
/**
|
|
112
111
|
* @public The column's input type, cached.
|
|
113
112
|
* @since 0.1.0
|
|
114
|
-
* @version
|
|
113
|
+
* @version 2
|
|
115
114
|
*/
|
|
116
|
-
|
|
115
|
+
inferInput: std.input<T['schema']>;
|
|
117
116
|
/**
|
|
118
117
|
* @public The column's output type, cached.
|
|
119
118
|
* @since 0.1.0
|
|
120
|
-
* @version
|
|
119
|
+
* @version 2
|
|
121
120
|
*/
|
|
122
|
-
|
|
121
|
+
inferOutput: std.output<T['schema']>;
|
|
123
122
|
}>;
|
|
124
123
|
/**
|
|
125
124
|
* @public A table definition.
|
|
@@ -208,28 +207,28 @@ type OmitOnUpdate = {
|
|
|
208
207
|
/**
|
|
209
208
|
* @private Infers the output type of a table's row.
|
|
210
209
|
* @since 0.1.0
|
|
211
|
-
* @version
|
|
210
|
+
* @version 2
|
|
212
211
|
*/
|
|
213
212
|
type Infer<T extends Table> = {
|
|
214
|
-
[K in keyof T['columns']]: T['columns'][K]['
|
|
213
|
+
[K in keyof T['columns']]: T['columns'][K]['inferOutput'];
|
|
215
214
|
};
|
|
216
215
|
/**
|
|
217
216
|
* @private Infers the input type required to insert a new row.
|
|
218
217
|
* @since 0.1.0
|
|
219
|
-
* @version
|
|
218
|
+
* @version 2
|
|
220
219
|
*/
|
|
221
220
|
type InferForInsert<T extends Table> = Expand<{
|
|
222
|
-
[K in keyof T['columns'] as T['columns'][K] extends OmitOnInsert | OptionalOnInsert ? never : K]: T['columns'][K]['
|
|
221
|
+
[K in keyof T['columns'] as T['columns'][K] extends OmitOnInsert | OptionalOnInsert ? never : K]: T['columns'][K]['inferInput'];
|
|
223
222
|
} & {
|
|
224
|
-
[K in keyof T['columns'] as T['columns'][K] extends OptionalOnInsert ? K : never]?: T['columns'][K]['
|
|
223
|
+
[K in keyof T['columns'] as T['columns'][K] extends OptionalOnInsert ? K : never]?: T['columns'][K]['inferInput'];
|
|
225
224
|
}>;
|
|
226
225
|
/**
|
|
227
226
|
* @private Infers the input type required to update an existing row.
|
|
228
227
|
* @since 0.1.0
|
|
229
|
-
* @version
|
|
228
|
+
* @version 2
|
|
230
229
|
*/
|
|
231
230
|
type InferForUpdate<T extends Table> = {
|
|
232
|
-
[K in keyof T['columns'] as T['columns'][K] extends OmitOnUpdate ? never : K]?: T['columns'][K]['
|
|
231
|
+
[K in keyof T['columns'] as T['columns'][K] extends OmitOnUpdate ? never : K]?: T['columns'][K]['inferInput'];
|
|
233
232
|
};
|
|
234
233
|
/**
|
|
235
234
|
* @private Adds the ability to infer types of a table.
|
|
@@ -259,7 +258,7 @@ type Inferrable<T extends Table> = T & {
|
|
|
259
258
|
/**
|
|
260
259
|
* @private A builder for column properties.
|
|
261
260
|
* @since 0.1.0
|
|
262
|
-
* @version
|
|
261
|
+
* @version 2
|
|
263
262
|
*/
|
|
264
263
|
declare class ColumnPropsBuilder<T extends ColumnProps = ColumnProps> {
|
|
265
264
|
readonly props: T;
|
|
@@ -273,17 +272,20 @@ declare class ColumnPropsBuilder<T extends ColumnProps = ColumnProps> {
|
|
|
273
272
|
autoincrement: true;
|
|
274
273
|
}>>;
|
|
275
274
|
/**
|
|
276
|
-
* @public Sets a default value for the column.
|
|
275
|
+
* @public Sets a default value for the column. This is not the
|
|
276
|
+
* table's default value for the column, but rather a
|
|
277
|
+
* fallback value that qx will use when inserting a new row
|
|
278
|
+
* where this column is nullish.
|
|
277
279
|
* @since 0.1.0
|
|
278
|
-
* @version
|
|
280
|
+
* @version 2
|
|
279
281
|
*/
|
|
280
|
-
default<S extends std.output<T['schema']
|
|
281
|
-
default:
|
|
282
|
+
default<S extends std.output<T['schema']> | (() => std.output<T['schema']>)>(value: S): ColumnPropsBuilder<Expand<T & {
|
|
283
|
+
default: S;
|
|
282
284
|
}>>;
|
|
283
285
|
/**
|
|
284
286
|
* @public Marks the column as nullable.
|
|
285
287
|
* @since 0.1.0
|
|
286
|
-
* @version
|
|
288
|
+
* @version 2
|
|
287
289
|
* @throws {Error} if the column is a primary key.
|
|
288
290
|
*/
|
|
289
291
|
nullable(): ColumnPropsBuilder<Expand<Omit<T, "schema"> & {
|
|
@@ -302,7 +304,7 @@ declare class ColumnPropsBuilder<T extends ColumnProps = ColumnProps> {
|
|
|
302
304
|
/**
|
|
303
305
|
* @public Marks the column as unique.
|
|
304
306
|
* @since 0.1.0
|
|
305
|
-
* @version
|
|
307
|
+
* @version 2
|
|
306
308
|
*/
|
|
307
309
|
unique(): ColumnPropsBuilder<Expand<T & {
|
|
308
310
|
unique: true;
|
|
@@ -982,6 +984,12 @@ interface ILogger {
|
|
|
982
984
|
* @version 1
|
|
983
985
|
*/
|
|
984
986
|
debug(sql: string, params: any[]): void;
|
|
987
|
+
/**
|
|
988
|
+
* @public Logs a query that has failed with an error.
|
|
989
|
+
* @since 0.1.12
|
|
990
|
+
* @version 1
|
|
991
|
+
*/
|
|
992
|
+
error(sql: string, params: any[], error: Error): void;
|
|
985
993
|
};
|
|
986
994
|
}
|
|
987
995
|
/**
|
|
@@ -1064,15 +1072,15 @@ declare class InsertBuilder<T extends Table> {
|
|
|
1064
1072
|
/**
|
|
1065
1073
|
* @public Adds one or more rows to be inserted.
|
|
1066
1074
|
* @since 0.1.0
|
|
1067
|
-
* @version
|
|
1075
|
+
* @version 2
|
|
1068
1076
|
*/
|
|
1069
|
-
|
|
1077
|
+
values(rows: InferForInsert<T> | InferForInsert<T>[]): this;
|
|
1070
1078
|
/**
|
|
1071
1079
|
* @public Executes the insert statement onto the given database.
|
|
1072
1080
|
* @since 0.1.0
|
|
1073
1081
|
* @version 1
|
|
1074
1082
|
*/
|
|
1075
|
-
|
|
1083
|
+
insert(db: IDatabase): Promise<Expand<Infer<T>>[]>;
|
|
1076
1084
|
}
|
|
1077
1085
|
/**
|
|
1078
1086
|
* @public Starts an insert statement for the given table.
|
|
@@ -1150,10 +1158,10 @@ type Query<T extends Record<string, Aliased<string, Table>> = Record<string, Ali
|
|
|
1150
1158
|
/**
|
|
1151
1159
|
* @private Infers the selection output type of given query.
|
|
1152
1160
|
* @since 0.1.0
|
|
1153
|
-
* @version
|
|
1161
|
+
* @version 2
|
|
1154
1162
|
*/
|
|
1155
1163
|
type InferSelection<T extends Query> = {
|
|
1156
|
-
[K in keyof T['select'] & string]: T['select'][K]['
|
|
1164
|
+
[K in keyof T['select'] & string]: T['select'][K]['inferOutput'];
|
|
1157
1165
|
};
|
|
1158
1166
|
/**
|
|
1159
1167
|
* @public Select statement builder with a fluent API.
|
|
@@ -1,46 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
2
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
3
|
+
}
|
|
4
|
+
declare namespace StandardSchemaV1 {
|
|
5
|
+
export interface Props<Input = unknown, Output = Input> {
|
|
6
|
+
readonly version: 1;
|
|
7
|
+
readonly vendor: string;
|
|
8
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
9
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
10
|
+
}
|
|
11
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
12
|
+
export interface SuccessResult<Output> {
|
|
13
|
+
readonly value: Output;
|
|
14
|
+
readonly issues?: undefined;
|
|
15
|
+
}
|
|
16
|
+
export interface FailureResult {
|
|
17
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
18
|
+
}
|
|
19
|
+
export interface Issue {
|
|
20
|
+
readonly message: string;
|
|
21
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
22
|
+
}
|
|
23
|
+
export interface PathSegment {
|
|
24
|
+
readonly key: PropertyKey;
|
|
25
|
+
}
|
|
26
|
+
export interface Types<Input = unknown, Output = Input> {
|
|
27
|
+
readonly input: Input;
|
|
28
|
+
readonly output: Output;
|
|
29
|
+
}
|
|
30
|
+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
31
|
+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
32
|
+
export {};
|
|
33
|
+
}
|
|
2
34
|
/**
|
|
3
35
|
* @public The Standard Schema interface.
|
|
4
36
|
* @since 0.1.0
|
|
5
37
|
* @version 1
|
|
6
38
|
*/
|
|
7
|
-
export { type StandardSchemaV1 as Schema }
|
|
39
|
+
export { type StandardSchemaV1 as Schema };
|
|
8
40
|
/**
|
|
9
41
|
* @public Infers the input type of a Standard Schema.
|
|
10
42
|
* @since 0.1.0
|
|
11
43
|
* @version 1
|
|
12
44
|
*/
|
|
13
|
-
export type input<T extends
|
|
45
|
+
export type input<T extends StandardSchemaV1> = StandardSchemaV1.InferInput<T>;
|
|
14
46
|
/**
|
|
15
47
|
* @public Infers the Input type of a Standard Schema.
|
|
16
48
|
* @since 0.1.0
|
|
17
49
|
* @version 1
|
|
18
50
|
*/
|
|
19
|
-
export type output<T extends
|
|
51
|
+
export type output<T extends StandardSchemaV1> = StandardSchemaV1.InferOutput<T>;
|
|
20
52
|
/**
|
|
21
53
|
* @public Use any standard schema to parse a value.
|
|
22
54
|
* @since 0.1.0
|
|
23
55
|
* @version 1
|
|
24
56
|
*/
|
|
25
|
-
export declare const parse: <T extends
|
|
57
|
+
export declare const parse: <T extends StandardSchemaV1>(schema: T, value: unknown) => StandardSchemaV1.Result<output<T>>;
|
|
26
58
|
/**
|
|
27
59
|
* @public Defines an array of a given standard schema.
|
|
28
60
|
* @since 0.1.0
|
|
29
61
|
* @version 1
|
|
30
62
|
*/
|
|
31
|
-
export type QxArray<T extends
|
|
63
|
+
export type QxArray<T extends StandardSchemaV1> = StandardSchemaV1<input<T>[], output<T>[]>;
|
|
32
64
|
/**
|
|
33
65
|
* @public Defines an array of a given standard schema.
|
|
34
66
|
* @since 0.1.0
|
|
35
67
|
* @version 1
|
|
36
68
|
*/
|
|
37
|
-
export declare const array: <T extends
|
|
69
|
+
export declare const array: <T extends StandardSchemaV1>(schema: T) => QxArray<T>;
|
|
38
70
|
/**
|
|
39
71
|
* @public Defines a standard schema for boolean values.
|
|
40
72
|
* @since 0.1.0
|
|
41
73
|
* @version 1
|
|
42
74
|
*/
|
|
43
|
-
export type QxBoolean =
|
|
75
|
+
export type QxBoolean = StandardSchemaV1<boolean, boolean>;
|
|
44
76
|
/**
|
|
45
77
|
* @public Defines a standard schema for boolean values.
|
|
46
78
|
* @since 0.1.0
|
|
@@ -54,7 +86,7 @@ export declare const boolean: () => QxBoolean;
|
|
|
54
86
|
* @since 0.1.0
|
|
55
87
|
* @version 1
|
|
56
88
|
*/
|
|
57
|
-
export type QxCoercibleDate =
|
|
89
|
+
export type QxCoercibleDate = StandardSchemaV1<Date | string | number, Date>;
|
|
58
90
|
/**
|
|
59
91
|
* @public Defines a standard schema for Date values that can be
|
|
60
92
|
* coerced from ISO 8601 strings or epoch timestamps in
|
|
@@ -69,7 +101,7 @@ export declare const date: () => QxCoercibleDate;
|
|
|
69
101
|
* @since 0.1.0
|
|
70
102
|
* @version 1
|
|
71
103
|
*/
|
|
72
|
-
export type QxInstanceOf<T> =
|
|
104
|
+
export type QxInstanceOf<T> = StandardSchemaV1<T, T>;
|
|
73
105
|
/**
|
|
74
106
|
* @public Defines a standard schema that validates instances of a
|
|
75
107
|
* given class.
|
|
@@ -83,7 +115,7 @@ export declare const instanceOf: <T>(ctor: new (...args: any[]) => T) => QxInsta
|
|
|
83
115
|
* @since 0.1.0
|
|
84
116
|
* @version 1
|
|
85
117
|
*/
|
|
86
|
-
export type QxInteger =
|
|
118
|
+
export type QxInteger = StandardSchemaV1<number, number>;
|
|
87
119
|
/**
|
|
88
120
|
* @public Defines a standard schema for integers with optional min and max constraints.
|
|
89
121
|
* @since 0.1.0
|
|
@@ -98,20 +130,20 @@ export declare const integer: ({ min, max }?: {
|
|
|
98
130
|
* @since 0.1.0
|
|
99
131
|
* @version 1
|
|
100
132
|
*/
|
|
101
|
-
export type QxNullable<T extends
|
|
133
|
+
export type QxNullable<T extends StandardSchemaV1 = StandardSchemaV1> = StandardSchemaV1<input<T> | null, output<T> | null>;
|
|
102
134
|
/**
|
|
103
135
|
* @public Makes any standard schema accepts `null` as a valid value.
|
|
104
136
|
* @since 0.1.0
|
|
105
137
|
* @version 1
|
|
106
138
|
*/
|
|
107
|
-
export declare const nullable: <T extends
|
|
139
|
+
export declare const nullable: <T extends StandardSchemaV1>(schema: T) => QxNullable<T>;
|
|
108
140
|
/**
|
|
109
141
|
* @public Defines a standard schema for numbers with optional min
|
|
110
142
|
* and max constraints.
|
|
111
143
|
* @since 0.1.0
|
|
112
144
|
* @version 1
|
|
113
145
|
*/
|
|
114
|
-
export type QxNumber =
|
|
146
|
+
export type QxNumber = StandardSchemaV1<number, number>;
|
|
115
147
|
/**
|
|
116
148
|
* @public Defines a standard schema for numbers with optional min
|
|
117
149
|
* and max constraints.
|
|
@@ -127,7 +159,7 @@ export declare const number: ({ min, max }?: {
|
|
|
127
159
|
* @since 0.1.0
|
|
128
160
|
* @version 1
|
|
129
161
|
*/
|
|
130
|
-
type QxStrictObject<T extends Record<string,
|
|
162
|
+
type QxStrictObject<T extends Record<string, StandardSchemaV1>> = StandardSchemaV1<{
|
|
131
163
|
[K in keyof T]: input<T[K]>;
|
|
132
164
|
}, {
|
|
133
165
|
[K in keyof T]: output<T[K]>;
|
|
@@ -137,14 +169,14 @@ type QxStrictObject<T extends Record<string, std.StandardSchemaV1>> = std.Standa
|
|
|
137
169
|
* @since 0.1.0
|
|
138
170
|
* @version 1
|
|
139
171
|
*/
|
|
140
|
-
export declare const strictObject: <T extends Record<string,
|
|
172
|
+
export declare const strictObject: <T extends Record<string, StandardSchemaV1>>(shape: T) => QxStrictObject<T>;
|
|
141
173
|
/**
|
|
142
174
|
* @public Defines a standard schema for strings with optional min
|
|
143
175
|
* and max length constraints.
|
|
144
176
|
* @since 0.1.0
|
|
145
177
|
* @version 1
|
|
146
178
|
*/
|
|
147
|
-
export type QxString =
|
|
179
|
+
export type QxString = StandardSchemaV1<string, string>;
|
|
148
180
|
/**
|
|
149
181
|
* @public Defines a standard schema for strings with optional min
|
|
150
182
|
* and max length constraints.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rwillians/qx",
|
|
3
3
|
"description": "A teeny tiny ORM for SQLite.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.12",
|
|
5
5
|
"author": "Rafael Willians <me@rwillians.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -54,10 +54,6 @@
|
|
|
54
54
|
"build:cjs": "bun run --bun tsc --project tsconfig-cjs.json",
|
|
55
55
|
"build:esm": "bun run --bun tsc --project tsconfig-esm.json"
|
|
56
56
|
},
|
|
57
|
-
"dependencies": {
|
|
58
|
-
"@standard-schema/spec": "^1.0.0",
|
|
59
|
-
"sql-highlight": "^6.1.0"
|
|
60
|
-
},
|
|
61
57
|
"devDependencies": {
|
|
62
58
|
"@types/bun": "latest"
|
|
63
59
|
},
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createPrettyLogger = void 0;
|
|
4
|
-
const node_util_1 = require("node:util");
|
|
5
|
-
const sql_highlight_1 = require("sql-highlight");
|
|
6
|
-
/**
|
|
7
|
-
* @public Creates a basic console logger that logs all queries.
|
|
8
|
-
* @since 0.1.0
|
|
9
|
-
* @version 1
|
|
10
|
-
*/
|
|
11
|
-
const createPrettyLogger = () => ({
|
|
12
|
-
query: {
|
|
13
|
-
debug: (sql, params) => { process.stdout.write((0, sql_highlight_1.highlight)(sql) + ' ' + (0, node_util_1.inspect)(params, false, null, true) + '\n'); },
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
exports.createPrettyLogger = createPrettyLogger;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { inspect } from 'node:util';
|
|
2
|
-
import { highlight } from 'sql-highlight';
|
|
3
|
-
import {} from './index';
|
|
4
|
-
/**
|
|
5
|
-
* @public Creates a basic console logger that logs all queries.
|
|
6
|
-
* @since 0.1.0
|
|
7
|
-
* @version 1
|
|
8
|
-
*/
|
|
9
|
-
export const createPrettyLogger = () => ({
|
|
10
|
-
query: {
|
|
11
|
-
debug: (sql, params) => { process.stdout.write(highlight(sql) + ' ' + inspect(params, false, null, true) + '\n'); },
|
|
12
|
-
},
|
|
13
|
-
});
|