Skip to content

Commit

Permalink
Added private schema names
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Feb 5, 2022
1 parent b4a88db commit 8d84ac1
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 17 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- `SchemaName:private` and `CollectionName::private` are two new constructors
that allow defining names without specifying an authority. Developers
creatingreusable collections and/or schemas should not use these methods as
namespacing is meant to help prevent name collisions.

## v0.1.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -24,7 +24,7 @@ struct Shape {
impl Collection for Shape {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "shapes")
CollectionName::private("shapes")
}
fn define_views(schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benches/collections/bonsai.rs
Expand Up @@ -17,7 +17,7 @@ use crate::collections::ResizableDocument;

impl Collection for ResizableDocument {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "resizable-docs")
CollectionName::private("resizable-docs")
}

fn define_views(_schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion book/book-examples/tests/view-example-enum.rs
Expand Up @@ -39,7 +39,7 @@ pub struct BlogPost {

impl Collection for BlogPost {
fn collection_name() -> CollectionName {
CollectionName::new("view-example", "blog-post")
CollectionName::private("blog-post")
}

fn define_views(schema: &mut bonsaidb::core::schema::Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion book/book-examples/tests/view-example-string.rs
Expand Up @@ -27,7 +27,7 @@ pub struct BlogPost {

impl Collection for BlogPost {
fn collection_name() -> CollectionName {
CollectionName::new("view-example", "blog-post")
CollectionName::private("blog-post")
}

fn define_views(schema: &mut bonsaidb::core::schema::Schematic) -> Result<(), Error> {
Expand Down
6 changes: 3 additions & 3 deletions crates/bonsaidb-core/src/connection.rs
Expand Up @@ -55,7 +55,7 @@ use crate::{
///
/// impl Collection for MyCollection {
/// fn collection_name() -> CollectionName {
/// CollectionName::new("MyAuthority", "MyCollection")
/// CollectionName::private("MyCollection")
/// }
///
/// fn define_views(schema: &mut Schematic) -> Result<(), Error> {
Expand Down Expand Up @@ -1585,7 +1585,7 @@ macro_rules! __doctest_prelude {

impl Schema for MySchema {
fn schema_name() -> SchemaName {
SchemaName::new("MyAuthority", "MySchema")
SchemaName::private("MySchema")
}

fn define_collections(schema: &mut Schematic) -> Result<(), Error> {
Expand Down Expand Up @@ -1616,7 +1616,7 @@ macro_rules! __doctest_prelude {

impl Collection for MyCollection {
fn collection_name() -> CollectionName {
CollectionName::new("MyAuthority", "MyCollection")
CollectionName::private("MyCollection")
}

fn define_views(schema: &mut Schematic) -> Result<(), bonsaidb_core::Error> {
Expand Down
24 changes: 21 additions & 3 deletions crates/bonsaidb-core/src/schema/names.rs
Expand Up @@ -195,6 +195,14 @@ pub struct SchemaName {
}

impl SchemaName {
/// Creates a name for a [`Schema`](super::Schema) that is not meant
/// to be shared with other developers.
pub fn private<N: Into<Name>>(name: N) -> Self {
let authority = Authority::from("private");
let name = name.into();
Self { authority, name }
}

/// Creates a new schema name.
pub fn new<A: Into<Authority>, N: Into<Name>>(authority: A, name: N) -> Self {
let authority = authority.into();
Expand Down Expand Up @@ -237,17 +245,27 @@ impl Display for SchemaName {
}
}

/// The name of a [`Collection`](super::Collection).
/// The namespaced name of a [`Collection`](super::Collection).
#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
pub struct CollectionName {
/// The authority of this collection.
/// The authority of this collection. This name is used to ensure
/// collections from multiple authors/authorities can be used in the same
/// schema.
pub authority: Authority,

/// The name of this collection.
/// The name of this collection. Must be unique within the [`Schema`](super::Schema)
pub name: Name,
}

impl CollectionName {
/// Creates a name for a [`Collection`](super::Collection) that is not meant
/// to be shared with other developers.
pub fn private<N: Into<Name>>(name: N) -> Self {
let authority = Authority::from("private");
let name = name.into();
Self { authority, name }
}

/// Creates a new collection name.
pub fn new<A: Into<Authority>, N: Into<Name>>(authority: A, name: N) -> Self {
let authority = authority.into();
Expand Down
2 changes: 1 addition & 1 deletion crates/bonsaidb-core/src/test_util.rs
Expand Up @@ -398,7 +398,7 @@ pub struct BasicSchema;

impl Schema for BasicSchema {
fn schema_name() -> SchemaName {
SchemaName::new("khonsulabs", "basic")
SchemaName::private("basic")
}

fn define_collections(schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/bonsaidb/crate-docs.md
Expand Up @@ -22,7 +22,7 @@ struct Shape {
impl Collection for Shape {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "shapes")
CollectionName::private("shapes")
}
fn define_views(schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-local/examples/basic-local-multidb.rs
Expand Up @@ -23,7 +23,7 @@ struct Message {

impl Collection for Message {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "messages")
CollectionName::private("messages")
}

fn define_views(_schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-local/examples/basic-local.rs
Expand Up @@ -22,7 +22,7 @@ struct Message {

impl Collection for Message {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "messages")
CollectionName::private("messages")
}

fn define_views(_schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-local/examples/view-examples.rs
Expand Up @@ -24,7 +24,7 @@ struct Shape {

impl Collection for Shape {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "shapes")
CollectionName::private("shapes")
}

fn define_views(schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-server/examples/support/schema.rs
Expand Up @@ -22,7 +22,7 @@ impl Shape {

impl Collection for Shape {
fn collection_name() -> CollectionName {
CollectionName::new("khonsulabs", "shapes")
CollectionName::private("shapes")
}

fn define_views(schema: &mut Schematic) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion examples/view-histogram/examples/view-histogram.rs
Expand Up @@ -102,7 +102,7 @@ pub struct Samples {

impl Collection for Samples {
fn collection_name() -> CollectionName {
CollectionName::new("histogram-example", "samples")
CollectionName::private("samples")
}

fn define_views(schema: &mut Schematic) -> Result<(), bonsaidb::core::Error> {
Expand Down

0 comments on commit 8d84ac1

Please sign in to comment.