
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@anrok/mammoth
Advanced tools
This is a fork of [@ff00ff/mammoth](https://github.com/Ff00ff/mammoth). We made this fork because the upstream project has not been active. We'd love to get rid of this fork if that changes.
This is a fork of @ff00ff/mammoth. We made this fork because the upstream project has not been active. We'd love to get rid of this fork if that changes.
This fork is based on the 1.x version rather than newer 2.x version. We made this decision because the 2.x version is still in alpha, and we ran into memory issues during TS compilation in the probject we were using it for. We start from c7c525c978aa6326042b35ca95f93699a69370c5 because that is the last commit before the big 2.x refactor.
null
data type, rather than undefined
.with
query to return the value provided by constructed query.VALUES
lists.LATERAL
joins.Column
objects now contain a reference to their ColumnDefinition
.SELECT DISTINCT
.📖 Work-in-progress documentation site is available at https://mammoth.tools/.
npm i @ff00ff/mammoth
Mammoth is a type-safe query builder. It only supports Postgres which we consider a feature. It's syntax is as close to SQL as possible so you already know how to use it. It's autocomplete features are great. It helps you avoid mistakes so you can develop applications faster.
const rows = await db
.select(star())
.from(db.foo)
.leftJoin(db.bar)
.on(db.foo.barId.eq(db.bar.id))
.where(db.foo.id.eq(`1`));
The above query produces the following SQL:
SELECT
*
FROM foo
LEFT JOIN bar ON (foo.bar_id = bar.id)
WHERE
foo.id = $1
More importantly, the resulting type of rows is { id: string; barId: string; name: string | undefined }[]
. Notice how the name is automatically nullable because of the left join.
const updateCount = await db.update(db.foo).set({ name: `Test` }).where(db.foo.value.gt(0));
UPDATE foo
SET
name = $1
WHERE
value > $2
const rows = await db
.insertInto(db.foo)
.values({
name: `Test`,
value: 123,
})
.returning(`id`);
INSERT INTO foo (
name,
value
) VALUES (
$1,
$2
)
RETURNING
id
const affectedCount = await db
.insertInto(db.foo, ['name'])
.select(db.bar.name)
.from(db.bar)
.where(db.bar.name.isNotNull());
INSERT INTO foo (name)
SELECT
bar.name
FROM bar
WHERE
bar.name IS NOT NULL
db.select(count()).from(db.foo);
SELECT COUNT(*) FROM foo
db.select(arrayAgg(db.foo.name.orderBy(db.foo.name.desc()))).from(db.foo);
SELECT array_agg(foo.name ORDER BY foo.name DESC) "arrayAgg" FROM foo
db.with(
`regionalSales`,
() =>
db
.select(db.orderLog.region, sum(db.orderLog.amount).as(`totalSales`))
.from(db.orderLog)
.groupBy(db.orderLog.region),
`topRegions`,
({ regionalSales }) =>
db
.select(regionalSales.region)
.from(regionalSales)
.where(
regionalSales.totalSales.gt(
db.select(sum(regionalSales.totalSales).divide(10)).from(regionalSales),
),
),
({ topRegions }) =>
db
.select(
db.orderLog.region,
db.orderLog.product,
sum(db.orderLog.quantity).as(`productUnits`),
sum(db.orderLog.amount).as(`productSales`),
)
.from(db.orderLog)
.where(db.orderLog.region.in(db.select(topRegions.region).from(topRegions)))
.groupBy(db.orderLog.region, db.orderLog.product),
);
WITH "regionalSales" AS (SELECT order_log.region, SUM (order_log.amount) "totalSales" FROM order_log GROUP BY order_log.region), "topRegions" AS (SELECT "regionalSales".region FROM "regionalSales" WHERE "regionalSales"."totalSales" > (SELECT SUM ("regionalSales"."totalSales") / $1 FROM "regionalSales")) SELECT order_log.region, order_log.product, SUM (order_log.quantity) "productUnits", SUM (order_log.amount) "productSales" FROM order_log WHERE order_log.region IN (SELECT "topRegions".region FROM "topRegions") GROUP BY order_log.region, order_log.product
Mammoth is a query builder pur sang so it doesn't include a database driver. You need to create a db and pass a callback to execute the query.
import { defineDb } from '@ff00ff/mammoth';
import { foo, bar } from './tables';
const db = defineDb({ foo, bar }, async (query, parameters) => {
const result = await pool.query(query, parameters);
return {
affectedCount: result.rowCount,
rows: result.rows,
};
});
In the defineDb
call you pass all your tables so they can be access through the db instance. You have to define all the tables to make sure Mammoth understands the type information. This should be close to the CREATE TABLE syntax.
const foo = defineTable({
id: uuid().primaryKey().default(`gen_random_uuid()`),
createDate: timestampWithTimeZone().notNull().default(`now()`),
name: text().notNull(),
value: integer(),
});
You should keep your column names camelCase in the defineTable call as they are automatically transformed to train_case throughout Mammoth.
Below is a list of clauses per query and a short description on what we Mammoth supports.
Now that we've hit 1.0 Mammoth will stick to semantic versioning, meaning, breaking changes will only be included in major updates.
Once you clone the repo, do a npm install
+ npm run build
. Now you should be able to run npm test
seeing everything turn green. Feel free to pick up one of the open issues — in particular you can pick up one labeled with "good first issue". Be sure to claim the issue before you start so we avoid two or more people working on the same thing.
FAQs
This is a fork of [@ff00ff/mammoth](https://github.com/Ff00ff/mammoth). We made this fork because the upstream project has not been active. We'd love to get rid of this fork if that changes.
We found that @anrok/mammoth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.