Modules

16 views
Skip to first unread message

Derek Frost

unread,
Feb 4, 2015, 11:21:06 AM2/4/15
to moonscri...@googlegroups.com
I'm sorry if this is a stupid question, I'm a bit new to ms (but I know coffeescript) and I know next to nothing about Lua.

So I've been falling in love with moonscript and having great fun playing with functions and objects, but now I want to interact with modules and try to get at least a basic db to get me started - even sqlite3 would be ok for now.  I've had no success using 'require' with moonscript and I failed to get tapis installed and not sure it would help me anyway because I don't have postgres.

What I really want to know is can I somehow connect to luasql and use moonscript with a db?  I can't find any code examples at all for using moonscript with a db, except with lapis which I copied.  I've tried to get tapis via luarocks but I'm not certain it's properly installed because I have a rocks folder but not much else - but it seems to install then says error unpacking.

I must say I'm stuck now.  Not succeeding with sqlite3 is lame - I usually sort that out in seconds with languages I'm pathetic at - like lua!  Any help would be great, many thanks,

Derek

leaf corcoran

unread,
Feb 4, 2015, 3:27:44 PM2/4/15
to Derek Frost, moonscript-users
Hello,

All modules that are available in Lua are available in MoonScript. Were you able to do the same require function call with a basic Lua script?

Regarding connecting to a db, you should be able to use the Lua documentation for any library, and the mapping should be pretty straight forward to turn the Lua to MoonScript. If you send me an example of a project's documentation I can write a MoonScript example for you to illustrate.



--
You received this message because you are subscribed to the Google Groups "MoonScript Users" group.
To post to this group, send email to moonscri...@googlegroups.com.
Visit this group at http://groups.google.com/group/moonscript-users.

Harley Laue

unread,
Feb 4, 2015, 7:05:59 PM2/4/15
to moonscri...@googlegroups.com
On 02/04/2015 10:21 AM, Derek Frost wrote:
> I'm sorry if this is a stupid question, I'm a bit new to ms (but I
> know coffeescript) and I know next to nothing about Lua.
>
> So I've been falling in love with moonscript and having great fun
> playing with functions and objects, but now I want to interact with
> modules and try to get at least a basic db to get me started - even
> sqlite3 would be ok for now. I've had no success using 'require' with
> moonscript and I failed to get tapis installed and not sure it would
> help me anyway because I don't have postgres.
>
> What I really want to know is can I somehow connect to luasql and use
> moonscript with a db? I can't find any code examples at all for using
> moonscript with a db, except with lapis which I copied. I've tried to
> get tapis via luarocks but I'm not certain it's properly installed
> because I have a rocks folder but not much else - but it seems to
> install then says error unpacking.
You've said "tapis" a few times by this point, do you mean Lapis? I
think you're right, if you don't want to or can't setup PostgreSQL,
SQLite is likely going to be your option.
https://github.com/leafo/lapis/blob/master/lapis/db.moon
>
> I must say I'm stuck now. Not succeeding with sqlite3 is lame - I
> usually sort that out in seconds with languages I'm pathetic at - like
> lua! Any help would be great, many thanks,

I tend to write one-off database updates here at work in Moonscript &
LuaSQL (specifically with luasql.mysql) Below is an example with some
comments added describing most of the functionality:

-- This is a database change to convert a table from being stored in hours
-- to minutes

-- Require luasql.mysql, assert if it doesn't load, and store the result
-- into luasql
luasql = assert require 'luasql.mysql'
-- Just some variables to define the host, username, & password
host = ''
user = ''
pass = ''

-- A select to get the id & hours from the entries table where hours are
-- not 0
entries = [[
SELECT id, hours
FROM entries
WHERE hours <> 0
]]

-- Basic update SQL to be filled in with a string.format
entry_update = [[
UPDATE entries
SET hours=%d
WHERE id=%d
]]

-- Setup the env to be mysql
env = assert luasql.mysql!
-- Actually connect to the database with the "some_schema" database
conn = assert env\connect 'some_schema', user, pass, host

-- Execute the select
cur = assert conn\execute entries

-- Get the first entry from the select as an associative array, so you can
-- access database columns with entry.column
entry = cur\fetch {}, 'a'
-- Standard loop over results
while entry
-- Execute the update with the hours converted to minutes
update = string.format entry_update, math.ceil(entry.hours*60),
entry.id
assert conn\execute update

-- Fetch the next result and store it in entry
entry = cur\fetch {}, 'a'

-- Close the cursor
cur\close!

-- Alter the table column name
assert conn\execute [[
ALTER TABLE entries
CHANGE COLUMN hours minutes INTEGER UNSIGNED NOT NULL DEFAULT 0
]]

-- close the connection & environment
conn\close!
env\close!

IIRC in the LuaSQL documentation, the env, conn, & cur will look very
similar to what they used there. Let me know if you have any questions,
because I use LuaSQL with Moonscript semi-regularly for maintenance tasks.

Derek Frost

unread,
Feb 10, 2015, 10:41:37 AM2/10/15
to moonscri...@googlegroups.com
Many thanks for your replies.  Well now I'm using moonscript with sqlite3 and it's great and does what I want.  But I can't get any new modules with luarocks.

I reinstalled luarocks and I have some folders I didn't have before.  I also followed some instructions which made me set up mingw which I believe is a c compiler and changed my path accordingly.  None of this affects moonscript which worked fine before, but anything new I try to search or install with luarocks comes up 'wget' is not a recognized command.

I'm not really interested in lua, but it's a concern that I can't get at new modules.  Yes, sorry I meant to say lapis and not tapis but it doesn't install whatever I type..  :(

Regards,

Derek

Harley Laue

unread,
Feb 11, 2015, 4:35:22 PM2/11/15
to moonscri...@googlegroups.com
I'm not sure how you installed MinGW (and I haven't done it in ages it seems), but it sounds like you need to go:
http://sourceforge.net/projects/mingw/files/Other/mingwPORT/Current%20Releases/
and download the wget-1.9.1-mingwPORT.tar.bz2 then extract wget.exe that into your MinGW install directory. Something like: C:\mingw\bin

The other option would be if you used mingw-get. You can look through the list of packages it provides to see if wget is one of them.

Hopefully once you have a working wget, it should only be a matter of running luarocks as you normally would. Keep in mind that if the Luarocks install path isn't the same as your Lua install path (usually isn't), you may need to use luarocks.loader with either a require or passing it in as an option to Lua (-lluarocks.loader as a command line option.) This should only affect you if you compile your Moonscript sources to Lua and then run it directly with with Lua.
--
Reply all
Reply to author
Forward
0 new messages