kempo 4.5.5 → 4.5.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server/db/index.js +28 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kempo",
3
- "version": "4.5.5",
3
+ "version": "4.5.7",
4
4
  "type": "module",
5
5
  "description": "A fullstack CMS framework for creating websites with user authentication",
6
6
  "scripts": {
@@ -20,7 +20,15 @@ if(existsSync(appSchemaPath) && realpathSync(appSchemaPath) !== realpathSync(fra
20
20
 
21
21
  const schema = { ...frameworkSchema, ...appSchema };
22
22
 
23
- let db;
23
+ /*
24
+ One database per process, whatever URL this module was loaded under. On Windows `c:\x` and `C:\x`
25
+ are different module URLs, so the same file can be evaluated twice (a test preload and a test file,
26
+ say), and the second evaluation would otherwise open a second connection, or boot a second in-memory
27
+ database that the first one's data is not in.
28
+ */
29
+ const SINGLETON = Symbol.for('kempo.database');
30
+
31
+ let db = globalThis[SINGLETON];
24
32
 
25
33
  /*
26
34
  KEMPO_DB=pglite swaps the server for an in-process, in-memory Postgres (PGlite) with the schema
@@ -28,12 +36,28 @@ let db;
28
36
  (jsonb, partial indexes, ON CONFLICT, triggers), not a mock. Nothing outside tests sets it, and
29
37
  PGlite is a dev dependency that is only imported on this branch.
30
38
  */
31
- if(process.env.KEMPO_DB === 'pglite'){
39
+ if(db){
40
+ // already created by an earlier evaluation of this module
41
+ } else if(process.env.KEMPO_DB === 'pglite'){
32
42
  const [{ PGlite }, { drizzle: drizzlePglite }, { generateDrizzleJson, generateMigration }] = await Promise.all([
33
43
  import('@electric-sql/pglite'),
34
44
  import('drizzle-orm/pglite'),
35
45
  import('drizzle-kit/api')
36
46
  ]);
47
+ /*
48
+ A project's drizzle.config.js lists every schema its database holds (its own, kempo's and any
49
+ extensions it sits on), because `drizzle-kit push` needs the same list. Use it, so the in-memory
50
+ database is exactly what a push would have made.
51
+ */
52
+ const configPath = join(process.cwd(), 'drizzle.config.js');
53
+ if(existsSync(configPath)){
54
+ const { default: config } = await import(pathToFileURL(configPath).href);
55
+ for(const entry of [config?.schema].flat().filter(Boolean)){
56
+ const file = join(process.cwd(), entry);
57
+ if(existsSync(file)) Object.assign(schema, await import(pathToFileURL(realpathSync(file)).href));
58
+ }
59
+ }
60
+
37
61
  const client = new PGlite();
38
62
  db = drizzlePglite(client, { schema });
39
63
 
@@ -57,4 +81,6 @@ if(process.env.KEMPO_DB === 'pglite'){
57
81
  db = drizzle(postgres(process.env.DATABASE_URL), { schema });
58
82
  }
59
83
 
84
+ globalThis[SINGLETON] = db;
85
+
60
86
  export default db;