topaz-crystal / topaz Goto Github PK
View Code? Open in Web Editor NEWA simple and useful db wrapper for Crystal-lang
License: MIT License
A simple and useful db wrapper for Crystal-lang
License: MIT License
I'd like to check if a model is persisted. It will help with validations later too.
class Thing < Topaz::Model
columns(
description: String
)
end
x = Thing.new("a")
x.persisted?
# => false
x.save
x.persisted?
# => true
y = Thing.create("b")
y.persisted?
# => true
has_many(childlen: {model: SampleChild, key: parent_id})
The Crystal community has somewhat decided to use crystal
as the language's topic.
To avoid fragmentation in the community, please drop the crystal-language
topic, that only has ~28 repositories at the moment. We're sticking to crystal
, instead.
Any contributor can change it from the repo's homepage.
See crystal-lang/crystal#4241 for more context.
Thank you very much :)
Is a DB connection opened on each save? Opening a connection is really, really slow. It should be done once in your application. I'm talking about these lines.
I've made a benchmark:
require "benchmark"
require "pg"
DB.open "postgres:///" do |db|
db.exec "drop table if exists contacts"
db.exec "create table contacts (num integer)"
end
shared_db = DB.open "postgres:///"
Benchmark.ips do |x|
x.report("open each time") do
DB.open "postgres:///" do |db|
db.exec "insert into contacts (num) values (1)"
end
end
x.report("open once") do
shared_db.exec "insert into contacts (num) values (1)"
end
end
Results:
open each time 73.53 ( 13.6ms) (± 4.26%) 58.82× slower
open once 4.32k (231.23µs) (± 1.68%) fastest
Almost 60 times slower to open and close a connection each time.
I'm not sure, but it could be the reason why you don't get very good performance here
This is more a suggestion than an issue itself, I was just wondering if you guys are considering integrate Topaz with micrate for handling database migrations.
From all the ORM that I have been evaluating, I really like Topaz because for its simplicity but I feel that the way Topaz handles migrations will be not enough for large-scale projects.
Thanks
We may need to update the dependency builds for TravisCI
Get:1 http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu precise/main amd64 libsqlite3-dev amd64 3.7.15.1-1~travis1 [582 kB]
Err:1 http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu precise/main amd64 libsqlite3-dev amd64 3.7.15.1-1~travis1
Hash Sum mismatch
Get:2 http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu precise/main amd64 libsqlite3-0 amd64 3.7.15.1-1~travis1 [459 kB]
Err:2 http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu precise/main amd64 libsqlite3-0 amd64 3.7.15.1-1~travis1
Hash Sum mismatch
Get:3 http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu precise/main amd64 sqlite3 amd64 3.7.15.1-1~travis1 [122 kB]
Err:3 http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu precise/main amd64 sqlite3 amd64 3.7.15.1-1~travis1
Hash Sum mismatch
Fetched 1,163 kB in 2s (572 kB/s)
E: Failed to fetch http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu/pool/main/s/sqlite3/libsqlite3-dev_3.7.15.1-1~travis1_amd64.deb Hash Sum mismatch
E: Failed to fetch http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu/pool/main/s/sqlite3/libsqlite3-0_3.7.15.1-1~travis1_amd64.deb Hash Sum mismatch
E: Failed to fetch http://ppa.launchpad.net/travis-ci/sqlite3/ubuntu/pool/main/s/sqlite3/sqlite3_3.7.15.1-1~travis1_amd64.deb Hash Sum mismatch
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
The command "sudo apt-get install sqlite3=3.7.15.1-1~travis1" failed and exited with 100 during .
Your build has been stopped.
This is causing all tests to fail (and not even run).
Just wanted to know if its possible to support existing db which has column names with spaces. Such as
"full name", "pin code", etc
After some testing and experimenting it looks like the default time format in the base Model class doesn't play nice with PG.
https://github.com/topaz-crystal/topaz/blob/master/src/topaz/model.cr#L9
Example using a sample table:
sampledb> insert into sample_table (name, created_at, updated_at)
values ('sdf', '2017-03-05 08:43:37.681', '2017-03-05 08:43:37.681');
INSERT 0 1
Time: 0.022s
sampledb> insert into sample_table (name, created_at, updated_at)
values ('sdf', '2017-03-05 08:43:37:681', '2017-03-05 08:43:37:681');
invalid input syntax for type timestamp: "2017-03-05 08:43:37:681"
LINE 1: ...me, created_at, updated_at) values ('sdf', '2017-03-0...
First example using the period. Second example using the colon.
I overridded it in my own project's base model class and that looks like it works fine
require "topaz"
class SampleTable < Topaz::Model
TIME_FORMAT = "%F %T.%L %z"
How do you update an existing model from JSON? I can't seem to figure out the use of from_json
and update
with this.
Say I want to have a default value for a model schema. Let's use a Todo app as an example:
class Todo < Topaz::Model
columns(
description: String,
completion: Time?
)
end
The completion parameter should be assumed to be nil
if no value is given unless nullable: false
has be set as in https://github.com/topaz-crystal/topaz/blob/master/spec/model/models.cr#L48.
So if the union has a type of Nil
in it I'd like the parameters in the create
method to have = nil
in it by default. Again, to avoid that I'd use nullable: false
.
As far as other default values I'm not sure how you'd want to do it. Maybe something like?
class Todo < Topaz::Model
columns(
description: String, {default: ""},
attempts: Int32, {default: 0}
)
end
Currently there is a way to create a many to many relationship with 2 models ?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.