40
loading...
This website collects cookies to deliver better user experience
staging
.users
table. It'll be an empty table with a few columns.hash_attribute
is the primary key for the table. It's the unique identifier for each row so it should be named something simple to work with. That's why I called it id
here. With this new table in place, we can start working on our Vue app.vue-harperdb
directory or you can make a new Vue app yourself. Vue is a front-end framework that allows developers to build SPAs quickly. To start working with Vue, you'll need to install the Vue CLI tools. Once the CLI tools are installed, we can start building our Vue app by running the following command in a terminal:vue create user-dashboard
HelloWorld.vue
component and remove the references to it in App.vue
. Your App.vue
file should look like this.<template>
</template>
<script>
export default {
name: 'App',
components: {
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Users.vue
file to the components directory. This is where we'll display the table, a button to add new users, and handle our connection to HarperDB.Users.vue
, we need to add the template
for the elements users will see on the page. Keep in mind that we're applying very little styling, so this will look a little rough in the browser.<template>
<div>
<div class="flex">
<h2>Users</h2>
<button v-on:click="showForm = true">Add new user</button>
</div>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" v-bind:key="user.id">
<th scope="row">{{ user.id }}</th>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.city }}</td>
</tr>
</tbody>
</table>
</div>
</template>
v-for
looping through our users so that we can dynamically generate this table. The v-bind:key
is also present because it's used with every instance of v-for
so that Vue can track the dependencies of the individual values so it knows which elements to update.v-on:click="showForm = true"
to the button so that we show a hidden form we'll make a little later.script
to generate some placeholder data in the Users.vue
file until we connect to HarperDB.<script>
import axios from "axios";
export default {
name: "Users",
data() {
return {
users: null,
showForm: false
};
},
mounted() {
axios.get("https://jsonplaceholder.typicode.com/users")
.then((res) => {
this.users = res.data;
});
},
};
</script>
data
object that allows us to access the users
in our template
. Then we have a function to fetch the data from this placeholder API using axios.npm i axios
in your terminal so that the package is available in your app.App.vue
to use the Users.vue
component. So make the following updates to the App.vue
file.<template>
<div id="app">
<users />
</div>
</template>
<script>
import Users from "./components/Users.vue";
export default {
name: 'app',
components: {
Users,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Users
component and rendering it when the app starts up. To see what the app looks like so far, run npm run serve
in your terminal. You should see something like this in your browser.template
in the Users.vue
file.<div v-if="showForm">
<form>
<div>
<label>Name</label>
<input type="text" v-model="formData.name" />
</div>
<div>
<label>City</label>
<input type="text" v-model="formData.city" />
</div>
<div>
<label>Email</label>
<input type="text" v-model="formData.email" />
</div>
<button type="submit" v-on:click="addUser(formData)">Submit</button>
</form>
</div>
v-if='showForm'
is how we will conditionally show the new user form. So when the button is clicked, the showForm
state is updated which shows our form.data
function inside of the Users
script. to hold a few more values and replace the placeholder API call with a call to our HarperDB instance.export default {
name: "Users",
data() {
return {
users: null,
errored: null,
loading: null,
showForm: false,
formData: {
name: "",
email: "",
city: "",
},
};
},
...
.env
file in the root directory of your project to hold your HarperDB authorization key and instance URL. You can get your HarperDB token from HarperDB Studio in the config settings of your instance..env
files to keep our account secrets from being hard-coded on the front-end. These values are usually stored as environment variables in your CI/CD pipeline.VUE_APP_HARPERDB_TOKEN
and the instance URL VUE_APP_HARPERDB_INSTANCE
. Here's what the .env
file will look like.methods
object to our Users
component. This will go inside of the script
after the data
function. The methods
object holds the methods we want to call inside of the template
.addUser
method to make the call to add a user in HarperDB. You'll need to change the URL in the POST
request to your cloud instance.methods: {
addUser: function(data) {
axios
.post(
process.env.VUE_APP_HARPERDB_INSTANCE,
JSON.stringify({
operation: "insert",
schema: "staging",
table: "users",
records: [
{
name: data.name,
city: data.city,
email: data.email,
},
],
}),
{
headers: {
"Content-Type": "application/json",
Authorization: process.env.VUE_APP_HARPERDB_TOKEN,
},
}
)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
this.errored = true;
});
},
},
mounted
lifecycle hook. The mounted
lifecycle hook is only called after the component instance has been mounted. That way the data fetching call is only executed once for the component as soon as its mounted.mounted() {
axios
.post(
process.env.VUE_APP_HARPERDB_INSTANCE,
JSON.stringify({
operation: "sql",
sql: "select * from staging.users",
}),
{
headers: {
"Content-Type": "application/json",
Authorization: process.env.VUE_APP_HARPERDB_TOKEN,
},
}
)
.then((response) => {
this.users = response.data;
})
.catch((error) => {
console.log(error);
this.errored = true;
})
.finally(() => (this.loading = false));
},
post
request, you can see the data in the call to our HarperDB instance is a SQL statement in JSON format. It's executing a SELECT
query on our users
table in the staging
schema.