jekyll-sqlite 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4537bdf95266e26c50110d5ca259f83726b540fe3bf3a6714821d2aa64e83a4
4
- data.tar.gz: f3aa0e1a40abd05e112a44b36aea75e39fc9bc03c49cf1f10710e0bf1fca1aee
3
+ metadata.gz: 84fe35922543ed1ca397dedc961e89c6529ee97bc2ef414e7278defa90a73de5
4
+ data.tar.gz: c001a8544fea6b316d9e1a4f71fc3897508e408c8af141073e5234b34744b173
5
5
  SHA512:
6
- metadata.gz: 97d7e1abe1fdcf60b04336443201eb2063e6f4c7c226d4c9b1052efc01b770d080cc967a3d7f9cebe479fa2b2abce800ddb5e8655a0023c682e9d845d0c5c2c7
7
- data.tar.gz: 97508c414be1884ba85f465316eb33c0494fc49190e4f3dec2a6a05739cca7e72b3fd745b45a2bb30d263660fea942fb92c8c210cf38d3f737a739ca1a3c8147
6
+ metadata.gz: fb3009dfc71784e54b032331b0530c1bbc130929152758497aaa4496e142f3af5d9d0f4187eec1ca9a6342cdc93025a5d18e6a08b912592014e010ea6372880f
7
+ data.tar.gz: daefd3282ecb6586d4c1a75559d7663b9b8063d74e044e57ccb034c0f1b14bd7c7494e67c420154bf445c328675ca21e03942252843209c8497f8b0a855dbf6e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jekyll-sqlite (0.1.0)
4
+ jekyll-sqlite (0.1.1)
5
5
  sqlite3 (~> 1.6)
6
6
 
7
7
  GEM
@@ -32,10 +32,12 @@ GEM
32
32
  rubocop (~> 1.0)
33
33
  ruby-progressbar (1.13.0)
34
34
  sqlite3 (1.6.2-aarch64-linux)
35
+ sqlite3 (1.6.2-x86_64-linux)
35
36
  unicode-display_width (2.4.2)
36
37
 
37
38
  PLATFORMS
38
39
  aarch64-linux
40
+ x86_64-linux
39
41
 
40
42
  DEPENDENCIES
41
43
  jekyll-sqlite!
data/README.md CHANGED
@@ -1,12 +1,14 @@
1
- # Jekyll SQlite plugin
1
+ # Jekyll SQLite plugin
2
2
 
3
- A Jekyll generator plugin to lets you use SQLite database instead of data files as a data source.
3
+ A Jekyll generator plugin to lets you use SQLite database instead of data files as a data source. It lets you easily create APIs and websites from a SQLite database, by linking together a database file, your template, and the relevant queries.
4
+
5
+ It additionally supports nested queries, so that you can use the rows of `site.data.items` as bind_params for your nested query.
4
6
 
5
7
  [![Continuous Integration](https://github.com/captn3m0/jekyll-sqlite/actions/workflows/main.yml/badge.svg)](https://github.com/captn3m0/jekyll-sqlite/actions/workflows/main.yml) [![Gem Version](https://badge.fury.io/rb/jekyll-sqlite.svg)](https://badge.fury.io/rb/jekyll-sqlite)
6
8
 
7
9
  ## Installation
8
10
 
9
- Add this line to your site's Gemfile:
11
+ Add this line to your site's `Gemfile`:
10
12
 
11
13
  ```ruby
12
14
  gem 'jekyll-sqlite'
@@ -16,46 +18,42 @@ And then add this line to your site's `_config.yml`:
16
18
 
17
19
  ```yml
18
20
  plugins:
19
- - jekyll-sqlite
21
+ - jekyll_sqlite
20
22
  ```
21
23
 
22
24
  :warning: If you are using Jekyll < 3.5.0 use the `gems` key instead of `plugins`.
23
25
 
24
-
25
- ## Installation
26
-
27
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
28
-
29
- Install the gem and add to the application's Gemfile by executing:
30
-
31
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
32
-
33
- If bundler is not being used to manage dependencies, install the gem by executing:
34
-
35
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
36
-
37
26
  ## Usage
38
27
 
39
28
  Update your `_config.yml` to define your data sources with your SQLite database.
40
29
 
41
30
  ```yml
42
31
  ...
32
+ # These are run in sequence, so any nested data can work well.
43
33
  sqlite:
44
- members:
34
+ - data: members
45
35
  file: _db/users.db
46
- sql: SELECT * FROM members ORDER by created_at DESC
36
+ query: SELECT * FROM members ORDER by created_at DESC
47
37
  # You can use `results_as_hash` to switch between array or hash results (default).
48
- verified:
38
+ - data: verified
49
39
  results_as_hash: false # default true
50
40
  file: _db/users.db
51
- sql: SELECT username, email FROM members WHERE verified=1
41
+ query: SELECT username, email FROM members WHERE verified=1
42
+ - data: members.posts
43
+ file: _db/posts.db
44
+ query: SELECT * FROM posts WHERE user_id = :id
52
45
  ```
53
46
 
54
47
  Then, you can use the `site.data` attributes accordingly:
55
48
 
56
49
  ```liquid
57
- {% for user in site.data.members %}
58
- - {{user.username}}
50
+ {% for member in site.data.members %}
51
+ - {{member.username}}
52
+
53
+ # Your Posts
54
+ {% for post in member.posts %}
55
+ {{post}}
56
+ {% endfor %}
59
57
  {% endfor %}
60
58
 
61
59
  # Result here is an array instead of a hash.
@@ -64,6 +62,77 @@ Then, you can use the `site.data` attributes accordingly:
64
62
  {% endfor %}
65
63
  ```
66
64
 
65
+ ## Generating Pages
66
+
67
+ It works well with the `datapage_gen` plugin:
68
+
69
+ See the [datapage_gen](https://github.com/avillafiorita/jekyll-datapage_gen) docs for more details.
70
+
71
+ Here's a sample configuration:
72
+
73
+ ```yaml
74
+ # This will automatically generate a file for each restaurant
75
+ # restaurants/#{id}.html file
76
+ # with the layout `_layouts/restaurant.html`
77
+ # and page.id, page.name, page.active set
78
+ # and page.title set to restaurant name
79
+ sqlite:
80
+ restaurants:
81
+ file: _db/reviews.db
82
+ sql: SELECT id, name, last_review_date > 1672531200 as active, address FROM restaurants;
83
+ page_gen:
84
+ - data: restaurants
85
+ template: restaurant
86
+ name: id
87
+ title: name
88
+ filter: active
89
+ ```
90
+
91
+ ## Nested Queries
92
+
93
+ You can use the rows of `site.data.items` as bind_params for your nested query. For this to work against
94
+ data generated by the plugin, the configuration order must be correct, so you need `site.data.items` above `site.data.items.nested` in your configuration.
95
+
96
+ Say you have a YAML file defining your items (`data/items.yaml`):
97
+
98
+ ```yaml
99
+ - id: 31323952-2708-42dc-a995-6006a23cbf00
100
+ name: Item 1
101
+ - id: 5c8e67a0-d490-4743-b5b8-8e67bd1f95a2
102
+ name: Item 2
103
+ ```
104
+ and the prices for the items in your SQLite database, the following configuration will enrich the `items` array with the price:
105
+
106
+ ```yaml
107
+ sql:
108
+ - data: site.data.items.meta
109
+ query: SELECT price,author FROM pricing WHERE id =:id
110
+ ```
111
+ This would allow the following Liquid loop to be written:
112
+
113
+ ```liquid
114
+ {% for item in site.data.items %}
115
+ {{item.meta.price}}, {{item.meta.author}}
116
+ {% endfor %}
117
+ ```
118
+
119
+ This works well with `results_as_configuration` as well.
120
+
121
+ ```yaml
122
+ sql:
123
+ - data: site.data.items.meta
124
+ query: SELECT price,author FROM pricing WHERE id =:id
125
+ results_as_hash: false
126
+ ```
127
+
128
+ The following also renders the price and author:
129
+
130
+ ```liquid
131
+ {% for item in site.data.items %}
132
+ {{item.meta[0]}}, {{item.meta[1]}}
133
+ {% endfor %}
134
+ ```
135
+
67
136
  ## Development
68
137
 
69
138
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -13,14 +13,72 @@ module JekyllSQlite
13
13
  db.execute("PRAGMA query_only = ON")
14
14
  end
15
15
 
16
+ # Get the root of where we are generating the data
17
+ def get_root(root, name)
18
+ name.split(".")[0..-2].each do |p|
19
+ root = root[p]
20
+ end
21
+ root
22
+ end
23
+
24
+ def gen_hash_data(root, db, _query)
25
+ root ||= {}
26
+
27
+ root[name] = db.execute(config["query"])
28
+ root[name].size
29
+ end
30
+
31
+ def gen_nested_data(item, db, query, name)
32
+ item[name] = []
33
+ db.prepare(query) do |stmt|
34
+ # We bind params, ignoring any errors
35
+ # Since there's no way to get required params
36
+ # From a statement
37
+ item.each do |key, value|
38
+ stmt.bind_param key, value
39
+ rescue StandardError # rubocop:disable Lint/SuppressedException
40
+ end
41
+ stmt.execute.each { |d| item[name] << d }
42
+ end
43
+ item[name].size
44
+ end
45
+
46
+ def array_gen(root, config, name, db)
47
+ count = 0
48
+ root.each do |item|
49
+ # TODO: Add support for binding Arrays as well.
50
+ if item.is_a? Hash
51
+ count += gen_nested_data(item, db, config["query"], name)
52
+ else
53
+ Jekyll.logger.info "Jekyll SQLite:", "Item is not a hash for #{name}. Unsupported configuration"
54
+ end
55
+ end
56
+ count
57
+ end
58
+
59
+ def gen_data(root, config, name, db)
60
+ count = 0
61
+ if root.nil? || (root.is_a? Hash)
62
+ count = gen_hash_data(root, db, config["query"])
63
+ elsif root.is_a? Array
64
+ count = array_gen(root, config, name, db)
65
+ end
66
+ count
67
+ end
68
+
69
+ def get_tip(name)
70
+ name.split(".")[-1]
71
+ end
72
+
16
73
  def generate(site)
17
- site.config["sqlite"].each do |name, config|
74
+ site.config["sqlite"].each do |config|
75
+ name = config["data"]
18
76
  SQLite3::Database.new config["file"], readonly: true do |db|
19
- Jekyll.logger.info "Jekyll SQLite:", "Starting to load #{name}"
20
77
  fast_setup db
21
78
  db.results_as_hash = config.fetch("results_as_hash", true)
22
- site.data[name] = db.execute(config["query"])
23
- Jekyll.logger.info "Jekyll SQLite:", "Loaded #{name}. Count=#{site.data[name].size}"
79
+ root = get_root(site.data, name)
80
+ count = gen_data(root, config, get_tip(name), db)
81
+ Jekyll.logger.info "Jekyll SQLite:", "Loaded #{name}. Count=#{count}. as_hash=#{db.results_as_hash}"
24
82
  end
25
83
  end
26
84
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Sqlite
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
data/lib/jekyll_sqlite.rb CHANGED
@@ -5,5 +5,4 @@ require "jekyll-sqlite/generator"
5
5
 
6
6
  module JekyllSQlite
7
7
  class Error < StandardError; end
8
- # Your code goes here...
9
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-sqlite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nemo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-08 00:00:00.000000000 Z
11
+ date: 2023-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3