@astrojs/db 0.9.0 → 0.9.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.
@@ -1,6 +1,8 @@
1
+ import { LibsqlError } from '@libsql/client';
1
2
  import type { ColumnsConfig, DBConfigInput, TableConfig } from '../core/types.js';
2
3
  import type { LibSQLDatabase } from 'drizzle-orm/libsql';
3
4
  export type Database = Omit<LibSQLDatabase, 'transaction'>;
5
+ export declare function isDbError(err: unknown): err is LibsqlError;
4
6
  export declare const column: {
5
7
  number: <T extends ({
6
8
  name?: string | undefined;
@@ -19,10 +19,11 @@ async function cmd({
19
19
  const appToken = await getManagedAppTokenOrExit(flags.token);
20
20
  const productionSnapshot = await getProductionCurrentSnapshot({ appToken: appToken.token });
21
21
  const currentSnapshot = createCurrentSnapshot(dbConfig);
22
- const isFromScratch = isForceReset || !productionSnapshot;
22
+ const isFromScratch = !productionSnapshot;
23
23
  const { queries: migrationQueries, confirmations } = await getMigrationQueries({
24
24
  oldSnapshot: isFromScratch ? createEmptySnapshot() : productionSnapshot,
25
- newSnapshot: currentSnapshot
25
+ newSnapshot: currentSnapshot,
26
+ reset: isForceReset
26
27
  });
27
28
  if (migrationQueries.length === 0) {
28
29
  console.log("Database schema is up to date.");
@@ -1,7 +1,8 @@
1
1
  import { type DBConfig, type DBSnapshot, type DBTable } from '../types.js';
2
- export declare function getMigrationQueries({ oldSnapshot, newSnapshot, }: {
2
+ export declare function getMigrationQueries({ oldSnapshot, newSnapshot, reset, }: {
3
3
  oldSnapshot: DBSnapshot;
4
4
  newSnapshot: DBSnapshot;
5
+ reset?: boolean;
5
6
  }): Promise<{
6
7
  queries: string[];
7
8
  confirmations: string[];
@@ -24,10 +24,16 @@ const sqlite = new SQLiteAsyncDialect();
24
24
  const genTempTableName = customAlphabet("abcdefghijklmnopqrstuvwxyz", 10);
25
25
  async function getMigrationQueries({
26
26
  oldSnapshot,
27
- newSnapshot
27
+ newSnapshot,
28
+ reset = false
28
29
  }) {
29
30
  const queries = [];
30
31
  const confirmations = [];
32
+ if (reset) {
33
+ const currentSnapshot = oldSnapshot;
34
+ oldSnapshot = createEmptySnapshot();
35
+ queries.push(...getDropTableQueriesForSnapshot(currentSnapshot));
36
+ }
31
37
  const addedCollections = getAddedCollections(oldSnapshot, newSnapshot);
32
38
  const droppedTables = getDroppedCollections(oldSnapshot, newSnapshot);
33
39
  const notDeprecatedDroppedTables = Object.fromEntries(
@@ -346,6 +352,14 @@ async function getProductionCurrentSnapshot({
346
352
  }
347
353
  return result.data;
348
354
  }
355
+ function getDropTableQueriesForSnapshot(snapshot) {
356
+ const queries = [];
357
+ for (const collectionName of Object.keys(snapshot.schema)) {
358
+ const dropQuery = `DROP TABLE ${sqlite.escapeName(collectionName)}`;
359
+ queries.unshift(dropQuery);
360
+ }
361
+ return queries;
362
+ }
349
363
  function createCurrentSnapshot({ tables = {} }) {
350
364
  const schema = JSON.parse(JSON.stringify(tables));
351
365
  return { version: MIGRATION_VERSION, schema };
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.9.0"}`
30
+ `v${"0.9.2"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -1,3 +1,4 @@
1
+ import { LibsqlError } from "@libsql/client";
1
2
  import { sql as _sql } from "drizzle-orm";
2
3
  function createColumn(type, schema) {
3
4
  return {
@@ -8,6 +9,9 @@ function createColumn(type, schema) {
8
9
  schema
9
10
  };
10
11
  }
12
+ function isDbError(err) {
13
+ return err instanceof LibsqlError;
14
+ }
11
15
  const column = {
12
16
  number: (opts = {}) => {
13
17
  return createColumn("number", opts);
@@ -86,6 +90,7 @@ export {
86
90
  gt,
87
91
  gte,
88
92
  inArray,
93
+ isDbError,
89
94
  isNotNull,
90
95
  isNull,
91
96
  like,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -81,8 +81,8 @@
81
81
  "mocha": "^10.2.0",
82
82
  "typescript": "^5.2.2",
83
83
  "vite": "^5.1.4",
84
- "astro": "4.5.7",
85
- "astro-scripts": "0.0.14"
84
+ "astro-scripts": "0.0.14",
85
+ "astro": "4.5.8"
86
86
  },
87
87
  "scripts": {
88
88
  "types:config": "tsc -p ./tsconfig.config-types.json",
package/virtual.d.ts CHANGED
@@ -11,6 +11,7 @@ declare module 'astro:db' {
11
11
  export const column: RuntimeConfig['column'];
12
12
  export const defineDb: RuntimeConfig['defineDb'];
13
13
  export const defineTable: RuntimeConfig['defineTable'];
14
+ export const isDbError: RuntimeConfig['isDbError'];
14
15
 
15
16
  export const eq: RuntimeConfig['eq'];
16
17
  export const gt: RuntimeConfig['gt'];