Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram committed Jan 13, 2024
1 parent 518aa38 commit a614bc9
Showing 1 changed file with 59 additions and 63 deletions.
122 changes: 59 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,65 @@ same directory with a `.index.json` extension (eg. `data.index.json`).
## Usage

```javascript
const { Database, predicate: p } = require("jify");

async function main() {
const db = new Database("books.json");

// Create
await db.create();

// Insert - Single
await db.insert({
title: "Robinson Crusoe",
year: 1719,
author: { name: "Daniel Defoe" },
});

// Insert - Batch
await db.insert([
{
title: "Great Expectations",
year: 1861,
author: { name: "Charles Dickens" },
},
{
title: "Oliver Twist",
year: 1838,
author: { name: "Charles Dickens" },
},
{
title: "Pride and Prejudice",
year: 1813,
author: { name: "Jane Austen" },
},
{
title: "Nineteen Eighty-Four",
year: 1949,
author: { name: "George Orwell" },
},
]);

// Index - creates books.index.json file
await db.index("title", "year", "author.name");

// Query
console.log("author.name = Charles Dickens, year > 1840");
const query = { "author.name": "Charles Dickens", year: p`> ${1840}` };
for await (const record of db.find(query)) console.log(record);

let records;

// Range query
console.log("1800 <= year < 1900");
records = await db.find({ year: p`>= ${1800} < ${1900}` }).toArray();
console.log(records);

// Multiple queries
console.log("year < 1800 or year > 1900");
records = await db
.find({ year: p`< ${1800}` }, { year: p`> ${1900}` })
.toArray();
console.log(records);
}

main();
import { Database, predicate as p } from "jify";

const db = new Database("books.json");

// Create
await db.create();

// Insert - Single
await db.insert({
title: "Robinson Crusoe",
year: 1719,
author: { name: "Daniel Defoe" },
});

// Insert - Batch
await db.insert([
{
title: "Great Expectations",
year: 1861,
author: { name: "Charles Dickens" },
},
{
title: "Oliver Twist",
year: 1838,
author: { name: "Charles Dickens" },
},
{
title: "Pride and Prejudice",
year: 1813,
author: { name: "Jane Austen" },
},
{
title: "Nineteen Eighty-Four",
year: 1949,
author: { name: "George Orwell" },
},
]);

// Index - creates books.index.json file
await db.index("title", "year", "author.name");

// Query
console.log("author.name = Charles Dickens, year > 1840");
const query = { "author.name": "Charles Dickens", year: p`> ${1840}` };
for await (const record of db.find(query)) console.log(record);

let records;

// Range query
console.log("1800 <= year < 1900");
records = await db.find({ year: p`>= ${1800} < ${1900}` }).toArray();
console.log(records);

// Multiple queries
console.log("year < 1800 or year > 1900");
records = await db
.find({ year: p`< ${1800}` }, { year: p`> ${1900}` })
.toArray();
console.log(records);
```

### CLI
Expand Down

0 comments on commit a614bc9

Please sign in to comment.