Skip to content

Commit

Permalink
Rename some schema attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
photino committed Nov 18, 2023
1 parent c6d0cfd commit 92e298e
Show file tree
Hide file tree
Showing 31 changed files with 204 additions and 163 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Logs
**/logs/*

Expand Down
2 changes: 1 addition & 1 deletion examples/actix-app/src/controller/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn login(mut req: Request) -> Result {
"last_login_ip": data.remove("current_login_ip"),
"current_login_at": current_time,
"current_login_ip": req.client_ip(),
"login_count": { "$inc": 1 },
"$inc": { "login_count": 1 },
});

let mut user_mutations = user_updates.into_map_opt().unwrap_or_default();
Expand Down
4 changes: 2 additions & 2 deletions examples/actix-app/src/model/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zino_derive::{DecodeRow, Model, ModelAccessor, ModelHooks, Schema};
#[serde(default)]
pub struct Tag {
// Basic fields.
#[schema(primary_key, readonly, constructor = "Uuid::new_v4")]
#[schema(primary_key, read_only, constructor = "Uuid::new_v4")]
id: Uuid,
#[schema(not_null, index_type = "text", comment = "Tag name")]
name: String,
Expand All @@ -40,7 +40,7 @@ pub struct Tag {
extra: Map,

// Revisions.
#[schema(readonly, default_value = "now", index_type = "btree")]
#[schema(read_only, default_value = "now", index_type = "btree")]
created_at: DateTime,
#[schema(default_value = "now", index_type = "btree")]
updated_at: DateTime,
Expand Down
2 changes: 1 addition & 1 deletion examples/axum-app/src/controller/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn login(mut req: Request) -> Result {
"last_login_ip": data.remove("current_login_ip"),
"current_login_at": current_time,
"current_login_ip": req.client_ip(),
"login_count": { "$inc": 1 },
"$inc": { "login_count": 1 },
});

let mut user_mutations = user_updates.into_map_opt().unwrap_or_default();
Expand Down
4 changes: 2 additions & 2 deletions examples/axum-app/src/model/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zino_derive::{DecodeRow, Model, ModelAccessor, ModelHooks, Schema};
#[serde(default)]
pub struct Tag {
// Basic fields.
#[schema(primary_key, auto_increment, readonly)]
#[schema(primary_key, auto_increment, read_only)]
id: i64,
#[schema(not_null, index_type = "text", comment = "Tag name")]
name: String,
Expand All @@ -40,7 +40,7 @@ pub struct Tag {
extra: Map,

// Revisions.
#[schema(readonly, default_value = "now", index_type = "btree")]
#[schema(read_only, default_value = "now", index_type = "btree")]
created_at: DateTime,
#[schema(default_value = "now", index_type = "btree")]
updated_at: DateTime,
Expand Down
10 changes: 5 additions & 5 deletions examples/axum-app/src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zino_model::user::JwtAuthService;
#[serde(default)]
pub struct User {
// Basic fields.
#[schema(primary_key, auto_increment, readonly)]
#[schema(primary_key, auto_increment, read_only)]
id: i64,
#[schema(not_null, index_type = "text", comment = "User name")]
name: String,
Expand All @@ -36,11 +36,11 @@ pub struct User {
description: String,

// Info fields.
#[schema(not_null, unique, writeonly, constructor = "AccessKeyId::new")]
#[schema(not_null, unique, write_only, constructor = "AccessKeyId::new")]
access_key_id: String,
#[schema(not_null, unique, writeonly, comment = "User account")]
#[schema(not_null, unique, write_only, comment = "User account")]
account: String,
#[schema(not_null, writeonly, comment = "User password")]
#[schema(not_null, write_only, comment = "User password")]
password: String,
mobile: String,
email: String,
Expand Down Expand Up @@ -69,7 +69,7 @@ pub struct User {
extra: Map,

// Revisions.
#[schema(readonly, default_value = "now", index_type = "btree")]
#[schema(read_only, default_value = "now", index_type = "btree")]
created_at: DateTime,
#[schema(default_value = "now", index_type = "btree")]
updated_at: DateTime,
Expand Down
2 changes: 1 addition & 1 deletion zino-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ version = "0.9.2"
optional = true

[dependencies.datafusion]
version = "32.0.0"
version = "33.0.0"
optional = true

[dependencies.minijinja]
Expand Down
2 changes: 1 addition & 1 deletion zino-core/src/application/metrics_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(super) fn init<APP: Application + ?Sized>() {
let host_addr = host
.parse::<IpAddr>()
.unwrap_or_else(|err| panic!("invalid host address `{host}`: {err}"));
tracing::warn!(exporter, "listen on {host_addr}:{port}");
tracing::warn!(exporter, "listen on `{host_addr}:{port}`");
PrometheusBuilder::new().with_http_listener((host_addr, port))
};
if let Some(quantiles) = metrics.get_array("quantiles") {
Expand Down
4 changes: 2 additions & 2 deletions zino-core/src/connector/connector_arrow/scalar_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ScalarValueExt for ScalarValue {
scalar
})
.collect::<Vec<_>>();
Self::new_list(Some(scalars), data_type)
Self::List(Self::new_list(&scalars, &data_type))
}
TomlValue::Table(table) => {
let mut fields = Vec::with_capacity(table.len());
Expand Down Expand Up @@ -80,7 +80,7 @@ impl ScalarValueExt for ScalarValue {
scalar
})
.collect::<Vec<_>>();
Self::new_list(Some(scalars), data_type)
Self::List(Self::new_list(&scalars, &data_type))
}
JsonValue::Object(map) => {
let mut fields = Vec::with_capacity(map.len());
Expand Down
28 changes: 20 additions & 8 deletions zino-core/src/model/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ impl<'a> Column<'a> {
self.type_name
}

/// Returns `true` if the column is the primary key.
#[inline]
pub fn is_primary_key(&self) -> bool {
self.has_attribute("primary_key")
}

/// Returns `true` if the column can not be null.
#[inline]
pub fn is_not_null(&self) -> bool {
Expand Down Expand Up @@ -161,6 +155,24 @@ impl<'a> Column<'a> {
true
}

/// Returns `true` if the column is the primary key.
#[inline]
pub fn is_primary_key(&self) -> bool {
self.has_attribute("primary_key")
}

/// Returns `true` if the column is read-only.
#[inline]
pub fn is_read_only(&self) -> bool {
self.has_attribute("read_only")
}

/// Returns `true` if the column is write-only.
#[inline]
pub fn is_write_only(&self) -> bool {
self.has_attribute("write_only")
}

/// Returns the Avro schema.
pub fn schema(&self) -> Schema {
let type_name = self.type_name();
Expand Down Expand Up @@ -367,10 +379,10 @@ impl<'a> Column<'a> {
if let Some(comment) = self.comment() {
definition.upsert("description", comment);
}
if self.has_attribute("readonly") {
if self.is_read_only() {
definition.upsert("readOnly", true);
}
if self.has_attribute("writeonly") {
if self.is_write_only() {
definition.upsert("writeOnly", true);
}
if self.has_attribute("deprecated") {
Expand Down
101 changes: 60 additions & 41 deletions zino-core/src/orm/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,74 @@ impl MutationExt<DatabaseDriver> for Mutation {

let fields = self.fields();
let permissive = fields.is_empty();
let readonly_fields = M::readonly_fields();
let mut mutations = Vec::new();
for (key, value) in updates.iter() {
if (permissive || fields.contains(key))
&& !readonly_fields.contains(&key.as_str())
&& let Some(col) = M::get_column(key)
{
let key = Query::format_field(key);
let Some(map) = value.as_object() else {
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
continue;
};

let mut set_json_object = true;
for (operator, value) in map {
match operator.as_str() {
"$inc" => {
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {key} + {value}"#);
mutations.push(mutation);
}
"$mul" => {
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {key} * {value}"#);
mutations.push(mutation);
match key.as_str() {
"$inc" => {
if let Some(update) = value.as_object() {
for (key, value) in update.iter() {
if fields.contains(key)
&& let Some(col) = M::get_column(key).filter(|c| !c.is_read_only())
{
let key = Query::format_field(key);
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {value} + {key}"#);
mutations.push(mutation);
}
}
"$min" => {
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = LEAST({key}, {value})"#);
mutations.push(mutation);
}
}
"$mul" => {
if let Some(update) = value.as_object() {
for (key, value) in update.iter() {
if fields.contains(key)
&& let Some(col) = M::get_column(key).filter(|c| !c.is_read_only())
{
let key = Query::format_field(key);
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {value} * {key}"#);
mutations.push(mutation);
}
}
"$max" => {
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = GREATEST({key}, {value})"#);
mutations.push(mutation);
}
}
"$min" => {
if let Some(update) = value.as_object() {
for (key, value) in update.iter() {
if fields.contains(key)
&& let Some(col) = M::get_column(key).filter(|c| !c.is_read_only())
{
let key = Query::format_field(key);
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = LEAST({value}, {key})"#);
mutations.push(mutation);
}
}
_ => (),
}
if operator.starts_with('$') {
set_json_object = false;
break;
}
"$max" => {
if let Some(update) = value.as_object() {
for (key, value) in update.iter() {
if fields.contains(key)
&& let Some(col) = M::get_column(key).filter(|c| !c.is_read_only())
{
let key = Query::format_field(key);
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = GREATEST({value}, {key})"#);
mutations.push(mutation);
}
}
}
}
if set_json_object {
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
_ => {
if (permissive || fields.contains(key))
&& let Some(col) = M::get_column(key).filter(|c| !c.is_read_only())
{
let key = Query::format_field(key);
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
}
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions zino-core/src/orm/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
/// Returns a reference to the column fields.
fn fields() -> &'static [&'static str];

/// Returns a reference to the readonly column fields.
fn readonly_fields() -> &'static [&'static str];
/// Returns a reference to the read-only column fields.
fn read_only_fields() -> &'static [&'static str];

/// Returns a reference to the writeonly column fields.
fn writeonly_fields() -> &'static [&'static str];
/// Returns a reference to the write-only column fields.
fn write_only_fields() -> &'static [&'static str];

/// Retrieves a connection pool for the model reader.
async fn acquire_reader() -> Result<&'static ConnectionPool, Error>;
Expand Down Expand Up @@ -131,7 +131,7 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
fn default_query() -> Query {
let mut query = Query::default();
query.allow_fields(Self::fields());
query.deny_fields(Self::writeonly_fields());
query.deny_fields(Self::write_only_fields());
query
}

Expand All @@ -140,7 +140,7 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
fn default_mutation() -> Mutation {
let mut mutation = Mutation::default();
mutation.allow_fields(Self::fields());
mutation.deny_fields(Self::readonly_fields());
mutation.deny_fields(Self::read_only_fields());
mutation
}

Expand Down Expand Up @@ -474,12 +474,12 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
let table_name = Self::table_name();
let primary_key = Query::escape_string(self.primary_key());
let map = self.into_map();
let readonly_fields = Self::readonly_fields();
let num_writable_fields = Self::fields().len() - readonly_fields.len();
let read_only_fields = Self::read_only_fields();
let num_writable_fields = Self::fields().len() - read_only_fields.len();
let mut mutations = Vec::with_capacity(num_writable_fields);
for col in Self::columns() {
let field = col.name();
if !readonly_fields.contains(&field) {
if !read_only_fields.contains(&field) {
let value = col.encode_value(map.get(field));
let field = Query::format_field(field);
mutations.push(format!("{field} = {value}"));
Expand Down Expand Up @@ -581,14 +581,14 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
let table_name = Self::table_name();
let fields = Self::fields();
let num_fields = fields.len();
let readonly_fields = Self::readonly_fields();
let num_writable_fields = num_fields - readonly_fields.len();
let read_only_fields = Self::read_only_fields();
let num_writable_fields = num_fields - read_only_fields.len();
let mut values = Vec::with_capacity(num_fields);
let mut mutations = Vec::with_capacity(num_writable_fields);
for col in Self::columns() {
let field = col.name();
let value = col.encode_value(map.get(field));
if !readonly_fields.contains(&field) {
if !read_only_fields.contains(&field) {
let field = Query::format_field(field);
mutations.push(format!("{field} = {value}"));
}
Expand Down
Loading

0 comments on commit 92e298e

Please sign in to comment.