# data-mapper-js

> A lightweight data access tool supporting Postgres and MySql today.

Latest version **0.1.1** (published 2012-11-14) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install data-mapper-js
pnpm add data-mapper-js
yarn add data-mapper-js
bun add data-mapper-js
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2012-11-14 |
| First published | 2012-11-14 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+4 in 3 direct dependencies) |
| Install scripts | no |
| Author | Brad Teller |
| Maintainers | bteller |
| Keywords | mysql, postgres, orm, database |

## Links

- npm: https://www.npmjs.com/package/data-mapper-js
- Repository: https://github.com/bteller/data-mapper-js
- npm.io page: https://npm.io/package/data-mapper-js

## Dependencies (3)

- [pg](https://npm.io/package/pg.md) 0.6.17
- [mysql](https://npm.io/package/mysql.md) 0.9.6
- [underscore](https://npm.io/package/underscore.md) 1.3.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2012-11-14

## README

## Okay, so what is it?

data-mapper-js is a little bit more than massive-js, but not so much that it gets in your way. Initially I was going to try and just extend massive-js to meet my needs, but my approach seemed different enough from that of massive-js that I gave birth to data-mapper-js. Currently it supports MySql and Postgres, but you should find it easily extensible, if you are so inclined.

## How do I get started?

I wasn't going to, but I went ahead and registered the package so installation is as easy as:

```
npm install data-mapper-js
```

Then just this to start using it:

```javascript
var mapper = require("data-mapper-js");
```

The important part, if you are trying to wire up any relational data queries, is to define the schema. 

```javascript
var schemaBuilder = mapper.schema();
	schemaBuilder
		.add("customers", "customerid")
			.hasManyWithMany("addresses", "customeraddresses", "addressid")
			.hasOne("profile", "profiles")
			.save()
		.add("addresses", "addressid").save()
		.add("profiles").save();
```

And lastly tell it what engine you want to use and how to connect to the database:

```javascript
var db = mapper.init("mysql", connString, schemaBuilder.schema);
```

## Querying

The query syntax is borrowed almost exactly from massive-js, but we don't build the tables for you.

A quick query to pull all the records in the customers table.

```javascript
db.find().in("customers").execute(function(err, result) {
	console.log(result[0].name);
});
```

You can also pass in one or more parameters.

```javascript
db.find({name: "brad", email: "bradley.teller@gmail.com"}).in("customers").execute(function(err, result) {
	console.log(result[0].name);
});
```

You can also tell it to load up the addresses for records matching your query.

```javascript
db.find({name: "bobcat"}).in("customers").load("addresses").execute(function(err, result) {
	console.log(name[0].addresses.length);
});	
```

## Inserts

Nothing really special here.

```javascript
db.insert({name: "new", email: "test@test.com"}).in("customers").execute(function(err, result) {
	//do something here
});
```

## Updates

Yeah, you can update stuff as well.

```javascript
db.update({name: "updated"}).in("customers").where({"customerid >": 1}).execute(function(err, result) {
	//do something here
});
```

## Deletes

And just in case you have to get rid of some things you can do that too.

```javascript
db.delete({"customerid >": 2}).in("customers").execute(function(err, result) {
	assert.equal(err, null);
	done();		
});
```

---
_Source: https://npm.io/package/data-mapper-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
