@astrojs/db 0.0.0-edge-nested-20240223135627
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/LICENSE +59 -0
- package/config-augment.d.ts +4 -0
- package/dist/core/cli/commands/gen/index.d.ts +6 -0
- package/dist/core/cli/commands/gen/index.js +39 -0
- package/dist/core/cli/commands/link/index.d.ts +8 -0
- package/dist/core/cli/commands/link/index.js +78 -0
- package/dist/core/cli/commands/login/index.d.ts +6 -0
- package/dist/core/cli/commands/login/index.js +46 -0
- package/dist/core/cli/commands/logout/index.d.ts +6 -0
- package/dist/core/cli/commands/logout/index.js +9 -0
- package/dist/core/cli/commands/push/index.d.ts +6 -0
- package/dist/core/cli/commands/push/index.js +209 -0
- package/dist/core/cli/commands/shell/index.d.ts +6 -0
- package/dist/core/cli/commands/shell/index.js +15 -0
- package/dist/core/cli/commands/verify/index.d.ts +6 -0
- package/dist/core/cli/commands/verify/index.js +43 -0
- package/dist/core/cli/index.d.ts +6 -0
- package/dist/core/cli/index.js +68 -0
- package/dist/core/cli/migration-queries.d.ts +26 -0
- package/dist/core/cli/migration-queries.js +418 -0
- package/dist/core/cli/migrations.d.ts +34 -0
- package/dist/core/cli/migrations.js +129 -0
- package/dist/core/consts.d.ts +6 -0
- package/dist/core/consts.js +17 -0
- package/dist/core/errors.d.ts +7 -0
- package/dist/core/errors.js +54 -0
- package/dist/core/integration/error-map.d.ts +6 -0
- package/dist/core/integration/error-map.js +79 -0
- package/dist/core/integration/file-url.d.ts +2 -0
- package/dist/core/integration/file-url.js +84 -0
- package/dist/core/integration/index.d.ts +2 -0
- package/dist/core/integration/index.js +173 -0
- package/dist/core/integration/load-astro-config.d.ts +6 -0
- package/dist/core/integration/load-astro-config.js +79 -0
- package/dist/core/integration/typegen.d.ts +5 -0
- package/dist/core/integration/typegen.js +41 -0
- package/dist/core/integration/vite-plugin-db.d.ts +25 -0
- package/dist/core/integration/vite-plugin-db.js +73 -0
- package/dist/core/integration/vite-plugin-inject-env-ts.d.ts +11 -0
- package/dist/core/integration/vite-plugin-inject-env-ts.js +53 -0
- package/dist/core/queries.d.ts +78 -0
- package/dist/core/queries.js +218 -0
- package/dist/core/tokens.d.ts +11 -0
- package/dist/core/tokens.js +131 -0
- package/dist/core/types.d.ts +8059 -0
- package/dist/core/types.js +209 -0
- package/dist/core/utils.d.ts +5 -0
- package/dist/core/utils.js +18 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +16 -0
- package/dist/runtime/db-client.d.ts +12 -0
- package/dist/runtime/db-client.js +96 -0
- package/dist/runtime/drizzle.d.ts +1 -0
- package/dist/runtime/drizzle.js +48 -0
- package/dist/runtime/index.d.ts +30 -0
- package/dist/runtime/index.js +124 -0
- package/dist/runtime/types.d.ts +69 -0
- package/dist/runtime/types.js +8 -0
- package/index.d.ts +3 -0
- package/package.json +81 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
|
|
2
|
+
import { collectionToTable } from "../runtime/index.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { SQL } from "drizzle-orm";
|
|
5
|
+
import { errorMap } from "./integration/error-map.js";
|
|
6
|
+
import { SERIALIZED_SQL_KEY } from "../runtime/types.js";
|
|
7
|
+
const sqlite = new SQLiteAsyncDialect();
|
|
8
|
+
const sqlSchema = z.instanceof(SQL).transform(
|
|
9
|
+
(sqlObj) => ({
|
|
10
|
+
[SERIALIZED_SQL_KEY]: true,
|
|
11
|
+
sql: sqlite.sqlToQuery(sqlObj).sql
|
|
12
|
+
})
|
|
13
|
+
);
|
|
14
|
+
const baseColumnSchema = z.object({
|
|
15
|
+
label: z.string().optional(),
|
|
16
|
+
optional: z.boolean().optional().default(false),
|
|
17
|
+
unique: z.boolean().optional().default(false),
|
|
18
|
+
// Defined when `defineReadableTable()` is called
|
|
19
|
+
name: z.string().optional(),
|
|
20
|
+
collection: z.string().optional()
|
|
21
|
+
});
|
|
22
|
+
const booleanColumnSchema = z.object({
|
|
23
|
+
type: z.literal("boolean"),
|
|
24
|
+
schema: baseColumnSchema.extend({
|
|
25
|
+
default: z.union([z.boolean(), sqlSchema]).optional()
|
|
26
|
+
})
|
|
27
|
+
});
|
|
28
|
+
const numberColumnBaseSchema = baseColumnSchema.omit({ optional: true }).and(
|
|
29
|
+
z.union([
|
|
30
|
+
z.object({
|
|
31
|
+
primaryKey: z.literal(false).optional().default(false),
|
|
32
|
+
optional: baseColumnSchema.shape.optional,
|
|
33
|
+
default: z.union([z.number(), sqlSchema]).optional()
|
|
34
|
+
}),
|
|
35
|
+
z.object({
|
|
36
|
+
// `integer primary key` uses ROWID as the default value.
|
|
37
|
+
// `optional` and `default` do not have an effect,
|
|
38
|
+
// so disable these config options for primary keys.
|
|
39
|
+
primaryKey: z.literal(true),
|
|
40
|
+
optional: z.literal(false).optional(),
|
|
41
|
+
default: z.literal(void 0).optional()
|
|
42
|
+
})
|
|
43
|
+
])
|
|
44
|
+
);
|
|
45
|
+
const numberColumnOptsSchema = numberColumnBaseSchema.and(
|
|
46
|
+
z.object({
|
|
47
|
+
references: z.function().returns(z.lazy(() => numberColumnSchema)).optional().transform((fn) => fn?.())
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
const numberColumnSchema = z.object({
|
|
51
|
+
type: z.literal("number"),
|
|
52
|
+
schema: numberColumnOptsSchema
|
|
53
|
+
});
|
|
54
|
+
const textColumnBaseSchema = baseColumnSchema.omit({ optional: true }).extend({
|
|
55
|
+
default: z.union([z.string(), sqlSchema]).optional(),
|
|
56
|
+
multiline: z.boolean().optional()
|
|
57
|
+
}).and(
|
|
58
|
+
z.union([
|
|
59
|
+
z.object({
|
|
60
|
+
primaryKey: z.literal(false).optional().default(false),
|
|
61
|
+
optional: baseColumnSchema.shape.optional
|
|
62
|
+
}),
|
|
63
|
+
z.object({
|
|
64
|
+
// text primary key allows NULL values.
|
|
65
|
+
// NULL values bypass unique checks, which could
|
|
66
|
+
// lead to duplicate URLs per record in Astro Studio.
|
|
67
|
+
// disable `optional` for primary keys.
|
|
68
|
+
primaryKey: z.literal(true),
|
|
69
|
+
optional: z.literal(false).optional()
|
|
70
|
+
})
|
|
71
|
+
])
|
|
72
|
+
);
|
|
73
|
+
const textColumnOptsSchema = textColumnBaseSchema.and(
|
|
74
|
+
z.object({
|
|
75
|
+
references: z.function().returns(z.lazy(() => textColumnSchema)).optional().transform((fn) => fn?.())
|
|
76
|
+
})
|
|
77
|
+
);
|
|
78
|
+
const textColumnSchema = z.object({
|
|
79
|
+
type: z.literal("text"),
|
|
80
|
+
schema: textColumnOptsSchema
|
|
81
|
+
});
|
|
82
|
+
const dateColumnSchema = z.object({
|
|
83
|
+
type: z.literal("date"),
|
|
84
|
+
schema: baseColumnSchema.extend({
|
|
85
|
+
default: z.union([
|
|
86
|
+
sqlSchema,
|
|
87
|
+
// transform to ISO string for serialization
|
|
88
|
+
z.date().transform((d) => d.toISOString())
|
|
89
|
+
]).optional()
|
|
90
|
+
})
|
|
91
|
+
});
|
|
92
|
+
const jsonColumnSchema = z.object({
|
|
93
|
+
type: z.literal("json"),
|
|
94
|
+
schema: baseColumnSchema.extend({
|
|
95
|
+
default: z.unknown().optional()
|
|
96
|
+
})
|
|
97
|
+
});
|
|
98
|
+
const columnSchema = z.union([
|
|
99
|
+
booleanColumnSchema,
|
|
100
|
+
numberColumnSchema,
|
|
101
|
+
textColumnSchema,
|
|
102
|
+
dateColumnSchema,
|
|
103
|
+
jsonColumnSchema
|
|
104
|
+
]);
|
|
105
|
+
const referenceableColumnSchema = z.union([textColumnSchema, numberColumnSchema]);
|
|
106
|
+
const columnsSchema = z.record(columnSchema);
|
|
107
|
+
const indexSchema = z.object({
|
|
108
|
+
on: z.string().or(z.array(z.string())),
|
|
109
|
+
unique: z.boolean().optional()
|
|
110
|
+
});
|
|
111
|
+
const foreignKeysSchema = z.object({
|
|
112
|
+
columns: z.string().or(z.array(z.string())),
|
|
113
|
+
references: z.function().returns(z.lazy(() => referenceableColumnSchema.or(z.array(referenceableColumnSchema)))).transform((fn) => fn())
|
|
114
|
+
});
|
|
115
|
+
const baseCollectionSchema = z.object({
|
|
116
|
+
columns: columnsSchema,
|
|
117
|
+
indexes: z.record(indexSchema).optional(),
|
|
118
|
+
foreignKeys: z.array(foreignKeysSchema).optional()
|
|
119
|
+
});
|
|
120
|
+
const readableCollectionSchema = baseCollectionSchema.extend({
|
|
121
|
+
writable: z.literal(false)
|
|
122
|
+
});
|
|
123
|
+
const writableCollectionSchema = baseCollectionSchema.extend({
|
|
124
|
+
writable: z.literal(true)
|
|
125
|
+
});
|
|
126
|
+
const collectionSchema = z.union([readableCollectionSchema, writableCollectionSchema]);
|
|
127
|
+
const tablesSchema = z.preprocess((rawCollections) => {
|
|
128
|
+
const tables = z.record(z.any()).parse(rawCollections, { errorMap });
|
|
129
|
+
for (const [collectionName, collection] of Object.entries(tables)) {
|
|
130
|
+
collection.table = collectionToTable(
|
|
131
|
+
collectionName,
|
|
132
|
+
collectionSchema.parse(collection, { errorMap })
|
|
133
|
+
);
|
|
134
|
+
const { columns } = z.object({ columns: z.record(z.any()) }).parse(collection, { errorMap });
|
|
135
|
+
for (const [columnName, column2] of Object.entries(columns)) {
|
|
136
|
+
column2.schema.name = columnName;
|
|
137
|
+
column2.schema.collection = collectionName;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return rawCollections;
|
|
141
|
+
}, z.record(collectionSchema));
|
|
142
|
+
function defineData(fn) {
|
|
143
|
+
return fn;
|
|
144
|
+
}
|
|
145
|
+
const dbDataFn = z.function().returns(z.union([z.void(), z.promise(z.void())]));
|
|
146
|
+
const dbConfigSchema = z.object({
|
|
147
|
+
studio: z.boolean().optional(),
|
|
148
|
+
tables: tablesSchema.optional(),
|
|
149
|
+
data: z.union([dbDataFn, z.array(dbDataFn)]).optional(),
|
|
150
|
+
unsafeWritable: z.boolean().optional().default(false)
|
|
151
|
+
});
|
|
152
|
+
const astroConfigWithDbSchema = z.object({
|
|
153
|
+
db: dbConfigSchema.optional()
|
|
154
|
+
});
|
|
155
|
+
function baseDefineCollection(userConfig, writable) {
|
|
156
|
+
return {
|
|
157
|
+
...userConfig,
|
|
158
|
+
writable,
|
|
159
|
+
// set at runtime to get the table name
|
|
160
|
+
table: null
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function defineReadableTable(userConfig) {
|
|
164
|
+
return baseDefineCollection(userConfig, false);
|
|
165
|
+
}
|
|
166
|
+
function defineWritableTable(userConfig) {
|
|
167
|
+
return baseDefineCollection(userConfig, true);
|
|
168
|
+
}
|
|
169
|
+
function createColumn(type, schema) {
|
|
170
|
+
return {
|
|
171
|
+
type,
|
|
172
|
+
/**
|
|
173
|
+
* @internal
|
|
174
|
+
*/
|
|
175
|
+
schema
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const column = {
|
|
179
|
+
number: (opts = {}) => {
|
|
180
|
+
return createColumn("number", opts);
|
|
181
|
+
},
|
|
182
|
+
boolean: (opts = {}) => {
|
|
183
|
+
return createColumn("boolean", opts);
|
|
184
|
+
},
|
|
185
|
+
text: (opts = {}) => {
|
|
186
|
+
return createColumn("text", opts);
|
|
187
|
+
},
|
|
188
|
+
date(opts = {}) {
|
|
189
|
+
return createColumn("date", opts);
|
|
190
|
+
},
|
|
191
|
+
json(opts = {}) {
|
|
192
|
+
return createColumn("json", opts);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
export {
|
|
196
|
+
astroConfigWithDbSchema,
|
|
197
|
+
collectionSchema,
|
|
198
|
+
column,
|
|
199
|
+
columnSchema,
|
|
200
|
+
dbConfigSchema,
|
|
201
|
+
defineData,
|
|
202
|
+
defineReadableTable,
|
|
203
|
+
defineWritableTable,
|
|
204
|
+
indexSchema,
|
|
205
|
+
readableCollectionSchema,
|
|
206
|
+
referenceableColumnSchema,
|
|
207
|
+
tablesSchema,
|
|
208
|
+
writableCollectionSchema
|
|
209
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AstroConfig } from 'astro';
|
|
2
|
+
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
|
3
|
+
export declare function getAstroStudioEnv(envMode?: string): Record<`ASTRO_STUDIO_${string}`, string>;
|
|
4
|
+
export declare function getRemoteDatabaseUrl(): string;
|
|
5
|
+
export declare function getAstroStudioUrl(): string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { loadEnv } from "vite";
|
|
2
|
+
function getAstroStudioEnv(envMode = "") {
|
|
3
|
+
const env = loadEnv(envMode, process.cwd(), "ASTRO_STUDIO_");
|
|
4
|
+
return env;
|
|
5
|
+
}
|
|
6
|
+
function getRemoteDatabaseUrl() {
|
|
7
|
+
const env = getAstroStudioEnv();
|
|
8
|
+
return env.ASTRO_STUDIO_REMOTE_DB_URL || "https://db.services.astro.build";
|
|
9
|
+
}
|
|
10
|
+
function getAstroStudioUrl() {
|
|
11
|
+
const env = getAstroStudioEnv();
|
|
12
|
+
return env.ASTRO_STUDIO_URL || "https://stardate.astro.build";
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
getAstroStudioEnv,
|
|
16
|
+
getAstroStudioUrl,
|
|
17
|
+
getRemoteDatabaseUrl
|
|
18
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { defineReadableTable, defineWritableTable, defineData, column } from './core/types.js';
|
|
2
|
+
export type { ResolvedCollectionConfig, DBDataContext } from './core/types.js';
|
|
3
|
+
export { cli } from './core/cli/index.js';
|
|
4
|
+
export { integration as default } from './core/integration/index.js';
|
|
5
|
+
export { sql, NOW, TRUE, FALSE } from './runtime/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { defineReadableTable, defineWritableTable, defineData, column } from "./core/types.js";
|
|
2
|
+
import { cli } from "./core/cli/index.js";
|
|
3
|
+
import { integration } from "./core/integration/index.js";
|
|
4
|
+
import { sql, NOW, TRUE, FALSE } from "./runtime/index.js";
|
|
5
|
+
export {
|
|
6
|
+
FALSE,
|
|
7
|
+
NOW,
|
|
8
|
+
TRUE,
|
|
9
|
+
cli,
|
|
10
|
+
column,
|
|
11
|
+
integration as default,
|
|
12
|
+
defineData,
|
|
13
|
+
defineReadableTable,
|
|
14
|
+
defineWritableTable,
|
|
15
|
+
sql
|
|
16
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { type DBTables } from '../core/types.js';
|
|
3
|
+
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
4
|
+
interface LocalDatabaseClient extends LibSQLDatabase, Disposable {
|
|
5
|
+
}
|
|
6
|
+
export declare function createLocalDatabaseClient({ tables, dbUrl, seeding, }: {
|
|
7
|
+
dbUrl: string;
|
|
8
|
+
tables: DBTables;
|
|
9
|
+
seeding: boolean;
|
|
10
|
+
}): Promise<LocalDatabaseClient>;
|
|
11
|
+
export declare function createRemoteDatabaseClient(appToken: string, remoteDbURL: string): import("drizzle-orm/sqlite-proxy").SqliteRemoteDatabase<Record<string, never>>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { createClient } from "@libsql/client";
|
|
2
|
+
import {} from "../core/types.js";
|
|
3
|
+
import { drizzle as drizzleLibsql } from "drizzle-orm/libsql";
|
|
4
|
+
import { drizzle as drizzleProxy } from "drizzle-orm/sqlite-proxy";
|
|
5
|
+
import {} from "drizzle-orm/sqlite-core";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { getTableName } from "drizzle-orm";
|
|
8
|
+
const isWebContainer = !!process.versions?.webcontainer;
|
|
9
|
+
async function createLocalDatabaseClient({
|
|
10
|
+
tables,
|
|
11
|
+
dbUrl,
|
|
12
|
+
seeding
|
|
13
|
+
}) {
|
|
14
|
+
const url = isWebContainer ? "file:content.db" : dbUrl;
|
|
15
|
+
const client = createClient({ url });
|
|
16
|
+
const db = Object.assign(drizzleLibsql(client), {
|
|
17
|
+
[Symbol.dispose || Symbol.for("Symbol.dispose")]() {
|
|
18
|
+
client.close();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
if (seeding)
|
|
22
|
+
return db;
|
|
23
|
+
const { insert: drizzleInsert, update: drizzleUpdate, delete: drizzleDelete } = db;
|
|
24
|
+
return Object.assign(db, {
|
|
25
|
+
insert(Table) {
|
|
26
|
+
checkIfModificationIsAllowed(tables, Table);
|
|
27
|
+
return drizzleInsert.call(this, Table);
|
|
28
|
+
},
|
|
29
|
+
update(Table) {
|
|
30
|
+
checkIfModificationIsAllowed(tables, Table);
|
|
31
|
+
return drizzleUpdate.call(this, Table);
|
|
32
|
+
},
|
|
33
|
+
delete(Table) {
|
|
34
|
+
checkIfModificationIsAllowed(tables, Table);
|
|
35
|
+
return drizzleDelete.call(this, Table);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function checkIfModificationIsAllowed(tables, Table) {
|
|
40
|
+
const tableName = getTableName(Table);
|
|
41
|
+
const collection = tables[tableName];
|
|
42
|
+
if (!collection.writable) {
|
|
43
|
+
throw new Error(`The [${tableName}] collection is read-only.`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function createRemoteDatabaseClient(appToken, remoteDbURL) {
|
|
47
|
+
const url = new URL("/db/query", remoteDbURL);
|
|
48
|
+
const db = drizzleProxy(async (sql, parameters, method) => {
|
|
49
|
+
const requestBody = { sql, args: parameters };
|
|
50
|
+
console.info(JSON.stringify(requestBody));
|
|
51
|
+
const res = await fetch(url, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${appToken}`,
|
|
55
|
+
"Content-Type": "application/json"
|
|
56
|
+
},
|
|
57
|
+
body: JSON.stringify(requestBody)
|
|
58
|
+
});
|
|
59
|
+
if (!res.ok) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Failed to execute query.
|
|
62
|
+
Query: ${sql}
|
|
63
|
+
Full error: ${res.status} ${await res.text()}}`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
const queryResultSchema = z.object({
|
|
67
|
+
rows: z.array(z.unknown())
|
|
68
|
+
});
|
|
69
|
+
let rows;
|
|
70
|
+
try {
|
|
71
|
+
const json = await res.json();
|
|
72
|
+
rows = queryResultSchema.parse(json).rows;
|
|
73
|
+
} catch (e) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Failed to execute query.
|
|
76
|
+
Query: ${sql}
|
|
77
|
+
Full error: Unexpected JSON response. ${e instanceof Error ? e.message : String(e)}`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const rowValues = [];
|
|
81
|
+
for (const row of rows) {
|
|
82
|
+
if (row != null && typeof row === "object") {
|
|
83
|
+
rowValues.push(Object.values(row));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (method === "get") {
|
|
87
|
+
return { rows: rowValues[0] };
|
|
88
|
+
}
|
|
89
|
+
return { rows: rowValues };
|
|
90
|
+
});
|
|
91
|
+
return db;
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
createLocalDatabaseClient,
|
|
95
|
+
createRemoteDatabaseClient
|
|
96
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sql, eq, gt, gte, lt, lte, ne, isNull, isNotNull, inArray, notInArray, exists, notExists, between, notBetween, like, notIlike, not, asc, desc, and, or, } from 'drizzle-orm';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sql,
|
|
3
|
+
eq,
|
|
4
|
+
gt,
|
|
5
|
+
gte,
|
|
6
|
+
lt,
|
|
7
|
+
lte,
|
|
8
|
+
ne,
|
|
9
|
+
isNull,
|
|
10
|
+
isNotNull,
|
|
11
|
+
inArray,
|
|
12
|
+
notInArray,
|
|
13
|
+
exists,
|
|
14
|
+
notExists,
|
|
15
|
+
between,
|
|
16
|
+
notBetween,
|
|
17
|
+
like,
|
|
18
|
+
notIlike,
|
|
19
|
+
not,
|
|
20
|
+
asc,
|
|
21
|
+
desc,
|
|
22
|
+
and,
|
|
23
|
+
or
|
|
24
|
+
} from "drizzle-orm";
|
|
25
|
+
export {
|
|
26
|
+
and,
|
|
27
|
+
asc,
|
|
28
|
+
between,
|
|
29
|
+
desc,
|
|
30
|
+
eq,
|
|
31
|
+
exists,
|
|
32
|
+
gt,
|
|
33
|
+
gte,
|
|
34
|
+
inArray,
|
|
35
|
+
isNotNull,
|
|
36
|
+
isNull,
|
|
37
|
+
like,
|
|
38
|
+
lt,
|
|
39
|
+
lte,
|
|
40
|
+
ne,
|
|
41
|
+
not,
|
|
42
|
+
notBetween,
|
|
43
|
+
notExists,
|
|
44
|
+
notIlike,
|
|
45
|
+
notInArray,
|
|
46
|
+
or,
|
|
47
|
+
sql
|
|
48
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
|
|
2
|
+
import { type DBTable, type DBColumn } from '../core/types.js';
|
|
3
|
+
import { type ColumnDataType, sql } from 'drizzle-orm';
|
|
4
|
+
export { sql };
|
|
5
|
+
export type SqliteDB = SqliteRemoteDatabase;
|
|
6
|
+
export type { Table } from './types.js';
|
|
7
|
+
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
|
|
8
|
+
export declare function hasPrimaryKey(column: DBColumn): boolean;
|
|
9
|
+
export declare const NOW: import("drizzle-orm").SQL<unknown>;
|
|
10
|
+
export declare const TRUE: import("drizzle-orm").SQL<unknown>;
|
|
11
|
+
export declare const FALSE: import("drizzle-orm").SQL<unknown>;
|
|
12
|
+
export declare function collectionToTable(name: string, collection: DBTable): import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
13
|
+
name: string;
|
|
14
|
+
schema: undefined;
|
|
15
|
+
columns: {
|
|
16
|
+
[x: string]: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
17
|
+
name: string;
|
|
18
|
+
tableName: string;
|
|
19
|
+
dataType: ColumnDataType;
|
|
20
|
+
columnType: string;
|
|
21
|
+
data: unknown;
|
|
22
|
+
driverParam: unknown;
|
|
23
|
+
notNull: false;
|
|
24
|
+
hasDefault: false;
|
|
25
|
+
enumValues: string[] | undefined;
|
|
26
|
+
baseColumn: never;
|
|
27
|
+
}, object>;
|
|
28
|
+
};
|
|
29
|
+
dialect: "sqlite";
|
|
30
|
+
}>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {} from "../core/types.js";
|
|
2
|
+
import { sql } from "drizzle-orm";
|
|
3
|
+
import {
|
|
4
|
+
customType,
|
|
5
|
+
integer,
|
|
6
|
+
sqliteTable,
|
|
7
|
+
text,
|
|
8
|
+
index
|
|
9
|
+
} from "drizzle-orm/sqlite-core";
|
|
10
|
+
import { isSerializedSQL } from "./types.js";
|
|
11
|
+
import { createRemoteDatabaseClient, createLocalDatabaseClient } from "./db-client.js";
|
|
12
|
+
function hasPrimaryKey(column) {
|
|
13
|
+
return "primaryKey" in column.schema && !!column.schema.primaryKey;
|
|
14
|
+
}
|
|
15
|
+
const NOW = sql`CURRENT_TIMESTAMP`;
|
|
16
|
+
const TRUE = sql`TRUE`;
|
|
17
|
+
const FALSE = sql`FALSE`;
|
|
18
|
+
const dateType = customType({
|
|
19
|
+
dataType() {
|
|
20
|
+
return "text";
|
|
21
|
+
},
|
|
22
|
+
toDriver(value) {
|
|
23
|
+
return value.toISOString();
|
|
24
|
+
},
|
|
25
|
+
fromDriver(value) {
|
|
26
|
+
return new Date(value);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
const jsonType = customType({
|
|
30
|
+
dataType() {
|
|
31
|
+
return "text";
|
|
32
|
+
},
|
|
33
|
+
toDriver(value) {
|
|
34
|
+
return JSON.stringify(value);
|
|
35
|
+
},
|
|
36
|
+
fromDriver(value) {
|
|
37
|
+
return JSON.parse(value);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
function collectionToTable(name, collection) {
|
|
41
|
+
const columns = {};
|
|
42
|
+
if (!Object.entries(collection.columns).some(([, column]) => hasPrimaryKey(column))) {
|
|
43
|
+
columns["_id"] = integer("_id").primaryKey();
|
|
44
|
+
}
|
|
45
|
+
for (const [columnName, column] of Object.entries(collection.columns)) {
|
|
46
|
+
columns[columnName] = columnMapper(columnName, column);
|
|
47
|
+
}
|
|
48
|
+
const table = sqliteTable(name, columns, (ormTable) => {
|
|
49
|
+
const indexes = {};
|
|
50
|
+
for (const [indexName, indexProps] of Object.entries(collection.indexes ?? {})) {
|
|
51
|
+
const onColNames = Array.isArray(indexProps.on) ? indexProps.on : [indexProps.on];
|
|
52
|
+
const onCols = onColNames.map((colName) => ormTable[colName]);
|
|
53
|
+
if (!atLeastOne(onCols))
|
|
54
|
+
continue;
|
|
55
|
+
indexes[indexName] = index(indexName).on(...onCols);
|
|
56
|
+
}
|
|
57
|
+
return indexes;
|
|
58
|
+
});
|
|
59
|
+
return table;
|
|
60
|
+
}
|
|
61
|
+
function atLeastOne(arr) {
|
|
62
|
+
return arr.length > 0;
|
|
63
|
+
}
|
|
64
|
+
function columnMapper(columnName, column) {
|
|
65
|
+
let c;
|
|
66
|
+
switch (column.type) {
|
|
67
|
+
case "text": {
|
|
68
|
+
c = text(columnName);
|
|
69
|
+
if (column.schema.default !== void 0)
|
|
70
|
+
c = c.default(handleSerializedSQL(column.schema.default));
|
|
71
|
+
if (column.schema.primaryKey === true)
|
|
72
|
+
c = c.primaryKey();
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case "number": {
|
|
76
|
+
c = integer(columnName);
|
|
77
|
+
if (column.schema.default !== void 0)
|
|
78
|
+
c = c.default(handleSerializedSQL(column.schema.default));
|
|
79
|
+
if (column.schema.primaryKey === true)
|
|
80
|
+
c = c.primaryKey();
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "boolean": {
|
|
84
|
+
c = integer(columnName, { mode: "boolean" });
|
|
85
|
+
if (column.schema.default !== void 0)
|
|
86
|
+
c = c.default(handleSerializedSQL(column.schema.default));
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case "json":
|
|
90
|
+
c = jsonType(columnName);
|
|
91
|
+
if (column.schema.default !== void 0)
|
|
92
|
+
c = c.default(column.schema.default);
|
|
93
|
+
break;
|
|
94
|
+
case "date": {
|
|
95
|
+
c = dateType(columnName);
|
|
96
|
+
if (column.schema.default !== void 0) {
|
|
97
|
+
const def = handleSerializedSQL(column.schema.default);
|
|
98
|
+
c = c.default(typeof def === "string" ? new Date(def) : def);
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!column.schema.optional)
|
|
104
|
+
c = c.notNull();
|
|
105
|
+
if (column.schema.unique)
|
|
106
|
+
c = c.unique();
|
|
107
|
+
return c;
|
|
108
|
+
}
|
|
109
|
+
function handleSerializedSQL(def) {
|
|
110
|
+
if (isSerializedSQL(def)) {
|
|
111
|
+
return sql.raw(def.sql);
|
|
112
|
+
}
|
|
113
|
+
return def;
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
FALSE,
|
|
117
|
+
NOW,
|
|
118
|
+
TRUE,
|
|
119
|
+
collectionToTable,
|
|
120
|
+
createLocalDatabaseClient,
|
|
121
|
+
createRemoteDatabaseClient,
|
|
122
|
+
hasPrimaryKey,
|
|
123
|
+
sql
|
|
124
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { ColumnDataType, ColumnBaseConfig } from 'drizzle-orm';
|
|
2
|
+
import type { SQLiteColumn, SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
|
|
3
|
+
import type { DBColumn, ColumnsConfig } from '../core/types.js';
|
|
4
|
+
type GeneratedConfig<T extends ColumnDataType = ColumnDataType> = Pick<ColumnBaseConfig<T, string>, 'name' | 'tableName' | 'notNull' | 'hasDefault'>;
|
|
5
|
+
export type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<T & {
|
|
6
|
+
data: string;
|
|
7
|
+
dataType: 'string';
|
|
8
|
+
columnType: 'SQLiteText';
|
|
9
|
+
driverParam: string;
|
|
10
|
+
enumValues: never;
|
|
11
|
+
baseColumn: never;
|
|
12
|
+
}>;
|
|
13
|
+
export type AstroDate<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
14
|
+
data: Date;
|
|
15
|
+
dataType: 'custom';
|
|
16
|
+
columnType: 'SQLiteCustomColumn';
|
|
17
|
+
driverParam: string;
|
|
18
|
+
enumValues: never;
|
|
19
|
+
baseColumn: never;
|
|
20
|
+
}>;
|
|
21
|
+
export type AstroBoolean<T extends GeneratedConfig<'boolean'>> = SQLiteColumn<T & {
|
|
22
|
+
data: boolean;
|
|
23
|
+
dataType: 'boolean';
|
|
24
|
+
columnType: 'SQLiteBoolean';
|
|
25
|
+
driverParam: number;
|
|
26
|
+
enumValues: never;
|
|
27
|
+
baseColumn: never;
|
|
28
|
+
}>;
|
|
29
|
+
export type AstroNumber<T extends GeneratedConfig<'number'>> = SQLiteColumn<T & {
|
|
30
|
+
data: number;
|
|
31
|
+
dataType: 'number';
|
|
32
|
+
columnType: 'SQLiteInteger';
|
|
33
|
+
driverParam: number;
|
|
34
|
+
enumValues: never;
|
|
35
|
+
baseColumn: never;
|
|
36
|
+
}>;
|
|
37
|
+
export type AstroJson<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
38
|
+
data: unknown;
|
|
39
|
+
dataType: 'custom';
|
|
40
|
+
columnType: 'SQLiteCustomColumn';
|
|
41
|
+
driverParam: string;
|
|
42
|
+
enumValues: never;
|
|
43
|
+
baseColumn: never;
|
|
44
|
+
}>;
|
|
45
|
+
export type Column<T extends DBColumn['type'], S extends GeneratedConfig> = T extends 'boolean' ? AstroBoolean<S> : T extends 'number' ? AstroNumber<S> : T extends 'text' ? AstroText<S> : T extends 'date' ? AstroDate<S> : T extends 'json' ? AstroJson<S> : never;
|
|
46
|
+
export type Table<TTableName extends string, TColumns extends ColumnsConfig> = SQLiteTableWithColumns<{
|
|
47
|
+
name: TTableName;
|
|
48
|
+
schema: undefined;
|
|
49
|
+
dialect: 'sqlite';
|
|
50
|
+
columns: {
|
|
51
|
+
[K in Extract<keyof TColumns, string>]: Column<TColumns[K]['type'], {
|
|
52
|
+
tableName: TTableName;
|
|
53
|
+
name: K;
|
|
54
|
+
hasDefault: TColumns[K]['schema'] extends {
|
|
55
|
+
default: NonNullable<unknown>;
|
|
56
|
+
} ? true : TColumns[K]['schema'] extends {
|
|
57
|
+
primaryKey: true;
|
|
58
|
+
} ? true : false;
|
|
59
|
+
notNull: TColumns[K]['schema']['optional'] extends true ? false : true;
|
|
60
|
+
}>;
|
|
61
|
+
};
|
|
62
|
+
}>;
|
|
63
|
+
export declare const SERIALIZED_SQL_KEY = "__serializedSQL";
|
|
64
|
+
export type SerializedSQL = {
|
|
65
|
+
[SERIALIZED_SQL_KEY]: true;
|
|
66
|
+
sql: string;
|
|
67
|
+
};
|
|
68
|
+
export declare function isSerializedSQL(value: any): value is SerializedSQL;
|
|
69
|
+
export {};
|
package/index.d.ts
ADDED