Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
$ npm install ts-node -g{ _id: 3, count: 1 }
{ _id: 4, count: 2 }
{ _id: 5, count: 1 }import Tashmet from '@tashmet/tashmet';
import Nabu from '@tashmet/nabu';
import mingo from '@tashmet/mingo';
// Create the storage engine
const store = Nabu
.configure({})
.use(mingo())
.bootstrap();
Tashmet
.connect(store.proxy())
.then(async tashmet => {
const db = tashmet.db('hello-world');
const posts = db.collection('posts');
// Insert a single document
await posts.insertOne({title: 'Hello World!'});
// Retrieve a cursor and get the first document from it
const doc = await posts.find().next();
console.log(doc);
});$ ts-node index.ts{ title: 'Hello World!', _id: '624de5a6a92e999d3a4b5dbf' }import { LogLevel } from '@tashmet/core';
import mingo from '@tashmet/mingo';
import Nabu from '@tashmet/nabu';
import TashmetServer from '@tashmet/server';
import { terminal } from '@tashmet/terminal';
const store = Nabu
.configure({
logLevel: LogLevel.Debug,
logFormat: terminal(),
persistentState: db => `${db}.yaml`,
})
.use(mingo())
.bootstrap();
new TashmetServer(store).listen(8080);collections:
posts:
storageEngine:
directory:
path: ./content/posts
extension: .md
format:
yaml:
frontMatter: true
contentKey: articleBodyimport Tashmet from '@tashmet/tashmet';
import ServerProxy from '@tashmet/proxy';
Tashmet
.connect(new ServerProxy({ uri: 'http://localhost:8080' }))
.then(async tashmet => {
const db = tashmet.db('content');
const posts = await db.collection('posts');
const cursor = posts.aggregate([
{ $set: { html: { $markdownToHtml: '$articleBody' } } }
]);
for await (const doc of cursor) {
console.log(doc);
}
tashmet.close();
});import Tashmet from '@tashmet/tashmet';
import Nabu from '@tashmet/nabu';
import mingo from '@tashmet/mingo';
const store = Nabu
.configure({})
.use(mingo())
.bootstrap();
Tashmet
.connect(store.proxy())
.then(async tashmet => {
const db = tashmet.db('aggregation');
const coll = await db.createCollection('restaurants');
// Create sample documents
const docs = [
{ stars: 3, categories: ["Bakery", "Sandwiches"], name: "Rising Sun Bakery" },
{ stars: 4, categories: ["Bakery", "Cafe", "Bar"], name: "Cafe au Late" },
{ stars: 5, categories: ["Coffee", "Bakery"], name: "Liz's Coffee Bar" },
{ stars: 3, categories: ["Steak", "Seafood"], name: "Oak Steakhouse" },
{ stars: 4, categories: ["Bakery", "Dessert"], name: "Petit Cookie" },
];
// Insert documents into the restaurants collection
await coll.insertMany(docs);
// Define an aggregation pipeline with a match stage and a group stage
const pipeline = [
{ $match: { categories: "Bakery" } },
{ $group: { _id: "$stars", count: { $sum: 1 } } }
];
// Execute the aggregation
const aggCursor = coll.aggregate(pipeline);
// Print the aggregated results
for await (const doc of aggCursor) {
console.log(doc);
}
});Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection');
});insertOne(
document: OptionalId<TSchema>, options?: InsertOneOptions
): Promise<InsertOneResult>insertMany(
documents: OptionalId<TSchema>[], options?: BulkWriteOptions
): Promise<InsertManyResult>findOne(): Promise<WithId<TSchema> | null>;
findOne(filter: Filter<TSchema>): Promise<WithId<TSchema> | null>;
findOne(filter: Filter<TSchema>, options: FindOptions): Promise<WithId<TSchema> | null>;find(): FindCursor<WithId<TSchema>>;
find(filter: Filter<TSchema>, options?: FindOptions): FindCursor<WithId<TSchema>>;aggregate<T extends Document = Document>(
pipeline: Document[] = [], options: AggregateOptions = {}
): AggregationCursor<T>distinct<Key extends keyof WithId<TSchema>>(
key: Key
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
filter: Filter<TSchema>
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
filter: Filter<TSchema>,
options: DistinctOptions
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
// Embedded documents overload
distinct(key: string): Promise<any[]>;
distinct(key: string, filter: Filter<TSchema>): Promise<any[]>;
distinct(key: string, filter: Filter<TSchema>, options: DistinctOptions): Promise<any[]>;countDocuments(
filter: Filter<TSchema> = {}, options: CountDocumentsOptions = {}
): Promise<number>replaceOne(
filter: Filter<TSchema>, replacement: TSchema, options?: ReplaceOneOptions
): Promise<UpdateResult>deleteOne(filter: Filter<TSchema>, options?: DeleteOptions): Promise<DeleteResult>import Nabu from '@tashmet/nabu';
import mingo from '@tashmet/mingo';
const store = Nabu
.configure({})
.use(mingo())
.bootstrap();const store = Nabu
.configure({
dbFile: db => `${db}.yaml`
})
// ...collections:
posts:
storageEngine:
directory:
path: ./content/posts
extension: .md
format:
yaml:
frontMatter: true
contentKey: articleBodyTashmet
.connect(store.proxy())
.then(async tashmet => {
const db = tashmet.db('mydb');
const posts = await db.collection('posts');const ns = new TashmetNamespace('mydb');
const storageEngine = Memory
.configure({})
.use(mingo())
.bootstrap()
// Create a collection named 'test'
await storageEngine.command(ns, {create: 'test'});
// Insert a number of documents into it.
await storageEngine.command(ns, {insert: 'test', documents: [
{ _id: 1, category: "cake", type: "chocolate", qty: 10 },
{ _id: 2, category: "cake", type: "ice cream", qty: 25 },
{ _id: 3, category: "pie", type: "boston cream", qty: 20 },
{ _id: 4, category: "pie", type: "blueberry", qty: 15 }
]});Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: {
glob: {
pattern: 'content/myCollection/**/*.yaml',
format: 'yaml'
}
}
});
});const store = Nabu
.configure({})
.use(mingo())
.io('yaml', (ns, options) => ({
glob: {
pattern: `${ns.db}/${ns.collection}/**/*.yaml`,
format: 'yaml'
}
}))
.bootstrap();
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: 'yaml'
});
});const store = Nabu
.configure({
defaultIO: 'yaml'
})
// ...
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection');
});// YAML with front matter
{
// ...
format: {
yaml: {
frontMatter: true,
contentKey: 'content'
}
}
}// Include file path
{
// ...
mergeStat: {
path: '$path'
}
}{
pattern: '/content/**/*.md',
format: {
yaml: {
frontMatter: true,
contentKey: 'markdown'
}
},
construct: {
html: {
$markdownToHtml: '$markdown'
}
}
}Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: {
arrayInFile: {
path: 'content/myCollection.yaml',
format: 'yaml'
}
}
});
});const store = Nabu
.configure({})
.use(mingo())
.io('yaml', (ns, options) => ({
arrayInFile: {
path: `${ns.db}/${ns.collection}.yaml`,
format: 'yaml'
}
}))
.bootstrap();
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: 'yaml'
});
});const store = Nabu
.configure({
defaultIO: 'yaml'
})
// ...
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection');
});const store = Nabu
.configure({})
.use(mingo())
.io('yaml', (ns, options) => ({
arrayInFile: {
path: `${ns.db}.yaml`,
format: 'yaml',
field: ns.collection
}
}))
.bootstrap();collection1:
- _id: d1
- _id: d2
collection2:
- _id: d1{
// ...
includeArrayIndex: 'order'
}import Memory from '@tashmet/memory';
import mingo from '@tashmet/mingo';
import json from '@tashmet/json';
const store = Memory
.configure({})
.use(mingo())
.use(json())
.bootstrap();const input = [
{ json: '{ "foo": "bar" }' }
];
const pipeline: Document[] = [
{ $documents: input },
{ $set: { object: { $jsonToObject: '$json' } } }
];
const doc = await tashmet.db('test').aggregate(pipeline).next();const input = [
{ object: { foo: 'bar' } }
];
const pipeline: Document[] = [
{ $documents: input },
{ $set: { json: { $objectToJson: '$object' } } }
];
const doc = await tashmet.db('test').aggregate(pipeline).next();import Memory from '@tashmet/memory';
import mingo from '@tashmet/mingo';
import fs from '@tashmet/fs';
const store = Memory
.configure({})
.use(mingo())
.use(fs())
.bootstrap();{
$writeFile: {
content: 'content to write (expression or actual data)',
path: 'path to file',
overwrite: 'true if file should be overwritten if it exists',
}
}const store = Nabu
.configure({})
.use(mingo())
.bootstrap();
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: {
objectInFile: {
path: 'content/myCollection.yaml',
format: 'yaml'
}
}
});
});await collection.insertMany([
{ _id: 'doc1', title: 'foo' },
{ _id: 'doc2', title: 'bar' },
];doc1:
title: foo
doc2:
title: barconst store = Nabu
.configure({})
.use(mingo())
.io('objectInYaml', (ns, options) => ({
objectInFile: {
path: `${ns.db}/${ns.collection}.yaml`,
format: 'yaml'
}
}))
.bootstrap();
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: 'objectInYaml'
});
});const store = Nabu
.configure({
defaultIO: 'objectInYaml'
})
// ...
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection');
});const store = Nabu
.configure({
defaultIO: 'dbInYaml'
})
.use(mingo())
.io('dbInYaml', (ns, options) => ({
objectInFile: {
path: `${ns.db}.yaml`,
format: 'yaml',
field: ns.collection
}
}))
.bootstrap();
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection');
await collection.insertMany([
{ _id: 'doc1', title: 'foo' },
{ _id: 'doc2', title: 'bar' },
];
});
myCollection:
doc1:
title: foo
doc2:
title: barconst store = Nabu
.configure({
yaml: { indent: 4 }
})
.bootstrap();import Memory from '@tashmet/memory';
import mingo from '@tashmet/mingo';
import yaml from '@tashmet/yaml';
const store = Memory
.configure({})
.use(mingo())
.use(yaml({ /* configuration options */}))
.bootstrap();const data = dedent`
title: foo
list:
- item1
- item2
`;
const pipeline: Document[] = [
{ $documents: [{ data }] },
{ $set: { data: { $yamlToObject: '$data' } } }
];
const doc = await tashmet.db('test').aggregate(pipeline).next();{ data: { title: 'foo', list: ['item1', 'item2'] } }const data = dedent`
---
title: foo
---
Content goes here
`;
const pipeline: Document[] = [
{ $documents: [{ data }] },
{
$set: {
data: {
$yamlToObject: {
data: '$data',
frontMatter: true,
contentKey: 'body'
}
}
}
}
];
const doc = await tashmet.db('test').aggregate(pipeline).next();{ data: { title: 'foo', body: 'Content goes here' } }const input = [
{ data: { foo: 'bar' } }
];
const pipeline: Document[] = [
{ $documents: input },
{ $set: { data: { $objectToYaml: '$data' } } }
];
const doc = await tashmet.db('test').aggregate(pipeline).next();How to setup Tashmet
$ npm install @tashmet/tashmet @tashmet/nabu @tashmet/mingocollections:
myCollection:
storageEngine:
directory:
path: ./content/myCollection
extension: .yaml
format: yamlTashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: {
directory: {
path: 'content/myCollection',
extension: '.yaml',
format: 'yaml'
}
}
});
});const store = Nabu
.configure({})
.use(mingo())
.io('yaml', (ns, options) => ({
directory: {
path: `${ns.db}/${ns.collection}`,
extension: '.yaml',
format: 'yaml'
}
}))
.bootstrap();
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection', {
storageEngine: 'yaml'
});
});const store = Nabu
.configure({
defaultIO: 'yaml'
})
// ...
Tashmet.connect(store.proxy()).then(async tashmet => {
const collection = await tashmet.db('myDb').createCollection('myCollection');
});{
// ...
extension: '.yaml'
}// YAML with front matter
{
// ...
format: {
yaml: {
frontMatter: true,
contentKey: 'content'
}
}
}// Include file path
{
// ...
mergeStat: {
path: '$path'
}
}{
path: '/content',
extension: '.md',
format: {
yaml: {
frontMatter: true,
contentKey: 'markdown'
}
},
construct: {
html: {
$markdownToHtml: '$markdown'
}
}
}{
path: '/content/posts',
extension: '.md',
format: {
yaml: {
frontMatter: true,
contentKey: 'markdown'
}
},
default: {
slug: '$_id'
}
}