# tower-resource

> Data models that can be stored in any database or remote service using adapters.

Latest version **0.1.0** (published 2013-06-24) · 0 weekly downloads

## Install

```sh
npm install tower-resource
pnpm add tower-resource
yarn add tower-resource
bun add tower-resource
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2013-06-24 |
| First published | 2013-06-02 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | viatropos |
| Keywords | collection, model, resource, orm, db, database, mvc |

## Links

- npm: https://www.npmjs.com/package/tower-resource
- Repository: http://github.com/tower/resource
- npm.io page: https://npm.io/package/tower-resource

## Dependencies (8)

- [tower-attr](https://npm.io/package/tower-attr.md) ~0.1.0
- [tower-load](https://npm.io/package/tower-load.md) ~0.1.0
- [tower-text](https://npm.io/package/tower-text.md) ~0.1.0
- [tower-query](https://npm.io/package/tower-query.md) ~0.1.0
- [tower-stream](https://npm.io/package/tower-stream.md) ~0.1.0
- [tower-emitter](https://npm.io/package/tower-emitter.md) ~0.1.0
- [tower-validator](https://npm.io/package/tower-validator.md) ~0.1.0
- [part-async-series](https://npm.io/package/part-async-series.md) *

## 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.0 (latest) — 2013-06-24
- 0.1.0pre — 2013-06-02

## README

# Tower Resource

Data models that can be stored in any database or remote service using adapters.

## Installation

node:

```bash
$ npm install tower-resource
```

browser:

```bash
$ component install tower/resource
```

## Examples

```js
var resource = require('tower-resource');
```

### Attributes

```js
resource('post')
  .attr('title') // defaults to 'string'
  .attr('body', 'text')
  .attr('published', 'boolean', false);
```

### Validations

```js
resource('user')
  .attr('email')
    .validate('presence')
    .validate('isEmail')
    .validate('emailProvider', { in: [ 'gmail.com' ] }) // some hypothetical one
  .attr('username')
    .validator(function(val){
      return !!val.match(/[a-zA-Z]/);
    });
```

There are two DSL methods for validation.

1. `validate`: for using predefined validations (see [tower-validator](https://github.com/tower/validator)), purely to clean up the API.
2. `validator`: for defining custom validator functions right inline. If you want to reuse your custom validator function across resources, just move the function into tower-validator.

### Queries

```js
resource('post')
  .where('published').eq(true)
  .all(function(err, posts){

  });
```

See [tower-query]() for the entire syntax. The `where` method just delegates to a `Query` instance. You can also access the query object directly (it just adds `.select(resourceName)` for you):

```js
resource('post').query().sort('title', -1).all();
```

### Actions

There are 4 main actions for resources (which are just delegated to `query().action(name)`:

- create
- all
- update
- remove

```js
resource('post').create();
resource('post').all();
resource('post').update({ published: true });
resource('post').remove();
```

Under the hood, when you execute one of these actions, they get handled by a database-/service-specific adapter (mongodb, cassandra, facebook, etc.). Those adapters can perform optimizations such as streaming query results back.

## License

MIT

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