Node.js Tutorial
#
Building a Simple CRUD with Deta Base#
SetupTwo dependencies are needed for this project, deta
and express
:
To configure the app, import the dependencies and instantiate your database.
#
Creating RecordsFor our database we are going to store records of users under a unique key
. Users can have three properties:
We'll expose a function that creates user records to HTTP POST
requests on the route /users
.
#
RequestPOST
a payload to the endpoint:
#
ResponseOur server should respond with a status of 201
and a body of:
#
Reading RecordsTo read records, we can simply use Base.get(key)
.
If we tie a GET
request to the /users
path with a path param giving a user id (i.e. /users/dl9e6w6859a9
), we can return a record of the user over HTTP.
Another option would to use Base.fetch(query)
to search for records to return, like so:
- version < 1.0.0
- version >= 1.0.0
#
RequestLet's try reading the record we just created.
Make a GET
to the path /users/dl9e6w6859a9
.
#
ResponseThe server should return the same record:
#
Updating RecordsTo update records under a given key
, we can use Base.put()
, which will replace the record under a given key.
We can tie a PUT
request to the path /users/{id}
to update a given user record over HTTP.
#
RequestWe can update the record by passing a PUT
to the path /users/dl9e6w6859a9
with the following payload:
#
ResponseOur server should respond with the new body of:
#
Deleting RecordsTo delete records under a given key
, we can use Base.delete(key)
, which will remove the record under a given key.
We can tie a DELETE
request to the path /users/{id}
to delete a given user record over HTTP.
#
RequestWe can delete the record by passing a DELETE
to the path /users/dl9e6w6859a9
.
#
ResponseOur server should respond with:
#
IssuesIf you run into any issues, consider reporting them in our Github Discussions.