├── .gitignore
├── LICENSE
├── README.md
├── bower.json
├── package-lock.json
├── package.json
├── src
└── Sequelize
│ ├── CRUD.purs
│ ├── CRUD
│ ├── Create.js
│ ├── Create.purs
│ ├── Destroy.js
│ ├── Destroy.purs
│ ├── Read.js
│ ├── Read.purs
│ ├── Unsafe.purs
│ ├── Update.js
│ └── Update.purs
│ ├── Class.purs
│ ├── Connection.js
│ ├── Connection.purs
│ ├── Free.purs
│ ├── Free
│ ├── Create.purs
│ ├── Destroy.purs
│ ├── Read.purs
│ └── Update.purs
│ ├── Instance.js
│ ├── Instance.purs
│ ├── Models.js
│ ├── Models.purs
│ ├── Models
│ ├── Columns.purs
│ ├── Options.purs
│ ├── Types.js
│ └── Types.purs
│ ├── Query
│ ├── Options.js
│ ├── Options.purs
│ ├── Options
│ │ └── Common.purs
│ └── Util.purs
│ ├── Transaction.js
│ ├── Transaction.purs
│ ├── Types.js
│ ├── Types.purs
│ └── Where.purs
└── test
├── Common.purs
├── Prelude.purs
├── Test.purs
├── js
├── associations.js
└── query.js
└── passing
├── Association.purs
├── Connection.purs
├── Free.purs
├── Insertion.purs
├── Instance.purs
├── Query.purs
└── Where.purs
/.gitignore:
--------------------------------------------------------------------------------
1 | # Dependencies
2 | .psci_modules
3 | bower_components
4 | node_modules
5 | .psc-ide-port
6 |
7 | # Generated files
8 | .psci
9 | output
10 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | ABOUT JUSPAY LICENSES
2 | JUSPAY platform is an open source project, whose source and
3 | copyright is owned and maintained by JUSPAY Technology Private Limited.
4 | This page is intended to clarify the licenses and terms under
5 | which JUSPAY platform is made available.
6 |
7 | JUSPAY COMMERCIAL LICENSE
8 | Entities who desire to use JUSPAY source software applications for
9 | any commercial purpose shall use JUSPAY Enterprise Edition. You must
10 | obtain a commercial license from JUSPAY Technologies Private Limited,
11 | by contacting us at info@juspay.in. The Enterprise License or
12 | closed-source software applications requires a commercial license,
13 | offered under a separate license term.
14 |
15 | JUSPAY EDUCATION EDITION
16 | Education Edition Licenses are provided to “qualified institutions” like
17 | schools, colleges, training institutes and any other entity that shall use the
18 | code for educational purposes on a non-commercial basis. Non-commercial herein
19 | means not primarily intended for or directed towards commercial advantage or
20 | monetary compensation. The licensed software shall not be used by any
21 | entity making any commercial value of the platform. Only qualified institutions
22 | and private/career schools that use the platform for a non-commercial/internal
23 | educational use qualify to use the licensed platform. The education edition
24 | license by JUSPAY shall be governed by GNU AGPL V3. You should have received a
25 | copy of the GNU Affero General Public License along with this program.
26 | If not, see https://www.gnu.org/licenses/agpl.html.
27 |
28 |
29 | INDEMNIFICATION AND DISCLAIMER
30 | By using the Education Edition license JUSPAY makes no representations or
31 | warranties, expressed or implied. The Licensed Software is provided free of
32 | charge, and, therefore, on an "as is" basis, without warranty of any kind,
33 | explicitly or implicitly. By way of example, but not limitation, JUSPAY makes
34 | no representations or warranties of merchantability or fitness for the
35 | Licensed Software and any particular application or that the use of the
36 | Licensed Software or will not infringe any patents, copyrights or trademarks
37 | or other rights of third parties. The entire risk as to the quality and
38 | performance of the product is borne by the end user. JUSPAY shall not be
39 | liable for any liability or damages with respect to any claim by the end user
40 | or any third party on account of or arising from the license or use of the
41 | Software. The end user will be liable for all damages without limitation,
42 | which is caused by the abuse of the Licensed Software and shall indemnify
43 | JUSPAY for such damages, claims, cost, including reasonable attorney
44 | fee claimed on JUSPAY. The end user has no right to claim any indemnification
45 | based on its use of Licensed Software.
46 |
47 |
48 |
49 |
50 |
51 | GNU AFFERO GENERAL PUBLIC LICENSE
52 | Version 3, 19 November 2007
53 |
54 | Copyright (C) 2007 Free Software Foundation, Inc.
55 | Everyone is permitted to copy and distribute verbatim copies
56 | of this license document, but changing it is not allowed.
57 |
58 | Preamble
59 |
60 | The GNU Affero General Public License is a free, copyleft license for
61 | software and other kinds of works, specifically designed to ensure
62 | cooperation with the community in the case of network server software.
63 |
64 | The licenses for most software and other practical works are designed
65 | to take away your freedom to share and change the works. By contrast,
66 | our General Public Licenses are intended to guarantee your freedom to
67 | share and change all versions of a program--to make sure it remains free
68 | software for all its users.
69 |
70 | When we speak of free software, we are referring to freedom, not
71 | price. Our General Public Licenses are designed to make sure that you
72 | have the freedom to distribute copies of free software (and charge for
73 | them if you wish), that you receive source code or can get it if you
74 | want it, that you can change the software or use pieces of it in new
75 | free programs, and that you know you can do these things.
76 |
77 | Developers that use our General Public Licenses protect your rights
78 | with two steps: (1) assert copyright on the software, and (2) offer
79 | you this License which gives you legal permission to copy, distribute
80 | and/or modify the software.
81 |
82 | A secondary benefit of defending all users' freedom is that
83 | improvements made in alternate versions of the program, if they
84 | receive widespread use, become available for other developers to
85 | incorporate. Many developers of free software are heartened and
86 | encouraged by the resulting cooperation. However, in the case of
87 | software used on network servers, this result may fail to come about.
88 | The GNU General Public License permits making a modified version and
89 | letting the public access it on a server without ever releasing its
90 | source code to the public.
91 |
92 | The GNU Affero General Public License is designed specifically to
93 | ensure that, in such cases, the modified source code becomes available
94 | to the community. It requires the operator of a network server to
95 | provide the source code of the modified version running there to the
96 | users of that server. Therefore, public use of a modified version, on
97 | a publicly accessible server, gives the public access to the source
98 | code of the modified version.
99 |
100 | An older license, called the Affero General Public License and
101 | published by Affero, was designed to accomplish similar goals. This is
102 | a different license, not a version of the Affero GPL, but Affero has
103 | released a new version of the Affero GPL which permits relicensing under
104 | this license.
105 |
106 | The precise terms and conditions for copying, distribution and
107 | modification follow.
108 |
109 | TERMS AND CONDITIONS
110 |
111 | 0. Definitions.
112 |
113 | "This License" refers to version 3 of the GNU Affero General Public License.
114 |
115 | "Copyright" also means copyright-like laws that apply to other kinds of
116 | works, such as semiconductor masks.
117 |
118 | "The Program" refers to any copyrightable work licensed under this
119 | License. Each licensee is addressed as "you". "Licensees" and
120 | "recipients" may be individuals or organizations.
121 |
122 | To "modify" a work means to copy from or adapt all or part of the work
123 | in a fashion requiring copyright permission, other than the making of an
124 | exact copy. The resulting work is called a "modified version" of the
125 | earlier work or a work "based on" the earlier work.
126 |
127 | A "covered work" means either the unmodified Program or a work based
128 | on the Program.
129 |
130 | To "propagate" a work means to do anything with it that, without
131 | permission, would make you directly or secondarily liable for
132 | infringement under applicable copyright law, except executing it on a
133 | computer or modifying a private copy. Propagation includes copying,
134 | distribution (with or without modification), making available to the
135 | public, and in some countries other activities as well.
136 |
137 | To "convey" a work means any kind of propagation that enables other
138 | parties to make or receive copies. Mere interaction with a user through
139 | a computer network, with no transfer of a copy, is not conveying.
140 |
141 | An interactive user interface displays "Appropriate Legal Notices"
142 | to the extent that it includes a convenient and prominently visible
143 | feature that (1) displays an appropriate copyright notice, and (2)
144 | tells the user that there is no warranty for the work (except to the
145 | extent that warranties are provided), that licensees may convey the
146 | work under this License, and how to view a copy of this License. If
147 | the interface presents a list of user commands or options, such as a
148 | menu, a prominent item in the list meets this criterion.
149 |
150 | 1. Source Code.
151 |
152 | The "source code" for a work means the preferred form of the work
153 | for making modifications to it. "Object code" means any non-source
154 | form of a work.
155 |
156 | A "Standard Interface" means an interface that either is an official
157 | standard defined by a recognized standards body, or, in the case of
158 | interfaces specified for a particular programming language, one that
159 | is widely used among developers working in that language.
160 |
161 | The "System Libraries" of an executable work include anything, other
162 | than the work as a whole, that (a) is included in the normal form of
163 | packaging a Major Component, but which is not part of that Major
164 | Component, and (b) serves only to enable use of the work with that
165 | Major Component, or to implement a Standard Interface for which an
166 | implementation is available to the public in source code form. A
167 | "Major Component", in this context, means a major essential component
168 | (kernel, window system, and so on) of the specific operating system
169 | (if any) on which the executable work runs, or a compiler used to
170 | produce the work, or an object code interpreter used to run it.
171 |
172 | The "Corresponding Source" for a work in object code form means all
173 | the source code needed to generate, install, and (for an executable
174 | work) run the object code and to modify the work, including scripts to
175 | control those activities. However, it does not include the work's
176 | System Libraries, or general-purpose tools or generally available free
177 | programs which are used unmodified in performing those activities but
178 | which are not part of the work. For example, Corresponding Source
179 | includes interface definition files associated with source files for
180 | the work, and the source code for shared libraries and dynamically
181 | linked subprograms that the work is specifically designed to require,
182 | such as by intimate data communication or control flow between those
183 | subprograms and other parts of the work.
184 |
185 | The Corresponding Source need not include anything that users
186 | can regenerate automatically from other parts of the Corresponding
187 | Source.
188 |
189 | The Corresponding Source for a work in source code form is that
190 | same work.
191 |
192 | 2. Basic Permissions.
193 |
194 | All rights granted under this License are granted for the term of
195 | copyright on the Program, and are irrevocable provided the stated
196 | conditions are met. This License explicitly affirms your unlimited
197 | permission to run the unmodified Program. The output from running a
198 | covered work is covered by this License only if the output, given its
199 | content, constitutes a covered work. This License acknowledges your
200 | rights of fair use or other equivalent, as provided by copyright law.
201 |
202 | You may make, run and propagate covered works that you do not
203 | convey, without conditions so long as your license otherwise remains
204 | in force. You may convey covered works to others for the sole purpose
205 | of having them make modifications exclusively for you, or provide you
206 | with facilities for running those works, provided that you comply with
207 | the terms of this License in conveying all material for which you do
208 | not control copyright. Those thus making or running the covered works
209 | for you must do so exclusively on your behalf, under your direction
210 | and control, on terms that prohibit them from making any copies of
211 | your copyrighted material outside their relationship with you.
212 |
213 | Conveying under any other circumstances is permitted solely under
214 | the conditions stated below. Sublicensing is not allowed; section 10
215 | makes it unnecessary.
216 |
217 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
218 |
219 | No covered work shall be deemed part of an effective technological
220 | measure under any applicable law fulfilling obligations under article
221 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
222 | similar laws prohibiting or restricting circumvention of such
223 | measures.
224 |
225 | When you convey a covered work, you waive any legal power to forbid
226 | circumvention of technological measures to the extent such circumvention
227 | is effected by exercising rights under this License with respect to
228 | the covered work, and you disclaim any intention to limit operation or
229 | modification of the work as a means of enforcing, against the work's
230 | users, your or third parties' legal rights to forbid circumvention of
231 | technological measures.
232 |
233 | 4. Conveying Verbatim Copies.
234 |
235 | You may convey verbatim copies of the Program's source code as you
236 | receive it, in any medium, provided that you conspicuously and
237 | appropriately publish on each copy an appropriate copyright notice;
238 | keep intact all notices stating that this License and any
239 | non-permissive terms added in accord with section 7 apply to the code;
240 | keep intact all notices of the absence of any warranty; and give all
241 | recipients a copy of this License along with the Program.
242 |
243 | You may charge any price or no price for each copy that you convey,
244 | and you may offer support or warranty protection for a fee.
245 |
246 | 5. Conveying Modified Source Versions.
247 |
248 | You may convey a work based on the Program, or the modifications to
249 | produce it from the Program, in the form of source code under the
250 | terms of section 4, provided that you also meet all of these conditions:
251 |
252 | a) The work must carry prominent notices stating that you modified
253 | it, and giving a relevant date.
254 |
255 | b) The work must carry prominent notices stating that it is
256 | released under this License and any conditions added under section
257 | 7. This requirement modifies the requirement in section 4 to
258 | "keep intact all notices".
259 |
260 | c) You must license the entire work, as a whole, under this
261 | License to anyone who comes into possession of a copy. This
262 | License will therefore apply, along with any applicable section 7
263 | additional terms, to the whole of the work, and all its parts,
264 | regardless of how they are packaged. This License gives no
265 | permission to license the work in any other way, but it does not
266 | invalidate such permission if you have separately received it.
267 |
268 | d) If the work has interactive user interfaces, each must display
269 | Appropriate Legal Notices; however, if the Program has interactive
270 | interfaces that do not display Appropriate Legal Notices, your
271 | work need not make them do so.
272 |
273 | A compilation of a covered work with other separate and independent
274 | works, which are not by their nature extensions of the covered work,
275 | and which are not combined with it such as to form a larger program,
276 | in or on a volume of a storage or distribution medium, is called an
277 | "aggregate" if the compilation and its resulting copyright are not
278 | used to limit the access or legal rights of the compilation's users
279 | beyond what the individual works permit. Inclusion of a covered work
280 | in an aggregate does not cause this License to apply to the other
281 | parts of the aggregate.
282 |
283 | 6. Conveying Non-Source Forms.
284 |
285 | You may convey a covered work in object code form under the terms
286 | of sections 4 and 5, provided that you also convey the
287 | machine-readable Corresponding Source under the terms of this License,
288 | in one of these ways:
289 |
290 | a) Convey the object code in, or embodied in, a physical product
291 | (including a physical distribution medium), accompanied by the
292 | Corresponding Source fixed on a durable physical medium
293 | customarily used for software interchange.
294 |
295 | b) Convey the object code in, or embodied in, a physical product
296 | (including a physical distribution medium), accompanied by a
297 | written offer, valid for at least three years and valid for as
298 | long as you offer spare parts or customer support for that product
299 | model, to give anyone who possesses the object code either (1) a
300 | copy of the Corresponding Source for all the software in the
301 | product that is covered by this License, on a durable physical
302 | medium customarily used for software interchange, for a price no
303 | more than your reasonable cost of physically performing this
304 | conveying of source, or (2) access to copy the
305 | Corresponding Source from a network server at no charge.
306 |
307 | c) Convey individual copies of the object code with a copy of the
308 | written offer to provide the Corresponding Source. This
309 | alternative is allowed only occasionally and noncommercially, and
310 | only if you received the object code with such an offer, in accord
311 | with subsection 6b.
312 |
313 | d) Convey the object code by offering access from a designated
314 | place (gratis or for a charge), and offer equivalent access to the
315 | Corresponding Source in the same way through the same place at no
316 | further charge. You need not require recipients to copy the
317 | Corresponding Source along with the object code. If the place to
318 | copy the object code is a network server, the Corresponding Source
319 | may be on a different server (operated by you or a third party)
320 | that supports equivalent copying facilities, provided you maintain
321 | clear directions next to the object code saying where to find the
322 | Corresponding Source. Regardless of what server hosts the
323 | Corresponding Source, you remain obligated to ensure that it is
324 | available for as long as needed to satisfy these requirements.
325 |
326 | e) Convey the object code using peer-to-peer transmission, provided
327 | you inform other peers where the object code and Corresponding
328 | Source of the work are being offered to the general public at no
329 | charge under subsection 6d.
330 |
331 | A separable portion of the object code, whose source code is excluded
332 | from the Corresponding Source as a System Library, need not be
333 | included in conveying the object code work.
334 |
335 | A "User Product" is either (1) a "consumer product", which means any
336 | tangible personal property which is normally used for personal, family,
337 | or household purposes, or (2) anything designed or sold for incorporation
338 | into a dwelling. In determining whether a product is a consumer product,
339 | doubtful cases shall be resolved in favor of coverage. For a particular
340 | product received by a particular user, "normally used" refers to a
341 | typical or common use of that class of product, regardless of the status
342 | of the particular user or of the way in which the particular user
343 | actually uses, or expects or is expected to use, the product. A product
344 | is a consumer product regardless of whether the product has substantial
345 | commercial, industrial or non-consumer uses, unless such uses represent
346 | the only significant mode of use of the product.
347 |
348 | "Installation Information" for a User Product means any methods,
349 | procedures, authorization keys, or other information required to install
350 | and execute modified versions of a covered work in that User Product from
351 | a modified version of its Corresponding Source. The information must
352 | suffice to ensure that the continued functioning of the modified object
353 | code is in no case prevented or interfered with solely because
354 | modification has been made.
355 |
356 | If you convey an object code work under this section in, or with, or
357 | specifically for use in, a User Product, and the conveying occurs as
358 | part of a transaction in which the right of possession and use of the
359 | User Product is transferred to the recipient in perpetuity or for a
360 | fixed term (regardless of how the transaction is characterized), the
361 | Corresponding Source conveyed under this section must be accompanied
362 | by the Installation Information. But this requirement does not apply
363 | if neither you nor any third party retains the ability to install
364 | modified object code on the User Product (for example, the work has
365 | been installed in ROM).
366 |
367 | The requirement to provide Installation Information does not include a
368 | requirement to continue to provide support service, warranty, or updates
369 | for a work that has been modified or installed by the recipient, or for
370 | the User Product in which it has been modified or installed. Access to a
371 | network may be denied when the modification itself materially and
372 | adversely affects the operation of the network or violates the rules and
373 | protocols for communication across the network.
374 |
375 | Corresponding Source conveyed, and Installation Information provided,
376 | in accord with this section must be in a format that is publicly
377 | documented (and with an implementation available to the public in
378 | source code form), and must require no special password or key for
379 | unpacking, reading or copying.
380 |
381 | 7. Additional Terms.
382 |
383 | "Additional permissions" are terms that supplement the terms of this
384 | License by making exceptions from one or more of its conditions.
385 | Additional permissions that are applicable to the entire Program shall
386 | be treated as though they were included in this License, to the extent
387 | that they are valid under applicable law. If additional permissions
388 | apply only to part of the Program, that part may be used separately
389 | under those permissions, but the entire Program remains governed by
390 | this License without regard to the additional permissions.
391 |
392 | When you convey a copy of a covered work, you may at your option
393 | remove any additional permissions from that copy, or from any part of
394 | it. (Additional permissions may be written to require their own
395 | removal in certain cases when you modify the work.) You may place
396 | additional permissions on material, added by you to a covered work,
397 | for which you have or can give appropriate copyright permission.
398 |
399 | Notwithstanding any other provision of this License, for material you
400 | add to a covered work, you may (if authorized by the copyright holders of
401 | that material) supplement the terms of this License with terms:
402 |
403 | a) Disclaiming warranty or limiting liability differently from the
404 | terms of sections 15 and 16 of this License; or
405 |
406 | b) Requiring preservation of specified reasonable legal notices or
407 | author attributions in that material or in the Appropriate Legal
408 | Notices displayed by works containing it; or
409 |
410 | c) Prohibiting misrepresentation of the origin of that material, or
411 | requiring that modified versions of such material be marked in
412 | reasonable ways as different from the original version; or
413 |
414 | d) Limiting the use for publicity purposes of names of licensors or
415 | authors of the material; or
416 |
417 | e) Declining to grant rights under trademark law for use of some
418 | trade names, trademarks, or service marks; or
419 |
420 | f) Requiring indemnification of licensors and authors of that
421 | material by anyone who conveys the material (or modified versions of
422 | it) with contractual assumptions of liability to the recipient, for
423 | any liability that these contractual assumptions directly impose on
424 | those licensors and authors.
425 |
426 | All other non-permissive additional terms are considered "further
427 | restrictions" within the meaning of section 10. If the Program as you
428 | received it, or any part of it, contains a notice stating that it is
429 | governed by this License along with a term that is a further
430 | restriction, you may remove that term. If a license document contains
431 | a further restriction but permits relicensing or conveying under this
432 | License, you may add to a covered work material governed by the terms
433 | of that license document, provided that the further restriction does
434 | not survive such relicensing or conveying.
435 |
436 | If you add terms to a covered work in accord with this section, you
437 | must place, in the relevant source files, a statement of the
438 | additional terms that apply to those files, or a notice indicating
439 | where to find the applicable terms.
440 |
441 | Additional terms, permissive or non-permissive, may be stated in the
442 | form of a separately written license, or stated as exceptions;
443 | the above requirements apply either way.
444 |
445 | 8. Termination.
446 |
447 | You may not propagate or modify a covered work except as expressly
448 | provided under this License. Any attempt otherwise to propagate or
449 | modify it is void, and will automatically terminate your rights under
450 | this License (including any patent licenses granted under the third
451 | paragraph of section 11).
452 |
453 | However, if you cease all violation of this License, then your
454 | license from a particular copyright holder is reinstated (a)
455 | provisionally, unless and until the copyright holder explicitly and
456 | finally terminates your license, and (b) permanently, if the copyright
457 | holder fails to notify you of the violation by some reasonable means
458 | prior to 60 days after the cessation.
459 |
460 | Moreover, your license from a particular copyright holder is
461 | reinstated permanently if the copyright holder notifies you of the
462 | violation by some reasonable means, this is the first time you have
463 | received notice of violation of this License (for any work) from that
464 | copyright holder, and you cure the violation prior to 30 days after
465 | your receipt of the notice.
466 |
467 | Termination of your rights under this section does not terminate the
468 | licenses of parties who have received copies or rights from you under
469 | this License. If your rights have been terminated and not permanently
470 | reinstated, you do not qualify to receive new licenses for the same
471 | material under section 10.
472 |
473 | 9. Acceptance Not Required for Having Copies.
474 |
475 | You are not required to accept this License in order to receive or
476 | run a copy of the Program. Ancillary propagation of a covered work
477 | occurring solely as a consequence of using peer-to-peer transmission
478 | to receive a copy likewise does not require acceptance. However,
479 | nothing other than this License grants you permission to propagate or
480 | modify any covered work. These actions infringe copyright if you do
481 | not accept this License. Therefore, by modifying or propagating a
482 | covered work, you indicate your acceptance of this License to do so.
483 |
484 | 10. Automatic Licensing of Downstream Recipients.
485 |
486 | Each time you convey a covered work, the recipient automatically
487 | receives a license from the original licensors, to run, modify and
488 | propagate that work, subject to this License. You are not responsible
489 | for enforcing compliance by third parties with this License.
490 |
491 | An "entity transaction" is a transaction transferring control of an
492 | organization, or substantially all assets of one, or subdividing an
493 | organization, or merging organizations. If propagation of a covered
494 | work results from an entity transaction, each party to that
495 | transaction who receives a copy of the work also receives whatever
496 | licenses to the work the party's predecessor in interest had or could
497 | give under the previous paragraph, plus a right to possession of the
498 | Corresponding Source of the work from the predecessor in interest, if
499 | the predecessor has it or can get it with reasonable efforts.
500 |
501 | You may not impose any further restrictions on the exercise of the
502 | rights granted or affirmed under this License. For example, you may
503 | not impose a license fee, royalty, or other charge for exercise of
504 | rights granted under this License, and you may not initiate litigation
505 | (including a cross-claim or counterclaim in a lawsuit) alleging that
506 | any patent claim is infringed by making, using, selling, offering for
507 | sale, or importing the Program or any portion of it.
508 |
509 | 11. Patents.
510 |
511 | A "contributor" is a copyright holder who authorizes use under this
512 | License of the Program or a work on which the Program is based. The
513 | work thus licensed is called the contributor's "contributor version".
514 |
515 | A contributor's "essential patent claims" are all patent claims
516 | owned or controlled by the contributor, whether already acquired or
517 | hereafter acquired, that would be infringed by some manner, permitted
518 | by this License, of making, using, or selling its contributor version,
519 | but do not include claims that would be infringed only as a
520 | consequence of further modification of the contributor version. For
521 | purposes of this definition, "control" includes the right to grant
522 | patent sublicenses in a manner consistent with the requirements of
523 | this License.
524 |
525 | Each contributor grants you a non-exclusive, worldwide, royalty-free
526 | patent license under the contributor's essential patent claims, to
527 | make, use, sell, offer for sale, import and otherwise run, modify and
528 | propagate the contents of its contributor version.
529 |
530 | In the following three paragraphs, a "patent license" is any express
531 | agreement or commitment, however denominated, not to enforce a patent
532 | (such as an express permission to practice a patent or covenant not to
533 | sue for patent infringement). To "grant" such a patent license to a
534 | party means to make such an agreement or commitment not to enforce a
535 | patent against the party.
536 |
537 | If you convey a covered work, knowingly relying on a patent license,
538 | and the Corresponding Source of the work is not available for anyone
539 | to copy, free of charge and under the terms of this License, through a
540 | publicly available network server or other readily accessible means,
541 | then you must either (1) cause the Corresponding Source to be so
542 | available, or (2) arrange to deprive yourself of the benefit of the
543 | patent license for this particular work, or (3) arrange, in a manner
544 | consistent with the requirements of this License, to extend the patent
545 | license to downstream recipients. "Knowingly relying" means you have
546 | actual knowledge that, but for the patent license, your conveying the
547 | covered work in a country, or your recipient's use of the covered work
548 | in a country, would infringe one or more identifiable patents in that
549 | country that you have reason to believe are valid.
550 |
551 | If, pursuant to or in connection with a single transaction or
552 | arrangement, you convey, or propagate by procuring conveyance of, a
553 | covered work, and grant a patent license to some of the parties
554 | receiving the covered work authorizing them to use, propagate, modify
555 | or convey a specific copy of the covered work, then the patent license
556 | you grant is automatically extended to all recipients of the covered
557 | work and works based on it.
558 |
559 | A patent license is "discriminatory" if it does not include within
560 | the scope of its coverage, prohibits the exercise of, or is
561 | conditioned on the non-exercise of one or more of the rights that are
562 | specifically granted under this License. You may not convey a covered
563 | work if you are a party to an arrangement with a third party that is
564 | in the business of distributing software, under which you make payment
565 | to the third party based on the extent of your activity of conveying
566 | the work, and under which the third party grants, to any of the
567 | parties who would receive the covered work from you, a discriminatory
568 | patent license (a) in connection with copies of the covered work
569 | conveyed by you (or copies made from those copies), or (b) primarily
570 | for and in connection with specific products or compilations that
571 | contain the covered work, unless you entered into that arrangement,
572 | or that patent license was granted, prior to 28 March 2007.
573 |
574 | Nothing in this License shall be construed as excluding or limiting
575 | any implied license or other defenses to infringement that may
576 | otherwise be available to you under applicable patent law.
577 |
578 | 12. No Surrender of Others' Freedom.
579 |
580 | If conditions are imposed on you (whether by court order, agreement or
581 | otherwise) that contradict the conditions of this License, they do not
582 | excuse you from the conditions of this License. If you cannot convey a
583 | covered work so as to satisfy simultaneously your obligations under this
584 | License and any other pertinent obligations, then as a consequence you may
585 | not convey it at all. For example, if you agree to terms that obligate you
586 | to collect a royalty for further conveying from those to whom you convey
587 | the Program, the only way you could satisfy both those terms and this
588 | License would be to refrain entirely from conveying the Program.
589 |
590 | 13. Remote Network Interaction; Use with the GNU General Public License.
591 |
592 | Notwithstanding any other provision of this License, if you modify the
593 | Program, your modified version must prominently offer all users
594 | interacting with it remotely through a computer network (if your version
595 | supports such interaction) an opportunity to receive the Corresponding
596 | Source of your version by providing access to the Corresponding Source
597 | from a network server at no charge, through some standard or customary
598 | means of facilitating copying of software. This Corresponding Source
599 | shall include the Corresponding Source for any work covered by version 3
600 | of the GNU General Public License that is incorporated pursuant to the
601 | following paragraph.
602 |
603 | Notwithstanding any other provision of this License, you have
604 | permission to link or combine any covered work with a work licensed
605 | under version 3 of the GNU General Public License into a single
606 | combined work, and to convey the resulting work. The terms of this
607 | License will continue to apply to the part which is the covered work,
608 | but the work with which it is combined will remain governed by version
609 | 3 of the GNU General Public License.
610 |
611 | 14. Revised Versions of this License.
612 |
613 | The Free Software Foundation may publish revised and/or new versions of
614 | the GNU Affero General Public License from time to time. Such new versions
615 | will be similar in spirit to the present version, but may differ in detail to
616 | address new problems or concerns.
617 |
618 | Each version is given a distinguishing version number. If the
619 | Program specifies that a certain numbered version of the GNU Affero General
620 | Public License "or any later version" applies to it, you have the
621 | option of following the terms and conditions either of that numbered
622 | version or of any later version published by the Free Software
623 | Foundation. If the Program does not specify a version number of the
624 | GNU Affero General Public License, you may choose any version ever published
625 | by the Free Software Foundation.
626 |
627 | If the Program specifies that a proxy can decide which future
628 | versions of the GNU Affero General Public License can be used, that proxy's
629 | public statement of acceptance of a version permanently authorizes you
630 | to choose that version for the Program.
631 |
632 | Later license versions may give you additional or different
633 | permissions. However, no additional obligations are imposed on any
634 | author or copyright holder as a result of your choosing to follow a
635 | later version.
636 |
637 | 15. Disclaimer of Warranty.
638 |
639 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
640 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
641 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
642 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
643 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
644 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
645 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
646 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
647 |
648 | 16. Limitation of Liability.
649 |
650 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
651 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
652 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
653 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
654 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
655 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
656 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
657 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
658 | SUCH DAMAGES.
659 |
660 | 17. Interpretation of Sections 15 and 16.
661 |
662 | If the disclaimer of warranty and limitation of liability provided
663 | above cannot be given local legal effect according to their terms,
664 | reviewing courts shall apply local law that most closely approximates
665 | an absolute waiver of all civil liability in connection with the
666 | Program, unless a warranty or assumption of liability accompanies a
667 | copy of the Program in return for a fee.
668 |
669 | END OF TERMS AND CONDITIONS
670 |
671 | How to Apply These Terms to Your New Programs
672 |
673 | If you develop a new program, and you want it to be of the greatest
674 | possible use to the public, the best way to achieve this is to make it
675 | free software which everyone can redistribute and change under these terms.
676 |
677 | To do so, attach the following notices to the program. It is safest
678 | to attach them to the start of each source file to most effectively
679 | state the exclusion of warranty; and each file should have at least
680 | the "copyright" line and a pointer to where the full notice is found.
681 |
682 |
683 | Copyright (C)
684 |
685 | This program is free software: you can redistribute it and/or modify
686 | it under the terms of the GNU Affero General Public License as published by
687 | the Free Software Foundation, either version 3 of the License, or
688 | (at your option) any later version.
689 |
690 | This program is distributed in the hope that it will be useful,
691 | but WITHOUT ANY WARRANTY; without even the implied warranty of
692 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
693 | GNU Affero General Public License for more details.
694 |
695 | You should have received a copy of the GNU Affero General Public License
696 | along with this program. If not, see .
697 |
698 | Also add information on how to contact you by electronic and paper mail.
699 |
700 | If your software can interact with users remotely through a computer
701 | network, you should also make sure that it provides a way for users to
702 | get its source. For example, if your program is a web application, its
703 | interface could display a "Source" link that leads users to an archive
704 | of the code. There are many ways you could offer source, and different
705 | solutions will be better for different programs; see section 13 for the
706 | specific requirements.
707 |
708 | You should also get your employer (if you work as a programmer) or school,
709 | if any, to sign a "copyright disclaimer" for the program, if necessary.
710 | For more information on this, and how to apply and follow the GNU AGPL, see
711 | .
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # purescript-sequelize
2 | Purescript wrapper for Sequelize - http://docs.sequelizejs.com/
3 |
4 | ## LICENSE
5 | https://github.com/juspay/purescript-sequelize/blob/master/LICENSE
6 |
7 | ## Usage
8 |
9 | ### Connections
10 |
11 | Before we can do anything, we need to have configuration information for our
12 | database.
13 | That means things like database type (SQLite, PostgreSQL, etc), username/password
14 | (if any), port, and so on.
15 |
16 | In JS that would be done like so:
17 |
18 | ```javascript
19 | const Sequelize = require('sequelize');
20 | const sequelize = new Sequelize({
21 | database: "myAwesomeDatabase",
22 | dialect: "sqlite",
23 | storage: "./awesome.sqlite"
24 | });
25 | ```
26 |
27 | In PS, the type of `sequelize` would be `Conn`.
28 | There is one way to get a value of this type:
29 |
30 | ```purescript
31 | getConn
32 | :: forall e
33 | . Options ConnOpts
34 | -> Aff ( sequelize :: SEQUELIZE | e ) Conn
35 | ```
36 |
37 | And we can use it like so:
38 |
39 | ```purescript
40 | myConn :: forall e. Aff (sequelize :: SEQUELIZE | e) Conn
41 | myConn = getConn opts
42 | where
43 | opts = database := "myAwesomeDatabase"
44 | <> dialect := SQLite
45 | <> storage := "./awesome.sqlite"
46 | ```
47 |
48 | Now that we have a connection to our database, we can start defining our tables
49 | via Sequelize's concept of a model.
50 |
51 | ### Models
52 |
53 | In PS, we use data types to represent Sequelize models.
54 | Let's say we have a database of users:
55 |
56 | ```purescript
57 | newtype User = User { name :: String, age :: Int }
58 | ```
59 |
60 | This means we want to have a table in the database for all our users, with
61 | one row that holds their name.
62 |
63 | Sequelize lets you define models using objects where keys are table columns names
64 | and values are options on those columns:
65 |
66 | ```javascript
67 | const user = sequelize.define("user", {
68 | name: {
69 | type: Sequelize.STRING(255),
70 | defaultValue: "me"
71 | },
72 | age: {
73 | type: Sequelize.INTEGER(255),
74 | defaultValue: 20
75 | }
76 | },
77 | {}
78 | );
79 | ```
80 |
81 | In order to pass an object like this to Sequelize, we'll need to transform
82 | any value of type `User` to a `Foreign` js object.
83 | To that end, we'll derive some instances:
84 |
85 | ```purescript
86 | derive instance eqUser :: Eq User
87 |
88 | derive instance genericUser :: Generic User _
89 |
90 | instance showUser :: Show User where
91 | show = genericShow
92 |
93 | instance decodeUser :: Decode User where
94 | decode x = genericDecodeModel x
95 |
96 | instance encodeUser :: Encode User where
97 | encode x = genericEncodeModel x
98 |
99 | instance encodeModelUser :: EncodeModel User where
100 | encodeModel x = genericEncodeModel x
101 |
102 | instance decodeModelUser :: DecodeModel User where
103 | decodeModel x = genericDecodeModel x
104 | ```
105 |
106 | The `EncodeModel` class is what lets Sequelize understand our PS `User` type.
107 |
108 | But recall that in our JS example, we defined a default value for the `name`
109 | column and told Sequelize its type (`STRING(255)`). We also passed it a string
110 | that tells Sequelize what name to store the table as:
111 |
112 | ```purescript
113 | instance isModelUser :: Model User where
114 | modelCols _ = userCols
115 | modelName _ = "user"
116 |
117 | userCols :: ModelCols
118 | userCols = ["name" /\ nameOpts, "age" /\ ageOpts]
119 | where
120 | nameOpts =
121 | columnType := ModelTypes.String {length: Nothing} <>
122 | defaultValue := toForeign "me"
123 | ageOpts =
124 | columnType := ModelTypes.Integer {length: Nothing} <>
125 | defaultValue := toForeign 20
126 | ```
127 |
128 | Note the catch-all pattern match in the `isModelUser` instance: this is because
129 | we need a proxy to tell the compiler how to pick an appropriate definition for
130 | our columns definition and name. If we didn't have that proxy, there wouldn't be
131 | any way to tell that the string "user" and the options for our columns correspond
132 | with our `User` type!
133 |
134 | Remember that in JS-land we had `const user = sequelize.define(...)`.
135 | In PS, that means `user` would have type `ModelOf User`.
136 | There is one way of getting a `ModelOf a`, for any `a` with an instance of `Model`:
137 |
138 | ```purescript
139 | makeModelOf
140 | :: forall a e. Model a
141 | => Conn
142 | -> Options ModelOpts
143 | -> Aff ( sequelize :: SEQUELIZE | e ) (ModelOf a)
144 | ```
145 |
146 | therefore we can get Sequelize's version of our model as in the following:
147 |
148 | ```purescript
149 | getModelOfUser :: forall e. Aff ( sequelize :: SEQUELIZE | e ) (ModelOf User)
150 | getModelOfUser = do
151 | conn <- myConn
152 | user <- makeModelOf conn mempty -- we don't want to pass any extra options
153 | syncConn conn {force: true} -- drop the current table and recreate it
154 | pure user
155 | ```
156 |
157 | ### Inserting rows to our table
158 |
159 | Suppose we have our hands on a concrete `User` value, for example from a
160 | registration form on our app's frontend:
161 |
162 | ```purescript
163 | user :: User
164 | user = User {name: "Jonas Valanciunas", age: 26}
165 | ```
166 |
167 | Sequelize lets you do this in two general ways: by building a value that then
168 | gets saved to the database, or creating it all at one time (which is equivalent
169 | to building and then saving immediately).
170 |
171 | Let's assume we're creating our user in one step. Then we can use:
172 |
173 | ```purescript
174 | create
175 | :: forall a b e
176 | . Submodel a b
177 | => ModelOf a
178 | -> b
179 | -> Aff ( sequelize :: SEQUELIZE | e ) (Instance b)
180 | ```
181 |
182 | The `Submodel` class is just a relation between two models. For example, we
183 | might also want to obtain our user's ID from the database, but there's no way
184 | to do that since if we try to use the `DecodeModel` instance, we can't get any
185 | information back besides what our user's name is. Hence, we can use a supermodel
186 | that includes the name _and_ the ID.
187 |
188 | There is an instance for reflexivity and a function for transitivity: that is,
189 | if `a` has a `Model` instance then it's already a `Submodel` of itself, and if
190 | `a` is a submodel of `b` which is a submodel of `c`, then `a` is already a
191 | submodel of `c`. (Transitivity is not provided as an instance because it's very
192 | easy to confuse the compiler as to which instance to pick).
193 |
194 | ### Retrieving data
195 |
196 | Now we want to fetch data from our table. Note that `create` gave us access to
197 | a value of type `Instance User`: this is just an opaque type representing
198 | Sequelize's notion of an instance, that is, what happens after using queries or
199 | something like `create`. In fact, all our queries in PS give us access to an
200 | `Instance User`, like the following:
201 |
202 | ```purescript
203 | findOne
204 | :: forall a b e. Submodel a b
205 | => ModelOf a
206 | -> Options b
207 | -> Aff ( sequelize :: SEQUELIZE | e ) (Maybe (Instance b))
208 | ```
209 |
210 | Since this type is opaque, we need a way to go from an `Instance User` to a
211 | regular `User`. We can use `instanceToModel`:
212 |
213 | ```purescript
214 | instanceToModel
215 | :: forall a. Model a
216 | => Instance a
217 | -> F a
218 | ```
219 |
220 | After handling any possible errors from trying to deserialize our `Instance`, we
221 | can use our `User` value.
222 |
223 | ### Using "Where"
224 |
225 | There are two different ways to construct a "where" clause in a query. In JS we
226 | might do something like the following:
227 |
228 | ```javascript
229 | user.findAll({
230 | where: {
231 | age: 20,
232 | }
233 | });
234 | ```
235 |
236 | In this example, we're matching an exact value: we want only the users whose age
237 | is exactly 20.
238 |
239 | Or we may do something a bit more complicated:
240 |
241 | ```javascript
242 | user.findAll({
243 | where: {
244 | age: {
245 | $gte: 20
246 | }
247 | }
248 | });
249 | ```
250 |
251 | In this case, we want only those users whose ages are strictly larger than 19.
252 |
253 | #### `WHERE`
254 |
255 | This is the simplest construction to use. All it does is let you match literal
256 | values in the row; it's the equivalent of the first JS example above:
257 |
258 | ```purescript
259 | simpleWhere :: WHERE User
260 | simpleWhere = WHERE [Tuple "age" (Int 20)]
261 | ```
262 |
263 | #### `Where`
264 |
265 | This is the more expressive construction. To write the equivalent of the second
266 | JS example:
267 |
268 | ```purescript
269 | moreComplicatedWhere :: Where User
270 | moreComplicatedWhere = "age" $>= 20
271 | ```
272 |
273 | We can construct more complicated clauses by using the boolean `$&` and `$|`
274 | combinators:
275 |
276 | ```purescript
277 | evenMoreComplicatedWhere :: Where User
278 | evenMoreComplicatedWhere = "age" $>= 20 $& "age" $<= 30
279 | ```
280 |
281 | And for more than two clauses conjoined by a boolean combinator, you can directly
282 | use the type constructors for `Where`:
283 |
284 | ```purescript
285 | nestedBooleanWhere :: Where User
286 | nestedBooleanWhere = Or
287 | [ "age" $>= 20 $& "age" $<= 30
288 | , "name" $<- [String "Pau Gasol", String "Marc Gasol"]
289 | ]
290 | ```
291 |
292 | which will match any users with ages between 20 and 30 or whose names are "Pau Gasol"
293 | or "Marc Gasol".
294 |
295 |
296 | ### Free monad DSL
297 |
298 | You can also use the free monad DSL from `Sequelize.Free`, which is parameterized
299 | by input and output types. It comes with a default interpreter:
300 |
301 | ```purescript
302 | runCRUD :: _ => ModelOf a -> CRUD a b ~> Aff (sequelize :: SEQUELIZE | e)
303 | ```
304 |
305 | as well as all the usual CRUD operations. You could use this for e.g. mocking,
306 | in which case you will need to define your own interpreter and switch it out
307 | in the appropriate place.
308 |
309 | ## Installing
310 |
311 | ```
312 | $ npm install --save config
313 | $ bower install --save purescript-sequelize
314 | ```
315 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "purescript-sequelize",
3 | "ignore": [
4 | "**/.*",
5 | "node_modules",
6 | "bower_components",
7 | "output"
8 | ],
9 | "dependencies": {
10 | "purescript-prelude": "^3.1.0",
11 | "purescript-console": "^3.0.0",
12 | "purescript-aff": "^3.1.0",
13 | "purescript-exceptions": "^3.0.0",
14 | "purescript-aff-promise": "^0.7.0",
15 | "purescript-options": "^3.0.0",
16 | "purescript-generics-rep": "^5.2.0",
17 | "purescript-foreign-generic": "^5.0.0",
18 | "purescript-free": "^4.1.0"
19 | },
20 | "devDependencies": {
21 | "purescript-psci-support": "^3.0.0",
22 | "purescript-debug": "^3.0.0"
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "purescript-sequelize",
3 | "version": "0.1.0",
4 | "description": "bindings for sequelize",
5 | "main": "index.js",
6 | "directories": {
7 | "test": "test"
8 | },
9 | "dependencies": {
10 | "sequelize": "^4.38.0",
11 | "sequelize-cli": "^5.4.0"
12 | },
13 | "devDependencies": {
14 | "pulp": "^12.3.1",
15 | "purescript": "^0.11.5",
16 | "sqlite3": "^4.0.6"
17 | },
18 | "scripts": {
19 | "test": "pulp test",
20 | "dev": "pulp --watch run",
21 | "start": "node ./app.js",
22 | "test-connection": "pulp test --main Test.Connection",
23 | "test-insertion": "pulp test --main Test.Insertion",
24 | "test-query": "pulp test --main Test.Query",
25 | "test-associations": "pulp test --main Test.Association",
26 | "test-where": "pulp test --main Test.Where",
27 | "test-free": "pulp test --main Test.Free",
28 | "test-failing": "pulp test --main Test.Failing"
29 | },
30 | "license": "ISC"
31 | }
32 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.CRUD (module Exports) where
27 |
28 | import Sequelize.CRUD.Create (build, bulkCreate, create, save) as Exports
29 | import Sequelize.CRUD.Read (count, findAll, findAll', findAndCountAll, findAndCountAll', findById, findByIdWithError, findByIntId, findByIntIdWithError, findByStringId, findByStringIdWithError, findOne, findOneWithError, findOrBuild, findOrCreate, findOrCreateWithError, max, min) as Exports
30 | import Sequelize.CRUD.Update (decrement, increment, update, updateModel) as Exports
31 | import Sequelize.CRUD.Destroy (destroy) as Exports
32 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Create.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports._build = function (model, defs) {
27 | return model.build(defs);
28 | };
29 |
30 | exports._save = function (model) {
31 | return model.save();
32 | };
33 |
34 | exports._create = function (model, defs, opts) {
35 | return model.create(defs, opts);
36 | };
37 |
38 | exports._bulkCreate = function (model, arr, opts) {
39 | return model.bulkCreate(arr, opts);
40 | };
41 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Create.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.CRUD.Create
27 | ( build
28 | , save
29 | , create
30 | , create'
31 | , bulkCreate
32 | , createWithOpts
33 | , createWithOpts'
34 | , bulkCreateWithOpts
35 | ) where
36 |
37 | import Prelude
38 |
39 | import Control.Monad.Aff (Aff)
40 | import Control.Promise (Promise, toAff)
41 | import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
42 | import Data.Options (Options, options)
43 | import Sequelize.Class (class Model, class Submodel, encodeModel, class EncodeModel)
44 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
45 |
46 | foreign import _build
47 | :: forall a b c.
48 | Fn2
49 | (ModelOf a)
50 | b
51 | (Instance c)
52 |
53 | build
54 | :: forall a b
55 | . Submodel a b
56 | => ModelOf a
57 | -> b
58 | -> Instance b
59 | build m t = runFn2 _build m $ encodeModel t
60 |
61 | foreign import _save
62 | :: forall a. Instance a -> Promise (Instance a)
63 |
64 | save
65 | :: forall a e
66 | . Model a
67 | => Instance a
68 | -> Aff ( sequelize :: SEQUELIZE | e ) (Instance a)
69 | save m = toAff $ _save m
70 |
71 | foreign import _create
72 | :: forall a b c d.
73 | Fn3
74 | (ModelOf a)
75 | b
76 | c
77 | (Promise (Instance d))
78 |
79 | create
80 | :: forall a b e
81 | . Submodel a b
82 | => ModelOf a
83 | -> b
84 | -> Aff ( sequelize :: SEQUELIZE | e ) (Instance b)
85 | create m t = toAff $ runFn3 _create m (encodeModel t) {}
86 |
87 | create'
88 | :: forall a e.
89 | EncodeModel a
90 | => ModelOf a
91 | -> a
92 | -> Aff ( sequelize :: SEQUELIZE | e ) (Instance a)
93 | create' m t = toAff $ runFn3 _create m (encodeModel t) {}
94 |
95 | foreign import _bulkCreate
96 | :: forall a b c d.
97 | Fn3
98 | (ModelOf a)
99 | (Array b)
100 | c
101 | (Promise (Array (Instance d)))
102 |
103 | bulkCreate
104 | :: forall a b e. Submodel a b
105 | => ModelOf a
106 | -> Array b
107 | -> Aff ( sequelize :: SEQUELIZE | e ) (Array (Instance b))
108 | bulkCreate m arr = toAff $ runFn3 _bulkCreate m (map encodeModel arr) {}
109 |
110 | createWithOpts
111 | :: forall a b c e
112 | . Submodel a b
113 | => ModelOf a
114 | -> b
115 | -> Options c
116 | -> Aff ( sequelize :: SEQUELIZE | e ) (Instance b)
117 | createWithOpts m t opts = toAff $ runFn3 _create m (encodeModel t) (options opts)
118 |
119 | createWithOpts'
120 | :: forall a c e.
121 | EncodeModel a
122 | => ModelOf a
123 | -> a
124 | -> Options c
125 | -> Aff ( sequelize :: SEQUELIZE | e ) (Instance a)
126 | createWithOpts' m t opts = toAff $ runFn3 _create m (encodeModel t) (options opts)
127 |
128 | bulkCreateWithOpts
129 | :: forall a b c e. Submodel a b
130 | => ModelOf a
131 | -> Array b
132 | -> Options c
133 | -> Aff ( sequelize :: SEQUELIZE | e ) (Array (Instance b))
134 | bulkCreateWithOpts m arr opts = toAff $ runFn3 _bulkCreate m (map encodeModel arr) (options opts)
135 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Destroy.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports._destroy = function (instance) {
27 | return function () {
28 | return instance.destroy();
29 | };
30 | };
31 |
32 | exports._delete = function (model,opts) {
33 | return model.destroy(opts);
34 | };
35 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Destroy.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.CRUD.Destroy (destroy, delete) where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Control.Monad.Eff (Eff)
32 | import Control.Monad.Eff.Class (liftEff)
33 | import Control.Promise (Promise, toAff)
34 | import Data.Array ((!!))
35 | import Data.Foreign (Foreign, isUndefined)
36 | import Data.Function.Uncurried (Fn2, Fn3, runFn2)
37 | import Data.Maybe (Maybe(..), maybe)
38 | import Data.Options (Options(..), options)
39 | import Sequelize.Class (class Model)
40 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
41 | import Unsafe.Coerce (unsafeCoerce)
42 |
43 | foreign import _destroy
44 | :: forall a e. Instance a -> Eff ( sequelize :: SEQUELIZE | e ) Unit
45 |
46 | foreign import _delete
47 | :: forall a b . Fn2 (ModelOf a) b (Promise Foreign)
48 |
49 |
50 | destroy
51 | :: forall a e. Model a
52 | => Instance a
53 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
54 | destroy = liftEff <<< _destroy
55 |
56 |
57 | delete
58 | :: forall a e. Model a
59 | => ModelOf a
60 | -> Options a
61 | -> Aff
62 | ( sequelize :: SEQUELIZE | e )
63 | { affectedCount :: Int }
64 | delete m o = do
65 | arrForeign <- toAff $ runFn2 _delete m (options o)
66 | let affectedCount = (unsafeCoerce arrForeign)
67 | pure { affectedCount }
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Read.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports._findById = function (model, id) {
27 | return model.findById(id);
28 | };
29 |
30 | exports._findOne = function (model, opts) {
31 | return model.findOne(opts);
32 | };
33 |
34 | exports._findOrBuild = function (model, opts) {
35 | return model.findOrBuild(opts);
36 | };
37 |
38 | exports._findOrCreate = function (model, opts) {
39 | return model.findOrCreate(opts);
40 | };
41 |
42 | exports._findAndCountAll = function (model, opts) {
43 | return model.findAndCountAll(opts);
44 | };
45 |
46 | exports._findAll = function (model, opts) {
47 | return model.findAll(opts);
48 | };
49 |
50 | exports._count = function (model, opts) {
51 | return model.count(opts);
52 | };
53 |
54 | exports._max = function (model, opts, field) {
55 | return model.max(field, opts);
56 | };
57 |
58 | exports._min = function (model, opts, field) {
59 | return model.min(field, opts);
60 | };
61 |
62 | exports._query = function(seq) {
63 | return function(rawquery) {
64 | return function() {
65 | return seq.query(rawquery, {
66 | type: seq.QueryTypes.SELECT
67 | });
68 | }
69 | }
70 | };
71 |
72 | exports["_query'"] = function(seq) {
73 | return function(rawquery) {
74 | return function(rep) {
75 | return function(opts){
76 | return function(){
77 | opts.replacements = rep;
78 | opts.type = seq.QueryTypes.SELECT
79 | return seq.query(rawquery, opts);
80 | }
81 | }
82 | }
83 | }
84 | };
85 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Read.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.CRUD.Read
27 | ( findById
28 | , findByIdWithError
29 | , findByStringId
30 | , findByStringIdWithError
31 | , findByIntId
32 | , findByIntIdWithError
33 | , findOne
34 | , findOneWithError
35 | , findOrBuild
36 | , findOrCreate
37 | , findOrCreateWithError
38 | , findAndCountAll
39 | , findAndCountAll'
40 | , findAll
41 | , findOne'
42 | , findAll'
43 | , query'
44 | , query''
45 | , count
46 | , max
47 | , min
48 | ) where
49 |
50 | import Prelude
51 |
52 | import Control.Monad.Aff (Aff)
53 | import Control.Monad.Eff (Eff)
54 | import Control.Monad.Eff.Class (liftEff)
55 | import Control.Monad.Eff.Exception (error)
56 | import Control.Monad.Error.Class (throwError)
57 | import Control.Promise (Promise, toAff)
58 | import Data.Array as Array
59 | import Data.Either (Either(..), either, hush)
60 | import Data.Foreign (Foreign, toForeign, isNull)
61 | import Data.Function.Uncurried (Fn2, Fn3, runFn2)
62 | import Data.Maybe (Maybe(..))
63 | import Data.Options (Options)
64 | import Data.StrMap as StrMap
65 | import Sequelize.Class (class Model, class Submodel)
66 | import Sequelize.Instance (instanceToModelE)
67 | import Sequelize.Query.Util (coerceArrayTuple, promiseToAff2, promiseToAff3)
68 | import Sequelize.Types (Conn, Instance, ModelOf, SEQUELIZE)
69 |
70 | foreign import _findById
71 | :: forall a b c.
72 | Fn2
73 | (ModelOf a)
74 | c
75 | (Promise (Instance b))
76 |
77 | findById
78 | :: forall a b e. Submodel a b
79 | => ModelOf a
80 | -> Either String Int
81 | -> Aff ( sequelize :: SEQUELIZE | e ) (Maybe (Instance b))
82 | findById model ident = do
83 | maybeInst <-
84 | toAff $ runFn2 _findById model (either toForeign toForeign ident)
85 | pure if isNull (toForeign maybeInst)
86 | then Nothing
87 | else Just maybeInst
88 |
89 | findByIdWithError
90 | :: forall a b e
91 | . Submodel a b
92 | => ModelOf a
93 | -> Either String Int
94 | -> String
95 | -> Aff ( sequelize :: SEQUELIZE | e ) b
96 | findByIdWithError m i msg = collapseErrors findById m i msg
97 |
98 | findByStringId
99 | :: forall a b e. Submodel a b
100 | => ModelOf a
101 | -> String
102 | -> Aff ( sequelize :: SEQUELIZE | e ) (Maybe (Instance b))
103 | findByStringId model = findById model <<< Left
104 |
105 | findByStringIdWithError
106 | :: forall a b e
107 | . Submodel a b
108 | => ModelOf a
109 | -> String
110 | -> String
111 | -> Aff ( sequelize :: SEQUELIZE | e ) b
112 | findByStringIdWithError m i msg = findByIdWithError m (Left i) msg
113 |
114 | findByIntId
115 | :: forall a b e. Submodel a b
116 | => ModelOf a
117 | -> Int
118 | -> Aff ( sequelize :: SEQUELIZE | e ) (Maybe (Instance b))
119 | findByIntId model = findById model <<< Right
120 |
121 | findByIntIdWithError
122 | :: forall a b e
123 | . Submodel a b
124 | => ModelOf a
125 | -> Int
126 | -> String
127 | -> Aff ( sequelize :: SEQUELIZE | e ) b
128 | findByIntIdWithError m i msg = findByIdWithError m (Right i) msg
129 |
130 | foreign import _findOne
131 | :: forall a b c.
132 | Fn2
133 | a
134 | c
135 | (Promise (Instance b))
136 |
137 | findOne
138 | :: forall a b e. Submodel a b
139 | => ModelOf a
140 | -> Options b
141 | -> Aff ( sequelize :: SEQUELIZE | e ) (Maybe (Instance b))
142 | findOne m o = do
143 | maybeInst <- promiseToAff2 _findOne m o
144 | pure if isNull (toForeign maybeInst)
145 | then Nothing
146 | else Just maybeInst
147 |
148 | findOne'
149 | :: forall a b e.
150 | ModelOf a
151 | -> Options b
152 | -> Aff ( sequelize :: SEQUELIZE | e ) (Maybe (Instance b))
153 | findOne' m o = do
154 | maybeInst <- promiseToAff2 _findOne m o
155 | pure if isNull (toForeign maybeInst)
156 | then Nothing
157 | else Just maybeInst
158 |
159 |
160 | findOneWithError
161 | :: forall a b e
162 | . Submodel a b
163 | => ModelOf a
164 | -> Options b
165 | -> String
166 | -> Aff ( sequelize :: SEQUELIZE | e ) b
167 | findOneWithError m o msg = collapseErrors findOne m o msg
168 |
169 | foreign import _findOrBuild
170 | :: forall a b.
171 | Fn2
172 | (ModelOf a)
173 | b
174 | (Promise (Array Foreign))
175 |
176 | -- | If the query fails, a new row will be built (but not saved). The Boolean
177 | -- | refers to whether the row was built (true) or not (false -- therefore the
178 | -- | query succeeded).
179 | findOrBuild
180 | :: forall a b e. Submodel a b
181 | => ModelOf a
182 | -> Options b
183 | -> Aff (sequelize :: SEQUELIZE | e)
184 | { inst :: Instance b
185 | , created :: Boolean
186 | }
187 | findOrBuild m o = promiseToAff2 _findOrBuild m o >>= coerceArrayTuple
188 |
189 | foreign import _findOrCreate
190 | :: forall a b.
191 | Fn2
192 | (ModelOf a)
193 | b
194 | (Promise (Array Foreign))
195 |
196 | -- | Same as findOrBuild, but saves in case of row creation.
197 | findOrCreate
198 | :: forall a b e. Submodel a b
199 | => ModelOf a
200 | -> Options b
201 | -> Aff (sequelize :: SEQUELIZE | e)
202 | { inst :: Instance b
203 | , created :: Boolean
204 | }
205 | findOrCreate m o = promiseToAff2 _findOrCreate m o >>= coerceArrayTuple
206 |
207 | findOrCreateWithError
208 | :: forall a b e
209 | . Submodel a b
210 | => ModelOf a
211 | -> Options b
212 | -> String
213 | -> Aff (sequelize :: SEQUELIZE | e)
214 | { model :: b
215 | , created :: Boolean
216 | }
217 | findOrCreateWithError m o msg = do
218 | {inst, created} <- findOrCreate m o
219 | case instanceToModelE inst of
220 | Left _ -> throwError $ error msg
221 | Right model -> pure {model, created}
222 |
223 | foreign import _findAndCountAll
224 | :: forall a b c.
225 | Fn2
226 | (ModelOf a)
227 | c
228 | (Promise {count :: Int, rows :: Array (Instance b)})
229 |
230 | findAndCountAll
231 | :: forall a b e. Submodel a b
232 | => ModelOf a
233 | -> Options b
234 | -> Aff ( sequelize :: SEQUELIZE | e ) {count :: Int, rows :: Array (Instance b)}
235 | findAndCountAll = promiseToAff2 _findAndCountAll
236 |
237 | findAndCountAll'
238 | :: forall a b e
239 | . Submodel a b
240 | => ModelOf a
241 | -> Options b
242 | -> Aff ( sequelize :: SEQUELIZE | e ) {count :: Int, rows :: Array b}
243 | findAndCountAll' m o = do
244 | all <- findAndCountAll m o
245 | pure {count: all.count, rows: catMaybes all.rows}
246 |
247 | foreign import _findAll
248 | :: forall a b c.
249 | Fn2
250 | (ModelOf a)
251 | c
252 | (Promise (Array (Instance b)))
253 |
254 | findAll
255 | :: forall a b e. Submodel a b
256 | => ModelOf a
257 | -> Options b
258 | -> Aff ( sequelize :: SEQUELIZE | e ) (Array (Instance b))
259 | findAll = promiseToAff2 _findAll
260 |
261 | findAll'
262 | :: forall a b e
263 | . Submodel a b
264 | => ModelOf a
265 | -> Options b
266 | -> Aff ( sequelize :: SEQUELIZE | e ) (Array b)
267 | findAll' m o = catMaybes <$> findAll m o
268 |
269 | foreign import _query :: forall a b e. Conn -> String -> (Eff (sequelize :: SEQUELIZE | e) (Promise b))
270 |
271 | query' :: forall a b e. Conn -> String -> (Aff (sequelize :: SEQUELIZE | e) (Array a))
272 | query' c q = do
273 | res <- liftEff $ _query c q
274 | toAff res
275 |
276 | foreign import _query' :: forall a b e. Conn -> String -> StrMap.StrMap Foreign -> StrMap.StrMap Foreign -> (Eff (sequelize :: SEQUELIZE | e) (Promise b))
277 |
278 | query'' :: forall a b e. Conn -> String -> StrMap.StrMap Foreign -> StrMap.StrMap Foreign -> (Aff (sequelize :: SEQUELIZE | e) (Array a))
279 | query'' c q rep opts = do
280 | res <- liftEff $ _query' c q rep opts
281 | toAff res
282 |
283 | foreign import _count
284 | :: forall a b.
285 | Fn2
286 | (ModelOf a)
287 | b
288 | (Promise Int)
289 |
290 | count
291 | :: forall a e. Model a
292 | => ModelOf a
293 | -> Options a
294 | -> Aff ( sequelize :: SEQUELIZE | e ) Int
295 | count = promiseToAff2 _count
296 |
297 | foreign import _max
298 | :: forall a b.
299 | Fn3
300 | (ModelOf a)
301 | b
302 | String
303 | (Promise Int)
304 |
305 | max
306 | :: forall a e. Model a
307 | => ModelOf a
308 | -> Options a
309 | -> String
310 | -> Aff ( sequelize :: SEQUELIZE | e ) Int
311 | max m o f = promiseToAff3 _max m o f
312 |
313 | foreign import _min
314 | :: forall a b.
315 | Fn3
316 | (ModelOf a)
317 | b
318 | String
319 | (Promise Int)
320 |
321 | min
322 | :: forall a e. Model a
323 | => ModelOf a
324 | -> Options a
325 | -> String
326 | -> Aff ( sequelize :: SEQUELIZE | e ) Int
327 | min m o f = promiseToAff3 _min m o f
328 |
329 | collapseErrors
330 | :: forall a b m eff
331 | . Model m
332 | => (a -> b -> Aff eff (Maybe (Instance m)))
333 | -> a
334 | -> b
335 | -> String
336 | -> Aff eff m
337 | collapseErrors find a b msg = do
338 | maybei <- map instanceToModelE <$> find a b
339 | case maybei of
340 | Just (Right x) -> pure x
341 | _ -> throwError $ error msg
342 |
343 | catMaybes :: forall m. Model m => Array (Instance m) -> Array m
344 | catMaybes ms = Array.catMaybes (hush <<< instanceToModelE <$> ms)
345 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Unsafe.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.CRUD.Unsafe where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Data.Maybe (Maybe)
32 | import Data.Options (Options)
33 | import Sequelize.CRUD.Update (update, updateModel)
34 | import Sequelize.Class (class Model)
35 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
36 | import Unsafe.Coerce (unsafeCoerce)
37 |
38 | unsafeUpdateModel
39 | :: forall a b e
40 | . Model a
41 | => ModelOf a
42 | -> b
43 | -> Options a
44 | -> Aff
45 | ( sequelize :: SEQUELIZE | e )
46 | { affectedCount :: Int, affectedRows :: Maybe (Array (Instance a)) }
47 | unsafeUpdateModel m b o = updateModel m (unsafeCoerce b) o
48 |
49 | unsafeUpdate
50 | :: forall a b e
51 | . Model a
52 | => Instance a
53 | -> b
54 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
55 | unsafeUpdate inst t = update inst (unsafeCoerce t)
56 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Update.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports._update = function (instance, defs) {
27 | return instance.update(defs);
28 | };
29 |
30 | exports._updateModel = function (modelOf, newVals, opts) {
31 | return modelOf.update(newVals, opts);
32 | };
33 |
34 | exports._increment = function (instance, strmap) {
35 | return instance.increment(strmap);
36 | };
37 |
38 | exports._decrement = function (instance, strmap) {
39 | return instance.decrement(strmap);
40 | };
41 |
--------------------------------------------------------------------------------
/src/Sequelize/CRUD/Update.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.CRUD.Update
27 | ( update
28 | , updateModel
29 | , increment
30 | , decrement
31 | ) where
32 |
33 | import Prelude
34 |
35 | import Control.Monad.Aff (Aff)
36 | import Control.Promise (Promise, toAff)
37 | import Data.Array ((!!))
38 | import Data.Foreign (Foreign, isUndefined)
39 | import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
40 | import Data.Maybe (Maybe(Nothing, Just), maybe)
41 | import Data.Options (Options, opt, options, (:=))
42 | import Data.StrMap (StrMap)
43 | import Sequelize.CRUD.Create (bulkCreateWithOpts)
44 | import Sequelize.Class (class Model, class Submodel, encodeModel)
45 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
46 | import Unsafe.Coerce (unsafeCoerce)
47 |
48 | foreign import _update
49 | :: forall a b.
50 | Fn2
51 | (Instance a)
52 | b
53 | (Promise Unit)
54 |
55 | update
56 | :: forall a e. Model a
57 | => Instance a
58 | -> a
59 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
60 | update inst t = toAff $ runFn2 _update inst $ encodeModel t
61 |
62 | foreign import _updateModel
63 | :: forall a b c.
64 | Fn3
65 | (ModelOf a)
66 | b
67 | c
68 | (Promise (Array Foreign))
69 |
70 | -- | NOTE: The options require a "where", or else an error will be thrown.
71 | updateModel
72 | :: forall a e. Model a
73 | => ModelOf a
74 | -> Options a
75 | -> Options a
76 | -> Aff
77 | ( sequelize :: SEQUELIZE | e )
78 | { affectedCount :: Int, affectedRows :: Maybe (Array (Instance a)) }
79 | updateModel m a o = do
80 | arrForeign <- toAff $ updateM m (options a) (options o)
81 | let affectedCount = maybe 0 unsafeCoerce $ arrForeign !! 0
82 | affectedRows = maybe Nothing handleUndefined $ arrForeign !! 1
83 | pure {affectedCount, affectedRows}
84 | where
85 | updateM = runFn3 _updateModel
86 | handleUndefined x = if isUndefined x then Nothing else Just (unsafeCoerce x)
87 | -- TODO: check if x really has a runtime representation of Array (Instance a)
88 |
89 | bulkUpdate
90 | :: forall a e. Model a
91 | => Submodel a Foreign
92 | => ModelOf a
93 | -> Array (Options a)
94 | -> Options a
95 | -> Aff
96 | ( sequelize :: SEQUELIZE | e )
97 | (Array (Instance Foreign))
98 | bulkUpdate m a o = do
99 | let b = map options a
100 | bulkCreateWithOpts m b o
101 |
102 | where
103 | updateDuplicates :: Options a
104 | updateDuplicates = opt "updateDuplicates" := true
105 |
106 | foreign import _increment
107 | :: forall a.
108 | Fn2
109 | (Instance a)
110 | (StrMap Int)
111 | (Promise Unit)
112 |
113 | increment
114 | :: forall a e. Model a
115 | => Instance a
116 | -> StrMap Int
117 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
118 | increment i m = toAff $ runFn2 _increment i m
119 |
120 | foreign import _decrement
121 | :: forall a.
122 | Fn2
123 | (Instance a)
124 | (StrMap Int)
125 | (Promise Unit)
126 |
127 | decrement
128 | :: forall a e. Model a
129 | => Instance a
130 | -> StrMap Int
131 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
132 | decrement i m = toAff $ runFn2 _decrement i m
133 |
--------------------------------------------------------------------------------
/src/Sequelize/Class.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Class where
27 |
28 | import Prelude
29 |
30 | import Data.Foreign (Foreign)
31 | import Data.Foreign as F
32 | import Data.Foreign.Class (class Decode, class Encode)
33 | import Data.Foreign.Generic (defaultOptions, genericDecode, genericEncode)
34 | import Data.Foreign.Generic.Class (class GenericDecode, class GenericEncode)
35 | import Data.Foreign.JSON (decodeJSONWith)
36 | import Data.Generic.Rep (class Generic)
37 | import Global.Unsafe (unsafeStringify)
38 | import Sequelize.Types (WhereClause, ModelCols)
39 |
40 | -- | Laws: `toWhere a` creates a well-formed "where" object
41 | class IsWhere f where
42 | toWhere :: forall a. f a -> WhereClause
43 |
44 | -- | This class represents types that can be decoded from Sequelize queries
45 | class Decode m <= DecodeModel m where
46 | decodeModel :: Foreign -> F.F m
47 |
48 | -- | This class represents types that can be encoded for Sequelize inserts
49 | class Encode m <= EncodeModel m where
50 | encodeModel :: m -> Foreign
51 |
52 | -- | This class represents a type that corresponds to a database table;
53 | -- | values of type m represent database rows.
54 | class (EncodeModel m, DecodeModel m) <= Model m where
55 | modelCols :: ModelCols m
56 | modelName :: forall proxy. proxy m -> String
57 |
58 | -- | This relation represents two models, one of which is a submodel of the other.
59 | -- | This is useful for when you want Sequelize to take care of, for example,
60 | -- | the "id", "createdAt" and "updatedAt" columns for insertion, but you want
61 | -- | access to them in your supermodel (hehe, supermodel).
62 | class (Model m, Model n) <= Submodel m n where
63 | project :: n -> m
64 |
65 | instance idSubmodel :: Model m => Submodel m m where
66 | project = id
67 |
68 | -- | Not provided as an instance because it's too easy for the compiler to
69 | -- | complain about infinite loops
70 | projectTransitive
71 | :: forall a b c
72 | . Model a
73 | => Model b
74 | => Model c
75 | => Submodel a b
76 | => Submodel b c
77 | => c -> a
78 | projectTransitive = (project :: b -> a) <<< (project :: c -> b)
79 |
80 | -- | NOTE: this definition uses `unwrapSingleConstructors = true`. In general,
81 | -- | you should make sure your Encode and Decode instances are sane, even if you
82 | -- | are deriving them generically. If you use this, make sure to eta-expand in
83 | -- | the instance to avoid this issue:
84 | -- | https://github.com/purescript/purescript/issues/2975#issuecomment-313650710
85 | genericEncodeModel
86 | :: forall t rep. Generic t rep
87 | => GenericEncode rep
88 | => t
89 | -> Foreign
90 | genericEncodeModel m =
91 | genericEncode (defaultOptions {unwrapSingleConstructors = true}) m
92 |
93 | -- | NOTE: this definition uses `unwrapSingleConstructors = true`. In general,
94 | -- | you should make sure your Encode and Decode instances are sane, even if you
95 | -- | are deriving them generically. If you use this, make sure to eta-expand in
96 | -- | the instance to avoid this issue:
97 | -- | https://github.com/purescript/purescript/issues/2975#issuecomment-313650710
98 | genericDecodeModel
99 | :: forall t rep. Generic t rep
100 | => GenericDecode rep
101 | => Foreign
102 | -> F.F t
103 | genericDecodeModel o =
104 | genericDecode (defaultOptions {unwrapSingleConstructors = true}) o
105 |
106 | encodeModelJSON :: forall m. EncodeModel m => m -> String
107 | encodeModelJSON = unsafeStringify <<< encodeModel
108 |
109 | decodeModelJSON :: forall m. DecodeModel m => String -> F.F m
110 | decodeModelJSON = decodeJSONWith decodeModel
111 |
--------------------------------------------------------------------------------
/src/Sequelize/Connection.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | const Sequelize = require('sequelize');
27 | const Promise = require('bluebird');
28 | const clsBluebird = require('cls-bluebird');
29 | const cls = require('cls-hooked');
30 |
31 | var ns = cls.getNamespace('zipkin') || cls.createNamespace('zipkin');
32 | clsBluebird(ns, Promise);
33 | Sequelize.useCLS(ns);
34 |
35 | exports._newSequelize = function (options) {
36 | return function () {
37 | return new Sequelize(options);
38 | };
39 | };
40 |
41 | exports._syncSequelize = function (isJust, fromJust, sequelize, opts) {
42 | var newOpts = {};
43 | if (isJust(opts["match"])) {
44 | newOpts["match"] = fromJust(opts["match"])();
45 | }
46 | if (opts["logging"] === true) {
47 | newOpts["logging"] = console.log;
48 | } else {
49 | newOpts["logging"] = opts["logging"];
50 | }
51 | newOpts["force"] = opts["force"];
52 | newOpts["schema"] = opts["schema"];
53 | newOpts["searchPath"] = opts["searchPath"];
54 | newOpts["hooks"] = opts["hooks"];
55 | newOpts["alter"] = opts["alter"];
56 | return sequelize.sync(newOpts);
57 | };
58 |
59 | exports._authenticate = function (sequelize) {
60 | return sequelize.authenticate();
61 | };
62 |
63 | exports.literal = Sequelize.literal;
64 |
--------------------------------------------------------------------------------
/src/Sequelize/Connection.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Connection
27 | ( getConn
28 | , syncConn
29 | , authenticate
30 | , _newSequelize
31 | , literal
32 | , username
33 | , password
34 | , database
35 | , host
36 | , port
37 | , Dialect(..)
38 | , dialect
39 | , dialectOptions
40 | , dialectModulePath
41 | , schema
42 | , storage
43 | , protocol
44 | , timezone
45 | , benchmark
46 | , omitNull
47 | , native
48 | , pool
49 | , replication
50 | , quoteIdentifiers
51 | , transactionType
52 | , isolationLevel
53 | , retry
54 | , typeValidation
55 | ) where
56 |
57 | import Prelude
58 |
59 | import Control.Monad.Aff (Aff)
60 | import Control.Monad.Eff (Eff)
61 | import Control.Monad.Eff.Class (liftEff)
62 | import Control.Promise (Promise, toAff)
63 | import Data.Foreign (Foreign)
64 | import Data.Function.Uncurried (Fn4, runFn4)
65 | import Data.Functor.Contravariant ((>$<))
66 | import Data.Maybe (Maybe, fromJust, isJust)
67 | import Data.Options (Option, Options, opt, options)
68 | import Data.StrMap (StrMap)
69 | import Sequelize.Types (Conn, ConnOpts, SEQUELIZE, SyncOpts, ReplicationOpts)
70 |
71 | foreign import _newSequelize
72 | :: forall e. Foreign -> Eff ( sequelize :: SEQUELIZE | e ) Conn
73 |
74 | getConn
75 | :: forall e. Options ConnOpts -> Aff ( sequelize :: SEQUELIZE | e ) Conn
76 | getConn = liftEff <<< _newSequelize <<< options
77 |
78 | foreign import _syncSequelize
79 | :: forall a.
80 | Fn4
81 | (Maybe a -> Boolean)
82 | (Partial => Maybe a -> a)
83 | Conn
84 | SyncOpts
85 | (Promise Unit)
86 |
87 | syncConn
88 | :: forall e
89 | . Conn
90 | -> SyncOpts
91 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
92 | syncConn conn opts = toAff $ runFn4 _syncSequelize isJust fromJust conn opts
93 |
94 | foreign import _authenticate
95 | :: Conn -> Promise Unit
96 |
97 | authenticate :: forall e. Conn -> Aff ( sequelize :: SEQUELIZE | e ) Unit
98 | authenticate = toAff <<< _authenticate
99 |
100 | foreign import literal :: String -> Foreign
101 |
102 | username :: Option ConnOpts String
103 | username = opt "username"
104 |
105 | password :: Option ConnOpts String
106 | password = opt "password"
107 |
108 | database :: Option ConnOpts String
109 | database = opt "database"
110 |
111 | host :: Option ConnOpts String
112 | host = opt "host"
113 |
114 | port :: Option ConnOpts Int
115 | port = opt "port"
116 |
117 | schema :: Option ConnOpts String
118 | schema = opt "schema"
119 |
120 | data Dialect
121 | = MySQL
122 | | PostgreSQL
123 | | SQLite
124 | | MSSQL
125 |
126 | stringifyDialect :: Dialect -> String
127 | stringifyDialect PostgreSQL = "postgres"
128 | stringifyDialect SQLite = "sqlite"
129 | stringifyDialect MySQL = "mysql"
130 | stringifyDialect MSSQL = "mssql"
131 |
132 | dialect :: Option ConnOpts Dialect
133 | dialect = stringifyDialect >$< opt "dialect"
134 |
135 | dialectModulePath :: Option ConnOpts String
136 | dialectModulePath = opt "dialectModulePath"
137 |
138 | -- | It's up to you to make sure the Foreign keys will make sense!
139 | dialectOptions :: Option ConnOpts (StrMap Foreign)
140 | dialectOptions = opt "dialectOptions"
141 |
142 | storage :: Option ConnOpts String
143 | storage = opt "storage"
144 |
145 | protocol :: Option ConnOpts String
146 | protocol = opt "protocol"
147 |
148 | timezone :: Option ConnOpts String
149 | timezone = opt "timezone"
150 |
151 | benchmark :: Option ConnOpts Boolean
152 | benchmark = opt "benchmark"
153 |
154 | omitNull :: Option ConnOpts Boolean
155 | omitNull = opt "omitNull"
156 |
157 | native :: Option ConnOpts Boolean
158 | native = opt "native"
159 |
160 | pool :: Option ConnOpts { max :: Int, min :: Int, idle :: Int, acquire :: Int }
161 | pool = opt "pool"
162 |
163 | quoteIdentifiers :: Option ConnOpts Boolean
164 | quoteIdentifiers = opt "quoteIdentifiers"
165 |
166 | transactionType :: Option ConnOpts String
167 | transactionType = opt "transactionType"
168 |
169 | isolationLevel :: Option ConnOpts String
170 | isolationLevel = opt "isolationLevel"
171 |
172 | retry :: Option ConnOpts { match :: Array String, max :: Int }
173 | retry = opt "retry"
174 |
175 | typeValidation :: Option ConnOpts Boolean
176 | typeValidation = opt "typeValidation"
177 |
178 | replication :: Option ConnOpts ReplicationOpts
179 | replication = opt "replication"
180 |
181 | logging :: Option ConnOpts Foreign
182 | logging = opt "logging"
183 |
--------------------------------------------------------------------------------
/src/Sequelize/Free.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Free
27 | ( CRUDF
28 | , CRUD
29 | , runCRUD
30 | , build
31 | , save
32 | , create
33 | , bulkCreate
34 | , findById
35 | , findByString
36 | , findByInt
37 | , findOne
38 | , findOrBuild
39 | , findOrCreate
40 | , findAndCountAll
41 | , findAll
42 | , count
43 | , max
44 | , min
45 | , update
46 | , increment
47 | , decrement
48 | , updateModel
49 | , destroy
50 | ) where
51 |
52 | import Prelude
53 |
54 | import Control.Monad.Aff (Aff)
55 | import Control.Monad.Free (Free, foldFree, liftF)
56 | import Data.Either (Either)
57 | import Data.Functor.Coproduct (left, right)
58 | import Data.Functor.Coproduct.Nested (Coproduct4, coproduct4, in1, in2, in3, in4)
59 | import Data.Maybe (Maybe)
60 | import Data.Options (Options)
61 | import Data.StrMap (StrMap)
62 | import Sequelize.Class (class Model, class Submodel)
63 | import Sequelize.Free.Create as C
64 | import Sequelize.Free.Destroy as D
65 | import Sequelize.Free.Read as R
66 | import Sequelize.Free.Update as U
67 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
68 |
69 | type CRUDF a b
70 | = Coproduct4 (C.CreateF b) (R.ReadF a b) (U.UpdateF a) (D.DestroyF a)
71 |
72 | interpretCRUDF
73 | :: forall a b e
74 | . Submodel a b
75 | => Model a
76 | => ModelOf a
77 | -> CRUDF a b
78 | ~> Aff (sequelize :: SEQUELIZE | e)
79 | interpretCRUDF m = coproduct4
80 | (C.interpretCreate m)
81 | (R.interpretRead m)
82 | (U.interpretUpdate m)
83 | D.interpretDestroy
84 |
85 | type CRUD a b = Free (CRUDF a b)
86 |
87 | inCreate :: forall a b. C.CreateF b ~> CRUD a b
88 | inCreate = liftF <<< in1
89 |
90 | inRead :: forall a b. R.ReadF a b ~> CRUD a b
91 | inRead = liftF <<< in2
92 |
93 | inFind :: forall a b. R.FindF b ~> R.ReadF a b
94 | inFind = left
95 |
96 | inCount :: forall a b. R.CountF a ~> R.ReadF a b
97 | inCount = right
98 |
99 | inUpdate :: forall a b. U.UpdateF a ~> CRUD a b
100 | inUpdate = liftF <<< in3
101 |
102 | inDestroy :: forall a b. D.DestroyF a ~> CRUD a b
103 | inDestroy = liftF <<< in4
104 |
105 | -- injections for module C into the CRUD monad
106 |
107 | build :: forall a b. b -> CRUD a b (Instance b)
108 | build = inCreate <<< C.buildF
109 |
110 | save :: forall a b. Instance b -> CRUD a b (Instance b)
111 | save = inCreate <<< C.saveF
112 |
113 | create :: forall a b. b -> CRUD a b (Instance b)
114 | create = inCreate <<< C.createOneF
115 |
116 | bulkCreate :: forall a b. Array b -> CRUD a b Unit
117 | bulkCreate = inCreate <<< C.bulkCreateF
118 |
119 | -- injections for module R into the CRUD monad
120 |
121 | findById :: forall a b. Either String Int -> CRUD a b (Maybe (Instance b))
122 | findById = inRead <<< inFind <<< R.findByIdF
123 |
124 | findByString :: forall a b. String -> CRUD a b (Maybe (Instance b))
125 | findByString = inRead <<< inFind <<< R.findByStringF
126 |
127 | findByInt :: forall a b. Int -> CRUD a b (Maybe (Instance b))
128 | findByInt = inRead <<< inFind <<< R.findByIntF
129 |
130 | findOne :: forall a b. Options b -> CRUD a b (Maybe (Instance b))
131 | findOne = inRead <<< inFind <<< R.findOneF
132 |
133 | findOrBuild
134 | :: forall a b
135 | . Options b
136 | -> CRUD a b {inst :: Instance b, created :: Boolean}
137 | findOrBuild = inRead <<< inFind <<< R.findOrBuildF
138 |
139 | findOrCreate
140 | :: forall a b
141 | . Options b
142 | -> CRUD a b {inst :: Instance b, created :: Boolean}
143 | findOrCreate = inRead <<< inFind <<< R.findOrCreateF
144 |
145 | findAndCountAll
146 | :: forall a b
147 | . Options b
148 | -> CRUD a b {count :: Int, rows :: Array (Instance b)}
149 | findAndCountAll = inRead <<< inFind <<< R.findAndCountAllF
150 |
151 | findAll :: forall a b. Options b -> CRUD a b (Array (Instance b))
152 | findAll = inRead <<< inFind <<< R.findAllF
153 |
154 | count :: forall a b. Options a -> CRUD a b Int
155 | count = inRead <<< inCount <<< R.countF
156 |
157 | max :: forall a b. Options a -> String -> CRUD a b Int
158 | max o s = inRead $ inCount $ R.maxF o s
159 |
160 | min :: forall a b. Options a -> String -> CRUD a b Int
161 | min o s = inRead $ inCount $ R.minF o s
162 |
163 | -- injections for module U into the CRUD monad
164 |
165 | update :: forall a b. Instance a -> a -> CRUD a b Unit
166 | update i a = inUpdate $ U.updateF i a
167 |
168 | increment :: forall a b. Instance a -> StrMap Int -> CRUD a b Unit
169 | increment i m = inUpdate $ U.incrementF i m
170 |
171 | decrement :: forall a b. Instance a -> StrMap Int -> CRUD a b Unit
172 | decrement i m = inUpdate $ U.decrementF i m
173 |
174 | updateModel
175 | :: forall a b
176 | . Options a
177 | -> Options a
178 | -> CRUD a b {affectedCount :: Int, affectedRows :: Maybe (Array (Instance a))}
179 | updateModel a o = inUpdate $ U.updateModelF a o
180 |
181 | -- injections for module D into the CRUD monad
182 |
183 | destroy :: forall a b. Instance a -> CRUD a b Unit
184 | destroy = inDestroy <<< D.destroyF
185 |
186 | runCRUD
187 | :: forall a b e
188 | . Submodel a b
189 | => Model a
190 | => ModelOf a
191 | -> CRUD a b
192 | ~> Aff (sequelize :: SEQUELIZE | e)
193 | runCRUD = foldFree <<< interpretCRUDF
194 |
--------------------------------------------------------------------------------
/src/Sequelize/Free/Create.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Free.Create where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Sequelize.Class (class Submodel)
32 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
33 | import Sequelize.CRUD.Create as Create
34 |
35 | data CreateF b next
36 | = Build b (Instance b -> next)
37 | | Save (Instance b) (Instance b -> next)
38 | | CreateOne b (Instance b -> next)
39 | | BulkCreate (Array b) next
40 |
41 | buildF :: forall b. b -> CreateF b (Instance b)
42 | buildF b = Build b id
43 |
44 | saveF :: forall b. Instance b -> CreateF b (Instance b)
45 | saveF b = Save b id
46 |
47 | createOneF :: forall b. b -> CreateF b (Instance b)
48 | createOneF b = CreateOne b id
49 |
50 | bulkCreateF :: forall b. Array b -> CreateF b Unit
51 | bulkCreateF bs = BulkCreate bs unit
52 |
53 | interpretCreate
54 | :: forall a b e
55 | . Submodel a b
56 | => ModelOf a
57 | -> CreateF b
58 | ~> Aff (sequelize :: SEQUELIZE | e)
59 | interpretCreate model = case _ of
60 | Build b k -> pure $ k $ Create.build model b
61 | Save b k -> k <$> Create.save b
62 | CreateOne b k -> k <$> Create.create model b
63 | BulkCreate arr next -> next <$ Create.bulkCreate model arr
64 |
--------------------------------------------------------------------------------
/src/Sequelize/Free/Destroy.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Free.Destroy where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Sequelize.CRUD.Destroy as Destroy
32 | import Sequelize.Class (class Model)
33 | import Sequelize.Types (Instance, SEQUELIZE)
34 |
35 | data DestroyF a next = Destroy (Instance a) next
36 |
37 | destroyF :: forall a. Instance a -> DestroyF a Unit
38 | destroyF i = Destroy i unit
39 |
40 | interpretDestroy
41 | :: forall a e
42 | . Model a
43 | => DestroyF a
44 | ~> Aff (sequelize :: SEQUELIZE | e)
45 | interpretDestroy (Destroy i next) = next <$ Destroy.destroy i
46 |
--------------------------------------------------------------------------------
/src/Sequelize/Free/Read.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Free.Read where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Data.Either (Either)
32 | import Data.Maybe (Maybe)
33 | import Data.Functor.Coproduct (Coproduct, coproduct)
34 | import Data.Options (Options)
35 | import Sequelize.CRUD.Read as Read
36 | import Sequelize.Class (class Model, class Submodel)
37 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
38 |
39 | data FindF a next
40 | = FindById (Either String Int) (Maybe (Instance a) -> next)
41 | | FindByString String (Maybe (Instance a) -> next)
42 | | FindByInt Int (Maybe (Instance a) -> next)
43 | | FindOne (Options a) (Maybe (Instance a) -> next)
44 | | FindOrBuild (Options a) ({inst :: Instance a, created :: Boolean} -> next)
45 | | FindOrCreate (Options a) ({inst :: Instance a, created :: Boolean} -> next)
46 | | FindAndCountAll (Options a) ({count :: Int, rows :: Array (Instance a)} -> next)
47 | | FindAll (Options a) (Array (Instance a) -> next)
48 |
49 | data CountF a next
50 | = Count (Options a) (Int -> next)
51 | | Max (Options a) String (Int -> next)
52 | | Min (Options a) String (Int -> next)
53 |
54 | findByIdF :: forall a. Either String Int -> FindF a (Maybe (Instance a))
55 | findByIdF i = FindById i id
56 |
57 | findByStringF :: forall a. String -> FindF a (Maybe (Instance a))
58 | findByStringF i = FindByString i id
59 |
60 | findByIntF :: forall a. Int -> FindF a (Maybe (Instance a))
61 | findByIntF i = FindByInt i id
62 |
63 | findOneF :: forall a. Options a -> FindF a (Maybe (Instance a))
64 | findOneF o = FindOne o id
65 |
66 | findOrBuildF
67 | :: forall a
68 | . Options a
69 | -> FindF a {inst :: Instance a, created :: Boolean}
70 | findOrBuildF o = FindOrBuild o id
71 |
72 | findOrCreateF
73 | :: forall a
74 | . Options a
75 | -> FindF a {inst :: Instance a, created :: Boolean}
76 | findOrCreateF o = FindOrCreate o id
77 |
78 | findAndCountAllF
79 | :: forall a
80 | . Options a
81 | -> FindF a {count :: Int, rows :: Array (Instance a)}
82 | findAndCountAllF o = FindAndCountAll o id
83 |
84 | findAllF :: forall a. Options a -> FindF a (Array (Instance a))
85 | findAllF o = FindAll o id
86 |
87 | countF :: forall a. Options a -> CountF a Int
88 | countF o = Count o id
89 |
90 | maxF :: forall a. Options a -> String -> CountF a Int
91 | maxF o key = Max o key id
92 |
93 | minF :: forall a. Options a -> String -> CountF a Int
94 | minF o key = Min o key id
95 |
96 | interpretFind
97 | :: forall a b e
98 | . Submodel a b
99 | => ModelOf a
100 | -> FindF b
101 | ~> Aff (sequelize :: SEQUELIZE | e)
102 | interpretFind model = case _ of
103 | FindById e k -> k <$> Read.findById model e
104 | FindByString i k -> k <$> Read.findByStringId model i
105 | FindByInt i k -> k <$> Read.findByIntId model i
106 | FindOne o k -> k <$> Read.findOne model o
107 | FindOrBuild o k -> k <$> Read.findOrBuild model o
108 | FindOrCreate o k -> k <$> Read.findOrCreate model o
109 | FindAndCountAll o k -> k <$> Read.findAndCountAll model o
110 | FindAll o k -> k <$> Read.findAll model o
111 |
112 | interpretCount
113 | :: forall a e
114 | . Model a
115 | => ModelOf a
116 | -> CountF a
117 | ~> Aff (sequelize :: SEQUELIZE | e)
118 | interpretCount model = case _ of
119 | Count o k -> k <$> Read.count model o
120 | Max o key k -> k <$> Read.max model o key
121 | Min o key k -> k <$> Read.min model o key
122 |
123 | type ReadF a b = Coproduct (FindF b) (CountF a)
124 |
125 | -- not sure about this type signature
126 | interpretRead
127 | :: forall a b e
128 | . Submodel a b
129 | => Model a
130 | => ModelOf a
131 | -> ReadF a b
132 | ~> Aff (sequelize :: SEQUELIZE | e)
133 | interpretRead ma = coproduct (interpretFind ma) (interpretCount ma)
134 |
--------------------------------------------------------------------------------
/src/Sequelize/Free/Update.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Free.Update where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Data.Maybe (Maybe)
32 | import Data.Options (Options)
33 | import Data.StrMap (StrMap)
34 | import Sequelize.CRUD.Update as Update
35 | import Sequelize.Class (class Model)
36 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
37 |
38 | data UpdateF a next
39 | = Update (Instance a) a next
40 | | Increment (Instance a) (StrMap Int) next
41 | | Decrement (Instance a) (StrMap Int) next
42 | | UpdateModel
43 | (Options a)
44 | (Options a)
45 | ({affectedCount :: Int, affectedRows :: Maybe (Array (Instance a))} -> next)
46 |
47 | updateF :: forall a. Instance a -> a -> UpdateF a Unit
48 | updateF i a = Update i a unit
49 |
50 | incrementF :: forall a. Instance a -> StrMap Int -> UpdateF a Unit
51 | incrementF i strmap = Increment i strmap unit
52 |
53 | decrementF :: forall a. Instance a -> StrMap Int -> UpdateF a Unit
54 | decrementF i strmap = Decrement i strmap unit
55 |
56 | updateModelF
57 | :: forall a
58 | . Options a
59 | -> Options a
60 | -> UpdateF a {affectedCount :: Int, affectedRows :: Maybe (Array (Instance a))}
61 | updateModelF a o = UpdateModel a o id
62 |
63 | interpretUpdate
64 | :: forall a e
65 | . Model a
66 | => ModelOf a
67 | -> UpdateF a
68 | ~> Aff (sequelize :: SEQUELIZE | e)
69 | interpretUpdate moa = case _ of
70 | Update inst a next -> next <$ Update.update inst a
71 | Increment inst strmap next -> next <$ Update.increment inst strmap
72 | Decrement inst strmap next -> next <$ Update.decrement inst strmap
73 | UpdateModel a o k -> k <$> Update.updateModel moa a o
74 |
--------------------------------------------------------------------------------
/src/Sequelize/Instance.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports._getValues = function (instance) {
27 | return instance.get({plain: true});
28 | };
29 |
30 | exports._equalsOneOf = function (i, is) {
31 | return i.equalsOneOf(is);
32 | }
33 |
34 | exports._unsafeEq = function (i1, i2) {
35 | return i1.equals(i2);
36 | };
37 |
--------------------------------------------------------------------------------
/src/Sequelize/Instance.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | -- | See http://docs.sequelizejs.com/manual/tutorial/instances.html
27 | module Sequelize.Instance
28 | ( instanceToModel
29 | , instanceToModelE
30 | , peek
31 | , unsafeEq, (==!)
32 | , unsafeEqAny
33 | ) where
34 |
35 | import Prelude
36 |
37 | import Control.Monad.Except (runExcept)
38 | import Data.Either (Either)
39 | import Data.Foreign (MultipleErrors)
40 | import Data.Foreign as F
41 | import Data.Foreign.Index (readProp)
42 | import Data.Function.Uncurried (Fn2, runFn2)
43 | import Sequelize.Class (class Model, decodeModel)
44 | import Sequelize.Types (Instance)
45 |
46 | foreign import _getValues
47 | :: forall a. Instance a -> F.Foreign
48 |
49 | instanceToModel
50 | :: forall a. Model a
51 | => Instance a
52 | -> F.F a
53 | instanceToModel = decodeModel <<< _getValues
54 |
55 | instanceToModelE
56 | :: forall a. Model a
57 | => Instance a
58 | -> Either MultipleErrors a
59 | instanceToModelE = runExcept <<< instanceToModel
60 |
61 | -- | See if the instance has a value. Useful for when your database has columns
62 | -- | which do not correspond to any part of your Model, for example a unique "id"
63 | -- | column.
64 | peek
65 | :: forall a
66 | . Model a
67 | => Instance a
68 | -> String
69 | -> F.F F.Foreign
70 | peek inst key = readProp key $ _getValues inst
71 |
72 | foreign import _unsafeEq :: forall a. Fn2 (Instance a) (Instance a) Boolean
73 |
74 | -- | NOTE: This is unsafe because it breaks the `Eq` laws owing to mutation on
75 | -- | instances. For example, for two instances of the same model built with
76 | -- | different parameters (i.e. `i1 = build m x` and `i2 = build m y`), calling
77 | -- | `i1 ==! i2` immediately will return `true`, but if called **after** saving
78 | -- | to the database, will return `false`.
79 | unsafeEq :: forall a. Instance a -> Instance a -> Boolean
80 | unsafeEq = runFn2 _unsafeEq
81 |
82 | infix 4 unsafeEq as ==!
83 |
84 | foreign import _equalsOneOf
85 | :: forall a. Fn2 (Instance a) (Array (Instance a)) Boolean
86 |
87 | unsafeEqAny :: forall a. Instance a -> Array (Instance a) -> Boolean
88 | unsafeEqAny = runFn2 _equalsOneOf
89 |
--------------------------------------------------------------------------------
/src/Sequelize/Models.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports._makeModel = function (sequelize, name, attrs, opts) {
27 | return function () {
28 | return sequelize.define(name, attrs, opts);
29 | };
30 | };
31 |
32 | exports._sync = function (isJust, fromJust, model, opts) {
33 | var newOpts = {};
34 | if (isJust(opts["match"])) {
35 | newOpts["match"] = fromJust(opts["match"])();
36 | }
37 | if (opts["logging"] === true) {
38 | newOpts["logging"] = console.log;
39 | } else {
40 | newOpts["logging"] = opts["logging"];
41 | }
42 | newOpts["force"] = opts["force"];
43 | newOpts["schema"] = opts["schema"];
44 | newOpts["searchPath"] = opts["searchPath"];
45 | newOpts["hooks"] = opts["hooks"];
46 | newOpts["alter"] = opts["alter"];
47 | return model.sync(newOpts);
48 | };
49 |
50 | exports._drop = function (model) {
51 | return model.drop();
52 | };
53 |
54 | exports._hasOne = function (source, target, alias) {
55 | return function() {
56 | source.hasOne(target, {as: alias});
57 | return null;
58 | };
59 | };
60 |
61 | exports._hasMany = function (source, target, alias) {
62 | return function () {
63 | source.hasOne(target, {as: alias});
64 | return null;
65 | };
66 | };
67 |
68 | exports._belongsTo = function (target, source, alias) {
69 | return function() {
70 | target.belongsTo(source, {as: alias});
71 | return null;
72 | };
73 | };
74 |
75 | exports._belongsToMany = function (target, source, thru) {
76 | return function () {
77 | target.belongsToMany(source, {through: thru});
78 | return null;
79 | };
80 | };
81 |
82 | exports._belongsToWithOptions = function (target, source, op) {
83 | return function() {
84 | target.belongsTo(source, op);
85 | return null;
86 | };
87 | };
88 |
--------------------------------------------------------------------------------
/src/Sequelize/Models.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Models
27 | ( makeModelOf
28 | , sync
29 | , drop
30 | , hasOne
31 | , hasMany
32 | , belongsTo
33 | , belongsToMany
34 | , belongsToWithOptions
35 | ) where
36 |
37 | import Prelude
38 |
39 | import Control.Monad.Aff (Aff)
40 | import Control.Monad.Eff (Eff)
41 | import Control.Monad.Eff.Class (liftEff)
42 | import Control.Promise (Promise, toAff)
43 | import Data.Bifunctor (rmap)
44 | import Data.Foreign (Foreign)
45 | import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
46 | import Data.Maybe (Maybe, fromJust, isJust)
47 | import Data.Options (Options, options)
48 | import Data.StrMap (StrMap, fromFoldable)
49 | import Sequelize.Class (class Model, modelName, modelCols)
50 | import Sequelize.Types (Alias, Conn, ModelOf, ModelOpts, SEQUELIZE, ModelCols, SyncOpts)
51 | import Type.Proxy (Proxy(..))
52 |
53 | foreign import _makeModel
54 | :: forall a e.
55 | Fn4
56 | Conn
57 | String -- Name
58 | (StrMap Foreign) -- Schema
59 | Foreign -- Options for the model
60 | (Eff ( sequelize :: SEQUELIZE | e ) (ModelOf a))
61 |
62 | makeModelOf
63 | :: forall a e. Model a
64 | => Conn
65 | -> Options (ModelOpts a)
66 | -> Aff ( sequelize :: SEQUELIZE | e ) (ModelOf a)
67 | makeModelOf conn opts =
68 | let opts' = options opts
69 | columns = fromFoldable $ map (rmap options) $ modelCols :: ModelCols a
70 | name = modelName (Proxy :: Proxy a)
71 | in liftEff $ runFn4 _makeModel conn name columns opts'
72 |
73 | foreign import _sync
74 | :: forall a b.
75 | Fn4
76 | (Maybe b -> Boolean)
77 | (Partial => Maybe a -> a)
78 | (ModelOf a)
79 | SyncOpts
80 | (Promise Unit)
81 |
82 | sync
83 | :: forall a e. Model a
84 | => ModelOf a
85 | -> SyncOpts
86 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
87 | sync mod force = toAff $ runFn4 _sync isJust fromJust mod force
88 |
89 | foreign import _drop :: forall a. ModelOf a -> Promise Unit
90 |
91 | drop
92 | :: forall a e. Model a
93 | => ModelOf a
94 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
95 | drop = toAff <<< _drop
96 |
97 | foreign import _hasOne
98 | :: forall a b alias e.
99 | Fn3
100 | (ModelOf a)
101 | (ModelOf b)
102 | alias
103 | (Eff e Unit)
104 |
105 | -- | HasOne associations are associations where the foreign key for the
106 | -- | one-to-one relation exists on the target model."
107 | hasOne
108 | :: forall source target e. Model source
109 | => Model target
110 | => ModelOf source
111 | -> ModelOf target
112 | -> Alias
113 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
114 | hasOne s t a = liftEff $ runFn3 _hasOne s t a
115 |
116 | foreign import _hasMany
117 | :: forall a b alias e.
118 | Fn3
119 | (ModelOf a)
120 | (ModelOf b)
121 | alias
122 | (Eff e Unit)
123 |
124 | -- | HasOne associations are associations where the foreign key for the
125 | -- | one-to-one relation exists on the target model."
126 | hasMany
127 | :: forall source target e. Model source
128 | => Model target
129 | => ModelOf source
130 | -> ModelOf target
131 | -> Alias
132 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
133 | hasMany s t a = liftEff $ runFn3 _hasMany s t a
134 |
135 | foreign import _belongsTo
136 | :: forall a b alias e.
137 | Fn3
138 | (ModelOf a)
139 | (ModelOf b)
140 | alias
141 | (Eff e Unit)
142 |
143 | -- | #Associations
144 | -- | See: http://docs.sequelizejs.com/manual/tutorial/associations.html
145 |
146 | -- | "BelongsTo associations are associations where the foreign key for the
147 | -- | one-to-one relation exists on the source model."
148 | belongsTo
149 | :: forall source target e. Model source
150 | => Model target
151 | => ModelOf target
152 | -> ModelOf source
153 | -> Alias
154 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
155 | belongsTo t s a = liftEff $ runFn3 _belongsTo t s a
156 |
157 | foreign import _belongsToMany
158 | :: forall a b through e.
159 | Fn3
160 | (ModelOf a)
161 | (ModelOf b)
162 | through
163 | (Eff e Unit)
164 |
165 | belongsToMany
166 | :: forall source target e. Model source
167 | => Model target
168 | => ModelOf target
169 | -> ModelOf source
170 | -> Alias
171 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
172 | belongsToMany t s a = liftEff $ runFn3 _belongsToMany t s a
173 |
174 | foreign import _belongsToWithOptions
175 | :: forall a b e.
176 | Fn3
177 | (ModelOf a)
178 | (ModelOf b)
179 | Foreign
180 | (Eff e Unit)
181 |
182 | belongsToWithOptions
183 | :: forall source target e. Model source
184 | => Model target
185 | => ModelOf target
186 | -> ModelOf source
187 | -> Foreign
188 | -> Aff ( sequelize :: SEQUELIZE | e ) Unit
189 | belongsToWithOptions t s op = liftEff $ runFn3 _belongsToWithOptions t s op
--------------------------------------------------------------------------------
/src/Sequelize/Models/Columns.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Models.Columns
27 | ( columnType
28 | , allowNull
29 | , defaultValue
30 | , unique
31 | , primaryKey
32 | , field
33 | , autoIncrement
34 | , comment
35 | , references
36 | , OnUpdateOrDelete(..)
37 | , onUpdate
38 | , onDelete
39 | ) where
40 |
41 | import Data.Either (Either, either)
42 | import Data.Foreign (Foreign, toForeign)
43 | import Data.Functor.Contravariant ((>$<))
44 | import Data.Options (Option, opt)
45 | import Sequelize.Models.Types (DataType, sqzDataTypetoForeign)
46 | import Sequelize.Types (ColumnOpts)
47 |
48 | columnType :: forall a. Option (ColumnOpts a) DataType
49 | columnType = sqzDataTypetoForeign >$< opt "type"
50 |
51 | allowNull :: forall a. Option (ColumnOpts a) Boolean
52 | allowNull = opt "allowNull"
53 |
54 | defaultValue :: forall a. Option (ColumnOpts a) Foreign
55 | defaultValue = opt "defaultValue"
56 |
57 | unique :: forall a. Option (ColumnOpts a) (Either String Boolean)
58 | unique = either toForeign toForeign >$< opt "unique"
59 |
60 | primaryKey :: forall a. Option (ColumnOpts a) Boolean
61 | primaryKey = opt "primaryKey"
62 |
63 | field :: forall a. Option (ColumnOpts a) String
64 | field = opt "field"
65 |
66 | autoIncrement :: forall a. Option (ColumnOpts a) Boolean
67 | autoIncrement = opt "autoIncrement"
68 |
69 | comment :: forall a. Option (ColumnOpts a) String
70 | comment = opt "comment"
71 |
72 | references :: forall a. Option (ColumnOpts a) { model :: String, key :: String }
73 | references = opt "references"
74 |
75 | onUpdate :: forall a. Option (ColumnOpts a) OnUpdateOrDelete
76 | onUpdate = stringifyOnUpdateOrDelete >$< opt "onUpdate"
77 |
78 | data OnUpdateOrDelete
79 | = Cascade
80 | | Restrict
81 | | SetDefault
82 | | SetNull
83 | | NoAction
84 |
85 | stringifyOnUpdateOrDelete :: OnUpdateOrDelete -> String
86 | stringifyOnUpdateOrDelete = case _ of
87 | Cascade -> "CASCADE"
88 | Restrict -> "RESTRICT"
89 | SetDefault -> "SET DEFAULT"
90 | SetNull -> "SET NULL"
91 | _ -> "NO ACTION"
92 |
93 | onDelete :: forall a. Option (ColumnOpts a) OnUpdateOrDelete
94 | onDelete = stringifyOnUpdateOrDelete >$< opt "onDelete"
95 |
--------------------------------------------------------------------------------
/src/Sequelize/Models/Options.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Models.Options
27 | ( omitNull
28 | , timestamps
29 | , paranoid
30 | , underscored
31 | , underscoredAll
32 | , freezeTableName
33 | , name
34 | , indexes
35 | , createdAt
36 | , updatedAt
37 | , deletedAt
38 | , tableName
39 | , schema
40 | , engine
41 | , charset
42 | , comment
43 | , collate
44 | , initialAutoIncrement
45 | , defaultScope
46 | , returning
47 | ) where
48 |
49 | import Data.Functor.Contravariant ((>$<))
50 | import Data.Options (Option, opt)
51 | import Sequelize.Class (class IsWhere, class Model, toWhere)
52 | import Sequelize.Types (ModelOpts)
53 |
54 | omitNull :: forall a. Model a => Option (ModelOpts a) Boolean
55 | omitNull = opt "omitNull"
56 |
57 | timestamps :: forall a. Model a => Option (ModelOpts a) Boolean
58 | timestamps = opt "timestamps"
59 |
60 | paranoid :: forall a. Model a => Option (ModelOpts a) Boolean
61 | paranoid = opt "paranoid"
62 |
63 | underscored :: forall a. Model a => Option (ModelOpts a) Boolean
64 | underscored = opt "underscored"
65 |
66 | returning :: forall a. Model a => Option a Boolean
67 | returning = opt "returning"
68 |
69 | underscoredAll :: forall a. Model a => Option (ModelOpts a) Boolean
70 | underscoredAll = opt "underscoredAll"
71 |
72 | freezeTableName :: forall a. Model a => Option (ModelOpts a) Boolean
73 | freezeTableName = opt "freezeTableName"
74 |
75 | name :: forall a. Model a => Option (ModelOpts a) { singular :: String, plural :: String }
76 | name = opt "name"
77 |
78 | indexes :: forall a. Model a => Option (ModelOpts a) (Array Index)
79 | indexes = opt "indexes"
80 |
81 | type Index =
82 | { name :: String
83 | , type :: String
84 | , method :: String
85 | , unique :: Boolean
86 | , concurrently :: Boolean
87 | , fields :: Array String
88 | }
89 |
90 | createdAt :: forall a. Model a => Option (ModelOpts a) String
91 | createdAt = opt "createdAt"
92 |
93 | updatedAt :: forall a. Model a => Option (ModelOpts a) String
94 | updatedAt = opt "updatedAt"
95 |
96 | deletedAt :: forall a. Model a => Option (ModelOpts a) String
97 | deletedAt = opt "deletedAt"
98 |
99 | tableName :: forall a. Model a => Option (ModelOpts a) String
100 | tableName = opt "tableName"
101 |
102 | schema :: forall a. Model a => Option (ModelOpts a) String
103 | schema = opt "schema"
104 |
105 | engine :: forall a. Model a => Option (ModelOpts a) String
106 | engine = opt "engine"
107 |
108 | charset :: forall a. Model a => Option (ModelOpts a) String
109 | charset = opt "charset"
110 |
111 | comment :: forall a. Model a => Option (ModelOpts a) String
112 | comment = opt "comment"
113 |
114 | collate :: forall a. Model a => Option (ModelOpts a) String
115 | collate = opt "collate"
116 |
117 | initialAutoIncrement :: forall a. Model a => Option (ModelOpts a) String
118 | initialAutoIncrement = opt "initialAutoIncrement"
119 |
120 | defaultScope :: forall wh a. IsWhere wh => Model a => Option (ModelOpts a) (wh a)
121 | defaultScope = toWhere >$< opt "where"
122 |
--------------------------------------------------------------------------------
/src/Sequelize/Models/Types.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | var Sequelize = require('sequelize');
27 |
28 | exports._STRING = Sequelize.STRING;
29 | exports._CHAR = Sequelize.CHAR;
30 | exports._TEXT = Sequelize.TEXT;
31 | exports._INTEGER = Sequelize.INTEGER;
32 | exports._BIGINT = Sequelize.BIGINT;
33 | exports._BIGINT0 = Sequelize.BIGINT;
34 | exports._FLOAT = Sequelize.FLOAT;
35 | exports._FLOAT0 = Sequelize.FLOAT;
36 | exports._FLOAT1 = Sequelize.FLOAT;
37 | exports._DOUBLE = Sequelize.DOUBLE;
38 | exports._DOUBLE0 = Sequelize.DOUBLE;
39 | exports._DOUBLE1 = Sequelize.DOUBLE;
40 | exports._DECIMAL = Sequelize.DECIMAL;
41 | exports._DECIMAL0 = Sequelize.DECIMAL;
42 | exports._REAL = Sequelize.REAL;
43 | exports._REAL0 = Sequelize.REAL;
44 | exports._REAL1 = Sequelize.REAL;
45 | exports._BOOLEAN = Sequelize.BOOLEAN;
46 | exports._BLOB = Sequelize.BLOB;
47 | exports._DATE = Sequelize.DATE;
48 | exports._DATE0 = Sequelize.DATE;
49 | exports._DATEONLY = Sequelize.DATEONLY;
50 | exports._TIME = Sequelize.TIME;
51 | exports._NOW = Sequelize.NOW;
52 | exports._UUID = Sequelize.UUID;
53 | exports._UUIDV1 = Sequelize.UUIDV1;
54 | exports._UUIDV4 = Sequelize.UUIDV4;
55 | exports._HSTORE = Sequelize.HSTORE;
56 | exports._JSON = Sequelize.JSON;
57 | exports._JSONB = Sequelize.JSONB;
58 | exports._ARRAY = Sequelize.ARRAY;
59 | exports._RANGE = Sequelize.RANGE;
60 | exports._GEOMETRY = Sequelize.GEOMETRY;
61 | exports._GEOGRAPHY = Sequelize.GEOGRAPHY;
62 | exports._VIRTUAL = Sequelize.VIRTUAL;
63 | exports._ENUM = function (arr) {
64 | return Sequelize.ENUM.apply(this, arr);
65 | };
66 |
--------------------------------------------------------------------------------
/src/Sequelize/Models/Types.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Models.Types
27 | ( DataType
28 | ( String
29 | , Char
30 | , Text
31 | , Integer
32 | , BigInt
33 | , BigInt0
34 | , Float
35 | , Float0
36 | , Float1
37 | , Double
38 | , Double0
39 | , Double1
40 | , Decimal
41 | , Decimal0
42 | , Real
43 | , Real0
44 | , Real1
45 | , Boolean
46 | , Blob
47 | , Date
48 | , Date0
49 | , DateOnly
50 | , Time
51 | , Now
52 | , UUID
53 | , UUIDv1
54 | , UUIDv4
55 | , HStore
56 | , Json
57 | , JsonB
58 | , Array
59 | , Range
60 | , Geometry
61 | , Geography
62 | , Virtual )
63 | , enum
64 | , enum'
65 | , Length(..)
66 | , PositiveInt
67 | , PositiveInt(..)
68 | , mkPositiveInt
69 | , sqzDataTypetoForeign
70 | ) where
71 |
72 | import Prelude
73 |
74 | import Data.Enum (class Enum, enumFromTo)
75 | import Data.Foreign (Foreign)
76 | import Data.Function.Uncurried (Fn1, Fn2, runFn2)
77 | import Data.Maybe (Maybe(..), maybe)
78 | import Data.Newtype (class Newtype, unwrap)
79 |
80 | data DataType
81 | = String { length :: Maybe PositiveInt }
82 | | Char { length :: Maybe PositiveInt }
83 | | Text Length
84 | | Integer { length :: Maybe PositiveInt }
85 | | BigInt { length :: PositiveInt }
86 | | BigInt0
87 | | Float { length :: PositiveInt, decimals :: PositiveInt }
88 | | Float0
89 | | Float1 { length :: PositiveInt }
90 | | Double { length :: PositiveInt, decimals :: PositiveInt }
91 | | Double0
92 | | Double1 { length :: PositiveInt }
93 | | Decimal { precision :: PositiveInt, scale :: PositiveInt }
94 | | Decimal0
95 | | Real { length :: PositiveInt, decimals :: PositiveInt }
96 | | Real0
97 | | Real1 { length :: PositiveInt }
98 | | Boolean
99 | | Blob Length
100 | | Enum (Array String)
101 | | Date { length :: PositiveInt }
102 | | Date0
103 | | DateOnly
104 | | Time
105 | | Now
106 | | UUID
107 | | UUIDv1
108 | | UUIDv4
109 | | HStore
110 | | Json
111 | | JsonB
112 | | Array DataType
113 | -- TODO: implement correct semantics for all of these
114 | | Range
115 | | Geometry
116 | | Geography
117 | | Virtual
118 |
119 | -- | For passing ENUM types, create a data type that's an instance of the Enum
120 | -- | and Bounded typeclasses.
121 | -- | Example: data MyEnum = A | B
122 | -- | `instance enumMyEnum :: Enum MyEnum where succ A = Just B; succ B = Nothing;`
123 | -- | `pred B = Just A; pred B = Nothing`
124 | -- | `instance boundedMyEnum :: Bounded MyEnum where bottom = A; top = B`
125 | -- | Then to pass this data type to Sequelize, use `enum (Proxy :: Proxy MyEnum)`
126 | -- | As an alternative you can also derive `Enum` and `Bounded` instances using
127 | -- | `generics-rep`.
128 | enum :: forall proxy a. Show a => Enum a => Bounded a => proxy a -> DataType
129 | enum _ = Enum $ map show xs
130 | where
131 | xs :: Array a
132 | xs = enumFromTo bottom top
133 |
134 | -- | An alternative to `enum` where your datatype is small enough to not warrant
135 | -- | deriving/defining the `Enum` and `Bounded` instances.
136 | -- | Instead of deriving instances like in the example above, just
137 | -- | define/derive `Show` and pass in an array literal: `enum' [A, B]`.
138 | enum' :: forall a. Show a => Array a -> DataType
139 | enum' xs = Enum $ map show xs
140 |
141 | newtype PositiveInt = PositiveInt Int
142 |
143 | derive instance newtypePositiveInt :: Newtype PositiveInt _
144 |
145 | mkPositiveInt :: Int -> Maybe PositiveInt
146 | mkPositiveInt n
147 | | 0 <= n = Just (PositiveInt n)
148 | | otherwise = Nothing
149 |
150 | data Length = Tiny | Medium | Long
151 |
152 | encodeLength :: Length -> String
153 | encodeLength Tiny = "tiny"
154 | encodeLength Medium = "medium"
155 | encodeLength Long = "long"
156 |
157 | foreign import _STRING :: Fn1 Int Foreign
158 | foreign import _CHAR :: Fn1 Int Foreign
159 | foreign import _TEXT :: Fn1 String Foreign
160 | foreign import _INTEGER :: Fn1 Int Foreign
161 | foreign import _BIGINT :: Fn1 Int Foreign
162 | foreign import _BIGINT0 :: Foreign
163 | foreign import _FLOAT :: Fn2 Int Int Foreign
164 | foreign import _FLOAT0 :: Foreign
165 | foreign import _FLOAT1 :: Fn1 Int Foreign
166 | foreign import _DOUBLE :: Fn2 Int Int Foreign
167 | foreign import _DOUBLE0 :: Foreign
168 | foreign import _DOUBLE1 :: Fn1 Int Foreign
169 | foreign import _DECIMAL :: Fn2 Int Int Foreign
170 | foreign import _DECIMAL0 :: Foreign
171 | foreign import _REAL :: Fn2 Int Int Foreign
172 | foreign import _REAL0 :: Foreign
173 | foreign import _REAL1 :: Fn1 Int Foreign
174 | foreign import _BOOLEAN :: Foreign
175 | foreign import _BLOB :: Fn1 String Foreign
176 | foreign import _DATE :: Fn1 Int Foreign
177 | foreign import _DATE0 :: Foreign
178 | foreign import _DATEONLY :: Foreign
179 | foreign import _TIME :: Foreign
180 | foreign import _NOW :: Foreign
181 | foreign import _UUID :: Foreign
182 | foreign import _UUIDV1 :: Foreign
183 | foreign import _UUIDV4 :: Foreign
184 | foreign import _HSTORE :: Foreign
185 | foreign import _JSON :: Foreign
186 | foreign import _JSONB :: Foreign
187 | foreign import _ARRAY :: Fn1 Foreign Foreign
188 | foreign import _RANGE :: Foreign
189 | foreign import _GEOMETRY :: Foreign
190 | foreign import _GEOGRAPHY :: Foreign
191 | foreign import _VIRTUAL :: Foreign
192 | foreign import _ENUM :: Fn1 (Array String) Foreign
193 |
194 | sqzDataTypetoForeign :: DataType -> Foreign
195 | sqzDataTypetoForeign = case _ of
196 | String {length} -> _STRING $ maybe 255 unwrap length
197 | Char {length} -> _CHAR $ maybe 255 unwrap length
198 | Text length -> _TEXT $ encodeLength length
199 | Integer {length} -> _INTEGER $ maybe 255 unwrap length
200 | BigInt {length} -> _BIGINT $ unwrap length
201 | BigInt0 -> _BIGINT0
202 | Float {length, decimals} -> runFn2 _FLOAT (unwrap length) (unwrap decimals)
203 | Float0 -> _FLOAT0
204 | Float1 {length} -> _FLOAT1 $ unwrap length
205 | Double {length, decimals} -> runFn2 _DOUBLE (unwrap length) (unwrap decimals)
206 | Double0 -> _DOUBLE0
207 | Double1 {length} -> _DOUBLE1 $ unwrap length
208 | Decimal {precision, scale} -> runFn2 _DECIMAL (unwrap precision) (unwrap scale)
209 | Decimal0 -> _DECIMAL0
210 | Real {length, decimals} -> runFn2 _REAL (unwrap length) (unwrap decimals)
211 | Real0 -> _REAL0
212 | Real1 {length} -> _REAL1 $ unwrap length
213 | Boolean -> _BOOLEAN
214 | Blob length -> _BLOB $ encodeLength length
215 | Enum xs -> _ENUM xs
216 | Date {length} -> _DATE $ unwrap length
217 | Date0 -> _DATE0
218 | DateOnly -> _DATEONLY
219 | Time -> _TIME
220 | Now -> _NOW
221 | UUID -> _UUID
222 | UUIDv1 -> _UUIDV1
223 | UUIDv4 -> _UUIDV4
224 | HStore -> _HSTORE
225 | Json -> _JSON
226 | JsonB -> _JSONB
227 | Array dt -> _ARRAY $ sqzDataTypetoForeign dt
228 | Range -> _RANGE
229 | Geometry -> _GEOMETRY
230 | Geography -> _GEOGRAPHY
231 | Virtual -> _VIRTUAL
232 |
--------------------------------------------------------------------------------
/src/Sequelize/Query/Options.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports._array2 = function (a, b) {
27 | return [a, b];
28 | };
29 |
30 | exports._array3 = function (a, b, c) {
31 | return [a, b, c];
32 | };
33 |
34 | exports._array4 = function (a, b, c, d) {
35 | return [a, b, c, d];
36 | };
37 |
38 | exports._array5 = function (a, b, c, d, e) {
39 | return [a, b, c, d, e];
40 | };
41 |
42 | exports._array6 = function (a, b, c, d, e, f) {
43 | return [a, b, c, d, e, f];
44 | };
45 |
46 | exports._array7 = function (a, b, c, d, e, f, g) {
47 | return [a, b, c, d, e, f, g];
48 | };
49 |
50 | exports._array8 = function (a, b, c, d, e, f, g, h) {
51 | return [a, b, c, d, e, f, g, h];
52 | };
53 |
54 | exports._array9 = function (a, b, c, d, e, f, g, h, i) {
55 | return [a, b, c, d, e, f, g, h, i];
56 | };
57 |
--------------------------------------------------------------------------------
/src/Sequelize/Query/Options.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Query.Options
27 | ( where_
28 | , attributes
29 | , paranoid
30 | , limit
31 | , offset
32 | , order
33 | , transaction
34 | , lock
35 | , searchPath
36 | , returning
37 | , defaults
38 | , include
39 | , include1
40 | , include2
41 | , include3
42 | , include4
43 | , include5
44 | , include6
45 | , include7
46 | , include8
47 | , include9
48 | , logging
49 | , logging2
50 | ) where
51 |
52 | import Control.Monad.Eff (Eff)
53 | import Control.Monad.Eff.Uncurried (EffFn1, EffFn2)
54 | import Data.Function.Uncurried (Fn2, Fn3, Fn4, Fn5, Fn6, Fn7, Fn8, Fn9, runFn2, runFn3, runFn4, runFn5, runFn6, runFn7, runFn8, runFn9)
55 | import Data.Functor.Contravariant ((>$<))
56 | import Data.Options (Option, opt)
57 | import Data.Tuple.Nested (type (/\), (/\))
58 | import Prelude (Unit)
59 | import Sequelize.Class (class IsWhere, class Model, encodeModel, toWhere)
60 | import Sequelize.Types (Alias, ModelOf, Transaction)
61 |
62 | where_ :: forall wh a. Model a => IsWhere wh => Option a (wh a)
63 | where_ = toWhere >$< opt "where"
64 |
65 | attributes :: forall a. Model a => Option a (Array (Array String))
66 | attributes = opt "attributes"
67 |
68 | paranoid :: forall a. Model a => Option a Boolean
69 | paranoid = opt "paranoid"
70 |
71 | limit :: forall a. Model a => Option a Int
72 | limit = opt "limit"
73 |
74 | offset :: forall a. Model a => Option a Int
75 | offset = opt "offset"
76 |
77 | order :: forall a. Model a => Option a (Array (Array String))
78 | order = opt "order"
79 |
80 | transaction :: forall a. Model a => Option a Transaction
81 | transaction = opt "transaction"
82 |
83 | lock :: forall a. Model a => Option a Boolean
84 | lock = opt "lock"
85 |
86 | searchPath :: forall a. Model a => Option a String
87 | searchPath = opt "searchPath"
88 |
89 | -- | Only for postgres
90 | returning :: forall a. Model a => Option a Boolean
91 | returning = opt "returning"
92 |
93 | defaults :: forall a. Model a => Option a a
94 | defaults = encodeModel >$< opt "defaults"
95 |
96 | -- | Alias for include1
97 | include :: forall a b. Model b => Option a {model :: ModelOf b, as :: Alias}
98 | include = include1
99 |
100 | include1 :: forall a b. Model b => Option a {model :: ModelOf b, as :: Alias}
101 | include1 = f >$< opt "include"
102 | where
103 | f :: forall x. x -> Array x
104 | f x = [x]
105 |
106 | include2
107 | :: forall a b
108 | . Model a
109 | => Model b
110 | => Option a (ModelOf a /\ ModelOf b)
111 | include2 = f >$< opt "include"
112 | where
113 | f (ma /\ mb) = runFn2 _array2 ma mb
114 |
115 | include3
116 | :: forall a b c
117 | . Model a
118 | => Model b
119 | => Model c
120 | => Option a (ModelOf a /\ ModelOf b /\ ModelOf c)
121 | include3 = f >$< opt "include"
122 | where
123 | f (ma /\ mb /\ mc) = runFn3 _array3 ma mb mc
124 |
125 | include4
126 | :: forall a b c d
127 | . Model a
128 | => Model b
129 | => Model c
130 | => Model d
131 | => Option a (ModelOf a /\ ModelOf b /\ ModelOf c /\ ModelOf d)
132 | include4 = f >$< opt "include"
133 | where
134 | f (ma /\ mb /\ mc /\ md) = runFn4 _array4 ma mb mc md
135 |
136 | include5
137 | :: forall a b c d e
138 | . Model a
139 | => Model b
140 | => Model c
141 | => Model d
142 | => Model e
143 | => Option a (ModelOf a /\ ModelOf b /\ ModelOf c /\ ModelOf d /\ ModelOf e)
144 | include5 = f >$< opt "include"
145 | where
146 | f (ma /\ mb /\ mc /\ md /\ me) = runFn5 _array5 ma mb mc md me
147 |
148 | include6
149 | :: forall a b c d e f
150 | . Model a
151 | => Model b
152 | => Model c
153 | => Model d
154 | => Model e
155 | => Model f
156 | => Option a (ModelOf a /\ ModelOf b /\ ModelOf c /\ ModelOf d /\ ModelOf e /\ ModelOf f)
157 | include6 = f >$< opt "include"
158 | where
159 | f (ma /\ mb /\ mc /\ md /\ me /\ mf) = runFn6 _array6 ma mb mc md me mf
160 |
161 | include7
162 | :: forall a b c d e f g
163 | . Model a
164 | => Model b
165 | => Model c
166 | => Model d
167 | => Model e
168 | => Model f
169 | => Model g
170 | => Option a (ModelOf a /\ ModelOf b /\ ModelOf c /\ ModelOf d /\ ModelOf e /\ ModelOf f /\ ModelOf g)
171 | include7 = f >$< opt "include"
172 | where
173 | f (ma /\ mb /\ mc /\ md /\ me /\ mf /\ mg) = runFn7 _array7 ma mb mc md me mf mg
174 |
175 | include8
176 | :: forall a b c d e f g h
177 | . Model a
178 | => Model b
179 | => Model c
180 | => Model d
181 | => Model e
182 | => Model f
183 | => Model g
184 | => Model h
185 | => Option a (ModelOf a /\ ModelOf b /\ ModelOf c /\ ModelOf d /\ ModelOf e /\ ModelOf f /\ ModelOf g /\ ModelOf h)
186 | include8 = f >$< opt "include"
187 | where
188 | f (ma /\ mb /\ mc /\ md /\ me /\ mf /\ mg /\ mh) = runFn8 _array8 ma mb mc md me mf mg mh
189 |
190 | include9
191 | :: forall a b c d e f g h i
192 | . Model a
193 | => Model b
194 | => Model c
195 | => Model d
196 | => Model e
197 | => Model f
198 | => Model g
199 | => Model h
200 | => Model i
201 | => Option a (ModelOf a /\ ModelOf b /\ ModelOf c /\ ModelOf d /\ ModelOf e /\ ModelOf f /\ ModelOf g /\ ModelOf h /\ ModelOf i)
202 | include9 = f >$< opt "include"
203 | where
204 | f (ma /\ mb /\ mc /\ md /\ me /\ mf /\ mg /\ mh /\ mi) = runFn9 _array9 ma mb mc md me mf mg mh mi
205 |
206 | logging :: forall a eff. Model a => Option a (EffFn1 eff String Unit)
207 | logging = opt "logging"
208 |
209 | logging2 :: forall a eff. Model a => Option a (EffFn2 eff String String Unit)
210 | logging2 = opt "logging"
211 |
212 | foreign import _array2 :: forall a b c. Fn2 a b (Array c)
213 | foreign import _array3 :: forall a b c d. Fn3 a b c (Array d)
214 | foreign import _array4 :: forall a b c d e. Fn4 a b c d (Array e)
215 | foreign import _array5 :: forall a b c d e f. Fn5 a b c d e (Array f)
216 | foreign import _array6 :: forall a b c d e f g. Fn6 a b c d e f (Array g)
217 | foreign import _array7 :: forall a b c d e f g h. Fn7 a b c d e f g (Array h)
218 | foreign import _array8 :: forall a b c d e f g h i. Fn8 a b c d e f g h (Array i)
219 | foreign import _array9 :: forall a b c d e f g h i j. Fn9 a b c d e f g h i (Array j)
220 |
--------------------------------------------------------------------------------
/src/Sequelize/Query/Options/Common.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Query.Options.Common where
27 |
28 | import Prelude
29 | import Data.Foreign (Foreign, toForeign)
30 |
31 | trueF :: Foreign
32 | trueF = toForeign true
33 |
34 | falseF :: Foreign
35 | falseF = toForeign false
36 |
37 | showF :: forall a. Show a => a -> Foreign
38 | showF a = toForeign (show a)
39 |
--------------------------------------------------------------------------------
/src/Sequelize/Query/Util.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | -- | This module is internal and not meant for consumption by users
27 | module Sequelize.Query.Util where
28 |
29 | import Prelude
30 |
31 | import Control.Monad.Aff (Aff)
32 | import Control.Monad.Eff.Exception (error)
33 | import Control.Monad.Error.Class (throwError)
34 | import Control.Monad.Except (runExcept)
35 | import Control.Promise (Promise, toAff)
36 | import Data.Either (Either(..))
37 | import Data.Foreign (Foreign, MultipleErrors, readBoolean)
38 | import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
39 | import Data.List.NonEmpty (foldMap1)
40 | import Data.Options (Options, options)
41 | import Sequelize.Class (class Model)
42 | import Sequelize.Types (Instance, ModelOf, SEQUELIZE)
43 | import Unsafe.Coerce (unsafeCoerce)
44 |
45 | coerceArrayTuple
46 | :: forall a e. Array Foreign
47 | -> Aff e {inst :: Instance a, created :: Boolean}
48 | coerceArrayTuple [f1, f2] = case runExcept $ readBoolean f2 of
49 | Right b -> pure $ {inst: unsafeCoerce f1, created: b}
50 | Left errs -> throwMultipleErrors errs
51 | coerceArrayTuple _ = throwError $ error "Could not coerce the return type"
52 |
53 | throwMultipleErrors :: forall a e. MultipleErrors -> Aff e a
54 | throwMultipleErrors errs =
55 | let errString = foldMap1 show errs
56 | in throwError $ error errString
57 |
58 | promiseToAff2 :: forall a b c d. Fn2 a Foreign (Promise b) -> a -> Options c -> Aff d b
59 | promiseToAff2 fn m o = toAff $ runFn2 fn m $ options o
60 |
61 | promiseToAff3
62 | :: forall a b c e. Model a
63 | => Fn3 (ModelOf a) Foreign c (Promise b)
64 | -> ModelOf a
65 | -> Options a
66 | -> c
67 | -> Aff ( sequelize :: SEQUELIZE | e ) b
68 | promiseToAff3 fn m o third = toAff $ runFn3 fn m (options o) third
69 |
--------------------------------------------------------------------------------
/src/Sequelize/Transaction.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | "use strict;"
27 |
28 | exports._transaction = function(sequelize){
29 | return sequelize.transaction();
30 | }
31 |
32 | exports._commitTransaction = function(transaction){
33 | return function(){
34 | transaction.commit();
35 | };
36 | }
37 |
38 | exports._rollbackTransaction = function (transaction) {
39 | return function () {
40 | transaction.rollback();
41 | };
42 | }
--------------------------------------------------------------------------------
/src/Sequelize/Transaction.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Transaction where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Control.Monad.Eff (Eff)
32 | import Control.Monad.Eff.Class (liftEff)
33 | import Control.Promise (Promise, toAff)
34 | import Sequelize.Types (Conn, SEQUELIZE, Transaction)
35 |
36 | foreign import _transaction :: Conn -> Promise Transaction
37 | foreign import _commitTransaction :: forall e. Transaction -> Eff ( sequelize :: SEQUELIZE | e ) Unit
38 | foreign import _rollbackTransaction :: forall e. Transaction -> Eff ( sequelize :: SEQUELIZE | e ) Unit
39 |
40 | startTransaction :: forall eff. Conn -> Aff ( sequelize :: SEQUELIZE | eff ) Transaction
41 | startTransaction conn = toAff <<< _transaction $ conn
42 |
43 | commitTransaction :: forall eff. Transaction -> Aff ( sequelize :: SEQUELIZE | eff ) Unit
44 | commitTransaction = liftEff <<< _commitTransaction
45 |
46 | rollbackTransaction :: forall eff. Transaction -> Aff ( sequelize :: SEQUELIZE | eff ) Unit
47 | rollbackTransaction = liftEff <<< _rollbackTransaction
--------------------------------------------------------------------------------
/src/Sequelize/Types.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 |
26 | exports["null"] = null;
27 |
--------------------------------------------------------------------------------
/src/Sequelize/Types.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Sequelize.Types where
27 |
28 | import Control.Monad.Eff (kind Effect)
29 | import Data.Foreign (Foreign)
30 | import Data.Maybe (Maybe(..))
31 | import Data.Newtype (class Newtype)
32 | import Data.Options (Options)
33 | import Data.StrMap (StrMap)
34 | import Data.String.Regex (Regex)
35 | import Data.Tuple (Tuple)
36 |
37 | foreign import null :: Foreign
38 |
39 | -- | the effect of using Sequelize
40 | foreign import data SEQUELIZE :: Effect
41 |
42 | -- | Transaction Instance Created by sequelize.transaction()
43 | foreign import data Transaction :: Type
44 |
45 | -- | The phantom type for the options object when making a SQZConn
46 | data ConnOpts
47 |
48 | -- | Represents a connection to a db, i.e. in `var sequelize = new Sequelize(str)`
49 | -- | this represents the type of `sequelize`
50 | foreign import data Conn :: Type
51 |
52 | -- used by both connections and models
53 | -- | see http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html#instance-method-sync
54 | type SyncOpts =
55 | { force :: Boolean
56 | , match :: Maybe Regex
57 | , logging :: Boolean
58 | , schema :: String
59 | , searchPath :: String
60 | , hooks :: Boolean
61 | , alter :: Boolean
62 | }
63 |
64 | defaultSyncOpts :: SyncOpts
65 | defaultSyncOpts =
66 | { force: false
67 | , match: Nothing
68 | , logging: true
69 | , schema: ""
70 | , searchPath: "DEFAULT"
71 | , hooks: true
72 | , alter: false
73 | }
74 |
75 | type ReplicationOpts =
76 | { read :: Array DbConnOpts
77 | , write :: DbConnOpts
78 | }
79 |
80 | type DbConnOpts =
81 | { host :: String
82 | , username :: String
83 | , password :: String
84 | , database :: String
85 | , port :: Int
86 | }
87 |
88 | -- for models
89 |
90 | -- | The type argument is a phantom type meant to track instances with their models
91 | foreign import data ModelOf :: Type -> Type
92 |
93 | -- | Phantom type for defining options on model columns
94 | data ColumnOpts a
95 |
96 | -- | Phantom type for defining options on models
97 | data ModelOpts a
98 |
99 | type ModelCols a = Array (Tuple String (Options (ColumnOpts a)))
100 |
101 | -- queries
102 |
103 | newtype WhereClause = WhereClause (StrMap Foreign)
104 |
105 | derive instance newtypeWhereClause :: Newtype WhereClause _
106 |
107 | -- for instances
108 |
109 | -- | The type argument is a phantom type meant to track instances with their models
110 | foreign import data Instance :: Type -> Type
111 |
112 | -- for associations
113 |
114 | newtype Alias = Alias String
115 |
116 | derive instance newtypeAlias :: Newtype Alias _
117 |
--------------------------------------------------------------------------------
/src/Sequelize/Where.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | -- | There are two different data types defined in this module that can be
27 | -- | used for 'where' clauses: `WHERE` and `Where`. `WHERE` is simpler to use,
28 | -- | and so it is not as powerful. For example:
29 | -- |
30 | -- | ```purescript
31 | -- | myWhere :: WHERE Car
32 | -- | myWhere = WHERE ["make" /\ String "BMW", "horsepower" /\ Int 600]
33 | -- | ```
34 | -- | will match any row with a make column equal to the string "BMW" and a
35 | -- | horsepower column equal to 600.
36 | -- |
37 | -- | The `Where` type is more powerful, and you generally don't need to use
38 | -- | the type constructor: just use the provided combinators.
39 | -- |
40 | -- | ```purescript
41 | -- | myWhere :: Where Car
42 | -- | myWhere = "make" $/= String "Audi" $& "horsepower" $<= 400
43 | -- | ```
44 | -- | will match any row in the table where the make isn't an Audi and the
45 | -- | horsepower is less than or equal to 400.
46 | module Sequelize.Where
47 | ( WHERE(..)
48 | , Where(..)
49 | , Term(..)
50 | , Literal(..)
51 | , (=?)
52 | , binaryAnd, (&&?)
53 | , binaryOr, (||?)
54 | , notEqS, (/=?)
55 | , eqS, (==?)
56 | , notS, (~?)
57 | , greaterThanS, (>?)
58 | , greaterThanOrEqS, (>=?)
59 | , lessThanS, ()
60 | , lessThanOrEqS, (<=?)
61 | , inS, (<-?)
62 | ) where
63 |
64 | import Data.Bifunctor (rmap)
65 | import Data.Foreign (Foreign, toForeign)
66 | import Data.Monoid (class Monoid)
67 | import Data.StrMap as Map
68 | import Data.Tuple (Tuple)
69 | import Prelude hiding (($>))
70 | import Sequelize.Types (WhereClause(..), null)
71 | import Sequelize.Class (class IsWhere)
72 |
73 | data Where a
74 | = And (Array (Where a))
75 | | Or (Array (Where a))
76 | | Is String Term
77 |
78 | instance whereIsWhere :: IsWhere Where where -- this is getting ridiculous
79 | toWhere = foldWhere
80 |
81 | instance semigroupWhere :: Semigroup (Where a) where
82 | append = binaryAnd
83 |
84 | instance monoidWhere :: Monoid (Where a) where
85 | mempty = And []
86 |
87 | newtype WHERE a = WHERE (Array (Tuple String Literal))
88 |
89 | instance wHEREIsWhere :: IsWhere WHERE where -- this looks even worse
90 | toWhere = foldWHERE
91 |
92 | foldWHERE :: forall a. WHERE a -> WhereClause
93 | foldWHERE (WHERE xs) =
94 | let clause = Map.fromFoldable $ rmap literalToForeign <$> xs
95 | in WhereClause clause
96 |
97 | data Term
98 | -- Literals
99 | = In (Array Literal)
100 | | NotIn (Array Literal)
101 | | Contains (Array Literal)
102 | | Contained (Array Literal)
103 | | Any (Array Literal)
104 | | Eq Literal
105 | | NotEq Literal
106 | | GreaterThan Literal
107 | | GreaterThanOrEq Literal
108 | | LessThan Literal
109 | | LessThanOrEq Literal
110 | -- Ints
111 | | Between (Array Int)
112 | | NotBetween (Array Int)
113 | | Overlap (Array Int)
114 | -- Strings
115 | | Like String
116 | | NotLike String
117 | | ILike String
118 | | NotILike String
119 | | RegExp String
120 | | NotRegExp String
121 | | IRegExp String
122 | | NotIRegExp String
123 | | Col String
124 | -- Booleans
125 | | Not Boolean
126 |
127 | data Literal
128 | = String String
129 | | Int Int
130 | | Number Number
131 | | Boolean Boolean
132 | | Null
133 |
134 | binaryOp :: forall a. (Array (Where a) -> Where a) -> Where a -> Where a -> Where a
135 | binaryOp f x y = f [x, y]
136 |
137 | binaryAnd :: forall a. Where a -> Where a -> Where a
138 | binaryAnd = binaryOp And
139 |
140 | binaryOr :: forall a. Where a -> Where a -> Where a
141 | binaryOr = binaryOp Or
142 |
143 | isConstructor :: forall a b. (b -> Term) -> String -> b -> Where a
144 | isConstructor ctr key val = Is key (ctr val)
145 |
146 | notEqS :: forall a. String -> Literal -> Where a
147 | notEqS = isConstructor NotEq
148 |
149 | eqS :: forall a. String -> Literal -> Where a
150 | eqS = isConstructor Eq
151 |
152 | notS :: forall a. String -> Boolean -> Where a
153 | notS = isConstructor Not
154 |
155 | greaterThanS :: forall a. String -> Literal -> Where a
156 | greaterThanS = isConstructor GreaterThan
157 |
158 | greaterThanOrEqS :: forall a. String -> Literal -> Where a
159 | greaterThanOrEqS = isConstructor GreaterThanOrEq
160 |
161 | lessThanS :: forall a. String -> Literal -> Where a
162 | lessThanS = isConstructor LessThan
163 |
164 | lessThanOrEqS :: forall a. String -> Literal -> Where a
165 | lessThanOrEqS = isConstructor LessThanOrEq
166 |
167 | inS :: forall a. String -> Array Literal -> Where a
168 | inS = isConstructor In
169 |
170 | infixr 3 binaryAnd as &&?
171 | infixr 2 binaryOr as ||?
172 | infix 4 notEqS as /=?
173 | infix 4 eqS as ==?
174 | infix 5 notS as ~?
175 | infix 8 Is as =?
176 | infixl 4 greaterThanS as >?
177 | infixl 4 greaterThanOrEqS as >=?
178 | infixl 4 lessThanS as
179 | infixl 4 lessThanOrEqS as <=?
180 | infixr 4 inS as <-?
181 |
182 | foldWhere :: forall a. Where a -> WhereClause
183 | foldWhere w = WhereClause $ foldWhere' w
184 |
185 | foldWhere' :: forall a. Where a -> Map.StrMap Foreign
186 | foldWhere' w = case w of
187 | And cs -> foldAnd cs
188 | Or cs -> foldOr cs
189 | Is key val -> foldIs key val
190 |
191 | foldAnd :: forall a. Array (Where a) -> Map.StrMap Foreign
192 | foldAnd [] = Map.empty
193 | foldAnd xs = Map.singleton "$and" (toForeign $ map foldWhere' xs)
194 |
195 | foldOr :: forall a. Array (Where a) -> Map.StrMap Foreign
196 | foldOr [] = Map.empty
197 | foldOr xs = Map.singleton "$or" (toForeign $ map foldWhere' xs)
198 |
199 | foldIs :: String -> Term -> Map.StrMap Foreign
200 | foldIs key val = Map.singleton key $ termToClause val
201 |
202 | termToClause :: Term -> Foreign
203 | termToClause = case _ of
204 | -- Literals
205 | In vals -> arrayToForeign literalToForeign "$in" vals
206 | NotIn vals -> arrayToForeign literalToForeign "$notIn" vals
207 | Contains vals -> arrayToForeign literalToForeign "$contains" vals
208 | Contained vals -> arrayToForeign literalToForeign "$contained" vals
209 | Any vals -> arrayToForeign literalToForeign "$any" vals
210 | Eq val -> literalToForeign val
211 | NotEq val -> singleToForeign literalToForeign "$ne" val
212 | GreaterThan val -> singleToForeign literalToForeign "$gt" val
213 | GreaterThanOrEq val -> singleToForeign literalToForeign "$gte" val
214 | LessThan val -> singleToForeign literalToForeign "$lt" val
215 | LessThanOrEq val -> singleToForeign literalToForeign "$lte" val
216 | -- Ints only
217 | Between vals -> arrayToForeign toForeign "$between" vals
218 | NotBetween vals -> arrayToForeign toForeign "$notBetween" vals
219 | Overlap vals -> arrayToForeign toForeign "$overlap" vals
220 | -- Strings only
221 | Like val -> singleToForeign toForeign "$like" val
222 | NotLike val -> singleToForeign toForeign "$notLike" val
223 | ILike val -> singleToForeign toForeign "$iLike" val
224 | NotILike val -> singleToForeign toForeign "$notILike" val
225 | RegExp val -> singleToForeign toForeign "$regexp" val
226 | NotRegExp val -> singleToForeign toForeign "$notRegexp" val
227 | IRegExp val -> singleToForeign toForeign "$iRegexp" val
228 | NotIRegExp val -> singleToForeign toForeign "$notIRegexp" val
229 | Col val -> singleToForeign toForeign "$col" val
230 | -- Booleans
231 | Not val -> singleToForeign toForeign "$not" val
232 |
233 | arrayToForeign :: forall a. (a -> Foreign) -> String -> Array a -> Foreign
234 | arrayToForeign f k vs = toForeign $ Map.singleton k $ map f vs
235 |
236 | singleToForeign :: forall a. (a -> Foreign) -> String -> a -> Foreign
237 | singleToForeign f k v = toForeign $ Map.singleton k $ f v
238 |
239 | literalToForeign :: Literal -> Foreign
240 | literalToForeign = case _ of
241 | String s -> toForeign s
242 | Int n -> toForeign n
243 | Number n -> toForeign n
244 | Boolean b -> toForeign b
245 | Null -> null
246 |
--------------------------------------------------------------------------------
/test/Common.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Common where
27 |
28 | import Prelude
29 |
30 | import Control.Monad.Aff (Aff)
31 | import Data.Foreign (toForeign)
32 | import Data.Foreign.Class (class Decode, class Encode)
33 | import Data.Generic.Rep (class Generic)
34 | import Data.Generic.Rep.Show (genericShow)
35 | import Data.Maybe (Maybe(..))
36 | import Data.Monoid (mempty)
37 | import Data.Options ((:=))
38 | import Data.Tuple.Nested ((/\))
39 | import Sequelize.Class (class EncodeModel, class DecodeModel, class Model, class Submodel, genericEncodeModel, genericDecodeModel)
40 | import Sequelize.Connection (Dialect(..), database, dialect, getConn, storage, syncConn)
41 | import Sequelize.Models (belongsTo, hasOne, makeModelOf)
42 | import Sequelize.Models.Columns (columnType, defaultValue)
43 | import Sequelize.Models.Types (DataType(..)) as ModelTypes
44 | import Sequelize.Types (Alias(..), Conn, ModelCols, ModelOf, SEQUELIZE, defaultSyncOpts)
45 |
46 | newtype Car = Car { model :: String, make :: String, hp :: Int }
47 | derive instance genericCar :: Generic Car _
48 | derive instance eqCar :: Eq Car
49 | instance showCar :: Show Car where
50 | show = genericShow
51 | instance decodeCar :: Decode Car where
52 | decode x = genericDecodeModel x
53 | instance encodeCar :: Encode Car where
54 | encode x = genericEncodeModel x
55 | instance decodeModelCar :: DecodeModel Car where
56 | decodeModel x = genericDecodeModel x
57 | instance encodeModelCar :: EncodeModel Car where
58 | encodeModel x = genericEncodeModel x
59 | instance isModelCar :: Model Car where
60 | modelCols = getCarCols
61 | modelName _ = "car"
62 | getCarCols :: ModelCols Car
63 | getCarCols = ["model" /\ modelOpts, "make" /\ makeOpts, "hp" /\ hpOpts]
64 | where
65 | modelOpts = columnType := ModelTypes.String {length: Nothing}
66 | makeOpts = columnType := ModelTypes.String {length: Nothing}
67 | hpOpts = columnType := ModelTypes.Integer {length: Nothing}
68 |
69 | getCarModel :: forall e. Aff (sequelize :: SEQUELIZE | e) (ModelOf Car)
70 | getCarModel = do
71 | conn <- myConn
72 | car <- makeModelOf conn mempty
73 | syncConn conn defaultSyncOpts {force = true}
74 | pure car
75 |
76 | newtype Company = Company { name :: String }
77 | derive instance genericCompany :: Generic Company _
78 | derive instance eqCompany :: Eq Company
79 | instance showCompany :: Show Company where
80 | show = genericShow
81 | instance decodeCompany :: Decode Company where
82 | decode x = genericDecodeModel x
83 | instance encodeCompany :: Encode Company where
84 | encode x = genericEncodeModel x
85 | instance decodeModelCompany :: DecodeModel Company where
86 | decodeModel x = genericDecodeModel x
87 | instance encodeModelCompany :: EncodeModel Company where
88 | encodeModel x = genericEncodeModel x
89 | instance isModelCompany :: Model Company where
90 | modelCols = getCompanyCols
91 | modelName _ = "company"
92 | getCompanyCols :: ModelCols Company
93 | getCompanyCols = ["name" /\ nameOpts]
94 | where
95 | nameOpts =
96 | columnType := ModelTypes.String {length: Nothing} <>
97 | defaultValue := toForeign "ACME Co"
98 |
99 | newtype User = User { name :: String }
100 | derive instance eqUser :: Eq User
101 | derive instance genericUser :: Generic User _
102 | instance showUser :: Show User where
103 | show = genericShow
104 | instance decodeUser :: Decode User where
105 | decode x = genericDecodeModel x
106 | instance encodeUser :: Encode User where
107 | encode x = genericEncodeModel x
108 | instance encodeModelUser :: EncodeModel User where
109 | encodeModel x = genericEncodeModel x
110 | instance decodeModelUser :: DecodeModel User where
111 | decodeModel x = genericDecodeModel x
112 | instance isModelUser :: Model User where
113 | modelCols = userCols
114 | modelName _ = "user"
115 | userCols :: ModelCols User
116 | userCols = ["name" /\ nameOpts]
117 | where
118 | nameOpts =
119 | columnType := ModelTypes.String {length: Nothing} <>
120 | defaultValue := toForeign "me"
121 |
122 | newtype SuperUser = SuperUser { name :: String, employerId :: Int }
123 | derive instance eqSuperUser :: Eq SuperUser
124 | derive instance genericSuperUser :: Generic SuperUser _
125 | instance showSuperUser :: Show SuperUser where
126 | show = genericShow
127 | instance decodeSuperUser :: Decode SuperUser where
128 | decode x = genericDecodeModel x
129 | instance encodeSuperUser :: Encode SuperUser where
130 | encode x = genericEncodeModel x
131 | instance encodeModelSuperUser :: EncodeModel SuperUser where
132 | encodeModel x = genericEncodeModel x
133 | instance decodeModelSuperUser :: DecodeModel SuperUser where
134 | decodeModel x = genericDecodeModel x
135 | instance isModelSuperUser :: Model SuperUser where
136 | modelCols = superUserCols
137 | modelName _ = "superUser"
138 | superUserCols :: ModelCols SuperUser
139 | superUserCols = ["name" /\ nameOpts]
140 | where
141 | nameOpts =
142 | columnType := ModelTypes.String {length: Nothing} <>
143 | defaultValue := toForeign "me"
144 |
145 | instance userSubSuper :: Submodel User SuperUser where
146 | project (SuperUser {name}) = User {name}
147 |
148 | myConn :: forall e. Aff (sequelize :: SEQUELIZE | e) Conn
149 | myConn = getConn opts
150 | where
151 | opts = database := "thunder"
152 | <> dialect := SQLite
153 | <> storage := "./test.sqlite"
154 |
155 | getUserAndCompany
156 | :: forall e
157 | .
158 | Aff ( sequelize :: SEQUELIZE | e )
159 | { company :: ModelOf Company
160 | , user :: ModelOf User
161 | }
162 | getUserAndCompany = do
163 | conn <- myConn
164 | company <- makeModelOf conn mempty
165 | user <- makeModelOf conn mempty
166 | user `belongsTo` company $ Alias "employer"
167 | company `hasOne` user $ Alias "employee"
168 | syncConn conn defaultSyncOpts {force = true}
169 | pure {company, user}
170 |
--------------------------------------------------------------------------------
/test/Prelude.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Prelude (AffTest, EffTest, TestAff, TestEff, module Exports) where
27 |
28 | import Control.Monad.Aff (Aff, launchAff) as Exports
29 | import Control.Monad.Aff.Console (log, logShow) as Exports
30 | import Control.Monad.Eff (Eff) as Exports
31 | import Control.Monad.Eff.Console (CONSOLE) as Exports
32 | import Control.Monad.Eff.Exception (EXCEPTION) as Exports
33 | import Control.Monad.Except (runExcept) as Exports
34 | import Data.Array (catMaybes) as Exports
35 | import Data.Either (Either(..), either) as Exports
36 | import Data.Foreign (F, Foreign, ForeignError(..), readInt, fail, toForeign) as Exports
37 | import Data.Maybe (Maybe(..), maybe) as Exports
38 | import Data.Monoid (class Monoid, mempty, power) as Exports
39 | import Data.Options ((:=)) as Exports
40 | import Data.Traversable (class Foldable, class Traversable, Accum, all, and, any, elem, find, fold, foldMap, foldMapDefaultL, foldMapDefaultR, foldl, foldlDefault, foldr, foldrDefault, for, for_, intercalate, mapAccumL, mapAccumR, maximum, maximumBy, minimum, minimumBy, notElem, oneOf, or, product, scanl, scanr, sequence, sequenceDefault, sequence_, sum, traverse, traverseDefault, traverse_) as Exports
41 | import Data.Tuple.Nested ((/\)) as Exports
42 | import Prelude (class Applicative, class Apply, class Bind, class BooleanAlgebra, class Bounded, class Category, class CommutativeRing, class Discard, class DivisionRing, class Eq, class EuclideanRing, class Field, class Functor, class HeytingAlgebra, class Monad, class Ord, class Ring, class Semigroup, class Semigroupoid, class Semiring, class Show, type (~>), Ordering(EQ, GT, LT), Unit, Void, absurd, add, ap, append, apply, between, bind, bottom, clamp, compare, comparing, compose, conj, const, degree, discard, disj, div, eq, flap, flip, gcd, id, ifM, join, lcm, liftA1, liftM1, map, mod, mul, negate, not, notEq, one, otherwise, pure, recip, show, sub, top, unit, unless, unlessM, void, when, whenM, zero, (#), ($), (&&), (*), (*>), (+), (-), (/), (/=), (<), (<#>), (<$), (<$>), (<*), (<*>), (<<<), (<=), (<=<), (<>), (<@>), (=<<), (==), (>), (>=), (>=>), (>>=), (>>>), (||)) as Exports
43 | import Sequelize.CRUD.Create (build, bulkCreate, create, save) as Exports
44 | import Sequelize.CRUD.Destroy (destroy, delete) as Exports
45 | import Sequelize.CRUD.Read (count, findAll, findAndCountAll, findById, findByIntId, findByStringId, findOne, findOrBuild, findOrCreate, max, min, query', query'') as Exports
46 | import Sequelize.CRUD.Update (decrement, increment, update, updateModel) as Exports
47 | import Sequelize.Instance (instanceToModel, instanceToModelE, peek, (==!)) as Exports
48 | import Sequelize.Query.Options (attributes, include, include1, include2, include3, include4, include5, include6, include7, include8, include9, limit, offset, paranoid, searchPath, where_) as Exports
49 | import Sequelize.Types (Alias(Alias), ColumnOpts, Conn, ConnOpts, Instance, ModelCols, ModelOf, ModelOpts, SEQUELIZE, WhereClause(WhereClause)) as Exports
50 | import Sequelize.Where (Literal(Int, Null, String), Term(Any, Between, Col, Contained, Contains, Eq, GreaterThan, GreaterThanOrEq, ILike, IRegExp, In, LessThan, LessThanOrEq, Like, Not, NotBetween, NotEq, NotILike, NotIRegExp, NotIn, NotLike, NotRegExp, Overlap, RegExp), WHERE(WHERE), Where(Is), binaryAnd, binaryOr, eqS, greaterThanOrEqS, greaterThanS, inS, lessThanOrEqS, lessThanS, notEqS, notS, (&&?), (/=?), (), (<-?), (<=?), (=?), (==?), (>?), (>=?), (||?), (~?)) as Exports
51 | import Test.Common (Company(..), User(..), SuperUser(..), Car(..), myConn, getUserAndCompany, getCarModel) as Exports
52 |
53 | type TestAff e = (console :: Exports.CONSOLE, sequelize :: Exports.SEQUELIZE | e)
54 | type TestEff e = TestAff (exception :: Exports. EXCEPTION | e)
55 | type AffTest e a = Exports.Aff (TestAff e) a
56 | type EffTest e a = Exports.Eff (TestEff e) a
57 |
--------------------------------------------------------------------------------
/test/Test.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Main where
27 |
28 | import Test.Prelude
29 |
30 | import Test.Association as Association
31 | import Test.Connection as Connection
32 | import Test.Insertion as Insertion
33 | import Test.Instance as Instance
34 | import Test.Query as Query
35 | import Test.Where as Where
36 | import Test.Free as Free
37 |
38 | main :: EffTest () Unit
39 | main = do
40 | Association.main
41 | Connection.main
42 | Insertion.main
43 | Query.main
44 | Where.main
45 | Free.main
46 | Instance.main
47 |
--------------------------------------------------------------------------------
/test/js/associations.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 | /*jshint esversion: 6 */
26 |
27 | const Sequelize = require('sequelize');
28 | const sequelize = new Sequelize({
29 | database: "thunder",
30 | dialect: "sqlite",
31 | storage: "./test.sqlite"
32 | });
33 |
34 | const company = sequelize.define("company", {
35 | name: {
36 | type: Sequelize.STRING(255),
37 | defaultValue: "ACME Co"
38 | }},
39 | {}
40 | );
41 | const user = sequelize.define("user", {
42 | name: {
43 | type: Sequelize.STRING(255),
44 | defaultValue: "me"
45 | }},
46 | {}
47 | );
48 |
49 | user.belongsTo(company, {as: "employer"});
50 | company.hasOne(user, {as: "employee"});
51 |
52 | sequelize.sync({force: true}).then(() => {
53 | company.create({name: "ACME Co"})
54 | .then((company1) => {
55 | myCompany = company1;
56 | return user.create({name: "Me", employerId: myCompany.id});
57 | })
58 | .then((u) => {
59 | const userInst = user.findOne({
60 | where: {
61 | employerId: myCompany.id
62 | },
63 | include: [{model: company, as: "employer"}]
64 | })
65 | .then((u) => {
66 | console.log(JSON.stringify(u));
67 | });
68 | return userInst;
69 | });
70 | });
71 |
72 |
--------------------------------------------------------------------------------
/test/js/query.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | * JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 | *
5 | * This file is part of JUSPAY Platform.
6 | *
7 | * JUSPAY Platform is free software: you can redistribute it and/or modify
8 | * it for only educational purposes under the terms of the GNU Affero General
9 | * Public License (GNU AGPL) as published by the Free Software Foundation,
10 | * either version 3 of the License, or (at your option) any later version.
11 | * For Enterprise/Commerical licenses, contact .
12 | *
13 | * This program is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | * be liable for all damages without limitation, which is caused by the
17 | * ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | * damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | * The end user has NO right to claim any indemnification based on its use
20 | * of Licensed Software. See the GNU Affero General Public License for more details.
21 | *
22 | * You should have received a copy of the GNU Affero General Public License
23 | * along with this program. If not, see .
24 | */
25 | /*jshint esversion: 6 */
26 |
27 | const Sequelize = require('sequelize');
28 | const sequelize = new Sequelize({
29 | database: "thunder",
30 | dialect: "sqlite",
31 | storage: "./test.sqlite"
32 | });
33 |
34 | const company = sequelize.define("company", {
35 | name: {
36 | type: Sequelize.STRING(255),
37 | defaultValue: "ACME Co"
38 | }},
39 | {}
40 | );
41 | const user = sequelize.define("user", {
42 | name: {
43 | type: Sequelize.STRING(255),
44 | defaultValue: "me"
45 | }},
46 | {}
47 | );
48 |
49 | user.belongsTo(company, {as: "employer"});
50 | company.hasOne(user, {as: "employee"});
51 |
52 | sequelize.sync({force: true}).then(() => {
53 | user.findOrCreate({
54 | where: {
55 | name: "default"
56 | }})
57 | .then(() => {
58 | return user.findOrCreate({
59 | where: {
60 | name: "default"
61 | }});
62 | })
63 | .then(function (arr) {
64 | console.log(arr[1]);
65 | })
66 | .catch(console.error);
67 | });
68 |
--------------------------------------------------------------------------------
/test/passing/Association.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Association where
27 |
28 | import Test.Prelude
29 |
30 | main :: EffTest () Unit
31 | main = void $ launchAff associationTest
32 |
33 | associationTest :: AffTest () Unit
34 | associationTest = do
35 | {company, user} <- getUserAndCompany
36 | c1 <- create company $ Company {name: "ACME Co"}
37 | let companyId = either (const 0) id $ runExcept do
38 | f <- peek c1 "id"
39 | readInt f
40 | su <- create user $ SuperUser {name: "Me", employerId: companyId}
41 | userInst :: Maybe (Instance SuperUser) <- findOne user $
42 | where_ := WHERE ["employerId" /\ Int companyId] <>
43 | include := {model: company, as: Alias "employer"}
44 | case userInst of
45 | Just ui -> either logShow logShow $ instanceToModelE ui
46 | _ -> log "Error: no instance found"
47 |
--------------------------------------------------------------------------------
/test/passing/Connection.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Connection where
27 |
28 | import Test.Prelude
29 |
30 | import Sequelize.Connection (getConn, username, database, password, dialect, Dialect(..), storage, authenticate)
31 |
32 | main :: forall e. Eff (console :: CONSOLE, sequelize :: SEQUELIZE, exception :: EXCEPTION | e) Unit
33 | main = void $ launchAff do
34 | conn <- getConn opts
35 | authenticate conn
36 | where
37 | opts = username := "username"
38 | <> password := "also username"
39 | <> database := "thunder"
40 | <> dialect := SQLite
41 | <> storage := "./test.sqlite"
42 |
--------------------------------------------------------------------------------
/test/passing/Free.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Free where
27 |
28 | import Test.Prelude
29 |
30 | import Data.Foreign (toForeign)
31 | import Data.Options (Options(..))
32 | import Data.StrMap as Map
33 | import Data.String (singleton)
34 | import Data.Tuple (Tuple(..))
35 | import Sequelize.Free as SQL
36 |
37 | type App a = SQL.CRUD Car Car a
38 |
39 | enterprise :: Char -> Car
40 | enterprise c
41 | = Car
42 | { make: "Federation of Planets"
43 | , model: "Enterprise " <> singleton c
44 | , hp: 1000000
45 | }
46 |
47 | voyager :: Car
48 | voyager
49 | = Car
50 | { make: "Federation of Planets"
51 | , model: "Voyager"
52 | , hp: 500000
53 | }
54 |
55 | defiant :: Car
56 | defiant
57 | = Car
58 | { make: "Federation of Planets"
59 | , model: "Defiant"
60 | , hp: 100000
61 | }
62 |
63 | updateOpts = Options
64 | [ "model" /\ (toForeign "Defiant")
65 | , "hp" /\ (toForeign 100000) ]
66 |
67 |
68 | spaceships :: Array Car
69 | spaceships = [enterprise 'A', voyager]
70 |
71 | testC :: App Unit
72 | testC = do
73 | i <- SQL.build $ enterprise 'D'
74 | _ <- SQL.save i
75 | _ <- SQL.create $ enterprise 'E'
76 | SQL.bulkCreate spaceships
77 |
78 | testR1 :: App (F Car)
79 | testR1 = do
80 | i <- SQL.findOne $ where_ := "model" =? Like "Enterprise%"
81 | pure case i of
82 | Just inst -> instanceToModel inst
83 | _ -> fail $ ForeignError "Badly formed findOne query, or unpopulated database"
84 |
85 | testR2 :: App Int
86 | testR2 = SQL.count $ where_ := "model" =? Like "Enterprise%"
87 |
88 | testR3 :: App (F Foreign)
89 | testR3 = do
90 | i <- SQL.findByInt 900
91 | pure case i of
92 | Just inst -> peek inst "propertyDoesNotExist"
93 | _ -> pure $ toForeign unit
94 |
95 | testU1 :: App Unit
96 | testU1 = do
97 | i <- SQL.create $ enterprise 'F'
98 | SQL.increment i (Map.singleton "hp" 1000000)
99 |
100 | testU2 :: App Unit
101 | testU2 = void $ -- changes voyager to the defiant
102 | SQL.updateModel updateOpts (where_ := WHERE ["model" /\ String "Voyager"])
103 |
104 | testD :: App Unit
105 | testD = do
106 | i <- SQL.create $ enterprise 'Q'
107 | SQL.destroy i -- there is no Enterprise Q!
108 |
109 | testCRUD :: App Unit
110 | testCRUD = do
111 | testC
112 | void testR1
113 | void testR2
114 | void testR3
115 | testU1
116 | testU2
117 | testD
118 |
119 | main :: EffTest () Unit
120 | main = void $ launchAff do
121 | m <- getCarModel
122 | SQL.runCRUD m testCRUD
123 |
--------------------------------------------------------------------------------
/test/passing/Insertion.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Insertion where
27 |
28 | import Data.Options
29 | import Test.Prelude
30 |
31 | import Control.Monad.Trampoline (done)
32 | import Data.Foreign (toForeign)
33 | import Data.StrMap as Map
34 | import Data.Tuple (Tuple(..))
35 |
36 | audi :: Car
37 | audi = Car {make: "Audi", model: "Audi Q7", hp: 333}
38 |
39 | honda :: Car
40 | honda = Car {make: "Honda", model: "Civic", hp: 306}
41 |
42 | noCar :: Car
43 | noCar = Car {make: "None", model: "None", hp: 0}
44 |
45 | -- note: execution order is not guaranteed
46 | main :: EffTest () Unit
47 | main = void $ launchAff do
48 | carModel <- getCarModel
49 | audiInstance <- buildAndSaveTest carModel
50 | hondaInstance <- createTest carModel
51 | updateTest audiInstance
52 | destroyTest audiInstance
53 | deleteTest carModel
54 |
55 | -- net result should be +5
56 | incrementTest hondaInstance
57 | decrementTest hondaInstance
58 |
59 | bulkCar <- bulkCreateTest carModel
60 |
61 | updateModelTest carModel
62 |
63 | createTest :: ModelOf Car -> AffTest () (Instance Car)
64 | createTest carModel = create carModel honda
65 |
66 | buildAndSaveTest :: ModelOf Car -> AffTest () (Instance Car)
67 | buildAndSaveTest carModel = do
68 | let audiInstance = build carModel audi
69 | save audiInstance
70 |
71 | updateTest :: Instance Car -> AffTest () Unit
72 | updateTest audiInstance = update audiInstance honda
73 |
74 | destroyTest :: Instance Car -> AffTest () Unit
75 | destroyTest = destroy
76 |
77 | deleteTest :: ModelOf Car -> AffTest () Unit
78 | deleteTest carModel = do
79 | x <- delete carModel (where_ := WHERE ["make" /\ String "Honda"])
80 | logShow x.affectedCount
81 | log case x.affectedCount of
82 | 0 -> "No record found"
83 | _ -> "Deleted " <> show x.affectedCount <> " records."
84 |
85 | incrementTest :: Instance Car -> AffTest () Unit
86 | incrementTest inst = increment inst $ Map.fromFoldable [Tuple "hp" 15]
87 |
88 | decrementTest :: Instance Car -> AffTest () Unit
89 | decrementTest inst = decrement inst $ Map.fromFoldable [Tuple "hp" 10]
90 |
91 | bulkCreateTest :: ModelOf Car -> AffTest () (Array (Instance Car))
92 | bulkCreateTest carModel = bulkCreate carModel [audi, honda]
93 |
94 | updateModelTest :: ModelOf Car -> AffTest () Unit
95 | updateModelTest carModel = do
96 | x <- updateModel carModel updateOpts opts
97 | logShow x.affectedCount
98 | log case x.affectedRows of
99 | Nothing -> "Nothing"
100 | _ -> "This shouldn't log since we're testing with SQLite!"
101 | where
102 | opts = where_ := WHERE ["make" /\ String "Honda"]
103 | updateOpts = Options [ "model" /\ (toForeign "testModel") ]
104 |
--------------------------------------------------------------------------------
/test/passing/Instance.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Instance where
27 |
28 | import Test.Prelude
29 |
30 | audi :: Car
31 | audi = Car {make: "Audi", model: "Audi Q7", hp: 333}
32 |
33 | honda :: Car
34 | honda = Car {make: "Honda", model: "Civic", hp: 306}
35 |
36 | main :: EffTest () Unit
37 | main = void $ launchAff do
38 | carModel <- getCarModel
39 | let a = build carModel audi
40 | a1 <- save a
41 | h1 <- create carModel honda
42 | a2 <- create carModel audi
43 |
44 | log "Testing if two different audi instances are eq:"
45 | logShow $ a1 ==! a2
46 | log "Testing if an audi instance is eq to a honda instance:"
47 | logShow $ h1 ==! a1
48 | log "Testing if an audi instance is eq to itself:"
49 | logShow $ a1 ==! a1
50 | log "Testing if a built audi instance is eq to its saved result:"
51 | logShow $ a ==! a1
52 |
--------------------------------------------------------------------------------
/test/passing/Query.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Query where
27 |
28 | import Test.Prelude
29 |
30 | import Control.Monad.Aff (attempt)
31 | import Data.StrMap as StrMap
32 |
33 | z4 :: Car
34 | z4 = Car {make: "BMW", model: "Z4", hp: 335}
35 |
36 | m4 :: Car
37 | m4 = Car {make: "BMW", model: "M4", hp: 444}
38 |
39 | m6 :: Car
40 | m6 = Car {make: "BMW", model: "M6", hp: 600}
41 |
42 | main :: EffTest () Unit
43 | main = void $ launchAff do
44 | conn <- myConn
45 | carModel <- getCarModel
46 | _ <- bulkCreate carModel [z4, m4, m6]
47 | findByIdTest carModel
48 | findOneTest carModel
49 | findOrCreateTest carModel
50 | findAndCountAllTest carModel
51 | countTest carModel
52 | findAllTest carModel
53 | maxTest carModel
54 | maxMissingTest carModel
55 | minTest carModel
56 | rawQuery conn
57 | rawsQueryWithRep conn
58 |
59 | findByIdTest :: ModelOf Car -> AffTest () Unit
60 | findByIdTest carModel = do
61 | maybeZ4 :: Maybe (Instance Car) <- findById carModel (Right 1)
62 | maybeM4 <- findByIntId carModel 2
63 | maybeM6 <- findById carModel (Right 3)
64 | let cars = traverse instanceToModelE $ catMaybes [maybeZ4, maybeM4, maybeM6]
65 | either logShow logShow cars
66 |
67 | findOneTest :: ModelOf Car -> AffTest () Unit
68 | findOneTest carModel = do
69 | maybeZ4 :: Maybe (Instance Car) <- findOne carModel $ where_ := WHERE ["model" /\ String "Z4"]
70 | case maybeZ4 of
71 | Just reallyZ4 -> either logShow logShow $ instanceToModelE reallyZ4
72 | _ -> log "Error: either bad findOne query or unpopulated database"
73 |
74 | findOrCreateTest :: ModelOf Car -> AffTest () Unit
75 | findOrCreateTest carModel = do
76 | {inst} <- findOrCreate carModel $
77 | where_ := WHERE ["model" /\ String "M4"]
78 | either logShow logShow $ instanceToModelE (inst :: Instance Car)
79 |
80 | findAndCountAllTest :: ModelOf Car -> AffTest () Unit
81 | findAndCountAllTest carModel = do
82 | {count} <- findAndCountAll carModel $ where_ := whereCar
83 | logShow count -- should be 3
84 | where
85 | whereCar :: WHERE Car
86 | whereCar = WHERE ["make" /\ String "BMW"]
87 |
88 | countTest :: ModelOf Car -> AffTest () Unit
89 | countTest carModel = do
90 | n <- count carModel $
91 | where_ := ("hp" >=? Int 400)
92 | logShow n -- should be 2
93 |
94 | findAllTest :: ModelOf Car -> AffTest () Unit
95 | findAllTest carModel = do
96 | arr :: Array (Instance Car) <- findAll carModel mempty
97 | either logShow logShow $ traverse instanceToModelE arr
98 |
99 | maxTest :: ModelOf Car -> AffTest () Unit
100 | maxTest carModel = do
101 | count <- max carModel mempty "hp"
102 | logShow count -- should be 600
103 |
104 | minTest :: ModelOf Car -> AffTest () Unit
105 | minTest carModel = do
106 | count <- min carModel mempty "hp"
107 | logShow count -- should be 335
108 |
109 | maxMissingTest :: ModelOf Car -> AffTest () Unit
110 | maxMissingTest carModel = do
111 | n <- attempt $ max carModel mempty "propertyDoesNotExist"
112 | log "It succeeds if it prints a Left value to console"
113 | logShow n
114 |
115 | rawQuery :: Conn -> AffTest () Unit
116 | rawQuery conn = do
117 | void $ query' conn "SELECT * FROM cars LIMIT 1;"
118 |
119 | rawsQueryWithRep :: Conn -> AffTest () Unit
120 | rawsQueryWithRep conn = do
121 | void $ query'' conn "SELECT * FROM cars where make = :make" replacements StrMap.empty
122 | where
123 | replacements =
124 | StrMap.singleton "make" (toForeign "BMW")
125 |
--------------------------------------------------------------------------------
/test/passing/Where.purs:
--------------------------------------------------------------------------------
1 | {-
2 | Copyright (c) 2012-2017 "JUSPAY Technologies"
3 | JUSPAY Technologies Pvt. Ltd. [https://www.juspay.in]
4 |
5 | This file is part of JUSPAY Platform.
6 |
7 | JUSPAY Platform is free software: you can redistribute it and/or modify
8 | it for only educational purposes under the terms of the GNU Affero General
9 | Public License (GNU AGPL) as published by the Free Software Foundation,
10 | either version 3 of the License, or (at your option) any later version.
11 | For Enterprise/Commerical licenses, contact .
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The end user will
16 | be liable for all damages without limitation, which is caused by the
17 | ABUSE of the LICENSED SOFTWARE and shall INDEMNIFY JUSPAY for such
18 | damages, claims, cost, including reasonable attorney fee claimed on Juspay.
19 | The end user has NO right to claim any indemnification based on its use
20 | of Licensed Software. See the GNU Affero General Public License for more details.
21 |
22 | You should have received a copy of the GNU Affero General Public License
23 | along with this program. If not, see .
24 | -}
25 |
26 | module Test.Where where
27 |
28 | import Test.Prelude
29 |
30 | main :: EffTest () Unit
31 | main = void $ launchAff testWhere
32 |
33 | testWhere :: AffTest () Unit
34 | testWhere = do
35 | carModel <- getCarModel
36 | _ <- create carModel (Car {make: "Ford", model: "T", hp: 20})
37 | modelT :: Maybe (Instance Car) <- findOne carModel $ where_ := ("hp" >=? Int 20)
38 | either logShow logShow $ traverse instanceToModelE modelT
39 |
--------------------------------------------------------------------------------