@astrojs/db 0.8.8 → 0.9.1

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,5 +1,8 @@
1
+ import { LibsqlError } from '@libsql/client';
1
2
  import type { ColumnsConfig, DBConfigInput, TableConfig } from '../core/types.js';
2
- export type { LibSQLDatabase } from 'drizzle-orm/libsql';
3
+ import type { LibSQLDatabase } from 'drizzle-orm/libsql';
4
+ export type Database = Omit<LibSQLDatabase, 'transaction'>;
5
+ export declare function isDbError(err: unknown): err is LibsqlError;
3
6
  export declare const column: {
4
7
  number: <T extends ({
5
8
  name?: string | undefined;
@@ -148,4 +151,4 @@ export declare function defineDb(userConfig: DBConfigInput): {
148
151
  export declare const NOW: import("drizzle-orm").SQL<unknown>;
149
152
  export declare const TRUE: import("drizzle-orm").SQL<unknown>;
150
153
  export declare const FALSE: import("drizzle-orm").SQL<unknown>;
151
- 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';
154
+ export { sql, eq, gt, gte, lt, lte, ne, isNull, isNotNull, inArray, notInArray, exists, notExists, between, notBetween, like, notIlike, not, asc, desc, and, or, count, countDistinct, avg, avgDistinct, sum, sumDistinct, max, min, } from 'drizzle-orm';
@@ -7,7 +7,6 @@ import { hasPrimaryKey } from "../../runtime/index.js";
7
7
  import {
8
8
  getCreateIndexQueries,
9
9
  getCreateTableQuery,
10
- getDropTableIfExistsQuery,
11
10
  getModifiers,
12
11
  getReferencesConfig,
13
12
  hasDefault,
@@ -57,7 +56,7 @@ async function getMigrationQueries({
57
56
  const addedColumns = getAdded(oldCollection.columns, newCollection.columns);
58
57
  const droppedColumns = getDropped(oldCollection.columns, newCollection.columns);
59
58
  const notDeprecatedDroppedColumns = Object.fromEntries(
60
- Object.entries(droppedColumns).filter(([key, col]) => !col.schema.deprecated)
59
+ Object.entries(droppedColumns).filter(([, col]) => !col.schema.deprecated)
61
60
  );
62
61
  if (!isEmpty(addedColumns) && !isEmpty(notDeprecatedDroppedColumns)) {
63
62
  throw new Error(
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.8.8"}`
30
+ `v${"0.9.1"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -19,7 +19,7 @@ ${Object.entries(tables).map(([name, collection]) => generateTableType(name, col
19
19
  await writeFile(new URL(DB_TYPES_FILE, dotAstroDir), content);
20
20
  }
21
21
  function generateTableType(name, collection) {
22
- const sanitizedColumnsList = Object.entries(collection.columns).filter(([key, val]) => !val.schema.deprecated);
22
+ const sanitizedColumnsList = Object.entries(collection.columns).filter(([, val]) => !val.schema.deprecated);
23
23
  const sanitizedColumns = Object.fromEntries(sanitizedColumnsList);
24
24
  let tableType = ` export const ${name}: import(${RUNTIME_IMPORT}).Table<
25
25
  ${JSON.stringify(name)},
@@ -94,10 +94,7 @@ function getStudioVirtualModContents({
94
94
  return `
95
95
  import {asDrizzleTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
96
96
 
97
- export const db = await createRemoteDatabaseClient(process.env.ASTRO_STUDIO_APP_TOKEN ?? ${JSON.stringify(
98
- appToken
99
- // Respect runtime env for user overrides in SSR
100
- )}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${JSON.stringify(getRemoteDatabaseUrl())});
97
+ export const db = await createRemoteDatabaseClient(process.env.ASTRO_STUDIO_APP_TOKEN);
101
98
 
102
99
  export * from ${RUNTIME_CONFIG_IMPORT};
103
100
 
@@ -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);
@@ -56,7 +60,15 @@ import {
56
60
  asc,
57
61
  desc,
58
62
  and,
59
- or
63
+ or,
64
+ count,
65
+ countDistinct,
66
+ avg,
67
+ avgDistinct,
68
+ sum,
69
+ sumDistinct,
70
+ max,
71
+ min
60
72
  } from "drizzle-orm";
61
73
  export {
62
74
  FALSE,
@@ -64,8 +76,12 @@ export {
64
76
  TRUE,
65
77
  and,
66
78
  asc,
79
+ avg,
80
+ avgDistinct,
67
81
  between,
68
82
  column,
83
+ count,
84
+ countDistinct,
69
85
  defineDb,
70
86
  defineTable,
71
87
  desc,
@@ -74,11 +90,14 @@ export {
74
90
  gt,
75
91
  gte,
76
92
  inArray,
93
+ isDbError,
77
94
  isNotNull,
78
95
  isNull,
79
96
  like,
80
97
  lt,
81
98
  lte,
99
+ max,
100
+ min,
82
101
  ne,
83
102
  not,
84
103
  notBetween,
@@ -86,5 +105,7 @@ export {
86
105
  notIlike,
87
106
  notInArray,
88
107
  or,
89
- sql
108
+ sql,
109
+ sum,
110
+ sumDistinct
90
111
  };
@@ -1,5 +1,6 @@
1
1
  import type { LibSQLDatabase } from 'drizzle-orm/libsql';
2
+ import { type SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
2
3
  export declare function createLocalDatabaseClient({ dbUrl }: {
3
4
  dbUrl: string;
4
5
  }): LibSQLDatabase;
5
- export declare function createRemoteDatabaseClient(appToken: string, remoteDbURL: string): import("drizzle-orm/sqlite-proxy").SqliteRemoteDatabase<Record<string, never>>;
6
+ export declare function createRemoteDatabaseClient(appToken: string, remoteDbURL: string): SqliteRemoteDatabase<Record<string, never>>;
@@ -4,10 +4,20 @@ import { drizzle as drizzleProxy } from "drizzle-orm/sqlite-proxy";
4
4
  import { z } from "zod";
5
5
  import { safeFetch } from "./utils.js";
6
6
  const isWebContainer = !!process.versions?.webcontainer;
7
+ function applyTransactionNotSupported(db) {
8
+ Object.assign(db, {
9
+ transaction() {
10
+ throw new Error(
11
+ "`db.transaction()` is not currently supported. We recommend `db.batch()` for automatic error rollbacks across multiple queries."
12
+ );
13
+ }
14
+ });
15
+ }
7
16
  function createLocalDatabaseClient({ dbUrl }) {
8
17
  const url = isWebContainer ? "file:content.db" : dbUrl;
9
18
  const client = createClient({ url });
10
19
  const db = drizzleLibsql(client);
20
+ applyTransactionNotSupported(db);
11
21
  return db;
12
22
  }
13
23
  const remoteResultSchema = z.object({
@@ -113,6 +123,7 @@ Full error: Unexpected JSON response. ${e instanceof Error ? e.message : String(
113
123
  return results;
114
124
  }
115
125
  );
126
+ applyTransactionNotSupported(db);
116
127
  return db;
117
128
  }
118
129
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.8.8",
3
+ "version": "0.9.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -58,7 +58,8 @@
58
58
  "@libsql/client": "^0.5.5",
59
59
  "async-listen": "^3.0.1",
60
60
  "deep-diff": "^1.0.2",
61
- "drizzle-orm": "^0.29.5",
61
+ "drizzle-orm": "^0.30.2",
62
+ "github-slugger": "^2.0.0",
62
63
  "kleur": "^4.1.5",
63
64
  "nanoid": "^5.0.1",
64
65
  "open": "^10.0.3",
@@ -80,7 +81,7 @@
80
81
  "mocha": "^10.2.0",
81
82
  "typescript": "^5.2.2",
82
83
  "vite": "^5.1.4",
83
- "astro": "4.5.6",
84
+ "astro": "4.5.8",
84
85
  "astro-scripts": "0.0.14"
85
86
  },
86
87
  "scripts": {
package/virtual.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  declare module 'astro:db' {
2
2
  type RuntimeConfig = typeof import('./dist/_internal/runtime/config.js');
3
3
 
4
- export const db: import('./dist/_internal/runtime/config.js').LibSQLDatabase;
4
+ export const db: import('./dist/_internal/runtime/config.js').Database;
5
5
  export const dbUrl: string;
6
6
 
7
7
  export const sql: RuntimeConfig['sql'];
@@ -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'];
@@ -33,4 +34,12 @@ declare module 'astro:db' {
33
34
  export const desc: RuntimeConfig['desc'];
34
35
  export const and: RuntimeConfig['and'];
35
36
  export const or: RuntimeConfig['or'];
37
+ export const count: RuntimeConfig['count'];
38
+ export const countDistinct: RuntimeConfig['countDistinct'];
39
+ export const avg: RuntimeConfig['avg'];
40
+ export const avgDistinct: RuntimeConfig['avgDistinct'];
41
+ export const sum: RuntimeConfig['sum'];
42
+ export const sumDistinct: RuntimeConfig['sumDistinct'];
43
+ export const max: RuntimeConfig['max'];
44
+ export const min: RuntimeConfig['min'];
36
45
  }