Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
.DS_Store
coverage
docs/.vitepress
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ README.md
CONTRIBUTING.md
api.md
todo.md
docs
81 changes: 48 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,34 @@

## Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Quick Start](#quick-start)
- [Defining Schemas](#defining-schemas)
- [Connecting to the Database](#connecting-to-the-database)
- [Inserting Documents](#inserting-documents)
- [Querying Documents](#querying-documents)
- [Updating Documents](#updating-documents)
- [Deleting Documents](#deleting-documents)
- [Types](#types)
- [Primitives](#primitives)
- [Literals](#literals)
- [Objects](#objects)
- [Records](#records)
- [Arrays](#arrays)
- [Tuples](#tuples)
- [Tagged Union](#tagged-union)
- [Monarch ORM](#monarch-orm)
- [Table of Contents](#table-of-contents)
- [Features](#features)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Quick Start](#quick-start)
- [Defining Schemas and connecting to the database](#defining-schemas-and-connecting-to-the-database)
- [Inserting Documents](#inserting-documents)
- [Querying Documents](#querying-documents)
- [Updating Documents](#updating-documents)
- [Alternative setup](#alternative-setup)
- [Types](#types)
- [Primitives](#primitives)
- [String - string()](#string---string)
- [Number - number()](#number---number)
- [Boolean - boolean()](#boolean---boolean)
- [Date - date()](#date---date)
- [Date String - dateString()](#date-string---datestring)
- [General Modifiers](#general-modifiers)
- [Literals](#literals)
- [Objects](#objects)
- [Records](#records)
- [Arrays](#arrays)
- [Tuples](#tuples)
- [Tagged Union](#tagged-union)
- [Union](#union)
- [Mixed](#mixed)
- [Mixed](#mixed-1)
<!-- - [Type Safety](#type-safety) -->
<!-- - [Configuration](#configuration) -->
- [Roadmap](#roadmap)
Expand Down Expand Up @@ -60,7 +70,7 @@ Or Yarn:
## Basic Usage

```typescript
import { boolean, createClient, createDatabase, createSchema, number, string } from "monarch-orm";
import { boolean, createClient, createDatabase, defineSchemas, createSchema, number, string } from "monarch-orm";

const UserSchema = createSchema("users", {
name: string().nullable(),
Expand All @@ -70,21 +80,22 @@ import { boolean, createClient, createDatabase, createSchema, number, string } f
});

const client = createClient(/** db uri **//)
const { collections } = createDatabase(client.db(), {
const schemas = defineSchemas({
users: UserSchema,
});

const { collections } = createDatabase(client.db(), schemas);

const newUser = await collections.users
.insert()
.values({
.insertOne({
name: "anon",
email: "anon@gmail.com",
age: 0,
isVerified: true,
})
;

const users = await collections.users.find().where({});
const users = await collections.users.find({});
```

## Quick Start
Expand All @@ -104,12 +115,16 @@ Create a database instance using any client you deem fit and drop it into the cr

Or you can use the built-in createClient function.

It is good practice (though not compulsory) to assign the result of `defineSchemas` to a variable before passing it to `createDatabase`. This keeps your database initialization clean.

Then you pass your schemas to the second arguement

```typescript
const { collections } = createDatabase(client.db(), {
const schemas = defineSchemas({
users: UserSchema,
});

const { collections } = createDatabase(client.db(), schemas);
```

### Inserting Documents
Expand All @@ -119,8 +134,7 @@ Example: Inserting a new user

```typescript
const newUser = await collections.users
.insert()
.values({
.insertOne({
name: "Alice",
email: "alice@example.com",
age: 25,
Expand All @@ -135,7 +149,7 @@ Retrieve documents from your collection using the find or findOne methods.
Example: Querying all users

```typescript
const users = await collections.users.find().where({});
const users = await collections.users.find({});
console.log(users);

// Or just...
Expand All @@ -145,7 +159,7 @@ console.log(users);

// For finding one

const user = await collections.users.find().where({
const user = await collections.users.find({
name: "Alice"
});
console.log(users);
Expand Down Expand Up @@ -343,14 +357,15 @@ const UserSchema = createSchema("users", {


// Example of inserting a user with grades
const { collections } = createDatabase(client.db(), {
const schemas = defineSchemas({
users: UserSchema,
});

const { collections } = createDatabase(client.db(), schemas);

// Inserting a new user with grades for different subjects
const newUser = await collections.users
.insert()
.values({
.insertOne({
name: "Alice",
email: "alice@example.com",
grades: {
Expand All @@ -362,7 +377,7 @@ const newUser = await collections.users
;

// Querying the user to retrieve grades
const user = await collections.users.findOne().where({ email: "alice@example.com" });
const user = await collections.users.findOne({ email: "alice@example.com" });
console.log(user.grades);
// Output: { math: 90, science: 85, history: 88 }
```
Expand Down Expand Up @@ -435,7 +450,7 @@ const NotificationSchema = createSchema("notifications", {
});

const notification = ;
await collections.notifications.insert().values({ notification: {
await collections.notifications.insertOne({ notification: {
tag: "email",
value: {
subject: "Welcome!",
Expand Down
Loading
Loading