@lobomfz/db 0.3.4 → 0.3.6
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/package.json +1 -1
- package/src/database.ts +36 -9
- package/src/index.ts +1 -1
package/package.json
CHANGED
package/src/database.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { Database as BunDatabase } from "bun:sqlite";
|
|
2
|
+
|
|
3
|
+
import type { Type } from "arktype";
|
|
2
4
|
import { Kysely, ParseJSONResultsPlugin } from "kysely";
|
|
5
|
+
|
|
3
6
|
import { BunSqliteDialect } from "./dialect/dialect";
|
|
4
|
-
import type { Type } from "arktype";
|
|
5
|
-
import type { GeneratedPreset } from "./generated";
|
|
6
7
|
import type { DbFieldMeta } from "./env";
|
|
8
|
+
import type { GeneratedPreset } from "./generated";
|
|
9
|
+
import { Differ, type DesiredTable } from "./migration/diff";
|
|
10
|
+
import { Executor } from "./migration/execute";
|
|
11
|
+
import { Introspector } from "./migration/introspect";
|
|
7
12
|
import { DeserializePlugin, type ColumnCoercion, type ColumnsMap } from "./plugin";
|
|
8
13
|
import type {
|
|
9
14
|
DatabaseOptions,
|
|
@@ -12,9 +17,6 @@ import type {
|
|
|
12
17
|
TablesFromSchemas,
|
|
13
18
|
DatabasePragmas,
|
|
14
19
|
} from "./types";
|
|
15
|
-
import { Introspector } from "./migration/introspect";
|
|
16
|
-
import { Differ, type DesiredTable } from "./migration/diff";
|
|
17
|
-
import { Executor } from "./migration/execute";
|
|
18
20
|
|
|
19
21
|
type ArkBranch = {
|
|
20
22
|
domain?: string;
|
|
@@ -43,6 +45,7 @@ type Prop = {
|
|
|
43
45
|
isBoolean?: boolean;
|
|
44
46
|
isInteger?: boolean;
|
|
45
47
|
isDate?: boolean;
|
|
48
|
+
isBlob?: boolean;
|
|
46
49
|
isJson?: boolean;
|
|
47
50
|
jsonSchema?: Type;
|
|
48
51
|
meta?: DbFieldMeta;
|
|
@@ -83,7 +86,10 @@ export class Database<T extends SchemaRecord> {
|
|
|
83
86
|
|
|
84
87
|
this.kysely = new Kysely<TablesFromSchemas<T>>({
|
|
85
88
|
dialect: new BunSqliteDialect({ database: this.sqlite }),
|
|
86
|
-
plugins: [
|
|
89
|
+
plugins: [
|
|
90
|
+
new DeserializePlugin(this.columns, this.tableColumns, validation),
|
|
91
|
+
new ParseJSONResultsPlugin(),
|
|
92
|
+
],
|
|
87
93
|
});
|
|
88
94
|
}
|
|
89
95
|
|
|
@@ -118,6 +124,18 @@ export class Database<T extends SchemaRecord> {
|
|
|
118
124
|
return { key, kind, nullable, isDate: true, generated, defaultValue };
|
|
119
125
|
}
|
|
120
126
|
|
|
127
|
+
if (v.proto === Uint8Array || nonNull.some((b) => b.proto === Uint8Array)) {
|
|
128
|
+
return {
|
|
129
|
+
key,
|
|
130
|
+
kind,
|
|
131
|
+
nullable,
|
|
132
|
+
isBlob: true,
|
|
133
|
+
meta: v.meta,
|
|
134
|
+
generated,
|
|
135
|
+
defaultValue,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
121
139
|
if (nonNull.length > 0 && nonNull.every((b) => b.domain === "boolean")) {
|
|
122
140
|
return { key, kind, nullable, isBoolean: true, generated, defaultValue };
|
|
123
141
|
}
|
|
@@ -154,6 +172,10 @@ export class Database<T extends SchemaRecord> {
|
|
|
154
172
|
return "TEXT";
|
|
155
173
|
}
|
|
156
174
|
|
|
175
|
+
if (prop.isBlob) {
|
|
176
|
+
return "BLOB";
|
|
177
|
+
}
|
|
178
|
+
|
|
157
179
|
if (prop.isDate || prop.isBoolean || prop.isInteger) {
|
|
158
180
|
return "INTEGER";
|
|
159
181
|
}
|
|
@@ -328,8 +350,8 @@ export class Database<T extends SchemaRecord> {
|
|
|
328
350
|
type: this.sqlType(prop),
|
|
329
351
|
notnull: isNotNull,
|
|
330
352
|
defaultValue: defaultClause
|
|
331
|
-
|
|
332
|
-
|
|
353
|
+
? defaultClause.replace("DEFAULT ", "").replace(/^\((.+)\)$/, "$1")
|
|
354
|
+
: null,
|
|
333
355
|
unique: !!prop.meta?.unique,
|
|
334
356
|
references: prop.meta?.references ?? null,
|
|
335
357
|
onDelete: prop.meta?.onDelete?.toUpperCase() ?? null,
|
|
@@ -342,7 +364,12 @@ export class Database<T extends SchemaRecord> {
|
|
|
342
364
|
sql: this.generateCreateIndexSQL(name, indexDef),
|
|
343
365
|
}));
|
|
344
366
|
|
|
345
|
-
desiredTables.push({
|
|
367
|
+
desiredTables.push({
|
|
368
|
+
name,
|
|
369
|
+
sql: this.generateCreateTableSQL(name, props),
|
|
370
|
+
columns,
|
|
371
|
+
indexes,
|
|
372
|
+
});
|
|
346
373
|
}
|
|
347
374
|
|
|
348
375
|
const existing = new Introspector(this.sqlite).introspect();
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { Database } from "./database";
|
|
2
2
|
export { generated, type GeneratedPreset } from "./generated";
|
|
3
3
|
export { JsonParseError } from "./errors";
|
|
4
|
-
export { sql, type Selectable, type Insertable, type Updateable } from "kysely";
|
|
4
|
+
export { sql, type Selectable, type Insertable, type Updateable, type Kysely } from "kysely";
|
|
5
5
|
export { type } from "arktype";
|
|
6
6
|
export { JsonValidationError } from "./validation-error";
|
|
7
7
|
export type { DbFieldMeta } from "./env";
|