You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A node/express backend API template for getting started with a new project that includes authentication, permissions, and a database configured to
21
+
use [Supabase](https://supabase.io/) or a local/cloud Postgres database.
22
+
23
+
This comes pre-defined with a workspaces model that allows accounts (users) to create workspaces and invite other profiles (users presence within a workspace) to access the workspace (membership). see the [Permissions](#Permissions) section for more information on how permissions are defined.
24
+
25
+
The contents of a workspace is not defined in this template and can be customized to suit the needs of the project.
This project has been setup to use ESM Node. This allows us to use ES6 imports in Node.
32
+
33
+
This uses [tsx](https://github.com/esbuild-kit/tsx) as a dev server and [pkgroll](https://github.com/privatenumber/pkgroll) to bundle and build the project.
21
34
22
35
## Requirements
23
36
@@ -29,15 +42,11 @@ To install volta run the following command in the terminal.
This project has been setup to use ESM Node. This allows us to use ES6 imports in Node.
45
+
You will need a Postgres database to run this project. You can use Docker to run a Postgres database or use a service like [Supabase](https://supabase.com/).
37
46
38
-
This uses [tsx](https://github.com/esbuild-kit/tsx) as a dev server and [pkgroll](https://github.com/privatenumber/pkgroll) to bundle and build the project.
47
+
See the [Database](#Database) section for more information on how to configure the database connection.
39
48
40
-
## ENV
49
+
###ENV
41
50
42
51
Create a .env file in the root of the project and copy the contents of .env.example into it.
43
52
@@ -47,20 +56,11 @@ cp .env.example .env
47
56
48
57
see the section on [Deployment with DigitalOcean](#deployment-with-digitalocean) for more information on how to configure the environment variables for deployment in different environments (eg. development and production).
49
58
50
-
##Setup
59
+
### Install dependencies
51
60
52
61
```
53
62
# install dependencies
54
63
npm i
55
-
56
-
# start the dev server
57
-
npm run dev
58
-
59
-
# make sure to configure the env variables
60
-
cp .env.example .env
61
-
62
-
# view it running on localhost
63
-
curl localhost:3000
64
64
```
65
65
66
66
## Testing
@@ -75,45 +75,39 @@ It's also recommended to install the [vitest extension for vscode](https://marke
75
75
76
76
You can view the database with `npx drizzle-kit studio` or `npm run studio`.
77
77
78
-
You can spin up a local copy of the database with `docker-compose` but this is not required when using Supabase.
78
+
You can spin up a local copy of the database and application with `docker-compose` but this is not required when using the Supabase db.
79
79
80
80
```
81
81
docker compose up -d
82
82
```
83
83
84
-
If you are using the local database and running the application within docker on the host machine you will need to replace the `POSTGRES_HOST` from `localhost` to `host.docker.internal` in the .env file.
85
-
86
-
```
87
-
POSTGRES_HOST=host.docker.internal
88
-
```
84
+
Alternatively you can create a local network and connect the containers to the network.
Then when running the application in docker you can connect to the database with the container name.
101
93
102
-
# start the docker container
103
-
docker run -d -p 3000:3000 node-express-esm
94
+
```bash
95
+
POSTGRES_HOST=mypostgres
96
+
```
104
97
105
-
# view it running on localhost
106
-
curl localhost:3000
98
+
Then run the application in docker and connect to the same network.
99
+
```bash
100
+
docker run --network mynetwork --name node-express -d -p 4000:4000 node-express
107
101
```
108
102
103
+
Note: If you are using a local database and running the application within docker on the host machine you will need to set `POSTGRES_HOST=host.docker.internal` in the .env file. [Read the docs for more info](https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host)
104
+
109
105
### Migrations
110
106
111
107
When the schema/model is changed make sure to create a new migration and run it against the db.
112
108
113
109
1. Create a new migration
114
110
115
-
<!-- TODO create a named migration and pass additional flag to npm. -->
116
-
117
111
```
118
112
npm run migrate:create
119
113
@@ -139,6 +133,28 @@ npm run seed
139
133
140
134
Be sure to update the seeds as new migrations are added.
Aliases can be configured in the import map, defined in package.json#imports.
@@ -153,24 +169,28 @@ This project uses JWT bearer token for authentication. The claims, id and sub mu
153
169
154
170
How permissions work.
155
171
156
-
A resource will have a permission level. A user will have a role/claim.
172
+
A resource will have a permission level for each route method based on users role within the workspace. Workspace permissions can be defined in `./src/helpers/permissions.ts`.
173
+
174
+
Workspace permissions:
175
+
Admin: Highest level of access to all resources within the workspace.
176
+
User: Regular user with limited permissions.
157
177
158
-
Routes will have their permission level defined in `./src/helpers/permissions.ts`
178
+
Resource permissions:
179
+
Owner: Has access to their own resources
159
180
160
-
When a user makes a request to a route the route will check the user's role/claim against the permission level of the resource.
181
+
Account permissions:
182
+
SuperAdmin: Has access to all super only resources.
161
183
162
-
### Route permission levels
184
+
### Workspace route permission levels
163
185
164
-
1. Owner - Route can only be accessed by the owner of the resource. Defined by the id of the resource being accessed matching the id of the user making the request.
165
-
2. User - Can access all resources with user permissions.
166
-
3. Admin - Can access all resources.
186
+
Ensure every request that requires workspace permissions includes a workspace context. This can be done using the `x-workspace-id header`.
167
187
168
-
### Claims / Roles
188
+
Passing the `x-workspace-id` header will allow the user to access the workspace resources if they are a member of the workspace with a sufficient role.
169
189
170
-
A claim is defined when the user is created which defines the user's role and permissions level.
190
+
A role/claim is defined when the account is added to the workspace as a member.
171
191
172
-
1. User - default user permissions
173
-
2. Admin - admin permissions
192
+
1. User - Can access all resources with user permissions.
0 commit comments