0.2.36 • Published 3 months ago

animal-orm v0.2.36

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Animal ORM

A simple Object-Relational Mapping (ORM) system for FaunaDB.

Installation

npm install animal-orm

Documentation

Classes

  • Model: The base class for all models.

  • Field: The base class for all fields.

  • RefField: Rerpresents a reference to another document

  • ManyToManyField: Represents a relationship with another document that has a list cardinality in both directions. This requires a tertiary table.

Functions

  • capitalize: Capitalizes the first letter of a string.

  • depluralize: Turns a plural English word into singular.

Example

Here's a simple example of how to use the animal-orm module:

> import { Model, Field, ForeignKeyField, ManyToManyField } from 'animal-orm';
> import * as z from 'zod';
> const User = new Model('User', {
  name: new Field(z.string()),
  email: new Field(z.string().email()),
  password: new Field(z.string(), {hidden: true})
})
> User.construct()
Do(
  CreateCollection({ name: 'User' })
);

> const Pet = new Model('Pet',
  name: new Field(z.string()),
  owner: new RefField(User, {reverse:"pets"}) //Sets up the reverse index
});
> Pet.construct()
Do(
  CreateCollection({ name: 'Pet' }),
  CreateIndex({
    name: 'Pets_by_Owner',
    source: Collection('Pet'),
    terms: [{ field: ['data', 'owner'] }],
    values: [{ field: ['ref'] }]
  }),
);

> const Trick = new Model('Trick',
  name: new Field(z.string()),
  trainedPets: new ManyToManyRelationship(Pet, {reverse:"tricks"}})
)
> Trick.construct()
Do(
  CreateCollection({ name: 'Trick' }),
  CreateIndex({
    name: 'TrainedPets_by_Trick',
    source: Collection('TrickTrainedPet'),
    terms: [{ field: ['data', 'trick'] }],
    values: [
      { field: ['ts'], reverse: true },
      { field: ['data', 'pet'] }
    ]
  }),
  CreateIndex({
    name: 'Tricks_by_TrainedPet',
    source: Collection('TrickTrainedPet'),
    terms: [{ field: ['data', 'pet'] }],
    values: [
      { field: ['ts'], reverse: true },
      { field: ['data', 'trick'] }
    ]
  })
);
> Trick.query('1')
Let({
  ref: Ref(Collection('Trick'), '1'),
  document: Get(Var('ref'))
}, {
  name: Select(['data', 'name'], Var('document')),
  trainedPets: Select(
    "data",
    Map(
      Paginate(Match(Index('TrainedPets_by_Trick'), Var('ref'))),
      Lambda(
        'values', 
        Let({
          ref: Select(Subtract(Count(Var('values')), 1), Var('values')),
          document: Get(Var('ref'))
        }, {
          name: Select(['data', 'name'], Var('document')),
          owner: Let({
            ref: Select(['data', 'owner'], Var('document')),
            document: Get(Var('ref'))
          }, {
            name: Select(['data', 'name'], Var('document')),
            email: Select(['data', 'email'], Var('document'))
          })
        })
      )
    )
  )
})
0.2.36

3 months ago

0.2.35

3 months ago

0.2.34

5 months ago

0.2.33

5 months ago

0.2.32

5 months ago

0.2.31

5 months ago

0.2.30

5 months ago

0.2.27

7 months ago

0.2.26

8 months ago

0.2.25

8 months ago

0.2.24

8 months ago

0.2.23

8 months ago

0.2.22

8 months ago

0.2.21

10 months ago

0.2.20

10 months ago

0.2.19

10 months ago

0.2.18

10 months ago

0.2.17

10 months ago

0.2.16

10 months ago

0.2.15

10 months ago

0.2.14

10 months ago

0.2.13

10 months ago

0.2.12

10 months ago

0.2.11

10 months ago

0.2.29

6 months ago

0.2.28

6 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.3

10 months ago

0.2.2

11 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.0

11 months ago