Primate Logo Primate
Guides Databases

Use MySQL

Add MySQL as a database with the @primate/mysql module. Configure it in config/db; Primate connects to it and provides a unified API.

Ensure MySQL is running and accessible.

1) Install

Install the Primate MySQL package.

$ npm install @primate/mysql```



---

### 2) Configure

Create a database configuration file.

<div class="tabbed"><span class="captionline">
    <span class="captions"><span class='active'> TypeScript</span></span><span class="filenames"><span class='active'>config/db.ts</span></span></span><span class="tabs"><div>

```ts
import mysql from "@primate/mysql";

export default mysql({
  host: "localhost",
  port: 3306,
  database: "app",
  // username: "user",
  // password: "pass",
});

3) Create a store

MySQL stores abstract a table. Give the store a name and wire the database.

TypeScriptstores/User.ts
import p from "pema";
import store from "primate/store";
import db from "../config/db.ts";

export default store({
  table: "user",
  db,
  schema: {
    id: store.key.primary(p.uuid),
    name: p.string,
    email: p.string.email(),
  },
});

4) Use the store

Use the store in routes.

TypeScriptroutes/users.ts
import User from "#store/User";
import route from "primate/route";

export default route({
  async get() {
    const users = await User.find({});
    return users;
  },
  async post(request) {
    const user = await User.insert(await request.body.form());
    return user;
  },
});