@astrojs/db 0.1.15 → 0.1.17

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,8 +1,9 @@
1
+ import { createClient } from "@libsql/client";
1
2
  import deepDiff from "deep-diff";
2
3
  import { eq, sql } from "drizzle-orm";
3
4
  import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
4
5
  import { appTokenError } from "../../../errors.js";
5
- import { collectionToTable, createLocalDatabaseClient } from "../../../internal.js";
6
+ import { drizzle } from "drizzle-orm/sqlite-proxy";
6
7
  import {
7
8
  createCurrentSnapshot,
8
9
  createEmptySnapshot,
@@ -20,6 +21,7 @@ import {
20
21
  migrationsTable
21
22
  } from "../../../utils.js";
22
23
  import { getMigrationQueries } from "../../queries.js";
24
+ import { setupDbTables } from "../../../internal.js";
23
25
  const { diff } = deepDiff;
24
26
  const sqliteDialect = new SQLiteAsyncDialect();
25
27
  async function cmd({ config, flags }) {
@@ -91,26 +93,34 @@ async function pushData({
91
93
  appToken,
92
94
  isDryRun
93
95
  }) {
94
- const db = await createLocalDatabaseClient({
95
- collections: config.db.collections,
96
- dbUrl: ":memory:",
97
- seeding: true
98
- });
99
96
  const queries = [];
100
- for (const [name, collection] of Object.entries(config.db.collections)) {
101
- if (collection.writable || !collection.data)
102
- continue;
103
- const table = collectionToTable(name, collection);
104
- const insert = db.insert(table).values(await collection.data());
105
- queries.push(insert.toSQL());
97
+ if (config.db?.data) {
98
+ const libsqlClient = createClient({ url: ":memory:" });
99
+ const db = await drizzle(async (sqlQuery, params, method) => {
100
+ const stmt = { sql: sqlQuery, args: params };
101
+ queries.push(stmt);
102
+ const { rows } = await libsqlClient.execute(stmt);
103
+ const rowValues = [];
104
+ for (const row of rows) {
105
+ if (row != null && typeof row === "object") {
106
+ rowValues.push(Object.values(row));
107
+ }
108
+ }
109
+ if (method === "get") {
110
+ return { rows: rowValues[0] };
111
+ }
112
+ return { rows: rowValues };
113
+ });
114
+ await setupDbTables({
115
+ db,
116
+ mode: "build",
117
+ collections: config.db.collections ?? {},
118
+ data: config.db.data
119
+ });
106
120
  }
107
121
  const url = new URL("/db/query", getRemoteDatabaseUrl());
108
- const requestBody = queries.map((q) => ({
109
- sql: q.sql,
110
- args: q.params
111
- }));
112
122
  if (isDryRun) {
113
- console.info("[DRY RUN] Batch data seed:", JSON.stringify(requestBody, null, 2));
123
+ console.info("[DRY RUN] Batch data seed:", JSON.stringify(queries, null, 2));
114
124
  return new Response(null, { status: 200 });
115
125
  }
116
126
  return await fetch(url, {
@@ -118,7 +128,7 @@ async function pushData({
118
128
  headers: new Headers({
119
129
  Authorization: `Bearer ${appToken}`
120
130
  }),
121
- body: JSON.stringify(requestBody)
131
+ body: JSON.stringify(queries)
122
132
  });
123
133
  }
124
134
  async function runBatchQuery({
@@ -2,7 +2,13 @@ import * as color from "kleur/colors";
2
2
  import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
3
3
  import { customAlphabet } from "nanoid";
4
4
  import prompts from "prompts";
5
- import { getCreateTableQuery, getModifiers, hasDefault, hasPrimaryKey, schemaTypeToSqlType } from "../internal.js";
5
+ import {
6
+ getCreateTableQuery,
7
+ getModifiers,
8
+ hasDefault,
9
+ hasPrimaryKey,
10
+ schemaTypeToSqlType
11
+ } from "../internal.js";
6
12
  const sqlite = new SQLiteAsyncDialect();
7
13
  const genTempTableName = customAlphabet("abcdefghijklmnopqrstuvwxyz", 10);
8
14
  async function getMigrationQueries({
@@ -115,12 +121,8 @@ async function getCollectionChangeQueries({
115
121
  process.exit(0);
116
122
  }
117
123
  }
118
- const addedPrimaryKey = Object.entries(added).find(
119
- ([, field]) => hasPrimaryKey(field)
120
- );
121
- const droppedPrimaryKey = Object.entries(dropped).find(
122
- ([, field]) => hasPrimaryKey(field)
123
- );
124
+ const addedPrimaryKey = Object.entries(added).find(([, field]) => hasPrimaryKey(field));
125
+ const droppedPrimaryKey = Object.entries(dropped).find(([, field]) => hasPrimaryKey(field));
124
126
  const updatedPrimaryKey = Object.entries(updated).find(
125
127
  ([, field]) => hasPrimaryKey(field.old) || hasPrimaryKey(field.new)
126
128
  );
package/dist/config.d.ts CHANGED
@@ -1,5 +1,14 @@
1
- import { type BooleanField, type DBFieldInput, type DateFieldInput, type JsonField, type NumberField, type TextField, type collectionSchema, type MaybePromise } from './types.js';
1
+ import type { SQLiteInsertValue } from 'drizzle-orm/sqlite-core';
2
+ import type { SqliteDB, Table } from './internal.js';
3
+ import type { MaybeArray, collectionSchema } from './types.js';
4
+ import { type BooleanField, type DBFieldInput, type DateFieldInput, type JsonField, type NumberField, type TextField, type indexSchema, type MaybePromise } from './types.js';
2
5
  import { z } from 'zod';
6
+ export type DBDataContext = {
7
+ db: SqliteDB;
8
+ seed<TFields extends FieldsConfig>(collection: ResolvedCollectionConfig<TFields>, data: MaybeArray<SQLiteInsertValue<Table<string,
9
+ /** TODO: true type inference */ Record<Extract<keyof TFields, string>, FieldsConfig[number]>>>>): Promise<any> /** TODO: type output */;
10
+ mode: 'dev' | 'build';
11
+ };
3
12
  export declare const dbConfigSchema: z.ZodObject<{
4
13
  studio: z.ZodOptional<z.ZodBoolean>;
5
14
  collections: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodObject<{
@@ -103,7 +112,18 @@ export declare const dbConfigSchema: z.ZodObject<{
103
112
  unique?: boolean | undefined;
104
113
  default?: unknown;
105
114
  }>]>>;
106
- data: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">, z.ZodPromise<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">>]>>>;
115
+ indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
116
+ on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
117
+ unique: z.ZodOptional<z.ZodBoolean>;
118
+ }, "strip", z.ZodTypeAny, {
119
+ on: (string | string[]) & (string | string[] | undefined);
120
+ unique?: boolean | undefined;
121
+ }, {
122
+ on: (string | string[]) & (string | string[] | undefined);
123
+ unique?: boolean | undefined;
124
+ }>>>;
125
+ table: z.ZodAny;
126
+ _setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>;
107
127
  writable: z.ZodLiteral<false>;
108
128
  }, "strip", z.ZodTypeAny, {
109
129
  fields: Record<string, {
@@ -141,7 +161,12 @@ export declare const dbConfigSchema: z.ZodObject<{
141
161
  default?: unknown;
142
162
  }>;
143
163
  writable: false;
144
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
164
+ indexes?: Record<string, {
165
+ on: (string | string[]) & (string | string[] | undefined);
166
+ unique?: boolean | undefined;
167
+ }> | undefined;
168
+ table?: any;
169
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
145
170
  }, {
146
171
  fields: Record<string, {
147
172
  type: "boolean";
@@ -178,7 +203,12 @@ export declare const dbConfigSchema: z.ZodObject<{
178
203
  default?: unknown;
179
204
  }>;
180
205
  writable: false;
181
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
206
+ indexes?: Record<string, {
207
+ on: (string | string[]) & (string | string[] | undefined);
208
+ unique?: boolean | undefined;
209
+ }> | undefined;
210
+ table?: any;
211
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
182
212
  }>, z.ZodObject<{
183
213
  fields: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodObject<{
184
214
  label: z.ZodOptional<z.ZodString>;
@@ -280,7 +310,18 @@ export declare const dbConfigSchema: z.ZodObject<{
280
310
  unique?: boolean | undefined;
281
311
  default?: unknown;
282
312
  }>]>>;
283
- seed: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">, z.ZodPromise<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">>]>>>;
313
+ indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
314
+ on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
315
+ unique: z.ZodOptional<z.ZodBoolean>;
316
+ }, "strip", z.ZodTypeAny, {
317
+ on: (string | string[]) & (string | string[] | undefined);
318
+ unique?: boolean | undefined;
319
+ }, {
320
+ on: (string | string[]) & (string | string[] | undefined);
321
+ unique?: boolean | undefined;
322
+ }>>>;
323
+ table: z.ZodAny;
324
+ _setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>;
284
325
  writable: z.ZodLiteral<true>;
285
326
  }, "strip", z.ZodTypeAny, {
286
327
  fields: Record<string, {
@@ -318,7 +359,12 @@ export declare const dbConfigSchema: z.ZodObject<{
318
359
  default?: unknown;
319
360
  }>;
320
361
  writable: true;
321
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
362
+ indexes?: Record<string, {
363
+ on: (string | string[]) & (string | string[] | undefined);
364
+ unique?: boolean | undefined;
365
+ }> | undefined;
366
+ table?: any;
367
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
322
368
  }, {
323
369
  fields: Record<string, {
324
370
  type: "boolean";
@@ -355,9 +401,14 @@ export declare const dbConfigSchema: z.ZodObject<{
355
401
  default?: unknown;
356
402
  }>;
357
403
  writable: true;
358
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
404
+ indexes?: Record<string, {
405
+ on: (string | string[]) & (string | string[] | undefined);
406
+ unique?: boolean | undefined;
407
+ }> | undefined;
408
+ table?: any;
409
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
359
410
  }>]>>>;
360
- data: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodUnknown>>;
411
+ data: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodVoid, z.ZodPromise<z.ZodVoid>]>>>;
361
412
  }, "strip", z.ZodTypeAny, {
362
413
  studio?: boolean | undefined;
363
414
  collections?: Record<string, {
@@ -396,7 +447,12 @@ export declare const dbConfigSchema: z.ZodObject<{
396
447
  default?: unknown;
397
448
  }>;
398
449
  writable: false;
399
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
450
+ indexes?: Record<string, {
451
+ on: (string | string[]) & (string | string[] | undefined);
452
+ unique?: boolean | undefined;
453
+ }> | undefined;
454
+ table?: any;
455
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
400
456
  } | {
401
457
  fields: Record<string, {
402
458
  type: "boolean";
@@ -433,9 +489,14 @@ export declare const dbConfigSchema: z.ZodObject<{
433
489
  default?: unknown;
434
490
  }>;
435
491
  writable: true;
436
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
492
+ indexes?: Record<string, {
493
+ on: (string | string[]) & (string | string[] | undefined);
494
+ unique?: boolean | undefined;
495
+ }> | undefined;
496
+ table?: any;
497
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
437
498
  }> | undefined;
438
- data?: ((args_0: any, ...args_1: unknown[]) => unknown) | undefined;
499
+ data?: ((...args: unknown[]) => void | Promise<void>) | undefined;
439
500
  }, {
440
501
  studio?: boolean | undefined;
441
502
  collections?: Record<string, {
@@ -474,7 +535,12 @@ export declare const dbConfigSchema: z.ZodObject<{
474
535
  default?: unknown;
475
536
  }>;
476
537
  writable: false;
477
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
538
+ indexes?: Record<string, {
539
+ on: (string | string[]) & (string | string[] | undefined);
540
+ unique?: boolean | undefined;
541
+ }> | undefined;
542
+ table?: any;
543
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
478
544
  } | {
479
545
  fields: Record<string, {
480
546
  type: "boolean";
@@ -511,11 +577,18 @@ export declare const dbConfigSchema: z.ZodObject<{
511
577
  default?: unknown;
512
578
  }>;
513
579
  writable: true;
514
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
580
+ indexes?: Record<string, {
581
+ on: (string | string[]) & (string | string[] | undefined);
582
+ unique?: boolean | undefined;
583
+ }> | undefined;
584
+ table?: any;
585
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
515
586
  }> | undefined;
516
- data?: ((args_0: any, ...args_1: unknown[]) => unknown) | undefined;
587
+ data?: ((...args: unknown[]) => void | Promise<void>) | undefined;
517
588
  }>;
518
- export type DBUserConfig = z.input<typeof dbConfigSchema>;
589
+ export type DBUserConfig = Omit<z.input<typeof dbConfigSchema>, 'data'> & {
590
+ data(params: DBDataContext): MaybePromise<void>;
591
+ };
519
592
  export declare const astroConfigWithDbSchema: z.ZodObject<{
520
593
  db: z.ZodOptional<z.ZodObject<{
521
594
  studio: z.ZodOptional<z.ZodBoolean>;
@@ -620,7 +693,18 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
620
693
  unique?: boolean | undefined;
621
694
  default?: unknown;
622
695
  }>]>>;
623
- data: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">, z.ZodPromise<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">>]>>>;
696
+ indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
697
+ on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
698
+ unique: z.ZodOptional<z.ZodBoolean>;
699
+ }, "strip", z.ZodTypeAny, {
700
+ on: (string | string[]) & (string | string[] | undefined);
701
+ unique?: boolean | undefined;
702
+ }, {
703
+ on: (string | string[]) & (string | string[] | undefined);
704
+ unique?: boolean | undefined;
705
+ }>>>;
706
+ table: z.ZodAny;
707
+ _setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>;
624
708
  writable: z.ZodLiteral<false>;
625
709
  }, "strip", z.ZodTypeAny, {
626
710
  fields: Record<string, {
@@ -658,7 +742,12 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
658
742
  default?: unknown;
659
743
  }>;
660
744
  writable: false;
661
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
745
+ indexes?: Record<string, {
746
+ on: (string | string[]) & (string | string[] | undefined);
747
+ unique?: boolean | undefined;
748
+ }> | undefined;
749
+ table?: any;
750
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
662
751
  }, {
663
752
  fields: Record<string, {
664
753
  type: "boolean";
@@ -695,7 +784,12 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
695
784
  default?: unknown;
696
785
  }>;
697
786
  writable: false;
698
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
787
+ indexes?: Record<string, {
788
+ on: (string | string[]) & (string | string[] | undefined);
789
+ unique?: boolean | undefined;
790
+ }> | undefined;
791
+ table?: any;
792
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
699
793
  }>, z.ZodObject<{
700
794
  fields: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodObject<{
701
795
  label: z.ZodOptional<z.ZodString>;
@@ -797,7 +891,18 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
797
891
  unique?: boolean | undefined;
798
892
  default?: unknown;
799
893
  }>]>>;
800
- seed: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">, z.ZodPromise<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">>]>>>;
894
+ indexes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
895
+ on: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
896
+ unique: z.ZodOptional<z.ZodBoolean>;
897
+ }, "strip", z.ZodTypeAny, {
898
+ on: (string | string[]) & (string | string[] | undefined);
899
+ unique?: boolean | undefined;
900
+ }, {
901
+ on: (string | string[]) & (string | string[] | undefined);
902
+ unique?: boolean | undefined;
903
+ }>>>;
904
+ table: z.ZodAny;
905
+ _setMeta: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnknown>>;
801
906
  writable: z.ZodLiteral<true>;
802
907
  }, "strip", z.ZodTypeAny, {
803
908
  fields: Record<string, {
@@ -835,7 +940,12 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
835
940
  default?: unknown;
836
941
  }>;
837
942
  writable: true;
838
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
943
+ indexes?: Record<string, {
944
+ on: (string | string[]) & (string | string[] | undefined);
945
+ unique?: boolean | undefined;
946
+ }> | undefined;
947
+ table?: any;
948
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
839
949
  }, {
840
950
  fields: Record<string, {
841
951
  type: "boolean";
@@ -872,9 +982,14 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
872
982
  default?: unknown;
873
983
  }>;
874
984
  writable: true;
875
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
985
+ indexes?: Record<string, {
986
+ on: (string | string[]) & (string | string[] | undefined);
987
+ unique?: boolean | undefined;
988
+ }> | undefined;
989
+ table?: any;
990
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
876
991
  }>]>>>;
877
- data: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodUnknown>>;
992
+ data: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodVoid, z.ZodPromise<z.ZodVoid>]>>>;
878
993
  }, "strip", z.ZodTypeAny, {
879
994
  studio?: boolean | undefined;
880
995
  collections?: Record<string, {
@@ -913,7 +1028,12 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
913
1028
  default?: unknown;
914
1029
  }>;
915
1030
  writable: false;
916
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1031
+ indexes?: Record<string, {
1032
+ on: (string | string[]) & (string | string[] | undefined);
1033
+ unique?: boolean | undefined;
1034
+ }> | undefined;
1035
+ table?: any;
1036
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
917
1037
  } | {
918
1038
  fields: Record<string, {
919
1039
  type: "boolean";
@@ -950,9 +1070,14 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
950
1070
  default?: unknown;
951
1071
  }>;
952
1072
  writable: true;
953
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1073
+ indexes?: Record<string, {
1074
+ on: (string | string[]) & (string | string[] | undefined);
1075
+ unique?: boolean | undefined;
1076
+ }> | undefined;
1077
+ table?: any;
1078
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
954
1079
  }> | undefined;
955
- data?: ((args_0: any, ...args_1: unknown[]) => unknown) | undefined;
1080
+ data?: ((...args: unknown[]) => void | Promise<void>) | undefined;
956
1081
  }, {
957
1082
  studio?: boolean | undefined;
958
1083
  collections?: Record<string, {
@@ -991,7 +1116,12 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
991
1116
  default?: unknown;
992
1117
  }>;
993
1118
  writable: false;
994
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1119
+ indexes?: Record<string, {
1120
+ on: (string | string[]) & (string | string[] | undefined);
1121
+ unique?: boolean | undefined;
1122
+ }> | undefined;
1123
+ table?: any;
1124
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
995
1125
  } | {
996
1126
  fields: Record<string, {
997
1127
  type: "boolean";
@@ -1028,9 +1158,14 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
1028
1158
  default?: unknown;
1029
1159
  }>;
1030
1160
  writable: true;
1031
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1161
+ indexes?: Record<string, {
1162
+ on: (string | string[]) & (string | string[] | undefined);
1163
+ unique?: boolean | undefined;
1164
+ }> | undefined;
1165
+ table?: any;
1166
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
1032
1167
  }> | undefined;
1033
- data?: ((args_0: any, ...args_1: unknown[]) => unknown) | undefined;
1168
+ data?: ((...args: unknown[]) => void | Promise<void>) | undefined;
1034
1169
  }>>;
1035
1170
  }, "strip", z.ZodTypeAny, {
1036
1171
  db?: {
@@ -1071,7 +1206,12 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
1071
1206
  default?: unknown;
1072
1207
  }>;
1073
1208
  writable: false;
1074
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1209
+ indexes?: Record<string, {
1210
+ on: (string | string[]) & (string | string[] | undefined);
1211
+ unique?: boolean | undefined;
1212
+ }> | undefined;
1213
+ table?: any;
1214
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
1075
1215
  } | {
1076
1216
  fields: Record<string, {
1077
1217
  type: "boolean";
@@ -1108,9 +1248,14 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
1108
1248
  default?: unknown;
1109
1249
  }>;
1110
1250
  writable: true;
1111
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1251
+ indexes?: Record<string, {
1252
+ on: (string | string[]) & (string | string[] | undefined);
1253
+ unique?: boolean | undefined;
1254
+ }> | undefined;
1255
+ table?: any;
1256
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
1112
1257
  }> | undefined;
1113
- data?: ((args_0: any, ...args_1: unknown[]) => unknown) | undefined;
1258
+ data?: ((...args: unknown[]) => void | Promise<void>) | undefined;
1114
1259
  } | undefined;
1115
1260
  }, {
1116
1261
  db?: {
@@ -1151,7 +1296,12 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
1151
1296
  default?: unknown;
1152
1297
  }>;
1153
1298
  writable: false;
1154
- data?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1299
+ indexes?: Record<string, {
1300
+ on: (string | string[]) & (string | string[] | undefined);
1301
+ unique?: boolean | undefined;
1302
+ }> | undefined;
1303
+ table?: any;
1304
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
1155
1305
  } | {
1156
1306
  fields: Record<string, {
1157
1307
  type: "boolean";
@@ -1188,27 +1338,30 @@ export declare const astroConfigWithDbSchema: z.ZodObject<{
1188
1338
  default?: unknown;
1189
1339
  }>;
1190
1340
  writable: true;
1191
- seed?: ((...args: unknown[]) => Record<string, unknown>[] | Promise<Record<string, unknown>[]>) | undefined;
1341
+ indexes?: Record<string, {
1342
+ on: (string | string[]) & (string | string[] | undefined);
1343
+ unique?: boolean | undefined;
1344
+ }> | undefined;
1345
+ table?: any;
1346
+ _setMeta?: ((...args: unknown[]) => unknown) | undefined;
1192
1347
  }> | undefined;
1193
- data?: ((args_0: any, ...args_1: unknown[]) => unknown) | undefined;
1348
+ data?: ((...args: unknown[]) => void | Promise<void>) | undefined;
1194
1349
  } | undefined;
1195
1350
  }>;
1196
- type CollectionConfig<TFields extends z.input<typeof collectionSchema>['fields'], Writable extends boolean> = Writable extends true ? {
1197
- fields: TFields;
1198
- seed?: Writable extends false ? never : () => MaybePromise<Array<Record<keyof TFields, any> & {
1199
- id?: string;
1200
- }>>;
1201
- } : {
1351
+ export type FieldsConfig = z.input<typeof collectionSchema>['fields'];
1352
+ interface CollectionConfig<TFields extends FieldsConfig> extends Pick<z.input<typeof collectionSchema>, 'fields' | 'indexes'> {
1202
1353
  fields: TFields;
1203
- data?: Writable extends true ? never : () => MaybePromise<Array<Record<keyof TFields, any> & {
1204
- id?: string;
1205
- }>>;
1206
- };
1207
- type ResolvedCollectionConfig<TFields extends z.input<typeof collectionSchema>['fields'], Writable extends boolean> = CollectionConfig<TFields, Writable> & {
1354
+ indexes?: Record<string, IndexConfig<TFields>>;
1355
+ }
1356
+ interface IndexConfig<TFields extends FieldsConfig> extends z.input<typeof indexSchema> {
1357
+ on: MaybeArray<Extract<keyof TFields, string>>;
1358
+ }
1359
+ export type ResolvedCollectionConfig<TFields extends FieldsConfig = FieldsConfig, Writable extends boolean = boolean> = CollectionConfig<TFields> & {
1208
1360
  writable: Writable;
1361
+ table: Table<string, TFields>;
1209
1362
  };
1210
- export declare function defineCollection<TFields extends z.input<typeof collectionSchema>['fields']>(userConfig: CollectionConfig<TFields, false>): ResolvedCollectionConfig<TFields, false>;
1211
- export declare function defineWritableCollection<TFields extends z.input<typeof collectionSchema>['fields']>(userConfig: CollectionConfig<TFields, true>): ResolvedCollectionConfig<TFields, true>;
1363
+ export declare function defineCollection<TFields extends FieldsConfig>(userConfig: CollectionConfig<TFields>): ResolvedCollectionConfig<TFields, false>;
1364
+ export declare function defineWritableCollection<TFields extends FieldsConfig>(userConfig: CollectionConfig<TFields>): ResolvedCollectionConfig<TFields, true>;
1212
1365
  export type AstroConfigWithDB = z.infer<typeof astroConfigWithDbSchema>;
1213
1366
  type FieldOpts<T extends DBFieldInput> = Omit<T, 'type'>;
1214
1367
  export declare const field: {
package/dist/config.js CHANGED
@@ -5,22 +5,39 @@ import { z } from "zod";
5
5
  const dbConfigSchema = z.object({
6
6
  studio: z.boolean().optional(),
7
7
  collections: collectionsSchema.optional(),
8
- // TODO: strict types
9
- data: z.function().args(z.any()).optional()
8
+ data: z.function().returns(z.union([z.void(), z.promise(z.void())])).optional()
10
9
  });
11
10
  const astroConfigWithDbSchema = z.object({
12
11
  db: dbConfigSchema.optional()
13
12
  });
14
13
  function defineCollection(userConfig) {
14
+ const meta = { table: null };
15
+ function _setMeta(values) {
16
+ Object.assign(meta, values);
17
+ }
15
18
  return {
16
19
  ...userConfig,
17
- writable: false
20
+ writable: false,
21
+ get table() {
22
+ return meta.table;
23
+ },
24
+ // @ts-expect-error private field
25
+ _setMeta
18
26
  };
19
27
  }
20
28
  function defineWritableCollection(userConfig) {
29
+ const meta = { table: null };
30
+ function _setMeta(values) {
31
+ Object.assign(meta, values);
32
+ }
21
33
  return {
22
34
  ...userConfig,
23
- writable: true
35
+ writable: true,
36
+ get table() {
37
+ return meta.table;
38
+ },
39
+ // @ts-expect-error private field
40
+ _setMeta
24
41
  };
25
42
  }
26
43
  const baseDefaults = {
@@ -0,0 +1,2 @@
1
+ import type { AstroIntegration } from 'astro';
2
+ export declare function fileURLIntegration(): AstroIntegration;
@@ -0,0 +1,71 @@
1
+ import fs from "node:fs";
2
+ import { pathToFileURL } from "node:url";
3
+ import path from "node:path";
4
+ async function copyFile(toDir, fromUrl, toUrl) {
5
+ await fs.promises.mkdir(toDir, { recursive: true });
6
+ await fs.promises.rename(fromUrl, toUrl);
7
+ }
8
+ function fileURLIntegration() {
9
+ const fileNames = [];
10
+ function createVitePlugin(command) {
11
+ const referenceIds = [];
12
+ return {
13
+ name: "@astrojs/db/file-url",
14
+ enforce: "pre",
15
+ async load(id) {
16
+ if (id.endsWith("?file-url")) {
17
+ const filePath = id.slice(0, id.indexOf("?"));
18
+ if (command === "build") {
19
+ const data = await fs.promises.readFile(filePath);
20
+ const name = path.basename(filePath);
21
+ const referenceId = this.emitFile({
22
+ name,
23
+ source: data,
24
+ type: "asset"
25
+ });
26
+ referenceIds.push(referenceId);
27
+ return `export default import.meta.ROLLUP_FILE_URL_${referenceId};`;
28
+ } else {
29
+ return `export default new URL(${JSON.stringify(pathToFileURL(filePath).toString())})`;
30
+ }
31
+ }
32
+ },
33
+ generateBundle() {
34
+ for (const referenceId of referenceIds) {
35
+ fileNames.push(this.getFileName(referenceId));
36
+ }
37
+ }
38
+ };
39
+ }
40
+ let config;
41
+ return {
42
+ name: "@astrojs/db/file-url",
43
+ hooks: {
44
+ "astro:config:setup"({ updateConfig, command }) {
45
+ updateConfig({
46
+ vite: {
47
+ plugins: [createVitePlugin(command)]
48
+ }
49
+ });
50
+ },
51
+ "astro:config:done": ({ config: _config }) => {
52
+ config = _config;
53
+ },
54
+ async "astro:build:done"() {
55
+ if (config.output !== "static") {
56
+ const moves = [];
57
+ for (const fileName of fileNames) {
58
+ const fromUrl = new URL(fileName, config.build.client);
59
+ const toUrl = new URL(fileName, config.build.server);
60
+ const toDir = new URL("./", toUrl);
61
+ moves.push(copyFile(toDir, fromUrl, toUrl));
62
+ }
63
+ await Promise.all(moves);
64
+ }
65
+ }
66
+ }
67
+ };
68
+ }
69
+ export {
70
+ fileURLIntegration
71
+ };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export { defineCollection, defineWritableCollection, field } from './config.js';
2
+ export type { ResolvedCollectionConfig, DBDataContext } from './config.js';
3
+ export type { DBCollection, DBCollections, DBSnapshot, DBField, BooleanField, NumberField, TextField, DateField, DateFieldInput, JsonField, FieldType, } from './types.js';
2
4
  export { cli } from './cli/index.js';
3
5
  export { integration as default } from './integration.js';
@@ -1,2 +1,2 @@
1
1
  import type { AstroIntegration } from 'astro';
2
- export declare function integration(): AstroIntegration;
2
+ export declare function integration(): AstroIntegration[];