0.2.36 • Published 1 year ago

animal-orm v0.2.36

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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

1 year ago

0.2.35

1 year ago

0.2.34

2 years ago

0.2.33

2 years ago

0.2.32

2 years ago

0.2.31

2 years ago

0.2.30

2 years ago

0.2.27

2 years ago

0.2.26

2 years ago

0.2.25

2 years ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.29

2 years ago

0.2.28

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago