@astrojs/db 0.7.1 → 0.7.2
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/dist/core/cli/commands/push/index.js +12 -1
- package/dist/core/cli/index.js +22 -21
- package/dist/core/cli/migration-queries.js +10 -0
- package/dist/core/cli/print-help.d.ts +11 -0
- package/dist/core/cli/print-help.js +55 -0
- package/dist/core/consts.d.ts +1 -1
- package/dist/core/consts.js +2 -3
- package/dist/core/integration/index.js +4 -7
- package/dist/core/integration/vite-plugin-db.js +14 -40
- package/dist/runtime/index.d.ts +1 -8
- package/dist/runtime/index.js +1 -32
- package/dist/runtime/seed-local.d.ts +10 -0
- package/dist/runtime/seed-local.js +53 -0
- package/package.json +4 -8
- package/dist/runtime/config.d.ts +0 -148
|
@@ -64,13 +64,24 @@ async function pushSchema({
|
|
|
64
64
|
return new Response(null, { status: 200 });
|
|
65
65
|
}
|
|
66
66
|
const url = new URL("/db/push", getRemoteDatabaseUrl());
|
|
67
|
-
|
|
67
|
+
const response = await fetch(url, {
|
|
68
68
|
method: "POST",
|
|
69
69
|
headers: new Headers({
|
|
70
70
|
Authorization: `Bearer ${appToken}`
|
|
71
71
|
}),
|
|
72
72
|
body: JSON.stringify(requestBody)
|
|
73
73
|
});
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
console.error(`${url.toString()} failed: ${response.status} ${response.statusText}`);
|
|
76
|
+
console.error(await response.text());
|
|
77
|
+
throw new Error(`/db/push fetch failed: ${response.status} ${response.statusText}`);
|
|
78
|
+
}
|
|
79
|
+
const result = await response.json();
|
|
80
|
+
if (!result.success) {
|
|
81
|
+
console.error(`${url.toString()} unsuccessful`);
|
|
82
|
+
console.error(await response.text());
|
|
83
|
+
throw new Error(`/db/push fetch unsuccessful`);
|
|
84
|
+
}
|
|
74
85
|
}
|
|
75
86
|
export {
|
|
76
87
|
cmd
|
package/dist/core/cli/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { resolveDbConfig } from "../load-file.js";
|
|
2
|
+
import { printHelp } from "./print-help.js";
|
|
2
3
|
async function cli({
|
|
3
4
|
flags,
|
|
4
5
|
config: astroConfig
|
|
@@ -44,31 +45,31 @@ async function cli({
|
|
|
44
45
|
return await cmd();
|
|
45
46
|
}
|
|
46
47
|
default: {
|
|
47
|
-
if (command
|
|
48
|
-
console.error(`
|
|
49
|
-
|
|
50
|
-
${showHelp()}`);
|
|
51
|
-
} else {
|
|
52
|
-
console.error(`Unknown command: ${command}
|
|
53
|
-
|
|
54
|
-
${showHelp()}`);
|
|
48
|
+
if (command != null) {
|
|
49
|
+
console.error(`Unknown command: ${command}`);
|
|
55
50
|
}
|
|
51
|
+
printHelp({
|
|
52
|
+
commandName: "astro db",
|
|
53
|
+
usage: "[command] [...flags]",
|
|
54
|
+
headline: " ",
|
|
55
|
+
tables: {
|
|
56
|
+
Commands: [
|
|
57
|
+
["push", "Push table schema updates to Astro Studio."],
|
|
58
|
+
["verify", "Test schema updates /w Astro Studio (good for CI)."],
|
|
59
|
+
[
|
|
60
|
+
"astro db execute <file-path>",
|
|
61
|
+
"Execute a ts/js file using astro:db. Use --remote to connect to Studio."
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
"astro db shell --query <sql-string>",
|
|
65
|
+
"Execute a SQL string. Use --remote to connect to Studio."
|
|
66
|
+
]
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
});
|
|
56
70
|
return;
|
|
57
71
|
}
|
|
58
72
|
}
|
|
59
|
-
function showHelp() {
|
|
60
|
-
return `astro db <command>
|
|
61
|
-
|
|
62
|
-
Usage:
|
|
63
|
-
|
|
64
|
-
astro login Authenticate your machine with Astro Studio
|
|
65
|
-
astro logout End your authenticated session with Astro Studio
|
|
66
|
-
astro link Link this directory to an Astro Studio project
|
|
67
|
-
|
|
68
|
-
astro db gen Creates snapshot based on your schema
|
|
69
|
-
astro db push Pushes schema updates to Astro Studio
|
|
70
|
-
astro db verify Tests schema updates /w Astro Studio (good for CI)`;
|
|
71
|
-
}
|
|
72
73
|
}
|
|
73
74
|
export {
|
|
74
75
|
cli
|
|
@@ -331,7 +331,17 @@ async function getProductionCurrentSnapshot({
|
|
|
331
331
|
Authorization: `Bearer ${appToken}`
|
|
332
332
|
})
|
|
333
333
|
});
|
|
334
|
+
if (!response.ok) {
|
|
335
|
+
console.error(`${url.toString()} failed: ${response.status} ${response.statusText}`);
|
|
336
|
+
console.error(await response.text());
|
|
337
|
+
throw new Error(`/db/schema fetch failed: ${response.status} ${response.statusText}`);
|
|
338
|
+
}
|
|
334
339
|
const result = await response.json();
|
|
340
|
+
if (!result.success) {
|
|
341
|
+
console.error(`${url.toString()} unsuccessful`);
|
|
342
|
+
console.error(await response.text());
|
|
343
|
+
throw new Error(`/db/schema fetch unsuccessful`);
|
|
344
|
+
}
|
|
335
345
|
return result.data;
|
|
336
346
|
}
|
|
337
347
|
function createCurrentSnapshot({ tables = {} }) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Uses implementation from Astro core
|
|
3
|
+
* @see https://github.com/withastro/astro/blob/main/packages/astro/src/core/messages.ts#L303
|
|
4
|
+
*/
|
|
5
|
+
export declare function printHelp({ commandName, headline, usage, tables, description, }: {
|
|
6
|
+
commandName: string;
|
|
7
|
+
headline?: string;
|
|
8
|
+
usage?: string;
|
|
9
|
+
tables?: Record<string, [command: string, help: string][]>;
|
|
10
|
+
description?: string;
|
|
11
|
+
}): void;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { bgGreen, bgWhite, black, bold, dim, green } from "kleur/colors";
|
|
2
|
+
function printHelp({
|
|
3
|
+
commandName,
|
|
4
|
+
headline,
|
|
5
|
+
usage,
|
|
6
|
+
tables,
|
|
7
|
+
description
|
|
8
|
+
}) {
|
|
9
|
+
const linebreak = () => "";
|
|
10
|
+
const title = (label) => ` ${bgWhite(black(` ${label} `))}`;
|
|
11
|
+
const table = (rows, { padding }) => {
|
|
12
|
+
const split = process.stdout.columns < 60;
|
|
13
|
+
let raw = "";
|
|
14
|
+
for (const row of rows) {
|
|
15
|
+
if (split) {
|
|
16
|
+
raw += ` ${row[0]}
|
|
17
|
+
`;
|
|
18
|
+
} else {
|
|
19
|
+
raw += `${`${row[0]}`.padStart(padding)}`;
|
|
20
|
+
}
|
|
21
|
+
raw += " " + dim(row[1]) + "\n";
|
|
22
|
+
}
|
|
23
|
+
return raw.slice(0, -1);
|
|
24
|
+
};
|
|
25
|
+
let message = [];
|
|
26
|
+
if (headline) {
|
|
27
|
+
message.push(
|
|
28
|
+
linebreak(),
|
|
29
|
+
` ${bgGreen(black(` ${commandName} `))} ${green(
|
|
30
|
+
`v${"0.7.2"}`
|
|
31
|
+
)} ${headline}`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
if (usage) {
|
|
35
|
+
message.push(linebreak(), ` ${green(commandName)} ${bold(usage)}`);
|
|
36
|
+
}
|
|
37
|
+
if (tables) {
|
|
38
|
+
let calculateTablePadding2 = function(rows) {
|
|
39
|
+
return rows.reduce((val, [first]) => Math.max(val, first.length), 0) + 2;
|
|
40
|
+
};
|
|
41
|
+
var calculateTablePadding = calculateTablePadding2;
|
|
42
|
+
const tableEntries = Object.entries(tables);
|
|
43
|
+
const padding = Math.max(...tableEntries.map(([, rows]) => calculateTablePadding2(rows)));
|
|
44
|
+
for (const [tableTitle, tableRows] of tableEntries) {
|
|
45
|
+
message.push(linebreak(), title(tableTitle), table(tableRows, { padding }));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (description) {
|
|
49
|
+
message.push(linebreak(), `${description}`);
|
|
50
|
+
}
|
|
51
|
+
console.log(message.join("\n") + "\n");
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
printHelp
|
|
55
|
+
};
|
package/dist/core/consts.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ export declare const RUNTIME_IMPORT: string;
|
|
|
3
3
|
export declare const RUNTIME_CONFIG_IMPORT: string;
|
|
4
4
|
export declare const DB_TYPES_FILE = "db-types.d.ts";
|
|
5
5
|
export declare const VIRTUAL_MODULE_ID = "astro:db";
|
|
6
|
-
export declare const DB_PATH
|
|
6
|
+
export declare const DB_PATH = ".astro/content.db";
|
|
7
7
|
export declare const CONFIG_FILE_NAMES: string[];
|
|
8
8
|
export declare const MIGRATION_VERSION = "2024-03-12";
|
package/dist/core/consts.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
1
|
import { readFileSync } from "node:fs";
|
|
3
2
|
const PACKAGE_NAME = JSON.parse(
|
|
4
3
|
readFileSync(new URL("../../package.json", import.meta.url), "utf8")
|
|
5
4
|
).name;
|
|
6
5
|
const RUNTIME_IMPORT = JSON.stringify(`${PACKAGE_NAME}/runtime`);
|
|
7
|
-
const RUNTIME_CONFIG_IMPORT = JSON.stringify(`${PACKAGE_NAME}/runtime/config`);
|
|
6
|
+
const RUNTIME_CONFIG_IMPORT = JSON.stringify(`${PACKAGE_NAME}/dist/runtime/config.js`);
|
|
8
7
|
const DB_TYPES_FILE = "db-types.d.ts";
|
|
9
8
|
const VIRTUAL_MODULE_ID = "astro:db";
|
|
10
|
-
const DB_PATH =
|
|
9
|
+
const DB_PATH = ".astro/content.db";
|
|
11
10
|
const CONFIG_FILE_NAMES = ["config.ts", "config.js", "config.mts", "config.mjs"];
|
|
12
11
|
const MIGRATION_VERSION = "2024-03-12";
|
|
13
12
|
export {
|
|
@@ -71,13 +71,10 @@ function astroDBIntegration() {
|
|
|
71
71
|
tables.get = () => dbConfig.tables;
|
|
72
72
|
seedFiles.get = () => integrationSeedPaths;
|
|
73
73
|
configFileDependencies = dependencies;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
await mkdir(dirname(fileURLToPath(dbUrl)), { recursive: true });
|
|
80
|
-
await writeFile(dbUrl, "");
|
|
74
|
+
const localDbUrl = new URL(DB_PATH, config.root);
|
|
75
|
+
if (!connectToStudio && !existsSync(localDbUrl)) {
|
|
76
|
+
await mkdir(dirname(fileURLToPath(localDbUrl)), { recursive: true });
|
|
77
|
+
await writeFile(localDbUrl, "");
|
|
81
78
|
}
|
|
82
79
|
await typegen({ tables: tables.get() ?? {}, root: config.root });
|
|
83
80
|
},
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
1
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { sql } from "drizzle-orm";
|
|
4
|
-
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
|
|
5
2
|
import { normalizePath } from "vite";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
SEED_DEV_FILE_NAME,
|
|
9
|
-
getCreateIndexQueries,
|
|
10
|
-
getCreateTableQuery
|
|
11
|
-
} from "../../runtime/queries.js";
|
|
3
|
+
import { SEED_DEV_FILE_NAME } from "../../runtime/queries.js";
|
|
12
4
|
import { DB_PATH, RUNTIME_CONFIG_IMPORT, RUNTIME_IMPORT, VIRTUAL_MODULE_ID } from "../consts.js";
|
|
13
5
|
import { getDbDirectoryUrl, getRemoteDatabaseUrl } from "../utils.js";
|
|
14
6
|
const WITH_SEED_VIRTUAL_MODULE_ID = "astro:db:seed";
|
|
@@ -18,9 +10,6 @@ const resolved = {
|
|
|
18
10
|
};
|
|
19
11
|
function vitePluginDb(params) {
|
|
20
12
|
const srcDirPath = normalizePath(fileURLToPath(params.srcDir));
|
|
21
|
-
const seedFilePaths = SEED_DEV_FILE_NAME.map(
|
|
22
|
-
(name) => normalizePath(fileURLToPath(new URL(name, getDbDirectoryUrl(params.root))))
|
|
23
|
-
);
|
|
24
13
|
return {
|
|
25
14
|
name: "astro:db",
|
|
26
15
|
enforce: "pre",
|
|
@@ -38,12 +27,6 @@ function vitePluginDb(params) {
|
|
|
38
27
|
return resolved.virtual;
|
|
39
28
|
},
|
|
40
29
|
async load(id) {
|
|
41
|
-
if (seedFilePaths.some((f) => id === f)) {
|
|
42
|
-
await recreateTables({
|
|
43
|
-
db: createLocalDatabaseClient({ dbUrl: new URL(DB_PATH, params.root).href }),
|
|
44
|
-
tables: params.tables.get()
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
30
|
if (id !== resolved.virtual && id !== resolved.seedVirtual)
|
|
48
31
|
return;
|
|
49
32
|
if (params.connectToStudio) {
|
|
@@ -75,24 +58,29 @@ function getLocalVirtualModContents({
|
|
|
75
58
|
// for Vite import.meta.glob
|
|
76
59
|
(name) => new URL(name, getDbDirectoryUrl("file:///")).pathname
|
|
77
60
|
);
|
|
78
|
-
const resolveId = (id) => id.startsWith(".") ?
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
61
|
+
const resolveId = (id) => id.startsWith(".") ? normalizePath(fileURLToPath(new URL(id, root))) : id;
|
|
62
|
+
const integrationSeedImportStatements = [];
|
|
63
|
+
const integrationSeedImportNames = [];
|
|
64
|
+
seedFiles.forEach((pathOrUrl, index) => {
|
|
65
|
+
const path = typeof pathOrUrl === "string" ? resolveId(pathOrUrl) : pathOrUrl.pathname;
|
|
66
|
+
const importName = "integration_seed_" + index;
|
|
67
|
+
integrationSeedImportStatements.push(`import ${importName} from ${JSON.stringify(path)};`);
|
|
68
|
+
integrationSeedImportNames.push(importName);
|
|
69
|
+
});
|
|
85
70
|
const dbUrl = new URL(DB_PATH, root);
|
|
86
71
|
return `
|
|
87
72
|
import { asDrizzleTable, createLocalDatabaseClient } from ${RUNTIME_IMPORT};
|
|
88
73
|
${shouldSeed ? `import { seedLocal } from ${RUNTIME_IMPORT};` : ""}
|
|
74
|
+
${shouldSeed ? integrationSeedImportStatements.join("\n") : ""}
|
|
89
75
|
|
|
90
76
|
const dbUrl = ${JSON.stringify(dbUrl)};
|
|
91
77
|
export const db = createLocalDatabaseClient({ dbUrl });
|
|
92
78
|
|
|
93
79
|
${shouldSeed ? `await seedLocal({
|
|
80
|
+
db,
|
|
81
|
+
tables: ${JSON.stringify(tables)},
|
|
94
82
|
userSeedGlob: import.meta.glob(${JSON.stringify(userSeedFilePaths)}, { eager: true }),
|
|
95
|
-
|
|
83
|
+
integrationSeedFunctions: [${integrationSeedImportNames.join(",")}],
|
|
96
84
|
});` : ""}
|
|
97
85
|
|
|
98
86
|
export * from ${RUNTIME_CONFIG_IMPORT};
|
|
@@ -123,20 +111,6 @@ function getStringifiedCollectionExports(tables) {
|
|
|
123
111
|
)}, false)`
|
|
124
112
|
).join("\n");
|
|
125
113
|
}
|
|
126
|
-
const sqlite = new SQLiteAsyncDialect();
|
|
127
|
-
async function recreateTables({ db, tables }) {
|
|
128
|
-
const setupQueries = [];
|
|
129
|
-
for (const [name, table] of Object.entries(tables)) {
|
|
130
|
-
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
|
|
131
|
-
const createQuery = sql.raw(getCreateTableQuery(name, table));
|
|
132
|
-
const indexQueries = getCreateIndexQueries(name, table);
|
|
133
|
-
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
|
|
134
|
-
}
|
|
135
|
-
await db.batch([
|
|
136
|
-
db.run(sql`pragma defer_foreign_keys=true;`),
|
|
137
|
-
...setupQueries.map((q) => db.run(q))
|
|
138
|
-
]);
|
|
139
|
-
}
|
|
140
114
|
export {
|
|
141
115
|
getConfigVirtualModContents,
|
|
142
116
|
getLocalVirtualModContents,
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -5,14 +5,7 @@ export { sql };
|
|
|
5
5
|
export type SqliteDB = LibSQLDatabase;
|
|
6
6
|
export type { Table } from './types.js';
|
|
7
7
|
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
|
|
8
|
-
export
|
|
9
|
-
userSeedGlob: Record<string, {
|
|
10
|
-
default?: () => Promise<void>;
|
|
11
|
-
}>;
|
|
12
|
-
integrationSeedImports: Array<() => Promise<{
|
|
13
|
-
default: () => Promise<void>;
|
|
14
|
-
}>>;
|
|
15
|
-
}): Promise<void>;
|
|
8
|
+
export { seedLocal } from './seed-local.js';
|
|
16
9
|
export declare function hasPrimaryKey(column: DBColumn): boolean;
|
|
17
10
|
export declare const NOW: import("drizzle-orm").SQL<unknown>;
|
|
18
11
|
export declare const TRUE: import("drizzle-orm").SQL<unknown>;
|
package/dist/runtime/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { LibsqlError } from "@libsql/client";
|
|
2
1
|
import { sql } from "drizzle-orm";
|
|
3
2
|
import {
|
|
4
3
|
customType,
|
|
@@ -7,40 +6,10 @@ import {
|
|
|
7
6
|
sqliteTable,
|
|
8
7
|
text
|
|
9
8
|
} from "drizzle-orm/sqlite-core";
|
|
10
|
-
import { SEED_DEFAULT_EXPORT_ERROR, SEED_ERROR } from "../core/errors.js";
|
|
11
9
|
import {} from "../core/types.js";
|
|
12
10
|
import { isSerializedSQL } from "./types.js";
|
|
13
11
|
import { createRemoteDatabaseClient, createLocalDatabaseClient } from "./db-client.js";
|
|
14
|
-
|
|
15
|
-
// Glob all potential seed files to catch renames and deletions.
|
|
16
|
-
userSeedGlob,
|
|
17
|
-
integrationSeedImports
|
|
18
|
-
}) {
|
|
19
|
-
const seedFilePath = Object.keys(userSeedGlob)[0];
|
|
20
|
-
if (seedFilePath) {
|
|
21
|
-
const mod = userSeedGlob[seedFilePath];
|
|
22
|
-
if (!mod.default) {
|
|
23
|
-
throw new Error(SEED_DEFAULT_EXPORT_ERROR(seedFilePath));
|
|
24
|
-
}
|
|
25
|
-
try {
|
|
26
|
-
await mod.default();
|
|
27
|
-
} catch (e) {
|
|
28
|
-
if (e instanceof LibsqlError) {
|
|
29
|
-
throw new Error(SEED_ERROR(e.message));
|
|
30
|
-
}
|
|
31
|
-
throw e;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
for (const importModule of integrationSeedImports) {
|
|
35
|
-
const mod = await importModule();
|
|
36
|
-
await mod.default().catch((e) => {
|
|
37
|
-
if (e instanceof LibsqlError) {
|
|
38
|
-
throw new Error(SEED_ERROR(e.message));
|
|
39
|
-
}
|
|
40
|
-
throw e;
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
12
|
+
import { seedLocal } from "./seed-local.js";
|
|
44
13
|
function hasPrimaryKey(column) {
|
|
45
14
|
return "primaryKey" in column.schema && !!column.schema.primaryKey;
|
|
46
15
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
2
|
+
import { type DBTables } from '../core/types.js';
|
|
3
|
+
export declare function seedLocal({ db, tables, userSeedGlob, integrationSeedFunctions, }: {
|
|
4
|
+
db: LibSQLDatabase;
|
|
5
|
+
tables: DBTables;
|
|
6
|
+
userSeedGlob: Record<string, {
|
|
7
|
+
default?: () => Promise<void>;
|
|
8
|
+
}>;
|
|
9
|
+
integrationSeedFunctions: Array<() => Promise<void>>;
|
|
10
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { LibsqlError } from "@libsql/client";
|
|
2
|
+
import { sql } from "drizzle-orm";
|
|
3
|
+
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
|
|
4
|
+
import { SEED_DEFAULT_EXPORT_ERROR, SEED_ERROR } from "../core/errors.js";
|
|
5
|
+
import {} from "../core/types.js";
|
|
6
|
+
import { getCreateIndexQueries, getCreateTableQuery } from "./queries.js";
|
|
7
|
+
const sqlite = new SQLiteAsyncDialect();
|
|
8
|
+
async function seedLocal({
|
|
9
|
+
db,
|
|
10
|
+
tables,
|
|
11
|
+
// Glob all potential seed files to catch renames and deletions.
|
|
12
|
+
userSeedGlob,
|
|
13
|
+
integrationSeedFunctions
|
|
14
|
+
}) {
|
|
15
|
+
await recreateTables({ db, tables });
|
|
16
|
+
const seedFunctions = [];
|
|
17
|
+
const seedFilePath = Object.keys(userSeedGlob)[0];
|
|
18
|
+
if (seedFilePath) {
|
|
19
|
+
const mod = userSeedGlob[seedFilePath];
|
|
20
|
+
if (!mod.default)
|
|
21
|
+
throw new Error(SEED_DEFAULT_EXPORT_ERROR(seedFilePath));
|
|
22
|
+
seedFunctions.push(mod.default);
|
|
23
|
+
}
|
|
24
|
+
for (const seedFn of integrationSeedFunctions) {
|
|
25
|
+
seedFunctions.push(seedFn);
|
|
26
|
+
}
|
|
27
|
+
for (const seed of seedFunctions) {
|
|
28
|
+
try {
|
|
29
|
+
await seed();
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e instanceof LibsqlError) {
|
|
32
|
+
throw new Error(SEED_ERROR(e.message));
|
|
33
|
+
}
|
|
34
|
+
throw e;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function recreateTables({ db, tables }) {
|
|
39
|
+
const setupQueries = [];
|
|
40
|
+
for (const [name, table] of Object.entries(tables)) {
|
|
41
|
+
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
|
|
42
|
+
const createQuery = sql.raw(getCreateTableQuery(name, table));
|
|
43
|
+
const indexQueries = getCreateIndexQueries(name, table);
|
|
44
|
+
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
|
|
45
|
+
}
|
|
46
|
+
await db.batch([
|
|
47
|
+
db.run(sql`pragma defer_foreign_keys=true;`),
|
|
48
|
+
...setupQueries.map((q) => db.run(q))
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
seedLocal
|
|
53
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/db",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -20,8 +20,7 @@
|
|
|
20
20
|
"types": "./dist/runtime/index.d.ts",
|
|
21
21
|
"import": "./dist/runtime/index.js"
|
|
22
22
|
},
|
|
23
|
-
"./runtime/config": {
|
|
24
|
-
"types": "./dist/runtime/config.d.ts",
|
|
23
|
+
"./dist/runtime/config.js": {
|
|
25
24
|
"import": "./dist/runtime/config.js"
|
|
26
25
|
},
|
|
27
26
|
"./package.json": "./package.json"
|
|
@@ -36,9 +35,6 @@
|
|
|
36
35
|
],
|
|
37
36
|
"runtime": [
|
|
38
37
|
"./dist/runtime/index.d.ts"
|
|
39
|
-
],
|
|
40
|
-
"runtime/config": [
|
|
41
|
-
"./dist/runtime/config.d.ts"
|
|
42
38
|
]
|
|
43
39
|
}
|
|
44
40
|
},
|
|
@@ -52,7 +48,7 @@
|
|
|
52
48
|
"astro-integration"
|
|
53
49
|
],
|
|
54
50
|
"dependencies": {
|
|
55
|
-
"@libsql/client": "^0.
|
|
51
|
+
"@libsql/client": "^0.5.5",
|
|
56
52
|
"async-listen": "^3.0.1",
|
|
57
53
|
"deep-diff": "^1.0.2",
|
|
58
54
|
"drizzle-orm": "^0.29.5",
|
|
@@ -77,7 +73,7 @@
|
|
|
77
73
|
"mocha": "^10.2.0",
|
|
78
74
|
"typescript": "^5.2.2",
|
|
79
75
|
"vite": "^5.1.4",
|
|
80
|
-
"astro": "4.5.
|
|
76
|
+
"astro": "4.5.1",
|
|
81
77
|
"astro-scripts": "0.0.14"
|
|
82
78
|
},
|
|
83
79
|
"scripts": {
|
package/dist/runtime/config.d.ts
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import type { ColumnsConfig, DBConfigInput, TableConfig } from '../core/types.js';
|
|
2
|
-
export declare const column: {
|
|
3
|
-
number: <T extends ({
|
|
4
|
-
name?: string | undefined;
|
|
5
|
-
label?: string | undefined;
|
|
6
|
-
unique?: boolean | undefined;
|
|
7
|
-
deprecated?: boolean | undefined;
|
|
8
|
-
collection?: string | undefined;
|
|
9
|
-
} & ({
|
|
10
|
-
primaryKey?: false | undefined;
|
|
11
|
-
optional?: boolean | undefined;
|
|
12
|
-
default?: number | import("drizzle-orm").SQL<any> | undefined;
|
|
13
|
-
} | {
|
|
14
|
-
primaryKey: true;
|
|
15
|
-
optional?: false | undefined;
|
|
16
|
-
default?: undefined;
|
|
17
|
-
})) & {
|
|
18
|
-
references?: (() => {
|
|
19
|
-
type: "number";
|
|
20
|
-
schema: ({
|
|
21
|
-
name?: string | undefined;
|
|
22
|
-
label?: string | undefined;
|
|
23
|
-
unique?: boolean | undefined;
|
|
24
|
-
deprecated?: boolean | undefined;
|
|
25
|
-
collection?: string | undefined;
|
|
26
|
-
} & {
|
|
27
|
-
primaryKey?: false | undefined;
|
|
28
|
-
optional?: boolean | undefined;
|
|
29
|
-
default?: number | import("drizzle-orm").SQL<any> | undefined;
|
|
30
|
-
} & any) | ({
|
|
31
|
-
name?: string | undefined;
|
|
32
|
-
label?: string | undefined;
|
|
33
|
-
unique?: boolean | undefined;
|
|
34
|
-
deprecated?: boolean | undefined;
|
|
35
|
-
collection?: string | undefined;
|
|
36
|
-
} & {
|
|
37
|
-
primaryKey: true;
|
|
38
|
-
optional?: false | undefined;
|
|
39
|
-
default?: undefined;
|
|
40
|
-
} & any);
|
|
41
|
-
}) | undefined;
|
|
42
|
-
}>(opts?: T) => {
|
|
43
|
-
type: "number";
|
|
44
|
-
/**
|
|
45
|
-
* @internal
|
|
46
|
-
*/
|
|
47
|
-
schema: T;
|
|
48
|
-
};
|
|
49
|
-
boolean: <T_1 extends {
|
|
50
|
-
name?: string | undefined;
|
|
51
|
-
label?: string | undefined;
|
|
52
|
-
optional?: boolean | undefined;
|
|
53
|
-
unique?: boolean | undefined;
|
|
54
|
-
deprecated?: boolean | undefined;
|
|
55
|
-
collection?: string | undefined;
|
|
56
|
-
default?: boolean | import("drizzle-orm").SQL<any> | undefined;
|
|
57
|
-
}>(opts?: T_1) => {
|
|
58
|
-
type: "boolean";
|
|
59
|
-
/**
|
|
60
|
-
* @internal
|
|
61
|
-
*/
|
|
62
|
-
schema: T_1;
|
|
63
|
-
};
|
|
64
|
-
text: <T_2 extends ({
|
|
65
|
-
name?: string | undefined;
|
|
66
|
-
label?: string | undefined;
|
|
67
|
-
unique?: boolean | undefined;
|
|
68
|
-
deprecated?: boolean | undefined;
|
|
69
|
-
collection?: string | undefined;
|
|
70
|
-
default?: string | import("drizzle-orm").SQL<any> | undefined;
|
|
71
|
-
multiline?: boolean | undefined;
|
|
72
|
-
} & ({
|
|
73
|
-
primaryKey?: false | undefined;
|
|
74
|
-
optional?: boolean | undefined;
|
|
75
|
-
} | {
|
|
76
|
-
primaryKey: true;
|
|
77
|
-
optional?: false | undefined;
|
|
78
|
-
})) & {
|
|
79
|
-
references?: (() => {
|
|
80
|
-
type: "text";
|
|
81
|
-
schema: ({
|
|
82
|
-
name?: string | undefined;
|
|
83
|
-
label?: string | undefined;
|
|
84
|
-
unique?: boolean | undefined;
|
|
85
|
-
deprecated?: boolean | undefined;
|
|
86
|
-
collection?: string | undefined;
|
|
87
|
-
default?: string | import("drizzle-orm").SQL<any> | undefined;
|
|
88
|
-
multiline?: boolean | undefined;
|
|
89
|
-
} & {
|
|
90
|
-
primaryKey?: false | undefined;
|
|
91
|
-
optional?: boolean | undefined;
|
|
92
|
-
} & any) | ({
|
|
93
|
-
name?: string | undefined;
|
|
94
|
-
label?: string | undefined;
|
|
95
|
-
unique?: boolean | undefined;
|
|
96
|
-
deprecated?: boolean | undefined;
|
|
97
|
-
collection?: string | undefined;
|
|
98
|
-
default?: string | import("drizzle-orm").SQL<any> | undefined;
|
|
99
|
-
multiline?: boolean | undefined;
|
|
100
|
-
} & {
|
|
101
|
-
primaryKey: true;
|
|
102
|
-
optional?: false | undefined;
|
|
103
|
-
} & any);
|
|
104
|
-
}) | undefined;
|
|
105
|
-
}>(opts?: T_2) => {
|
|
106
|
-
type: "text";
|
|
107
|
-
/**
|
|
108
|
-
* @internal
|
|
109
|
-
*/
|
|
110
|
-
schema: T_2;
|
|
111
|
-
};
|
|
112
|
-
date<T_3 extends {
|
|
113
|
-
name?: string | undefined;
|
|
114
|
-
label?: string | undefined;
|
|
115
|
-
optional?: boolean | undefined;
|
|
116
|
-
unique?: boolean | undefined;
|
|
117
|
-
deprecated?: boolean | undefined;
|
|
118
|
-
collection?: string | undefined;
|
|
119
|
-
default?: Date | import("drizzle-orm").SQL<any> | undefined;
|
|
120
|
-
}>(opts?: T_3): {
|
|
121
|
-
type: "date";
|
|
122
|
-
/**
|
|
123
|
-
* @internal
|
|
124
|
-
*/
|
|
125
|
-
schema: T_3;
|
|
126
|
-
};
|
|
127
|
-
json<T_4 extends {
|
|
128
|
-
name?: string | undefined;
|
|
129
|
-
label?: string | undefined;
|
|
130
|
-
optional?: boolean | undefined;
|
|
131
|
-
unique?: boolean | undefined;
|
|
132
|
-
deprecated?: boolean | undefined;
|
|
133
|
-
collection?: string | undefined;
|
|
134
|
-
default?: unknown;
|
|
135
|
-
}>(opts?: T_4): {
|
|
136
|
-
type: "json";
|
|
137
|
-
/**
|
|
138
|
-
* @internal
|
|
139
|
-
*/
|
|
140
|
-
schema: T_4;
|
|
141
|
-
};
|
|
142
|
-
};
|
|
143
|
-
export declare function defineTable<TColumns extends ColumnsConfig>(userConfig: TableConfig<TColumns>): TableConfig<TColumns>;
|
|
144
|
-
export declare function defineDb(userConfig: DBConfigInput): {
|
|
145
|
-
tables?: unknown;
|
|
146
|
-
};
|
|
147
|
-
export { NOW, TRUE, FALSE } from './index.js';
|
|
148
|
-
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';
|