Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/liammartens/fqlx-micro-orm
https://github.com/liammartens/fqlx-micro-orm
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/liammartens/fqlx-micro-orm
- Owner: LiamMartens
- Created: 2023-09-12T13:54:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-18T02:30:59.000Z (6 months ago)
- Last Synced: 2024-10-18T16:13:05.900Z (2 months ago)
- Language: TypeScript
- Size: 67.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://www.npmjs.com/package/fauna)## 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](npmjs.com/package/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*```js
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 {
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.```js
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);
```