npm package: @laboratoria/sdk-js
Description: Laboratoria JavaScript (browser) SDK
Latest version: 8.1.0

## Laboratoria JavaScript (browser) SDK

[![Node.js CI](https://github.com/Laboratoria/sdk-js/actions/workflows/node.js.yml/badge.svg)](https://github.com/Laboratoria/sdk-js/actions/workflows/node.js.yml)

:warning: This tool is still in draft stage and is likely to change without
notice.

## Installation

```sh
npm i --save @laboratoria/sdk-js
```

## Usage


```js
import { createApp } from '@laboratoria/sdk-js';

const app = createApp();
```

With options:

```js
const app = createApp({
  firebaseApiKey: '',
  firebaseProject: '',
  coreApiUrl: '',
});
```

## API

### Auth

```js
app.auth.onChange(({ authUser, user }) => {
  // `authUser` is the Firebase Auth user
  // `user` is the user profile stored in Laboratoria Core
});

app.auth.signIn(email, password);
app.auth.signOut();
```

### Models

The `app` object exposes data models used to access data. Each model represents
a _type_ of object, which is stored in a database table.

All models have the following methods and properties.

#### CRUD methods

* `create`
* `delete`
* `findById`
* `findMany`
* `update`
* `upsert`

#### Other methods

* `parse`
* `stats`
* `validate`
* `validateAttr`

#### Properties

* `relations`
* `schema`

### Model Schema

Each _model_ has a _schema_ (`model.schema`) that describes the _shape_ of the
data, what properties (columns) it has, what data types these columns are,
whether they are _required_ or not, etc.

The `model.schema` is an object with the following properties:

* `inputProps`: An array listing properties to be taken into account when
  building input UI or sending data (via `model.create`, `model.update`, etc).
* `primaryKey`: Primary key in the underlying table.
* `properties`: An object with the model properties. Each property is described
  by an object having the following keys:
  - `type`: It can be: `boolean`, `integer`, `string`, `date` o una referncia a
    un modelo, en cuyo caso el string será el nombre del modelo de prisma.
  - `default`
  - `enum`
  - `format`: `date-time`
  - `$ref`
  - `anyOf`
  - `items`
  - `inputType`
  - `isRequired`
  - `isScalar`
  - `isRef`
* `type`: The schema for the whole model will always be of type `object`.

***

### Users

```js
app.user.findMany({
  where: { name: { contains: 'foo' } },
  include: { students: true },
  take: 10,
  skip: 0,
});

app.user.findById('some-firebase-uid', {
  include: { gigs: true },
});

app.user.update({
  where: { uid: 'some-firebase-uid' },
  data: {
    name: 'OMG',
  },
});

app.user.delete('some-firebase-uid');

app.user.create({
  data: {
    name: 'OMG',
    email: 'omg@omg.omg',
    password: 'secret',
  },
});
```

### Cohorts

```js
app.cohort.findMany({
  where: {
    end: { gt: new Date() },
    stage: { name: 'bootcamp' },
  },
  include: {
    program: true,
    stage: true,
    students: true,
    dropouts: true,
  },
})
```

### Students

WIP

### Dropouts

WIP

### Projects

```js
app.project.findMany();

app.project.findMany({ tag: 'v6.5.0' });

app.project.findMany({
  track: 'web-dev',
});

app.project.findById('cipher');

app.project.findById('cipher', { tag: 'v6.5.0' });
```

### Topics

```js
app.topic.findMany();

app.topic.findMany({ tag: 'v6.5.0' });

app.topic.findMany({
  track: 'web-dev',
});

app.topic.findById('javascript');

app.topic.findById('javascript', { tag: 'v6.5.0' });
```

### Learning Objectives

```js
app.learningObjective.findMany();

app.learningObjective.findMany({ tag: 'v6.5.0' });
```