0.20.1 • Published 2 years ago

@opaquejs/opaque v0.20.1

Weekly downloads
40
License
MIT
Repository
github
Last release
2 years ago

Opaque JS

Take a look at the documentation for more information.

User stories

  • What is this? -> Introduction
  • This is cool, lets get started! -> Quickstart
  • I don't remember how x is done / I want to do y, is this possible? -> Main Part
  • I want to get a deeper understanding of z / How does z actually works (advanced users or adapter authors) -> Digging deeper / Reference

Introduction

You may know the concept of an ORM from server side frameworks like Laravels with the Eloquent ORM or Adonisjs with the Lucid ORM. Opaquejs tries to bring this experience of a unified way of handling your data to your client side Javascript. Opaquejs let's you rewrite this:

let tasks = undefined;
try {
  const res = await axios.post("http://some.website/graphql", {
    query: "{ tasks(done: false) { title createdAt } }",
  });
  tasks = res.data.data.tasks;
} catch (e) {
  // Handle the error somehow
}
for (const task of tasks) {
  const createdAt = Date.parse(task.createdAt);
  if (createdAt > new Date()) {
    // Do something
  }
}

To this:

import Task from "./models/Task";

const tasks = await Task.query().where("done", false).get();

for (const task of tasks) {
  if (task.isCreatedInFuture()) {
    // Do something
  }
}

Opaquejs is framework independent, you can use it with any frontend framework like vue, react and any backend such as GraphQL, a REST API, or even an SQL Database directly. If no integration exists for your intended use case, you can easily develop your own custom adapter to support any possible data source.

Additionally, Opaquejs is completely written in typescript to get the most out of intellisense autocompletion and error detection. But if you are sticking with traditional "vanilla" javascript, you can totally do this. The only difference will be the lack of decorators in plain javascript.

Quickstart

npm i @opaquejs/opaque

The most minimal setup would be a model stored in-memory:

// .../models/Task.ts
import { InMemoryAdapter, OpaqueModel, attribute } from "@opaquejs/opaque";
import { DateTime } from "luxon";

export class Task extends OpaqueModel {
  static adapter = new InMemoryAdapter();

  @attribute({ primary: true })
  public id: string;

  @attribute()
  public title: string;

  @attribute()
  public done: boolean = false;

  @attribute.dateTime()
  public createdAt: DateTime = DateTime.now();
}

You are now ready to use your Model to store tasks in memory!

import { Task } from "./Task";

const task = await Task.create({ title: "My first Task!" });

task.done = true;
await task.save();

const doneTasks = await Task.query().where("done", true).get();

CRUD Operations

Create

Create a model using the normal constructor approach:

import { Task } from "./Task";

const task = new Task();
task.title = "Buy Milk";
await task.save();

The static create method

There is also a helper method to make this task easier:

import { Task } from "./Task";

const task = await Task.create({ title: "Buy Milk" });

Read

all

Fetch all tasks from your defined adapter.

import { Task } from "./Task";

const allTasks = await Task.all();

find

Fetch only one task with the defined primary key. This returns a single task instead of an array.

import { Task } from "./Task";

const task = await Task.find(12);

first

Fetch only the first task. This returns a single task instead of an array.

import { Task } from "./Task";

const task = await Task.query().first();

using the query builder

Sometimes, you need more control over the fetched records. The query builder is here to help you!

import { DateTime } from "luxon";
import { Task } from "./Task";

const task = await Task.query()
  .where("done", false)
  .orWhere("createdAt", "<", DateTime.local())
  .limit(10)
  .skip(20)
  .get();

Update

Updating is as straight forward as creating:

import { Task } from "./Task";

const task = await Task.first();
task.done = true;
await task.save();

Deleting

Deleting your models also isn't a problem.

import { Task } from "./Task";

const task = await Task.first();
await task.delete();

Connecting to a Real Backend

There are two main approaches when connecting to a backend:

  1. Connecting to an existing backend and using the query language this backend offers
    • You will need to use some kind of translator between the opaque query language and your api query language or implement your own query builder
  2. Developing an endpoint specifically for Opaquejs query language.
    • You can pass the Opaque queries directly to the API and translate those queries on the server side with one of the translators

Adapters

An adapter is a collection of functions that fetches and pushes data to and from a data source. This is used to connect a model to a backend.

For further information about how to use an adapter, please read through the adapter documentation. Most of the time, it will look like this on the client side:

import { OpaqueModel } from "@opaquejs/opaque";
import { SomeAdapter } from "somebackend-opaque-adapter";

export class BaseModel extends OpaqueModel {
  static adapter = new SomeAdapter({ someOption: true });
}

Here is a list of first party adapters:

  • GraphQL

Translators

If you want to integrate Opaquejs deeper in your infrastructure, you can also use one of the translators to parse Opaque Queries on the server side. This lets you translate Opaque Queries to other representations like knex.js queries.

The basic functionality of translators is the following: You give them an Opaque Query, and the translator function returns the query in another "language", like an SQL query.

Here is a list of the first-party translators:

  • Knex.js

Relationships

Belongs To

You can define a belongsTo relationship using the belongsTo Method on a model instance:

import { OpaqueModel } from "@opaquejs/opaque";
import { User } from "./User";

export class Task extends OpaqueModel {
  public user() {
    return this.belongsTo(User);
  }
}

You should then use the relationship using the exec method:

import { Task } from "./Task";

const user = await Task.first().user().exec();

Has Many

import { OpaqueModel } from "@opaquejs/opaque";
import { Task } from "./Task";

export class User extends OpaqueModel {
  public tasks() {
    return this.hasMany(Task);
  }
}

You should then use the relationship using the exec method:

import { User } from "./User";

const tasks = await User.first().user().exec();
0.20.1

2 years ago

0.20.0

2 years ago

0.19.2

2 years ago

0.19.3

2 years ago

0.19.4

2 years ago

0.18.0

3 years ago

0.17.3

3 years ago

0.17.5

3 years ago

0.17.6

3 years ago

0.17.2

3 years ago

0.17.1

3 years ago

0.17.0

3 years ago

0.16.0

3 years ago

0.15.1

3 years ago

0.15.0

3 years ago

0.14.5

3 years ago

0.14.4

3 years ago

0.14.2

3 years ago

0.14.3

3 years ago

0.13.0

3 years ago

0.13.1

3 years ago

0.12.0

3 years ago

0.11.4

3 years ago

0.11.1

3 years ago

0.11.3

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.6

3 years ago

0.9.5

3 years ago

0.9.4

3 years ago

0.9.3

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.3

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.5

4 years ago

0.6.4

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.4.11

4 years ago

0.4.9

4 years ago

0.4.10

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.3.19

4 years ago

0.3.18

4 years ago

0.4.0

4 years ago

0.3.17

4 years ago

0.3.16

4 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.9

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

1.0.0

4 years ago