RavenDB:如何创建一个集合?

4
我在ravendb中设置了一个免费的数据库“TestDB”,当您创建一些文档时,默认会出现空的@empty集合。但是我该如何创建自己的集合?关于这个问题没有文档,并且似乎从他们的npm ravendb包中也没有API可以做到这一点...?
以下是我的代码:
import { DocumentStore } from 'ravendb'
import * as fs from 'fs'

// load certificate and prepare authentication options
const authOptions = {
    certificate: fs.readFileSync(
        'C:/Users/asdasd/Desktop/testravendb/free.asdasdasd.client.certificate.pfx'
    ),
    type: 'pfx', // or "pem"
    password: '',
}

const store = new DocumentStore(
    ['https://a.free.asdasdasd.ravendb.cloud'],
    'TestDB',
    authOptions
)
store.initialize()

console.log(store)
const session = store.openSession()
console.log(session)

let product = {
    title: 'iPhone X',
    price: 999.99,
    currency: 'USD',
    storage: 64,
    manufacturer: 'Apple',
    in_stock: true,
    last_update: new Date('2017-10-01T00:00:00'),
}
async function save() {
    await session.store(product, 'products/')
    console.log(product.id) // Products/1-A
    await session.saveChanges()
}
save()

async function getData() {
    const query = session.query({ collection: '@empty' })
    const results = await query.all()
    //let product2 = await session.load('products')
    console.log(results) // iPhone X
}

getData()

只要我使用@empty集合,它就能工作。

1
可以使用对象字面量表示实体 - 请参考: https://github.com/ravendb/ravendb-nodejs-client#using-object-literals-for-entities 或者 - 使用类表示实体 - 请参考: https://github.com/ravendb/ravendb-nodejs-client#using-classes-for-entities - Danielle
此外,请查看这个 node.js演示教程: https://demo.ravendb.net/demos/nodejs/basics/create-document#step-1 - Danielle
2个回答

2

1
你能提供一个如何使用它的例子吗?我不理解它。 - MMMM

1

要创建一个集合,您需要创建一个带有类实例作为参数的实体。

更重要的是,如果您传递一个类的实例,它将为您生成一个唯一的 slug,而不是长 UUID。

const { User } = require('./user.js')

const store = new DocumentStore(url, db);
store.initialize();

const add = async () => {
    const user = new User({ email@gmail.com, John, 32 })
    await session.store(user);
    await session.saveChanges();
}

执行add()函数,将新文档添加到Users集合中。

它甚至会将集合命名为“Users”,尽管您创建的类名为“User”。

user.js:

class User {
    constructor({ email, name, age}) {
        this.email = email;
        this.name= name;
        this.age= age;
    }
}

module.exports {
    User
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接