@nitwel/sandbox 1.0.0

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/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@nitwel/sandbox",
3
+ "version": "1.0.0",
4
+ "description": "Toolkit for spinning up directus test environments",
5
+ "homepage": "https://directus.io",
6
+ "exports": {
7
+ ".": "./dist/index.js",
8
+ "./package.json": "./package.json"
9
+ },
10
+ "main": "dist/index.js",
11
+ "bin": {
12
+ "sandbox": "dist/cli.js"
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "docker"
17
+ ],
18
+ "keywords": [],
19
+ "type": "module",
20
+ "author": "Nils Twelker (nils@directus.io)",
21
+ "license": "MIT",
22
+ "private": false,
23
+ "devDependencies": {
24
+ "@directus/tsconfig": "3.0.0",
25
+ "@directus/utils": "13.0.9",
26
+ "@types/lodash-es": "4.17.12",
27
+ "@types/node": "22.13.14",
28
+ "chalk": "5.4.1",
29
+ "commander": "14.0.0",
30
+ "copyfiles": "2.4.1",
31
+ "get-port": "7.1.0",
32
+ "lodash-es": "4.17.21",
33
+ "pid-port": "1.0.2",
34
+ "tsdown": "0.14.2",
35
+ "tsx": "4.20.3",
36
+ "typedoc": "0.28.12",
37
+ "typedoc-plugin-markdown": "4.8.1",
38
+ "typescript": "5.8.3",
39
+ "@directus/types": "13.2.3"
40
+ },
41
+ "engines": {
42
+ "node": ">=12.20.0"
43
+ },
44
+ "optionalDependencies": {
45
+ "mysql2": "3.14.3",
46
+ "oracledb": "6.9.0",
47
+ "pg": "8.16.3",
48
+ "sqlite3": "5.1.7",
49
+ "tedious": "18.6.1"
50
+ },
51
+ "scripts": {
52
+ "cli": "tsx src/cli.ts",
53
+ "build": "tsc && copyfiles src/docker/*.yml dist -u 1",
54
+ "dev": "tsc -w ",
55
+ "build:tests": "tsdown src/index.ts --format=esm",
56
+ "docs": "typedoc --plugin typedoc-plugin-markdown --out ./docs",
57
+ "postinstall": "pnpm link"
58
+ }
59
+ }
package/readme.md ADDED
@@ -0,0 +1,70 @@
1
+ # Directus Api Sandbox 🛠️
2
+
3
+ Utility functions for quickly spinning up and down instances of directus for usage such as testing or development.
4
+
5
+ ## Usage
6
+
7
+ The package offers two ways of interacting, either through calling JS functions or through accessing the command line
8
+ interface.
9
+
10
+ ### CLI
11
+
12
+ ```
13
+ Usage: sandbox [options] <database>
14
+
15
+ Arguments:
16
+ database What database to start the api with (choices: "maria", "cockroachdb", "mssql", "mysql", "oracle", "postgres", "sqlite")
17
+
18
+ Options:
19
+ -b, --build Rebuild directus from source
20
+ -d, --dev Start directus in developer mode. Not compatible with build
21
+ -w, --watch Restart the api when changes are made
22
+ --inspect Start the api with debugger (default: true)
23
+ -p, --port <port> Port to start the api on
24
+ -x, --export Export the schema to a file every 2 seconds
25
+ -s, --schema [schema] Load an additional schema snapshot on startup
26
+ --docker.basePort <dockerBasePort> Minimum port number to use for docker containers
27
+ --docker.keep Keep containers running when stopping the sandbox
28
+ --docker.name Overwrite the name of the docker project
29
+ -e, --extras <extras> Enable redis,maildev,saml or other extras
30
+ -i, --instances <instances> Horizontally scale directus to a given number of instances (default: "1")
31
+ --killPorts Forcefully kills all processes that occupy ports that the api would use
32
+ -h, --help display help for command
33
+ ```
34
+
35
+ ### API
36
+
37
+ The api is accessed through the following two functions:
38
+
39
+ - [sandbox](docs/functions/sandbox.md)
40
+ - [sandboxes](docs/functions/sandboxes.md)
41
+
42
+ #### Example
43
+
44
+ ```ts
45
+ import { sandbox } from '@directus/sandbox';
46
+
47
+ const sb = await sandbox('postgres', { dev: true });
48
+
49
+ // Interact via Rest, GQL or WebSockets
50
+ const result = await fetch(sb.env.PUBLIC_URL + '/items/articles');
51
+
52
+ console.log(await result.json());
53
+
54
+ await sb.close();
55
+ ```
56
+
57
+ ## Inner workings
58
+
59
+ Depending on what is set in the configuration, some of these steps might be skipped:
60
+
61
+ 1. **Building of the api**: If enabled, the api is freshly build each time the sandbox is started. Use the `watch`
62
+ option to quickly iterate on changes.
63
+ 2. **Starting of the docker containers**: All required docker containers like databases or extras like redis are spun up
64
+ and awaited until healthy. If the docker containers are still running, they will be reused instead of starting up new
65
+ ones.
66
+ 3. **Bootstrapping of the database**: If not already bootstrapped, the sandbox will make sure that all necessary
67
+ database tables are created.
68
+ 4. **Loading of a schema snapshot**: In case the `schema` option is set, the database will also the snapshot applied
69
+ before starting directus.
70
+ 5. **Starting of the api**: Finally, the api(s) are spun up with the right environment variables configured.