2.4.0 • Published 8 months ago

fqlx-micro-orm v2.4.0

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

FQLx Micro ORM

This library was created to interact with the FQLx API in a more type-safe way. This library will not execute the actual queries but provides a way of interacting with the Fauna Driver

Usage

Define Collections

You will start by creating a Collection class, which will mirror the collection in your database. For example, the code below defines a collection class for Address and one for Person. Both of which are tied to a zod schema. Note: the schemas aren't used for validation, but rather for type inference. However they will be helpful as schema definitions in your codebase

import z from 'zod';
import { Collection } from 'fqlx-micro-orm';
import { documentReferenceSchemaFactory } from 'fauna-x-schemas';

const addressSchema = z.object({
  street: z.string(),
  zip: z.string(),
});

const personSchema = z.object({
  firstName: z.string(),
  lastName: z.string(),
  address: documentReferenceSchemaFactory('Address').optional(),
});

class AddressCollection extends Collection<typeof addressSchema, 'Address'> {
  public name = 'Address' as const;
  public schema = addressSchema;
}

class PersonCollection extends Collection<
  typeof personSchema,
  'Person',
  { byFirstName: [string] } // index defintions
> {
  public name = 'Person' as const;
  public schema = personSchema;
}

Build query

Once you have the collections set-up you can use them to build a typed query. For the best experience you should use the built-in query method. This is because Fauna implicitly auto-paginates sets. This behavior is handled by the query method.

import { Client } from 'fauna';
import { query } from 'fqlx-micro-orm';

const client = new Client();
const user = new PersonCollection()
  .index('byFirstName', 'John')
  .project(['firstName', 'lastName'])
  .resolve('address', 'address', ['street', 'zip'], new AddressCollection());

// type-safe!
const data = await query(client, user);
2.4.0

8 months ago

2.3.2

8 months ago

2.3.2-beta.0

8 months ago

2.3.1

8 months ago

2.3.0

8 months ago

2.2.0

8 months ago

2.1.0

8 months ago

2.0.0

8 months ago

1.6.4

8 months ago

1.6.3

8 months ago

1.6.2

8 months ago

1.6.1

8 months ago

1.6.0

8 months ago

1.5.1

8 months ago

1.5.0

8 months ago

1.4.0-beta.2

8 months ago

1.4.0-beta.1

8 months ago

1.4.0-beta.0

8 months ago

1.4.0

8 months ago

1.3.0

8 months ago

1.2.0

8 months ago

1.1.4

8 months ago

1.1.3

8 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.0-beta.1

8 months ago

1.0.0

8 months ago