Mammoth
Search…
Getting started
Of course, first, you need to install Mammoth.
NPM
Yarn
Deno
1
npm i @ff00ff/mammoth
Copied!
1
yarn add @ff00ff/mammoth
Copied!
1
import { defineDb } from 'https://deno.land/x/mammoth'
Copied!
Because Mammoth is a query builder pur sang it doesn’t include a database driver. This means you have to manage your connection pool to your database yourself and pass a query executor function to Mammoth’s defineDb call.
The query executor function, or QueryExecutorFn, receives the query as a string and it’s parameters as arguments and should return a promise with the resulting rows and the affected row count.
Below is an example on how to set this all up. Please note that the pool in this case uses a pg pool but you can use any pool you want.
Node.js
1
import { defineDb } from '@ff00ff/mammoth';
2
import { pool } from './pool';
3
import { foo, bar } from './tables';
4
5
const db = defineDb({ foo, bar }, async (query, parameters) => {
6
const result = await pool.query(query, parameters);
7
8
return {
9
affectedCount: result.rowCount,
10
rows: result.rows,
11
};
12
});
Copied!
As you can see the above example imports a set of tables from ./tables . These are the tables of your database schema. What they do and how they are defined are explained in the next chapter.
Last modified 11mo ago
Copy link